[package] libiwinfo: fix hw mode detection
[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 return wext_get_txpower(ifname, buf);
139 }
140
141 int wl_get_bitrate(const char *ifname, int *buf)
142 {
143 int ret = -1;
144 int rate = 0;
145
146 if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
147 *buf = ((rate / 2) * 1000) + ((rate & 1) ? 500 : 0);
148
149 return ret;
150 }
151
152 int wl_get_signal(const char *ifname, int *buf)
153 {
154 unsigned int ap, rssi, i, rssi_count;
155 int ioctl_req_version = 0x2000;
156 char tmp[WLC_IOCTL_MAXLEN];
157 struct wl_maclist *macs = NULL;
158 wl_sta_rssi_t starssi;
159
160 memset(tmp, 0, WLC_IOCTL_MAXLEN);
161 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
162
163 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
164
165 if( !wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap )
166 {
167 *buf = tmp[WL_BSS_RSSI_OFFSET];
168 }
169 else
170 {
171 rssi = rssi_count = 0;
172
173 /* Calculate average rssi from conntected stations */
174 if( (macs = wl_read_assoclist(ifname)) != NULL )
175 {
176 for( i = 0; i < macs->count; i++ )
177 {
178 memcpy(starssi.mac, &macs->ea[i], 6);
179
180 if( !wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12) )
181 {
182 rssi -= starssi.rssi;
183 rssi_count++;
184 }
185 }
186
187 free(macs);
188 }
189
190 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
191 }
192
193 return 0;
194 }
195
196 int wl_get_noise(const char *ifname, int *buf)
197 {
198 unsigned int ap, noise;
199 int ioctl_req_version = 0x2000;
200 char tmp[WLC_IOCTL_MAXLEN];
201
202 memset(tmp, 0, WLC_IOCTL_MAXLEN);
203 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
204
205 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
206
207 if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
208 {
209 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
210 noise = 0;
211 }
212 else
213 {
214 noise = tmp[WL_BSS_NOISE_OFFSET];
215 }
216
217 *buf = noise;
218
219 return 0;
220 }
221
222 int wl_get_quality(const char *ifname, int *buf)
223 {
224 return wext_get_quality(ifname, buf);
225 }
226
227 int wl_get_quality_max(const char *ifname, int *buf)
228 {
229 return wext_get_quality_max(ifname, buf);
230 }
231
232 int wl_get_encryption(const char *ifname, char *buf)
233 {
234 uint32_t wsec, wauth, wpa;
235 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
236
237 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
238 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) ||
239 wl_ioctl(ifname, WLC_GET_AUTH, &wauth, sizeof(uint32_t)) )
240 return -1;
241
242 switch(wsec)
243 {
244 case 2:
245 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
246 break;
247
248 case 4:
249 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
250 break;
251
252 case 6:
253 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
254 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
255 break;
256 }
257
258 switch(wpa)
259 {
260 case 0:
261 if( wsec && !wauth )
262 c->auth_algs |= IWINFO_AUTH_OPEN;
263
264 else if( wsec && wauth )
265 c->auth_algs |= IWINFO_AUTH_SHARED;
266
267 /* ToDo: evaluate WEP key lengths */
268 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
269 c->auth_suites |= IWINFO_KMGMT_NONE;
270 break;
271
272 case 2:
273 c->wpa_version = 1;
274 c->auth_suites |= IWINFO_KMGMT_8021x;
275 break;
276
277 case 4:
278 c->wpa_version = 1;
279 c->auth_suites |= IWINFO_KMGMT_PSK;
280 break;
281
282 case 32:
283 case 64:
284 c->wpa_version = 2;
285 c->auth_suites |= IWINFO_KMGMT_8021x;
286 break;
287
288 case 66:
289 c->wpa_version = 3;
290 c->auth_suites |= IWINFO_KMGMT_8021x;
291 break;
292
293 case 128:
294 c->wpa_version = 2;
295 c->auth_suites |= IWINFO_KMGMT_PSK;
296 break;
297
298 case 132:
299 c->wpa_version = 3;
300 c->auth_suites |= IWINFO_KMGMT_PSK;
301 break;
302
303 default:
304 break;
305 }
306
307 c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
308 c->group_ciphers = c->pair_ciphers;
309
310 return 0;
311 }
312
313 int wl_get_enctype(const char *ifname, char *buf)
314 {
315 uint32_t wsec, wpa;
316 char algo[11];
317
318 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
319 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
320 return -1;
321
322 switch(wsec)
323 {
324 case 2:
325 sprintf(algo, "TKIP");
326 break;
327
328 case 4:
329 sprintf(algo, "CCMP");
330 break;
331
332 case 6:
333 sprintf(algo, "TKIP, CCMP");
334 break;
335 }
336
337 switch(wpa)
338 {
339 case 0:
340 sprintf(buf, "%s", wsec ? "WEP" : "None");
341 break;
342
343 case 2:
344 sprintf(buf, "WPA 802.1X (%s)", algo);
345 break;
346
347 case 4:
348 sprintf(buf, "WPA PSK (%s)", algo);
349 break;
350
351 case 32:
352 sprintf(buf, "802.1X (%s)", algo);
353 break;
354
355 case 64:
356 sprintf(buf, "WPA2 802.1X (%s)", algo);
357 break;
358
359 case 66:
360 sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
361 break;
362
363 case 128:
364 sprintf(buf, "WPA2 PSK (%s)", algo);
365 break;
366
367 case 132:
368 sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
369 break;
370
371 default:
372 sprintf(buf, "Unknown");
373 }
374
375 return 0;
376 }
377
378 int wl_get_assoclist(const char *ifname, char *buf, int *len)
379 {
380 int i, j, noise;
381 int ap, infra, passive;
382 char line[128];
383 char macstr[18];
384 char devstr[IFNAMSIZ];
385 struct wl_maclist *macs;
386 struct wl_sta_rssi rssi;
387 struct iwinfo_assoclist_entry entry;
388 FILE *arp;
389
390 ap = infra = passive = 0;
391
392 wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
393 wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
394 wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
395
396 if( wl_get_noise(ifname, &noise) )
397 noise = 0;
398
399 if( (ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL) )
400 {
401 for( i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry) )
402 {
403 memcpy(rssi.mac, &macs->ea[i], 6);
404
405 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
406 entry.signal = (rssi.rssi - 0x100);
407 else
408 entry.signal = 0;
409
410 entry.noise = noise;
411 memcpy(entry.mac, &macs->ea[i], 6);
412 memcpy(&buf[j], &entry, sizeof(entry));
413 }
414
415 *len = j;
416 free(macs);
417 return 0;
418 }
419 else if( (arp = fopen("/proc/net/arp", "r")) != NULL )
420 {
421 j = 0;
422
423 while( fgets(line, sizeof(line), arp) != NULL )
424 {
425 if( sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname) )
426 {
427 rssi.mac[0] = strtol(&macstr[0], NULL, 16);
428 rssi.mac[1] = strtol(&macstr[3], NULL, 16);
429 rssi.mac[2] = strtol(&macstr[6], NULL, 16);
430 rssi.mac[3] = strtol(&macstr[9], NULL, 16);
431 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
432 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
433
434 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
435 entry.signal = (rssi.rssi - 0x100);
436 else
437 entry.signal = 0;
438
439 entry.noise = noise;
440 memcpy(entry.mac, rssi.mac, 6);
441 memcpy(&buf[j], &entry, sizeof(entry));
442
443 j += sizeof(entry);
444 }
445 }
446
447 *len = j;
448 (void) fclose(arp);
449 return 0;
450 }
451
452 return -1;
453 }
454
455 int wl_get_txpwrlist(const char *ifname, char *buf, int *len)
456 {
457 struct iwinfo_txpwrlist_entry entry;
458 uint8_t dbm[8] = { 0, 6, 8, 10, 12, 14, 16, 18 };
459 uint8_t mw[8] = { 1, 3, 6, 10, 15, 25, 39, 63 };
460 int i;
461
462 for( i = 0; i < 8; i++ )
463 {
464 entry.dbm = dbm[i];
465 entry.mw = mw[i];
466 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
467 }
468
469 *len = 8 * sizeof(entry);
470 return 0;
471 }
472
473 int wl_get_scanlist(const char *ifname, char *buf, int *len)
474 {
475 return wext_get_scanlist(ifname, buf, len);
476 }
477
478 int wl_get_freqlist(const char *ifname, char *buf, int *len)
479 {
480 return wext_get_freqlist(ifname, buf, len);
481 }
482
483 int wl_get_country(const char *ifname, char *buf)
484 {
485 char ccode[WLC_CNTRY_BUF_SZ];
486
487 if( !wl_ioctl(ifname, WLC_GET_COUNTRY, ccode, WLC_CNTRY_BUF_SZ) )
488 {
489 /* IL0 -> World */
490 if( !strcmp(ccode, "IL0") )
491 sprintf(buf, "00");
492
493 /* YU -> RS */
494 else if( !strcmp(ccode, "YU") )
495 sprintf(buf, "RS");
496
497 else
498 memcpy(buf, ccode, 2);
499
500 return 0;
501 }
502
503 return -1;
504 }
505
506 int wl_get_countrylist(const char *ifname, char *buf, int *len)
507 {
508 int i, count;
509 char cdata[WLC_IOCTL_MAXLEN];
510 struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
511 wl_country_list_t *cl = (wl_country_list_t *)cdata;
512
513 cl->buflen = sizeof(cdata);
514
515 if( !wl_ioctl(ifname, WLC_GET_COUNTRY_LIST, cl, cl->buflen) )
516 {
517 for( i = 0, count = 0; i < cl->count; i++, c++ )
518 {
519 sprintf(c->ccode, &cl->country_abbrev[i * WLC_CNTRY_BUF_SZ]);
520 c->iso3166 = c->ccode[0] * 256 + c->ccode[1];
521
522 /* IL0 -> World */
523 if( !strcmp(c->ccode, "IL0") )
524 c->iso3166 = 0x3030;
525
526 /* YU -> RS */
527 else if( !strcmp(c->ccode, "YU") )
528 c->iso3166 = 0x5253;
529 }
530
531 *len = (i * sizeof(struct iwinfo_country_entry));
532 return 0;
533 }
534
535 return -1;
536 }
537
538 int wl_get_hwmodelist(const char *ifname, int *buf)
539 {
540 return wext_get_hwmodelist(ifname, buf);
541 }
542
543 int wl_get_mbssid_support(const char *ifname, int *buf)
544 {
545 wlc_rev_info_t revinfo;
546
547 /* Multi bssid support only works on corerev >= 9 */
548 if( !wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)) )
549 {
550 if( revinfo.corerev >= 9 )
551 {
552 *buf = 1;
553 return 0;
554 }
555 }
556
557 return -1;
558 }
This page took 0.074441 seconds and 5 git commands to generate.