2 * Wireless Network Adapter configuration utility
4 * Copyright (C) 2005 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.
25 #define ADD_VIF_RETRIES 5
28 /*------------------------------------------------------------------*/
30 * Macro to handle errors when setting WE
31 * Print a nice error message and exit...
32 * We define them as macro so that "return" do the right thing.
33 * The "do {...} while(0)" is a standard trick
35 #define ERR_SET_EXT(rname, request) \
36 fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n", \
39 #define ABORT_ARG_NUM(rname, request) \
41 ERR_SET_EXT(rname, request); \
42 fprintf(stderr, " too few arguments.\n"); \
45 #define ABORT_ARG_TYPE(rname, request, arg) \
47 ERR_SET_EXT(rname, request); \
48 fprintf(stderr, " invalid argument \"%s\".\n", arg); \
51 #define ABORT_ARG_SIZE(rname, request, max) \
53 ERR_SET_EXT(rname, request); \
54 fprintf(stderr, " argument too big (max %d)\n", max); \
57 /*------------------------------------------------------------------*/
59 * Wrapper to push some Wireless Parameter in the driver
60 * Use standard wrapper and add pretty error message if fail...
62 #define IW_SET_EXT_ERR(skfd, ifname, request, wrq, rname) \
64 if(iw_set_ext(skfd, ifname, request, wrq) < 0) { \
65 ERR_SET_EXT(rname, request); \
66 fprintf(stderr, " SET failed on device %-1.16s ; %s.\n", \
67 ifname, strerror(errno)); \
70 /*------------------------------------------------------------------*/
72 * Wrapper to extract some Wireless Parameter out of the driver
73 * Use standard wrapper and add pretty error message if fail...
75 #define IW_GET_EXT_ERR(skfd, ifname, request, wrq, rname) \
77 if(iw_get_ext(skfd, ifname, request, wrq) < 0) { \
78 ERR_SET_EXT(rname, request); \
79 fprintf(stderr, " GET failed on device %-1.16s ; %s.\n", \
80 ifname, strerror(errno)); \
83 static void set_wext_ssid(int skfd
, char *ifname
);
86 static char buffer
[128];
87 static int wpa_enc
= 0;
89 static char *wl_var(char *name
)
91 sprintf(buffer
, "%s_%s", prefix
, name
);
95 static char *vif_var(int vif
, char *name
)
100 sprintf(buffer
, "%s.%d_%s", prefix
, vif
, name
);
104 static int nvram_enabled(char *name
)
106 return (nvram_match(name
, "1") || nvram_match(name
, "on") || nvram_match(name
, "enabled") || nvram_match(name
, "true") || nvram_match(name
, "yes") ? 1 : 0);
109 static int nvram_disabled(char *name
)
111 return (nvram_match(name
, "0") || nvram_match(name
, "off") || nvram_match(name
, "disabled") || nvram_match(name
, "false") || nvram_match(name
, "no") ? 1 : 0);
115 /* Quarter dBm units to mW
116 * Table starts at QDBM_OFFSET, so the first entry is mW for qdBm=153
117 * Table is offset so the last entry is largest mW value that fits in
121 #define QDBM_OFFSET 153
122 #define QDBM_TABLE_LEN 40
124 /* Smallest mW value that will round up to the first table entry, QDBM_OFFSET.
125 * Value is ( mW(QDBM_OFFSET - 1) + mW(QDBM_OFFSET) ) / 2
127 #define QDBM_TABLE_LOW_BOUND 6493
129 /* Largest mW value that will round down to the last table entry,
130 * QDBM_OFFSET + QDBM_TABLE_LEN-1.
131 * Value is ( mW(QDBM_OFFSET + QDBM_TABLE_LEN - 1) + mW(QDBM_OFFSET + QDBM_TABLE_LEN) ) / 2.
133 #define QDBM_TABLE_HIGH_BOUND 64938
135 static const uint16 nqdBm_to_mW_map
[QDBM_TABLE_LEN
] = {
136 /* qdBm: +0 +1 +2 +3 +4 +5 +6 +7 */
137 /* 153: */ 6683, 7079, 7499, 7943, 8414, 8913, 9441, 10000,
138 /* 161: */ 10593, 11220, 11885, 12589, 13335, 14125, 14962, 15849,
139 /* 169: */ 16788, 17783, 18836, 19953, 21135, 22387, 23714, 25119,
140 /* 177: */ 26607, 28184, 29854, 31623, 33497, 35481, 37584, 39811,
141 /* 185: */ 42170, 44668, 47315, 50119, 53088, 56234, 59566, 63096
144 unsigned char mw_to_qdbm(uint16 mw
)
151 /* handle boundary case */
155 offset
= QDBM_OFFSET
;
157 /* move mw into the range of the table */
158 while (mw_uint
< QDBM_TABLE_LOW_BOUND
) {
163 for (qdbm
= 0; qdbm
< QDBM_TABLE_LEN
-1; qdbm
++) {
164 boundary
= nqdBm_to_mW_map
[qdbm
] + (nqdBm_to_mW_map
[qdbm
+1] - nqdBm_to_mW_map
[qdbm
])/2;
165 if (mw_uint
< boundary
) break;
168 qdbm
+= (unsigned char)offset
;
173 static int bcom_ioctl(int skfd
, char *ifname
, int cmd
, void *buf
, int len
)
183 ifr
.ifr_data
= (caddr_t
) &ioc
;
184 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
186 ret
= ioctl(skfd
, SIOCDEVPRIVATE
, &ifr
);
190 fprintf(stderr
, "IOCTL %d failed: %d\n", cmd
, ret
);
196 static int bcom_set_var(int skfd
, char *ifname
, char *var
, void *val
, int len
)
201 if (strlen(var
) + 1 > sizeof(buf
) || len
> sizeof(buf
))
204 bzero(buf
, sizeof(buf
));
206 memcpy(&buf
[strlen(var
) + 1], val
, len
);
208 ret
= bcom_ioctl(skfd
, ifname
, WLC_SET_VAR
, buf
, sizeof(buf
));
212 fprintf(stderr
, "SET_VAR %s failed: %d\n", var
, ret
);
218 static int bcom_get_var(int skfd
, char *ifname
, char *var
, void *buf
, int len
)
222 if (strlen(var
) + 1 > sizeof(buf
) || len
> sizeof(buf
))
225 bzero(buf
, sizeof(buf
));
228 ret
= bcom_ioctl(skfd
, ifname
, WLC_GET_VAR
, buf
, sizeof(buf
));
232 fprintf(stderr
, "GET_VAR %s failed: %d\n", var
, ret
);
238 static int bcom_set_bss_var(int skfd
, char *ifname
, int bss
, char *var
, void *val
, int len
)
243 bzero(buf
, sizeof(buf
));
244 if (strlen(var
) + len
+ 8 > sizeof(buf
) || len
> sizeof(buf
))
247 // "bsscfg:<name>\x00" <bss> <data>
248 i
= sprintf(buf
, "bsscfg:%s", var
);
251 memcpy(buf
+ i
, &bss
, sizeof(bss
));
254 memcpy(buf
+ i
, val
, len
);
257 ret
= bcom_ioctl(skfd
, ifname
, WLC_SET_VAR
, buf
, i
);
261 fprintf(stderr
, "SET_BSS_VAR %s failed: %d\n", var
, ret
);
267 static int bcom_set_int(int skfd
, char *ifname
, char *var
, int val
)
269 return bcom_set_var(skfd
, ifname
, var
, &val
, sizeof(val
));
272 static int bcom_set_bss_int(int skfd
, char *ifname
, int bss
, char *var
, int val
)
274 return bcom_set_bss_var(skfd
, ifname
, bss
, var
, &val
, sizeof(val
));
277 static int is_new_bcom(int skfd
, char *ifname
)
282 bcom_ioctl(skfd
, ifname
, WLC_DUMP
, buf
, 8192);
284 if (strstr(buf
, "3.130"))
290 static int bcom_get_wsec(int vif
)
294 if (nvram_match(vif_var(vif
, "crypto"), "tkip"))
296 else if (nvram_match(vif_var(vif
, "crypto"), "aes"))
298 else if (nvram_match(vif_var(vif
, "crypto"), "tkip+aes") || nvram_match(vif_var(vif
, "crypto"), "aes+tkip"))
299 val
= TKIP_ENABLED
| AES_ENABLED
;
306 static int bcom_get_wauth(int vif
)
308 char *v
, *next
, var
[80];
311 if (!(v
= nvram_get(vif_var(vif
, "akm"))))
312 v
= nvram_safe_get(vif_var(vif
, "auth_mode"));
314 foreach(var
, v
, next
) {
315 if (strcmp(var
, "psk") == 0)
317 else if (strcmp(var
, "psk2") == 0)
318 res
|= WPA2_AUTH_PSK
;
319 else if (strcmp(var
, "wpa") == 0)
320 res
|= WPA_AUTH_UNSPECIFIED
;
321 else if (strcmp(var
, "wpa2") == 0)
322 res
|= WPA2_AUTH_UNSPECIFIED
;
328 static void stop_bcom(int skfd
, char *ifname
)
333 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
338 bcom_ioctl(skfd
, ifname
, WLC_SET_SSID
, &ssid
, sizeof(ssid
));
339 bcom_ioctl(skfd
, ifname
, WLC_DOWN
, NULL
, 0);
343 static void start_bcom(int skfd
, char *ifname
)
347 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
350 bcom_ioctl(skfd
, ifname
, WLC_UP
, &val
, sizeof(val
));
353 static int setup_bcom_wds(int skfd
, char *ifname
)
360 if (v
= nvram_get(wl_var("wds"))) {
361 struct maclist
*wdslist
= (struct maclist
*) buf
;
362 struct ether_addr
*addr
= wdslist
->ea
;
365 memset(buf
, 0, 8192);
366 foreach(wbuf
, v
, next
) {
367 if (ether_atoe(wbuf
, addr
->ether_addr_octet
)) {
373 bcom_ioctl(skfd
, ifname
, WLC_SET_WDSLIST
, buf
, sizeof(buf
));
378 static void set_wext_mode(skfd
, ifname
)
381 int ap
= 0, infra
= 0, wet
= 0;
383 /* Set operation mode */
384 ap
= !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
385 infra
= !nvram_disabled(wl_var("infra"));
386 wet
= !ap
&& nvram_match(wl_var("mode"), "wet");
388 wrq
.u
.mode
= (!infra
? IW_MODE_ADHOC
: (ap
? IW_MODE_MASTER
: (wet
? IW_MODE_REPEAT
: IW_MODE_INFRA
)));
389 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWMODE
, &wrq
, "Set Mode");
393 void start_watchdog(int skfd
, char *ifname
)
397 unsigned char buf
[8192], buf2
[8192], wbuf
[80], *p
, *tmp
;
398 int wds
= 0, i
, j
, restart_wds
;
403 system("kill $(cat /var/run/wifi.pid) 2>&- >&-");
404 f
= fopen("/var/run/wifi.pid", "w");
405 fprintf(f
, "%d\n", getpid());
408 v
= nvram_safe_get(wl_var("wds"));
409 memset(buf2
, 0, 8192);
411 foreach(wbuf
, v
, next
) {
412 if (ether_atoe(wbuf
, p
)) {
417 v
= nvram_safe_get(wl_var("ssid"));
418 ssid
.SSID_len
= strlen(v
);
419 strncpy(ssid
.SSID
, v
, 32);
425 bcom_ioctl(skfd
, ifname
, WLC_GET_AP
, &i
, sizeof(i
));
428 if (bcom_ioctl(skfd
, ifname
, WLC_GET_BSSID
, buf
, 6) < 0)
430 memcpy(buf
+ 6, "\x00\x00\x00\x00\x00\x00", 6);
431 if (memcmp(buf
, buf
+ 6, 6) == 0)
434 memset(buf
, 0, 8192);
435 strcpy(buf
, "sta_info");
436 bcom_ioctl(skfd
, ifname
, WLC_GET_BSSID
, buf
+ strlen(buf
) + 1, 6);
437 if (bcom_ioctl(skfd
, ifname
, WLC_GET_VAR
, buf
, 8192) < 0) {
440 sta_info_t
*sta
= (sta_info_t
*) (buf
+ 4);
441 if ((sta
->flags
& 0x18) != 0x18)
448 bcom_ioctl(skfd
, ifname
, WLC_SET_SSID
, &ssid
, sizeof(ssid
));
455 for (i
= 0; i
< wds
; i
++) {
456 memset(buf
, 0, 8192);
457 strcpy(buf
, "sta_info");
458 memcpy(buf
+ strlen(buf
) + 1, p
, 6);
459 if (bcom_ioctl(skfd
, ifname
, WLC_GET_VAR
, buf
, 8192) < 0) {
461 sta_info_t
*sta
= (sta_info_t
*) (buf
+ 4);
462 if (!(sta
->flags
& 0x40)) {
471 setup_bcom_wds(skfd
, ifname
);
475 static void setup_bcom_vif_sec(int skfd
, char *ifname
, int vif
)
477 int val
, wep
, wsec
, i
;
480 wsec
= bcom_get_wsec(vif
);
482 val
= bcom_get_wauth(vif
);
486 bcom_set_bss_int(skfd
, ifname
, vif
, "wpa_auth", val
);
489 if (WPA_AUTH_PSK
| WPA2_AUTH_PSK
) {
490 v
= nvram_safe_get(wl_var("wpa_psk"));
491 if ((strlen(v
) >= 8) && (strlen(v
) < 63) && nvram_match(wl_var("mode"), "wet") && (vif
== 0)) {
492 /* Enable in-driver WPA supplicant */
495 pmk
.key_len
= (unsigned short) strlen(v
);
496 pmk
.flags
= WSEC_PASSPHRASE
;
498 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC_PMK
, &pmk
, sizeof(pmk
));
499 bcom_set_int(skfd
, ifname
, "sup_wpa", 1);
501 bcom_set_int(skfd
, ifname
, "sup_wpa", 0);
504 bcom_set_bss_int(skfd
, ifname
, vif
, "eap_restrict", 1);
505 bcom_set_bss_int(skfd
, ifname
, vif
, "wsec", wsec
);
506 bcom_set_bss_int(skfd
, ifname
, vif
, "wsec_restrict", 1);
508 bcom_set_bss_int(skfd
, ifname
, vif
, "eap_restrict", 0);
509 if (wep
= nvram_enabled(vif_var(vif
, "wep"))) {
510 wep
= atoi(nvram_safe_get(vif_var(vif
, "key")));
511 if ((wep
>= 1) && (wep
<= 4)) {
512 for (i
= 1; i
< 4; i
++) {
514 char name
[5] = "key0";
515 unsigned char *kdata
= k
.data
;
518 bzero(&k
, sizeof(k
));
520 kstr
= nvram_safe_get(vif_var(vif
, name
));
521 k
.len
= strlen(kstr
);
522 if ((k
.len
== 10) || (k
.len
== 26)) {
525 fprintf(stderr
, "Adding WEP key %d to VIF %d: ", i
, vif
);
529 strncpy(name
, kstr
, 2);
531 *kdata
= (unsigned char) strtoul(name
, NULL
, 16);
533 fprintf(stderr
, "%02x", *kdata
);
540 fprintf(stderr
, "\n");
545 if ((k
.len
> 0) && (i
== wep
))
546 k
.flags
= WL_PRIMARY_KEY
;
548 bcom_set_bss_var(skfd
, ifname
, vif
, "wsec_key", &k
, sizeof(k
));
551 bcom_set_bss_int(skfd
, ifname
, vif
, "wsec", WEP_ENABLED
);
552 bcom_set_bss_int(skfd
, ifname
, vif
, "wsec_restrict", 1);
553 bcom_set_bss_int(skfd
, ifname
, vif
, "auth", 1);
561 bcom_set_bss_int(skfd
, ifname
, vif
, "wsec", 0);
562 bcom_set_bss_int(skfd
, ifname
, vif
, "wsec_restrict", 0);
565 // bcom_set_bss_int(skfd, ifname, vif, "auth", atoi(nvram_safe_get(vif_var(vif, "auth"))));
568 static void setup_bcom_vif(int skfd
, char *ifname
, int vif
)
570 int val
, wep
, wsec
, i
;
574 s
= nvram_safe_get(vif_var(vif
, "ssid"));
575 strncpy(ssid
.SSID
, s
, sizeof(ssid
.SSID
));
576 ssid
.SSID_len
= strlen(ssid
.SSID
);
577 ssid
.SSID_len
= ((ssid
.SSID_len
> sizeof(ssid
.SSID
)) ? sizeof(ssid
.SSID
) : ssid
.SSID_len
);
578 bcom_set_bss_var(skfd
, ifname
, vif
, "ssid", &ssid
, sizeof(ssid
));
580 val
= nvram_enabled(vif_var(vif
, "closed"));
581 bcom_set_bss_int(skfd
, ifname
, vif
, "closednet", val
);
583 val
= nvram_enabled(wl_var("ap_isolate"));
584 bcom_set_bss_int(skfd
, ifname
, vif
, "ap_isolate", val
);
588 static void start_bcom_vif(int skfd
, char *ifname
, int vif
)
595 for (i
= 0; i
< ADD_VIF_RETRIES
; i
++) {
596 if (bcom_set_var(skfd
, ifname
, "bss" , cfg
, sizeof(cfg
)) == 0)
602 static void setup_bcom_common(int skfd
, char *ifname
)
605 char buf
[8192], wbuf
[80], *v
;
607 nvram_set(wl_var("ifname"), ifname
);
610 strncpy(buf
, nvram_safe_get(wl_var("country_code")), 4);
612 bcom_ioctl(skfd
, ifname
, WLC_SET_COUNTRY
, buf
, 4);
614 if (v
= nvram_get(wl_var("txpwr"))) {
616 val
= mw_to_qdbm(val
);
617 bcom_set_int(skfd
, ifname
, "qtxpower", val
);
620 /* Set other options */
621 val
= nvram_enabled(wl_var("lazywds"));
622 bcom_ioctl(skfd
, ifname
, WLC_SET_LAZYWDS
, &val
, sizeof(val
));
624 if (v
= nvram_get(wl_var("dtim"))) {
626 bcom_ioctl(skfd
, ifname
, WLC_SET_DTIMPRD
, &val
, sizeof(val
));
628 if (v
= nvram_get(wl_var("bcn"))) {
630 bcom_ioctl(skfd
, ifname
, WLC_SET_BCNPRD
, &val
, sizeof(val
));
632 if (v
= nvram_get(wl_var("antdiv"))) {
634 bcom_ioctl(skfd
, ifname
, WLC_SET_ANTDIV
, &val
, sizeof(val
));
636 if (v
= nvram_get(wl_var("txant"))) {
638 bcom_ioctl(skfd
, ifname
, WLC_SET_TXANT
, &val
, sizeof(val
));
640 if (v
= nvram_get(wl_var("maxassoc"))) {
642 bcom_set_int(skfd
, ifname
, "maxassoc", val
);
645 val
= nvram_enabled(wl_var("frameburst"));
646 bcom_ioctl(skfd
, ifname
, WLC_SET_FAKEFRAG
, &val
, sizeof(val
));
648 ap
= !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
651 val
= setup_bcom_wds(skfd
, ifname
);
653 if ((!ap
|| val
) && is_new_bcom(skfd
, ifname
))
654 start_watchdog(skfd
, ifname
);
656 /* Set up afterburner, disabled it if WDS is enabled */
657 if (val
|| nvram_enabled(wl_var("lazywds"))) {
661 if (nvram_enabled(wl_var("afterburner")))
663 if (nvram_disabled(wl_var("afterburner")))
667 bcom_set_var(skfd
, ifname
, "afterburner_override", &val
, sizeof(val
));
669 /* Set up MAC list */
670 if (nvram_match(wl_var("macmode"), "allow"))
671 val
= WLC_MACMODE_ALLOW
;
672 else if (nvram_match(wl_var("macmode"), "deny"))
673 val
= WLC_MACMODE_DENY
;
675 val
= WLC_MACMODE_DISABLED
;
677 if ((val
!= WLC_MACMODE_DISABLED
) && (v
= nvram_get(wl_var("maclist")))) {
678 struct maclist
*mac_list
;
679 struct ether_addr
*addr
;
682 memset(buf
, 0, 8192);
683 mac_list
= (struct maclist
*) buf
;
686 foreach(wbuf
, v
, next
) {
687 if (ether_atoe(wbuf
, addr
->ether_addr_octet
)) {
692 bcom_ioctl(skfd
, ifname
, WLC_SET_MACLIST
, buf
, sizeof(buf
));
694 val
= WLC_MACMODE_DISABLED
;
696 bcom_ioctl(skfd
, ifname
, WLC_SET_MACMODE
, &val
, sizeof(val
));
699 bcom_ioctl(skfd
, ifname
, WLC_GET_PHYTYPE
, &val
, sizeof(val
));
701 int override
= WLC_G_PROTECTION_OFF
;
702 int control
= WLC_G_PROTECTION_CTL_OFF
;
704 if (v
= nvram_get(wl_var("gmode")))
712 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE
, &val
, sizeof(val
));
714 if (nvram_match(wl_var("gmode_protection"), "auto")) {
715 override
= WLC_G_PROTECTION_AUTO
;
716 control
= WLC_G_PROTECTION_CTL_OVERLAP
;
718 if (nvram_enabled(wl_var("gmode_protection"))) {
719 override
= WLC_G_PROTECTION_ON
;
720 control
= WLC_G_PROTECTION_CTL_OVERLAP
;
722 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE_PROTECTION_CONTROL
, &override
, sizeof(control
));
723 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE_PROTECTION_OVERRIDE
, &override
, sizeof(override
));
726 if (nvram_match(wl_var("plcphdr"), "long"))
729 val
= WLC_PLCP_SHORT
;
731 bcom_ioctl(skfd
, ifname
, WLC_SET_PLCPHDR
, &val
, sizeof(val
));
736 static void setup_bcom_new(int skfd
, char *ifname
)
739 int iface
[16], ifaces
= 1;
740 int ap
, apsta
, sta
, wet
;
743 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
748 for (i
= 0; i
< 16; i
++) {
749 int cfg
[2]; /* index, enabled */
754 bcom_set_var(skfd
, ifname
, "bss", cfg
, sizeof(cfg
));
756 if ((i
> 0) && nvram_enabled(vif_var(i
, "enabled")) && (i
== 0 || nvram_get(vif_var(i
, "ssid")))) {
762 set_wext_mode(skfd
, ifname
);
764 ap
= nvram_match(wl_var("mode"), "ap") || nvram_match(wl_var("mode"), "apsta");
765 apsta
= nvram_match(wl_var("mode"), "apsta");
766 sta
= nvram_match(wl_var("mode"), "sta");
768 bcom_set_int(skfd
, ifname
, "apsta", apsta
);
769 bcom_set_int(skfd
, ifname
, "mssid", (ifaces
> 1));
771 for (i
= 0; i
< (sta
? 0 : ifaces
); i
++) {
773 fprintf(stderr
, "setup_bcom_vif(%d) start\n", iface
[i
]);
775 setup_bcom_vif(skfd
, ifname
, iface
[i
]);
777 fprintf(stderr
, "setup_bcom_vif(%d) end\n", iface
[i
]);
782 if (v
= nvram_get(wl_var("rts"))) {
784 bcom_set_int(skfd
, ifname
, "rtsthresh", val
);
786 if (v
= nvram_get(wl_var("frag"))) {
788 bcom_set_int(skfd
, ifname
, "fragthresh", val
);
791 val
= (nvram_disabled(wl_var("radio")) ? (1 | (1 << 16)) : 0);
792 bcom_ioctl(skfd
, ifname
, WLC_SET_RADIO
, &val
, sizeof(val
));
794 setup_bcom_common(skfd
, ifname
);
795 start_bcom(skfd
, ifname
);
797 val
= atoi(nvram_safe_get(wl_var("channel")));
799 bcom_ioctl(skfd
, ifname
, WLC_SET_CHANNEL
, &val
, sizeof(val
));
802 bcom_ioctl(skfd
, ifname
, WLC_SET_CS_SCAN_TIMER
, &val
, sizeof(val
));
804 for (i
= 0; i
< (sta
? 0 : ifaces
); i
++) {
805 setup_bcom_vif_sec(skfd
, ifname
, iface
[i
]);
808 for (i
= 0; i
< (sta
? 0 : ifaces
); i
++) {
809 start_bcom_vif(skfd
, ifname
, iface
[i
]);
813 static void setup_bcom_old(int skfd
, char *ifname
)
820 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
823 setup_bcom_common(skfd
, ifname
);
825 if (v
= nvram_get(wl_var("frag"))) {
827 bcom_ioctl(skfd
, ifname
, WLC_SET_FRAG
, &val
, sizeof(val
));
829 if (v
= nvram_get(wl_var("rts"))) {
831 bcom_ioctl(skfd
, ifname
, WLC_SET_RTS
, &val
, sizeof(val
));
834 val
= nvram_enabled(wl_var("closed"));
835 bcom_ioctl(skfd
, ifname
, WLC_SET_CLOSED
, &val
, sizeof(val
));
837 val
= nvram_enabled(wl_var("ap_isolate"));
838 bcom_set_int(skfd
, ifname
, "ap_isolate", val
);
840 start_bcom(skfd
, ifname
);
841 set_wext_ssid(skfd
, ifname
);
843 val
= bcom_get_wauth(0);
844 bcom_ioctl(skfd
, ifname
, WLC_SET_WPA_AUTH
, &val
, sizeof(val
));
846 if (val
& (WPA_AUTH_PSK
| WPA2_AUTH_PSK
)) {
847 v
= nvram_safe_get(wl_var("wpa_psk"));
848 if ((strlen(v
) >= 8) && (strlen(v
) < 63) && nvram_match(wl_var("mode"), "wet")) {
849 /* Enable in-driver WPA supplicant */
852 pmk
.key_len
= (unsigned short) strlen(v
);
853 pmk
.flags
= WSEC_PASSPHRASE
;
855 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC_PMK
, &pmk
, sizeof(pmk
));
856 bcom_set_int(skfd
, ifname
, "sup_wpa", 1);
861 bcom_ioctl(skfd
, ifname
, WLC_SET_EAP_RESTRICT
, &val
, sizeof(val
));
862 val
= bcom_get_wsec(0);
863 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC
, &val
, sizeof(val
));
866 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC
, &val
, sizeof(val
));
867 bcom_ioctl(skfd
, ifname
, WLC_SET_EAP_RESTRICT
, &val
, sizeof(val
));
868 bcom_set_int(skfd
, ifname
, "sup_wpa", 0);
872 static void set_wext_ssid(int skfd
, char *ifname
)
875 char essid
[IW_ESSID_MAX_SIZE
+ 1];
878 buffer
= nvram_get(wl_var("ssid"));
880 if (!buffer
|| (strlen(buffer
) > IW_ESSID_MAX_SIZE
))
883 wrq
.u
.essid
.flags
= 1;
884 strcpy(essid
, buffer
);
885 wrq
.u
.essid
.pointer
= (caddr_t
) essid
;
886 wrq
.u
.essid
.length
= strlen(essid
) + 1;
887 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWESSID
, &wrq
, "Set ESSID");
890 static void setup_wext_wep(int skfd
, char *ifname
)
896 unsigned char key
[IW_ENCODING_TOKEN_MAX
];
898 memset(&wrq
, 0, sizeof(wrq
));
899 strcpy(keystr
, "key1");
900 for (i
= 1; i
<= 4; i
++) {
901 if (keyval
= nvram_get(wl_var(keystr
))) {
902 keylen
= iw_in_key(keyval
, key
);
905 wrq
.u
.data
.length
= keylen
;
906 wrq
.u
.data
.pointer
= (caddr_t
) key
;
907 wrq
.u
.data
.flags
= i
;
908 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWENCODE
, &wrq
, "Set Encode");
914 memset(&wrq
, 0, sizeof(wrq
));
915 i
= atoi(nvram_safe_get(wl_var("key")));
916 if (i
> 0 && i
< 4) {
917 wrq
.u
.data
.flags
= i
| IW_ENCODE_RESTRICTED
;
918 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWENCODE
, &wrq
, "Set Encode");
922 static void setup_wext(int skfd
, char *ifname
)
928 int channel
= atoi(nvram_safe_get(wl_var("channel")));
932 wrq
.u
.freq
.flags
= 0;
935 wrq
.u
.freq
.flags
= IW_FREQ_FIXED
;
936 wrq
.u
.freq
.m
= channel
;
937 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWFREQ
, &wrq
, "Set Frequency");
940 /* Disable radio if wlX_radio is set and not enabled */
941 wrq
.u
.txpower
.disabled
= nvram_disabled(wl_var("radio"));
943 wrq
.u
.txpower
.value
= -1;
944 wrq
.u
.txpower
.fixed
= 1;
945 wrq
.u
.txpower
.flags
= IW_TXPOW_DBM
;
946 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWTXPOW
, &wrq
, "Set Tx Power");
949 if (nvram_enabled(wl_var("wep")) && !wpa_enc
)
950 setup_wext_wep(skfd
, ifname
);
953 set_wext_ssid(skfd
, ifname
);
957 static int setup_interfaces(int skfd
, char *ifname
, char *args
[], int count
)
962 /* Avoid "Unused parameter" warning */
963 args
= args
; count
= count
;
965 if(iw_get_ext(skfd
, ifname
, SIOCGIWNAME
, &wrq
) < 0)
968 if (strncmp(ifname
, "ath", 3) == 0) {
969 set_wext_mode(skfd
, ifname
);
970 setup_wext(skfd
, ifname
);
972 if (is_new_bcom(skfd
, ifname
)) {
974 fprintf(stderr
, "New Broadcom driver detected.\n");
976 stop_bcom(skfd
, ifname
);
978 fprintf(stderr
, "Setup start.\n");
980 setup_bcom_new(skfd
, ifname
);
982 fprintf(stderr
, "Setup done.\n");
986 fprintf(stderr
, "Old Broadcom driver detected.\n");
988 stop_bcom(skfd
, ifname
);
989 set_wext_mode(skfd
, ifname
);
990 setup_bcom_old(skfd
, ifname
);
991 setup_wext(skfd
, ifname
);
998 int main(int argc
, char **argv
)
1001 if((skfd
= iw_sockets_open()) < 0) {
1006 prefix
= strdup("wl0");
1007 iw_enum_devices(skfd
, &setup_interfaces
, NULL
, 0);