f5f43f1b3ecb4e04d7b6b3f3fece92426da10c12
[openwrt.git] / openwrt / 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 static int bcom_ioctl(int skfd, char *ifname, int cmd, void *buf, int len)
116 {
117 struct ifreq ifr;
118 wl_ioctl_t ioc;
119 int ret;
120
121 ioc.cmd = cmd;
122 ioc.buf = buf;
123 ioc.len = len;
124
125 ifr.ifr_data = (caddr_t) &ioc;
126 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
127
128 ret = ioctl(skfd, SIOCDEVPRIVATE, &ifr);
129
130 #ifdef DEBUG
131 if (ret < 0)
132 fprintf(stderr, "IOCTL %d failed: %d\n", cmd, ret);
133 #endif
134
135 return ret;
136 }
137
138 static int bcom_set_var(int skfd, char *ifname, char *var, void *val, int len)
139 {
140 char buf[8192];
141 int ret;
142
143 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
144 return -1;
145
146 bzero(buf, sizeof(buf));
147 strcpy(buf, var);
148 memcpy(&buf[strlen(var) + 1], val, len);
149
150 ret = bcom_ioctl(skfd, ifname, WLC_SET_VAR, buf, sizeof(buf));
151
152 #ifdef DEBUG
153 if (ret < 0)
154 fprintf(stderr, "SET_VAR %s failed: %d\n", var, ret);
155 #endif
156
157 return ret;
158 }
159
160 static int bcom_get_var(int skfd, char *ifname, char *var, void *buf, int len)
161 {
162 int ret;
163
164 if (strlen(var) + 1 > sizeof(buf) || len > sizeof(buf))
165 return -1;
166
167 bzero(buf, sizeof(buf));
168 strcpy(buf, var);
169
170 ret = bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, sizeof(buf));
171
172 #ifdef DEBUG
173 if (ret < 0)
174 fprintf(stderr, "GET_VAR %s failed: %d\n", var, ret);
175 #endif
176
177 return ret;
178 }
179
180 static int bcom_set_bss_var(int skfd, char *ifname, int bss, char *var, void *val, int len)
181 {
182 char buf[8192];
183 int i = 0, ret;
184
185 bzero(buf, sizeof(buf));
186 if (strlen(var) + len + 8 > sizeof(buf) || len > sizeof(buf))
187 return -1;
188
189 // "bsscfg:<name>\x00" <bss> <data>
190 i = sprintf(buf, "bsscfg:%s", var);
191 buf[i++] = 0;
192
193 memcpy(buf + i, &bss, sizeof(bss));
194 i += sizeof(bss);
195
196 memcpy(buf + i, val, len);
197 i += len;
198
199 ret = bcom_ioctl(skfd, ifname, WLC_SET_VAR, buf, i);
200
201 #ifdef DEBUG
202 if (ret < 0)
203 fprintf(stderr, "SET_BSS_VAR %s failed: %d\n", var, ret);
204 #endif
205
206 return ret;
207 }
208
209 static int bcom_set_int(int skfd, char *ifname, char *var, int val)
210 {
211 return bcom_set_var(skfd, ifname, var, &val, sizeof(val));
212 }
213
214 static int bcom_set_bss_int(int skfd, char *ifname, int bss, char *var, int val)
215 {
216 return bcom_set_bss_var(skfd, ifname, bss, var, &val, sizeof(val));
217 }
218
219 static int is_new_bcom(int skfd, char *ifname)
220 {
221 char buf[8192];
222
223 bzero(buf, 8192);
224 bcom_ioctl(skfd, ifname, WLC_DUMP, buf, 8192);
225
226 if (strstr(buf, "3.130"))
227 return 1;
228
229 return 0;
230 }
231
232 static int bcom_get_wsec(int vif)
233 {
234 int val;
235
236 if (nvram_match(vif_var(vif, "crypto"), "tkip"))
237 val = TKIP_ENABLED;
238 else if (nvram_match(vif_var(vif, "crypto"), "aes"))
239 val = AES_ENABLED;
240 else if (nvram_match(vif_var(vif, "crypto"), "tkip+aes") || nvram_match(vif_var(vif, "crypto"), "aes+tkip"))
241 val = TKIP_ENABLED | AES_ENABLED;
242 else
243 val = 0;
244
245 return val;
246 }
247
248 static int bcom_get_wauth(int vif)
249 {
250 char *v, *next, var[80];
251 int res = 0;
252
253 if (!(v = nvram_get(vif_var(vif, "akm"))))
254 v = nvram_safe_get(vif_var(vif, "auth_mode"));
255
256 foreach(var, v, next) {
257 if (strcmp(var, "psk") == 0)
258 res |= WPA_AUTH_PSK;
259 else if (strcmp(var, "psk2") == 0)
260 res |= WPA2_AUTH_PSK;
261 else if (strcmp(var, "wpa") == 0)
262 res |= WPA_AUTH_UNSPECIFIED;
263 else if (strcmp(var, "wpa2") == 0)
264 res |= WPA2_AUTH_UNSPECIFIED;
265 }
266
267 return res;
268 }
269
270 static void stop_bcom(int skfd, char *ifname)
271 {
272 int val = 0;
273 wlc_ssid_t ssid;
274
275 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
276 return;
277
278 ssid.SSID_len = 0;
279 ssid.SSID[0] = 0;
280 bcom_ioctl(skfd, ifname, WLC_SET_SSID, &ssid, sizeof(ssid));
281 bcom_ioctl(skfd, ifname, WLC_DOWN, NULL, 0);
282
283 }
284
285 static void start_bcom(int skfd, char *ifname)
286 {
287 int val = 0;
288
289 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
290 return;
291
292 bcom_ioctl(skfd, ifname, WLC_UP, &val, sizeof(val));
293 }
294
295 static int setup_bcom_wds(int skfd, char *ifname)
296 {
297 char buf[8192];
298 char wbuf[80];
299 char *v;
300 int wds_enabled = 0;
301
302 if (v = nvram_get(wl_var("wds"))) {
303 struct maclist *wdslist = (struct maclist *) buf;
304 struct ether_addr *addr = wdslist->ea;
305 char *next;
306
307 memset(buf, 0, 8192);
308 foreach(wbuf, v, next) {
309 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
310 wdslist->count++;
311 addr++;
312 wds_enabled = 1;
313 }
314 }
315 bcom_ioctl(skfd, ifname, WLC_SET_WDSLIST, buf, sizeof(buf));
316 }
317 return wds_enabled;
318 }
319
320 static void set_wext_mode(skfd, ifname)
321 {
322 struct iwreq wrq;
323 int ap = 0, infra = 0, wet = 0;
324
325 /* Set operation mode */
326 ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
327 infra = !nvram_disabled(wl_var("infra"));
328 wet = !ap && nvram_match(wl_var("mode"), "wet");
329
330 wrq.u.mode = (!infra ? IW_MODE_ADHOC : (ap ? IW_MODE_MASTER : (wet ? IW_MODE_REPEAT : IW_MODE_INFRA)));
331 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWMODE, &wrq, "Set Mode");
332 }
333
334
335 void start_watchdog(int skfd, char *ifname)
336 {
337 FILE *f;
338 char *v, *next;
339 unsigned char buf[8192], buf2[8192], wbuf[80], *p, *tmp;
340 int wds = 0, i, restart_wds;
341
342 if (fork())
343 return;
344
345 system("kill $(cat /var/run/wifi.pid) 2>&- >&-");
346 f = fopen("/var/run/wifi.pid", "w");
347 fprintf(f, "%d\n", getpid());
348 fclose(f);
349
350 v = nvram_safe_get(wl_var("wds"));
351 memset(buf2, 0, 8192);
352 p = buf2;
353 foreach(wbuf, v, next) {
354 if (ether_atoe(wbuf, p)) {
355 p += 6;
356 wds++;
357 }
358 }
359 v = nvram_safe_get(wl_var("ssid"));
360
361 for (;;) {
362 sleep(5);
363 if (bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf, 6) < 0)
364 bcom_ioctl(skfd, ifname, WLC_SET_SSID, v, strlen(v));
365 p = buf2;
366 restart_wds = 0;
367 for (i = 0; i < wds; i++) {
368 memset(buf, 0, 8192);
369 strcpy(buf, "sta_info");
370 memcpy(buf + strlen(buf) + 1, p, 6);
371 if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
372 } else {
373 sta_info_t *sta = (sta_info_t *) (buf + 4);
374 if (!(sta->flags & 0x40)) {
375 } else {
376 if (sta->idle > 120)
377 restart_wds = 1;
378 }
379 }
380 p += 6;
381 }
382 if (restart_wds)
383 setup_bcom_wds(skfd, ifname);
384 }
385 }
386
387 static void setup_bcom_vif_sec(int skfd, char *ifname, int vif)
388 {
389 int val, wep, wsec, i;
390 char *v;
391
392 wsec = bcom_get_wsec(vif);
393 if (wsec)
394 val = bcom_get_wauth(vif);
395 else
396 val = 0;
397
398 bcom_set_bss_int(skfd, ifname, vif, "wpa_auth", val);
399
400 if (val) {
401 if (WPA_AUTH_PSK | WPA2_AUTH_PSK) {
402 v = nvram_safe_get(wl_var("wpa_psk"));
403 if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet") && (vif == 0)) {
404 /* Enable in-driver WPA supplicant */
405 wsec_pmk_t pmk;
406
407 pmk.key_len = (unsigned short) strlen(v);
408 pmk.flags = WSEC_PASSPHRASE;
409 strcpy(pmk.key, v);
410 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
411 bcom_set_int(skfd, ifname, "sup_wpa", 1);
412 } else {
413 bcom_set_int(skfd, ifname, "sup_wpa", 0);
414 }
415 }
416 bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 1);
417 bcom_set_bss_int(skfd, ifname, vif, "wsec", wsec);
418 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
419 } else {
420 bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 0);
421 if (wep = nvram_enabled(vif_var(vif, "wep"))) {
422 wep = atoi(nvram_safe_get(vif_var(vif, "key")));
423 if ((wep >= 1) && (wep <= 4)) {
424 for (i = 1; i < 4; i++) {
425 wl_wsec_key_t k;
426 char name[5] = "key0";
427 unsigned char *kdata = k.data;
428 unsigned char *kstr;
429
430 bzero(&k, sizeof(k));
431 name[3] += i;
432 kstr = nvram_safe_get(vif_var(vif, name));
433 k.len = strlen(kstr);
434 if ((k.len == 10) || (k.len == 26)) {
435 k.index = i - 1;
436 #ifdef DEBUG
437 fprintf(stderr, "Adding WEP key %d to VIF %d: ", i, vif);
438 #endif
439 k.len = 0;
440 while (*kstr != 0) {
441 strncpy(name, kstr, 2);
442 name[2] = 0;
443 *kdata = (unsigned char) strtoul(name, NULL, 16);
444 #ifdef DEBUG
445 fprintf(stderr, "%02x", *kdata);
446 #endif
447 kstr += 2;
448 kdata++;
449 k.len++;
450 }
451 #ifdef DEBUG
452 fprintf(stderr, "\n");
453 #endif
454 } else {
455 k.len = 0;
456 }
457 if ((k.len > 0) && (i == wep))
458 k.flags = WL_PRIMARY_KEY;
459
460 bcom_set_bss_var(skfd, ifname, vif, "wsec_key", &k, sizeof(k));
461 }
462 wep = 1;
463 bcom_set_bss_int(skfd, ifname, vif, "wsec", WEP_ENABLED);
464 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
465 bcom_set_bss_int(skfd, ifname, vif, "auth", 1);
466 } else {
467 wep = 0;
468 }
469 }
470 }
471
472 if (!wep && !val) {
473 bcom_set_bss_int(skfd, ifname, vif, "wsec", 0);
474 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 0);
475 }
476
477 // bcom_set_bss_int(skfd, ifname, vif, "auth", atoi(nvram_safe_get(vif_var(vif, "auth"))));
478 }
479
480 static void setup_bcom_vif(int skfd, char *ifname, int vif)
481 {
482 int val, wep, wsec, i;
483 char *s, *v;
484 wlc_ssid_t ssid;
485
486 s = nvram_safe_get(vif_var(vif, "ssid"));
487 strncpy(ssid.SSID, s, sizeof(ssid.SSID));
488 ssid.SSID_len = strlen(ssid.SSID);
489 ssid.SSID_len = ((ssid.SSID_len > sizeof(ssid.SSID)) ? sizeof(ssid.SSID) : ssid.SSID_len);
490 bcom_set_bss_var(skfd, ifname, vif, "ssid", &ssid, sizeof(ssid));
491
492 val = nvram_enabled(vif_var(vif, "closed"));
493 bcom_set_bss_int(skfd, ifname, vif, "closednet", val);
494
495 val = nvram_enabled(wl_var("ap_isolate"));
496 bcom_set_bss_int(skfd, ifname, vif, "ap_isolate", val);
497
498 }
499
500 static void start_bcom_vif(int skfd, char *ifname, int vif)
501 {
502 int cfg[2];
503 int i;
504
505 cfg[0] = vif;
506 cfg[1] = 1;
507 for (i = 0; i < ADD_VIF_RETRIES; i++) {
508 if (bcom_set_var(skfd, ifname, "bss" , cfg, sizeof(cfg)) == 0)
509 break;
510 usleep(1000 * 1000);
511 }
512 }
513
514 static void setup_bcom_common(int skfd, char *ifname)
515 {
516 int val = 0, ap;
517 char buf[8192], wbuf[80], *v;
518
519 nvram_set(wl_var("ifname"), ifname);
520
521 /* Set Country */
522 strncpy(buf, nvram_safe_get(wl_var("country_code")), 4);
523 buf[3] = 0;
524 bcom_ioctl(skfd, ifname, WLC_SET_COUNTRY, buf, 4);
525
526 /* Set other options */
527 val = nvram_enabled(wl_var("lazywds"));
528 bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
529
530 if (v = nvram_get(wl_var("dtim"))) {
531 val = atoi(v);
532 bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
533 }
534 if (v = nvram_get(wl_var("bcn"))) {
535 val = atoi(v);
536 bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
537 }
538 if (v = nvram_get(wl_var("antdiv"))) {
539 val = atoi(v);
540 bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
541 }
542 if (v = nvram_get(wl_var("txant"))) {
543 val = atoi(v);
544 bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
545 }
546 if (v = nvram_get(wl_var("maxassoc"))) {
547 val = atoi(v);
548 bcom_set_int(skfd, ifname, "maxassoc", val);
549 }
550
551 val = nvram_enabled(wl_var("frameburst"));
552 bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));
553
554 ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
555
556 if (ap)
557 val = setup_bcom_wds(skfd, ifname);
558
559 if ((!ap || val) && is_new_bcom(skfd, ifname))
560 start_watchdog(skfd, ifname);
561
562 /* Set up afterburner, disabled it if WDS is enabled */
563 if (val || nvram_enabled(wl_var("lazywds"))) {
564 val = ABO_OFF;
565 } else {
566 val = ABO_AUTO;
567 if (nvram_enabled(wl_var("afterburner")))
568 val = ABO_ON;
569 if (nvram_disabled(wl_var("afterburner")))
570 val = ABO_OFF;
571 }
572
573 bcom_set_var(skfd, ifname, "afterburner_override", &val, sizeof(val));
574
575 /* Set up MAC list */
576 if (nvram_match(wl_var("macmode"), "allow"))
577 val = WLC_MACMODE_ALLOW;
578 else if (nvram_match(wl_var("macmode"), "deny"))
579 val = WLC_MACMODE_DENY;
580 else
581 val = WLC_MACMODE_DISABLED;
582
583 if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
584 struct maclist *mac_list;
585 struct ether_addr *addr;
586 char *next;
587
588 memset(buf, 0, 8192);
589 mac_list = (struct maclist *) buf;
590 addr = mac_list->ea;
591
592 foreach(wbuf, v, next) {
593 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
594 mac_list->count++;
595 addr++;
596 }
597 }
598 bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
599 } else {
600 val = WLC_MACMODE_DISABLED;
601 }
602 bcom_ioctl(skfd, ifname, WLC_SET_MACMODE, &val, sizeof(val));
603
604 /* Set up G mode */
605 bcom_ioctl(skfd, ifname, WLC_GET_PHYTYPE, &val, sizeof(val));
606 if (val == 2) {
607 int override = WLC_G_PROTECTION_OFF;
608 int control = WLC_G_PROTECTION_CTL_OFF;
609
610 if (v = nvram_get(wl_var("gmode")))
611 val = atoi(v);
612 else
613 val = 1;
614
615 if (val > 5)
616 val = 1;
617
618 bcom_ioctl(skfd, ifname, WLC_SET_GMODE, &val, sizeof(val));
619
620 if (nvram_match(wl_var("gmode_protection"), "auto")) {
621 override = WLC_G_PROTECTION_AUTO;
622 control = WLC_G_PROTECTION_CTL_OVERLAP;
623 }
624 if (nvram_enabled(wl_var("gmode_protection"))) {
625 override = WLC_G_PROTECTION_ON;
626 control = WLC_G_PROTECTION_CTL_OVERLAP;
627 }
628 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_CONTROL, &override, sizeof(control));
629 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_OVERRIDE, &override, sizeof(override));
630
631 if (val = 0) {
632 if (nvram_match(wl_var("plcphdr"), "long"))
633 val = WLC_PLCP_AUTO;
634 else
635 val = WLC_PLCP_SHORT;
636
637 bcom_ioctl(skfd, ifname, WLC_SET_PLCPHDR, &val, sizeof(val));
638 }
639 }
640 }
641
642 static void setup_bcom_new(int skfd, char *ifname)
643 {
644 int val = 0, i;
645 int iface[16], ifaces = 1;
646 int ap, apsta, sta, wet;
647 char *v;
648
649 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
650 return;
651
652 /* Clear all VIFs */
653 iface[0] = 0;
654 for (i = 0; i < 16; i++) {
655 int cfg[2]; /* index, enabled */
656
657 cfg[0] = i;
658 cfg[1] = 0;
659
660 bcom_set_var(skfd, ifname, "bss", cfg, sizeof(cfg));
661
662 if ((i > 0) && nvram_enabled(vif_var(i, "enabled")) && (i == 0 || nvram_get(vif_var(i, "ssid")))) {
663 iface[ifaces] = i;
664 ifaces++;
665 }
666 }
667
668 set_wext_mode(skfd, ifname);
669
670 ap = nvram_match(wl_var("mode"), "ap") || nvram_match(wl_var("mode"), "apsta");
671 apsta = nvram_match(wl_var("mode"), "apsta");
672 sta = nvram_match(wl_var("mode"), "sta");
673
674 bcom_set_int(skfd, ifname, "apsta", apsta);
675 bcom_set_int(skfd, ifname, "mssid", (ifaces > 1));
676
677 for (i = 0; i < (sta ? 0 : ifaces); i++) {
678 #ifdef DEBUG
679 fprintf(stderr, "setup_bcom_vif(%d) start\n", iface[i]);
680 #endif
681 setup_bcom_vif(skfd, ifname, iface[i]);
682 #ifdef DEBUG
683 fprintf(stderr, "setup_bcom_vif(%d) end\n", iface[i]);
684 #endif
685 }
686
687
688 if (v = nvram_get(wl_var("rts"))) {
689 val = atoi(v);
690 bcom_set_int(skfd, ifname, "rtsthresh", val);
691 }
692 if (v = nvram_get(wl_var("frag"))) {
693 val = atoi(v);
694 bcom_set_int(skfd, ifname, "fragthresh", val);
695 }
696
697 val = (nvram_disabled(wl_var("radio")) ? (1 | (1 << 16)) : 0);
698 bcom_ioctl(skfd, ifname, WLC_SET_RADIO, &val, sizeof(val));
699
700 setup_bcom_common(skfd, ifname);
701 start_bcom(skfd, ifname);
702
703 val = atoi(nvram_safe_get(wl_var("channel")));
704 if (val > 0)
705 bcom_ioctl(skfd, ifname, WLC_SET_CHANNEL, &val, sizeof(val));
706
707 val = (ap ? 15 : 0);
708 bcom_ioctl(skfd, ifname, WLC_SET_CS_SCAN_TIMER, &val, sizeof(val));
709
710 for (i = 0; i < (sta ? 0 : ifaces); i++) {
711 setup_bcom_vif_sec(skfd, ifname, iface[i]);
712 }
713
714 for (i = 0; i < (sta ? 0 : ifaces); i++) {
715 start_bcom_vif(skfd, ifname, iface[i]);
716 }
717 }
718
719 static void setup_bcom_old(int skfd, char *ifname)
720 {
721 int val = 0, i;
722 char buf[8192];
723 char wbuf[80];
724 char *v;
725
726 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
727 return;
728
729 setup_bcom_common(skfd, ifname);
730
731 if (v = nvram_get(wl_var("frag"))) {
732 val = atoi(v);
733 bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
734 }
735 if (v = nvram_get(wl_var("rts"))) {
736 val = atoi(v);
737 bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
738 }
739
740 val = nvram_enabled(wl_var("closed"));
741 bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));
742
743 val = nvram_enabled(wl_var("ap_isolate"));
744 bcom_set_int(skfd, ifname, "ap_isolate", val);
745
746 start_bcom(skfd, ifname);
747 set_wext_ssid(skfd, ifname);
748
749 val = bcom_get_wauth(0);
750 bcom_ioctl(skfd, ifname, WLC_SET_WPA_AUTH, &val, sizeof(val));
751
752 if (val & (WPA_AUTH_PSK | WPA2_AUTH_PSK)) {
753 v = nvram_safe_get(wl_var("wpa_psk"));
754 if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet")) {
755 /* Enable in-driver WPA supplicant */
756 wsec_pmk_t pmk;
757
758 pmk.key_len = (unsigned short) strlen(v);
759 pmk.flags = WSEC_PASSPHRASE;
760 strcpy(pmk.key, v);
761 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
762 bcom_set_int(skfd, ifname, "sup_wpa", 1);
763 }
764 }
765 if (val) {
766 val = 1;
767 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
768 val = bcom_get_wsec(0);
769 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
770 } else {
771 val = 0;
772 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
773 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
774 bcom_set_int(skfd, ifname, "sup_wpa", 0);
775 }
776 }
777
778 static void set_wext_ssid(int skfd, char *ifname)
779 {
780 char *buffer;
781 char essid[IW_ESSID_MAX_SIZE + 1];
782 struct iwreq wrq;
783
784 buffer = nvram_get(wl_var("ssid"));
785
786 if (!buffer || (strlen(buffer) > IW_ESSID_MAX_SIZE))
787 buffer = "OpenWrt";
788
789 wrq.u.essid.flags = 1;
790 strcpy(essid, buffer);
791 wrq.u.essid.pointer = (caddr_t) essid;
792 wrq.u.essid.length = strlen(essid) + 1;
793 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq, "Set ESSID");
794 }
795
796 static void setup_wext_wep(int skfd, char *ifname)
797 {
798 int i, keylen;
799 struct iwreq wrq;
800 char keystr[5];
801 char *keyval;
802 unsigned char key[IW_ENCODING_TOKEN_MAX];
803
804 memset(&wrq, 0, sizeof(wrq));
805 strcpy(keystr, "key1");
806 for (i = 1; i <= 4; i++) {
807 if (keyval = nvram_get(wl_var(keystr))) {
808 keylen = iw_in_key(keyval, key);
809
810 if (keylen > 0) {
811 wrq.u.data.length = keylen;
812 wrq.u.data.pointer = (caddr_t) key;
813 wrq.u.data.flags = i;
814 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
815 }
816 }
817 keystr[3]++;
818 }
819
820 memset(&wrq, 0, sizeof(wrq));
821 i = atoi(nvram_safe_get(wl_var("key")));
822 if (i > 0 && i < 4) {
823 wrq.u.data.flags = i | IW_ENCODE_RESTRICTED;
824 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
825 }
826 }
827
828 static void setup_wext(int skfd, char *ifname)
829 {
830 char *buffer;
831 struct iwreq wrq;
832
833 /* Set channel */
834 int channel = atoi(nvram_safe_get(wl_var("channel")));
835
836 wrq.u.freq.m = -1;
837 wrq.u.freq.e = 0;
838 wrq.u.freq.flags = 0;
839
840 if (channel > 0) {
841 wrq.u.freq.flags = IW_FREQ_FIXED;
842 wrq.u.freq.m = channel;
843 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq, "Set Frequency");
844 }
845
846 /* Disable radio if wlX_radio is set and not enabled */
847 wrq.u.txpower.disabled = nvram_disabled(wl_var("radio"));
848
849 wrq.u.txpower.value = -1;
850 wrq.u.txpower.fixed = 1;
851 wrq.u.txpower.flags = IW_TXPOW_DBM;
852 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWTXPOW, &wrq, "Set Tx Power");
853
854 /* Set up WEP */
855 if (nvram_enabled(wl_var("wep")) && !wpa_enc)
856 setup_wext_wep(skfd, ifname);
857
858 /* Set ESSID */
859 set_wext_ssid(skfd, ifname);
860
861 }
862
863 static int setup_interfaces(int skfd, char *ifname, char *args[], int count)
864 {
865 struct iwreq wrq;
866 int rc;
867
868 /* Avoid "Unused parameter" warning */
869 args = args; count = count;
870
871 if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
872 return 0;
873
874 if (strncmp(ifname, "ath", 3) == 0) {
875 set_wext_mode(skfd, ifname);
876 setup_wext(skfd, ifname);
877 } else {
878 if (is_new_bcom(skfd, ifname)) {
879 #ifdef DEBUG
880 fprintf(stderr, "New Broadcom driver detected.\n");
881 #endif
882 stop_bcom(skfd, ifname);
883 #ifdef DEBUG
884 fprintf(stderr, "Setup start.\n");
885 #endif
886 setup_bcom_new(skfd, ifname);
887 #ifdef DEBUG
888 fprintf(stderr, "Setup done.\n");
889 #endif
890 } else {
891 #ifdef DEBUG
892 fprintf(stderr, "Old Broadcom driver detected.\n");
893 #endif
894 stop_bcom(skfd, ifname);
895 set_wext_mode(skfd, ifname);
896 setup_bcom_old(skfd, ifname);
897 setup_wext(skfd, ifname);
898 }
899 }
900
901 prefix[2]++;
902 }
903
904 int main(int argc, char **argv)
905 {
906 int skfd;
907 if((skfd = iw_sockets_open()) < 0) {
908 perror("socket");
909 exit(-1);
910 }
911
912 prefix = strdup("wl0");
913 iw_enum_devices(skfd, &setup_interfaces, NULL, 0);
914
915 return 0;
916 }
This page took 0.145911 seconds and 3 git commands to generate.