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