utils: only enable extended lookup if section looks like it
[project/iwinfo.git] / 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.h"
22 #include "api/broadcom.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 int wl_iovar(const char *name, const char *cmd, const char *arg,
41 int arglen, void *buf, int buflen)
42 {
43 int cmdlen = strlen(cmd) + 1;
44
45 memcpy(buf, cmd, cmdlen);
46
47 if (arg && arglen > 0)
48 memcpy(buf + cmdlen, arg, arglen);
49
50 return wl_ioctl(name, WLC_GET_VAR, buf, buflen);
51 }
52
53 static struct wl_maclist * wl_read_assoclist(const char *ifname)
54 {
55 struct wl_maclist *macs;
56 int maclen = 4 + WL_MAX_STA_COUNT * 6;
57
58 if (strstr(ifname, "wds"))
59 return NULL;
60
61 if ((macs = (struct wl_maclist *) malloc(maclen)) != NULL)
62 {
63 memset(macs, 0, maclen);
64 macs->count = WL_MAX_STA_COUNT;
65
66 if (!wl_ioctl(ifname, WLC_GET_ASSOCLIST, macs, maclen))
67 return macs;
68
69 free(macs);
70 }
71
72 return NULL;
73 }
74
75
76 static int wl_probe(const char *ifname)
77 {
78 int magic;
79 return (!wl_ioctl(ifname, WLC_GET_MAGIC, &magic, sizeof(magic)) &&
80 (magic == WLC_IOCTL_MAGIC));
81 }
82
83 static void wl_close(void)
84 {
85 /* Nop */
86 }
87
88 static int wl_get_mode(const char *ifname, int *buf)
89 {
90 int ret = -1;
91 int ap, infra, passive;
92
93 if ((ret = wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap))))
94 return ret;
95
96 if ((ret = wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra))))
97 return ret;
98
99 if ((ret = wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive))))
100 return ret;
101
102 if (passive)
103 *buf = IWINFO_OPMODE_MONITOR;
104 else if (!infra)
105 *buf = IWINFO_OPMODE_ADHOC;
106 else if (ap)
107 *buf = IWINFO_OPMODE_MASTER;
108 else
109 *buf = IWINFO_OPMODE_CLIENT;
110
111 return 0;
112 }
113
114 static int wl_get_ssid(const char *ifname, char *buf)
115 {
116 int ret = -1;
117 wlc_ssid_t ssid;
118
119 if (!(ret = wl_ioctl(ifname, WLC_GET_SSID, &ssid, sizeof(ssid))))
120 memcpy(buf, ssid.ssid, ssid.ssid_len);
121
122 return ret;
123 }
124
125 static int wl_get_bssid(const char *ifname, char *buf)
126 {
127 int ret = -1;
128 char bssid[6];
129
130 if (!(ret = wl_ioctl(ifname, WLC_GET_BSSID, bssid, 6)))
131 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
132 (uint8_t)bssid[0], (uint8_t)bssid[1], (uint8_t)bssid[2],
133 (uint8_t)bssid[3], (uint8_t)bssid[4], (uint8_t)bssid[5]
134 );
135
136 return ret;
137 }
138
139 static int wl_get_channel(const char *ifname, int *buf)
140 {
141 return wl_ioctl(ifname, WLC_GET_CHANNEL, buf, sizeof(buf));
142 }
143
144 static int wl_get_frequency(const char *ifname, int *buf)
145 {
146 return wext_ops.frequency(ifname, buf);
147 }
148
149 static int wl_get_txpower(const char *ifname, int *buf)
150 {
151 /* WLC_GET_VAR "qtxpower" */
152 return wext_ops.txpower(ifname, buf);
153 }
154
155 static int wl_get_bitrate(const char *ifname, int *buf)
156 {
157 int ret = -1;
158 int rate = 0;
159
160 if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
161 *buf = ((rate / 2) * 1000) + ((rate & 1) ? 500 : 0);
162
163 return ret;
164 }
165
166 static int wl_get_signal(const char *ifname, int *buf)
167 {
168 unsigned int ap, rssi, i, rssi_count;
169 int ioctl_req_version = 0x2000;
170 char tmp[WLC_IOCTL_MAXLEN];
171 struct wl_maclist *macs = NULL;
172 wl_sta_rssi_t starssi;
173
174 memset(tmp, 0, WLC_IOCTL_MAXLEN);
175 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
176
177 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
178
179 if (!wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap)
180 {
181 *buf = tmp[WL_BSS_RSSI_OFFSET];
182 }
183 else
184 {
185 rssi = rssi_count = 0;
186
187 /* Calculate average rssi from conntected stations */
188 if ((macs = wl_read_assoclist(ifname)) != NULL)
189 {
190 for (i = 0; i < macs->count; i++)
191 {
192 memcpy(starssi.mac, &macs->ea[i], 6);
193
194 if (!wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12))
195 {
196 rssi -= starssi.rssi;
197 rssi_count++;
198 }
199 }
200
201 free(macs);
202 }
203
204 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
205 }
206
207 return 0;
208 }
209
210 static int wl_get_noise(const char *ifname, int *buf)
211 {
212 unsigned int ap, noise;
213 int ioctl_req_version = 0x2000;
214 char tmp[WLC_IOCTL_MAXLEN];
215
216 memset(tmp, 0, WLC_IOCTL_MAXLEN);
217 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
218
219 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
220
221 if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
222 {
223 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
224 noise = 0;
225 }
226 else
227 {
228 noise = tmp[WL_BSS_NOISE_OFFSET];
229 }
230
231 *buf = noise;
232
233 return 0;
234 }
235
236 static int wl_get_quality(const char *ifname, int *buf)
237 {
238 return wext_ops.quality(ifname, buf);
239 }
240
241 static int wl_get_quality_max(const char *ifname, int *buf)
242 {
243 return wext_ops.quality_max(ifname, buf);
244 }
245
246 static int wl_get_encryption(const char *ifname, char *buf)
247 {
248 uint32_t wsec, wauth, wpa;
249 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
250
251 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
252 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) ||
253 wl_ioctl(ifname, WLC_GET_AUTH, &wauth, sizeof(uint32_t)) )
254 return -1;
255
256 switch(wsec)
257 {
258 case 2:
259 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
260 break;
261
262 case 4:
263 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
264 break;
265
266 case 6:
267 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
268 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
269 break;
270 }
271
272 switch(wpa)
273 {
274 case 0:
275 if (wsec && !wauth)
276 c->auth_algs |= IWINFO_AUTH_OPEN;
277
278 else if (wsec && wauth)
279 c->auth_algs |= IWINFO_AUTH_SHARED;
280
281 /* ToDo: evaluate WEP key lengths */
282 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
283 c->auth_suites |= IWINFO_KMGMT_NONE;
284 break;
285
286 case 2:
287 c->wpa_version = 1;
288 c->auth_suites |= IWINFO_KMGMT_8021x;
289 break;
290
291 case 4:
292 c->wpa_version = 1;
293 c->auth_suites |= IWINFO_KMGMT_PSK;
294 break;
295
296 case 32:
297 case 64:
298 c->wpa_version = 2;
299 c->auth_suites |= IWINFO_KMGMT_8021x;
300 break;
301
302 case 66:
303 c->wpa_version = 3;
304 c->auth_suites |= IWINFO_KMGMT_8021x;
305 break;
306
307 case 128:
308 c->wpa_version = 2;
309 c->auth_suites |= IWINFO_KMGMT_PSK;
310 break;
311
312 case 132:
313 c->wpa_version = 3;
314 c->auth_suites |= IWINFO_KMGMT_PSK;
315 break;
316
317 default:
318 break;
319 }
320
321 c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
322 c->group_ciphers = c->pair_ciphers;
323
324 return 0;
325 }
326
327 static int wl_get_phyname(const char *ifname, char *buf)
328 {
329 char *p;
330
331 strcpy(buf, ifname);
332
333 if ((p = strchr(buf, '.')) != NULL)
334 *p = 0;
335
336 return 0;
337 }
338
339 static int wl_get_enctype(const char *ifname, char *buf)
340 {
341 uint32_t wsec, wpa;
342 char algo[11];
343
344 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
345 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
346 return -1;
347
348 switch(wsec)
349 {
350 case 2:
351 sprintf(algo, "TKIP");
352 break;
353
354 case 4:
355 sprintf(algo, "CCMP");
356 break;
357
358 case 6:
359 sprintf(algo, "TKIP, CCMP");
360 break;
361 }
362
363 switch(wpa)
364 {
365 case 0:
366 sprintf(buf, "%s", wsec ? "WEP" : "None");
367 break;
368
369 case 2:
370 sprintf(buf, "WPA 802.1X (%s)", algo);
371 break;
372
373 case 4:
374 sprintf(buf, "WPA PSK (%s)", algo);
375 break;
376
377 case 32:
378 sprintf(buf, "802.1X (%s)", algo);
379 break;
380
381 case 64:
382 sprintf(buf, "WPA2 802.1X (%s)", algo);
383 break;
384
385 case 66:
386 sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
387 break;
388
389 case 128:
390 sprintf(buf, "WPA2 PSK (%s)", algo);
391 break;
392
393 case 132:
394 sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
395 break;
396
397 default:
398 sprintf(buf, "Unknown");
399 }
400
401 return 0;
402 }
403
404 static void wl_get_assoclist_cb(const char *ifname,
405 struct iwinfo_assoclist_entry *e)
406 {
407 wl_sta_info_t sta = { 0 };
408
409 if (!wl_iovar(ifname, "sta_info", e->mac, 6, &sta, sizeof(sta)) &&
410 (sta.ver >= 2))
411 {
412 e->inactive = sta.idle * 1000;
413 e->rx_packets = sta.rx_ucast_pkts;
414 e->tx_packets = sta.tx_pkts;
415 e->rx_rate.rate = sta.rx_rate;
416 e->tx_rate.rate = sta.tx_rate;
417
418 /* ToDo: 11n */
419 e->rx_rate.mcs = -1;
420 e->tx_rate.mcs = -1;
421 }
422 }
423
424 static int wl_get_assoclist(const char *ifname, char *buf, int *len)
425 {
426 int i, j, noise;
427 int ap, infra, passive;
428 char line[128];
429 char macstr[18];
430 char devstr[IFNAMSIZ];
431 struct wl_maclist *macs;
432 struct wl_sta_rssi rssi;
433 struct iwinfo_assoclist_entry entry;
434 FILE *arp;
435
436 ap = infra = passive = 0;
437
438 wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
439 wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
440 wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
441
442 if (wl_get_noise(ifname, &noise))
443 noise = 0;
444
445 if ((ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL))
446 {
447 for (i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry))
448 {
449 memset(&entry, 0, sizeof(entry));
450 memcpy(rssi.mac, &macs->ea[i], 6);
451
452 if (!wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)))
453 entry.signal = (rssi.rssi - 0x100);
454 else
455 entry.signal = 0;
456
457 entry.noise = noise;
458 memcpy(entry.mac, &macs->ea[i], 6);
459 wl_get_assoclist_cb(ifname, &entry);
460
461 memcpy(&buf[j], &entry, sizeof(entry));
462 }
463
464 *len = j;
465 free(macs);
466 return 0;
467 }
468 else if ((arp = fopen("/proc/net/arp", "r")) != NULL)
469 {
470 j = 0;
471
472 while (fgets(line, sizeof(line), arp) != NULL)
473 {
474 if (sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname))
475 {
476 rssi.mac[0] = strtol(&macstr[0], NULL, 16);
477 rssi.mac[1] = strtol(&macstr[3], NULL, 16);
478 rssi.mac[2] = strtol(&macstr[6], NULL, 16);
479 rssi.mac[3] = strtol(&macstr[9], NULL, 16);
480 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
481 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
482
483 if (!wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)))
484 entry.signal = (rssi.rssi - 0x100);
485 else
486 entry.signal = 0;
487
488 entry.noise = noise;
489 memcpy(entry.mac, rssi.mac, 6);
490 memcpy(&buf[j], &entry, sizeof(entry));
491
492 j += sizeof(entry);
493 }
494 }
495
496 *len = j;
497 (void) fclose(arp);
498 return 0;
499 }
500
501 return -1;
502 }
503
504 static int wl_get_txpwrlist(const char *ifname, char *buf, int *len)
505 {
506 struct iwinfo_txpwrlist_entry entry;
507 uint8_t dbm[11] = { 0, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };
508 uint8_t mw[11] = { 1, 3, 6, 10, 15, 25, 39, 63, 100, 158, 251 };
509 int i;
510
511 for (i = 0; i < 11; i++)
512 {
513 entry.dbm = dbm[i];
514 entry.mw = mw[i];
515 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
516 }
517
518 *len = 11 * sizeof(entry);
519 return 0;
520 }
521
522 static int wl_get_scanlist(const char *ifname, char *buf, int *len)
523 {
524 return wext_ops.scanlist(ifname, buf, len);
525 }
526
527 static int wl_get_freqlist(const char *ifname, char *buf, int *len)
528 {
529 return wext_ops.freqlist(ifname, buf, len);
530 }
531
532 static int wl_get_country(const char *ifname, char *buf)
533 {
534 char ccode[WLC_CNTRY_BUF_SZ];
535
536 if (!wl_ioctl(ifname, WLC_GET_COUNTRY, ccode, WLC_CNTRY_BUF_SZ))
537 {
538 /* IL0 -> World */
539 if (!strcmp(ccode, "IL0"))
540 sprintf(buf, "00");
541
542 /* YU -> RS */
543 else if (!strcmp(ccode, "YU"))
544 sprintf(buf, "RS");
545
546 else
547 memcpy(buf, ccode, 2);
548
549 return 0;
550 }
551
552 return -1;
553 }
554
555 static int wl_get_countrylist(const char *ifname, char *buf, int *len)
556 {
557 int i, count;
558 char cdata[WLC_IOCTL_MAXLEN];
559 struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
560 wl_country_list_t *cl = (wl_country_list_t *)cdata;
561
562 cl->buflen = sizeof(cdata);
563
564 if (!wl_ioctl(ifname, WLC_GET_COUNTRY_LIST, cl, cl->buflen))
565 {
566 for (i = 0, count = 0; i < cl->count; i++, c++)
567 {
568 snprintf(c->ccode, sizeof(c->ccode), "%s", &cl->country_abbrev[i * WLC_CNTRY_BUF_SZ]);
569 c->iso3166 = c->ccode[0] * 256 + c->ccode[1];
570
571 /* IL0 -> World */
572 if (!strcmp(c->ccode, "IL0"))
573 c->iso3166 = 0x3030;
574
575 /* YU -> RS */
576 else if (!strcmp(c->ccode, "YU"))
577 c->iso3166 = 0x5253;
578 }
579
580 *len = (i * sizeof(struct iwinfo_country_entry));
581 return 0;
582 }
583
584 return -1;
585 }
586
587 static int wl_get_hwmodelist(const char *ifname, int *buf)
588 {
589 int phytype;
590 uint i, band[WLC_BAND_ALL], bands;
591
592 if (!wl_ioctl(ifname, WLC_GET_PHYTYPE, &phytype, sizeof(phytype)) &&
593 !wl_ioctl(ifname, WLC_GET_BANDLIST, band, sizeof(band)))
594 {
595 *buf = 0;
596 switch (phytype)
597 {
598 case WLC_PHY_TYPE_A:
599 *buf = IWINFO_80211_A;
600 break;
601 case WLC_PHY_TYPE_B:
602 *buf = IWINFO_80211_B;
603 break;
604 case WLC_PHY_TYPE_AC:
605 *buf |= IWINFO_80211_AC;
606 case WLC_PHY_TYPE_HT:
607 case WLC_PHY_TYPE_N:
608 *buf |= IWINFO_80211_N;
609 case WLC_PHY_TYPE_LP:
610 case WLC_PHY_TYPE_G:
611 bands = 0;
612 for (i = 1; i <= band[0]; i++)
613 {
614 bands |= band[i];
615 }
616 if (bands & WLC_BAND_5G)
617 *buf |= IWINFO_80211_A;
618 if (bands & WLC_BAND_2G)
619 {
620 *buf |= IWINFO_80211_B;
621 *buf |= IWINFO_80211_G;
622 }
623 break;
624 default:
625 return -1;
626 break;
627 }
628 return 0;
629 }
630 return -1;
631 }
632
633 static int wl_get_mbssid_support(const char *ifname, int *buf)
634 {
635 wlc_rev_info_t revinfo;
636
637 /* Multi bssid support only works on corerev >= 9 */
638 if (!wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)))
639 {
640 if (revinfo.corerev >= 9)
641 {
642 *buf = 1;
643 return 0;
644 }
645 }
646
647 return -1;
648 }
649
650 static int wl_get_hardware_id(const char *ifname, char *buf)
651 {
652 wlc_rev_info_t revinfo;
653 struct iwinfo_hardware_id *ids = (struct iwinfo_hardware_id *)buf;
654
655 if (wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)))
656 return -1;
657
658 ids->vendor_id = revinfo.vendorid;
659 ids->device_id = revinfo.deviceid;
660 ids->subsystem_vendor_id = revinfo.boardvendor;
661 ids->subsystem_device_id = revinfo.boardid;
662
663 return 0;
664 }
665
666 static int wl_get_hardware_name(const char *ifname, char *buf)
667 {
668 struct iwinfo_hardware_id ids;
669
670 if (wl_get_hardware_id(ifname, (char *)&ids))
671 return -1;
672
673 sprintf(buf, "Broadcom BCM%04X", ids.device_id);
674
675 return 0;
676 }
677
678 static int wl_get_txpower_offset(const char *ifname, int *buf)
679 {
680 FILE *p;
681 char off[8];
682
683 *buf = 0;
684
685 if ((p = popen("/usr/sbin/nvram get opo", "r")) != NULL)
686 {
687 if (fread(off, 1, sizeof(off), p))
688 *buf = strtoul(off, NULL, 16);
689
690 pclose(p);
691 }
692
693 return 0;
694 }
695
696 static int wl_get_frequency_offset(const char *ifname, int *buf)
697 {
698 /* Stub */
699 *buf = 0;
700 return -1;
701 }
702
703 const struct iwinfo_ops wl_ops = {
704 .name = "wl",
705 .probe = wl_probe,
706 .channel = wl_get_channel,
707 .frequency = wl_get_frequency,
708 .frequency_offset = wl_get_frequency_offset,
709 .txpower = wl_get_txpower,
710 .txpower_offset = wl_get_txpower_offset,
711 .bitrate = wl_get_bitrate,
712 .signal = wl_get_signal,
713 .noise = wl_get_noise,
714 .quality = wl_get_quality,
715 .quality_max = wl_get_quality_max,
716 .mbssid_support = wl_get_mbssid_support,
717 .hwmodelist = wl_get_hwmodelist,
718 .mode = wl_get_mode,
719 .ssid = wl_get_ssid,
720 .bssid = wl_get_bssid,
721 .country = wl_get_country,
722 .hardware_id = wl_get_hardware_id,
723 .hardware_name = wl_get_hardware_name,
724 .encryption = wl_get_encryption,
725 .phyname = wl_get_phyname,
726 .assoclist = wl_get_assoclist,
727 .txpwrlist = wl_get_txpwrlist,
728 .scanlist = wl_get_scanlist,
729 .freqlist = wl_get_freqlist,
730 .countrylist = wl_get_countrylist,
731 .close = wl_close
732 };