disable kmod-ip6tables if netfilter ipv6 support is disabled
[openwrt.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 = "vif",
747 .param = INT,
748 .handler = wlc_int,
749 .data.ptr = &vif,
750 .desc = "Current vif index"
751 },
752 {
753 .name = "enabled",
754 .param = INT,
755 .handler = wlc_vif_enabled,
756 .desc = "vif enabled flag"
757 },
758 {
759 .name = "ssid",
760 .param = STRING,
761 .handler = wlc_ssid,
762 .desc = "Interface ESSID"
763 },
764 {
765 .name = "closed",
766 .param = INT,
767 .handler = wlc_bssiovar,
768 .data.str = "closednet",
769 .desc = "Hidden ESSID flag"
770 },
771 {
772 .name = "wsec",
773 .param = INT,
774 .handler = wlc_bssiovar,
775 .data.str = "wsec",
776 .desc = "Security mode flags"
777 },
778 {
779 .name = "wepkey",
780 .param = STRING,
781 .handler = wlc_wsec_key,
782 .desc = "Set/Remove WEP keys"
783 },
784 {
785 .name = "wsec_restrict",
786 .param = INT,
787 .handler = wlc_bssiovar,
788 .data.str = "wsec_restrict",
789 .desc = "Drop unencrypted traffic"
790 },
791 {
792 .name = "eap_restrict",
793 .param = INT,
794 .handler = wlc_bssiovar,
795 .data.str = "eap_restrict",
796 .desc = "Only allow 802.1X traffic until 802.1X authorized"
797 },
798 {
799 .name = "wpa_auth",
800 .param = INT,
801 .handler = wlc_bssiovar,
802 .data.str = "wpa_auth",
803 .desc = "WPA authentication modes"
804 },
805 {
806 .name = "ap_isolate",
807 .param = INT,
808 .handler = wlc_bssiovar,
809 .data.str = "ap_isolate",
810 .desc = "Isolate connected clients"
811 },
812 {
813 .name = "supplicant",
814 .param = INT,
815 .handler = wlc_iovar,
816 .data.str = "sup_wpa",
817 .desc = "Built-in WPA supplicant"
818 },
819 {
820 .name = "maxassoc",
821 .param = INT,
822 .handler = wlc_iovar,
823 .data.str = "maxassoc",
824 .desc = "Max. number of associated clients",
825 },
826 {
827 .name = "wme",
828 .param = INT,
829 .handler = wlc_iovar,
830 .data.str = "wme",
831 .desc = "WME enabled"
832 },
833 {
834 .name = "wme_ac_ap",
835 .param = STRING,
836 .handler = wlc_wme_ac,
837 .data.str = "wme_ac_ap",
838 .desc = "Set WME AC options for AP mode",
839 },
840 {
841 .name = "wme_ac_sta",
842 .param = STRING,
843 .handler = wlc_wme_ac,
844 .data.str = "wme_ac_sta",
845 .desc = "Set WME AC options for STA mode",
846 },
847 {
848 .name = "wme_noack",
849 .param = INT,
850 .handler = wlc_iovar,
851 .data.str = "wme_noack",
852 .desc = "WME ACK disable request",
853 },
854 {
855 .name = "fragthresh",
856 .param = INT,
857 .handler = wlc_iovar,
858 .data.str = "fragthresh",
859 .desc = "Fragmentation threshold",
860 },
861 {
862 .name = "rtsthresh",
863 .param = INT,
864 .handler = wlc_iovar,
865 .data.str = "rtsthresh",
866 .desc = "RTS threshold"
867 },
868 {
869 .name = "rxant",
870 .param = INT,
871 .handler = wlc_ioctl,
872 .data.num = ((WLC_GET_ANTDIV << 16) | WLC_SET_ANTDIV),
873 .desc = "Rx antenna selection"
874 },
875 {
876 .name = "txant",
877 .param = INT,
878 .handler = wlc_ioctl,
879 .data.num = ((WLC_GET_TXANT << 16) | WLC_SET_TXANT),
880 .desc = "Tx antenna selection"
881 },
882 {
883 .name = "dtim",
884 .param = INT,
885 .handler = wlc_ioctl,
886 .data.num = ((WLC_GET_DTIMPRD << 16) | WLC_SET_DTIMPRD),
887 .desc = "DTIM period",
888 },
889 {
890 .name = "bcn",
891 .param = INT,
892 .handler = wlc_ioctl,
893 .data.num = ((WLC_GET_BCNPRD << 16) | WLC_SET_BCNPRD),
894 .desc = "Beacon interval"
895 },
896 {
897 .name = "frameburst",
898 .param = INT,
899 .handler = wlc_ioctl,
900 .data.num = ((WLC_GET_FAKEFRAG << 16) | WLC_SET_FAKEFRAG),
901 .desc = "Framebursting"
902 },
903 {
904 .name = "monitor",
905 .param = INT,
906 .handler = wlc_ioctl,
907 .data.num = ((WLC_GET_MONITOR << 16) | WLC_SET_MONITOR),
908 .desc = "Monitor mode"
909 },
910 {
911 .name = "passive",
912 .param = INT,
913 .handler = wlc_ioctl,
914 .data.num = ((WLC_GET_PASSIVE << 16) | WLC_SET_PASSIVE),
915 .desc = "Passive mode"
916 },
917 {
918 .name = "macfilter",
919 .param = INT,
920 .handler = wlc_ioctl,
921 .data.num = ((WLC_GET_MACMODE << 16) | WLC_SET_MACMODE),
922 .desc = "MAC filter mode (0:disabled, 1:deny, 2:allow)"
923 },
924 {
925 .name = "maclist",
926 .param = STRING,
927 .data.num = ((WLC_GET_MACLIST << 16) | WLC_SET_MACLIST),
928 .handler = wlc_maclist,
929 .desc = "MAC filter list"
930 },
931 {
932 .name = "autowds",
933 .param = INT,
934 .handler = wlc_ioctl,
935 .data.num = ((WLC_GET_LAZYWDS << 16) | WLC_SET_LAZYWDS),
936 .desc = "Automatic WDS"
937 },
938 {
939 .name = "wds",
940 .param = STRING,
941 .data.num = ((WLC_GET_WDSLIST << 16) | WLC_SET_WDSLIST),
942 .handler = wlc_maclist,
943 .desc = "WDS connection list"
944 },
945 {
946 .name = "wdstimeout",
947 .param = INT,
948 .handler = wlc_iovar,
949 .data.str = "wdstimeout",
950 .desc = "WDS link detection timeout"
951 },
952 {
953 .name = "afterburner",
954 .param = INT,
955 .handler = wlc_afterburner,
956 .desc = "Broadcom Afterburner"
957 },
958 {
959 .name = "slottime",
960 .param = INT,
961 .handler = wlc_slottime,
962 .desc = "Slot time (-1 = auto)"
963 },
964 {
965 .name = "txack",
966 .param = INT,
967 .handler = wlc_noack,
968 .desc = "Tx ACK enabled flag"
969 },
970 {
971 .name = "ibss_merge",
972 .param = INT,
973 .handler = wlc_ibss_merge,
974 .desc = "Allow IBSS merge in Ad-Hoc mode"
975 }
976 };
977 #define wlc_calls_size (sizeof(wlc_calls) / sizeof(struct wlc_call))
978
979 static void usage(char *cmd)
980 {
981 int i;
982 fprintf(stderr, "Usage: %s <command> [<argument> ...]\n"
983 "\n"
984 "Available commands:\n", cmd);
985 for (i = 0; i < wlc_calls_size; i++) {
986 fprintf(stderr, "\t%-16s\t%s\n", wlc_calls[i].name ?: "", wlc_calls[i].desc ?: "");
987 }
988 fprintf(stderr, "\n");
989 exit(1);
990 }
991
992 static int do_command(const struct wlc_call *cmd, char *arg)
993 {
994 static char buf[BUFSIZE];
995 int set;
996 int ret = 0;
997 char *format, *end;
998 int intval;
999
1000 if (debug >= 10) {
1001 fprintf(stderr, "do_command %-16s\t'%s'\n", cmd->name, arg);
1002 }
1003
1004 if ((arg == NULL) && ((cmd->param & PARAM_TYPE) != NONE)) {
1005 set = 0;
1006 ret = cmd->handler(cmd->param | GET, (void *) &cmd->data, (void *) buf);
1007 if (ret == 0) {
1008 switch(cmd->param & PARAM_TYPE) {
1009 case INT:
1010 intval = *((int *) buf);
1011
1012 if (intval > 65535)
1013 format = "0x%08x\n";
1014 else if (intval > 255)
1015 format = "0x%04x\n";
1016 else
1017 format = "%d\n";
1018
1019 fprintf(stdout, format, intval);
1020 break;
1021 case STRING:
1022 fprintf(stdout, "%s\n", buf);
1023 }
1024 }
1025 } else { /* SET */
1026 set = 1;
1027 switch(cmd->param & PARAM_TYPE) {
1028 case INT:
1029 intval = strtoul(arg, &end, 10);
1030 if (end && !(*end)) {
1031 memcpy(buf, &intval, sizeof(intval));
1032 } else {
1033 fprintf(stderr, "%s: Invalid argument\n", cmd->name);
1034 return -1;
1035 }
1036 break;
1037 case STRING:
1038 strncpy(buf, arg, BUFSIZE);
1039 buf[BUFSIZE - 1] = 0;
1040 }
1041
1042 ret = cmd->handler(cmd->param | SET, (void *) &cmd->data, (void *) buf);
1043 }
1044
1045 if ((debug > 0) && (ret != 0))
1046 fprintf(stderr, "Command '%s %s' failed: %d\n", (set == 1 ? "set" : "get"), cmd->name, ret);
1047
1048 return ret;
1049 }
1050
1051 static struct wlc_call *find_cmd(char *name)
1052 {
1053 int found = 0, i = 0;
1054
1055 while (!found && (i < wlc_calls_size)) {
1056 if (strcmp(name, wlc_calls[i].name) == 0)
1057 found = 1;
1058 else
1059 i++;
1060 }
1061
1062 return (struct wlc_call *) (found ? &wlc_calls[i] : NULL);
1063 }
1064
1065 int main(int argc, char **argv)
1066 {
1067 static char buf[BUFSIZE];
1068 char *s, *s2;
1069 char *cmd = argv[0];
1070 struct wlc_call *call;
1071 int ret = 0;
1072
1073 if (argc < 2)
1074 usage(argv[0]);
1075
1076 for(interface[2] = '0'; (interface[2] < '3') && (wl_probe(interface) != 0); interface[2]++);
1077 if (interface[2] == '3') {
1078 fprintf(stderr, "No Broadcom wl interface found!\n");
1079 return -1;
1080 }
1081
1082 argv++;
1083 argc--;
1084 while ((argc > 0) && (argv[0] != NULL)) {
1085 if ((call = find_cmd(argv[0])) == NULL) {
1086 fprintf(stderr, "Invalid command: %s\n\n", argv[0]);
1087 usage(cmd);
1088 }
1089 if ((argc > 1) && (!(call->param & NOARG))) {
1090 ret = do_command(call, argv[1]);
1091 argv += 2;
1092 argc -= 2;
1093 } else {
1094 ret = do_command(call, NULL);
1095 argv++;
1096 argc--;
1097 }
1098 }
1099
1100 while (fromstdin && !feof(stdin)) {
1101 *buf = 0;
1102 fgets(buf, BUFSIZE - 1, stdin);
1103
1104 if (*buf == 0)
1105 continue;
1106
1107 if ((s = strchr(buf, '\r')) != NULL)
1108 *s = 0;
1109 if ((s = strchr(buf, '\n')) != NULL)
1110 *s = 0;
1111
1112 s = buf;
1113 while (isspace(*s))
1114 s++;
1115
1116 if (!*s)
1117 continue;
1118
1119 if ((s2 = strchr(buf, ' ')) != NULL)
1120 *(s2++) = 0;
1121
1122 while (s2 && isspace(*s2))
1123 s2++;
1124
1125 if ((call = find_cmd(buf)) == NULL) {
1126 fprintf(stderr, "Invalid command: %s\n", buf);
1127 ret = -1;
1128 } else
1129 ret = do_command(call, ((call->param & NOARG) ? NULL : s2));
1130 }
1131
1132 return ret;
1133 }
This page took 0.094468 seconds and 5 git commands to generate.