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