+ return wds_enabled;
+}
+
+static void set_wext_mode(skfd, ifname)
+{
+ struct iwreq wrq;
+ int ap = 0, infra = 0, wet = 0;
+
+ /* Set operation mode */
+ ap = !nvram_match(wl_var("mode"), "sta") && !nvram_match(wl_var("mode"), "wet");
+ infra = !nvram_disabled(wl_var("infra"));
+ wet = !ap && nvram_match(wl_var("mode"), "wet");
+
+ wrq.u.mode = (!infra ? IW_MODE_ADHOC : (ap ? IW_MODE_MASTER : (wet ? IW_MODE_REPEAT : IW_MODE_INFRA)));
+ IW_SET_EXT_ERR(skfd, ifname, SIOCSIWMODE, &wrq, "Set Mode");
+}
+
+
+void start_watchdog(int skfd, char *ifname)
+{
+ FILE *f;
+ char *v, *next;
+ unsigned char buf[8192], buf2[8192], wbuf[80], *p, *tmp;
+ int wds = 0, i, j, restart_wds;
+ wlc_ssid_t ssid;
+
+ if (fork())
+ return;
+
+ system("kill $(cat /var/run/wifi.pid) 2>&- >&-");
+ f = fopen("/var/run/wifi.pid", "w");
+ fprintf(f, "%d\n", getpid());
+ fclose(f);
+
+ v = nvram_safe_get(wl_var("wds"));
+ memset(buf2, 0, 8192);
+ p = buf2;
+ foreach(wbuf, v, next) {
+ if (ether_atoe(wbuf, p)) {
+ p += 6;
+ wds++;
+ }
+ }
+ v = nvram_safe_get(wl_var("ssid"));
+ ssid.SSID_len = strlen(v);
+ strncpy(ssid.SSID, v, 32);
+
+ for (;;) {
+ sleep(5);
+
+ /* client mode */
+ bcom_ioctl(skfd, ifname, WLC_GET_AP, &i, sizeof(i));
+ if (!i) {
+ i = 0;
+ if (bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf, 6) < 0)
+ i = 1;
+ memcpy(buf + 6, "\x00\x00\x00\x00\x00\x00", 6);
+ if (memcmp(buf, buf + 6, 6) == 0)
+ i = 1;
+
+ memset(buf, 0, 8192);
+ strcpy(buf, "sta_info");
+ bcom_ioctl(skfd, ifname, WLC_GET_BSSID, buf + strlen(buf) + 1, 6);
+ if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
+ i = 1;
+ } else {
+ sta_info_t *sta = (sta_info_t *) (buf + 4);
+ if ((sta->flags & 0x18) != 0x18)
+ i = 1;
+ if (sta->idle > 20)
+ i = 1;
+ }
+
+ if (i)
+ bcom_ioctl(skfd, ifname, WLC_SET_SSID, &ssid, sizeof(ssid));
+ }
+
+
+ /* wds */
+ p = buf2;
+ restart_wds = 0;
+ for (i = 0; i < wds; i++) {
+ memset(buf, 0, 8192);
+ strcpy(buf, "sta_info");
+ memcpy(buf + strlen(buf) + 1, p, 6);
+ if (bcom_ioctl(skfd, ifname, WLC_GET_VAR, buf, 8192) < 0) {
+ } else {
+ sta_info_t *sta = (sta_info_t *) (buf + 4);
+ if (!(sta->flags & 0x40)) {
+ } else {
+ if (sta->idle > 120)
+ restart_wds = 1;
+ }
+ }
+ p += 6;
+ }
+ if (restart_wds)
+ setup_bcom_wds(skfd, ifname);
+ }
+}
+
+static void setup_bcom_vif_sec(int skfd, char *ifname, int vif)
+{
+ int val, wep, wsec, i;
+ char *v;
+
+ wsec = bcom_get_wsec(vif);
+ if (wsec)
+ val = bcom_get_wauth(vif);
+ else
+ val = 0;
+
+ bcom_set_bss_int(skfd, ifname, vif, "wpa_auth", val);
+
+ if (val) {
+ if (WPA_AUTH_PSK | WPA2_AUTH_PSK) {
+ v = nvram_safe_get(wl_var("wpa_psk"));
+ if ((strlen(v) >= 8) && (strlen(v) < 63) && nvram_match(wl_var("mode"), "wet") && (vif == 0)) {
+ /* Enable in-driver WPA supplicant */
+ wsec_pmk_t pmk;
+
+ pmk.key_len = (unsigned short) strlen(v);
+ pmk.flags = WSEC_PASSPHRASE;
+ strcpy(pmk.key, v);
+ bcom_ioctl(skfd, ifname, WLC_SET_WSEC_PMK, &pmk, sizeof(pmk));
+ bcom_set_int(skfd, ifname, "sup_wpa", 1);
+ } else {
+ bcom_set_int(skfd, ifname, "sup_wpa", 0);
+ }
+ }
+ bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 1);
+ bcom_set_bss_int(skfd, ifname, vif, "wsec", wsec);
+ bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
+ } else {
+ bcom_set_bss_int(skfd, ifname, vif, "eap_restrict", 0);
+ if (wep = nvram_enabled(vif_var(vif, "wep"))) {
+ wep = atoi(nvram_safe_get(vif_var(vif, "key")));
+ if ((wep >= 1) && (wep <= 4)) {
+ for (i = 1; i < 4; i++) {
+ wl_wsec_key_t k;
+ char name[5] = "key0";
+ unsigned char *kdata = k.data;
+ unsigned char *kstr;
+
+ bzero(&k, sizeof(k));
+ name[3] += i;
+ kstr = nvram_safe_get(vif_var(vif, name));
+ k.len = strlen(kstr);
+ if ((k.len == 10) || (k.len == 26)) {
+ k.index = i - 1;
+#ifdef DEBUG
+ fprintf(stderr, "Adding WEP key %d to VIF %d: ", i, vif);
+#endif
+ k.len = 0;
+ while (*kstr != 0) {
+ strncpy(name, kstr, 2);
+ name[2] = 0;
+ *kdata = (unsigned char) strtoul(name, NULL, 16);
+#ifdef DEBUG
+ fprintf(stderr, "%02x", *kdata);
+#endif
+ kstr += 2;
+ kdata++;
+ k.len++;
+ }
+#ifdef DEBUG
+ fprintf(stderr, "\n");
+#endif
+ } else {
+ k.len = 0;
+ }
+ if ((k.len > 0) && (i == wep))
+ k.flags = WL_PRIMARY_KEY;
+
+ bcom_set_bss_var(skfd, ifname, vif, "wsec_key", &k, sizeof(k));
+ }
+ wep = 1;
+ bcom_set_bss_int(skfd, ifname, vif, "wsec", WEP_ENABLED);
+ bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 1);
+ bcom_set_bss_int(skfd, ifname, vif, "auth", nvram_enabled(vif_var(vif, "auth")));
+ } else {
+ wep = 0;
+ }
+ }
+ }
+
+ if (!wep && !val) {
+ bcom_set_bss_int(skfd, ifname, vif, "wsec", 0);
+ bcom_set_bss_int(skfd, ifname, vif, "wsec_restrict", 0);
+ }
+
+// bcom_set_bss_int(skfd, ifname, vif, "auth", atoi(nvram_safe_get(vif_var(vif, "auth"))));
+}
+
+static void setup_bcom_vif(int skfd, char *ifname, int vif)
+{
+ int val, wep, wsec, i;
+ char *s, *v;
+ wlc_ssid_t ssid;
+
+ s = nvram_safe_get(vif_var(vif, "ssid"));
+ strncpy(ssid.SSID, s, sizeof(ssid.SSID));
+ ssid.SSID_len = strlen(ssid.SSID);
+ ssid.SSID_len = ((ssid.SSID_len > sizeof(ssid.SSID)) ? sizeof(ssid.SSID) : ssid.SSID_len);
+ bcom_set_bss_var(skfd, ifname, vif, "ssid", &ssid, sizeof(ssid));
+
+ val = nvram_enabled(vif_var(vif, "closed"));
+ bcom_set_bss_int(skfd, ifname, vif, "closednet", val);
+
+ val = nvram_enabled(wl_var("ap_isolate"));
+ bcom_set_bss_int(skfd, ifname, vif, "ap_isolate", val);
+
+}
+
+static void start_bcom_vif(int skfd, char *ifname, int vif)
+{
+ int cfg[2];
+ int i;
+
+ cfg[0] = vif;
+ cfg[1] = 1;
+ for (i = 0; i < ADD_VIF_RETRIES; i++) {
+ if (bcom_set_var(skfd, ifname, "bss" , cfg, sizeof(cfg)) == 0)
+ break;
+ usleep(1000 * 1000);
+ }
+}
+
+static void setup_bcom_common(int skfd, char *ifname)
+{
+ int val = 0, ap;
+ char buf[8192], wbuf[80], *v;
+
+ nvram_set(wl_var("ifname"), ifname);
+
+ /* Set Country */
+ strncpy(buf, nvram_safe_get(wl_var("country_code")), 4);
+ buf[3] = 0;
+ bcom_ioctl(skfd, ifname, WLC_SET_COUNTRY, buf, 4);
+
+ if (v = nvram_get(wl_var("txpwr"))) {