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