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"); \
42 #define ABORT_ARG_TYPE(rname, request, arg) \
44 ERR_SET_EXT(rname, request); \
45 fprintf(stderr, " invalid argument \"%s\".\n", arg); \
49 #define ABORT_ARG_SIZE(rname, request, max) \
51 ERR_SET_EXT(rname, request); \
52 fprintf(stderr, " argument too big (max %d)\n", max); \
56 /*------------------------------------------------------------------*/
58 * Wrapper to push some Wireless Parameter in the driver
59 * Use standard wrapper and add pretty error message if fail...
61 #define IW_SET_EXT_ERR(skfd, ifname, request, wrq, rname) \
63 if(iw_set_ext(skfd, ifname, request, wrq) < 0) { \
64 ERR_SET_EXT(rname, request); \
65 fprintf(stderr, " SET failed on device %-1.16s ; %s.\n", \
66 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)); \
87 char *wl_var(char *name
)
89 strcpy(buffer
, prefix
);
93 int nvram_enabled(char *name
)
95 return (nvram_match(name
, "1") || nvram_match(name
, "on") || nvram_match(name
, "enabled") ? 1 : 0);
98 int nvram_disabled(char *name
)
100 return (nvram_match(name
, "0") || nvram_match(name
, "off") || nvram_match(name
, "disabled") ? 1 : 0);
104 int bcom_ioctl(int skfd
, char *ifname
, int cmd
, void *buf
, int len
)
114 ifr
.ifr_data
= (caddr_t
) &ioc
;
115 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
117 ret
= ioctl(skfd
, SIOCDEVPRIVATE
, &ifr
);
119 fprintf(stderr
, "bcom_ioctl [cmd=%d, buf=%08x, len=%d] failed: %d\n", cmd
, buf
, len
, ret
);
124 int bcom_set_val(int skfd
, char *ifname
, char *var
, void *val
, int len
)
129 if (strlen(var
) + 1 > sizeof(buf
) || len
> sizeof(buf
))
134 if ((ret
= bcom_ioctl(skfd
, ifname
, WLC_GET_VAR
, buf
, sizeof(buf
))))
137 memcpy(val
, buf
, len
);
141 int bcom_set_int(int skfd
, char *ifname
, char *var
, int val
)
143 return bcom_set_val(skfd
, ifname
, var
, &val
, sizeof(val
));
146 void setup_bcom(int skfd
, char *ifname
)
151 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
154 bcom_ioctl(skfd
, ifname
, WLC_DOWN
, NULL
, 0);
157 if (nvram_match(wl_var("crypto"), "tkip"))
159 else if (nvram_match(wl_var("crypto"), "aes"))
161 else if (nvram_match(wl_var("crypto"), "tkip+aes"))
162 val
= TKIP_ENABLED
| AES_ENABLED
;
165 bcom_ioctl(skfd
, ifname
, WLC_SET_WSEC
, &val
, sizeof(val
));
167 if (val
&& nvram_get(wl_var("wpa_psk"))) {
169 bcom_ioctl(skfd
, ifname
, WLC_SET_EAP_RESTRICT
, &val
, sizeof(val
));
172 /* Set up afterburner */
174 if (nvram_enabled(wl_var("afterburner")))
176 if (nvram_disabled(wl_var("afterburner")))
178 bcom_set_val(skfd
, ifname
, "afterburner_override", &val
, sizeof(val
));
180 /* Set other options */
181 val
= atoi(nvram_safe_get(wl_var("lazywds")));
182 bcom_ioctl(skfd
, ifname
, WLC_SET_LAZYWDS
, &val
, sizeof(val
));
183 val
= atoi(nvram_safe_get(wl_var("frag")));
184 bcom_ioctl(skfd
, ifname
, WLC_SET_FRAG
, &val
, sizeof(val
));
185 val
= atoi(nvram_safe_get(wl_var("dtim")));
186 bcom_ioctl(skfd
, ifname
, WLC_SET_DTIMPRD
, &val
, sizeof(val
));
187 val
= atoi(nvram_safe_get(wl_var("bcn")));
188 bcom_ioctl(skfd
, ifname
, WLC_SET_BCNPRD
, &val
, sizeof(val
));
189 val
= atoi(nvram_safe_get(wl_var("rts")));
190 bcom_ioctl(skfd
, ifname
, WLC_SET_RTS
, &val
, sizeof(val
));
191 val
= atoi(nvram_safe_get(wl_var("antdiv")));
192 bcom_ioctl(skfd
, ifname
, WLC_SET_ANTDIV
, &val
, sizeof(val
));
193 val
= atoi(nvram_safe_get(wl_var("txant")));
194 bcom_ioctl(skfd
, ifname
, WLC_SET_TXANT
, &val
, sizeof(val
));
196 val
= nvram_enabled(wl_var("ap_isolate"));
197 bcom_set_int(skfd
, ifname
, "ap_isolate", val
);
198 val
= nvram_enabled(wl_var("frameburst"));
199 bcom_ioctl(skfd
, ifname
, WLC_SET_FAKEFRAG
, &val
, sizeof(val
));
201 /* Set up MAC list */
202 if (nvram_match(wl_var("macmode"), "allow"))
203 val
= WLC_MACMODE_ALLOW
;
204 else if (nvram_match(wl_var("macmode"), "deny"))
205 val
= WLC_MACMODE_DENY
;
207 val
= WLC_MACMODE_DISABLED
;
209 if ((val
!= WLC_MACMODE_DISABLED
) && (v
= nvram_get(wl_var("maclist")))) {
211 struct maclist
*mac_list
;
212 struct ether_addr
*addr
;
215 memset(buf
, 0, 8192);
216 mac_list
= (struct maclist
*) buf
;
219 foreach(v
, nvram_safe_get(wl_var("maclist")), next
) {
220 if (ether_atoe(v
, addr
->ether_addr_octet
)) {
225 bcom_ioctl(skfd
, ifname
, WLC_SET_MACLIST
, buf
, sizeof(buf
));
227 val
= WLC_MACMODE_DISABLED
;
229 bcom_ioctl(skfd
, ifname
, WLC_SET_MACMODE
, &val
, sizeof(val
));
232 bcom_ioctl(skfd
, ifname
, WLC_GET_PHYTYPE
, &val
, sizeof(val
));
234 int override
= WLC_G_PROTECTION_OFF
;
235 int control
= WLC_G_PROTECTION_CTL_OFF
;
237 val
= atoi(nvram_safe_get(wl_var("gmode")));
240 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE
, &val
, sizeof(val
));
242 if (nvram_match(wl_var("gmode_protection"), "auto")) {
243 override
= WLC_G_PROTECTION_AUTO
;
244 control
= WLC_G_PROTECTION_CTL_OVERLAP
;
246 if (nvram_enabled(wl_var("gmode_protection"))) {
247 override
= WLC_G_PROTECTION_ON
;
248 control
= WLC_G_PROTECTION_CTL_OVERLAP
;
250 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE_PROTECTION_CONTROL
, &override
, sizeof(control
));
251 bcom_ioctl(skfd
, ifname
, WLC_SET_GMODE_PROTECTION_OVERRIDE
, &override
, sizeof(override
));
255 void set_wext_ssid(int skfd
, char *ifname
)
260 if (buffer
= nvram_get(wl_var("ssid"))) {
261 if (strlen(buffer
) > IW_ESSID_MAX_SIZE
) {
262 ABORT_ARG_SIZE("Set ESSID", SIOCSIWESSID
, IW_ESSID_MAX_SIZE
);
264 char essid
[IW_ESSID_MAX_SIZE
+ 1];
266 wrq
.u
.essid
.flags
= 1;
267 strcpy(essid
, buffer
);
268 wrq
.u
.essid
.pointer
= (caddr_t
) essid
;
269 wrq
.u
.essid
.length
= strlen(essid
) + 1;
270 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWESSID
, &wrq
, "Set ESSID");
275 void start_bcom(int skfd
, char *ifname
)
279 if (bcom_ioctl(skfd
, ifname
, WLC_GET_MAGIC
, &val
, sizeof(val
)) < 0)
282 bcom_ioctl(skfd
, ifname
, WLC_UP
, &val
, sizeof(val
));
284 /* Need to re-set SSID after WLC_UP */
285 set_wext_ssid(skfd
, ifname
);
288 void setup_wext_wep(int skfd
, char *ifname
)
294 unsigned char key
[IW_ENCODING_TOKEN_MAX
];
296 strcpy(keystr
, "key1");
297 for (i
= 1; i
<= 4; i
++) {
298 if (keyval
= nvram_get(wl_var(keystr
))) {
299 keylen
= iw_in_key(keyval
, key
);
302 wrq
.u
.data
.length
= keylen
;
303 wrq
.u
.data
.pointer
= (caddr_t
) key
;
304 wrq
.u
.data
.flags
= i
;
305 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWENCODE
, &wrq
, "Set Encode");
312 i
= atoi(nvram_safe_get(wl_var("key")));
313 if (i
> 0 && i
< 4) {
314 wrq
.u
.data
.flags
= i
| IW_ENCODE_RESTRICTED
;
315 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWENCODE
, &wrq
, "Set Encode");
319 void setup_wext(int skfd
, char *ifname
)
325 set_wext_ssid(skfd
, ifname
);
328 int channel
= atoi(nvram_safe_get(wl_var("channel")));
332 wrq
.u
.freq
.flags
= 0;
335 wrq
.u
.freq
.flags
= IW_FREQ_FIXED
;
336 wrq
.u
.freq
.m
= channel
;
338 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWFREQ
, &wrq
, "Set Frequency");
340 /* Set operation mode */
341 int ap
= 0, infra
= 0, wet
= 0;
343 ap
= (nvram_match(wl_var("mode"), "ap") || nvram_match(wl_var("mode"), "wds"));
344 infra
= nvram_enabled(wl_var("infra"));
345 wet
= nvram_enabled(wl_var("wet"));
347 wrq
.u
.mode
= (!infra
? IW_MODE_ADHOC
: (ap
? IW_MODE_MASTER
: (wet
? IW_MODE_REPEAT
: IW_MODE_INFRA
)));
348 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWMODE
, &wrq
, "Set Mode");
350 /* Disable radio if wlX_radio is set and not enabled */
351 if (nvram_disabled(wl_var("radio")))
352 wrq
.u
.txpower
.disabled
= 1;
354 wrq
.u
.txpower
.disabled
= 0;
356 wrq
.u
.txpower
.value
= -1;
357 wrq
.u
.txpower
.fixed
= 1;
358 wrq
.u
.txpower
.flags
= IW_TXPOW_MWATT
;
359 IW_SET_EXT_ERR(skfd
, ifname
, SIOCSIWTXPOW
, &wrq
, "Set Tx Power");
362 if (nvram_enabled(wl_var("wep")))
363 setup_wext_wep(skfd
, ifname
);
368 static int setup_interfaces(int skfd
, char *ifname
, char *args
[], int count
)
373 /* Avoid "Unused parameter" warning */
374 args
= args
; count
= count
;
376 if(iw_get_ext(skfd
, ifname
, SIOCGIWNAME
, &wrq
) < 0)
379 setup_bcom(skfd
, ifname
);
380 setup_wext(skfd
, ifname
);
381 start_bcom(skfd
, ifname
);
385 int main(int argc
, char **argv
)
389 if((skfd
= iw_sockets_open()) < 0) {
394 prefix
= strdup("wl0_");
395 iw_enum_devices(skfd
, &setup_interfaces
, NULL
, 0);