2d6b846a0935075395fec98df5beb2f30482b4a0
[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 int wlc_ifname(wlc_param param, void *data, void *value)
648 {
649 char *val = (char *) value;
650 int ret = 0;
651
652 if (param & SET) {
653 if (strlen(val) < 16)
654 strcpy(interface, val);
655 else ret = -1;
656 }
657 if (param & GET) {
658 strcpy(val, interface);
659 }
660
661 return ret;
662 }
663
664 static int wlc_wdsmac(wlc_param param, void *data, void *value)
665 {
666 static struct ether_addr mac;
667 int ret = 0;
668
669 ret = wl_ioctl(interface, WLC_WDS_GET_REMOTE_HWADDR, &mac, 6);
670 if (ret == 0) {
671 strcpy((char *) value, ether_ntoa(&mac));
672 }
673
674 return ret;
675 }
676
677 static const struct wlc_call wlc_calls[] = {
678 {
679 .name = "version",
680 .param = STRING|NOARG,
681 .handler = wlc_string,
682 .data.str = VERSION,
683 .desc = "Version of this program"
684 },
685 {
686 .name = "debug",
687 .param = INT,
688 .handler = wlc_int,
689 .data.ptr = &debug,
690 .desc = "wlc debug level"
691 },
692 {
693 .name = "stdin",
694 .param = NOARG,
695 .handler = wlc_flag,
696 .data.ptr = &fromstdin,
697 .desc = "Accept input from stdin"
698 },
699 {
700 .name = "ifname",
701 .param = STRING,
702 .handler = wlc_ifname,
703 .desc = "interface to send commands to"
704 },
705 {
706 .name = "up",
707 .param = NOARG,
708 .handler = wlc_ioctl,
709 .data.num = WLC_UP,
710 .desc = "Bring the interface up"
711 },
712 {
713 .name = "down",
714 .param = NOARG,
715 .handler = wlc_ioctl,
716 .data.num = WLC_DOWN,
717 .desc = "Bring the interface down"
718 },
719 {
720 .name = "radio",
721 .param = INT,
722 .handler = wlc_radio,
723 .desc = "Radio enabled flag"
724 },
725 {
726 .name = "ap",
727 .param = INT,
728 .handler = wlc_ioctl,
729 .data.num = ((WLC_GET_AP << 16) | WLC_SET_AP),
730 .desc = "Access Point mode"
731 },
732 {
733 .name = "mssid",
734 .param = INT,
735 .handler = wlc_iovar,
736 .data.str = "mssid",
737 .desc = "Multi-ssid mode"
738 },
739 {
740 .name = "apsta",
741 .param = INT,
742 .handler = wlc_iovar,
743 .data.str = "apsta",
744 .desc = "AP+STA mode"
745 },
746 {
747 .name = "infra",
748 .param = INT,
749 .handler = wlc_ioctl,
750 .data.num = ((WLC_GET_INFRA << 16) | WLC_SET_INFRA),
751 .desc = "Infrastructure mode"
752 },
753 {
754 .name = "wet",
755 .param = INT,
756 .handler = wlc_ioctl,
757 .data.num = ((WLC_GET_WET << 16) | WLC_SET_WET),
758 .desc = "Wireless repeater mode",
759 },
760 {
761 .name = "statimeout",
762 .param = INT,
763 .handler = wlc_iovar,
764 .data.str = "sta_retry_time",
765 .desc = "STA connection timeout"
766 },
767 {
768 .name = "country",
769 .param = STRING,
770 .handler = wlc_ioctl,
771 .data.num = ((WLC_GET_COUNTRY << 16) | WLC_SET_COUNTRY),
772 .desc = "Country code"
773 },
774 {
775 .name = "channel",
776 .param = INT,
777 .handler = wlc_ioctl,
778 .data.num = ((WLC_GET_CHANNEL << 16) | WLC_SET_CHANNEL),
779 .desc = "Channel",
780 },
781 {
782 .name = "vlan_mode",
783 .param = INT,
784 .handler = wlc_bssiovar,
785 .data.str = "vlan_mode",
786 .desc = "Parse 802.1Q tags",
787 },
788 {
789 .name = "vif",
790 .param = INT,
791 .handler = wlc_int,
792 .data.ptr = &vif,
793 .desc = "Current vif index"
794 },
795 {
796 .name = "enabled",
797 .param = INT,
798 .handler = wlc_vif_enabled,
799 .desc = "vif enabled flag"
800 },
801 {
802 .name = "ssid",
803 .param = STRING,
804 .handler = wlc_ssid,
805 .desc = "Interface ESSID"
806 },
807 {
808 .name = "closed",
809 .param = INT,
810 .handler = wlc_bssiovar,
811 .data.str = "closednet",
812 .desc = "Hidden ESSID flag"
813 },
814 {
815 .name = "wsec",
816 .param = INT,
817 .handler = wlc_bssiovar,
818 .data.str = "wsec",
819 .desc = "Security mode flags"
820 },
821 {
822 .name = "wepkey",
823 .param = STRING,
824 .handler = wlc_wsec_key,
825 .desc = "Set/Remove WEP keys"
826 },
827 {
828 .name = "wsec_restrict",
829 .param = INT,
830 .handler = wlc_bssiovar,
831 .data.str = "wsec_restrict",
832 .desc = "Drop unencrypted traffic"
833 },
834 {
835 .name = "eap_restrict",
836 .param = INT,
837 .handler = wlc_bssiovar,
838 .data.str = "eap_restrict",
839 .desc = "Only allow 802.1X traffic until 802.1X authorized"
840 },
841 {
842 .name = "wpa_auth",
843 .param = INT,
844 .handler = wlc_bssiovar,
845 .data.str = "wpa_auth",
846 .desc = "WPA authentication modes"
847 },
848 {
849 .name = "ap_isolate",
850 .param = INT,
851 .handler = wlc_bssiovar,
852 .data.str = "ap_isolate",
853 .desc = "Isolate connected clients"
854 },
855 {
856 .name = "supplicant",
857 .param = INT,
858 .handler = wlc_iovar,
859 .data.str = "sup_wpa",
860 .desc = "Built-in WPA supplicant"
861 },
862 {
863 .name = "maxassoc",
864 .param = INT,
865 .handler = wlc_iovar,
866 .data.str = "maxassoc",
867 .desc = "Max. number of associated clients",
868 },
869 {
870 .name = "wme",
871 .param = INT,
872 .handler = wlc_iovar,
873 .data.str = "wme",
874 .desc = "WME enabled"
875 },
876 {
877 .name = "wme_ac_ap",
878 .param = STRING,
879 .handler = wlc_wme_ac,
880 .data.str = "wme_ac_ap",
881 .desc = "Set WME AC options for AP mode",
882 },
883 {
884 .name = "wme_ac_sta",
885 .param = STRING,
886 .handler = wlc_wme_ac,
887 .data.str = "wme_ac_sta",
888 .desc = "Set WME AC options for STA mode",
889 },
890 {
891 .name = "wme_noack",
892 .param = INT,
893 .handler = wlc_iovar,
894 .data.str = "wme_noack",
895 .desc = "WME ACK disable request",
896 },
897 {
898 .name = "fragthresh",
899 .param = INT,
900 .handler = wlc_iovar,
901 .data.str = "fragthresh",
902 .desc = "Fragmentation threshold",
903 },
904 {
905 .name = "rtsthresh",
906 .param = INT,
907 .handler = wlc_iovar,
908 .data.str = "rtsthresh",
909 .desc = "RTS threshold"
910 },
911 {
912 .name = "rxant",
913 .param = INT,
914 .handler = wlc_ioctl,
915 .data.num = ((WLC_GET_ANTDIV << 16) | WLC_SET_ANTDIV),
916 .desc = "Rx antenna selection"
917 },
918 {
919 .name = "txant",
920 .param = INT,
921 .handler = wlc_ioctl,
922 .data.num = ((WLC_GET_TXANT << 16) | WLC_SET_TXANT),
923 .desc = "Tx antenna selection"
924 },
925 {
926 .name = "dtim",
927 .param = INT,
928 .handler = wlc_ioctl,
929 .data.num = ((WLC_GET_DTIMPRD << 16) | WLC_SET_DTIMPRD),
930 .desc = "DTIM period",
931 },
932 {
933 .name = "bcn",
934 .param = INT,
935 .handler = wlc_ioctl,
936 .data.num = ((WLC_GET_BCNPRD << 16) | WLC_SET_BCNPRD),
937 .desc = "Beacon interval"
938 },
939 {
940 .name = "frameburst",
941 .param = INT,
942 .handler = wlc_ioctl,
943 .data.num = ((WLC_GET_FAKEFRAG << 16) | WLC_SET_FAKEFRAG),
944 .desc = "Framebursting"
945 },
946 {
947 .name = "monitor",
948 .param = INT,
949 .handler = wlc_ioctl,
950 .data.num = ((WLC_GET_MONITOR << 16) | WLC_SET_MONITOR),
951 .desc = "Monitor mode"
952 },
953 {
954 .name = "passive",
955 .param = INT,
956 .handler = wlc_ioctl,
957 .data.num = ((WLC_GET_PASSIVE << 16) | WLC_SET_PASSIVE),
958 .desc = "Passive mode"
959 },
960 {
961 .name = "macfilter",
962 .param = INT,
963 .handler = wlc_ioctl,
964 .data.num = ((WLC_GET_MACMODE << 16) | WLC_SET_MACMODE),
965 .desc = "MAC filter mode (0:disabled, 1:deny, 2:allow)"
966 },
967 {
968 .name = "maclist",
969 .param = STRING,
970 .data.num = ((WLC_GET_MACLIST << 16) | WLC_SET_MACLIST),
971 .handler = wlc_maclist,
972 .desc = "MAC filter list"
973 },
974 {
975 .name = "autowds",
976 .param = INT,
977 .handler = wlc_ioctl,
978 .data.num = ((WLC_GET_LAZYWDS << 16) | WLC_SET_LAZYWDS),
979 .desc = "Automatic WDS"
980 },
981 {
982 .name = "wds",
983 .param = STRING,
984 .data.num = ((WLC_GET_WDSLIST << 16) | WLC_SET_WDSLIST),
985 .handler = wlc_maclist,
986 .desc = "WDS connection list"
987 },
988 {
989 .name = "wdstimeout",
990 .param = INT,
991 .handler = wlc_iovar,
992 .data.str = "wdstimeout",
993 .desc = "WDS link detection timeout"
994 },
995 {
996 .name = "wdsmac",
997 .param = STRING|NOARG,
998 .handler = wlc_wdsmac,
999 .desc = "MAC of the remote WDS endpoint (only with wds0.* interfaces)"
1000 },
1001 {
1002 .name = "afterburner",
1003 .param = INT,
1004 .handler = wlc_afterburner,
1005 .desc = "Broadcom Afterburner"
1006 },
1007 {
1008 .name = "slottime",
1009 .param = INT,
1010 .handler = wlc_slottime,
1011 .desc = "Slot time (-1 = auto)"
1012 },
1013 {
1014 .name = "txack",
1015 .param = INT,
1016 .handler = wlc_noack,
1017 .desc = "Tx ACK enabled flag"
1018 },
1019 {
1020 .name = "ibss_merge",
1021 .param = INT,
1022 .handler = wlc_ibss_merge,
1023 .desc = "Allow IBSS merge in Ad-Hoc mode"
1024 }
1025 };
1026 #define wlc_calls_size (sizeof(wlc_calls) / sizeof(struct wlc_call))
1027
1028 static void usage(char *cmd)
1029 {
1030 int i;
1031 fprintf(stderr, "Usage: %s <command> [<argument> ...]\n"
1032 "\n"
1033 "Available commands:\n", cmd);
1034 for (i = 0; i < wlc_calls_size; i++) {
1035 fprintf(stderr, "\t%-16s\t%s\n", wlc_calls[i].name ?: "", wlc_calls[i].desc ?: "");
1036 }
1037 fprintf(stderr, "\n");
1038 exit(1);
1039 }
1040
1041 static int do_command(const struct wlc_call *cmd, char *arg)
1042 {
1043 static char buf[BUFSIZE];
1044 int set;
1045 int ret = 0;
1046 char *format, *end;
1047 int intval;
1048
1049 if (debug >= 10) {
1050 fprintf(stderr, "do_command %-16s\t'%s'\n", cmd->name, arg);
1051 }
1052
1053 if ((arg == NULL) && ((cmd->param & PARAM_TYPE) != NONE)) {
1054 set = 0;
1055 ret = cmd->handler(cmd->param | GET, (void *) &cmd->data, (void *) buf);
1056 if (ret == 0) {
1057 switch(cmd->param & PARAM_TYPE) {
1058 case INT:
1059 intval = *((int *) buf);
1060
1061 if (intval > 65535)
1062 format = "0x%08x\n";
1063 else if (intval > 255)
1064 format = "0x%04x\n";
1065 else
1066 format = "%d\n";
1067
1068 fprintf(stdout, format, intval);
1069 break;
1070 case STRING:
1071 fprintf(stdout, "%s\n", buf);
1072 }
1073 }
1074 } else { /* SET */
1075 set = 1;
1076 switch(cmd->param & PARAM_TYPE) {
1077 case INT:
1078 intval = strtoul(arg, &end, 10);
1079 if (end && !(*end)) {
1080 memcpy(buf, &intval, sizeof(intval));
1081 } else {
1082 fprintf(stderr, "%s: Invalid argument\n", cmd->name);
1083 return -1;
1084 }
1085 break;
1086 case STRING:
1087 strncpy(buf, arg, BUFSIZE);
1088 buf[BUFSIZE - 1] = 0;
1089 }
1090
1091 ret = cmd->handler(cmd->param | SET, (void *) &cmd->data, (void *) buf);
1092 }
1093
1094 if ((debug > 0) && (ret != 0))
1095 fprintf(stderr, "Command '%s %s' failed: %d\n", (set == 1 ? "set" : "get"), cmd->name, ret);
1096
1097 return ret;
1098 }
1099
1100 static struct wlc_call *find_cmd(char *name)
1101 {
1102 int found = 0, i = 0;
1103
1104 while (!found && (i < wlc_calls_size)) {
1105 if (strcmp(name, wlc_calls[i].name) == 0)
1106 found = 1;
1107 else
1108 i++;
1109 }
1110
1111 return (struct wlc_call *) (found ? &wlc_calls[i] : NULL);
1112 }
1113
1114 int main(int argc, char **argv)
1115 {
1116 static char buf[BUFSIZE];
1117 char *s, *s2;
1118 char *cmd = argv[0];
1119 struct wlc_call *call;
1120 int ret = 0;
1121
1122 if (argc < 2)
1123 usage(argv[0]);
1124
1125 for(interface[2] = '0'; (interface[2] < '3') && (wl_probe(interface) != 0); interface[2]++);
1126 if (interface[2] == '3') {
1127 fprintf(stderr, "No Broadcom wl interface found!\n");
1128 return -1;
1129 }
1130
1131 argv++;
1132 argc--;
1133 while ((argc > 0) && (argv[0] != NULL)) {
1134 if ((call = find_cmd(argv[0])) == NULL) {
1135 fprintf(stderr, "Invalid command: %s\n\n", argv[0]);
1136 usage(cmd);
1137 }
1138 if ((argc > 1) && (!(call->param & NOARG))) {
1139 ret = do_command(call, argv[1]);
1140 argv += 2;
1141 argc -= 2;
1142 } else {
1143 ret = do_command(call, NULL);
1144 argv++;
1145 argc--;
1146 }
1147 }
1148
1149 while (fromstdin && !feof(stdin)) {
1150 *buf = 0;
1151 fgets(buf, BUFSIZE - 1, stdin);
1152
1153 if (*buf == 0)
1154 continue;
1155
1156 if ((s = strchr(buf, '\r')) != NULL)
1157 *s = 0;
1158 if ((s = strchr(buf, '\n')) != NULL)
1159 *s = 0;
1160
1161 s = buf;
1162 while (isspace(*s))
1163 s++;
1164
1165 if (!*s)
1166 continue;
1167
1168 if ((s2 = strchr(buf, ' ')) != NULL)
1169 *(s2++) = 0;
1170
1171 while (s2 && isspace(*s2))
1172 s2++;
1173
1174 if ((call = find_cmd(buf)) == NULL) {
1175 fprintf(stderr, "Invalid command: %s\n", buf);
1176 ret = -1;
1177 } else
1178 ret = do_command(call, ((call->param & NOARG) ? NULL : s2));
1179 }
1180
1181 return ret;
1182 }