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