add less
[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 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") || nvram_match(name, "true") || nvram_match(name, "yes") ? 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") || nvram_match(name, "false") || nvram_match(name, "no") ? 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 buf[8192];
150 char wbuf[80];
151 char *v;
152
153 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
154 return;
155
156 bcom_ioctl(skfd, ifname, WLC_DOWN, NULL, 0);
157
158 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")))) {
159 /* Set up WPA */
160 if (nvram_match(wl_var("crypto"), "tkip"))
161 val = TKIP_ENABLED;
162 else if (nvram_match(wl_var("crypto"), "aes"))
163 val = AES_ENABLED;
164 else if (nvram_match(wl_var("crypto"), "tkip+aes"))
165 val = TKIP_ENABLED | AES_ENABLED;
166 else
167 val = 0;
168 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
169
170 if (val && nvram_get(wl_var("wpa_psk"))) {
171 val = 1;
172 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
173 }
174 }
175
176 /* Set up afterburner */
177 val = ABO_AUTO;
178 if (nvram_enabled(wl_var("afterburner")))
179 val = ABO_ON;
180 if (nvram_disabled(wl_var("afterburner")))
181 val = ABO_OFF;
182 bcom_set_val(skfd, ifname, "afterburner_override", &val, sizeof(val));
183
184 /* Set other options */
185 if (v = nvram_get(wl_var("lazywds"))) {
186 val = atoi(v);
187 bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
188 }
189 if (v = nvram_get(wl_var("frag"))) {
190 val = atoi(v);
191 bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
192 }
193 if (v = nvram_get(wl_var("dtim"))) {
194 val = atoi(v);
195 bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
196 }
197 if (v = nvram_get(wl_var("bcn"))) {
198 val = atoi(v);
199 bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
200 }
201 if (v = nvram_get(wl_var("rts"))) {
202 val = atoi(v);
203 bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
204 }
205 if (v = nvram_get(wl_var("antdiv"))) {
206 val = atoi(v);
207 bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
208 }
209 if (v = nvram_get(wl_var("txant"))) {
210 val = atoi(v);
211 bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
212 }
213
214 val = nvram_enabled(wl_var("closed"));
215 bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));
216
217 val = nvram_enabled(wl_var("ap_isolate"));
218 bcom_set_int(skfd, ifname, "ap_isolate", val);
219
220 val = nvram_enabled(wl_var("frameburst"));
221 bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));
222
223 /* Set up MAC list */
224 if (nvram_match(wl_var("macmode"), "allow"))
225 val = WLC_MACMODE_ALLOW;
226 else if (nvram_match(wl_var("macmode"), "deny"))
227 val = WLC_MACMODE_DENY;
228 else
229 val = WLC_MACMODE_DISABLED;
230
231 if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
232 struct maclist *mac_list;
233 struct ether_addr *addr;
234 char *next;
235
236 memset(buf, 0, 8192);
237 mac_list = (struct maclist *) buf;
238 addr = mac_list->ea;
239
240 foreach(wbuf, v, next) {
241 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
242 mac_list->count++;
243 addr++;
244 }
245 }
246 bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
247 } else {
248 val = WLC_MACMODE_DISABLED;
249 }
250 bcom_ioctl(skfd, ifname, WLC_SET_MACMODE, &val, sizeof(val));
251
252 if (v = nvram_get(wl_var("wds"))) {
253 struct maclist *wdslist = (struct maclist *) buf;
254 struct ether_addr *addr = wdslist->ea;
255 char *next;
256
257 memset(buf, 0, 8192);
258 foreach(wbuf, v, next) {
259 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
260 wdslist->count++;
261 addr++;
262 }
263 }
264 bcom_ioctl(skfd, ifname, WLC_SET_WDSLIST, buf, sizeof(buf));
265 }
266
267 /* Set up G mode */
268 bcom_ioctl(skfd, ifname, WLC_GET_PHYTYPE, &val, sizeof(val));
269 if (val == 2) {
270 int override = WLC_G_PROTECTION_OFF;
271 int control = WLC_G_PROTECTION_CTL_OFF;
272
273 val = atoi(nvram_safe_get(wl_var("gmode")));
274 if (val > 5)
275 val = 1;
276 bcom_ioctl(skfd, ifname, WLC_SET_GMODE, &val, sizeof(val));
277
278 if (nvram_match(wl_var("gmode_protection"), "auto")) {
279 override = WLC_G_PROTECTION_AUTO;
280 control = WLC_G_PROTECTION_CTL_OVERLAP;
281 }
282 if (nvram_enabled(wl_var("gmode_protection"))) {
283 override = WLC_G_PROTECTION_ON;
284 control = WLC_G_PROTECTION_CTL_OVERLAP;
285 }
286 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_CONTROL, &override, sizeof(control));
287 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_OVERRIDE, &override, sizeof(override));
288 }
289 }
290
291 void set_wext_ssid(int skfd, char *ifname)
292 {
293 char *buffer;
294 struct iwreq wrq;
295
296 if (buffer = nvram_get(wl_var("ssid"))) {
297 if (strlen(buffer) > IW_ESSID_MAX_SIZE) {
298 ABORT_ARG_SIZE("Set ESSID", SIOCSIWESSID, IW_ESSID_MAX_SIZE);
299 } else {
300 char essid[IW_ESSID_MAX_SIZE + 1];
301
302 wrq.u.essid.flags = 1;
303 strcpy(essid, buffer);
304 wrq.u.essid.pointer = (caddr_t) essid;
305 wrq.u.essid.length = strlen(essid) + 1;
306 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq, "Set ESSID");
307 }
308 }
309 }
310
311 void start_bcom(int skfd, char *ifname)
312 {
313 int val = 0;
314
315 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
316 return;
317
318 bcom_ioctl(skfd, ifname, WLC_UP, &val, sizeof(val));
319
320 /* Need to re-set SSID after WLC_UP */
321 set_wext_ssid(skfd, ifname);
322 }
323
324 void setup_wext_wep(int skfd, char *ifname)
325 {
326 int i, keylen;
327 struct iwreq wrq;
328 char keystr[5];
329 char *keyval;
330 unsigned char key[IW_ENCODING_TOKEN_MAX];
331
332 strcpy(keystr, "key1");
333 for (i = 1; i <= 4; i++) {
334 if (keyval = nvram_get(wl_var(keystr))) {
335 keylen = iw_in_key(keyval, key);
336
337 if (keylen > 0) {
338 wrq.u.data.length = keylen;
339 wrq.u.data.pointer = (caddr_t) key;
340 wrq.u.data.flags = i;
341 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
342 }
343 }
344 keystr[3]++;
345 }
346
347
348 i = atoi(nvram_safe_get(wl_var("key")));
349 if (i > 0 && i < 4) {
350 wrq.u.data.flags = i | IW_ENCODE_RESTRICTED;
351 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
352 }
353 }
354
355 void setup_wext(int skfd, char *ifname)
356 {
357 char *buffer;
358 struct iwreq wrq;
359
360 /* Set ESSID */
361 set_wext_ssid(skfd, ifname);
362
363 /* Set channel */
364 int channel = atoi(nvram_safe_get(wl_var("channel")));
365
366 wrq.u.freq.m = -1;
367 wrq.u.freq.e = 0;
368 wrq.u.freq.flags = 0;
369
370 if (channel > 0) {
371 wrq.u.freq.flags = IW_FREQ_FIXED;
372 wrq.u.freq.m = channel;
373 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq, "Set Frequency");
374 }
375
376 /* Set operation mode */
377 int ap = 0, infra = 0, wet = 0;
378
379 ap = !nvram_match(wl_var("mode"), "sta");
380 infra = !nvram_disabled(wl_var("infra"));
381 wet = nvram_enabled(wl_var("wet"));
382
383 wrq.u.mode = (!infra ? IW_MODE_ADHOC : (ap ? IW_MODE_MASTER : (wet ? IW_MODE_REPEAT : IW_MODE_INFRA)));
384 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWMODE, &wrq, "Set Mode");
385
386 /* Disable radio if wlX_radio is set and not enabled */
387 wrq.u.txpower.disabled = nvram_disabled(wl_var("radio"));
388
389 wrq.u.txpower.value = -1;
390 wrq.u.txpower.fixed = 1;
391 wrq.u.txpower.flags = IW_TXPOW_MWATT;
392 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWTXPOW, &wrq, "Set Tx Power");
393
394 /* Set up WEP */
395 if (nvram_enabled(wl_var("wep")))
396 setup_wext_wep(skfd, ifname);
397
398 }
399
400
401 static int setup_interfaces(int skfd, char *ifname, char *args[], int count)
402 {
403 struct iwreq wrq;
404 int rc;
405
406 /* Avoid "Unused parameter" warning */
407 args = args; count = count;
408
409 if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
410 return 0;
411
412 setup_bcom(skfd, ifname);
413 setup_wext(skfd, ifname);
414 start_bcom(skfd, ifname);
415 prefix[2]++;
416 }
417
418 int main(int argc, char **argv)
419 {
420 int skfd;
421 if((skfd = iw_sockets_open()) < 0) {
422 perror("socket");
423 exit(-1);
424 }
425
426 prefix = strdup("wl0_");
427 iw_enum_devices(skfd, &setup_interfaces, NULL, 0);
428
429 return 0;
430 }
This page took 0.059573 seconds and 5 git commands to generate.