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