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

Archive Download this file



interactive