add libgsm and gsm-utils packages
[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 /* 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, 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
419 for (;;) {
420 sleep(5);
421 if (bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf, 6) < 0)
422 bcom_ioctl(skfd, ifname, WLC_SET_SSID, v, strlen(v));
423 p = buf2;
424 restart_wds = 0;
425 for (i = 0; i < wds; i++) {
426 memset(buf, 0, 8192);
427 strcpy(buf, "sta_info");
428 memcpy(buf + strlen(buf) + 1, p, 6);
429 if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
430 } else {
431 sta_info_t *sta = (sta_info_t *) (buf + 4);
432 if (!(sta->flags & 0x40)) {
433 } else {
434 if (sta->idle > 120)
435 restart_wds = 1;
436 }
437 }
438 p += 6;
439 }
440 if (restart_wds)
441 setup_bcom_wds(skfd, ifname);
442 }
443 }
444
445 static void setup_bcom_vif_sec(int skfd, char *ifname, int vif)
446 {
447 int val, wep, wsec, i;
448 char *v;
449
450 wsec = bcom_get_wsec(vif);
451 if (wsec)
452 val = bcom_get_wauth(vif);
453 else
454 val = 0;
455
456 bcom_set_bss_int(skfd, ifname, vif, "wpa_auth", val);
457
458 if (val) {
459 if (WPA_AUTH_PSK | WPA2_AUTH_PSK) {
460 v = nvram_safe_get(wl_var("wpa_psk"));
461 if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet") && (vif == 0)) {
462 /* Enable in-driver WPA supplicant */
463 wsec_pmk_t pmk;
464
465 pmk.key_len = (unsigned short) strlen(v);
466 pmk.flags = WSEC_PASSPHRASE;
467 strcpy(pmk.key, v);
468 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
469 bcom_set_int(skfd, ifname, "sup_wpa", 1);
470 } else {
471 bcom_set_int(skfd, ifname, "sup_wpa", 0);
472 }
473 }
474 bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 1);
475 bcom_set_bss_int(skfd, ifname, vif, "wsec", wsec);
476 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
477 } else {
478 bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 0);
479 if (wep = nvram_enabled(vif_var(vif, "wep"))) {
480 wep = atoi(nvram_safe_get(vif_var(vif, "key")));
481 if ((wep >= 1) && (wep <= 4)) {
482 for (i = 1; i < 4; i++) {
483 wl_wsec_key_t k;
484 char name[5] = "key0";
485 unsigned char *kdata = k.data;
486 unsigned char *kstr;
487
488 bzero(&k, sizeof(k));
489 name[3] += i;
490 kstr = nvram_safe_get(vif_var(vif, name));
491 k.len = strlen(kstr);
492 if ((k.len == 10) || (k.len == 26)) {
493 k.index = i - 1;
494 #ifdef DEBUG
495 fprintf(stderr, "Adding WEP key %d to VIF %d: ", i, vif);
496 #endif
497 k.len = 0;
498 while (*kstr != 0) {
499 strncpy(name, kstr, 2);
500 name[2] = 0;
501 *kdata = (unsigned char) strtoul(name, NULL, 16);
502 #ifdef DEBUG
503 fprintf(stderr, "%02x", *kdata);
504 #endif
505 kstr += 2;
506 kdata++;
507 k.len++;
508 }
509 #ifdef DEBUG
510 fprintf(stderr, "\n");
511 #endif
512 } else {
513 k.len = 0;
514 }
515 if ((k.len > 0) && (i == wep))
516 k.flags = WL_PRIMARY_KEY;
517
518 bcom_set_bss_var(skfd, ifname, vif, "wsec_key", &k, sizeof(k));
519 }
520 wep = 1;
521 bcom_set_bss_int(skfd, ifname, vif, "wsec", WEP_ENABLED);
522 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
523 bcom_set_bss_int(skfd, ifname, vif, "auth", 1);
524 } else {
525 wep = 0;
526 }
527 }
528 }
529
530 if (!wep && !val) {
531 bcom_set_bss_int(skfd, ifname, vif, "wsec", 0);
532 bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 0);
533 }
534
535 // bcom_set_bss_int(skfd, ifname, vif, "auth", atoi(nvram_safe_get(vif_var(vif, "auth"))));
536 }
537
538 static void setup_bcom_vif(int skfd, char *ifname, int vif)
539 {
540 int val, wep, wsec, i;
541 char *s, *v;
542 wlc_ssid_t ssid;
543
544 s = nvram_safe_get(vif_var(vif, "ssid"));
545 strncpy(ssid.SSID, s, sizeof(ssid.SSID));
546 ssid.SSID_len = strlen(ssid.SSID);
547 ssid.SSID_len = ((ssid.SSID_len > sizeof(ssid.SSID)) ? sizeof(ssid.SSID) : ssid.SSID_len);
548 bcom_set_bss_var(skfd, ifname, vif, "ssid", &ssid, sizeof(ssid));
549
550 val = nvram_enabled(vif_var(vif, "closed"));
551 bcom_set_bss_int(skfd, ifname, vif, "closednet", val);
552
553 val = nvram_enabled(wl_var("ap_isolate"));
554 bcom_set_bss_int(skfd, ifname, vif, "ap_isolate", val);
555
556 }
557
558 static void start_bcom_vif(int skfd, char *ifname, int vif)
559 {
560 int cfg[2];
561 int i;
562
563 cfg[0] = vif;
564 cfg[1] = 1;
565 for (i = 0; i < ADD_VIF_RETRIES; i++) {
566 if (bcom_set_var(skfd, ifname, "bss" , cfg, sizeof(cfg)) == 0)
567 break;
568 usleep(1000 * 1000);
569 }
570 }
571
572 static void setup_bcom_common(int skfd, char *ifname)
573 {
574 int val = 0, ap;
575 char buf[8192], wbuf[80], *v;
576
577 nvram_set(wl_var("ifname"), ifname);
578
579 /* Set Country */
580 strncpy(buf, nvram_safe_get(wl_var("country_code")), 4);
581 buf[3] = 0;
582 bcom_ioctl(skfd, ifname, WLC_SET_COUNTRY, buf, 4);
583
584 if (v = nvram_get(wl_var("txpwr"))) {
585 val = atoi(v);
586 val = mw_to_qdbm(val);
587 bcom_set_int(skfd, ifname, "qtxpower", val);
588 }
589
590 /* Set other options */
591 val = nvram_enabled(wl_var("lazywds"));
592 bcom_ioctl(skfd, ifname, WLC_SET_LAZYWDS, &val, sizeof(val));
593
594 if (v = nvram_get(wl_var("dtim"))) {
595 val = atoi(v);
596 bcom_ioctl(skfd, ifname, WLC_SET_DTIMPRD, &val, sizeof(val));
597 }
598 if (v = nvram_get(wl_var("bcn"))) {
599 val = atoi(v);
600 bcom_ioctl(skfd, ifname, WLC_SET_BCNPRD, &val, sizeof(val));
601 }
602 if (v = nvram_get(wl_var("antdiv"))) {
603 val = atoi(v);
604 bcom_ioctl(skfd, ifname, WLC_SET_ANTDIV, &val, sizeof(val));
605 }
606 if (v = nvram_get(wl_var("txant"))) {
607 val = atoi(v);
608 bcom_ioctl(skfd, ifname, WLC_SET_TXANT, &val, sizeof(val));
609 }
610 if (v = nvram_get(wl_var("maxassoc"))) {
611 val = atoi(v);
612 bcom_set_int(skfd, ifname, "maxassoc", val);
613 }
614
615 val = nvram_enabled(wl_var("frameburst"));
616 bcom_ioctl(skfd, ifname, WLC_SET_FAKEFRAG, &val, sizeof(val));
617
618 ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
619
620 if (ap)
621 val = setup_bcom_wds(skfd, ifname);
622
623 if ((!ap || val) && is_new_bcom(skfd, ifname))
624 start_watchdog(skfd, ifname);
625
626 /* Set up afterburner, disabled it if WDS is enabled */
627 if (val || nvram_enabled(wl_var("lazywds"))) {
628 val = ABO_OFF;
629 } else {
630 val = ABO_AUTO;
631 if (nvram_enabled(wl_var("afterburner")))
632 val = ABO_ON;
633 if (nvram_disabled(wl_var("afterburner")))
634 val = ABO_OFF;
635 }
636
637 bcom_set_var(skfd, ifname, "afterburner_override", &val, sizeof(val));
638
639 /* Set up MAC list */
640 if (nvram_match(wl_var("macmode"), "allow"))
641 val = WLC_MACMODE_ALLOW;
642 else if (nvram_match(wl_var("macmode"), "deny"))
643 val = WLC_MACMODE_DENY;
644 else
645 val = WLC_MACMODE_DISABLED;
646
647 if ((val != WLC_MACMODE_DISABLED) && (v = nvram_get(wl_var("maclist")))) {
648 struct maclist *mac_list;
649 struct ether_addr *addr;
650 char *next;
651
652 memset(buf, 0, 8192);
653 mac_list = (struct maclist *) buf;
654 addr = mac_list->ea;
655
656 foreach(wbuf, v, next) {
657 if (ether_atoe(wbuf, addr->ether_addr_octet)) {
658 mac_list->count++;
659 addr++;
660 }
661 }
662 bcom_ioctl(skfd, ifname, WLC_SET_MACLIST, buf, sizeof(buf));
663 } else {
664 val = WLC_MACMODE_DISABLED;
665 }
666 bcom_ioctl(skfd, ifname, WLC_SET_MACMODE, &val, sizeof(val));
667
668 /* Set up G mode */
669 bcom_ioctl(skfd, ifname, WLC_GET_PHYTYPE, &val, sizeof(val));
670 if (val == 2) {
671 int override = WLC_G_PROTECTION_OFF;
672 int control = WLC_G_PROTECTION_CTL_OFF;
673
674 if (v = nvram_get(wl_var("gmode")))
675 val = atoi(v);
676 else
677 val = 1;
678
679 if (val > 5)
680 val = 1;
681
682 bcom_ioctl(skfd, ifname, WLC_SET_GMODE, &val, sizeof(val));
683
684 if (nvram_match(wl_var("gmode_protection"), "auto")) {
685 override = WLC_G_PROTECTION_AUTO;
686 control = WLC_G_PROTECTION_CTL_OVERLAP;
687 }
688 if (nvram_enabled(wl_var("gmode_protection"))) {
689 override = WLC_G_PROTECTION_ON;
690 control = WLC_G_PROTECTION_CTL_OVERLAP;
691 }
692 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_CONTROL, &override, sizeof(control));
693 bcom_ioctl(skfd, ifname, WLC_SET_GMODE_PROTECTION_OVERRIDE, &override, sizeof(override));
694
695 if (val = 0) {
696 if (nvram_match(wl_var("plcphdr"), "long"))
697 val = WLC_PLCP_AUTO;
698 else
699 val = WLC_PLCP_SHORT;
700
701 bcom_ioctl(skfd, ifname, WLC_SET_PLCPHDR, &val, sizeof(val));
702 }
703 }
704 }
705
706 static void setup_bcom_new(int skfd, char *ifname)
707 {
708 int val = 0, i;
709 int iface[16], ifaces = 1;
710 int ap, apsta, sta, wet;
711 char *v;
712
713 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
714 return;
715
716 /* Clear all VIFs */
717 iface[0] = 0;
718 for (i = 0; i < 16; i++) {
719 int cfg[2]; /* index, enabled */
720
721 cfg[0] = i;
722 cfg[1] = 0;
723
724 bcom_set_var(skfd, ifname, "bss", cfg, sizeof(cfg));
725
726 if ((i > 0) && nvram_enabled(vif_var(i, "enabled")) && (i == 0 || nvram_get(vif_var(i, "ssid")))) {
727 iface[ifaces] = i;
728 ifaces++;
729 }
730 }
731
732 set_wext_mode(skfd, ifname);
733
734 ap = nvram_match(wl_var("mode"), "ap") || nvram_match(wl_var("mode"), "apsta");
735 apsta = nvram_match(wl_var("mode"), "apsta");
736 sta = nvram_match(wl_var("mode"), "sta");
737
738 bcom_set_int(skfd, ifname, "apsta", apsta);
739 bcom_set_int(skfd, ifname, "mssid", (ifaces > 1));
740
741 for (i = 0; i < (sta ? 0 : ifaces); i++) {
742 #ifdef DEBUG
743 fprintf(stderr, "setup_bcom_vif(%d) start\n", iface[i]);
744 #endif
745 setup_bcom_vif(skfd, ifname, iface[i]);
746 #ifdef DEBUG
747 fprintf(stderr, "setup_bcom_vif(%d) end\n", iface[i]);
748 #endif
749 }
750
751
752 if (v = nvram_get(wl_var("rts"))) {
753 val = atoi(v);
754 bcom_set_int(skfd, ifname, "rtsthresh", val);
755 }
756 if (v = nvram_get(wl_var("frag"))) {
757 val = atoi(v);
758 bcom_set_int(skfd, ifname, "fragthresh", val);
759 }
760
761 val = (nvram_disabled(wl_var("radio")) ? (1 | (1 << 16)) : 0);
762 bcom_ioctl(skfd, ifname, WLC_SET_RADIO, &val, sizeof(val));
763
764 setup_bcom_common(skfd, ifname);
765 start_bcom(skfd, ifname);
766
767 val = atoi(nvram_safe_get(wl_var("channel")));
768 if (val > 0)
769 bcom_ioctl(skfd, ifname, WLC_SET_CHANNEL, &val, sizeof(val));
770
771 val = (ap ? 15 : 0);
772 bcom_ioctl(skfd, ifname, WLC_SET_CS_SCAN_TIMER, &val, sizeof(val));
773
774 for (i = 0; i < (sta ? 0 : ifaces); i++) {
775 setup_bcom_vif_sec(skfd, ifname, iface[i]);
776 }
777
778 for (i = 0; i < (sta ? 0 : ifaces); i++) {
779 start_bcom_vif(skfd, ifname, iface[i]);
780 }
781 }
782
783 static void setup_bcom_old(int skfd, char *ifname)
784 {
785 int val = 0, i;
786 char buf[8192];
787 char wbuf[80];
788 char *v;
789
790 if (bcom_ioctl(skfd, ifname, WLC_GET_MAGIC, &val, sizeof(val)) < 0)
791 return;
792
793 setup_bcom_common(skfd, ifname);
794
795 if (v = nvram_get(wl_var("frag"))) {
796 val = atoi(v);
797 bcom_ioctl(skfd, ifname, WLC_SET_FRAG, &val, sizeof(val));
798 }
799 if (v = nvram_get(wl_var("rts"))) {
800 val = atoi(v);
801 bcom_ioctl(skfd, ifname, WLC_SET_RTS, &val, sizeof(val));
802 }
803
804 val = nvram_enabled(wl_var("closed"));
805 bcom_ioctl(skfd, ifname, WLC_SET_CLOSED, &val, sizeof(val));
806
807 val = nvram_enabled(wl_var("ap_isolate"));
808 bcom_set_int(skfd, ifname, "ap_isolate", val);
809
810 start_bcom(skfd, ifname);
811 set_wext_ssid(skfd, ifname);
812
813 val = bcom_get_wauth(0);
814 bcom_ioctl(skfd, ifname, WLC_SET_WPA_AUTH, &val, sizeof(val));
815
816 if (val & (WPA_AUTH_PSK | WPA2_AUTH_PSK)) {
817 v = nvram_safe_get(wl_var("wpa_psk"));
818 if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet")) {
819 /* Enable in-driver WPA supplicant */
820 wsec_pmk_t pmk;
821
822 pmk.key_len = (unsigned short) strlen(v);
823 pmk.flags = WSEC_PASSPHRASE;
824 strcpy(pmk.key, v);
825 bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
826 bcom_set_int(skfd, ifname, "sup_wpa", 1);
827 }
828 }
829 if (val) {
830 val = 1;
831 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
832 val = bcom_get_wsec(0);
833 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
834 } else {
835 val = 0;
836 bcom_ioctl(skfd, ifname, WLC_SET_WSEC, &val, sizeof(val));
837 bcom_ioctl(skfd, ifname, WLC_SET_EAP_RESTRICT, &val, sizeof(val));
838 bcom_set_int(skfd, ifname, "sup_wpa", 0);
839 }
840 }
841
842 static void set_wext_ssid(int skfd, char *ifname)
843 {
844 char *buffer;
845 char essid[IW_ESSID_MAX_SIZE + 1];
846 struct iwreq wrq;
847
848 buffer = nvram_get(wl_var("ssid"));
849
850 if (!buffer || (strlen(buffer) > IW_ESSID_MAX_SIZE))
851 buffer = "OpenWrt";
852
853 wrq.u.essid.flags = 1;
854 strcpy(essid, buffer);
855 wrq.u.essid.pointer = (caddr_t) essid;
856 wrq.u.essid.length = strlen(essid) + 1;
857 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWESSID, &wrq, "Set ESSID");
858 }
859
860 static void setup_wext_wep(int skfd, char *ifname)
861 {
862 int i, keylen;
863 struct iwreq wrq;
864 char keystr[5];
865 char *keyval;
866 unsigned char key[IW_ENCODING_TOKEN_MAX];
867
868 memset(&wrq, 0, sizeof(wrq));
869 strcpy(keystr, "key1");
870 for (i = 1; i <= 4; i++) {
871 if (keyval = nvram_get(wl_var(keystr))) {
872 keylen = iw_in_key(keyval, key);
873
874 if (keylen > 0) {
875 wrq.u.data.length = keylen;
876 wrq.u.data.pointer = (caddr_t) key;
877 wrq.u.data.flags = i;
878 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
879 }
880 }
881 keystr[3]++;
882 }
883
884 memset(&wrq, 0, sizeof(wrq));
885 i = atoi(nvram_safe_get(wl_var("key")));
886 if (i > 0 && i < 4) {
887 wrq.u.data.flags = i | IW_ENCODE_RESTRICTED;
888 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWENCODE, &wrq, "Set Encode");
889 }
890 }
891
892 static void setup_wext(int skfd, char *ifname)
893 {
894 char *buffer;
895 struct iwreq wrq;
896
897 /* Set channel */
898 int channel = atoi(nvram_safe_get(wl_var("channel")));
899
900 wrq.u.freq.m = -1;
901 wrq.u.freq.e = 0;
902 wrq.u.freq.flags = 0;
903
904 if (channel > 0) {
905 wrq.u.freq.flags = IW_FREQ_FIXED;
906 wrq.u.freq.m = channel;
907 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWFREQ, &wrq, "Set Frequency");
908 }
909
910 /* Disable radio if wlX_radio is set and not enabled */
911 wrq.u.txpower.disabled = nvram_disabled(wl_var("radio"));
912
913 wrq.u.txpower.value = -1;
914 wrq.u.txpower.fixed = 1;
915 wrq.u.txpower.flags = IW_TXPOW_DBM;
916 IW_SET_EXT_ERR(skfd, ifname, SIOCSIWTXPOW, &wrq, "Set Tx Power");
917
918 /* Set up WEP */
919 if (nvram_enabled(wl_var("wep")) && !wpa_enc)
920 setup_wext_wep(skfd, ifname);
921
922 /* Set ESSID */
923 set_wext_ssid(skfd, ifname);
924
925 }
926
927 static int setup_interfaces(int skfd, char *ifname, char *args[], int count)
928 {
929 struct iwreq wrq;
930 int rc;
931
932 /* Avoid "Unused parameter" warning */
933 args = args; count = count;
934
935 if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
936 return 0;
937
938 if (strncmp(ifname, "ath", 3) == 0) {
939 set_wext_mode(skfd, ifname);
940 setup_wext(skfd, ifname);
941 } else {
942 if (is_new_bcom(skfd, ifname)) {
943 #ifdef DEBUG
944 fprintf(stderr, "New Broadcom driver detected.\n");
945 #endif
946 stop_bcom(skfd, ifname);
947 #ifdef DEBUG
948 fprintf(stderr, "Setup start.\n");
949 #endif
950 setup_bcom_new(skfd, ifname);
951 #ifdef DEBUG
952 fprintf(stderr, "Setup done.\n");
953 #endif
954 } else {
955 #ifdef DEBUG
956 fprintf(stderr, "Old Broadcom driver detected.\n");
957 #endif
958 stop_bcom(skfd, ifname);
959 set_wext_mode(skfd, ifname);
960 setup_bcom_old(skfd, ifname);
961 setup_wext(skfd, ifname);
962 }
963 }
964
965 prefix[2]++;
966 }
967
968 int main(int argc, char **argv)
969 {
970 int skfd;
971 if((skfd = iw_sockets_open()) < 0) {
972 perror("socket");
973 exit(-1);
974 }
975
976 prefix = strdup("wl0");
977 iw_enum_devices(skfd, &setup_interfaces, NULL, 0);
978
979 return 0;
980 }
This page took 0.097674 seconds and 5 git commands to generate.