2 * Wireless Network Adapter configuration utility
4 * Copyright (C) 2005 Felix Fietkau <nbd@vd-s.ath.cx>
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.
24 /*------------------------------------------------------------------*/
26 * Macro to handle errors when setting WE
27 * Print a nice error message and exit...
28 * We define them as macro so that "return" do the right thing.
29 * The "do {...} while(0)" is a standard trick
31 #define ERR_SET_EXT(rname, request) \
32 fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n", \
35 #define ABORT_ARG_NUM(rname, request) \
37 ERR_SET_EXT(rname, request); \
38 fprintf(stderr, " too few arguments.\n"); \
41 #define ABORT_ARG_TYPE(rname, request, arg) \
43 ERR_SET_EXT(rname, request); \
44 fprintf(stderr, " invalid argument \"%s\".\n", arg); \
47 #define ABORT_ARG_SIZE(rname, request, max) \
49 ERR_SET_EXT(rname, request); \
50 fprintf(stderr, " argument too big (max %d)\n", max); \
53 /*------------------------------------------------------------------*/
55 * Wrapper to push some Wireless Parameter in the driver
56 * Use standard wrapper and add pretty error message if fail...
58 #define IW_SET_EXT_ERR(skfd, ifname, request, wrq, rname) \
60 if(iw_set_ext(skfd, ifname, request, wrq) < 0) { \
61 ERR_SET_EXT(rname, request); \
62 fprintf(stderr, " SET failed on device %-1.16s ; %s.\n", \
63 ifname, strerror(errno)); \
66 /*------------------------------------------------------------------*/
68 * Wrapper to extract some Wireless Parameter out of the driver
69 * Use standard wrapper and add pretty error message if fail...
71 #define IW_GET_EXT_ERR(skfd, ifname, request, wrq, rname) \
73 if(iw_get_ext(skfd, ifname, request, wrq) < 0) { \
74 ERR_SET_EXT(rname, request); \
75 fprintf(stderr, " GET failed on device %-1.16s ; %s.\n", \
76 ifname, strerror(errno)); \
79 void set_wext_ssid(int skfd
, char *ifname
);
85 char *wl_var(char *name
)
87 strcpy(buffer
, prefix
);
91 int nvram_enabled(char *name
)
93 return (nvram_match(name
, "1") || nvram_match(name
, "on") || nvram_match(name
, "enabled") || nvram_match(name
, "true") || nvram_match(name
, "yes") ? 1 : 0);
96 int nvram_disabled(char *name
)
98 return (nvram_match(name
, "0") || nvram_match(name
, "off") || nvram_match(name
, "disabled") || nvram_match(name
, "false") || nvram_match(name
, "no") ? 1 : 0);
102 int bcom_ioctl(int skfd
, char *ifname
, int cmd
, void *buf
, int len
)
112 ifr
.ifr_data
= (caddr_t
) &ioc
;
113 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
115 ret
= ioctl(skfd
, SIOCDEVPRIVATE
, &ifr
);
120 int bcom_set_val(int skfd
, char *ifname
, char *var
, void *val
, int len
)
125 if (strlen(var
) + 1 > sizeof(buf
) || len
> sizeof(buf
))
130 if ((ret
= bcom_ioctl(skfd
, ifname
, WLC_SET_VAR
, buf
, sizeof(buf
))))
133 memcpy(val
, buf
, len
);
137 int bcom_set_int(int skfd
, char *ifname
, char *var
, int val
)
139 return bcom_set_val(skfd
, ifname
, var
, &val
, sizeof(val
));
142 void stop_bcom(int skfd
, char *ifname
)
147 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
152 bcom_ioctl(skfd
, ifname
, WLC_SET_SSID
, &ssid
, sizeof(ssid
));
153 bcom_ioctl(skfd
, ifname
, WLC_DOWN
, NULL
, 0);
157 void start_bcom(int skfd
, char *ifname
)
161 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
164 bcom_ioctl(skfd
, ifname
, WLC_UP
, &val
, sizeof(val
));
165 set_wext_ssid(skfd
, ifname
);
169 void setup_bcom(int skfd
, char *ifname
)
176 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
179 stop_bcom(skfd
, ifname
);
182 strncpy(buf
, nvram_safe_get(wl_var("country_code")), 4);
184 bcom_ioctl(skfd
, ifname
, WLC_SET_COUNTRY
, buf
, 4);
186 /* Set up afterburner */
188 if (nvram_enabled(wl_var("afterburner")))
190 if (nvram_disabled(wl_var("afterburner")))
192 bcom_set_val(skfd
, ifname
, "afterburner_override", &val
, sizeof(val
));
194 /* Set other options */
195 val
= nvram_enabled(wl_var("lazywds"));
196 bcom_ioctl(skfd
, ifname
, WLC_SET_LAZYWDS
, &val
, sizeof(val
));
198 if (v
= nvram_get(wl_var("frag"))) {
200 bcom_ioctl(skfd
, ifname
, WLC_SET_FRAG
, &val
, sizeof(val
));
202 if (v
= nvram_get(wl_var("dtim"))) {
204 bcom_ioctl(skfd
, ifname
, WLC_SET_DTIMPRD
, &val
, sizeof(val
));
206 if (v
= nvram_get(wl_var("bcn"))) {
208 bcom_ioctl(skfd
, ifname
, WLC_SET_BCNPRD
, &val
, sizeof(val
));
210 if (v
= nvram_get(wl_var("rts"))) {
212 bcom_ioctl(skfd
, ifname
, WLC_SET_RTS
, &val
, sizeof(val
));
214 if (v
= nvram_get(wl_var("antdiv"))) {
216 bcom_ioctl(skfd
, ifname
, WLC_SET_ANTDIV
, &val
, sizeof(val
));
218 if (v
= nvram_get(wl_var("txant"))) {
220 bcom_ioctl(skfd
, ifname
, WLC_SET_TXANT
, &val
, sizeof(val
));
223 val
= nvram_enabled(wl_var("closed"));
224 bcom_ioctl(skfd
, ifname
, WLC_SET_CLOSED
, &val
, sizeof(val
));
226 val
= nvram_enabled(wl_var("ap_isolate"));
227 bcom_set_int(skfd
, ifname
, "ap_isolate", val
);
229 val
= nvram_enabled(wl_var("frameburst"));
230 bcom_ioctl(skfd
, ifname
, WLC_SET_FAKEFRAG
, &val
, sizeof(val
));
232 /* Set up MAC list */
233 if (nvram_match(wl_var("macmode"), "allow"))
234 val
= WLC_MACMODE_ALLOW
;
235 else if (nvram_match(wl_var("macmode"), "deny"))
236 val
= WLC_MACMODE_DENY
;
238 val
= WLC_MACMODE_DISABLED
;
240 if ((val
!= WLC_MACMODE_DISABLED
) && (v
= nvram_get(wl_var("maclist")))) {
241 struct maclist
*mac_list
;
242 struct ether_addr
*addr
;
245 memset(buf
, 0, 8192);
246 mac_list
= (struct maclist
*) buf
;
249 foreach(wbuf
, v
, next
) {
250 if (ether_atoe(wbuf
, addr
->ether_addr_octet
)) {
255 bcom_ioctl(skfd
, ifname
, WLC_SET_MACLIST
, buf
, sizeof(buf
));
257 val
= WLC_MACMODE_DISABLED
;
259 bcom_ioctl(skfd
, ifname
, WLC_SET_MACMODE
, &val
, sizeof(val
));
261 if (v
= nvram_get(wl_var("wds"))) {
262 struct maclist
*wdslist
= (struct maclist
*) buf
;
263 struct ether_addr
*addr
= wdslist
->ea
;
266 memset(buf
, 0, 8192);
267 foreach(wbuf
, v
, next
) {
268 if (ether_atoe(wbuf
, addr
->ether_addr_octet
)) {
273 bcom_ioctl(skfd
, ifname
, WLC_SET_WDSLIST
, buf
, sizeof(buf
));
277 bcom_ioctl(skfd
, ifname
, WLC_GET_PHYTYPE
, &val
, sizeof(val
));
279 int override
= WLC_G_PROTECTION_OFF
;
280 int control
= WLC_G_PROTECTION_CTL_OFF
;
282 if (v
= nvram_get(wl_var("gmode")))
290 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE
, &val
, sizeof(val
));
292 if (nvram_match(wl_var("gmode_protection"), "auto")) {
293 override
= WLC_G_PROTECTION_AUTO
;
294 control
= WLC_G_PROTECTION_CTL_OVERLAP
;
296 if (nvram_enabled(wl_var("gmode_protection"))) {
297 override
= WLC_G_PROTECTION_ON
;
298 control
= WLC_G_PROTECTION_CTL_OVERLAP
;
300 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE_PROTECTION_CONTROL
, &override
, sizeof(control
));
301 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE_PROTECTION_OVERRIDE
, &override
, sizeof(override
));
304 if (nvram_match(wl_var("plcphdr"), "long"))
307 val
= WLC_PLCP_SHORT
;
309 bcom_ioctl(skfd
, ifname
, WLC_SET_PLCPHDR
, &val
, sizeof(val
));
313 start_bcom(skfd
, ifname
);
315 if (!(v
= nvram_get(wl_var("akm"))))
316 v
= nvram_safe_get(wl_var("auth_mode"));
318 if (strstr(v
, "wpa") || strstr(v
, "psk")) {
322 if (nvram_match(wl_var("crypto"), "tkip"))
324 else if (nvram_match(wl_var("crypto"), "aes"))
326 else if (nvram_match(wl_var("crypto"), "tkip+aes"))
327 val
= TKIP_ENABLED
| AES_ENABLED
;
330 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC
, &val
, sizeof(val
));
332 if (val
&& strstr(v
, "psk")) {
333 v
= nvram_safe_get(wl_var("wpa_psk"));
335 if ((strlen(v
) >= 8) && (strlen(v
) < 63)) {
337 bcom_ioctl(skfd
, ifname
, WLC_SET_WPA_AUTH
, &val
, sizeof(val
));
339 bcom_ioctl(skfd
, ifname
, WLC_GET_AP
, &val
, sizeof(val
));
341 /* Enable in-driver WPA supplicant */
344 pmk
.key_len
= (unsigned short) strlen(v
);
345 pmk
.flags
= WSEC_PASSPHRASE
;
347 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC_PMK
, &pmk
, sizeof(pmk
));
348 bcom_set_int(skfd
, ifname
, "sup_wpa", 1);
353 bcom_ioctl(skfd
, ifname
, WLC_SET_EAP_RESTRICT
, &val
, sizeof(val
));
358 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC
, &val
, sizeof(val
));
359 bcom_ioctl(skfd
, ifname
, WLC_SET_WPA_AUTH
, &val
, sizeof(val
));
360 bcom_ioctl(skfd
, ifname
, WLC_SET_EAP_RESTRICT
, &val
, sizeof(val
));
361 bcom_set_int(skfd
, ifname
, "sup_wpa", 0);
365 void set_wext_ssid(int skfd
, char *ifname
)
368 char essid
[IW_ESSID_MAX_SIZE
+ 1];
371 buffer
= nvram_get(wl_var("ssid"));
373 if (!buffer
|| (strlen(buffer
) > IW_ESSID_MAX_SIZE
))
376 wrq
.u
.essid
.flags
= 1;
377 strcpy(essid
, buffer
);
378 wrq
.u
.essid
.pointer
= (caddr_t
) essid
;
379 wrq
.u
.essid
.length
= strlen(essid
) + 1;
380 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWESSID
, &wrq
, "Set ESSID");
383 void setup_wext_wep(int skfd
, char *ifname
)
389 unsigned char key
[IW_ENCODING_TOKEN_MAX
];
391 memset(&wrq
, 0, sizeof(wrq
));
392 strcpy(keystr
, "key1");
393 for (i
= 1; i
<= 4; i
++) {
394 if (keyval
= nvram_get(wl_var(keystr
))) {
395 keylen
= iw_in_key(keyval
, key
);
398 wrq
.u
.data
.length
= keylen
;
399 wrq
.u
.data
.pointer
= (caddr_t
) key
;
400 wrq
.u
.data
.flags
= i
;
401 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWENCODE
, &wrq
, "Set Encode");
407 memset(&wrq
, 0, sizeof(wrq
));
408 i
= atoi(nvram_safe_get(wl_var("key")));
409 if (i
> 0 && i
< 4) {
410 wrq
.u
.data
.flags
= i
| IW_ENCODE_RESTRICTED
;
411 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWENCODE
, &wrq
, "Set Encode");
415 void set_wext_mode(skfd
, ifname
)
418 int ap
= 0, infra
= 0, wet
= 0;
420 /* Set operation mode */
421 ap
= !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
422 infra
= !nvram_disabled(wl_var("infra"));
423 wet
= nvram_enabled(wl_var("wet")) || !nvram_match(wl_var("mode"), "wet");
425 wrq
.u
.mode
= (!infra
? IW_MODE_ADHOC
: (ap
? IW_MODE_MASTER
: (wet
? IW_MODE_REPEAT
: IW_MODE_INFRA
)));
426 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWMODE
, &wrq
, "Set Mode");
429 void setup_wext(int skfd
, char *ifname
)
435 int channel
= atoi(nvram_safe_get(wl_var("channel")));
439 wrq
.u
.freq
.flags
= 0;
442 wrq
.u
.freq
.flags
= IW_FREQ_FIXED
;
443 wrq
.u
.freq
.m
= channel
;
444 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWFREQ
, &wrq
, "Set Frequency");
448 /* Disable radio if wlX_radio is set and not enabled */
449 wrq
.u
.txpower
.disabled
= nvram_disabled(wl_var("radio"));
451 wrq
.u
.txpower
.value
= -1;
452 wrq
.u
.txpower
.fixed
= 1;
453 wrq
.u
.txpower
.flags
= IW_TXPOW_DBM
;
454 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWTXPOW
, &wrq
, "Set Tx Power");
457 if (nvram_enabled(wl_var("wep")) && !wpa_enc
)
458 setup_wext_wep(skfd
, ifname
);
461 set_wext_ssid(skfd
, ifname
);
466 static int setup_interfaces(int skfd
, char *ifname
, char *args
[], int count
)
471 /* Avoid "Unused parameter" warning */
472 args
= args
; count
= count
;
474 if(iw_get_ext(skfd
, ifname
, SIOCGIWNAME
, &wrq
) < 0)
477 if (strncmp(ifname
, "ath", 3) == 0) {
478 set_wext_mode(skfd
, ifname
);
479 setup_wext(skfd
, ifname
);
481 stop_bcom(skfd
, ifname
);
482 set_wext_mode(skfd
, ifname
);
483 setup_bcom(skfd
, ifname
);
484 setup_wext(skfd
, ifname
);
490 int main(int argc
, char **argv
)
493 if((skfd
= iw_sockets_open()) < 0) {
498 prefix
= strdup("wl0_");
499 iw_enum_devices(skfd
, &setup_interfaces
, NULL
, 0);