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;
72 PARAM_OPTIONS = 0x0f0,
84 int (*handler)(wlc_param param, void *data, void *value);
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)
97 return sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
98 ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
101 static int wlc_ioctl(wlc_param param, void *data, void *value)
103 unsigned int *var = ((unsigned int *) data);
104 unsigned int ioc = *var;
107 return wl_ioctl(interface, ioc, NULL, 0);
109 switch(param & PARAM_TYPE) {
111 return wl_ioctl(interface, ((param & SET) ? (ioc) : (ioc >> 16)) & 0xffff, value, 6);
113 return wl_ioctl(interface, ((param & SET) ? (ioc) : (ioc >> 16)) & 0xffff, value, sizeof(int));
115 return wl_ioctl(interface, ((param & SET) ? (ioc) : (ioc >> 16)) & 0xffff, value, BUFSIZE);
120 static int wlc_iovar(wlc_param param, void *data, void *value)
122 int *val = (int *) value;
123 char *iov = *((char **) data);
127 switch(param & PARAM_TYPE) {
129 ret = wl_iovar_setint(interface, iov, *val);
132 ret = wl_iovar_set(interface, iov, value, 6);
137 switch(param & PARAM_TYPE) {
139 ret = wl_iovar_get(interface, iov, val, sizeof(int));
142 ret = wl_iovar_get(interface, iov, value, 6);
150 static int wlc_bssiovar(wlc_param param, void *data, void *value)
152 int *val = (int *) value;
153 char *iov = *((char **) data);
157 switch(param & PARAM_TYPE) {
159 ret = wl_bssiovar_setint(interface, iov, vif, *val);
163 switch(param & PARAM_TYPE) {
165 ret = wl_bssiovar_get(interface, iov, vif, val, sizeof(int));
172 static int wlc_vif_enabled(wlc_param param, void *data, void *value)
174 int *val = (int *) value;
178 sprintf((char *) buf, "bss");
181 buf[2] = (*val ? 1 : 0);
182 ret = wl_ioctl(interface, WLC_SET_VAR, buf, sizeof(buf));
183 } else if (param & GET) {
184 ret = wl_ioctl(interface, WLC_GET_VAR, buf, sizeof(buf));
191 static int wlc_ssid(wlc_param param, void *data, void *value)
193 int ret = -1, ret2 = -1;
194 char *dest = (char *) value;
197 if ((param & PARAM_MODE) == GET) {
198 ret = wl_bssiovar_get(interface, "ssid", vif, &ssid, sizeof(ssid));
201 /* if we can't get the ssid through the bssiovar, try WLC_GET_SSID */
202 ret = wl_ioctl(interface, WLC_GET_SSID, &ssid, sizeof(ssid));
205 memcpy(dest, ssid.SSID, ssid.SSID_len);
206 dest[ssid.SSID_len] = 0;
208 } else if ((param & PARAM_MODE) == SET) {
209 strncpy(ssid.SSID, value, 32);
210 ssid.SSID_len = strlen(value);
212 if (ssid.SSID_len > 32)
216 /* for the main interface, also try the WLC_SET_SSID call */
217 ret2 = wl_ioctl(interface, WLC_SET_SSID, &ssid, sizeof(ssid));
220 ret = wl_bssiovar_set(interface, "ssid", vif, &ssid, sizeof(ssid));
221 ret = (!ret2 ? 0 : ret);
227 static int wlc_int(wlc_param param, void *data, void *value)
229 int *var = *((int **) data);
230 int *val = (int *) value;
232 if ((param & PARAM_MODE) == SET) {
234 } else if ((param & PARAM_MODE) == GET) {
241 static int wlc_flag(wlc_param param, void *data, void *value)
243 int *var = *((int **) data);
250 static int wlc_string(wlc_param param, void *data, void *value)
252 char *var = *((char **) data);
254 if ((param & PARAM_MODE) == GET) {
261 static int wlc_afterburner(wlc_param param, void *data, void *value)
263 int *val = (int *) value;
266 if ((param & PARAM_MODE) == GET) {
267 ret = wl_iovar_get(interface, "afterburner", val, sizeof(int));
269 wl_iovar_setint(interface, "wlfeatureflag", (*val ? 3 : 0));
270 ret = wl_iovar_setint(interface, "afterburner", (*val ? 1 : 0));
271 wl_iovar_setint(interface, "afterburner_override", *val);
277 static int wlc_maclist(wlc_param param, void *data, void *value)
279 unsigned int *var = ((unsigned int *) data);
280 unsigned int ioc = *var;
281 int limit = (sizeof(wlbuf) - 4) / sizeof(struct ether_addr);
282 struct maclist *list = (struct maclist *) wlbuf;
283 char *str = (char *) value;
285 struct ether_addr *addr;
289 if ((param & PARAM_MODE) == GET) {
291 ret = wl_ioctl(interface, (ioc >> 16) & 0xffff, wlbuf, sizeof(wlbuf));
294 while (list->count) {
295 str += sprintf(str, "%s", ((((char *) value) == str) ? "" : " "));
296 str += my_ether_ntoa((unsigned char *) &list->ea[list->count-- - 1], str);
301 while (*str && isspace(*str))
308 if (wl_ioctl(interface, (ioc >> 16) & 0xffff, wlbuf, sizeof(wlbuf)) == 0)
311 while (*str && isspace(*str))
316 memset(wlbuf, 0, sizeof(wlbuf));
318 foreach(astr, str, p) {
319 if (list->count >= limit)
322 if ((addr = ether_aton(astr)) != NULL)
323 memcpy(&list->ea[list->count++], addr, sizeof(struct ether_addr));
326 return wl_ioctl(interface, ioc & 0xffff, wlbuf, sizeof(wlbuf));
330 static int wlc_radio(wlc_param param, void *data, void *value)
332 int *val = (int *) value;
335 if ((param & PARAM_MODE) == GET) {
336 ret = wl_ioctl(interface, WLC_GET_RADIO, val, sizeof(int));
337 *val = ((*val & 1) ? 0 : 1);
339 *val = (1 << 16) | (*val ? 0 : 1);
340 ret = wl_ioctl(interface, WLC_SET_RADIO, val, sizeof(int));
346 static int wlc_wsec_key(wlc_param param, void *null, void *value)
348 wl_wsec_key_t wsec_key;
349 unsigned char *index = value;
352 unsigned char hex[3];
354 if ((param & PARAM_MODE) != SET)
357 memset(&wsec_key, 0, sizeof(wsec_key));
358 if (index[0] == '=') {
359 wsec_key.flags = WL_PRIMARY_KEY;
363 if ((index[0] < '1') || (index[0] > '4') || (index[1] != ','))
367 if (strncmp(key, "d:", 2) == 0) { /* delete key */
368 } else if (strncmp(key, "s:", 2) == 0) { /* ascii key */
370 wsec_key.len = strlen(key);
372 if ((wsec_key.len != 5) && (wsec_key.len != 13))
375 strcpy(wsec_key.data, key);
376 } else { /* hex key */
377 wsec_key.len = strlen(key);
378 if ((wsec_key.len != 10) && (wsec_key.len != 26))
382 data = wsec_key.data;
387 *(data++) = (unsigned char) strtoul(hex, NULL, 16);
391 return wl_bssiovar_set(interface, "wsec_key", vif, &wsec_key, sizeof(wsec_key));
394 static inline int cw2ecw(int cw)
397 for (cw++, i = 0; cw; i++) cw >>=1;
401 static int wlc_wme_ac(wlc_param param, void *data, void *value)
403 char *type = *((char **) data);
404 char *settings = (char *) value;
405 char cmd[100], *p, *val;
406 edcf_acparam_t params[AC_COUNT];
412 if ((param & PARAM_MODE) != SET)
415 memset(params, 0, sizeof(params));
416 ret = wl_iovar_get(interface, type, params, sizeof(params));
417 memset(buf, 0, BUFSIZE);
419 buf += strlen(buf) + 1;
421 foreach(cmd, settings, p) {
422 val = strchr(cmd, '=');
424 if (strcmp(cmd, "be") == 0)
426 else if (strcmp(cmd, "bk") == 0)
428 else if (strcmp(cmd, "vi") == 0)
430 else if (strcmp(cmd, "vo") == 0)
436 params[cur].ACI = (params[cur].ACI & (0x3 << 5)) | (cur << 5);
440 intval = strtoul(val, NULL, 10);
441 if (strcmp(cmd, "cwmin") == 0)
442 params[cur].ECW = (params[cur].ECW & ~(0xf)) | cw2ecw(intval);
443 else if (strcmp(cmd, "ecwmin") == 0)
444 params[cur].ECW = (params[cur].ECW & ~(0xf)) | (intval & 0xf);
445 else if (strcmp(cmd, "cwmax") == 0)
446 params[cur].ECW = (params[cur].ECW & ~(0xf << 4)) | (cw2ecw(intval) << 4);
447 else if (strcmp(cmd, "ecwmax") == 0)
448 params[cur].ECW = (params[cur].ECW & ~(0xf << 4)) | ((intval & 0xf) << 4);
449 else if (strcmp(cmd, "aifsn") == 0)
450 params[cur].ACI = (params[cur].ACI & ~(0xf)) | (intval & 0xf);
451 else if (strcmp(cmd, "txop") == 0)
452 params[cur].TXOP = intval >> 5;
453 else if (strcmp(cmd, "force") == 0)
454 params[cur].ACI = (params[cur].ACI & ~(1 << 4)) | ((intval) ? (1 << 4) : 0);
457 memcpy(buf, ¶ms[cur], sizeof(edcf_acparam_t));
458 wl_ioctl(interface, WLC_SET_VAR, wlbuf, BUFSIZE);
464 static int wlc_ifname(wlc_param param, void *data, void *value)
466 char *val = (char *) value;
470 if (strlen(val) < 16)
471 strcpy(interface, val);
475 strcpy(val, interface);
481 static int wlc_wdsmac(wlc_param param, void *data, void *value)
483 unsigned char mac[6];
486 ret = wl_ioctl(interface, WLC_WDS_GET_REMOTE_HWADDR, &mac, 6);
488 my_ether_ntoa(mac, value);
493 static int wlc_pmk(wlc_param param, void *data, void *value)
496 char *str = (char *) value;
499 /* driver doesn't support GET */
501 if ((param & PARAM_MODE) == SET) {
502 strncpy(pmk.key, value, WSEC_MAX_PSK_LEN);
503 pmk.key_len = strlen(value);
505 if (pmk.key_len > WSEC_MAX_PSK_LEN)
506 pmk.key_len = WSEC_MAX_PSK_LEN;
508 pmk.flags = WSEC_PASSPHRASE;
510 ret = wl_ioctl(interface, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
516 static const struct wlc_call wlc_calls[] = {
519 .param = STRING|NOARG,
520 .handler = wlc_string,
522 .desc = "Version of this program"
529 .desc = "wlc debug level"
535 .data.ptr = &fromstdin,
536 .desc = "Accept input from stdin"
541 .handler = wlc_ifname,
542 .desc = "interface to send commands to"
547 .handler = wlc_ioctl,
549 .desc = "Bring the interface up"
554 .handler = wlc_ioctl,
555 .data.num = WLC_DOWN,
556 .desc = "Bring the interface down"
561 .handler = wlc_radio,
562 .desc = "Radio enabled flag"
567 .handler = wlc_ioctl,
568 .data.num = ((WLC_GET_AP << 16) | WLC_SET_AP),
569 .desc = "Access Point mode"
574 .handler = wlc_iovar,
576 .desc = "Multi-ssid mode"
581 .handler = wlc_iovar,
583 .desc = "AP+STA mode"
588 .handler = wlc_ioctl,
589 .data.num = ((WLC_GET_INFRA << 16) | WLC_SET_INFRA),
590 .desc = "Infrastructure mode"
595 .handler = wlc_ioctl,
596 .data.num = ((WLC_GET_WET << 16) | WLC_SET_WET),
597 .desc = "Wireless repeater mode",
600 .name = "statimeout",
602 .handler = wlc_iovar,
603 .data.str = "sta_retry_time",
604 .desc = "STA connection timeout"
609 .handler = wlc_ioctl,
610 .data.num = ((WLC_GET_COUNTRY << 16) | WLC_SET_COUNTRY),
611 .desc = "Country code"
616 .handler = wlc_ioctl,
617 .data.num = ((WLC_GET_CHANNEL << 16) | WLC_SET_CHANNEL),
623 .handler = wlc_bssiovar,
624 .data.str = "vlan_mode",
625 .desc = "Parse 802.1Q tags",
632 .desc = "Current vif index"
637 .handler = wlc_vif_enabled,
638 .desc = "vif enabled flag"
644 .desc = "Interface ESSID"
649 .handler = wlc_bssiovar,
650 .data.str = "closednet",
651 .desc = "Hidden ESSID flag"
656 .handler = wlc_bssiovar,
658 .desc = "Security mode flags"
663 .handler = wlc_wsec_key,
664 .desc = "Set/Remove WEP keys"
669 .handler = wlc_ioctl,
670 .data.num = ((WLC_GET_AUTH << 16) | WLC_SET_AUTH),
671 .desc = "WEP authentication type. 0 = OpenSystem, 1 = SharedKey"
674 .name = "wsec_restrict",
676 .handler = wlc_bssiovar,
677 .data.str = "wsec_restrict",
678 .desc = "Drop unencrypted traffic"
681 .name = "eap_restrict",
683 .handler = wlc_bssiovar,
684 .data.str = "eap_restrict",
685 .desc = "Only allow 802.1X traffic until 802.1X authorized"
690 .handler = wlc_bssiovar,
691 .data.str = "wpa_auth",
692 .desc = "WPA authentication modes"
695 .name = "ap_isolate",
697 .handler = wlc_bssiovar,
698 .data.str = "ap_isolate",
699 .desc = "Isolate connected clients"
702 .name = "supplicant",
704 .handler = wlc_iovar,
705 .data.str = "sup_wpa",
706 .desc = "Built-in WPA supplicant"
709 .name = "passphrase",
712 .desc = "Passphrase for built-in WPA supplicant",
717 .handler = wlc_iovar,
718 .data.str = "maxassoc",
719 .desc = "Max. number of associated clients",
724 .handler = wlc_iovar,
726 .desc = "WME enabled"
731 .handler = wlc_wme_ac,
732 .data.str = "wme_ac_ap",
733 .desc = "Set WME AC options for AP mode",
736 .name = "wme_ac_sta",
738 .handler = wlc_wme_ac,
739 .data.str = "wme_ac_sta",
740 .desc = "Set WME AC options for STA mode",
745 .handler = wlc_iovar,
746 .data.str = "wme_noack",
747 .desc = "WME ACK disable request",
752 .handler = wlc_ioctl,
753 .data.num = ((WLC_GET_REGULATORY << 16) | WLC_SET_REGULATORY),
754 .desc = "Enable/disable 802.11d regulatory management",
759 .handler = wlc_ioctl,
760 .data.num = ((WLC_GET_SPECT_MANAGMENT << 16) | WLC_SET_SPECT_MANAGMENT),
761 .desc = "Enable/disable 802.11h spectrum management",
764 .name = "fragthresh",
766 .handler = wlc_iovar,
767 .data.str = "fragthresh",
768 .desc = "Fragmentation threshold",
773 .handler = wlc_iovar,
774 .data.str = "rtsthresh",
775 .desc = "RTS threshold"
780 .handler = wlc_iovar,
781 .data.str = "acktiming",
787 .handler = wlc_ioctl,
788 .data.num = ((WLC_GET_ANTDIV << 16) | WLC_SET_ANTDIV),
789 .desc = "Rx antenna selection"
794 .handler = wlc_ioctl,
795 .data.num = ((WLC_GET_TXANT << 16) | WLC_SET_TXANT),
796 .desc = "Tx antenna selection"
801 .handler = wlc_ioctl,
802 .data.num = ((WLC_GET_DTIMPRD << 16) | WLC_SET_DTIMPRD),
803 .desc = "DTIM period",
808 .handler = wlc_ioctl,
809 .data.num = ((WLC_GET_BCNPRD << 16) | WLC_SET_BCNPRD),
810 .desc = "Beacon interval"
813 .name = "frameburst",
815 .handler = wlc_ioctl,
816 .data.num = ((WLC_GET_FAKEFRAG << 16) | WLC_SET_FAKEFRAG),
817 .desc = "Framebursting"
822 .handler = wlc_ioctl,
823 .data.num = ((WLC_GET_MONITOR << 16) | WLC_SET_MONITOR),
824 .desc = "Monitor mode"
827 .name = "passive_scan",
829 .handler = wlc_ioctl,
830 .data.num = ((WLC_GET_PASSIVE_SCAN << 16) | WLC_SET_PASSIVE_SCAN),
831 .desc = "Passive scan mode"
836 .handler = wlc_ioctl,
837 .data.num = ((WLC_GET_MACMODE << 16) | WLC_SET_MACMODE),
838 .desc = "MAC filter mode (0:disabled, 1:deny, 2:allow)"
843 .data.num = ((WLC_GET_MACLIST << 16) | WLC_SET_MACLIST),
844 .handler = wlc_maclist,
845 .desc = "MAC filter list"
850 .handler = wlc_ioctl,
851 .data.num = ((WLC_GET_LAZYWDS << 16) | WLC_SET_LAZYWDS),
852 .desc = "Automatic WDS"
857 .data.num = ((WLC_GET_WDSLIST << 16) | WLC_SET_WDSLIST),
858 .handler = wlc_maclist,
859 .desc = "WDS connection list"
862 .name = "wdstimeout",
864 .handler = wlc_iovar,
865 .data.str = "wdstimeout",
866 .desc = "WDS link detection timeout"
870 .param = STRING|NOARG,
871 .handler = wlc_wdsmac,
872 .desc = "MAC of the remote WDS endpoint (only with wds0.* interfaces)"
875 .name = "afterburner",
877 .handler = wlc_afterburner,
878 .desc = "Broadcom Afterburner"
881 .name = "ibss_merge",
883 .handler = wlc_iovar,
884 .data.str = "ibss_coalesce_allowed",
885 .desc = "Allow IBSS merges"
890 .handler = wlc_ioctl,
891 .data.num = ((WLC_GET_BSSID << 16) | WLC_SET_BSSID),
895 .name = "default_bssid",
897 .handler = wlc_iovar,
898 .data.str = "perm_etheraddr",
899 .desc = "Default BSSID (read-only)"
904 .data.num = (WLC_GET_ASSOCLIST << 16),
905 .handler = wlc_maclist,
906 .desc = "MACs of associated stations"
911 .data.num = ((WLC_GET_GMODE << 16) | WLC_SET_GMODE),
912 .handler = wlc_ioctl,
916 #define wlc_calls_size (sizeof(wlc_calls) / sizeof(struct wlc_call))
918 static void usage(char *cmd)
921 fprintf(stderr, "Usage: %s <command> [<argument> ...]\n"
923 "Available commands:\n", cmd);
924 for (i = 0; i < wlc_calls_size; i++) {
925 fprintf(stderr, "\t%-16s\t%s\n", wlc_calls[i].name ?: "", wlc_calls[i].desc ?: "");
927 fprintf(stderr, "\n");
931 static int do_command(const struct wlc_call *cmd, char *arg)
933 static char buf[BUFSIZE];
938 void *ptr = (void *) buf;
941 fprintf(stderr, "do_command %-16s\t'%s'\n", cmd->name, arg);
944 if ((arg == NULL) && ((cmd->param & PARAM_TYPE) != NONE)) {
946 ret = cmd->handler(cmd->param | GET, (void *) &cmd->data, (void *) buf);
948 switch(cmd->param & PARAM_TYPE) {
950 intval = *((int *) buf);
954 else if (intval > 255)
959 fprintf(stdout, format, intval);
962 fprintf(stdout, "%s\n", buf);
965 my_ether_ntoa(buf, buf + 6);
966 fprintf(stdout, "%s\n", buf + 6);
972 switch(cmd->param & PARAM_TYPE) {
974 intval = strtoul(arg, &end, 10);
975 if (end && !(*end)) {
976 memcpy(buf, &intval, sizeof(intval));
978 fprintf(stderr, "%s: Invalid argument\n", cmd->name);
983 strncpy(buf, arg, BUFSIZE);
984 buf[BUFSIZE - 1] = 0;
987 ptr = ether_aton(arg);
989 fprintf(stderr, "%s: Invalid mac address '%s'\n", cmd->name, arg);
995 ret = cmd->handler(cmd->param | SET, (void *) &cmd->data, ptr);
998 if ((debug > 0) && (ret != 0))
999 fprintf(stderr, "Command '%s %s' failed: %d\n", (set == 1 ? "set" : "get"), cmd->name, ret);
1004 static struct wlc_call *find_cmd(char *name)
1006 int found = 0, i = 0;
1008 while (!found && (i < wlc_calls_size)) {
1009 if (strcmp(name, wlc_calls[i].name) == 0)
1015 return (struct wlc_call *) (found ? &wlc_calls[i] : NULL);
1018 int main(int argc, char **argv)
1020 static char buf[BUFSIZE];
1022 char *cmd = argv[0];
1023 struct wlc_call *call;
1029 for(interface[2] = '0'; (interface[2] < '3') && (wl_probe(interface) != 0); interface[2]++);
1030 if (interface[2] == '3') {
1031 fprintf(stderr, "No Broadcom wl interface found!\n");
1037 while ((argc > 0) && (argv[0] != NULL)) {
1038 if ((call = find_cmd(argv[0])) == NULL) {
1039 fprintf(stderr, "Invalid command: %s\n\n", argv[0]);
1042 if ((argc > 1) && (!(call->param & NOARG))) {
1043 ret = do_command(call, argv[1]);
1047 ret = do_command(call, NULL);
1053 while (fromstdin && !feof(stdin)) {
1055 fgets(buf, BUFSIZE - 1, stdin);
1060 if ((s = strchr(buf, '\r')) != NULL)
1062 if ((s = strchr(buf, '\n')) != NULL)
1072 if ((s2 = strchr(buf, ' ')) != NULL)
1075 while (s2 && isspace(*s2))
1078 if ((call = find_cmd(buf)) == NULL) {
1079 fprintf(stderr, "Invalid command: %s\n", buf);
1082 ret = do_command(call, ((call->param & NOARG) ? NULL : s2));