finally move buildroot-ng to trunk
[openwrt/svn-archive/archive.git] / package / broadcom-wl / src / wlc / wlc.c
1 /*
2 * wlc - Broadcom Wireless Driver Control Utility
3 *
4 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <glob.h>
25 #include <ctype.h>
26
27 #include <typedefs.h>
28 #include <wlutils.h>
29 #include <proto/802.11.h>
30
31 #define VERSION "0.1"
32 #define BUFSIZE 8192
33 #define PTABLE_MAGIC 0xbadc0ded
34 #define PTABLE_SLT1 1
35 #define PTABLE_SLT2 2
36 #define PTABLE_ACKW 3
37 #define PTABLE_ADHM 4
38 #define PTABLE_END 0xffffffff
39
40 /*
41 * Copy each token in wordlist delimited by space into word
42 * Taken from Broadcom shutils.h
43 */
44 #define foreach(word, wordlist, next) \
45 for (next = &wordlist[strspn(wordlist, " ")], \
46 strncpy(word, next, sizeof(word)), \
47 word[strcspn(word, " ")] = '\0', \
48 word[sizeof(word) - 1] = '\0', \
49 next = strchr(next, ' '); \
50 strlen(word); \
51 next = next ? &next[strspn(next, " ")] : "", \
52 strncpy(word, next, sizeof(word)), \
53 word[strcspn(word, " ")] = '\0', \
54 word[sizeof(word) - 1] = '\0', \
55 next = strchr(next, ' '))
56
57 static char wlbuf[8192];
58 static char interface[16] = "wl0";
59 static unsigned long ptable[128];
60 static unsigned long kmem_offset = 0;
61 static int vif = 0, debug = 1, fromstdin = 0;
62
63 typedef enum {
64 NONE = 0x00,
65
66 /* types */
67 PARAM_TYPE = 0x00f,
68 INT = 0x001,
69 STRING = 0x002,
70
71 /* options */
72 PARAM_OPTIONS = 0x0f0,
73 NOARG = 0x010,
74
75 /* modes */
76 PARAM_MODE = 0xf00,
77 GET = 0x100,
78 SET = 0x200,
79 } wlc_param;
80
81 struct wlc_call {
82 const char *name;
83 wlc_param param;
84 int (*handler)(wlc_param param, void *data, void *value);
85 union {
86 int num;
87 char *str;
88 void *ptr;
89 } data;
90 const char *desc;
91 };
92
93 /* can't use the system include because of the stupid broadcom header files */
94 extern struct ether_addr *ether_aton(const char *asc);
95 extern char *ether_ntoa(const struct ether_addr *addr);
96
97 /*
98 * find the starting point of wl.o in memory
99 * by reading /proc/ksyms
100 */
101 static inline void wlc_get_mem_offset(void)
102 {
103 FILE *f;
104 char s[64];
105
106 /* yes, i'm lazy ;) */
107 f = popen("grep '\\[wl]' /proc/ksyms | sort", "r");
108 if (fgets(s, 64, f) == 0)
109 return;
110
111 pclose(f);
112
113 s[8] = 0;
114 kmem_offset = strtoul(s, NULL, 16);
115
116 /* sanity check */
117 if (kmem_offset < 0xc0000000)
118 kmem_offset = 0;
119 }
120
121
122 static int ptable_init(void)
123 {
124 glob_t globbuf;
125 struct stat statbuf;
126 int fd;
127
128 if (ptable[0] == PTABLE_MAGIC)
129 return 0;
130
131 glob("/lib/modules/2.4.*/wl.o.patch", 0, NULL, &globbuf);
132
133 if (globbuf.gl_pathv[0] == NULL)
134 return -1;
135
136 if ((fd = open(globbuf.gl_pathv[0], O_RDONLY)) < 0)
137 return -1;
138
139 if (fstat(fd, &statbuf) < 0)
140 goto failed;
141
142 if (statbuf.st_size < 512)
143 goto failed;
144
145 if (read(fd, ptable, 512) < 512)
146 goto failed;
147
148 if (ptable[0] != PTABLE_MAGIC)
149 goto failed;
150
151 close(fd);
152
153 wlc_get_mem_offset();
154 if (kmem_offset == 0)
155 return -1;
156
157 return 0;
158
159 failed:
160 close(fd);
161
162 return -1;
163 }
164
165 static inline unsigned long wlc_kmem_read(unsigned long offset)
166 {
167 int fd;
168 unsigned long ret;
169
170 if ((fd = open("/dev/kmem", O_RDONLY )) < 0)
171 return -1;
172
173 lseek(fd, 0x70000000, SEEK_SET);
174 lseek(fd, (kmem_offset - 0x70000000) + offset, SEEK_CUR);
175 read(fd, &ret, 4);
176 close(fd);
177
178 return ret;
179 }
180
181 static inline void wlc_kmem_write(unsigned long offset, unsigned long value)
182 {
183 int fd;
184
185 if ((fd = open("/dev/kmem", O_WRONLY )) < 0)
186 return;
187
188 lseek(fd, 0x70000000, SEEK_SET);
189 lseek(fd, (kmem_offset - 0x70000000) + offset, SEEK_CUR);
190 write(fd, &value, 4);
191 close(fd);
192 }
193
194 static int wlc_patcher_getval(unsigned long key, unsigned long *val)
195 {
196 unsigned long *pt = &ptable[1];
197 unsigned long tmp;
198
199 if (ptable_init() < 0) {
200 fprintf(stderr, "Could not load the ptable\n");
201 return -1;
202 }
203
204 while (*pt != PTABLE_END) {
205 if (*pt == key) {
206 tmp = wlc_kmem_read(pt[1]);
207
208 if (tmp == pt[2])
209 *val = 0xffffffff;
210 else
211 *val = tmp;
212
213 return 0;
214 }
215 pt += 3;
216 }
217
218 return -1;
219 }
220
221 static int wlc_patcher_setval(unsigned long key, unsigned long val)
222 {
223 unsigned long *pt = &ptable[1];
224
225 if (ptable_init() < 0) {
226 fprintf(stderr, "Could not load the ptable\n");
227 return -1;
228 }
229
230 if (val != 0xffffffff)
231 val = (pt[2] & ~(0xffff)) | (val & 0xffff);
232
233 while (*pt != PTABLE_END) {
234 if (*pt == key) {
235 if (val == 0xffffffff) /* default */
236 val = pt[2];
237
238 wlc_kmem_write(pt[1], val);
239 }
240 pt += 3;
241 }
242
243 return 0;
244 }
245
246 static int wlc_slottime(wlc_param param, void *data, void *value)
247 {
248 int *val = (int *) value;
249 int ret = 0;
250
251 if ((param & PARAM_MODE) == SET) {
252 wlc_patcher_setval(PTABLE_SLT1, *val);
253 wlc_patcher_setval(PTABLE_SLT2, ((*val == -1) ? *val : *val + 510));
254 } else if ((param & PARAM_MODE) == GET) {
255 ret = wlc_patcher_getval(PTABLE_SLT1, (unsigned long *) val);
256 if (*val != 0xffffffff)
257 *val &= 0xffff;
258 }
259
260 return ret;
261 }
262
263 static int wlc_noack(wlc_param param, void *data, void *value)
264 {
265 int *val = (int *) value;
266 int ret = 0;
267
268 if ((param & PARAM_MODE) == SET) {
269 wlc_patcher_setval(PTABLE_ACKW, ((*val) ? 1 : 0));
270 } else if ((param & PARAM_MODE) == GET) {
271 ret = wlc_patcher_getval(PTABLE_ACKW, (unsigned long *) val);
272 *val &= 0xffff;
273 *val = (*val ? 1 : 0);
274 }
275
276 return ret;
277 }
278
279 static int wlc_ibss_merge(wlc_param param, void *data, void *value)
280 {
281 int *val = (int *) value;
282 int ret = 0;
283
284 if ((param & PARAM_MODE) == SET) {
285 /* overwrite the instruction with 'lui v0,0x0' - fake a return
286 * status of 0 for wlc_bcn_tsf_later */
287 wlc_patcher_setval(PTABLE_ACKW, ((*val) ? -1 : 0x3c020000));
288 } else if ((param & PARAM_MODE) == GET) {
289 ret = wlc_patcher_getval(PTABLE_ACKW, (unsigned long *) val);
290 *val = ((*val == -1) ? 1 : 0);
291 }
292
293 return ret;
294 }
295
296 static int wlc_ioctl(wlc_param param, void *data, void *value)
297 {
298 unsigned int *var = ((unsigned int *) data);
299 unsigned int ioc = *var;
300
301 if (param & NOARG) {
302 return wl_ioctl(interface, ioc, NULL, 0);
303 }
304 switch(param & PARAM_TYPE) {
305 case INT:
306 return wl_ioctl(interface, ((param & SET) ? (ioc) : (ioc >> 16)) & 0xffff, value, sizeof(int));
307 case STRING:
308 return wl_ioctl(interface, ((param & SET) ? (ioc) : (ioc >> 16)) & 0xffff, value, BUFSIZE);
309 }
310 return 0;
311 }
312
313 static int wlc_iovar(wlc_param param, void *data, void *value)
314 {
315 int *val = (int *) value;
316 char *iov = *((char **) data);
317 int ret = 0;
318
319 if (param & SET) {
320 switch(param & PARAM_TYPE) {
321 case INT:
322 ret = wl_iovar_setint(interface, iov, *val);
323 }
324 }
325 if (param & GET) {
326 switch(param & PARAM_TYPE) {
327 case INT:
328 ret = wl_iovar_getint(interface, iov, val);
329 }
330 }
331
332 return ret;
333 }
334
335 static int wlc_bssiovar(wlc_param param, void *data, void *value)
336 {
337 int *val = (int *) value;
338 char *iov = *((char **) data);
339 int ret = 0;
340
341 if (param & SET) {
342 switch(param & PARAM_TYPE) {
343 case INT:
344 ret = wl_bssiovar_setint(interface, iov, vif, *val);
345 }
346 }
347 if (param & GET) {
348 switch(param & PARAM_TYPE) {
349 case INT:
350 ret = wl_bssiovar_getint(interface, iov, vif, val);
351 }
352 }
353
354 return ret;
355 }
356
357 static int wlc_vif_enabled(wlc_param param, void *data, void *value)
358 {
359 int *val = (int *) value;
360 int buf[3];
361 int ret = 0;
362
363 sprintf((char *) buf, "bss");
364 buf[1] = vif;
365 if (param & SET) {
366 buf[2] = (*val ? 1 : 0);
367 ret = wl_ioctl(interface, WLC_SET_VAR, buf, sizeof(buf));
368 } else if (param & GET) {
369 ret = wl_ioctl(interface, WLC_GET_VAR, buf, sizeof(buf));
370 *val = buf[0];
371 }
372
373 return ret;
374 }
375
376 static int wlc_ssid(wlc_param param, void *data, void *value)
377 {
378 int ret = -1, ret2 = -1;
379 char *dest = (char *) value;
380 wlc_ssid_t ssid;
381
382 if ((param & PARAM_MODE) == GET) {
383 ret = wl_bssiovar_get(interface, "ssid", vif, &ssid, sizeof(ssid));
384
385 if (ret)
386 /* if we can't get the ssid through the bssiovar, try WLC_GET_SSID */
387 ret = wl_ioctl(interface, WLC_GET_SSID, &ssid, sizeof(ssid));
388
389 if (!ret) {
390 memcpy(dest, ssid.SSID, ssid.SSID_len);
391 dest[ssid.SSID_len] = 0;
392 }
393 } else if ((param & PARAM_MODE) == SET) {
394 strncpy(ssid.SSID, value, 32);
395 ssid.SSID_len = strlen(value);
396
397 if (ssid.SSID_len > 32)
398 ssid.SSID_len = 32;
399
400 if (vif == 0) {
401 /* for the main interface, also try the WLC_SET_SSID call */
402 ret2 = wl_ioctl(interface, WLC_SET_SSID, &ssid, sizeof(ssid));
403 }
404
405 ret = wl_bssiovar_set(interface, "ssid", vif, &ssid, sizeof(ssid));
406 ret = (!ret2 ? 0 : ret);
407 }
408
409 return ret;
410 }
411
412 static int wlc_int(wlc_param param, void *data, void *value)
413 {
414 int *var = *((int **) data);
415 int *val = (int *) value;
416
417 if ((param & PARAM_MODE) == SET) {
418 *var = *val;
419 } else if ((param & PARAM_MODE) == GET) {
420 *val = *var;
421 }
422
423 return 0;
424 }
425
426 static int wlc_flag(wlc_param param, void *data, void *value)
427 {
428 int *var = *((int **) data);
429
430 *var = 1;
431
432 return 0;
433 }
434
435 static int wlc_string(wlc_param param, void *data, void *value)
436 {
437 char *var = *((char **) data);
438
439 if ((param & PARAM_MODE) == GET) {
440 strcpy(value, var);
441 }
442
443 return 0;
444 }
445
446 static int wlc_afterburner(wlc_param param, void *data, void *value)
447 {
448 int *val = (int *) value;
449 int ret = 0;
450
451 if ((param & PARAM_MODE) == GET) {
452 ret = wl_iovar_getint(interface, "afterburner", val);
453 } else {
454 wl_iovar_setint(interface, "wlfeatureflag", (*val ? 3 : 0));
455 ret = wl_iovar_setint(interface, "afterburner", (*val ? 1 : 0));
456 wl_iovar_setint(interface, "afterburner_override", *val);
457 }
458
459 return ret;
460 }
461
462 static int wlc_maclist(wlc_param param, void *data, void *value)
463 {
464 unsigned int *var = ((unsigned int *) data);
465 unsigned int ioc = *var;
466 int limit = (sizeof(wlbuf) - 4) / sizeof(struct ether_addr);
467 struct maclist *list = (struct maclist *) wlbuf;
468 char *str = (char *) value;
469 char astr[30], *p;
470 struct ether_addr *addr;
471 int isset = 0;
472 int ret;
473
474 if ((param & PARAM_MODE) == GET) {
475 list->count = limit;
476 ret = wl_ioctl(interface, (ioc >> 16) & 0xffff, wlbuf, sizeof(wlbuf));
477
478 if (!ret)
479 while (list->count)
480 str += sprintf(str, "%s%s", ((((char *) value) == str) ? "" : " "), ether_ntoa(&list->ea[list->count-- - 1]));
481
482 return ret;
483 } else {
484 while (*str && isspace(*str))
485 *str++;
486
487 if (*str == '+') {
488 str++;
489
490 list->count = limit;
491 if (wl_ioctl(interface, (ioc >> 16) & 0xffff, wlbuf, sizeof(wlbuf)) == 0)
492 isset = 1;
493
494 while (*str && isspace(*str))
495 str++;
496 }
497
498 if (!isset)
499 memset(wlbuf, 0, sizeof(wlbuf));
500
501 foreach(astr, str, p) {
502 if (list->count >= limit)
503 break;
504
505 if ((addr = ether_aton(astr)) != NULL)
506 memcpy(&list->ea[list->count++], addr, sizeof(struct ether_addr));
507 }
508
509 return wl_ioctl(interface, ioc & 0xffff, wlbuf, sizeof(wlbuf));
510 }
511 }
512
513 static int wlc_radio(wlc_param param, void *data, void *value)
514 {
515 int *val = (int *) value;
516 int ret;
517
518 if ((param & PARAM_MODE) == GET) {
519 ret = wl_ioctl(interface, WLC_GET_RADIO, val, sizeof(int));
520 *val = ((*val & 1) ? 0 : 1);
521 } else {
522 *val = (1 << 16) | (*val ? 0 : 1);
523 ret = wl_ioctl(interface, WLC_SET_RADIO, val, sizeof(int));
524 }
525
526 return ret;
527 }
528
529 static int wlc_wsec_key(wlc_param param, void *null, void *value)
530 {
531 wl_wsec_key_t wsec_key;
532 unsigned char *index = value;
533 unsigned char *key;
534 unsigned char *data;
535 unsigned char hex[3];
536
537 if ((param & PARAM_MODE) != SET)
538 return 0;
539
540 memset(&wsec_key, 0, sizeof(wsec_key));
541 if (index[0] == '=') {
542 wsec_key.flags = WL_PRIMARY_KEY;
543 index++;
544 }
545
546 if ((index[0] < '1') || (index[0] > '4') || (index[1] != ','))
547 return -1;
548
549 key = index + 2;
550 if (strncmp(key, "d:", 2) == 0) { /* delete key */
551 } else if (strncmp(key, "s:", 2) == 0) { /* ascii key */
552 key += 2;
553 wsec_key.len = strlen(key);
554
555 if ((wsec_key.len != 5) && (wsec_key.len != 13))
556 return -1;
557
558 strcpy(wsec_key.data, key);
559 } else { /* hex key */
560 wsec_key.len = strlen(key);
561 if ((wsec_key.len != 10) && (wsec_key.len != 26))
562 return -1;
563
564 wsec_key.len /= 2;
565 data = wsec_key.data;
566 hex[2] = 0;
567 do {
568 hex[0] = *(key++);
569 hex[1] = *(key++);
570 *(data++) = (unsigned char) strtoul(hex, NULL, 16);
571 } while (*key != 0);
572 }
573
574 return wl_bssiovar_set(interface, "wsec_key", vif, &wsec_key, sizeof(wsec_key));
575 }
576
577 static inline int cw2ecw(int cw)
578 {
579 int i;
580 for (cw++, i = 0; cw; i++) cw >>=1;
581 return i - 1;
582 }
583
584 static int wlc_wme_ac(wlc_param param, void *data, void *value)
585 {
586 char *type = *((char **) data);
587 char *settings = (char *) value;
588 char cmd[100], *p, *val;
589 edcf_acparam_t params[AC_COUNT];
590 int ret;
591 int intval;
592 int cur = -1;
593 char *buf = wlbuf;
594
595 if ((param & PARAM_MODE) != SET)
596 return -1;
597
598 memset(params, 0, sizeof(params));
599 ret = wl_iovar_get(interface, type, params, sizeof(params));
600 memset(buf, 0, BUFSIZE);
601 strcpy(buf, type);
602 buf += strlen(buf) + 1;
603
604 foreach(cmd, settings, p) {
605 val = strchr(cmd, '=');
606 if (val == NULL) {
607 if (strcmp(cmd, "be") == 0)
608 cur = AC_BE;
609 else if (strcmp(cmd, "bk") == 0)
610 cur = AC_BK;
611 else if (strcmp(cmd, "vi") == 0)
612 cur = AC_VI;
613 else if (strcmp(cmd, "vo") == 0)
614 cur = AC_VO;
615 else
616 return -1;
617
618 /* just in case */
619 params[cur].ACI = (params[cur].ACI & (0x3 << 5)) | (cur << 5);
620 } else {
621 *(val++) = 0;
622
623 intval = strtoul(val, NULL, 10);
624 if (strcmp(cmd, "cwmin") == 0)
625 params[cur].ECW = (params[cur].ECW & ~(0xf)) | cw2ecw(intval);
626 else if (strcmp(cmd, "ecwmin") == 0)
627 params[cur].ECW = (params[cur].ECW & ~(0xf)) | (intval & 0xf);
628 else if (strcmp(cmd, "cwmax") == 0)
629 params[cur].ECW = (params[cur].ECW & ~(0xf << 4)) | (cw2ecw(intval) << 4);
630 else if (strcmp(cmd, "ecwmax") == 0)
631 params[cur].ECW = (params[cur].ECW & ~(0xf << 4)) | ((intval & 0xf) << 4);
632 else if (strcmp(cmd, "aifsn") == 0)
633 params[cur].ACI = (params[cur].ACI & ~(0xf)) | (intval & 0xf);
634 else if (strcmp(cmd, "txop") == 0)
635 params[cur].TXOP = intval >> 5;
636 else if (strcmp(cmd, "force") == 0)
637 params[cur].ACI = (params[cur].ACI & ~(1 << 4)) | ((intval) ? (1 << 4) : 0);
638 else return -1;
639
640 memcpy(buf, &params[cur], sizeof(edcf_acparam_t));
641 wl_ioctl(interface, WLC_SET_VAR, wlbuf, BUFSIZE);
642 }
643 }
644 return ret;
645 }
646
647 static const struct wlc_call wlc_calls[] = {
648 {
649 .name = "version",
650 .param = STRING|NOARG,
651 .handler = wlc_string,
652 .data.str = VERSION,
653 .desc = "Version of this program"
654 },
655 {
656 .name = "debug",
657 .param = INT,
658 .handler = wlc_int,
659 .data.ptr = &debug,
660 .desc = "wlc debug level"
661 },
662 {
663 .name = "stdin",
664 .param = NOARG,
665 .handler = wlc_flag,
666 .data.ptr = &fromstdin,
667 .desc = "Accept input from stdin"
668 },
669 {
670 .name = "up",
671 .param = NOARG,
672 .handler = wlc_ioctl,
673 .data.num = WLC_UP,
674 .desc = "Bring the interface up"
675 },
676 {
677 .name = "down",
678 .param = NOARG,
679 .handler = wlc_ioctl,
680 .data.num = WLC_DOWN,
681 .desc = "Bring the interface down"
682 },
683 {
684 .name = "radio",
685 .param = INT,
686 .handler = wlc_radio,
687 .desc = "Radio enabled flag"
688 },
689 {
690 .name = "ap",
691 .param = INT,
692 .handler = wlc_ioctl,
693 .data.num = ((WLC_GET_AP << 16) | WLC_SET_AP),
694 .desc = "Access Point mode"
695 },
696 {
697 .name = "mssid",
698 .param = INT,
699 .handler = wlc_iovar,
700 .data.str = "mssid",
701 .desc = "Multi-ssid mode"
702 },
703 {
704 .name = "apsta",
705 .param = INT,
706 .handler = wlc_iovar,
707 .data.str = "apsta",
708 .desc = "AP+STA mode"
709 },
710 {
711 .name = "infra",
712 .param = INT,
713 .handler = wlc_ioctl,
714 .data.num = ((WLC_GET_INFRA << 16) | WLC_SET_INFRA),
715 .desc = "Infrastructure mode"
716 },
717 {
718 .name = "wet",
719 .param = INT,
720 .handler = wlc_ioctl,
721 .data.num = ((WLC_GET_WET << 16) | WLC_SET_WET),
722 .desc = "Wireless repeater mode",
723 },
724 {
725 .name = "statimeout",
726 .param = INT,
727 .handler = wlc_iovar,
728 .data.str = "sta_retry_time",
729 .desc = "STA connection timeout"
730 },
731 {
732 .name = "country",
733 .param = STRING,
734 .handler = wlc_ioctl,
735 .data.num = ((WLC_GET_COUNTRY << 16) | WLC_SET_COUNTRY),
736 .desc = "Country code"
737 },
738 {
739 .name = "channel",
740 .param = INT,
741 .handler = wlc_ioctl,
742 .data.num = ((WLC_GET_CHANNEL << 16) | WLC_SET_CHANNEL),
743 .desc = "Channel",
744 },
745 {
746 .name = "vlan_mode",
747 .param = INT,
748 .handler = wlc_bssiovar,
749 .data.str = "vlan_mode",
750 .desc = "Parse 802.1Q tags",
751 },
752 {
753 .name = "vif",
754 .param = INT,
755 .handler = wlc_int,
756 .data.ptr = &vif,
757 .desc = "Current vif index"
758 },
759 {
760 .name = "enabled",
761 .param = INT,
762 .handler = wlc_vif_enabled,
763 .desc = "vif enabled flag"
764 },
765 {
766 .name = "ssid",
767 .param = STRING,
768 .handler = wlc_ssid,
769 .desc = "Interface ESSID"
770 },
771 {
772 .name = "closed",
773 .param = INT,
774 .handler = wlc_bssiovar,
775 .data.str = "closednet",
776 .desc = "Hidden ESSID flag"
777 },
778 {
779 .name = "wsec",
780 .param = INT,
781 .handler = wlc_bssiovar,
782 .data.str = "wsec",
783 .desc = "Security mode flags"
784 },
785 {
786 .name = "wepkey",
787 .param = STRING,
788 .handler = wlc_wsec_key,
789 .desc = "Set/Remove WEP keys"
790 },
791 {
792 .name = "wsec_restrict",
793 .param = INT,
794 .handler = wlc_bssiovar,
795 .data.str = "wsec_restrict",
796 .desc = "Drop unencrypted traffic"
797 },
798 {
799 .name = "eap_restrict",
800 .param = INT,
801 .handler = wlc_bssiovar,
802 .data.str = "eap_restrict",
803 .desc = "Only allow 802.1X traffic until 802.1X authorized"
804 },
805 {
806 .name = "wpa_auth",
807 .param = INT,
808 .handler = wlc_bssiovar,
809 .data.str = "wpa_auth",
810 .desc = "WPA authentication modes"
811 },
812 {
813 .name = "ap_isolate",
814 .param = INT,
815 .handler = wlc_bssiovar,
816 .data.str = "ap_isolate",
817 .desc = "Isolate connected clients"
818 },
819 {
820 .name = "supplicant",
821 .param = INT,
822 .handler = wlc_iovar,
823 .data.str = "sup_wpa",
824 .desc = "Built-in WPA supplicant"
825 },
826 {
827 .name = "maxassoc",
828 .param = INT,
829 .handler = wlc_iovar,
830 .data.str = "maxassoc",
831 .desc = "Max. number of associated clients",
832 },
833 {
834 .name = "wme",
835 .param = INT,
836 .handler = wlc_iovar,
837 .data.str = "wme",
838 .desc = "WME enabled"
839 },
840 {
841 .name = "wme_ac_ap",
842 .param = STRING,
843 .handler = wlc_wme_ac,
844 .data.str = "wme_ac_ap",
845 .desc = "Set WME AC options for AP mode",
846 },
847 {
848 .name = "wme_ac_sta",
849 .param = STRING,
850 .handler = wlc_wme_ac,
851 .data.str = "wme_ac_sta",
852 .desc = "Set WME AC options for STA mode",
853 },
854 {
855 .name = "wme_noack",
856 .param = INT,
857 .handler = wlc_iovar,
858 .data.str = "wme_noack",
859 .desc = "WME ACK disable request",
860 },
861 {
862 .name = "fragthresh",
863 .param = INT,
864 .handler = wlc_iovar,
865 .data.str = "fragthresh",
866 .desc = "Fragmentation threshold",
867 },
868 {
869 .name = "rtsthresh",
870 .param = INT,
871 .handler = wlc_iovar,
872 .data.str = "rtsthresh",
873 .desc = "RTS threshold"
874 },
875 {
876 .name = "rxant",
877 .param = INT,
878 .handler = wlc_ioctl,
879 .data.num = ((WLC_GET_ANTDIV << 16) | WLC_SET_ANTDIV),
880 .desc = "Rx antenna selection"
881 },
882 {
883 .name = "txant",
884 .param = INT,
885 .handler = wlc_ioctl,
886 .data.num = ((WLC_GET_TXANT << 16) | WLC_SET_TXANT),
887 .desc = "Tx antenna selection"
888 },
889 {
890 .name = "dtim",
891 .param = INT,
892 .handler = wlc_ioctl,
893 .data.num = ((WLC_GET_DTIMPRD << 16) | WLC_SET_DTIMPRD),
894 .desc = "DTIM period",
895 },
896 {
897 .name = "bcn",
898 .param = INT,
899 .handler = wlc_ioctl,
900 .data.num = ((WLC_GET_BCNPRD << 16) | WLC_SET_BCNPRD),
901 .desc = "Beacon interval"
902 },
903 {
904 .name = "frameburst",
905 .param = INT,
906 .handler = wlc_ioctl,
907 .data.num = ((WLC_GET_FAKEFRAG << 16) | WLC_SET_FAKEFRAG),
908 .desc = "Framebursting"
909 },
910 {
911 .name = "monitor",
912 .param = INT,
913 .handler = wlc_ioctl,
914 .data.num = ((WLC_GET_MONITOR << 16) | WLC_SET_MONITOR),
915 .desc = "Monitor mode"
916 },
917 {
918 .name = "passive",
919 .param = INT,
920 .handler = wlc_ioctl,
921 .data.num = ((WLC_GET_PASSIVE << 16) | WLC_SET_PASSIVE),
922 .desc = "Passive mode"
923 },
924 {
925 .name = "macfilter",
926 .param = INT,
927 .handler = wlc_ioctl,
928 .data.num = ((WLC_GET_MACMODE << 16) | WLC_SET_MACMODE),
929 .desc = "MAC filter mode (0:disabled, 1:deny, 2:allow)"
930 },
931 {
932 .name = "maclist",
933 .param = STRING,
934 .data.num = ((WLC_GET_MACLIST << 16) | WLC_SET_MACLIST),
935 .handler = wlc_maclist,
936 .desc = "MAC filter list"
937 },
938 {
939 .name = "autowds",
940 .param = INT,
941 .handler = wlc_ioctl,
942 .data.num = ((WLC_GET_LAZYWDS << 16) | WLC_SET_LAZYWDS),
943 .desc = "Automatic WDS"
944 },
945 {
946 .name = "wds",
947 .param = STRING,
948 .data.num = ((WLC_GET_WDSLIST << 16) | WLC_SET_WDSLIST),
949 .handler = wlc_maclist,
950 .desc = "WDS connection list"
951 },
952 {
953 .name = "wdstimeout",
954 .param = INT,
955 .handler = wlc_iovar,
956 .data.str = "wdstimeout",
957 .desc = "WDS link detection timeout"
958 },
959 {
960 .name = "afterburner",
961 .param = INT,
962 .handler = wlc_afterburner,
963 .desc = "Broadcom Afterburner"
964 },
965 {
966 .name = "slottime",
967 .param = INT,
968 .handler = wlc_slottime,
969 .desc = "Slot time (-1 = auto)"
970 },
971 {
972 .name = "txack",
973 .param = INT,
974 .handler = wlc_noack,
975 .desc = "Tx ACK enabled flag"
976 },
977 {
978 .name = "ibss_merge",
979 .param = INT,
980 .handler = wlc_ibss_merge,
981 .desc = "Allow IBSS merge in Ad-Hoc mode"
982 }
983 };
984 #define wlc_calls_size (sizeof(wlc_calls) / sizeof(struct wlc_call))
985
986 static void usage(char *cmd)
987 {
988 int i;
989 fprintf(stderr, "Usage: %s <command> [<argument> ...]\n"
990 "\n"
991 "Available commands:\n", cmd);
992 for (i = 0; i < wlc_calls_size; i++) {
993 fprintf(stderr, "\t%-16s\t%s\n", wlc_calls[i].name ?: "", wlc_calls[i].desc ?: "");
994 }
995 fprintf(stderr, "\n");
996 exit(1);
997 }
998
999 static int do_command(const struct wlc_call *cmd, char *arg)
1000 {
1001 static char buf[BUFSIZE];
1002 int set;
1003 int ret = 0;
1004 char *format, *end;
1005 int intval;
1006
1007 if (debug >= 10) {
1008 fprintf(stderr, "do_command %-16s\t'%s'\n", cmd->name, arg);
1009 }
1010
1011 if ((arg == NULL) && ((cmd->param & PARAM_TYPE) != NONE)) {
1012 set = 0;
1013 ret = cmd->handler(cmd->param | GET, (void *) &cmd->data, (void *) buf);
1014 if (ret == 0) {
1015 switch(cmd->param & PARAM_TYPE) {
1016 case INT:
1017 intval = *((int *) buf);
1018
1019 if (intval > 65535)
1020 format = "0x%08x\n";
1021 else if (intval > 255)
1022 format = "0x%04x\n";
1023 else
1024 format = "%d\n";
1025
1026 fprintf(stdout, format, intval);
1027 break;
1028 case STRING:
1029 fprintf(stdout, "%s\n", buf);
1030 }
1031 }
1032 } else { /* SET */
1033 set = 1;
1034 switch(cmd->param & PARAM_TYPE) {
1035 case INT:
1036 intval = strtoul(arg, &end, 10);
1037 if (end && !(*end)) {
1038 memcpy(buf, &intval, sizeof(intval));
1039 } else {
1040 fprintf(stderr, "%s: Invalid argument\n", cmd->name);
1041 return -1;
1042 }
1043 break;
1044 case STRING:
1045 strncpy(buf, arg, BUFSIZE);
1046 buf[BUFSIZE - 1] = 0;
1047 }
1048
1049 ret = cmd->handler(cmd->param | SET, (void *) &cmd->data, (void *) buf);
1050 }
1051
1052 if ((debug > 0) && (ret != 0))
1053 fprintf(stderr, "Command '%s %s' failed: %d\n", (set == 1 ? "set" : "get"), cmd->name, ret);
1054
1055 return ret;
1056 }
1057
1058 static struct wlc_call *find_cmd(char *name)
1059 {
1060 int found = 0, i = 0;
1061
1062 while (!found && (i < wlc_calls_size)) {
1063 if (strcmp(name, wlc_calls[i].name) == 0)
1064 found = 1;
1065 else
1066 i++;
1067 }
1068
1069 return (struct wlc_call *) (found ? &wlc_calls[i] : NULL);
1070 }
1071
1072 int main(int argc, char **argv)
1073 {
1074 static char buf[BUFSIZE];
1075 char *s, *s2;
1076 char *cmd = argv[0];
1077 struct wlc_call *call;
1078 int ret = 0;
1079
1080 if (argc < 2)
1081 usage(argv[0]);
1082
1083 for(interface[2] = '0'; (interface[2] < '3') && (wl_probe(interface) != 0); interface[2]++);
1084 if (interface[2] == '3') {
1085 fprintf(stderr, "No Broadcom wl interface found!\n");
1086 return -1;
1087 }
1088
1089 argv++;
1090 argc--;
1091 while ((argc > 0) && (argv[0] != NULL)) {
1092 if ((call = find_cmd(argv[0])) == NULL) {
1093 fprintf(stderr, "Invalid command: %s\n\n", argv[0]);
1094 usage(cmd);
1095 }
1096 if ((argc > 1) && (!(call->param & NOARG))) {
1097 ret = do_command(call, argv[1]);
1098 argv += 2;
1099 argc -= 2;
1100 } else {
1101 ret = do_command(call, NULL);
1102 argv++;
1103 argc--;
1104 }
1105 }
1106
1107 while (fromstdin && !feof(stdin)) {
1108 *buf = 0;
1109 fgets(buf, BUFSIZE - 1, stdin);
1110
1111 if (*buf == 0)
1112 continue;
1113
1114 if ((s = strchr(buf, '\r')) != NULL)
1115 *s = 0;
1116 if ((s = strchr(buf, '\n')) != NULL)
1117 *s = 0;
1118
1119 s = buf;
1120 while (isspace(*s))
1121 s++;
1122
1123 if (!*s)
1124 continue;
1125
1126 if ((s2 = strchr(buf, ' ')) != NULL)
1127 *(s2++) = 0;
1128
1129 while (s2 && isspace(*s2))
1130 s2++;
1131
1132 if ((call = find_cmd(buf)) == NULL) {
1133 fprintf(stderr, "Invalid command: %s\n", buf);
1134 ret = -1;
1135 } else
1136 ret = do_command(call, ((call->param & NOARG) ? NULL : s2));
1137 }
1138
1139 return ret;
1140 }