7a9cde5a1ad879f350ded97c59968bb4fee73b1f
[openwrt/svn-archive/archive.git] / openwrt / package / wificonf / wificonf.c
1 /*
2 * Wireless Network Adapter configuration utility
3 *
4 * Copyright (C) 2005 Felix Fietkau <nbd@vd-s.ath.cx>
5 *
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.
10 *
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.
15 */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <iwlib.h>
20 #include <bcmnvram.h>
21 #include <shutils.h>
22 #include <wlioctl.h>
23
24 /*------------------------------------------------------------------*/
25 /*
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
30 */
31 #define ERR_SET_EXT(rname, request) \
32 fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n", \
33 rname, request)
34
35 #define ABORT_ARG_NUM(rname, request) \
36 do { \
37 ERR_SET_EXT(rname, request); \
38 fprintf(stderr, " too few arguments.\n"); \
39 } while(0)
40
41 #define ABORT_ARG_TYPE(rname, request, arg) \
42 do { \
43 ERR_SET_EXT(rname, request); \
44 fprintf(stderr, " invalid argument \"%s\".\n", arg); \
45 } while(0)
46
47 #define ABORT_ARG_SIZE(rname, request, max) \
48 do { \
49 ERR_SET_EXT(rname, request); \
50 fprintf(stderr, " argument too big (max %d)\n", max); \
51 } while(0)
52
53 /*------------------------------------------------------------------*/
54 /*
55 * Wrapper to push some Wireless Parameter in the driver
56 * Use standard wrapper and add pretty error message if fail...
57 */
58 #define IW_SET_EXT_ERR(skfd, ifname, request, wrq, rname) \
59 do { \
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)); \
64 } } while(0)
65
66 /*------------------------------------------------------------------*/
67 /*
68 * Wrapper to extract some Wireless Parameter out of the driver
69 * Use standard wrapper and add pretty error message if fail...
70 */
71 #define IW_GET_EXT_ERR(skfd, ifname, request, wrq, rname) \
72 do { \
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)); \
77 } } while(0)
78
79 char *prefix;
80 char buffer[128];
81
82 char *wl_var(char *name)
83 {
84 strcpy(buffer, prefix);
85 strcat(buffer, name);
86 }
87
88 int nvram_enabled(char *name)
89 {
90 return (nvram_match(name, "1") || nvram_match(name, "on") || nvram_match(name, "enabled") || nvram_match(name, "true") || nvram_match(name, "yes") ? 1 : 0);
91 }
92
93 int nvram_disabled(char *name)
94 {
95 return (nvram_match(name, "0") || nvram_match(name, "off") || nvram_match(name, "disabled") || nvram_match(name, "false") || nvram_match(name, "no") ? 1 : 0);
96 }
97
98
99 int bcom_ioctl(int skfd, char *ifname, int cmd, void *buf, int len)
100 {
101 struct ifreq ifr;
102 wl_ioctl_t ioc;
103 int ret;
104
105 ioc.cmd = cmd;
106 ioc.buf = buf;
107 ioc.len = len;
108
109 ifr.ifr_data = (caddr_t) &ioc;
110 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
111
112 ret = ioctl(skfd, SIOCDEVPRIVATE, &ifr);
113
114 return ret;
115 }
116
117 int bcom_set_val(int skfd, char *ifname, char *var, void *val, int len)
118 {
119 char buf[8192];
120 int ret;
121
122 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
123 return -1;
124
125 strcpy(buf, var);
126
127 if ((ret = bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, sizeof(buf))))
128 return ret;
129
130 memcpy(val, buf, len);
131 return 0;
132 }
133
134 int bcom_set_int(int skfd, char *ifname, char *var, int val)
135 {
136 return bcom_set_val(skfd, ifname, var, &val, sizeof(val));
137 }
138
139 void setup_bcom(int skfd, char *ifname)
140 {
141 int val = 0;
142 char buf[8192];
143 char wbuf[80];
144 char *v;
145
146 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
147 return;
148
149 bcom_ioctl(skfd, ifname, WLC_DOWN, NULL, 0);
150
151 if (nvram_match(wl_var("auth_mode"), "wpa") || nvram_match(wl_var("auth_mode"), "psk") || (nvram_get(wl_var("akm")) && !nvram_disabled(wl_var("akm")))) {
152 /* Set up WPA */
153 if (nvram_match(wl_var("crypto"), "tkip"))
154 val = TKIP_ENABLED;
155 else if (nvram_match(wl_var("crypto"), "aes"))
156 val = AES_ENABLED;
157 else if (nvram_match(wl_var("crypto"), "tkip+aes"))
158 val = TKIP_ENABLED | AES_ENABLED;
159 else
160 val = 0;
161 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
162
163 if (val && nvram_get(wl_var("wpa_psk"))) {
164 val = 1;
165 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
166 }
167 }
168
169 /* Set up afterburner */
170 val = ABO_AUTO;
171 if (nvram_enabled(wl_var("afterburner")))
172 val = ABO_ON;
173 if (nvram_disabled(wl_var("afterburner")))
174 val = ABO_OFF;
175 bcom_set_val(skfd, ifname, "afterburner_override", &val, sizeof(val));
176
177 /* Set other options */
178 if (v = nvram_get(wl_var("lazywds"))) {
179 val = atoi(v);
180 bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
181 }
182 if (v = nvram_get(wl_var("frag"))) {
183 val = atoi(v);
184 bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
185 }
186 if (v = nvram_get(wl_var("dtim"))) {
187 val = atoi(v);
188 bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
189 }
190 if (v = nvram_get(wl_var("bcn"))) {
191 val = atoi(v);
192 bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
193 }
194 if (v = nvram_get(wl_var("rts"))) {
195 val = atoi(v);
196 bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
197 }
198 if (v = nvram_get(wl_var("antdiv"))) {
199 val = atoi(v);
200 bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
201 }
202 if (v = nvram_get(wl_var("txant"))) {
203 val = atoi(v);
204 bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
205 }
206
207 val = nvram_enabled(wl_var("closed"));
208 bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));
209
210 val = nvram_enabled(wl_var("ap_isolate"));
211 bcom_set_int(skfd, ifname, "ap_isolate", val);
212
213 val = nvram_enabled(wl_var("frameburst"));
214 bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));
215
216 /* Set up MAC list */
217 if (nvram_match(wl_var("macmode"), "allow"))
218 val = WLC_MACMODE_ALLOW;
219 else if (nvram_match(wl_var("macmode"), "deny"))
220 val = WLC_MACMODE_DENY;
221 else
222 val = WLC_MACMODE_DISABLED;
223
224 if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
225 struct maclist *mac_list;
226 struct ether_addr *addr;
227 char *next;
228
229 memset(buf, 0, 8192);
230 mac_list = (struct maclist *) buf;
231 addr = mac_list->ea;
232
233 foreach(wbuf, v, next) {
234 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
235 mac_list->count++;
236 addr++;
237 }
238 }
239 bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
240 } else {
241 val = WLC_MACMODE_DISABLED;
242 }
243 bcom_ioctl(skfd, ifname, WLC_SET_MACMODE, &val, sizeof(val));
244
245 if (v = nvram_get(wl_var("wds"))) {
246 struct maclist *wdslist = (struct maclist *) buf;
247 struct ether_addr *addr = wdslist->ea;
248 char *next;
249
250 memset(buf, 0, 8192);
251 foreach(wbuf, v, next) {
252 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
253 wdslist->count++;
254 addr++;
255 }
256 }
257 bcom_ioctl(skfd, ifname, WLC_SET_WDSLIST, buf, sizeof(buf));
258 }
259
260 /* Set up G mode */
261 bcom_ioctl(skfd, ifname, WLC_GET_PHYTYPE, &val, sizeof(val));
262 if (val == 2) {
263 int override = WLC_G_PROTECTION_OFF;
264 int control = WLC_G_PROTECTION_CTL_OFF;
265
266 if (v = nvram_get(wl_var("gmode")))
267 val = atoi(v);
268 else
269 val = 1;
270
271 if (val > 5)
272 val = 1;
273
274 bcom_ioctl(skfd, ifname, WLC_SET_GMODE, &val, sizeof(val));
275
276 if (nvram_match(wl_var("gmode_protection"), "auto")) {
277 override = WLC_G_PROTECTION_AUTO;
278 control = WLC_G_PROTECTION_CTL_OVERLAP;
279 }
280 if (nvram_enabled(wl_var("gmode_protection"))) {
281 override = WLC_G_PROTECTION_ON;
282 control = WLC_G_PROTECTION_CTL_OVERLAP;
283 }
284 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_CONTROL, &override, sizeof(control));
285 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_OVERRIDE, &override, sizeof(override));
286
287 if (val = 0) {
288 if (nvram_match(wl_var("plcphdr"), "long"))
289 val = WLC_PLCP_AUTO;
290 else
291 val = WLC_PLCP_SHORT;
292
293 bcom_ioctl(skfd, ifname, WLC_SET_PLCPHDR, &val, sizeof(val));
294 }
295 }
296
297 }
298
299 void set_wext_ssid(int skfd, char *ifname)
300 {
301 char *buffer;
302 struct iwreq wrq;
303
304 if (buffer = nvram_get(wl_var("ssid"))) {
305 if (strlen(buffer) > IW_ESSID_MAX_SIZE) {
306 ABORT_ARG_SIZE("Set ESSID", SIOCSIWESSID, IW_ESSID_MAX_SIZE);
307 } else {
308 char essid[IW_ESSID_MAX_SIZE + 1];
309
310 wrq.u.essid.flags = 1;
311 strcpy(essid, buffer);
312 wrq.u.essid.pointer = (caddr_t) essid;
313 wrq.u.essid.length = strlen(essid) + 1;
314 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq, "Set ESSID");
315 }
316 }
317 }
318
319 void start_bcom(int skfd, char *ifname)
320 {
321 int val = 0;
322
323 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
324 return;
325
326 bcom_ioctl(skfd, ifname, WLC_UP, &val, sizeof(val));
327
328 /* Need to re-set SSID after WLC_UP */
329 set_wext_ssid(skfd, ifname);
330 }
331
332 void setup_wext_wep(int skfd, char *ifname)
333 {
334 int i, keylen;
335 struct iwreq wrq;
336 char keystr[5];
337 char *keyval;
338 unsigned char key[IW_ENCODING_TOKEN_MAX];
339
340 strcpy(keystr, "key1");
341 for (i = 1; i <= 4; i++) {
342 if (keyval = nvram_get(wl_var(keystr))) {
343 keylen = iw_in_key(keyval, key);
344
345 if (keylen > 0) {
346 wrq.u.data.length = keylen;
347 wrq.u.data.pointer = (caddr_t) key;
348 wrq.u.data.flags = i;
349 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
350 }
351 }
352 keystr[3]++;
353 }
354
355
356 i = atoi(nvram_safe_get(wl_var("key")));
357 if (i > 0 && i < 4) {
358 wrq.u.data.flags = i | IW_ENCODE_RESTRICTED;
359 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
360 }
361 }
362
363 void setup_wext(int skfd, char *ifname)
364 {
365 char *buffer;
366 struct iwreq wrq;
367
368 /* Set ESSID */
369 set_wext_ssid(skfd, ifname);
370
371 /* Set channel */
372 int channel = atoi(nvram_safe_get(wl_var("channel")));
373
374 wrq.u.freq.m = -1;
375 wrq.u.freq.e = 0;
376 wrq.u.freq.flags = 0;
377
378 if (channel > 0) {
379 wrq.u.freq.flags = IW_FREQ_FIXED;
380 wrq.u.freq.m = channel;
381 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq, "Set Frequency");
382 }
383
384 /* Set operation mode */
385 int ap = 0, infra = 0, wet = 0;
386
387 ap = !nvram_match(wl_var("mode"), "sta");
388 infra = !nvram_disabled(wl_var("infra"));
389 wet = nvram_enabled(wl_var("wet"));
390
391 wrq.u.mode = (!infra ? IW_MODE_ADHOC : (ap ? IW_MODE_MASTER : (wet ? IW_MODE_REPEAT : IW_MODE_INFRA)));
392 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWMODE, &wrq, "Set Mode");
393
394 /* Disable radio if wlX_radio is set and not enabled */
395 wrq.u.txpower.disabled = nvram_disabled(wl_var("radio"));
396
397 wrq.u.txpower.value = -1;
398 wrq.u.txpower.fixed = 1;
399 wrq.u.txpower.flags = IW_TXPOW_DBM;
400 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWTXPOW, &wrq, "Set Tx Power");
401
402 /* Set up WEP */
403 if (nvram_enabled(wl_var("wep")))
404 setup_wext_wep(skfd, ifname);
405
406 }
407
408
409 static int setup_interfaces(int skfd, char *ifname, char *args[], int count)
410 {
411 struct iwreq wrq;
412 int rc;
413
414 /* Avoid "Unused parameter" warning */
415 args = args; count = count;
416
417 if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
418 return 0;
419
420 setup_bcom(skfd, ifname);
421 setup_wext(skfd, ifname);
422 start_bcom(skfd, ifname);
423 prefix[2]++;
424 }
425
426 int main(int argc, char **argv)
427 {
428 int skfd;
429 if((skfd = iw_sockets_open()) < 0) {
430 perror("socket");
431 exit(-1);
432 }
433
434 prefix = strdup("wl0_");
435 iw_enum_devices(skfd, &setup_interfaces, NULL, 0);
436
437 return 0;
438 }