more tristate -> prompt fixes
[openwrt.git] / package / wificonf / wificonf.c
1 /*
2 * Wireless Network Adapter configuration utility
3 *
4 * Copyright (C) 2005 Felix Fietkau <nbd@openwrt.org>
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 #include <signal.h>
24
25 #define ADD_VIF_RETRIES 5
26 // #define DEBUG
27
28 /*------------------------------------------------------------------*/
29 /*
30 * Macro to handle errors when setting WE
31 * Print a nice error message and exit...
32 * We define them as macro so that "return" do the right thing.
33 * The "do {...} while(0)" is a standard trick
34 */
35 #define ERR_SET_EXT(rname, request) \
36 fprintf(stderr, "Error for wireless request \"%s\" (%X) :\n", \
37 rname, request)
38
39 #define ABORT_ARG_NUM(rname, request) \
40 do { \
41 ERR_SET_EXT(rname, request); \
42 fprintf(stderr, " too few arguments.\n"); \
43 } while(0)
44
45 #define ABORT_ARG_TYPE(rname, request, arg) \
46 do { \
47 ERR_SET_EXT(rname, request); \
48 fprintf(stderr, " invalid argument \"%s\".\n", arg); \
49 } while(0)
50
51 #define ABORT_ARG_SIZE(rname, request, max) \
52 do { \
53 ERR_SET_EXT(rname, request); \
54 fprintf(stderr, " argument too big (max %d)\n", max); \
55 } while(0)
56
57 /*------------------------------------------------------------------*/
58 /*
59 * Wrapper to push some Wireless Parameter in the driver
60 * Use standard wrapper and add pretty error message if fail...
61 */
62 #define IW_SET_EXT_ERR(skfd, ifname, request, wrq, rname) \
63 do { \
64 if(iw_set_ext(skfd, ifname, request, wrq) < 0) { \
65 ERR_SET_EXT(rname, request); \
66 fprintf(stderr, " SET failed on device %-1.16s ; %s.\n", \
67 ifname, strerror(errno)); \
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 } } while(0)
82
83 static void set_wext_ssid(int skfd, char *ifname);
84
85 static char *prefix;
86 static char buffer[128];
87 static int wpa_enc = 0;
88
89 static char *wl_var(char *name)
90 {
91 sprintf(buffer, "%s_%s", prefix, name);
92 return buffer;
93 }
94
95 static char *vif_var(int vif, char *name)
96 {
97 if (vif == 0)
98 return wl_var(name);
99
100 sprintf(buffer, "%s.%d_%s", prefix, vif, name);
101 return buffer;
102 }
103
104 static int nvram_enabled(char *name)
105 {
106 return (nvram_match(name, "1") || nvram_match(name, "on") || nvram_match(name, "enabled") || nvram_match(name, "true") || nvram_match(name, "yes") ? 1 : 0);
107 }
108
109 static int nvram_disabled(char *name)
110 {
111 return (nvram_match(name, "0") || nvram_match(name, "off") || nvram_match(name, "disabled") || nvram_match(name, "false") || nvram_match(name, "no") ? 1 : 0);
112 }
113
114
115 /* Quarter dBm units to mW
116 * Table starts at QDBM_OFFSET, so the first entry is mW for qdBm=153
117 * Table is offset so the last entry is largest mW value that fits in
118 * a uint16.
119 */
120
121 #define QDBM_OFFSET 153
122 #define QDBM_TABLE_LEN 40
123
124 /* Smallest mW value that will round up to the first table entry, QDBM_OFFSET.
125 * Value is ( mW(QDBM_OFFSET - 1) + mW(QDBM_OFFSET) ) / 2
126 */
127 #define QDBM_TABLE_LOW_BOUND 6493
128
129 /* Largest mW value that will round down to the last table entry,
130 * QDBM_OFFSET + QDBM_TABLE_LEN-1.
131 * Value is ( mW(QDBM_OFFSET + QDBM_TABLE_LEN - 1) + mW(QDBM_OFFSET + QDBM_TABLE_LEN) ) / 2.
132 */
133 #define QDBM_TABLE_HIGH_BOUND 64938
134
135 static const uint16 nqdBm_to_mW_map[QDBM_TABLE_LEN] = {
136 /* qdBm: +0 +1 +2 +3 +4 +5 +6 +7 */
137 /* 153: */ 6683, 7079, 7499, 7943, 8414, 8913, 9441, 10000,
138 /* 161: */ 10593, 11220, 11885, 12589, 13335, 14125, 14962, 15849,
139 /* 169: */ 16788, 17783, 18836, 19953, 21135, 22387, 23714, 25119,
140 /* 177: */ 26607, 28184, 29854, 31623, 33497, 35481, 37584, 39811,
141 /* 185: */ 42170, 44668, 47315, 50119, 53088, 56234, 59566, 63096
142 };
143
144 unsigned char mw_to_qdbm(uint16 mw)
145 {
146 char qdbm;
147 int offset;
148 uint mw_uint = mw;
149 uint boundary;
150
151 /* handle boundary case */
152 if (mw_uint <= 1)
153 return 0;
154
155 offset = QDBM_OFFSET;
156
157 /* move mw into the range of the table */
158 while (mw_uint < QDBM_TABLE_LOW_BOUND) {
159 mw_uint *= 10;
160 offset -= 40;
161 }
162
163 for (qdbm = 0; qdbm < QDBM_TABLE_LEN-1; qdbm++) {
164 boundary = nqdBm_to_mW_map[qdbm] + (nqdBm_to_mW_map[qdbm+1] - nqdBm_to_mW_map[qdbm])/2;
165 if (mw_uint < boundary) break;
166 }
167
168 qdbm += (unsigned char)offset;
169
170 return(qdbm);
171 }
172
173 static int bcom_ioctl(int skfd, char *ifname, int cmd, void *buf, int len)
174 {
175 struct ifreq ifr;
176 wl_ioctl_t ioc;
177 int ret;
178
179 ioc.cmd = cmd;
180 ioc.buf = buf;
181 ioc.len = len;
182
183 ifr.ifr_data = (caddr_t) &ioc;
184 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
185
186 ret = ioctl(skfd, SIOCDEVPRIVATE, &ifr);
187
188 #ifdef DEBUG
189 if (ret < 0)
190 fprintf(stderr, "IOCTL %d failed: %d\n", cmd, ret);
191 #endif
192
193 return ret;
194 }
195
196 static int bcom_set_var(int skfd, char *ifname, char *var, void *val, int len)
197 {
198 char buf[8192];
199 int ret;
200
201 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
202 return -1;
203
204 bzero(buf, sizeof(buf));
205 strcpy(buf, var);
206 memcpy(&buf[strlen(var) + 1], val, len);
207
208 ret = bcom_ioctl(skfd, ifname, WLC_SET_VAR, buf, sizeof(buf));
209
210 #ifdef DEBUG
211 if (ret < 0)
212 fprintf(stderr, "SET_VAR %s failed: %d\n", var, ret);
213 #endif
214
215 return ret;
216 }
217
218 static int bcom_get_var(int skfd, char *ifname, char *var, void *buf, int len)
219 {
220 int ret;
221
222 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
223 return -1;
224
225 bzero(buf, sizeof(buf));
226 strcpy(buf, var);
227
228 ret = bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, sizeof(buf));
229
230 #ifdef DEBUG
231 if (ret < 0)
232 fprintf(stderr, "GET_VAR %s failed: %d\n", var, ret);
233 #endif
234
235 return ret;
236 }
237
238 static int bcom_set_bss_var(int skfd, char *ifname, int bss, char *var, void *val, int len)
239 {
240 char buf[8192];
241 int i = 0, ret;
242
243 bzero(buf, sizeof(buf));
244 if (strlen(var) + len + 8 > sizeof(buf) || len > sizeof(buf))
245 return -1;
246
247 // "bsscfg:<name>\x00" <bss> <data>
248 i = sprintf(buf, "bsscfg:%s", var);
249 buf[i++] = 0;
250
251 memcpy(buf + i, &bss, sizeof(bss));
252 i += sizeof(bss);
253
254 memcpy(buf + i, val, len);
255 i += len;
256
257 ret = bcom_ioctl(skfd, ifname, WLC_SET_VAR, buf, i);
258
259 #ifdef DEBUG
260 if (ret < 0)
261 fprintf(stderr, "SET_BSS_VAR %s failed: %d\n", var, ret);
262 #endif
263
264 return ret;
265 }
266
267 static int bcom_set_int(int skfd, char *ifname, char *var, int val)
268 {
269 return bcom_set_var(skfd, ifname, var, &val, sizeof(val));
270 }
271
272 static int bcom_set_bss_int(int skfd, char *ifname, int bss, char *var, int val)
273 {
274 return bcom_set_bss_var(skfd, ifname, bss, var, &val, sizeof(val));
275 }
276
277 static int is_new_bcom(int skfd, char *ifname)
278 {
279 char buf[8192];
280
281 bzero(buf, 8192);
282 bcom_ioctl(skfd, ifname, WLC_DUMP, buf, 8192);
283
284 if (strstr(buf, "3.130"))
285 return 1;
286
287 return 0;
288 }
289
290 static int bcom_get_wsec(int vif)
291 {
292 int val;
293
294 if (nvram_match(vif_var(vif, "crypto"), "tkip"))
295 val = TKIP_ENABLED;
296 else if (nvram_match(vif_var(vif, "crypto"), "aes"))
297 val = AES_ENABLED;
298 else if (nvram_match(vif_var(vif, "crypto"), "tkip+aes") || nvram_match(vif_var(vif, "crypto"), "aes+tkip"))
299 val = TKIP_ENABLED | AES_ENABLED;
300 else
301 val = 0;
302
303 return val;
304 }
305
306 static int bcom_get_wauth(int vif)
307 {
308 char *v, *next, var[80];
309 int res = 0;
310
311 if (!(v = nvram_get(vif_var(vif, "akm"))))
312 v = nvram_safe_get(vif_var(vif, "auth_mode"));
313
314 foreach(var, v, next) {
315 if (strcmp(var, "psk") == 0)
316 res |= WPA_AUTH_PSK;
317 else if (strcmp(var, "psk2") == 0)
318 res |= WPA2_AUTH_PSK;
319 else if (strcmp(var, "wpa") == 0)
320 res |= WPA_AUTH_UNSPECIFIED;
321 else if (strcmp(var, "wpa2") == 0)
322 res |= WPA2_AUTH_UNSPECIFIED;
323 }
324
325 return res;
326 }
327
328 static void stop_bcom(int skfd, char *ifname)
329 {
330 int val = 0;
331 wlc_ssid_t ssid;
332
333 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
334 return;
335
336 ssid.SSID_len = 0;
337 ssid.SSID[0] = 0;
338 bcom_ioctl(skfd, ifname, WLC_SET_SSID, &ssid, sizeof(ssid));
339 bcom_ioctl(skfd, ifname, WLC_DOWN, NULL, 0);
340
341 }
342
343 static void start_bcom(int skfd, char *ifname)
344 {
345 int val = 0;
346
347 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
348 return;
349
350 bcom_ioctl(skfd, ifname, WLC_UP, &val, sizeof(val));
351 }
352
353 static int setup_bcom_wds(int skfd, char *ifname)
354 {
355 char buf[8192];
356 char wbuf[80];
357 char *v;
358 int wds_enabled = 0;
359
360 if (v = nvram_get(wl_var("wds"))) {
361 struct maclist *wdslist = (struct maclist *) buf;
362 struct ether_addr *addr = wdslist->ea;
363 char *next;
364
365 memset(buf, 0, 8192);
366 foreach(wbuf, v, next) {
367 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
368 wdslist->count++;
369 addr++;
370 wds_enabled = 1;
371 }
372 }
373 bcom_ioctl(skfd, ifname, WLC_SET_WDSLIST, buf, sizeof(buf));
374 }
375 return wds_enabled;
376 }
377
378 static void set_wext_mode(skfd, ifname)
379 {
380 struct iwreq wrq;
381 int ap = 0, infra = 0, wet = 0;
382
383 /* Set operation mode */
384 ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
385 infra = !nvram_disabled(wl_var("infra"));
386 wet = !ap && nvram_match(wl_var("mode"), "wet");
387
388 wrq.u.mode = (!infra ? IW_MODE_ADHOC : (ap ? IW_MODE_MASTER : (wet ? IW_MODE_REPEAT : IW_MODE_INFRA)));
389 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWMODE, &wrq, "Set Mode");
390 }
391
392
393 void start_watchdog(int skfd, char *ifname)
394 {
395 FILE *f;
396 char *v, *next;
397 unsigned char buf[8192], buf2[8192], wbuf[80], *p, *tmp;
398 int wds = 0, i, j, restart_wds;
399
400 if (fork())
401 return;
402
403 system("kill $(cat /var/run/wifi.pid) 2>&- >&-");
404 f = fopen("/var/run/wifi.pid", "w");
405 fprintf(f, "%d\n", getpid());
406 fclose(f);
407
408 v = nvram_safe_get(wl_var("wds"));
409 memset(buf2, 0, 8192);
410 p = buf2;
411 foreach(wbuf, v, next) {
412 if (ether_atoe(wbuf, p)) {
413 p += 6;
414 wds++;
415 }
416 }
417 v = nvram_safe_get(wl_var("ssid"));
418 ssid.SSID_len = strlen(v);
419 strncpy(ssid.SSID, v, 32);
420
421 for (;;) {
422 sleep(5);
423
424 /* client mode */
425 bcom_ioctl(skfd, ifname, WLC_GET_AP, &i, sizeof(i));
426 if (!i) {
427 i = 0;
428 if (bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf, 6) < 0)
429 i = 1;
430 memcpy(buf + 6, "\x00\x00\x00\x00\x00\x00", 6);
431 if (memcmp(buf, buf + 6, 6) == 0)
432 i = 1;
433
434 memset(buf, 0, 8192);
435 strcpy(buf, "sta_info");
436 bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf + strlen(buf) + 1, 6);
437 if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
438 i = 1;
439 } else {
440 sta_info_t *sta = (sta_info_t *) (buf + 4);
441 if ((sta->flags & 0x18) != 0x18)
442 i = 1;
443 if (sta->idle > 20)
444 i = 1;
445 }
446
447 if (i)
448 bcom_ioctl(skfd, ifname, WLC_SET_SSID, &ssid, sizeof(ssid));
449 }
450
451
452 /* wds */
453 p = buf2;
454 restart_wds = 0;
455 for (i = 0; i < wds; i++) {
456 memset(buf, 0, 8192);
457 strcpy(buf, "sta_info");
458 memcpy(buf + strlen(buf) + 1, p, 6);
459 if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
460 } else {
461 sta_info_t *sta = (sta_info_t *) (buf + 4);
462 if (!(sta->flags & 0x40)) {
463 } else {
464 if (sta->idle > 120)
465 restart_wds = 1;
466 }
467 }
468 p += 6;
469 }
470 if (restart_wds)
471 setup_bcom_wds(skfd, ifname);
472 }
473 }
474
475 static void setup_bcom_vif_sec(int skfd, char *ifname, int vif)
476 {
477 int val, wep, wsec, i;
478 char *v;
479
480 wsec = bcom_get_wsec(vif);
481 if (wsec)
482 val = bcom_get_wauth(vif);
483 else
484 val = 0;
485
486 bcom_set_bss_int(skfd, ifname, vif, "wpa_auth", val);
487
488 if (val) {
489 if (WPA_AUTH_PSK | WPA2_AUTH_PSK) {
490 v = nvram_safe_get(wl_var("wpa_psk"));
491 if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet") && (vif == 0)) {
492 /* Enable in-driver WPA supplicant */
493 wsec_pmk_t pmk;
494
495 pmk.key_len = (unsigned short) strlen(v);
496 pmk.flags = WSEC_PASSPHRASE;
497 strcpy(pmk.key, v);
498 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
499 bcom_set_int(skfd, ifname, "sup_wpa", 1);
500 } else {
501 bcom_set_int(skfd, ifname, "sup_wpa", 0);
502 }
503 }
504 bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 1);
505 bcom_set_bss_int(skfd, ifname, vif, "wsec", wsec);
506 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
507 } else {
508 bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 0);
509 if (wep = nvram_enabled(vif_var(vif, "wep"))) {
510 wep = atoi(nvram_safe_get(vif_var(vif, "key")));
511 if ((wep >= 1) && (wep <= 4)) {
512 for (i = 1; i < 4; i++) {
513 wl_wsec_key_t k;
514 char name[5] = "key0";
515 unsigned char *kdata = k.data;
516 unsigned char *kstr;
517
518 bzero(&k, sizeof(k));
519 name[3] += i;
520 kstr = nvram_safe_get(vif_var(vif, name));
521 k.len = strlen(kstr);
522 if ((k.len == 10) || (k.len == 26)) {
523 k.index = i - 1;
524 #ifdef DEBUG
525 fprintf(stderr, "Adding WEP key %d to VIF %d: ", i, vif);
526 #endif
527 k.len = 0;
528 while (*kstr != 0) {
529 strncpy(name, kstr, 2);
530 name[2] = 0;
531 *kdata = (unsigned char) strtoul(name, NULL, 16);
532 #ifdef DEBUG
533 fprintf(stderr, "%02x", *kdata);
534 #endif
535 kstr += 2;
536 kdata++;
537 k.len++;
538 }
539 #ifdef DEBUG
540 fprintf(stderr, "\n");
541 #endif
542 } else {
543 k.len = 0;
544 }
545 if ((k.len > 0) && (i == wep))
546 k.flags = WL_PRIMARY_KEY;
547
548 bcom_set_bss_var(skfd, ifname, vif, "wsec_key", &k, sizeof(k));
549 }
550 wep = 1;
551 bcom_set_bss_int(skfd, ifname, vif, "wsec", WEP_ENABLED);
552 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
553 bcom_set_bss_int(skfd, ifname, vif, "auth", 1);
554 } else {
555 wep = 0;
556 }
557 }
558 }
559
560 if (!wep && !val) {
561 bcom_set_bss_int(skfd, ifname, vif, "wsec", 0);
562 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 0);
563 }
564
565 // bcom_set_bss_int(skfd, ifname, vif, "auth", atoi(nvram_safe_get(vif_var(vif, "auth"))));
566 }
567
568 static void setup_bcom_vif(int skfd, char *ifname, int vif)
569 {
570 int val, wep, wsec, i;
571 char *s, *v;
572 wlc_ssid_t ssid;
573
574 s = nvram_safe_get(vif_var(vif, "ssid"));
575 strncpy(ssid.SSID, s, sizeof(ssid.SSID));
576 ssid.SSID_len = strlen(ssid.SSID);
577 ssid.SSID_len = ((ssid.SSID_len > sizeof(ssid.SSID)) ? sizeof(ssid.SSID) : ssid.SSID_len);
578 bcom_set_bss_var(skfd, ifname, vif, "ssid", &ssid, sizeof(ssid));
579
580 val = nvram_enabled(vif_var(vif, "closed"));
581 bcom_set_bss_int(skfd, ifname, vif, "closednet", val);
582
583 val = nvram_enabled(wl_var("ap_isolate"));
584 bcom_set_bss_int(skfd, ifname, vif, "ap_isolate", val);
585
586 }
587
588 static void start_bcom_vif(int skfd, char *ifname, int vif)
589 {
590 int cfg[2];
591 int i;
592
593 cfg[0] = vif;
594 cfg[1] = 1;
595 for (i = 0; i < ADD_VIF_RETRIES; i++) {
596 if (bcom_set_var(skfd, ifname, "bss" , cfg, sizeof(cfg)) == 0)
597 break;
598 usleep(1000 * 1000);
599 }
600 }
601
602 static void setup_bcom_common(int skfd, char *ifname)
603 {
604 int val = 0, ap;
605 char buf[8192], wbuf[80], *v;
606
607 nvram_set(wl_var("ifname"), ifname);
608
609 /* Set Country */
610 strncpy(buf, nvram_safe_get(wl_var("country_code")), 4);
611 buf[3] = 0;
612 bcom_ioctl(skfd, ifname, WLC_SET_COUNTRY, buf, 4);
613
614 if (v = nvram_get(wl_var("txpwr"))) {
615 val = atoi(v);
616 val = mw_to_qdbm(val);
617 bcom_set_int(skfd, ifname, "qtxpower", val);
618 }
619
620 /* Set other options */
621 val = nvram_enabled(wl_var("lazywds"));
622 bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
623
624 if (v = nvram_get(wl_var("dtim"))) {
625 val = atoi(v);
626 bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
627 }
628 if (v = nvram_get(wl_var("bcn"))) {
629 val = atoi(v);
630 bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
631 }
632 if (v = nvram_get(wl_var("antdiv"))) {
633 val = atoi(v);
634 bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
635 }
636 if (v = nvram_get(wl_var("txant"))) {
637 val = atoi(v);
638 bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
639 }
640 if (v = nvram_get(wl_var("maxassoc"))) {
641 val = atoi(v);
642 bcom_set_int(skfd, ifname, "maxassoc", val);
643 }
644
645 val = nvram_enabled(wl_var("frameburst"));
646 bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));
647
648 ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
649
650 if (ap)
651 val = setup_bcom_wds(skfd, ifname);
652
653 if ((!ap || val) && is_new_bcom(skfd, ifname))
654 start_watchdog(skfd, ifname);
655
656 /* Set up afterburner, disabled it if WDS is enabled */
657 if (val || nvram_enabled(wl_var("lazywds"))) {
658 val = ABO_OFF;
659 } else {
660 val = ABO_AUTO;
661 if (nvram_enabled(wl_var("afterburner")))
662 val = ABO_ON;
663 if (nvram_disabled(wl_var("afterburner")))
664 val = ABO_OFF;
665 }
666
667 bcom_set_var(skfd, ifname, "afterburner_override", &val, sizeof(val));
668
669 /* Set up MAC list */
670 if (nvram_match(wl_var("macmode"), "allow"))
671 val = WLC_MACMODE_ALLOW;
672 else if (nvram_match(wl_var("macmode"), "deny"))
673 val = WLC_MACMODE_DENY;
674 else
675 val = WLC_MACMODE_DISABLED;
676
677 if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
678 struct maclist *mac_list;
679 struct ether_addr *addr;
680 char *next;
681
682 memset(buf, 0, 8192);
683 mac_list = (struct maclist *) buf;
684 addr = mac_list->ea;
685
686 foreach(wbuf, v, next) {
687 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
688 mac_list->count++;
689 addr++;
690 }
691 }
692 bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
693 } else {
694 val = WLC_MACMODE_DISABLED;
695 }
696 bcom_ioctl(skfd, ifname, WLC_SET_MACMODE, &val, sizeof(val));
697
698 /* Set up G mode */
699 bcom_ioctl(skfd, ifname, WLC_GET_PHYTYPE, &val, sizeof(val));
700 if (val == 2) {
701 int override = WLC_G_PROTECTION_OFF;
702 int control = WLC_G_PROTECTION_CTL_OFF;
703
704 if (v = nvram_get(wl_var("gmode")))
705 val = atoi(v);
706 else
707 val = 1;
708
709 if (val > 5)
710 val = 1;
711
712 bcom_ioctl(skfd, ifname, WLC_SET_GMODE, &val, sizeof(val));
713
714 if (nvram_match(wl_var("gmode_protection"), "auto")) {
715 override = WLC_G_PROTECTION_AUTO;
716 control = WLC_G_PROTECTION_CTL_OVERLAP;
717 }
718 if (nvram_enabled(wl_var("gmode_protection"))) {
719 override = WLC_G_PROTECTION_ON;
720 control = WLC_G_PROTECTION_CTL_OVERLAP;
721 }
722 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_CONTROL, &override, sizeof(control));
723 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_OVERRIDE, &override, sizeof(override));
724
725 if (val = 0) {
726 if (nvram_match(wl_var("plcphdr"), "long"))
727 val = WLC_PLCP_AUTO;
728 else
729 val = WLC_PLCP_SHORT;
730
731 bcom_ioctl(skfd, ifname, WLC_SET_PLCPHDR, &val, sizeof(val));
732 }
733 }
734 }
735
736 static void setup_bcom_new(int skfd, char *ifname)
737 {
738 int val = 0, i;
739 int iface[16], ifaces = 1;
740 int ap, apsta, sta, wet;
741 char *v;
742
743 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
744 return;
745
746 /* Clear all VIFs */
747 iface[0] = 0;
748 for (i = 0; i < 16; i++) {
749 int cfg[2]; /* index, enabled */
750
751 cfg[0] = i;
752 cfg[1] = 0;
753
754 bcom_set_var(skfd, ifname, "bss", cfg, sizeof(cfg));
755
756 if ((i > 0) && nvram_enabled(vif_var(i, "enabled")) && (i == 0 || nvram_get(vif_var(i, "ssid")))) {
757 iface[ifaces] = i;
758 ifaces++;
759 }
760 }
761
762 set_wext_mode(skfd, ifname);
763
764 ap = nvram_match(wl_var("mode"), "ap") || nvram_match(wl_var("mode"), "apsta");
765 apsta = nvram_match(wl_var("mode"), "apsta");
766 sta = nvram_match(wl_var("mode"), "sta");
767
768 bcom_set_int(skfd, ifname, "apsta", apsta);
769 bcom_set_int(skfd, ifname, "mssid", (ifaces > 1));
770
771 for (i = 0; i < (sta ? 0 : ifaces); i++) {
772 #ifdef DEBUG
773 fprintf(stderr, "setup_bcom_vif(%d) start\n", iface[i]);
774 #endif
775 setup_bcom_vif(skfd, ifname, iface[i]);
776 #ifdef DEBUG
777 fprintf(stderr, "setup_bcom_vif(%d) end\n", iface[i]);
778 #endif
779 }
780
781
782 if (v = nvram_get(wl_var("rts"))) {
783 val = atoi(v);
784 bcom_set_int(skfd, ifname, "rtsthresh", val);
785 }
786 if (v = nvram_get(wl_var("frag"))) {
787 val = atoi(v);
788 bcom_set_int(skfd, ifname, "fragthresh", val);
789 }
790
791 val = (nvram_disabled(wl_var("radio")) ? (1 | (1 << 16)) : 0);
792 bcom_ioctl(skfd, ifname, WLC_SET_RADIO, &val, sizeof(val));
793
794 setup_bcom_common(skfd, ifname);
795 start_bcom(skfd, ifname);
796
797 val = atoi(nvram_safe_get(wl_var("channel")));
798 if (val > 0)
799 bcom_ioctl(skfd, ifname, WLC_SET_CHANNEL, &val, sizeof(val));
800
801 val = (ap ? 15 : 0);
802 bcom_ioctl(skfd, ifname, WLC_SET_CS_SCAN_TIMER, &val, sizeof(val));
803
804 for (i = 0; i < (sta ? 0 : ifaces); i++) {
805 setup_bcom_vif_sec(skfd, ifname, iface[i]);
806 }
807
808 for (i = 0; i < (sta ? 0 : ifaces); i++) {
809 start_bcom_vif(skfd, ifname, iface[i]);
810 }
811 }
812
813 static void setup_bcom_old(int skfd, char *ifname)
814 {
815 int val = 0, i;
816 char buf[8192];
817 char wbuf[80];
818 char *v;
819
820 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
821 return;
822
823 setup_bcom_common(skfd, ifname);
824
825 if (v = nvram_get(wl_var("frag"))) {
826 val = atoi(v);
827 bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
828 }
829 if (v = nvram_get(wl_var("rts"))) {
830 val = atoi(v);
831 bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
832 }
833
834 val = nvram_enabled(wl_var("closed"));
835 bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));
836
837 val = nvram_enabled(wl_var("ap_isolate"));
838 bcom_set_int(skfd, ifname, "ap_isolate", val);
839
840 start_bcom(skfd, ifname);
841 set_wext_ssid(skfd, ifname);
842
843 val = bcom_get_wauth(0);
844 bcom_ioctl(skfd, ifname, WLC_SET_WPA_AUTH, &val, sizeof(val));
845
846 if (val & (WPA_AUTH_PSK | WPA2_AUTH_PSK)) {
847 v = nvram_safe_get(wl_var("wpa_psk"));
848 if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet")) {
849 /* Enable in-driver WPA supplicant */
850 wsec_pmk_t pmk;
851
852 pmk.key_len = (unsigned short) strlen(v);
853 pmk.flags = WSEC_PASSPHRASE;
854 strcpy(pmk.key, v);
855 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
856 bcom_set_int(skfd, ifname, "sup_wpa", 1);
857 }
858 }
859 if (val) {
860 val = 1;
861 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
862 val = bcom_get_wsec(0);
863 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
864 } else {
865 val = 0;
866 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
867 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
868 bcom_set_int(skfd, ifname, "sup_wpa", 0);
869 }
870 }
871
872 static void set_wext_ssid(int skfd, char *ifname)
873 {
874 char *buffer;
875 char essid[IW_ESSID_MAX_SIZE + 1];
876 struct iwreq wrq;
877
878 buffer = nvram_get(wl_var("ssid"));
879
880 if (!buffer || (strlen(buffer) > IW_ESSID_MAX_SIZE))
881 buffer = "OpenWrt";
882
883 wrq.u.essid.flags = 1;
884 strcpy(essid, buffer);
885 wrq.u.essid.pointer = (caddr_t) essid;
886 wrq.u.essid.length = strlen(essid) + 1;
887 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq, "Set ESSID");
888 }
889
890 static void setup_wext_wep(int skfd, char *ifname)
891 {
892 int i, keylen;
893 struct iwreq wrq;
894 char keystr[5];
895 char *keyval;
896 unsigned char key[IW_ENCODING_TOKEN_MAX];
897
898 memset(&wrq, 0, sizeof(wrq));
899 strcpy(keystr, "key1");
900 for (i = 1; i <= 4; i++) {
901 if (keyval = nvram_get(wl_var(keystr))) {
902 keylen = iw_in_key(keyval, key);
903
904 if (keylen > 0) {
905 wrq.u.data.length = keylen;
906 wrq.u.data.pointer = (caddr_t) key;
907 wrq.u.data.flags = i;
908 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
909 }
910 }
911 keystr[3]++;
912 }
913
914 memset(&wrq, 0, sizeof(wrq));
915 i = atoi(nvram_safe_get(wl_var("key")));
916 if (i > 0 && i < 4) {
917 wrq.u.data.flags = i | IW_ENCODE_RESTRICTED;
918 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
919 }
920 }
921
922 static void setup_wext(int skfd, char *ifname)
923 {
924 char *buffer;
925 struct iwreq wrq;
926
927 /* Set channel */
928 int channel = atoi(nvram_safe_get(wl_var("channel")));
929
930 wrq.u.freq.m = -1;
931 wrq.u.freq.e = 0;
932 wrq.u.freq.flags = 0;
933
934 if (channel > 0) {
935 wrq.u.freq.flags = IW_FREQ_FIXED;
936 wrq.u.freq.m = channel;
937 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq, "Set Frequency");
938 }
939
940 /* Disable radio if wlX_radio is set and not enabled */
941 wrq.u.txpower.disabled = nvram_disabled(wl_var("radio"));
942
943 wrq.u.txpower.value = -1;
944 wrq.u.txpower.fixed = 1;
945 wrq.u.txpower.flags = IW_TXPOW_DBM;
946 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWTXPOW, &wrq, "Set Tx Power");
947
948 /* Set up WEP */
949 if (nvram_enabled(wl_var("wep")) && !wpa_enc)
950 setup_wext_wep(skfd, ifname);
951
952 /* Set ESSID */
953 set_wext_ssid(skfd, ifname);
954
955 }
956
957 static int setup_interfaces(int skfd, char *ifname, char *args[], int count)
958 {
959 struct iwreq wrq;
960 int rc;
961
962 /* Avoid "Unused parameter" warning */
963 args = args; count = count;
964
965 if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
966 return 0;
967
968 if (strncmp(ifname, "ath", 3) == 0) {
969 set_wext_mode(skfd, ifname);
970 setup_wext(skfd, ifname);
971 } else {
972 if (is_new_bcom(skfd, ifname)) {
973 #ifdef DEBUG
974 fprintf(stderr, "New Broadcom driver detected.\n");
975 #endif
976 stop_bcom(skfd, ifname);
977 #ifdef DEBUG
978 fprintf(stderr, "Setup start.\n");
979 #endif
980 setup_bcom_new(skfd, ifname);
981 #ifdef DEBUG
982 fprintf(stderr, "Setup done.\n");
983 #endif
984 } else {
985 #ifdef DEBUG
986 fprintf(stderr, "Old Broadcom driver detected.\n");
987 #endif
988 stop_bcom(skfd, ifname);
989 set_wext_mode(skfd, ifname);
990 setup_bcom_old(skfd, ifname);
991 setup_wext(skfd, ifname);
992 }
993 }
994
995 prefix[2]++;
996 }
997
998 int main(int argc, char **argv)
999 {
1000 int skfd;
1001 if((skfd = iw_sockets_open()) < 0) {
1002 perror("socket");
1003 exit(-1);
1004 }
1005
1006 prefix = strdup("wl0");
1007 iw_enum_devices(skfd, &setup_interfaces, NULL, 0);
1008
1009 return 0;
1010 }
This page took 0.101896 seconds and 5 git commands to generate.