2 * wlc - Broadcom Wireless Driver Control Utility
4 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
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.
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.
17 #include <sys/types.h>
29 #include <proto/802.11.h>
33 #define PTABLE_MAGIC 0xbadc0ded
38 #define PTABLE_END 0xffffffff
41 * Copy each token in wordlist delimited by space into word
42 * Taken from Broadcom shutils.h
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, ' '); \
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, ' '))
57 static char wlbuf
[8192];
58 static char interface
[16] = "wl0";
59 static unsigned long kmem_offset
= 0;
60 static int vif
= 0, debug
= 1, fromstdin
= 0;
71 PARAM_OPTIONS
= 0x0f0,
83 int (*handler
)(wlc_param param
, void *data
, void *value
);
92 /* can't use the system include because of the stupid broadcom header files */
93 extern struct ether_addr
*ether_aton(const char *asc
);
94 static inline int my_ether_ntoa(unsigned char *ea
, char *buf
)
96 return sprintf(buf
, "%02x:%02x:%02x:%02x:%02x:%02x",
97 ea
[0], ea
[1], ea
[2], ea
[3], ea
[4], ea
[5]);
100 static int wlc_ioctl(wlc_param param
, void *data
, void *value
)
102 unsigned int *var
= ((unsigned int *) data
);
103 unsigned int ioc
= *var
;
106 return wl_ioctl(interface
, ioc
, NULL
, 0);
108 switch(param
& PARAM_TYPE
) {
110 return wl_ioctl(interface
, ((param
& SET
) ? (ioc
) : (ioc
>> 16)) & 0xffff, value
, sizeof(int));
112 return wl_ioctl(interface
, ((param
& SET
) ? (ioc
) : (ioc
>> 16)) & 0xffff, value
, BUFSIZE
);
117 static int wlc_iovar(wlc_param param
, void *data
, void *value
)
119 int *val
= (int *) value
;
120 char *iov
= *((char **) data
);
124 switch(param
& PARAM_TYPE
) {
126 ret
= wl_iovar_setint(interface
, iov
, *val
);
130 switch(param
& PARAM_TYPE
) {
132 ret
= wl_iovar_getint(interface
, iov
, val
);
139 static int wlc_bssiovar(wlc_param param
, void *data
, void *value
)
141 int *val
= (int *) value
;
142 char *iov
= *((char **) data
);
146 switch(param
& PARAM_TYPE
) {
148 ret
= wl_bssiovar_setint(interface
, iov
, vif
, *val
);
152 switch(param
& PARAM_TYPE
) {
154 ret
= wl_bssiovar_getint(interface
, iov
, vif
, val
);
161 static int wlc_vif_enabled(wlc_param param
, void *data
, void *value
)
163 int *val
= (int *) value
;
167 sprintf((char *) buf
, "bss");
170 buf
[2] = (*val
? 1 : 0);
171 ret
= wl_ioctl(interface
, WLC_SET_VAR
, buf
, sizeof(buf
));
172 } else if (param
& GET
) {
173 ret
= wl_ioctl(interface
, WLC_GET_VAR
, buf
, sizeof(buf
));
180 static int wlc_ssid(wlc_param param
, void *data
, void *value
)
182 int ret
= -1, ret2
= -1;
183 char *dest
= (char *) value
;
186 if ((param
& PARAM_MODE
) == GET
) {
187 ret
= wl_bssiovar_get(interface
, "ssid", vif
, &ssid
, sizeof(ssid
));
190 /* if we can't get the ssid through the bssiovar, try WLC_GET_SSID */
191 ret
= wl_ioctl(interface
, WLC_GET_SSID
, &ssid
, sizeof(ssid
));
194 memcpy(dest
, ssid
.SSID
, ssid
.SSID_len
);
195 dest
[ssid
.SSID_len
] = 0;
197 } else if ((param
& PARAM_MODE
) == SET
) {
198 strncpy(ssid
.SSID
, value
, 32);
199 ssid
.SSID_len
= strlen(value
);
201 if (ssid
.SSID_len
> 32)
205 /* for the main interface, also try the WLC_SET_SSID call */
206 ret2
= wl_ioctl(interface
, WLC_SET_SSID
, &ssid
, sizeof(ssid
));
209 ret
= wl_bssiovar_set(interface
, "ssid", vif
, &ssid
, sizeof(ssid
));
210 ret
= (!ret2
? 0 : ret
);
216 static int wlc_int(wlc_param param
, void *data
, void *value
)
218 int *var
= *((int **) data
);
219 int *val
= (int *) value
;
221 if ((param
& PARAM_MODE
) == SET
) {
223 } else if ((param
& PARAM_MODE
) == GET
) {
230 static int wlc_flag(wlc_param param
, void *data
, void *value
)
232 int *var
= *((int **) data
);
239 static int wlc_string(wlc_param param
, void *data
, void *value
)
241 char *var
= *((char **) data
);
243 if ((param
& PARAM_MODE
) == GET
) {
250 static int wlc_afterburner(wlc_param param
, void *data
, void *value
)
252 int *val
= (int *) value
;
255 if ((param
& PARAM_MODE
) == GET
) {
256 ret
= wl_iovar_getint(interface
, "afterburner", val
);
258 wl_iovar_setint(interface
, "wlfeatureflag", (*val
? 3 : 0));
259 ret
= wl_iovar_setint(interface
, "afterburner", (*val
? 1 : 0));
260 wl_iovar_setint(interface
, "afterburner_override", *val
);
266 static int wlc_maclist(wlc_param param
, void *data
, void *value
)
268 unsigned int *var
= ((unsigned int *) data
);
269 unsigned int ioc
= *var
;
270 int limit
= (sizeof(wlbuf
) - 4) / sizeof(struct ether_addr
);
271 struct maclist
*list
= (struct maclist
*) wlbuf
;
272 char *str
= (char *) value
;
274 struct ether_addr
*addr
;
278 if ((param
& PARAM_MODE
) == GET
) {
280 ret
= wl_ioctl(interface
, (ioc
>> 16) & 0xffff, wlbuf
, sizeof(wlbuf
));
283 while (list
->count
) {
284 str
+= sprintf(str
, "%s", ((((char *) value
) == str
) ? "" : " "));
285 str
+= my_ether_ntoa((unsigned char *) &list
->ea
[list
->count
-- - 1], str
);
290 while (*str
&& isspace(*str
))
297 if (wl_ioctl(interface
, (ioc
>> 16) & 0xffff, wlbuf
, sizeof(wlbuf
)) == 0)
300 while (*str
&& isspace(*str
))
305 memset(wlbuf
, 0, sizeof(wlbuf
));
307 foreach(astr
, str
, p
) {
308 if (list
->count
>= limit
)
311 if ((addr
= ether_aton(astr
)) != NULL
)
312 memcpy(&list
->ea
[list
->count
++], addr
, sizeof(struct ether_addr
));
315 return wl_ioctl(interface
, ioc
& 0xffff, wlbuf
, sizeof(wlbuf
));
319 static int wlc_radio(wlc_param param
, void *data
, void *value
)
321 int *val
= (int *) value
;
324 if ((param
& PARAM_MODE
) == GET
) {
325 ret
= wl_ioctl(interface
, WLC_GET_RADIO
, val
, sizeof(int));
326 *val
= ((*val
& 1) ? 0 : 1);
328 *val
= (1 << 16) | (*val
? 0 : 1);
329 ret
= wl_ioctl(interface
, WLC_SET_RADIO
, val
, sizeof(int));
335 static int wlc_wsec_key(wlc_param param
, void *null
, void *value
)
337 wl_wsec_key_t wsec_key
;
338 unsigned char *index
= value
;
341 unsigned char hex
[3];
343 if ((param
& PARAM_MODE
) != SET
)
346 memset(&wsec_key
, 0, sizeof(wsec_key
));
347 if (index
[0] == '=') {
348 wsec_key
.flags
= WL_PRIMARY_KEY
;
352 if ((index
[0] < '1') || (index
[0] > '4') || (index
[1] != ','))
356 if (strncmp(key
, "d:", 2) == 0) { /* delete key */
357 } else if (strncmp(key
, "s:", 2) == 0) { /* ascii key */
359 wsec_key
.len
= strlen(key
);
361 if ((wsec_key
.len
!= 5) && (wsec_key
.len
!= 13))
364 strcpy(wsec_key
.data
, key
);
365 } else { /* hex key */
366 wsec_key
.len
= strlen(key
);
367 if ((wsec_key
.len
!= 10) && (wsec_key
.len
!= 26))
371 data
= wsec_key
.data
;
376 *(data
++) = (unsigned char) strtoul(hex
, NULL
, 16);
380 return wl_bssiovar_set(interface
, "wsec_key", vif
, &wsec_key
, sizeof(wsec_key
));
383 static inline int cw2ecw(int cw
)
386 for (cw
++, i
= 0; cw
; i
++) cw
>>=1;
390 static int wlc_wme_ac(wlc_param param
, void *data
, void *value
)
392 char *type
= *((char **) data
);
393 char *settings
= (char *) value
;
394 char cmd
[100], *p
, *val
;
395 edcf_acparam_t params
[AC_COUNT
];
401 if ((param
& PARAM_MODE
) != SET
)
404 memset(params
, 0, sizeof(params
));
405 ret
= wl_iovar_get(interface
, type
, params
, sizeof(params
));
406 memset(buf
, 0, BUFSIZE
);
408 buf
+= strlen(buf
) + 1;
410 foreach(cmd
, settings
, p
) {
411 val
= strchr(cmd
, '=');
413 if (strcmp(cmd
, "be") == 0)
415 else if (strcmp(cmd
, "bk") == 0)
417 else if (strcmp(cmd
, "vi") == 0)
419 else if (strcmp(cmd
, "vo") == 0)
425 params
[cur
].ACI
= (params
[cur
].ACI
& (0x3 << 5)) | (cur
<< 5);
429 intval
= strtoul(val
, NULL
, 10);
430 if (strcmp(cmd
, "cwmin") == 0)
431 params
[cur
].ECW
= (params
[cur
].ECW
& ~(0xf)) | cw2ecw(intval
);
432 else if (strcmp(cmd
, "ecwmin") == 0)
433 params
[cur
].ECW
= (params
[cur
].ECW
& ~(0xf)) | (intval
& 0xf);
434 else if (strcmp(cmd
, "cwmax") == 0)
435 params
[cur
].ECW
= (params
[cur
].ECW
& ~(0xf << 4)) | (cw2ecw(intval
) << 4);
436 else if (strcmp(cmd
, "ecwmax") == 0)
437 params
[cur
].ECW
= (params
[cur
].ECW
& ~(0xf << 4)) | ((intval
& 0xf) << 4);
438 else if (strcmp(cmd
, "aifsn") == 0)
439 params
[cur
].ACI
= (params
[cur
].ACI
& ~(0xf)) | (intval
& 0xf);
440 else if (strcmp(cmd
, "txop") == 0)
441 params
[cur
].TXOP
= intval
>> 5;
442 else if (strcmp(cmd
, "force") == 0)
443 params
[cur
].ACI
= (params
[cur
].ACI
& ~(1 << 4)) | ((intval
) ? (1 << 4) : 0);
446 memcpy(buf
, ¶ms
[cur
], sizeof(edcf_acparam_t
));
447 wl_ioctl(interface
, WLC_SET_VAR
, wlbuf
, BUFSIZE
);
453 static int wlc_ifname(wlc_param param
, void *data
, void *value
)
455 char *val
= (char *) value
;
459 if (strlen(val
) < 16)
460 strcpy(interface
, val
);
464 strcpy(val
, interface
);
470 static int wlc_wdsmac(wlc_param param
, void *data
, void *value
)
472 unsigned char mac
[6];
475 ret
= wl_ioctl(interface
, WLC_WDS_GET_REMOTE_HWADDR
, &mac
, 6);
477 my_ether_ntoa(mac
, value
);
482 static int wlc_pmk(wlc_param param
, void *data
, void *value
)
485 char *str
= (char *) value
;
488 /* driver doesn't support GET */
490 if ((param
& PARAM_MODE
) == SET
) {
491 strncpy(pmk
.key
, value
, WSEC_MAX_PSK_LEN
);
492 pmk
.key_len
= strlen(value
);
494 if (pmk
.key_len
> WSEC_MAX_PSK_LEN
)
495 pmk
.key_len
= WSEC_MAX_PSK_LEN
;
497 pmk
.flags
= WSEC_PASSPHRASE
;
499 ret
= wl_ioctl(interface
, WLC_SET_WSEC_PMK
, &pmk
, sizeof(pmk
));
505 static const struct wlc_call wlc_calls
[] = {
508 .param
= STRING
|NOARG
,
509 .handler
= wlc_string
,
511 .desc
= "Version of this program"
518 .desc
= "wlc debug level"
524 .data
.ptr
= &fromstdin
,
525 .desc
= "Accept input from stdin"
530 .handler
= wlc_ifname
,
531 .desc
= "interface to send commands to"
536 .handler
= wlc_ioctl
,
538 .desc
= "Bring the interface up"
543 .handler
= wlc_ioctl
,
544 .data
.num
= WLC_DOWN
,
545 .desc
= "Bring the interface down"
550 .handler
= wlc_radio
,
551 .desc
= "Radio enabled flag"
556 .handler
= wlc_ioctl
,
557 .data
.num
= ((WLC_GET_AP
<< 16) | WLC_SET_AP
),
558 .desc
= "Access Point mode"
563 .handler
= wlc_iovar
,
565 .desc
= "Multi-ssid mode"
570 .handler
= wlc_iovar
,
572 .desc
= "AP+STA mode"
577 .handler
= wlc_ioctl
,
578 .data
.num
= ((WLC_GET_INFRA
<< 16) | WLC_SET_INFRA
),
579 .desc
= "Infrastructure mode"
584 .handler
= wlc_ioctl
,
585 .data
.num
= ((WLC_GET_WET
<< 16) | WLC_SET_WET
),
586 .desc
= "Wireless repeater mode",
589 .name
= "statimeout",
591 .handler
= wlc_iovar
,
592 .data
.str
= "sta_retry_time",
593 .desc
= "STA connection timeout"
598 .handler
= wlc_ioctl
,
599 .data
.num
= ((WLC_GET_COUNTRY
<< 16) | WLC_SET_COUNTRY
),
600 .desc
= "Country code"
605 .handler
= wlc_ioctl
,
606 .data
.num
= ((WLC_GET_CHANNEL
<< 16) | WLC_SET_CHANNEL
),
612 .handler
= wlc_bssiovar
,
613 .data
.str
= "vlan_mode",
614 .desc
= "Parse 802.1Q tags",
621 .desc
= "Current vif index"
626 .handler
= wlc_vif_enabled
,
627 .desc
= "vif enabled flag"
633 .desc
= "Interface ESSID"
638 .handler
= wlc_bssiovar
,
639 .data
.str
= "closednet",
640 .desc
= "Hidden ESSID flag"
645 .handler
= wlc_bssiovar
,
647 .desc
= "Security mode flags"
652 .handler
= wlc_wsec_key
,
653 .desc
= "Set/Remove WEP keys"
656 .name
= "wsec_restrict",
658 .handler
= wlc_bssiovar
,
659 .data
.str
= "wsec_restrict",
660 .desc
= "Drop unencrypted traffic"
663 .name
= "eap_restrict",
665 .handler
= wlc_bssiovar
,
666 .data
.str
= "eap_restrict",
667 .desc
= "Only allow 802.1X traffic until 802.1X authorized"
672 .handler
= wlc_bssiovar
,
673 .data
.str
= "wpa_auth",
674 .desc
= "WPA authentication modes"
677 .name
= "ap_isolate",
679 .handler
= wlc_bssiovar
,
680 .data
.str
= "ap_isolate",
681 .desc
= "Isolate connected clients"
684 .name
= "supplicant",
686 .handler
= wlc_iovar
,
687 .data
.str
= "sup_wpa",
688 .desc
= "Built-in WPA supplicant"
691 .name
= "passphrase",
694 .desc
= "Passphrase for built-in WPA supplicant",
699 .handler
= wlc_iovar
,
700 .data
.str
= "maxassoc",
701 .desc
= "Max. number of associated clients",
706 .handler
= wlc_iovar
,
708 .desc
= "WME enabled"
713 .handler
= wlc_wme_ac
,
714 .data
.str
= "wme_ac_ap",
715 .desc
= "Set WME AC options for AP mode",
718 .name
= "wme_ac_sta",
720 .handler
= wlc_wme_ac
,
721 .data
.str
= "wme_ac_sta",
722 .desc
= "Set WME AC options for STA mode",
727 .handler
= wlc_iovar
,
728 .data
.str
= "wme_noack",
729 .desc
= "WME ACK disable request",
734 .handler
= wlc_ioctl
,
735 .data
.num
= ((WLC_GET_REGULATORY
<< 16) | WLC_SET_REGULATORY
),
736 .desc
= "Enable/disable 802.11d regulatory management",
741 .handler
= wlc_ioctl
,
742 .data
.num
= ((WLC_GET_SPECT_MANAGMENT
<< 16) | WLC_SET_SPECT_MANAGMENT
),
743 .desc
= "Enable/disable 802.11h spectrum management",
746 .name
= "fragthresh",
748 .handler
= wlc_iovar
,
749 .data
.str
= "fragthresh",
750 .desc
= "Fragmentation threshold",
755 .handler
= wlc_iovar
,
756 .data
.str
= "rtsthresh",
757 .desc
= "RTS threshold"
762 .handler
= wlc_iovar
,
763 .data
.str
= "acktiming",
769 .handler
= wlc_ioctl
,
770 .data
.num
= ((WLC_GET_ANTDIV
<< 16) | WLC_SET_ANTDIV
),
771 .desc
= "Rx antenna selection"
776 .handler
= wlc_ioctl
,
777 .data
.num
= ((WLC_GET_TXANT
<< 16) | WLC_SET_TXANT
),
778 .desc
= "Tx antenna selection"
783 .handler
= wlc_ioctl
,
784 .data
.num
= ((WLC_GET_DTIMPRD
<< 16) | WLC_SET_DTIMPRD
),
785 .desc
= "DTIM period",
790 .handler
= wlc_ioctl
,
791 .data
.num
= ((WLC_GET_BCNPRD
<< 16) | WLC_SET_BCNPRD
),
792 .desc
= "Beacon interval"
795 .name
= "frameburst",
797 .handler
= wlc_ioctl
,
798 .data
.num
= ((WLC_GET_FAKEFRAG
<< 16) | WLC_SET_FAKEFRAG
),
799 .desc
= "Framebursting"
804 .handler
= wlc_ioctl
,
805 .data
.num
= ((WLC_GET_MONITOR
<< 16) | WLC_SET_MONITOR
),
806 .desc
= "Monitor mode"
811 .handler
= wlc_ioctl
,
812 .data
.num
= ((WLC_GET_PASSIVE
<< 16) | WLC_SET_PASSIVE
),
813 .desc
= "Passive mode"
818 .handler
= wlc_ioctl
,
819 .data
.num
= ((WLC_GET_MACMODE
<< 16) | WLC_SET_MACMODE
),
820 .desc
= "MAC filter mode (0:disabled, 1:deny, 2:allow)"
825 .data
.num
= ((WLC_GET_MACLIST
<< 16) | WLC_SET_MACLIST
),
826 .handler
= wlc_maclist
,
827 .desc
= "MAC filter list"
832 .handler
= wlc_ioctl
,
833 .data
.num
= ((WLC_GET_LAZYWDS
<< 16) | WLC_SET_LAZYWDS
),
834 .desc
= "Automatic WDS"
839 .data
.num
= ((WLC_GET_WDSLIST
<< 16) | WLC_SET_WDSLIST
),
840 .handler
= wlc_maclist
,
841 .desc
= "WDS connection list"
844 .name
= "wdstimeout",
846 .handler
= wlc_iovar
,
847 .data
.str
= "wdstimeout",
848 .desc
= "WDS link detection timeout"
852 .param
= STRING
|NOARG
,
853 .handler
= wlc_wdsmac
,
854 .desc
= "MAC of the remote WDS endpoint (only with wds0.* interfaces)"
857 .name
= "afterburner",
859 .handler
= wlc_afterburner
,
860 .desc
= "Broadcom Afterburner"
863 #define wlc_calls_size (sizeof(wlc_calls) / sizeof(struct wlc_call))
865 static void usage(char *cmd
)
868 fprintf(stderr
, "Usage: %s <command> [<argument> ...]\n"
870 "Available commands:\n", cmd
);
871 for (i
= 0; i
< wlc_calls_size
; i
++) {
872 fprintf(stderr
, "\t%-16s\t%s\n", wlc_calls
[i
].name
?: "", wlc_calls
[i
].desc
?: "");
874 fprintf(stderr
, "\n");
878 static int do_command(const struct wlc_call
*cmd
, char *arg
)
880 static char buf
[BUFSIZE
];
887 fprintf(stderr
, "do_command %-16s\t'%s'\n", cmd
->name
, arg
);
890 if ((arg
== NULL
) && ((cmd
->param
& PARAM_TYPE
) != NONE
)) {
892 ret
= cmd
->handler(cmd
->param
| GET
, (void *) &cmd
->data
, (void *) buf
);
894 switch(cmd
->param
& PARAM_TYPE
) {
896 intval
= *((int *) buf
);
900 else if (intval
> 255)
905 fprintf(stdout
, format
, intval
);
908 fprintf(stdout
, "%s\n", buf
);
913 switch(cmd
->param
& PARAM_TYPE
) {
915 intval
= strtoul(arg
, &end
, 10);
916 if (end
&& !(*end
)) {
917 memcpy(buf
, &intval
, sizeof(intval
));
919 fprintf(stderr
, "%s: Invalid argument\n", cmd
->name
);
924 strncpy(buf
, arg
, BUFSIZE
);
925 buf
[BUFSIZE
- 1] = 0;
928 ret
= cmd
->handler(cmd
->param
| SET
, (void *) &cmd
->data
, (void *) buf
);
931 if ((debug
> 0) && (ret
!= 0))
932 fprintf(stderr
, "Command '%s %s' failed: %d\n", (set
== 1 ? "set" : "get"), cmd
->name
, ret
);
937 static struct wlc_call
*find_cmd(char *name
)
939 int found
= 0, i
= 0;
941 while (!found
&& (i
< wlc_calls_size
)) {
942 if (strcmp(name
, wlc_calls
[i
].name
) == 0)
948 return (struct wlc_call
*) (found
? &wlc_calls
[i
] : NULL
);
951 int main(int argc
, char **argv
)
953 static char buf
[BUFSIZE
];
956 struct wlc_call
*call
;
962 for(interface
[2] = '0'; (interface
[2] < '3') && (wl_probe(interface
) != 0); interface
[2]++);
963 if (interface
[2] == '3') {
964 fprintf(stderr
, "No Broadcom wl interface found!\n");
970 while ((argc
> 0) && (argv
[0] != NULL
)) {
971 if ((call
= find_cmd(argv
[0])) == NULL
) {
972 fprintf(stderr
, "Invalid command: %s\n\n", argv
[0]);
975 if ((argc
> 1) && (!(call
->param
& NOARG
))) {
976 ret
= do_command(call
, argv
[1]);
980 ret
= do_command(call
, NULL
);
986 while (fromstdin
&& !feof(stdin
)) {
988 fgets(buf
, BUFSIZE
- 1, stdin
);
993 if ((s
= strchr(buf
, '\r')) != NULL
)
995 if ((s
= strchr(buf
, '\n')) != NULL
)
1005 if ((s2
= strchr(buf
, ' ')) != NULL
)
1008 while (s2
&& isspace(*s2
))
1011 if ((call
= find_cmd(buf
)) == NULL
) {
1012 fprintf(stderr
, "Invalid command: %s\n", buf
);
1015 ret
= do_command(call
, ((call
->param
& NOARG
) ? NULL
: s2
));