Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / shared / misc.c
blobb9983dfb47b2b30f3cbb36869ba7f0eeceec7951
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 return 0;
242 if ((proto == WP_PPTP) || (proto == WP_L2TP) || (proto == WP_PPPOE) || (proto == WP_PPP3G)) {
243 if (f_read_string("/tmp/ppp/link", buf1, sizeof(buf1)) > 0) {
244 // contains the base name of a file in /var/run/ containing pid of a daemon
245 snprintf(buf2, sizeof(buf2), "/var/run/%s.pid", buf1);
246 if (f_read_string(buf2, buf1, sizeof(buf1)) > 0) {
247 name = psname(atoi(buf1), buf2, sizeof(buf2));
248 if (strcmp(name, "pppd") == 0) up = 1;
250 else {
251 _dprintf("%s: error reading %s\n", __FUNCTION__, buf2);
253 if (!up) {
254 unlink("/tmp/ppp/link");
255 _x_dprintf("required daemon not found, assuming link is dead\n");
258 else {
259 _x_dprintf("%s: error reading %s\n", __FUNCTION__, "/tmp/ppp/link");
262 else if (!nvram_match("wan_ipaddr", "0.0.0.0")) {
263 up = 1;
265 else {
266 _x_dprintf("%s: default !up\n", __FUNCTION__);
269 if ((up) && ((f = socket(AF_INET, SOCK_DGRAM, 0)) >= 0)) {
270 strlcpy(ifr.ifr_name, nvram_safe_get("wan_iface"), sizeof(ifr.ifr_name));
271 if (ioctl(f, SIOCGIFFLAGS, &ifr) < 0) {
272 up = 0;
273 _x_dprintf("%s: SIOCGIFFLAGS\n", __FUNCTION__);
275 close(f);
276 if ((ifr.ifr_flags & IFF_UP) == 0) {
277 up = 0;
278 _x_dprintf("%s: !IFF_UP\n", __FUNCTION__);
281 if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" on
282 led(LED_WHITE,up);
285 return up;
289 const dns_list_t *get_dns(void)
291 static dns_list_t dns;
292 char s[512];
293 int n;
294 int i, j;
295 struct in_addr ia;
296 char d[7][22];
297 unsigned short port;
298 char *c;
300 dns.count = 0;
302 strlcpy(s, nvram_safe_get("wan_dns"), sizeof(s));
303 if ((nvram_get_int("dns_addget")) || (s[0] == 0)) {
304 n = strlen(s);
305 snprintf(s + n, sizeof(s) - n, " %s", nvram_safe_get("wan_get_dns"));
308 n = sscanf(s, "%21s %21s %21s %21s %21s %21s %21s", d[0], d[1], d[2], d[3], d[4], d[5], d[6]);
309 for (i = 0; i < n; ++i) {
310 port = 53;
312 if ((c = strchr(d[i], ':')) != NULL) {
313 *c++ = 0;
314 if (((j = atoi(c)) < 1) || (j > 0xFFFF)) continue;
315 port = j;
318 if (inet_pton(AF_INET, d[i], &ia) > 0) {
319 for (j = dns.count - 1; j >= 0; --j) {
320 if ((dns.dns[j].addr.s_addr == ia.s_addr) && (dns.dns[j].port == port)) break;
322 if (j < 0) {
323 dns.dns[dns.count].port = port;
324 dns.dns[dns.count++].addr.s_addr = ia.s_addr;
325 if (dns.count == 6) break;
330 return &dns;
333 // -----------------------------------------------------------------------------
335 void set_action(int a)
337 int r = 3;
338 while (f_write("/var/lock/action", &a, sizeof(a), 0, 0) != sizeof(a)) {
339 sleep(1);
340 if (--r == 0) return;
342 if (a != ACT_IDLE) sleep(2);
345 int check_action(void)
347 int a;
348 int r = 3;
350 while (f_read("/var/lock/action", &a, sizeof(a)) != sizeof(a)) {
351 sleep(1);
352 if (--r == 0) return ACT_UNKNOWN;
354 return a;
357 int wait_action_idle(int n)
359 while (n-- > 0) {
360 if (check_action() == ACT_IDLE) return 1;
361 sleep(1);
363 return 0;
366 // -----------------------------------------------------------------------------
368 const wanface_list_t *get_wanfaces(void)
370 static wanface_list_t wanfaces;
371 char *ip, *iface;
372 int proto;
374 wanfaces.count = 0;
376 switch ((proto = get_wan_proto())) {
377 case WP_PPTP:
378 case WP_L2TP:
379 while (wanfaces.count < 2) {
380 if (wanfaces.count == 0) {
381 ip = nvram_safe_get("ppp_get_ip");
382 iface = nvram_safe_get("wan_iface");
383 if (!(*iface)) iface = "ppp+";
385 else /* if (wanfaces.count == 1) */ {
386 ip = nvram_safe_get("wan_ipaddr");
387 if ((!(*ip) || strcmp(ip, "0.0.0.0") == 0) && (wanfaces.count > 0))
388 iface = "";
389 else
390 iface = nvram_safe_get("wan_ifname");
392 strlcpy(wanfaces.iface[wanfaces.count].ip, ip, sizeof(wanfaces.iface[0].ip));
393 strlcpy(wanfaces.iface[wanfaces.count].name, iface, IFNAMSIZ);
394 ++wanfaces.count;
396 break;
397 default:
398 ip = (proto == WP_DISABLED) ? "0.0.0.0" : nvram_safe_get("wan_ipaddr");
399 if ((proto == WP_PPPOE) || (proto == WP_PPP3G)) {
400 iface = nvram_safe_get("wan_iface");
401 if (!(*iface)) iface = "ppp+";
403 else {
404 iface = nvram_safe_get("wan_ifname");
406 strlcpy(wanfaces.iface[wanfaces.count].ip, ip, sizeof(wanfaces.iface[0].ip));
407 strlcpy(wanfaces.iface[wanfaces.count++].name, iface, IFNAMSIZ);
408 break;
411 return &wanfaces;
414 const char *get_wanface(void)
416 return (*get_wanfaces()).iface[0].name;
419 #ifdef TCONFIG_IPV6
420 const char *get_wan6face(void)
422 switch (get_ipv6_service()) {
423 case IPV6_NATIVE:
424 case IPV6_NATIVE_DHCP:
425 return get_wanface();
426 case IPV6_ANYCAST_6TO4:
427 return "v6to4";
428 case IPV6_6IN4:
429 return "v6in4";
431 return nvram_safe_get("ipv6_ifname");
433 #endif
435 const char *get_wanip(void)
437 if (!check_wanup()) return "0.0.0.0";
439 return (*get_wanfaces()).iface[0].ip;
442 const char *getifaddr(char *ifname, int family, int linklocal)
444 static char buf[INET6_ADDRSTRLEN];
445 void *addr = NULL;
446 struct ifaddrs *ifap, *ifa;
448 if (getifaddrs(&ifap) != 0) {
449 _dprintf("getifaddrs failed: %s\n", strerror(errno));
450 return NULL;
453 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
454 if ((ifa->ifa_addr == NULL) ||
455 (strncmp(ifa->ifa_name, ifname, IFNAMSIZ) != 0) ||
456 (ifa->ifa_addr->sa_family != family))
457 continue;
459 #ifdef TCONFIG_IPV6
460 if (ifa->ifa_addr->sa_family == AF_INET6) {
461 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
462 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) ^ linklocal)
463 continue;
464 addr = (void *)&(s6->sin6_addr);
466 else
467 #endif
469 struct sockaddr_in *s = (struct sockaddr_in *)(ifa->ifa_addr);
470 addr = (void *)&(s->sin_addr);
473 if ((addr) && inet_ntop(ifa->ifa_addr->sa_family, addr, buf, sizeof(buf)) != NULL) {
474 freeifaddrs(ifap);
475 return buf;
479 freeifaddrs(ifap);
480 return NULL;
483 // -----------------------------------------------------------------------------
485 long get_uptime(void)
487 struct sysinfo si;
488 sysinfo(&si);
489 return si.uptime;
492 char *wl_nvname(const char *nv, int unit, int subunit)
494 static char tmp[128];
495 char prefix[] = "wlXXXXXXXXXX_";
497 if (unit < 0)
498 strcpy(prefix, "wl_");
499 else if (subunit > 0)
500 snprintf(prefix, sizeof(prefix), "wl%d.%d_", unit, subunit);
501 else
502 snprintf(prefix, sizeof(prefix), "wl%d_", unit);
503 return strcat_r(prefix, nv, tmp);
506 int get_radio(int unit)
508 uint32 n;
510 return (wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_GET_RADIO, &n, sizeof(n)) == 0) &&
511 ((n & WL_RADIO_SW_DISABLE) == 0);
514 void set_radio(int on, int unit)
516 uint32 n;
518 #ifndef WL_BSS_INFO_VERSION
519 #error WL_BSS_INFO_VERSION
520 #endif
522 #if WL_BSS_INFO_VERSION >= 108
523 n = on ? (WL_RADIO_SW_DISABLE << 16) : ((WL_RADIO_SW_DISABLE << 16) | 1);
524 wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n));
525 if (!on) {
526 led(LED_WLAN, 0);
527 led(LED_DIAG, 0);
529 #else
530 n = on ? 0 : WL_RADIO_SW_DISABLE;
531 wl_ioctl(nvram_safe_get(wl_nvname("ifname", unit, 0)), WLC_SET_RADIO, &n, sizeof(n));
532 if (!on) {
533 led(LED_DIAG, 0);
535 #endif
538 // -----------------------------------------------------------------------------
540 int mtd_getinfo(const char *mtdname, int *part, int *size)
542 FILE *f;
543 char s[256];
544 char t[256];
545 int r;
547 r = 0;
548 if ((strlen(mtdname) < 128) && (strcmp(mtdname, "pmon") != 0)) {
549 sprintf(t, "\"%s\"", mtdname);
550 if ((f = fopen("/proc/mtd", "r")) != NULL) {
551 while (fgets(s, sizeof(s), f) != NULL) {
552 if ((sscanf(s, "mtd%d: %x", part, size) == 2) && (strstr(s, t) != NULL)) {
553 // don't accidentally mess with bl (0)
554 if (*part > 0) r = 1;
555 break;
558 fclose(f);
561 if (!r) {
562 *size = 0;
563 *part = -1;
565 return r;
568 // -----------------------------------------------------------------------------
570 int nvram_get_int(const char *key)
572 return atoi(nvram_safe_get(key));
576 long nvram_xget_long(const char *name, long min, long max, long def)
578 const char *p;
579 char *e;
580 long n;
582 p = nvram_get(name);
583 if ((p != NULL) && (*p != 0)) {
584 n = strtol(p, &e, 0);
585 if ((e != p) && ((*e == 0) || (*e == ' ')) && (n > min) && (n < max)) {
586 return n;
589 return def;
593 int nvram_get_file(const char *key, const char *fname, int max)
595 int n;
596 char *p;
597 char *b;
598 int r;
600 r = 0;
601 p = nvram_safe_get(key);
602 n = strlen(p);
603 if (n <= max) {
604 if ((b = malloc(base64_decoded_len(n) + 128)) != NULL) {
605 n = base64_decode(p, b, n);
606 if (n > 0) r = (f_write(fname, b, n, 0, 0644) == n);
607 free(b);
610 return r;
612 char b[2048];
613 int n;
614 char *p;
616 p = nvram_safe_get(key);
617 n = strlen(p);
618 if (n <= max) {
619 n = base64_decode(p, b, n);
620 if (n > 0) return (f_write(fname, b, n, 0, 0700) == n);
622 return 0;
626 int nvram_set_file(const char *key, const char *fname, int max)
628 char *in;
629 char *out;
630 long len;
631 int n;
632 int r;
634 if ((len = f_size(fname)) > max) return 0;
635 max = (int)len;
636 r = 0;
637 if (f_read_alloc(fname, &in, max) == max) {
638 if ((out = malloc(base64_encoded_len(max) + 128)) != NULL) {
639 n = base64_encode(in, out, max);
640 out[n] = 0;
641 nvram_set(key, out);
642 free(out);
643 r = 1;
645 free(in);
647 return r;
649 char a[2048];
650 char b[4096];
651 int n;
653 if (((n = f_read(fname, &a, sizeof(a))) > 0) && (n <= max)) {
654 n = base64_encode(a, b, n);
655 b[n] = 0;
656 nvram_set(key, b);
657 return 1;
659 return 0;
663 int nvram_contains_word(const char *key, const char *word)
665 return (find_word(nvram_safe_get(key), word) != NULL);
668 int nvram_is_empty(const char *key)
670 char *p;
671 return (((p = nvram_get(key)) == NULL) || (*p == 0));
674 void nvram_commit_x(void)
676 if (!nvram_get_int("debug_nocommit")) nvram_commit();
679 int connect_timeout(int fd, const struct sockaddr *addr, socklen_t len, int timeout)
681 fd_set fds;
682 struct timeval tv;
683 int flags;
684 int n;
685 int r;
687 if (((flags = fcntl(fd, F_GETFL, 0)) < 0) ||
688 (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)) {
689 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__, fd);
690 return -1;
693 if (connect(fd, addr, len) < 0) {
694 // _dprintf("%s: connect %d = <0\n", __FUNCTION__, fd);
696 if (errno != EINPROGRESS) {
697 _dprintf("%s: error in connect %d errno=%d\n", __FUNCTION__, fd, errno);
698 return -1;
701 while (1) {
702 tv.tv_sec = timeout;
703 tv.tv_usec = 0;
704 FD_ZERO(&fds);
705 FD_SET(fd, &fds);
706 r = select(fd + 1, NULL, &fds, NULL, &tv);
707 if (r == 0) {
708 _dprintf("%s: timeout in select %d\n", __FUNCTION__, fd);
709 return -1;
711 else if (r < 0) {
712 if (errno != EINTR) {
713 _dprintf("%s: error in select %d\n", __FUNCTION__, fd);
714 return -1;
716 // loop
718 else {
719 r = 0;
720 n = sizeof(r);
721 if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &r, &n) < 0) || (r != 0)) {
722 _dprintf("%s: error in SO_ERROR %d\n", __FUNCTION__, fd);
723 return -1;
725 break;
730 if (fcntl(fd, F_SETFL, flags) < 0) {
731 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__, fd);
732 return -1;
735 // _dprintf("%s: OK %d\n", __FUNCTION__, fd);
736 return 0;
739 void chld_reap(int sig)
741 while (waitpid(-1, NULL, WNOHANG) > 0) {}
745 int time_ok(void)
747 return time(0) > Y2K;