4 Copyright (C) 2006-2009 Jonathan Zarate
15 #include <sys/ioctl.h>
17 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
21 #include <sys/sysinfo.h>
22 #include <sys/types.h>
33 int get_wan_proto(void)
35 const char *names
[] = { // order must be synced with def at shared.h
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;
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
62 "other", // IPV6_MANUAL
64 "6rd-pd", // IPV6_6RD_DHCP
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;
77 const char *ipv6_router_address(struct in6_addr
*in6addr
)
81 static char addr6
[INET6_ADDRSTRLEN
];
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);
96 inet_ntop(AF_INET6
, &addr
, addr6
, sizeof(addr6
));
98 memcpy(in6addr
, &addr
, sizeof(addr
));
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
;
112 if (!prefix
|| !local_ip
|| !local_prefix
|| !local_prefix_len
) {
116 *local_prefix_len
= prefix_len
+ 32 - relay_prefix_len
;
117 if (*local_prefix_len
> 64) {
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));
137 int using_dhcpc(void)
139 switch (get_wan_proto()) {
144 return nvram_get_int("pptp_dhcp");
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
))
160 char name
[64], ifname
[64], *next
= NULL
;
161 int unit
= -1, subunit
= -1;
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
));
180 foreach(name
, ifnames
, next
) {
181 if (nvifname_to_osifname(name
, ifname
, sizeof(ifname
)) != 0)
184 if (wl_probe(ifname
) || wl_ioctl(ifname
, WLC_GET_INSTANCE
, &unit
, sizeof(unit
)))
187 // Convert eth name to wl name
188 if (osifname_to_nvifname(name
, ifname
, sizeof(ifname
)) != 0)
191 // Slave intefaces have a '.' in the name
192 if (strchr(ifname
, '.') && !include_vifs
)
195 if (get_ifname_unit(ifname
, &unit
, &subunit
) < 0)
198 ret
|= func(i
++, unit
, subunit
, param
);
203 void notice_set(const char *path
, const char *format
, ...)
209 va_start(args
, format
);
210 vsnprintf(buf
, sizeof(buf
), format
, 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)
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
);
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;
254 _dprintf("%s: error reading %s\n", __FUNCTION__
, buf2
);
257 unlink("/tmp/ppp/link");
258 _x_dprintf("required daemon not found, assuming link is dead\n");
262 _x_dprintf("%s: error reading %s\n", __FUNCTION__
, "/tmp/ppp/link");
265 else if (!nvram_match("wan_ipaddr", "0.0.0.0")) {
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) {
276 _x_dprintf("%s: SIOCGIFFLAGS\n", __FUNCTION__
);
279 if ((ifr
.ifr_flags
& IFF_UP
) == 0) {
281 _x_dprintf("%s: !IFF_UP\n", __FUNCTION__
);
284 if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" on
287 if (nvram_match("boardtype", "0x052b") && nvram_match("boardrev", "0x1204")) { //rt-n15u wan led on
295 const dns_list_t
*get_dns(void)
297 static dns_list_t dns
;
308 strlcpy(s
, nvram_safe_get("wan_dns"), sizeof(s
));
309 if ((nvram_get_int("dns_addget")) || (s
[0] == 0)) {
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
) {
318 if ((c
= strchr(d
[i
], ':')) != NULL
) {
320 if (((j
= atoi(c
)) < 1) || (j
> 0xFFFF)) continue;
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;
329 dns
.dns
[dns
.count
].port
= port
;
330 dns
.dns
[dns
.count
++].addr
.s_addr
= ia
.s_addr
;
331 if (dns
.count
== 6) break;
339 // -----------------------------------------------------------------------------
341 void set_action(int a
)
344 while (f_write("/var/lock/action", &a
, sizeof(a
), 0, 0) != sizeof(a
)) {
346 if (--r
== 0) return;
348 if (a
!= ACT_IDLE
) sleep(2);
351 int check_action(void)
356 while (f_read("/var/lock/action", &a
, sizeof(a
)) != sizeof(a
)) {
358 if (--r
== 0) return ACT_UNKNOWN
;
363 int wait_action_idle(int n
)
366 if (check_action() == ACT_IDLE
) return 1;
372 // -----------------------------------------------------------------------------
374 const wanface_list_t
*get_wanfaces(void)
376 static wanface_list_t wanfaces
;
382 switch ((proto
= get_wan_proto())) {
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))
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
);
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+";
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
);
420 const char *get_wanface(void)
422 return (*get_wanfaces()).iface
[0].name
;
426 const char *get_wan6face(void)
428 switch (get_ipv6_service()) {
430 case IPV6_NATIVE_DHCP
:
431 return get_wanface();
432 case IPV6_ANYCAST_6TO4
:
437 return nvram_safe_get("ipv6_ifname");
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
];
452 struct ifaddrs
*ifap
, *ifa
;
454 if (getifaddrs(&ifap
) != 0) {
455 _dprintf("getifaddrs failed: %s\n", strerror(errno
));
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
))
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
)
470 addr
= (void *)&(s6
->sin6_addr
);
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
) {
489 // -----------------------------------------------------------------------------
491 long get_uptime(void)
498 char *wl_nvname(const char *nv
, int unit
, int subunit
)
500 static char tmp
[128];
501 char prefix
[] = "wlXXXXXXXXXX_";
504 strcpy(prefix
, "wl_");
505 else if (subunit
> 0)
506 snprintf(prefix
, sizeof(prefix
), "wl%d.%d_", unit
, subunit
);
508 snprintf(prefix
, sizeof(prefix
), "wl%d_", unit
);
509 return strcat_r(prefix
, nv
, tmp
);
512 int get_radio(int unit
)
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
)
524 #ifndef WL_BSS_INFO_VERSION
525 #error WL_BSS_INFO_VERSION
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
));
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
));
544 // -----------------------------------------------------------------------------
546 int mtd_getinfo(const char *mtdname
, int *part
, int *size
)
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;
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)
589 if ((p != NULL) && (*p != 0)) {
590 n = strtol(p, &e, 0);
591 if ((e != p) && ((*e == 0) || (*e == ' ')) && (n > min) && (n < max)) {
599 int nvram_get_file(const char *key
, const char *fname
, int max
)
607 p
= nvram_safe_get(key
);
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
);
622 p = nvram_safe_get(key);
625 n = base64_decode(p, b, n);
626 if (n > 0) return (f_write(fname, b, n, 0, 0700) == n);
632 int nvram_set_file(const char *key
, const char *fname
, int max
)
640 if ((len
= f_size(fname
)) > max
) return 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
);
659 if (((n = f_read(fname, &a, sizeof(a))) > 0) && (n <= max)) {
660 n = base64_encode(a, b, n);
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
)
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
)
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
);
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
);
712 r
= select(fd
+ 1, NULL
, &fds
, NULL
, &tv
);
714 _dprintf("%s: timeout in select %d\n", __FUNCTION__
, fd
);
718 if (errno
!= EINTR
) {
719 _dprintf("%s: error in select %d\n", __FUNCTION__
, fd
);
727 if ((getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &r
, &n
) < 0) || (r
!= 0)) {
728 _dprintf("%s: error in SO_ERROR %d\n", __FUNCTION__
, fd
);
736 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
737 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__
, fd
);
741 // _dprintf("%s: OK %d\n", __FUNCTION__, fd);
745 void chld_reap(int sig
)
747 while (waitpid(-1, NULL
, WNOHANG
) > 0) {}
753 return time(0) > Y2K;