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