wlc: use custom ether_ntoa implementation that pads the address parts
[librecmc/librecmc.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 = "fragthresh",
904                 .param = INT,
905                 .handler = wlc_iovar,
906                 .data.str = "fragthresh",
907                 .desc = "Fragmentation threshold",
908         },
909         {
910                 .name = "rtsthresh",
911                 .param = INT,
912                 .handler = wlc_iovar,
913                 .data.str = "rtsthresh",
914                 .desc = "RTS threshold"
915         },
916         {
917                 .name = "rxant",
918                 .param = INT,
919                 .handler = wlc_ioctl,
920                 .data.num = ((WLC_GET_ANTDIV << 16) | WLC_SET_ANTDIV),
921                 .desc = "Rx antenna selection"
922         },
923         {
924                 .name = "txant",
925                 .param = INT,
926                 .handler = wlc_ioctl,
927                 .data.num = ((WLC_GET_TXANT << 16) | WLC_SET_TXANT),
928                 .desc = "Tx antenna selection"
929         },
930         {
931                 .name = "dtim",
932                 .param = INT,
933                 .handler = wlc_ioctl,
934                 .data.num = ((WLC_GET_DTIMPRD << 16) | WLC_SET_DTIMPRD),
935                 .desc = "DTIM period",
936         },
937         {
938                 .name = "bcn",
939                 .param = INT,
940                 .handler = wlc_ioctl,
941                 .data.num = ((WLC_GET_BCNPRD << 16) | WLC_SET_BCNPRD),
942                 .desc = "Beacon interval"
943         },
944         {
945                 .name = "frameburst",
946                 .param = INT,
947                 .handler = wlc_ioctl,
948                 .data.num = ((WLC_GET_FAKEFRAG << 16) | WLC_SET_FAKEFRAG),
949                 .desc = "Framebursting"
950         },
951         {
952                 .name = "monitor",
953                 .param = INT,
954                 .handler = wlc_ioctl,
955                 .data.num = ((WLC_GET_MONITOR << 16) | WLC_SET_MONITOR),
956                 .desc = "Monitor mode"
957         },
958         {
959                 .name = "passive",
960                 .param = INT,
961                 .handler = wlc_ioctl,
962                 .data.num = ((WLC_GET_PASSIVE << 16) | WLC_SET_PASSIVE),
963                 .desc = "Passive mode"
964         },
965         {
966                 .name = "macfilter",
967                 .param = INT,
968                 .handler = wlc_ioctl,
969                 .data.num = ((WLC_GET_MACMODE << 16) | WLC_SET_MACMODE),
970                 .desc = "MAC filter mode (0:disabled, 1:deny, 2:allow)"
971         },
972         {
973                 .name = "maclist",
974                 .param = STRING,
975                 .data.num = ((WLC_GET_MACLIST << 16) | WLC_SET_MACLIST),
976                 .handler = wlc_maclist,
977                 .desc = "MAC filter list"
978         },
979         {
980                 .name = "autowds",
981                 .param = INT,
982                 .handler = wlc_ioctl,
983                 .data.num = ((WLC_GET_LAZYWDS << 16) | WLC_SET_LAZYWDS),
984                 .desc = "Automatic WDS"
985         },
986         {
987                 .name = "wds",
988                 .param = STRING,
989                 .data.num = ((WLC_GET_WDSLIST << 16) | WLC_SET_WDSLIST),
990                 .handler = wlc_maclist,
991                 .desc = "WDS connection list"
992         },
993         {
994                 .name = "wdstimeout",
995                 .param = INT,
996                 .handler = wlc_iovar,
997                 .data.str = "wdstimeout",
998                 .desc = "WDS link detection timeout"
999         },
1000         {
1001                 .name = "wdsmac",
1002                 .param = STRING|NOARG,
1003                 .handler = wlc_wdsmac,
1004                 .desc = "MAC of the remote WDS endpoint (only with wds0.* interfaces)"
1005         },
1006         {
1007                 .name = "afterburner",
1008                 .param = INT,
1009                 .handler = wlc_afterburner,
1010                 .desc = "Broadcom Afterburner"
1011         },
1012         {
1013                 .name = "slottime",
1014                 .param = INT,
1015                 .handler = wlc_slottime,
1016                 .desc = "Slot time (-1 = auto)"
1017         },
1018         {
1019                 .name = "txack",
1020                 .param = INT,
1021                 .handler = wlc_noack,
1022                 .desc = "Tx ACK enabled flag"
1023         },
1024         {
1025                 .name = "ibss_merge",
1026                 .param = INT,
1027                 .handler = wlc_ibss_merge,
1028                 .desc = "Allow IBSS merge in Ad-Hoc mode"
1029         }
1030 };
1031 #define wlc_calls_size (sizeof(wlc_calls) / sizeof(struct wlc_call))
1032
1033 static void usage(char *cmd)
1034 {
1035         int i;
1036         fprintf(stderr, "Usage: %s <command> [<argument> ...]\n"
1037                                         "\n"
1038                                         "Available commands:\n", cmd);
1039         for (i = 0; i < wlc_calls_size; i++) {
1040                 fprintf(stderr, "\t%-16s\t%s\n", wlc_calls[i].name ?: "", wlc_calls[i].desc ?: "");
1041         }
1042         fprintf(stderr, "\n");
1043         exit(1);
1044 }
1045
1046 static int do_command(const struct wlc_call *cmd, char *arg)
1047 {
1048         static char buf[BUFSIZE];
1049         int set;
1050         int ret = 0;
1051         char *format, *end;
1052         int intval;
1053
1054         if (debug >= 10) {
1055                 fprintf(stderr, "do_command %-16s\t'%s'\n", cmd->name, arg);
1056         }
1057         
1058         if ((arg == NULL) && ((cmd->param & PARAM_TYPE) != NONE)) {
1059                 set = 0;
1060                 ret = cmd->handler(cmd->param | GET, (void *) &cmd->data, (void *) buf);
1061                 if (ret == 0) {
1062                         switch(cmd->param & PARAM_TYPE) {
1063                                 case INT:
1064                                         intval = *((int *) buf);
1065                                         
1066                                         if (intval > 65535)
1067                                                 format = "0x%08x\n";
1068                                         else if (intval > 255)
1069                                                 format = "0x%04x\n";
1070                                         else
1071                                                 format = "%d\n";
1072                                         
1073                                         fprintf(stdout, format, intval);
1074                                         break;
1075                                 case STRING:
1076                                         fprintf(stdout, "%s\n", buf);
1077                         }
1078                 }
1079         } else { /* SET */
1080                 set = 1;
1081                 switch(cmd->param & PARAM_TYPE) {
1082                         case INT:
1083                                 intval = strtoul(arg, &end, 10);
1084                                 if (end && !(*end)) {
1085                                         memcpy(buf, &intval, sizeof(intval));
1086                                 } else {
1087                                         fprintf(stderr, "%s: Invalid argument\n", cmd->name);
1088                                         return -1;
1089                                 }
1090                                 break;
1091                         case STRING:
1092                                 strncpy(buf, arg, BUFSIZE);
1093                                 buf[BUFSIZE - 1] = 0;
1094                 }
1095
1096                 ret = cmd->handler(cmd->param | SET, (void *) &cmd->data, (void *) buf);
1097         }
1098         
1099         if ((debug > 0) && (ret != 0)) 
1100                 fprintf(stderr, "Command '%s %s' failed: %d\n", (set == 1 ? "set" : "get"), cmd->name, ret);
1101         
1102         return ret;
1103 }
1104
1105 static struct wlc_call *find_cmd(char *name)
1106 {
1107         int found = 0, i = 0;
1108
1109         while (!found && (i < wlc_calls_size)) {
1110                 if (strcmp(name, wlc_calls[i].name) == 0)
1111                         found = 1;
1112                 else
1113                         i++;
1114         }
1115
1116         return (struct wlc_call *) (found ? &wlc_calls[i] : NULL);
1117 }
1118
1119 int main(int argc, char **argv)
1120 {
1121         static char buf[BUFSIZE];
1122         char *s, *s2;
1123         char *cmd = argv[0];
1124         struct wlc_call *call;
1125         int ret = 0;
1126
1127         if (argc < 2)
1128                 usage(argv[0]);
1129
1130         for(interface[2] = '0'; (interface[2] < '3') && (wl_probe(interface) != 0); interface[2]++);
1131         if (interface[2] == '3') {
1132                 fprintf(stderr, "No Broadcom wl interface found!\n");
1133                 return -1;
1134         }
1135
1136         argv++;
1137         argc--;
1138         while ((argc > 0) && (argv[0] != NULL)) {
1139                 if ((call = find_cmd(argv[0])) == NULL) {
1140                         fprintf(stderr, "Invalid command: %s\n\n", argv[0]);
1141                         usage(cmd);
1142                 }
1143                 if ((argc > 1) && (!(call->param & NOARG))) {
1144                         ret = do_command(call, argv[1]);
1145                         argv += 2;
1146                         argc -= 2;
1147                 } else {
1148                         ret = do_command(call, NULL);
1149                         argv++;
1150                         argc--;
1151                 }
1152         }
1153
1154         while (fromstdin && !feof(stdin)) {
1155                 *buf = 0;
1156                 fgets(buf, BUFSIZE - 1, stdin);
1157                 
1158                 if (*buf == 0)
1159                         continue;
1160                 
1161                 if ((s = strchr(buf, '\r')) != NULL)
1162                         *s = 0;
1163                 if ((s = strchr(buf, '\n')) != NULL)
1164                         *s = 0;
1165
1166                 s = buf;
1167                 while (isspace(*s))
1168                         s++;
1169
1170                 if (!*s)
1171                         continue;
1172         
1173                 if ((s2 = strchr(buf, ' ')) != NULL)
1174                         *(s2++) = 0;
1175                 
1176                 while (s2 && isspace(*s2))
1177                         s2++;
1178                 
1179                 if ((call = find_cmd(buf)) == NULL) {
1180                         fprintf(stderr, "Invalid command: %s\n", buf);
1181                         ret = -1;
1182                 } else
1183                         ret = do_command(call, ((call->param & NOARG) ? NULL : s2));
1184         }
1185
1186         return ret;
1187 }