build jffs2 image too since it works with CF and MTD block emulation
[openwrt.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 void set_wext_ssid(int skfd, char *ifname);
80
81 char *prefix;
82 char buffer[128];
83
84 char *wl_var(char *name)
85 {
86 strcpy(buffer, prefix);
87 strcat(buffer, name);
88 }
89
90 int nvram_enabled(char *name)
91 {
92 return (nvram_match(name, "1") || nvram_match(name, "on") || nvram_match(name, "enabled") || nvram_match(name, "true") || nvram_match(name, "yes") ? 1 : 0);
93 }
94
95 int nvram_disabled(char *name)
96 {
97 return (nvram_match(name, "0") || nvram_match(name, "off") || nvram_match(name, "disabled") || nvram_match(name, "false") || nvram_match(name, "no") ? 1 : 0);
98 }
99
100
101 int bcom_ioctl(int skfd, char *ifname, int cmd, void *buf, int len)
102 {
103 struct ifreq ifr;
104 wl_ioctl_t ioc;
105 int ret;
106
107 ioc.cmd = cmd;
108 ioc.buf = buf;
109 ioc.len = len;
110
111 ifr.ifr_data = (caddr_t) &ioc;
112 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
113
114 ret = ioctl(skfd, SIOCDEVPRIVATE, &ifr);
115
116 return ret;
117 }
118
119 int bcom_set_val(int skfd, char *ifname, char *var, void *val, int len)
120 {
121 char buf[8192];
122 int ret;
123
124 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
125 return -1;
126
127 strcpy(buf, var);
128
129 if ((ret = bcom_ioctl(skfd, ifname, WLC_SET_VAR, buf, sizeof(buf))))
130 return ret;
131
132 memcpy(val, buf, len);
133 return 0;
134 }
135
136 int bcom_set_int(int skfd, char *ifname, char *var, int val)
137 {
138 return bcom_set_val(skfd, ifname, var, &val, sizeof(val));
139 }
140
141 void stop_bcom(int skfd, char *ifname)
142 {
143 int val = 0;
144 wlc_ssid_t ssid;
145
146 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
147 return;
148
149 ssid.SSID_len = 0;
150 ssid.SSID[0] = 0;
151 bcom_ioctl(skfd, ifname, WLC_SET_SSID, &ssid, sizeof(ssid));
152 bcom_ioctl(skfd, ifname, WLC_DOWN, NULL, 0);
153
154 }
155
156 void start_bcom(int skfd, char *ifname)
157 {
158 int val = 0;
159
160 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
161 return;
162
163 bcom_ioctl(skfd, ifname, WLC_UP, &val, sizeof(val));
164 set_wext_ssid(skfd, ifname);
165 }
166
167
168 void setup_bcom(int skfd, char *ifname)
169 {
170 int val = 0;
171 char buf[8192];
172 char wbuf[80];
173 char *v;
174
175 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
176 return;
177
178 stop_bcom(skfd, ifname);
179
180 /* Set Country */
181 strncpy(buf, nvram_safe_get(wl_var("country_code")), 4);
182 buf[3] = 0;
183 bcom_ioctl(skfd, ifname, WLC_SET_COUNTRY, buf, 4);
184
185 /* Set up afterburner */
186 val = ABO_AUTO;
187 if (nvram_enabled(wl_var("afterburner")))
188 val = ABO_ON;
189 if (nvram_disabled(wl_var("afterburner")))
190 val = ABO_OFF;
191 bcom_set_val(skfd, ifname, "afterburner_override", &val, sizeof(val));
192
193 /* Set other options */
194 if (v = nvram_get(wl_var("lazywds"))) {
195 val = atoi(v);
196 bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
197 }
198 if (v = nvram_get(wl_var("frag"))) {
199 val = atoi(v);
200 bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
201 }
202 if (v = nvram_get(wl_var("dtim"))) {
203 val = atoi(v);
204 bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
205 }
206 if (v = nvram_get(wl_var("bcn"))) {
207 val = atoi(v);
208 bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
209 }
210 if (v = nvram_get(wl_var("rts"))) {
211 val = atoi(v);
212 bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
213 }
214 if (v = nvram_get(wl_var("antdiv"))) {
215 val = atoi(v);
216 bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
217 }
218 if (v = nvram_get(wl_var("txant"))) {
219 val = atoi(v);
220 bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
221 }
222
223 val = nvram_enabled(wl_var("closed"));
224 bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));
225
226 val = nvram_enabled(wl_var("ap_isolate"));
227 bcom_set_int(skfd, ifname, "ap_isolate", val);
228
229 val = nvram_enabled(wl_var("frameburst"));
230 bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));
231
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;
237 else
238 val = WLC_MACMODE_DISABLED;
239
240 if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
241 struct maclist *mac_list;
242 struct ether_addr *addr;
243 char *next;
244
245 memset(buf, 0, 8192);
246 mac_list = (struct maclist *) buf;
247 addr = mac_list->ea;
248
249 foreach(wbuf, v, next) {
250 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
251 mac_list->count++;
252 addr++;
253 }
254 }
255 bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
256 } else {
257 val = WLC_MACMODE_DISABLED;
258 }
259 bcom_ioctl(skfd, ifname, WLC_SET_MACMODE, &val, sizeof(val));
260
261 if (v = nvram_get(wl_var("wds"))) {
262 struct maclist *wdslist = (struct maclist *) buf;
263 struct ether_addr *addr = wdslist->ea;
264 char *next;
265
266 memset(buf, 0, 8192);
267 foreach(wbuf, v, next) {
268 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
269 wdslist->count++;
270 addr++;
271 }
272 }
273 bcom_ioctl(skfd, ifname, WLC_SET_WDSLIST, buf, sizeof(buf));
274 }
275
276 /* Set up G mode */
277 bcom_ioctl(skfd, ifname, WLC_GET_PHYTYPE, &val, sizeof(val));
278 if (val == 2) {
279 int override = WLC_G_PROTECTION_OFF;
280 int control = WLC_G_PROTECTION_CTL_OFF;
281
282 if (v = nvram_get(wl_var("gmode")))
283 val = atoi(v);
284 else
285 val = 1;
286
287 if (val > 5)
288 val = 1;
289
290 bcom_ioctl(skfd, ifname, WLC_SET_GMODE, &val, sizeof(val));
291
292 if (nvram_match(wl_var("gmode_protection"), "auto")) {
293 override = WLC_G_PROTECTION_AUTO;
294 control = WLC_G_PROTECTION_CTL_OVERLAP;
295 }
296 if (nvram_enabled(wl_var("gmode_protection"))) {
297 override = WLC_G_PROTECTION_ON;
298 control = WLC_G_PROTECTION_CTL_OVERLAP;
299 }
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));
302
303 if (val = 0) {
304 if (nvram_match(wl_var("plcphdr"), "long"))
305 val = WLC_PLCP_AUTO;
306 else
307 val = WLC_PLCP_SHORT;
308
309 bcom_ioctl(skfd, ifname, WLC_SET_PLCPHDR, &val, sizeof(val));
310 }
311 }
312
313 start_bcom(skfd, ifname);
314
315 if (!(v = nvram_get(wl_var("akm"))))
316 v = nvram_safe_get(wl_var("auth_mode"));
317
318 if (strstr(v, "wpa") || strstr(v, "psk")) {
319 /* Set up WPA */
320 if (nvram_match(wl_var("crypto"), "tkip"))
321 val = TKIP_ENABLED;
322 else if (nvram_match(wl_var("crypto"), "aes"))
323 val = AES_ENABLED;
324 else if (nvram_match(wl_var("crypto"), "tkip+aes"))
325 val = TKIP_ENABLED | AES_ENABLED;
326 else
327 val = 0;
328 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
329
330 if (val && strstr(v, "psk")) {
331 v = nvram_safe_get(wl_var("wpa_psk"));
332
333 if ((strlen(v) >= 8) && (strlen(v) < 63)) {
334 val = 4;
335 bcom_ioctl(skfd, ifname, WLC_SET_WPA_AUTH, &val, sizeof(val));
336
337 bcom_ioctl(skfd, ifname, WLC_GET_AP, &val, sizeof(val));
338 if (!val) {
339 /* Enable in-driver WPA supplicant */
340 wsec_pmk_t pmk;
341
342 pmk.key_len = (unsigned short) strlen(v);
343 pmk.flags = WSEC_PASSPHRASE;
344 strcpy(pmk.key, v);
345 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
346 bcom_set_int(skfd, ifname, "sup_wpa", 1);
347 }
348 }
349 } else {
350 val = 1;
351 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
352 }
353 } else {
354 val = 0;
355
356 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
357 bcom_ioctl(skfd, ifname, WLC_SET_WPA_AUTH, &val, sizeof(val));
358 }
359
360
361 }
362
363 void set_wext_ssid(int skfd, char *ifname)
364 {
365 char *buffer;
366 struct iwreq wrq;
367
368 if (buffer = nvram_get(wl_var("ssid"))) {
369 if (strlen(buffer) > IW_ESSID_MAX_SIZE) {
370 ABORT_ARG_SIZE("Set ESSID", SIOCSIWESSID, IW_ESSID_MAX_SIZE);
371 } else {
372 char essid[IW_ESSID_MAX_SIZE + 1];
373
374 wrq.u.essid.flags = 1;
375 strcpy(essid, buffer);
376 wrq.u.essid.pointer = (caddr_t) essid;
377 wrq.u.essid.length = strlen(essid) + 1;
378 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq, "Set ESSID");
379 }
380 }
381 }
382 void setup_wext_wep(int skfd, char *ifname)
383 {
384 int i, keylen;
385 struct iwreq wrq;
386 char keystr[5];
387 char *keyval;
388 unsigned char key[IW_ENCODING_TOKEN_MAX];
389
390 strcpy(keystr, "key1");
391 for (i = 1; i <= 4; i++) {
392 if (keyval = nvram_get(wl_var(keystr))) {
393 keylen = iw_in_key(keyval, key);
394
395 if (keylen > 0) {
396 wrq.u.data.length = keylen;
397 wrq.u.data.pointer = (caddr_t) key;
398 wrq.u.data.flags = i;
399 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
400 }
401 }
402 keystr[3]++;
403 }
404
405
406 i = atoi(nvram_safe_get(wl_var("key")));
407 if (i > 0 && i < 4) {
408 wrq.u.data.flags = i | IW_ENCODE_RESTRICTED;
409 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
410 }
411 }
412
413 void set_wext_mode(skfd, ifname)
414 {
415 struct iwreq wrq;
416 int ap = 0, infra = 0, wet = 0;
417
418 /* Set operation mode */
419 ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
420 infra = !nvram_disabled(wl_var("infra"));
421 wet = nvram_enabled(wl_var("wet")) || !nvram_match(wl_var("mode"), "wet");
422
423 wrq.u.mode = (!infra ? IW_MODE_ADHOC : (ap ? IW_MODE_MASTER : (wet ? IW_MODE_REPEAT : IW_MODE_INFRA)));
424 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWMODE, &wrq, "Set Mode");
425 }
426
427 void setup_wext(int skfd, char *ifname)
428 {
429 char *buffer;
430 struct iwreq wrq;
431
432 /* Set channel */
433 int channel = atoi(nvram_safe_get(wl_var("channel")));
434
435 wrq.u.freq.m = -1;
436 wrq.u.freq.e = 0;
437 wrq.u.freq.flags = 0;
438
439 if (channel > 0) {
440 wrq.u.freq.flags = IW_FREQ_FIXED;
441 wrq.u.freq.m = channel;
442 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq, "Set Frequency");
443 }
444
445
446 /* Disable radio if wlX_radio is set and not enabled */
447 wrq.u.txpower.disabled = nvram_disabled(wl_var("radio"));
448
449 wrq.u.txpower.value = -1;
450 wrq.u.txpower.fixed = 1;
451 wrq.u.txpower.flags = IW_TXPOW_DBM;
452 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWTXPOW, &wrq, "Set Tx Power");
453
454 /* Set up WEP */
455 if (nvram_enabled(wl_var("wep")))
456 setup_wext_wep(skfd, ifname);
457
458 /* Set ESSID */
459 set_wext_ssid(skfd, ifname);
460
461 }
462
463
464 static int setup_interfaces(int skfd, char *ifname, char *args[], int count)
465 {
466 struct iwreq wrq;
467 int rc;
468
469 /* Avoid "Unused parameter" warning */
470 args = args; count = count;
471
472 if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
473 return 0;
474
475 stop_bcom(skfd, ifname);
476 set_wext_mode(skfd, ifname);
477 setup_bcom(skfd, ifname);
478 setup_wext(skfd, ifname);
479
480 prefix[2]++;
481 }
482
483 int main(int argc, char **argv)
484 {
485 int skfd;
486 if((skfd = iw_sockets_open()) < 0) {
487 perror("socket");
488 exit(-1);
489 }
490
491 prefix = strdup("wl0_");
492 iw_enum_devices(skfd, &setup_interfaces, NULL, 0);
493
494 return 0;
495 }
This page took 0.07238 seconds and 5 git commands to generate.