Merge remote-tracking branch 'remotes/origin/Toastman-RT' into Teaman-RT
[tomato.git] / release / src / router / shared / misc.c
blob6adcbbc861737ed0ddb01ddd5172d9905890a433
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <stdarg.h>
14 #include <syslog.h>
15 #include <sys/ioctl.h>
16 #include <net/if.h>
17 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <ifaddrs.h>
21 #include <sys/sysinfo.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
25 #include <bcmnvram.h>
26 #include <bcmdevs.h>
27 #include <wlutils.h>
29 #include "shutils.h"
30 #include "shared.h"
33 int get_wan_proto(void)
35 const char *names[] = { // order must be synced with def at shared.h
36 "static",
37 "dhcp",
38 "l2tp",
39 "pppoe",
40 "pptp",
41 NULL
43 int i;
44 const char *p;
46 p = nvram_safe_get("wan_proto");
47 for (i = 0; names[i] != NULL; ++i) {
48 if (strcmp(p, names[i]) == 0) return i + 1;
50 return WP_DISABLED;
53 #ifdef TCONFIG_IPV6
54 int get_ipv6_service(void)
56 const char *names[] = { // order must be synced with def at shared.h
57 "native", // IPV6_NATIVE
58 "native-pd", // IPV6_NATIVE_DHCP
59 "6to4", // IPV6_ANYCAST_6TO4
60 "sit", // IPV6_6IN4
61 "other", // IPV6_MANUAL
62 NULL
64 int i;
65 const char *p;
67 p = nvram_safe_get("ipv6_service");
68 for (i = 0; names[i] != NULL; ++i) {
69 if (strcmp(p, names[i]) == 0) return i + 1;
71 return IPV6_DISABLED;
74 const char *ipv6_router_address(struct in6_addr *in6addr)
76 char *p;
77 struct in6_addr addr;
78 static char addr6[INET6_ADDRSTRLEN];
80 addr6[0] = '\0';
82 if ((p = nvram_get("ipv6_rtr_addr")) && *p) {
83 inet_pton(AF_INET6, p, &addr);
85 else if ((p = nvram_get("ipv6_prefix")) && *p) {
86 inet_pton(AF_INET6, p, &addr);
87 addr.s6_addr16[7] = htons(0x0001);
89 else {
90 return addr6;
93 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
94 if (in6addr)
95 memcpy(in6addr, &addr, sizeof(addr));
97 return addr6;
99 #endif
101 int using_dhcpc(void)
103 switch (get_wan_proto()) {
104 case WP_DHCP:
105 return 1;
106 case WP_L2TP:
107 case WP_PPTP:
108 return nvram_get_int("pptp_dhcp");
110 return 0;
113 int wl_client(int unit, int subunit)
115 char *mode = nvram_safe_get(wl_nvname("mode", unit, subunit));
117 return ((strcmp(mode, "sta") == 0) || (strcmp(mode, "wet") == 0));
120 int foreach_wif(int include_vifs, void *param,
121 int (*func)(int idx, int unit, int subunit, void *param))
123 char ifnames[256];
124 char name[64], ifname[64], *next = NULL;
125 int unit = -1, subunit = -1;
126 int i;
127 int ret = 0;
129 snprintf(ifnames, sizeof(ifnames), "%s %s %s %s %s",
130 nvram_safe_get("lan_ifnames"), nvram_safe_get("lan1_ifnames"), nvram_safe_get("lan2_ifnames"), nvram_safe_get("lan3_ifnames"), nvram_safe_get("wan_ifnames"));
131 remove_dups(ifnames, sizeof(ifnames));
133 i = 0;
134 foreach(name, ifnames, next) {
135 if (nvifname_to_osifname(name, ifname, sizeof(ifname)) != 0)
136 continue;
138 if (wl_probe(ifname) || wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)))
139 continue;
141 // Convert eth name to wl name
142 if (osifname_to_nvifname(name, ifname, sizeof(ifname)) != 0)
143 continue;
145 // Slave intefaces have a '.' in the name
146 if (strchr(ifname, '.') && !include_vifs)
147 continue;
149 if (get_ifname_unit(ifname, &unit, &subunit) < 0)
150 continue;
152 ret |= func(i++, unit, subunit, param);
154 return ret;
157 void notice_set(const char *path, const char *format, ...)
159 char p[256];
160 char buf[2048];
161 va_list args;
163 va_start(args, format);
164 vsnprintf(buf, sizeof(buf), format, args);
165 va_end(args);
167 mkdir("/var/notice", 0755);
168 snprintf(p, sizeof(p), "/var/notice/%s", path);
169 f_write_string(p, buf, 0, 0);
170 if (buf[0]) syslog(LOG_INFO, "notice[%s]: %s", path, buf);
174 // #define _x_dprintf(args...) syslog(LOG_DEBUG, args);
175 #define _x_dprintf(args...) do { } while (0);
177 int check_wanup(void)
179 int up = 0;
180 int proto;
181 char buf1[64];
182 char buf2[64];
183 const char *name;
184 int f;
185 struct ifreq ifr;
187 proto = get_wan_proto();
188 if (proto == WP_DISABLED) return 0;
190 if ((proto == WP_PPTP) || (proto == WP_L2TP) || (proto == WP_PPPOE)) {
191 if (f_read_string("/tmp/ppp/link", buf1, sizeof(buf1)) > 0) {
192 // contains the base name of a file in /var/run/ containing pid of a daemon
193 snprintf(buf2, sizeof(buf2), "/var/run/%s.pid", buf1);
194 if (f_read_string(buf2, buf1, sizeof(buf1)) > 0) {
195 name = psname(atoi(buf1), buf2, sizeof(buf2));
196 if (strcmp(name, "pppd") == 0) up = 1;
198 else {
199 _dprintf("%s: error reading %s\n", __FUNCTION__, buf2);
201 if (!up) {
202 unlink("/tmp/ppp/link");
203 _x_dprintf("required daemon not found, assuming link is dead\n");
206 else {
207 _x_dprintf("%s: error reading %s\n", __FUNCTION__, "/tmp/ppp/link");
210 else if (!nvram_match("wan_ipaddr", "0.0.0.0")) {
211 up = 1;
213 else {
214 _x_dprintf("%s: default !up\n", __FUNCTION__);
217 if ((up) && ((f = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)) {
218 strlcpy(ifr.ifr_name, nvram_safe_get("wan_iface"), sizeof(ifr.ifr_name));
219 if (ioctl(f, SIOCGIFFLAGS, &ifr) < 0) {
220 up = 0;
221 _x_dprintf("%s: SIOCGIFFLAGS\n", __FUNCTION__);
223 close(f);
224 if ((ifr.ifr_flags & IFF_UP) == 0) {
225 up = 0;
226 _x_dprintf("%s: !IFF_UP\n", __FUNCTION__);
230 return up;
234 const dns_list_t *get_dns(void)
236 static dns_list_t dns;
237 char s[512];
238 int n;
239 int i, j;
240 struct in_addr ia;
241 char d[7][22];
242 unsigned short port;
243 char *c;
245 dns.count = 0;
247 strlcpy(s, nvram_safe_get("wan_dns"), sizeof(s));
248 if ((nvram_get_int("dns_addget")) || (s[0] == 0)) {
249 n = strlen(s);
250 snprintf(s + n, sizeof(s) - n, " %s", nvram_safe_get("wan_get_dns"));
253 n = sscanf(s, "%21s %21s %21s %21s %21s %21s %21s", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
254 for (i = 0; i < n; ++i) {
255 port = 53;
257 if ((c = strchr(d[i], ':')) != NULL) {
258 *c++ = 0;
259 if (((j = atoi(c)) < 1) || (j > 0xFFFF)) continue;
260 port = j;
263 if (inet_pton(AF_INET, d[i], &ia) > 0) {
264 for (j = dns.count - 1; j >= 0; --j) {
265 if ((dns.dns[j].addr.s_addr == ia.s_addr) && (dns.dns[j].port == port)) break;
267 if (j < 0) {
268 dns.dns[dns.count].port = port;
269 dns.dns[dns.count++].addr.s_addr = ia.s_addr;
270 if (dns.count == 6) break;
275 return &dns;
278 // -----------------------------------------------------------------------------
280 void set_action(int a)
282 int r = 3;
283 while (f_write("/var/lock/action", &a, sizeof(a), 0, 0) != sizeof(a)) {
284 sleep(1);
285 if (--r == 0) return;
287 if (a != ACT_IDLE) sleep(2);
290 int check_action(void)
292 int a;
293 int r = 3;
295 while (f_read("/var/lock/action", &a, sizeof(a)) != sizeof(a)) {
296 sleep(1);
297 if (--r == 0) return ACT_UNKNOWN;
299 return a;
302 int wait_action_idle(int n)
304 while (n-- > 0) {
305 if (check_action() == ACT_IDLE) return 1;
306 sleep(1);
308 return 0;
311 // -----------------------------------------------------------------------------
313 const wanface_list_t *get_wanfaces(void)
315 static wanface_list_t wanfaces;
316 char *ip, *iface;
317 int proto;
319 wanfaces.count = 0;
321 switch ((proto = get_wan_proto())) {
322 case WP_PPTP:
323 case WP_L2TP:
324 while (wanfaces.count < 2) {
325 if (wanfaces.count == 0) {
326 ip = nvram_safe_get("ppp_get_ip");
327 iface = nvram_safe_get("wan_iface");
328 if (!(*iface)) iface = "ppp+";
330 else /* if (wanfaces.count == 1) */ {
331 ip = nvram_safe_get("wan_ipaddr");
332 if ((!(*ip) || strcmp(ip, "0.0.0.0") == 0) && (wanfaces.count > 0))
333 iface = "";
334 else
335 iface = nvram_safe_get("wan_ifname");
337 strlcpy(wanfaces.iface[wanfaces.count].ip, ip, sizeof(wanfaces.iface[0].ip));
338 strlcpy(wanfaces.iface[wanfaces.count].name, iface, IFNAMSIZ);
339 ++wanfaces.count;
341 break;
342 default:
343 ip = (proto == WP_DISABLED) ? "0.0.0.0" : nvram_safe_get("wan_ipaddr");
344 if (proto == WP_PPPOE) {
345 iface = nvram_safe_get("wan_iface");
346 if (!(*iface)) iface = "ppp+";
348 else {
349 iface = nvram_safe_get("wan_ifname");
351 strlcpy(wanfaces.iface[wanfaces.count].ip, ip, sizeof(wanfaces.iface[0].ip));
352 strlcpy(wanfaces.iface[wanfaces.count++].name, iface, IFNAMSIZ);
353 break;
356 return &wanfaces;
359 const char *get_wanface(void)
361 return (*get_wanfaces()).iface[0].name;
364 #ifdef TCONFIG_IPV6
365 const char *get_wan6face(void)
367 switch (get_ipv6_service()) {
368 case IPV6_NATIVE:
369 case IPV6_NATIVE_DHCP:
370 return get_wanface();
371 case IPV6_ANYCAST_6TO4:
372 return "v6to4";
373 case IPV6_6IN4:
374 return "v6in4";
376 return nvram_safe_get("ipv6_ifname");
378 #endif
380 const char *get_wanip(void)
382 if (!check_wanup()) return "0.0.0.0";
384 return (*get_wanfaces()).iface[0].ip;
387 const char *getifaddr(char *ifname, int family, int linklocal)
389 static char buf[INET6_ADDRSTRLEN];
390 void *addr = NULL;
391 struct ifaddrs *ifap, *ifa;
393 if (getifaddrs(&ifap) != 0) {
394 _dprintf("getifaddrs failed: %s\n", strerror(errno));
395 return NULL;
398 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
399 if ((ifa->ifa_addr == NULL) ||
400 (strncmp(ifa->ifa_name, ifname, IFNAMSIZ) != 0) ||
401 (ifa->ifa_addr->sa_family != family))
402 continue;
404 #ifdef TCONFIG_IPV6
405 if (ifa->ifa_addr->sa_family == AF_INET6) {
406 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
407 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ^ linklocal)
408 continue;
409 addr = (void *)&(s6->sin6_addr);
411 else
412 #endif
414 struct sockaddr_in *s = (struct sockaddr_in *)(ifa->ifa_addr);
415 addr = (void *)&(s->sin_addr);
418 if ((addr) && inet_ntop(ifa->ifa_addr->sa_family, addr, buf, sizeof(buf)) != NULL) {
419 freeifaddrs(ifap);
420 return buf;
424 freeifaddrs(ifap);
425 return NULL;
428 // -----------------------------------------------------------------------------
430 long get_uptime(void)
432 struct sysinfo si;
433 sysinfo(&si);
434 return si.uptime;
437 char *wl_nvname(const char *nv, int unit, int subunit)
439 static char tmp[128];
440 char prefix[] = "wlXXXXXXXXXX_";
442 if (unit < 0)
443 strcpy(prefix, "wl_");
444 else if (subunit > 0)
445 snprintf(prefix, sizeof(prefix), "wl%d.%d_", unit, subunit);
446 else
447 snprintf(prefix, sizeof(prefix), "wl%d_", unit);
448 return strcat_r(prefix, nv, tmp);
451 int get_radio(int unit)
453 uint32 n;
455 return (wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_GET_RADIO, &n, sizeof(n)) == 0) &&
456 ((n & WL_RADIO_SW_DISABLE) == 0);
459 void set_radio(int on, int unit)
461 uint32 n;
463 #ifndef WL_BSS_INFO_VERSION
464 #error WL_BSS_INFO_VERSION
465 #endif
467 #if WL_BSS_INFO_VERSION >= 108
468 n = on ? (WL_RADIO_SW_DISABLE << 16) : ((WL_RADIO_SW_DISABLE << 16) | 1);
469 wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n));
470 if (!on) {
471 led(LED_WLAN, 0);
472 led(LED_DIAG, 0);
474 #else
475 n = on ? 0 : WL_RADIO_SW_DISABLE;
476 wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n));
477 if (!on) {
478 led(LED_DIAG, 0);
480 #endif
483 // -----------------------------------------------------------------------------
485 int mtd_getinfo(const char *mtdname, int *part, int *size)
487 FILE *f;
488 char s[256];
489 char t[256];
490 int r;
492 r = 0;
493 if ((strlen(mtdname) < 128) && (strcmp(mtdname, "pmon") != 0)) {
494 sprintf(t, "\"%s\"", mtdname);
495 if ((f = fopen("/proc/mtd", "r")) != NULL) {
496 while (fgets(s, sizeof(s), f) != NULL) {
497 if ((sscanf(s, "mtd%d: %x", part, size) == 2) && (strstr(s, t) != NULL)) {
498 // don't accidentally mess with bl (0)
499 if (*part > 0) r = 1;
500 break;
503 fclose(f);
506 if (!r) {
507 *size = 0;
508 *part = -1;
510 return r;
513 // -----------------------------------------------------------------------------
515 int nvram_get_int(const char *key)
517 return atoi(nvram_safe_get(key));
521 long nvram_xget_long(const char *name, long min, long max, long def)
523 const char *p;
524 char *e;
525 long n;
527 p = nvram_get(name);
528 if ((p != NULL) && (*p != 0)) {
529 n = strtol(p, &e, 0);
530 if ((e != p) && ((*e == 0) || (*e == ' ')) && (n > min) && (n < max)) {
531 return n;
534 return def;
538 int nvram_get_file(const char *key, const char *fname, int max)
540 int n;
541 char *p;
542 char *b;
543 int r;
545 r = 0;
546 p = nvram_safe_get(key);
547 n = strlen(p);
548 if (n <= max) {
549 if ((b = malloc(base64_decoded_len(n) + 128)) != NULL) {
550 n = base64_decode(p, b, n);
551 if (n > 0) r = (f_write(fname, b, n, 0, 0644) == n);
552 free(b);
555 return r;
557 char b[2048];
558 int n;
559 char *p;
561 p = nvram_safe_get(key);
562 n = strlen(p);
563 if (n <= max) {
564 n = base64_decode(p, b, n);
565 if (n > 0) return (f_write(fname, b, n, 0, 0700) == n);
567 return 0;
571 int nvram_set_file(const char *key, const char *fname, int max)
573 char *in;
574 char *out;
575 long len;
576 int n;
577 int r;
579 if ((len = f_size(fname)) > max) return 0;
580 max = (int)len;
581 r = 0;
582 if (f_read_alloc(fname, &in, max) == max) {
583 if ((out = malloc(base64_encoded_len(max) + 128)) != NULL) {
584 n = base64_encode(in, out, max);
585 out[n] = 0;
586 nvram_set(key, out);
587 free(out);
588 r = 1;
590 free(in);
592 return r;
594 char a[2048];
595 char b[4096];
596 int n;
598 if (((n = f_read(fname, &a, sizeof(a))) > 0) && (n <= max)) {
599 n = base64_encode(a, b, n);
600 b[n] = 0;
601 nvram_set(key, b);
602 return 1;
604 return 0;
608 int nvram_contains_word(const char *key, const char *word)
610 return (find_word(nvram_safe_get(key), word) != NULL);
613 int nvram_is_empty(const char *key)
615 char *p;
616 return (((p = nvram_get(key)) == NULL) || (*p == 0));
619 void nvram_commit_x(void)
621 if (!nvram_get_int("debug_nocommit")) nvram_commit();
624 int connect_timeout(int fd, const struct sockaddr *addr, socklen_t len, int timeout)
626 fd_set fds;
627 struct timeval tv;
628 int flags;
629 int n;
630 int r;
632 if (((flags = fcntl(fd, F_GETFL, 0)) < 0) ||
633 (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)) {
634 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__, fd);
635 return -1;
638 if (connect(fd, addr, len) < 0) {
639 // _dprintf("%s: connect %d = <0\n", __FUNCTION__, fd);
641 if (errno != EINPROGRESS) {
642 _dprintf("%s: error in connect %d errno=%d\n", __FUNCTION__, fd, errno);
643 return -1;
646 while (1) {
647 tv.tv_sec = timeout;
648 tv.tv_usec = 0;
649 FD_ZERO(&fds);
650 FD_SET(fd, &fds);
651 r = select(fd + 1, NULL, &fds, NULL, &tv);
652 if (r == 0) {
653 _dprintf("%s: timeout in select %d\n", __FUNCTION__, fd);
654 return -1;
656 else if (r < 0) {
657 if (errno != EINTR) {
658 _dprintf("%s: error in select %d\n", __FUNCTION__, fd);
659 return -1;
661 // loop
663 else {
664 r = 0;
665 n = sizeof(r);
666 if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &r, &n) < 0) || (r != 0)) {
667 _dprintf("%s: error in SO_ERROR %d\n", __FUNCTION__, fd);
668 return -1;
670 break;
675 if (fcntl(fd, F_SETFL, flags) < 0) {
676 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__, fd);
677 return -1;
680 // _dprintf("%s: OK %d\n", __FUNCTION__, fd);
681 return 0;
684 void chld_reap(int sig)
686 while (waitpid(-1, NULL, WNOHANG) > 0) {}
690 int time_ok(void)
692 return time(0) > Y2K;