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