Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / shared / misc.c
blob3eadc16e161c32dec355c88213c8dacafd9f207e
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 "ppp3g",
42 NULL
44 int i;
45 const char *p;
47 p = nvram_safe_get("wan_proto");
48 for (i = 0; names[i] != NULL; ++i) {
49 if (strcmp(p, names[i]) == 0) return i + 1;
51 return WP_DISABLED;
54 #ifdef TCONFIG_IPV6
55 int get_ipv6_service(void)
57 const char *names[] = { // order must be synced with def at shared.h
58 "native", // IPV6_NATIVE
59 "native-pd", // IPV6_NATIVE_DHCP
60 "6to4", // IPV6_ANYCAST_6TO4
61 "sit", // IPV6_6IN4
62 "other", // IPV6_MANUAL
63 "6rd", // IPV6_6RD
64 "6rd-pd", // IPV6_6RD_DHCP
65 NULL
67 int i;
68 const char *p;
70 p = nvram_safe_get("ipv6_service");
71 for (i = 0; names[i] != NULL; ++i) {
72 if (strcmp(p, names[i]) == 0) return i + 1;
74 return IPV6_DISABLED;
77 const char *ipv6_router_address(struct in6_addr *in6addr)
79 char *p;
80 struct in6_addr addr;
81 static char addr6[INET6_ADDRSTRLEN];
83 addr6[0] = '\0';
85 if ((p = nvram_get("ipv6_rtr_addr")) && *p) {
86 inet_pton(AF_INET6, p, &addr);
88 else if ((p = nvram_get("ipv6_prefix")) && *p) {
89 inet_pton(AF_INET6, p, &addr);
90 addr.s6_addr16[7] = htons(0x0001);
92 else {
93 return addr6;
96 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
97 if (in6addr)
98 memcpy(in6addr, &addr, sizeof(addr));
100 return addr6;
103 int calc_6rd_local_prefix(const struct in6_addr *prefix,
104 int prefix_len, int relay_prefix_len,
105 const struct in_addr *local_ip,
106 struct in6_addr *local_prefix, int *local_prefix_len)
108 // the following code is based on ipv6calc's code
109 uint32_t local_ip_bits, j;
110 int i;
112 if (!prefix || !local_ip || !local_prefix || !local_prefix_len) {
113 return 0;
116 *local_prefix_len = prefix_len + 32 - relay_prefix_len;
117 if (*local_prefix_len > 64) {
118 return 0;
121 local_ip_bits = ntohl(local_ip->s_addr) << relay_prefix_len;
123 for (i=0; i<4; i++) {
124 local_prefix->s6_addr32[i] = prefix->s6_addr32[i];
127 for (j = 0x80000000, i = prefix_len; i < *local_prefix_len; i++, j>>=1)
129 if (local_ip_bits & j)
130 local_prefix->s6_addr[i>>3] |= (0x80 >> (i & 0x7));
133 return 1;
135 #endif
137 int using_dhcpc(void)
139 switch (get_wan_proto()) {
140 case WP_DHCP:
141 return 1;
142 case WP_L2TP:
143 case WP_PPTP:
144 return nvram_get_int("pptp_dhcp");
146 return 0;
149 int wl_client(int unit, int subunit)
151 char *mode = nvram_safe_get(wl_nvname("mode", unit, subunit));
153 return ((strcmp(mode, "sta") == 0) || (strcmp(mode, "wet") == 0));
156 int foreach_wif(int include_vifs, void *param,
157 int (*func)(int idx, int unit, int subunit, void *param))
159 char ifnames[256];
160 char name[64], ifname[64], *next = NULL;
161 int unit = -1, subunit = -1;
162 int i;
163 int ret = 0;
165 snprintf(ifnames, sizeof(ifnames), "%s %s %s %s %s %s %s %s %s %s",
166 nvram_safe_get("lan_ifnames"),
167 nvram_safe_get("lan1_ifnames"),
168 nvram_safe_get("lan2_ifnames"),
169 nvram_safe_get("lan3_ifnames"),
170 nvram_safe_get("wan_ifnames"),
171 nvram_safe_get("wl_ifname"),
172 nvram_safe_get("wl0_ifname"),
173 nvram_safe_get("wl0_vifs"),
174 nvram_safe_get("wl1_ifname"),
175 nvram_safe_get("wl1_vifs"));
176 remove_dups(ifnames, sizeof(ifnames));
177 sort_list(ifnames, sizeof(ifnames));
179 i = 0;
180 foreach(name, ifnames, next) {
181 if (nvifname_to_osifname(name, ifname, sizeof(ifname)) != 0)
182 continue;
184 if (wl_probe(ifname) || wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)))
185 continue;
187 // Convert eth name to wl name
188 if (osifname_to_nvifname(name, ifname, sizeof(ifname)) != 0)
189 continue;
191 // Slave intefaces have a '.' in the name
192 if (strchr(ifname, '.') && !include_vifs)
193 continue;
195 if (get_ifname_unit(ifname, &unit, &subunit) < 0)
196 continue;
198 ret |= func(i++, unit, subunit, param);
200 return ret;
203 void notice_set(const char *path, const char *format, ...)
205 char p[256];
206 char buf[2048];
207 va_list args;
209 va_start(args, format);
210 vsnprintf(buf, sizeof(buf), format, args);
211 va_end(args);
213 mkdir("/var/notice", 0755);
214 snprintf(p, sizeof(p), "/var/notice/%s", path);
215 f_write_string(p, buf, 0, 0);
216 if (buf[0]) syslog(LOG_INFO, "notice[%s]: %s", path, buf);
220 // #define _x_dprintf(args...) syslog(LOG_DEBUG, args);
221 #define _x_dprintf(args...) do { } while (0);
223 int check_wanup(void)
225 int up = 0;
226 int proto;
227 char buf1[64];
228 char buf2[64];
229 const char *name;
230 int f;
231 struct ifreq ifr;
233 proto = get_wan_proto();
234 if (proto == WP_DISABLED)
236 if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" off
237 led(LED_WHITE,LED_OFF);
239 if (nvram_match("boardtype", "0x052b") && nvram_match("boardrev", "0x1204")) { //rt-n15u wan led off
240 led(LED_WHITE,LED_OFF);
242 return 0;
245 if ((proto == WP_PPTP) || (proto == WP_L2TP) || (proto == WP_PPPOE) || (proto == WP_PPP3G)) {
246 if (f_read_string("/tmp/ppp/link", buf1, sizeof(buf1)) > 0) {
247 // contains the base name of a file in /var/run/ containing pid of a daemon
248 snprintf(buf2, sizeof(buf2), "/var/run/%s.pid", buf1);
249 if (f_read_string(buf2, buf1, sizeof(buf1)) > 0) {
250 name = psname(atoi(buf1), buf2, sizeof(buf2));
251 if (strcmp(name, "pppd") == 0) up = 1;
253 else {
254 _dprintf("%s: error reading %s\n", __FUNCTION__, buf2);
256 if (!up) {
257 unlink("/tmp/ppp/link");
258 _x_dprintf("required daemon not found, assuming link is dead\n");
261 else {
262 _x_dprintf("%s: error reading %s\n", __FUNCTION__, "/tmp/ppp/link");
265 else if (!nvram_match("wan_ipaddr", "0.0.0.0")) {
266 up = 1;
268 else {
269 _x_dprintf("%s: default !up\n", __FUNCTION__);
272 if ((up) && ((f = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)) {
273 strlcpy(ifr.ifr_name, nvram_safe_get("wan_iface"), sizeof(ifr.ifr_name));
274 if (ioctl(f, SIOCGIFFLAGS, &ifr) < 0) {
275 up = 0;
276 _x_dprintf("%s: SIOCGIFFLAGS\n", __FUNCTION__);
278 close(f);
279 if ((ifr.ifr_flags & IFF_UP) == 0) {
280 up = 0;
281 _x_dprintf("%s: !IFF_UP\n", __FUNCTION__);
284 if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" on
285 led(LED_WHITE,up);
287 if (nvram_match("boardtype", "0x052b") && nvram_match("boardrev", "0x1204")) { //rt-n15u wan led on
288 led(LED_WHITE,up);
291 return up;
295 const dns_list_t *get_dns(void)
297 static dns_list_t dns;
298 char s[512];
299 int n;
300 int i, j;
301 struct in_addr ia;
302 char d[7][22];
303 unsigned short port;
304 char *c;
306 dns.count = 0;
308 strlcpy(s, nvram_safe_get("wan_dns"), sizeof(s));
309 if ((nvram_get_int("dns_addget")) || (s[0] == 0)) {
310 n = strlen(s);
311 snprintf(s + n, sizeof(s) - n, " %s", nvram_safe_get("wan_get_dns"));
314 n = sscanf(s, "%21s %21s %21s %21s %21s %21s %21s", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
315 for (i = 0; i < n; ++i) {
316 port = 53;
318 if ((c = strchr(d[i], ':')) != NULL) {
319 *c++ = 0;
320 if (((j = atoi(c)) < 1) || (j > 0xFFFF)) continue;
321 port = j;
324 if (inet_pton(AF_INET, d[i], &ia) > 0) {
325 for (j = dns.count - 1; j >= 0; --j) {
326 if ((dns.dns[j].addr.s_addr == ia.s_addr) && (dns.dns[j].port == port)) break;
328 if (j < 0) {
329 dns.dns[dns.count].port = port;
330 dns.dns[dns.count++].addr.s_addr = ia.s_addr;
331 if (dns.count == 6) break;
336 return &dns;
339 // -----------------------------------------------------------------------------
341 void set_action(int a)
343 int r = 3;
344 while (f_write("/var/lock/action", &a, sizeof(a), 0, 0) != sizeof(a)) {
345 sleep(1);
346 if (--r == 0) return;
348 if (a != ACT_IDLE) sleep(2);
351 int check_action(void)
353 int a;
354 int r = 3;
356 while (f_read("/var/lock/action", &a, sizeof(a)) != sizeof(a)) {
357 sleep(1);
358 if (--r == 0) return ACT_UNKNOWN;
360 return a;
363 int wait_action_idle(int n)
365 while (n-- > 0) {
366 if (check_action() == ACT_IDLE) return 1;
367 sleep(1);
369 return 0;
372 // -----------------------------------------------------------------------------
374 const wanface_list_t *get_wanfaces(void)
376 static wanface_list_t wanfaces;
377 char *ip, *iface;
378 int proto;
380 wanfaces.count = 0;
382 switch ((proto = get_wan_proto())) {
383 case WP_PPTP:
384 case WP_L2TP:
385 while (wanfaces.count < 2) {
386 if (wanfaces.count == 0) {
387 ip = nvram_safe_get("ppp_get_ip");
388 iface = nvram_safe_get("wan_iface");
389 if (!(*iface)) iface = "ppp+";
391 else /* if (wanfaces.count == 1) */ {
392 ip = nvram_safe_get("wan_ipaddr");
393 if ((!(*ip) || strcmp(ip, "0.0.0.0") == 0) && (wanfaces.count > 0))
394 iface = "";
395 else
396 iface = nvram_safe_get("wan_ifname");
398 strlcpy(wanfaces.iface[wanfaces.count].ip, ip, sizeof(wanfaces.iface[0].ip));
399 strlcpy(wanfaces.iface[wanfaces.count].name, iface, IFNAMSIZ);
400 ++wanfaces.count;
402 break;
403 default:
404 ip = (proto == WP_DISABLED) ? "0.0.0.0" : nvram_safe_get("wan_ipaddr");
405 if ((proto == WP_PPPOE) || (proto == WP_PPP3G)) {
406 iface = nvram_safe_get("wan_iface");
407 if (!(*iface)) iface = "ppp+";
409 else {
410 iface = nvram_safe_get("wan_ifname");
412 strlcpy(wanfaces.iface[wanfaces.count].ip, ip, sizeof(wanfaces.iface[0].ip));
413 strlcpy(wanfaces.iface[wanfaces.count++].name, iface, IFNAMSIZ);
414 break;
417 return &wanfaces;
420 const char *get_wanface(void)
422 return (*get_wanfaces()).iface[0].name;
425 #ifdef TCONFIG_IPV6
426 const char *get_wan6face(void)
428 switch (get_ipv6_service()) {
429 case IPV6_NATIVE:
430 case IPV6_NATIVE_DHCP:
431 return get_wanface();
432 case IPV6_ANYCAST_6TO4:
433 return "v6to4";
434 case IPV6_6IN4:
435 return "v6in4";
437 return nvram_safe_get("ipv6_ifname");
439 #endif
441 const char *get_wanip(void)
443 if (!check_wanup()) return "0.0.0.0";
445 return (*get_wanfaces()).iface[0].ip;
448 const char *getifaddr(char *ifname, int family, int linklocal)
450 static char buf[INET6_ADDRSTRLEN];
451 void *addr = NULL;
452 struct ifaddrs *ifap, *ifa;
454 if (getifaddrs(&ifap) != 0) {
455 _dprintf("getifaddrs failed: %s\n", strerror(errno));
456 return NULL;
459 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
460 if ((ifa->ifa_addr == NULL) ||
461 (strncmp(ifa->ifa_name, ifname, IFNAMSIZ) != 0) ||
462 (ifa->ifa_addr->sa_family != family))
463 continue;
465 #ifdef TCONFIG_IPV6
466 if (ifa->ifa_addr->sa_family == AF_INET6) {
467 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
468 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ^ linklocal)
469 continue;
470 addr = (void *)&(s6->sin6_addr);
472 else
473 #endif
475 struct sockaddr_in *s = (struct sockaddr_in *)(ifa->ifa_addr);
476 addr = (void *)&(s->sin_addr);
479 if ((addr) && inet_ntop(ifa->ifa_addr->sa_family, addr, buf, sizeof(buf)) != NULL) {
480 freeifaddrs(ifap);
481 return buf;
485 freeifaddrs(ifap);
486 return NULL;
489 // -----------------------------------------------------------------------------
491 long get_uptime(void)
493 struct sysinfo si;
494 sysinfo(&si);
495 return si.uptime;
498 char *wl_nvname(const char *nv, int unit, int subunit)
500 static char tmp[128];
501 char prefix[] = "wlXXXXXXXXXX_";
503 if (unit < 0)
504 strcpy(prefix, "wl_");
505 else if (subunit > 0)
506 snprintf(prefix, sizeof(prefix), "wl%d.%d_", unit, subunit);
507 else
508 snprintf(prefix, sizeof(prefix), "wl%d_", unit);
509 return strcat_r(prefix, nv, tmp);
512 int get_radio(int unit)
514 uint32 n;
516 return (wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_GET_RADIO, &n, sizeof(n)) == 0) &&
517 ((n & WL_RADIO_SW_DISABLE) == 0);
520 void set_radio(int on, int unit)
522 uint32 n;
524 #ifndef WL_BSS_INFO_VERSION
525 #error WL_BSS_INFO_VERSION
526 #endif
528 #if WL_BSS_INFO_VERSION >= 108
529 n = on ? (WL_RADIO_SW_DISABLE << 16) : ((WL_RADIO_SW_DISABLE << 16) | 1);
530 wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n));
531 if (!on) {
532 led(LED_WLAN, 0);
533 led(LED_DIAG, 0);
535 #else
536 n = on ? 0 : WL_RADIO_SW_DISABLE;
537 wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n));
538 if (!on) {
539 led(LED_DIAG, 0);
541 #endif
544 // -----------------------------------------------------------------------------
546 int mtd_getinfo(const char *mtdname, int *part, int *size)
548 FILE *f;
549 char s[256];
550 char t[256];
551 int r;
553 r = 0;
554 if ((strlen(mtdname) < 128) && (strcmp(mtdname, "pmon") != 0)) {
555 sprintf(t, "\"%s\"", mtdname);
556 if ((f = fopen("/proc/mtd", "r")) != NULL) {
557 while (fgets(s, sizeof(s), f) != NULL) {
558 if ((sscanf(s, "mtd%d: %x", part, size) == 2) && (strstr(s, t) != NULL)) {
559 // don't accidentally mess with bl (0)
560 if (*part > 0) r = 1;
561 break;
564 fclose(f);
567 if (!r) {
568 *size = 0;
569 *part = -1;
571 return r;
574 // -----------------------------------------------------------------------------
576 int nvram_get_int(const char *key)
578 return atoi(nvram_safe_get(key));
582 long nvram_xget_long(const char *name, long min, long max, long def)
584 const char *p;
585 char *e;
586 long n;
588 p = nvram_get(name);
589 if ((p != NULL) && (*p != 0)) {
590 n = strtol(p, &e, 0);
591 if ((e != p) && ((*e == 0) || (*e == ' ')) && (n > min) && (n < max)) {
592 return n;
595 return def;
599 int nvram_get_file(const char *key, const char *fname, int max)
601 int n;
602 char *p;
603 char *b;
604 int r;
606 r = 0;
607 p = nvram_safe_get(key);
608 n = strlen(p);
609 if (n <= max) {
610 if ((b = malloc(base64_decoded_len(n) + 128)) != NULL) {
611 n = base64_decode(p, b, n);
612 if (n > 0) r = (f_write(fname, b, n, 0, 0644) == n);
613 free(b);
616 return r;
618 char b[2048];
619 int n;
620 char *p;
622 p = nvram_safe_get(key);
623 n = strlen(p);
624 if (n <= max) {
625 n = base64_decode(p, b, n);
626 if (n > 0) return (f_write(fname, b, n, 0, 0700) == n);
628 return 0;
632 int nvram_set_file(const char *key, const char *fname, int max)
634 char *in;
635 char *out;
636 long len;
637 int n;
638 int r;
640 if ((len = f_size(fname)) > max) return 0;
641 max = (int)len;
642 r = 0;
643 if (f_read_alloc(fname, &in, max) == max) {
644 if ((out = malloc(base64_encoded_len(max) + 128)) != NULL) {
645 n = base64_encode(in, out, max);
646 out[n] = 0;
647 nvram_set(key, out);
648 free(out);
649 r = 1;
651 free(in);
653 return r;
655 char a[2048];
656 char b[4096];
657 int n;
659 if (((n = f_read(fname, &a, sizeof(a))) > 0) && (n <= max)) {
660 n = base64_encode(a, b, n);
661 b[n] = 0;
662 nvram_set(key, b);
663 return 1;
665 return 0;
669 int nvram_contains_word(const char *key, const char *word)
671 return (find_word(nvram_safe_get(key), word) != NULL);
674 int nvram_is_empty(const char *key)
676 char *p;
677 return (((p = nvram_get(key)) == NULL) || (*p == 0));
680 void nvram_commit_x(void)
682 if (!nvram_get_int("debug_nocommit")) nvram_commit();
685 int connect_timeout(int fd, const struct sockaddr *addr, socklen_t len, int timeout)
687 fd_set fds;
688 struct timeval tv;
689 int flags;
690 int n;
691 int r;
693 if (((flags = fcntl(fd, F_GETFL, 0)) < 0) ||
694 (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)) {
695 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__, fd);
696 return -1;
699 if (connect(fd, addr, len) < 0) {
700 // _dprintf("%s: connect %d = <0\n", __FUNCTION__, fd);
702 if (errno != EINPROGRESS) {
703 _dprintf("%s: error in connect %d errno=%d\n", __FUNCTION__, fd, errno);
704 return -1;
707 while (1) {
708 tv.tv_sec = timeout;
709 tv.tv_usec = 0;
710 FD_ZERO(&fds);
711 FD_SET(fd, &fds);
712 r = select(fd + 1, NULL, &fds, NULL, &tv);
713 if (r == 0) {
714 _dprintf("%s: timeout in select %d\n", __FUNCTION__, fd);
715 return -1;
717 else if (r < 0) {
718 if (errno != EINTR) {
719 _dprintf("%s: error in select %d\n", __FUNCTION__, fd);
720 return -1;
722 // loop
724 else {
725 r = 0;
726 n = sizeof(r);
727 if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &r, &n) < 0) || (r != 0)) {
728 _dprintf("%s: error in SO_ERROR %d\n", __FUNCTION__, fd);
729 return -1;
731 break;
736 if (fcntl(fd, F_SETFL, flags) < 0) {
737 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__, fd);
738 return -1;
741 // _dprintf("%s: OK %d\n", __FUNCTION__, fd);
742 return 0;
745 void chld_reap(int sig)
747 while (waitpid(-1, NULL, WNOHANG) > 0) {}
751 int time_ok(void)
753 return time(0) > Y2K;