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
);
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;
251 _dprintf("%s: error reading %s\n", __FUNCTION__
, buf2
);
254 unlink("/tmp/ppp/link");
255 _x_dprintf("required daemon not found, assuming link is dead\n");
259 _x_dprintf("%s: error reading %s\n", __FUNCTION__
, "/tmp/ppp/link");
262 else if (!nvram_match("wan_ipaddr", "0.0.0.0")) {
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) {
273 _x_dprintf("%s: SIOCGIFFLAGS\n", __FUNCTION__
);
276 if ((ifr
.ifr_flags
& IFF_UP
) == 0) {
278 _x_dprintf("%s: !IFF_UP\n", __FUNCTION__
);
281 if (nvram_match("boardrev", "0x11")) { // Ovislink 1600GL - led "connected" on
289 const dns_list_t
*get_dns(void)
291 static dns_list_t dns
;
302 strlcpy(s
, nvram_safe_get("wan_dns"), sizeof(s
));
303 if ((nvram_get_int("dns_addget")) || (s
[0] == 0)) {
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
) {
312 if ((c
= strchr(d
[i
], ':')) != NULL
) {
314 if (((j
= atoi(c
)) < 1) || (j
> 0xFFFF)) continue;
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;
323 dns
.dns
[dns
.count
].port
= port
;
324 dns
.dns
[dns
.count
++].addr
.s_addr
= ia
.s_addr
;
325 if (dns
.count
== 6) break;
333 // -----------------------------------------------------------------------------
335 void set_action(int a
)
338 while (f_write("/var/lock/action", &a
, sizeof(a
), 0, 0) != sizeof(a
)) {
340 if (--r
== 0) return;
342 if (a
!= ACT_IDLE
) sleep(2);
345 int check_action(void)
350 while (f_read("/var/lock/action", &a
, sizeof(a
)) != sizeof(a
)) {
352 if (--r
== 0) return ACT_UNKNOWN
;
357 int wait_action_idle(int n
)
360 if (check_action() == ACT_IDLE
) return 1;
366 // -----------------------------------------------------------------------------
368 const wanface_list_t
*get_wanfaces(void)
370 static wanface_list_t wanfaces
;
376 switch ((proto
= get_wan_proto())) {
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))
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
);
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+";
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
);
414 const char *get_wanface(void)
416 return (*get_wanfaces()).iface
[0].name
;
420 const char *get_wan6face(void)
422 switch (get_ipv6_service()) {
424 case IPV6_NATIVE_DHCP
:
425 return get_wanface();
426 case IPV6_ANYCAST_6TO4
:
431 return nvram_safe_get("ipv6_ifname");
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
];
446 struct ifaddrs
*ifap
, *ifa
;
448 if (getifaddrs(&ifap
) != 0) {
449 _dprintf("getifaddrs failed: %s\n", strerror(errno
));
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
))
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
)
464 addr
= (void *)&(s6
->sin6_addr
);
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
) {
483 // -----------------------------------------------------------------------------
485 long get_uptime(void)
492 char *wl_nvname(const char *nv
, int unit
, int subunit
)
494 static char tmp
[128];
495 char prefix
[] = "wlXXXXXXXXXX_";
498 strcpy(prefix
, "wl_");
499 else if (subunit
> 0)
500 snprintf(prefix
, sizeof(prefix
), "wl%d.%d_", unit
, subunit
);
502 snprintf(prefix
, sizeof(prefix
), "wl%d_", unit
);
503 return strcat_r(prefix
, nv
, tmp
);
506 int get_radio(int unit
)
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
)
518 #ifndef WL_BSS_INFO_VERSION
519 #error WL_BSS_INFO_VERSION
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
));
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
));
538 // -----------------------------------------------------------------------------
540 int mtd_getinfo(const char *mtdname
, int *part
, int *size
)
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;
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)
583 if ((p != NULL) && (*p != 0)) {
584 n = strtol(p, &e, 0);
585 if ((e != p) && ((*e == 0) || (*e == ' ')) && (n > min) && (n < max)) {
593 int nvram_get_file(const char *key
, const char *fname
, int max
)
601 p
= nvram_safe_get(key
);
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
);
616 p = nvram_safe_get(key);
619 n = base64_decode(p, b, n);
620 if (n > 0) return (f_write(fname, b, n, 0, 0700) == n);
626 int nvram_set_file(const char *key
, const char *fname
, int max
)
634 if ((len
= f_size(fname
)) > max
) return 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
);
653 if (((n = f_read(fname, &a, sizeof(a))) > 0) && (n <= max)) {
654 n = base64_encode(a, b, n);
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
)
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
)
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
);
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
);
706 r
= select(fd
+ 1, NULL
, &fds
, NULL
, &tv
);
708 _dprintf("%s: timeout in select %d\n", __FUNCTION__
, fd
);
712 if (errno
!= EINTR
) {
713 _dprintf("%s: error in select %d\n", __FUNCTION__
, fd
);
721 if ((getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &r
, &n
) < 0) || (r
!= 0)) {
722 _dprintf("%s: error in SO_ERROR %d\n", __FUNCTION__
, fd
);
730 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
731 _dprintf("%s: error in F_*ETFL %d\n", __FUNCTION__
, fd
);
735 // _dprintf("%s: OK %d\n", __FUNCTION__, fd);
739 void chld_reap(int sig
)
741 while (waitpid(-1, NULL
, WNOHANG
) > 0) {}
747 return time(0) > Y2K;