Tomato 1.28
[tomato.git] / release / src / router / rc / wnas.c
blobe294c046e589a10fd955d20b6f9e86dcb4808670
1 /*
3 Copyright 2003, CyberTAN Inc. All Rights Reserved
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
6 the contents of this file may not be disclosed to third parties,
7 copied or duplicated in any form without the prior written
8 permission of CyberTAN Inc.
10 This software should be used as a reference only, and it not
11 intended for production use!
13 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
14 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. CYBERTAN
15 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
16 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
21 Copyright 2005, Broadcom Corporation
22 All Rights Reserved.
24 THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
25 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
26 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
27 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
31 #include "rc.h"
33 #include <sys/sysinfo.h>
34 #include <bcmutils.h>
35 #include <wlutils.h>
38 // ref: http://wiki.openwrt.org/OpenWrtDocs/nas
40 // #define DEBUG_TIMING
42 void start_nas(void)
44 mode_t m;
46 if ((nvram_match("wl_mode", "wet")) || (nvram_match("wl0_radio", "0")) ||
47 (nvram_match("security_mode", "disabled"))) {
48 return;
51 #ifdef DEBUG_TIMING
52 struct sysinfo si;
53 sysinfo(&si);
54 _dprintf("%s: uptime=%ld\n", __FUNCTION__, si.uptime);
55 #else
56 _dprintf("%s\n", __FUNCTION__);
57 #endif
59 m = umask(0077);
60 xstart("nas", "/etc/nas.conf", "/var/run/nas.pid", nvram_match("wl_mode", "sta") ? "wan" : "lan");
61 umask(m);
64 void stop_nas(void)
66 #ifdef DEBUG_TIMING
67 struct sysinfo si;
68 sysinfo(&si);
69 _dprintf("%s: uptime=%ld\n", __FUNCTION__, si.uptime);
70 #else
71 _dprintf("%s\n", __FUNCTION__);
72 #endif
74 killall("nas", SIGTERM);
77 void notify_nas(const char *ifname)
79 #ifdef DEBUG_TIMING
80 struct sysinfo si;
81 sysinfo(&si);
82 _dprintf("%s: ifname=%s uptime=%ld\n", __FUNCTION__, ifname, si.uptime);
83 #else
84 _dprintf("%s: ifname=%s\n", __FUNCTION__, ifname);
85 #endif
87 if (nvram_match("security_mode", "disabled")) return;
89 int i;
91 i = 10;
92 while (pidof("nas") == -1) {
93 _dprintf("waiting for nas i=%d\n", i);
94 if (--i < 0) {
95 syslog(LOG_ERR, "Unable to find nas");
96 break;
98 sleep(1);
100 sleep(5);
102 xstart("nas4not", "lan", ifname, "up", "auto",
103 nvram_safe_get("wl_crypto"), // aes, tkip (aes+tkip ok?)
104 nvram_safe_get("wl_akm"), // psk (only?)
105 nvram_safe_get("wl_wpa_psk"), // shared key
106 nvram_safe_get("wl_ssid") // ssid
113 #if 0 // old stuff for ref
117 void del_wds_wsec(int unit, int which)
119 char name[32];
121 sprintf(name, "wl%d_wds%d", unit, which);
122 nvram_unset(name);
128 xstart("nas",
129 nvram_match("wl_mode", "sta") ? "-S" : "-A",
130 "-H", "34954",
131 "-i", nvram_safe_get("wl_ifname"),
132 "-l", nvram_safe_get("lan_ifname"),
137 // WPA doesn't support shared key removed, handled during config set zzz
138 // if (strstr(nvram_safe_get("security_mode2"), "wpa") != NULL) {
139 // nvram_set("wl_auth", "0");
140 // }
142 // if ((nvram_match("wl_mode", "sta")) && (nvram_match("wl_akm", "psk psk2"))) {
143 // nvram_set("wl_akm", "psk2");
144 // }
146 // convert_wds();
151 static int get_wds_wsec(int unit, int which, char *mac, char *role, char *crypto, char *auth, char *ssid, char *pass)
153 char buf[512];
154 char *next;
156 sprintf(buf, "wl%d_wds%d", unit, which);
157 strlcpy(buf, nvram_safe_get(buf), sizeof(buf));
158 next = buf;
160 strcpy(mac, strsep(&next, ","));
161 if (!next) return 0;
163 strcpy(role, strsep(&next, ","));
164 if (!next) return 0;
166 strcpy(crypto, strsep(&next, ","));
167 if (!next) return 0;
169 strcpy(auth, strsep(&next, ","));
170 if (!next) return 0;
172 strcpy(ssid, strsep(&next, ","));
173 if (!next) return 0;
175 strcpy(pass, next);
176 return 1;
179 static void convert_wds(void)
181 char wds_mac[254];
182 char buf[254];
184 if (nvram_match("wl_wds", "")) { // For Router, accept all WDS link
185 strcpy(wds_mac, "*");
187 else { // For AP, assign remote WDS MAC
188 strlcpy(wds_mac, nvram_safe_get("wl_wds"), sizeof(wds_mac));
192 // For WPA-PSK mode, we want to convert wl_wds_mac to wl0_wds0 ... wl0_wds255
193 if (strstr(nvram_safe_get("security_mode"), "psk")) {
194 char wl_wds[32];
195 int i = 0;
196 int j;
197 char mac[254];
198 char *next;
200 foreach(mac, wds_mac, next) {
201 snprintf(wl_wds, sizeof(wl_wds), "wl0_wds%d", i);
202 snprintf(buf, sizeof(buf), "%s,auto,%s,%s,%s,%s",
203 mac,
204 nvram_safe_get("wl_crypto"),
205 nvram_safe_get("security_mode"),
206 nvram_safe_get("wl_ssid"),
207 nvram_safe_get("wl_wpa_psk"));
209 nvram_set(wl_wds, buf);
210 i++;
213 for (j = i; j < 20; j++)
214 del_wds_wsec(0, j);
219 #if 0
220 void notify_nas(char *ifname)
222 char *argv[] = {"nas4not", "lan", ifname, "up",
223 NULL, /* role */
224 NULL, /* crypto */
225 NULL, /* auth */
226 NULL, /* passphrase */
227 NULL, /* ssid */
228 NULL};
229 char tmp[100], prefix[32];
230 int unit;
231 char remote[ETHER_ADDR_LEN];
232 char ssid[48], pass[80], auth[16], crypto[16], role[8];
233 int i;
235 /* the wireless interface must be configured to run NAS */
236 wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit));
237 sprintf(prefix, "wl%d_", unit);
238 if (nvram_match(strcat_r(prefix, "akm", tmp), "") && nvram_match(strcat_r(prefix, "auth_mode", tmp), "none")) {
239 return;
242 // wait until nas is up and running
243 char s[64];
244 int r;
246 r = 10;
247 while (f_read("/tmp/nas.lan.pid", s, sizeof(s)) <= 0) {
248 if (--r <= 0) {
249 cprintf("notify_nas: unable to find nas");
250 break;
252 sleep(1);
254 sleep(3);
257 /* find WDS link configuration */
258 wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN);
259 for (i = 0; i < 20; ++i) {
260 char mac[ETHER_ADDR_STR_LEN];
261 uint8 ea[ETHER_ADDR_LEN];
263 if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass) &&
264 ether_atoe(mac, ea) && !bcmp(ea, remote, ETHER_ADDR_LEN)) {
265 argv[4] = role;
266 argv[5] = crypto;
267 argv[6] = auth;
268 argv[7] = pass;
269 argv[8] = ssid;
271 cprintf("notify_nas: get_wds_wsec(%d,%d) crypto=%s", unit, i, argv[5]);
272 break;
275 /* did not find WDS link configuration, use wireless' */
276 if (i == 20) {
277 /* role */
278 argv[4] = "auto";
279 /* crypto */
280 argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp));
281 /* auth mode */
282 argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp));
283 /* passphrase */
284 argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp));
285 /* ssid */
286 argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp));
288 cprintf("notify_nas: i==MAX crypto=%s", argv[5]);
291 #if 0
292 char cl[512];
293 cl[0] = 0;
294 for (i = 0; argv[i]; ++i) {
295 strcat(cl, argv[i]);
296 strcat(cl, " ");
298 cprintf("notify_nas: %s", cl);
299 #endif
301 int pid;
302 _eval(argv, ">/dev/console", 0, &pid);
304 #endif
308 #endif // 0