3 Copyright 2003, CyberTAN Inc. All Rights Reserved
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
6 the contents of this file may not be disclosed to third parties,
7 copied or duplicated in any form without the prior written
8 permission of CyberTAN Inc.
10 This software should be used as a reference only, and it not
11 intended for production use!
13 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
14 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. CYBERTAN
15 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
16 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
21 Copyright 2005, Broadcom Corporation
24 THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
25 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
26 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
27 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
32 Modified for Tomato Firmware
33 Portions, Copyright (C) 2006-2009 Jonathan Zarate
38 #include <arpa/inet.h>
44 #include <sys/mount.h>
48 // Pop an alarm to recheck pids in 500 msec.
49 static const struct itimerval pop_tv
= { {0,0}, {0, 500 * 1000} };
51 // Pop an alarm to reap zombies.
52 static const struct itimerval zombie_tv
= { {0,0}, {307, 0} };
54 // -----------------------------------------------------------------------------
56 static const char dmhosts
[] = "/etc/dnsmasq/hosts";
57 static const char dmdhcp
[] = "/etc/dnsmasq/dhcp";
58 static const char dmresolv
[] = "/etc/resolv.dnsmasq";
60 static pid_t pid_dnsmasq
= -1;
62 static int is_wet(int idx
, int unit
, int subunit
, void *param
)
64 return nvram_match(wl_nvname("mode", unit
, subunit
), "wet");
73 const char *router_ip
;
77 char *mac
, *ip
, *name
;
92 start_service("dnsmasq");
98 if (foreach_wif(1, NULL
, is_wet
)) return;
100 if ((f
= fopen("/etc/dnsmasq.conf", "w")) == NULL
) return;
102 router_ip
= nvram_safe_get("lan_ipaddr");
105 "pid-file=/var/run/dnsmasq.pid\n");
106 if (((nv
= nvram_get("wan_domain")) != NULL
) || ((nv
= nvram_get("wan_get_domain")) != NULL
)) {
107 if (*nv
) fprintf(f
, "domain=%s\n", nv
);
111 const dns_list_t
*dns
= get_dns(); // this always points to a static buffer
113 if (((nv
= nvram_get("dns_minport")) != NULL
) && (*nv
)) n
= atoi(nv
);
116 "resolv-file=%s\n" // the real stuff is here
117 "addn-hosts=%s\n" // directory with additional hosts files
118 "dhcp-hostsfile=%s\n" // directory with dhcp hosts files
119 "expand-hosts\n" // expand hostnames in hosts file
120 "min-port=%u\n", // min port used for random src port
121 dmresolv
, dmhosts
, dmdhcp
, n
);
122 do_dns
= nvram_match("dhcpd_dmdns", "1");
124 // DNS rebinding protection, will discard upstream RFC1918 responses
125 if (nvram_get_int("dns_norebind")) {
128 "rebind-localhost-ok\n");
129 // allow RFC1918 responses for server domain
130 switch (get_wan_proto()) {
132 nv
= nvram_get("pptp_server_ip");
135 nv
= nvram_get("l2tp_server_ip");
141 if (nv
&& *nv
) fprintf(f
, "rebind-domain-ok=%s\n", nv
);
144 #ifdef TCONFIG_DNSCRYPT
145 if (nvram_match("dnscrypt_proxy", "1")) {
146 fprintf(f
, "server=127.0.0.1#40\n");
150 for (n
= 0 ; n
< dns
->count
; ++n
) {
151 if (dns
->dns
[n
].port
!= 53) {
152 fprintf(f
, "server=%s#%u\n", inet_ntoa(dns
->dns
[n
].addr
), dns
->dns
[n
].port
);
156 if (nvram_get_int("dhcpd_static_only")) {
157 fprintf(f
, "dhcp-ignore=tag:!known\n");
162 char lanN_proto
[] = "lanXX_proto";
163 char lanN_ifname
[] = "lanXX_ifname";
164 char lanN_ipaddr
[] = "lanXX_ipaddr";
165 char lanN_netmask
[] = "lanXX_netmask";
166 char dhcpdN_startip
[] = "dhcpdXX_startip";
167 char dhcpdN_endip
[] = "dhcpdXX_endip";
168 char dhcpN_start
[] = "dhcpXX_start";
169 char dhcpN_num
[] = "dhcpXX_num";
170 char dhcpN_lease
[] = "dhcpXX_lease";
172 for(br
=0 ; br
<=3 ; br
++) {
173 char bridge
[2] = "0";
179 sprintf(lanN_proto
, "lan%s_proto", bridge
);
180 sprintf(lanN_ifname
, "lan%s_ifname", bridge
);
181 sprintf(lanN_ipaddr
, "lan%s_ipaddr", bridge
);
182 do_dhcpd
= nvram_match(lanN_proto
, "dhcp");
186 router_ip
= nvram_safe_get(lanN_ipaddr
);
187 strlcpy(lan
, router_ip
, sizeof(lan
));
188 if ((p
= strrchr(lan
, '.')) != NULL
) *(p
+ 1) = 0;
192 nvram_safe_get(lanN_ifname
));
194 sprintf(dhcpN_lease
, "dhcp%s_lease", bridge
);
195 dhcp_lease
= nvram_get_int(dhcpN_lease
);
197 if (dhcp_lease
<= 0) dhcp_lease
= 1440;
199 if ((e
= nvram_get("dhcpd_slt")) != NULL
) n
= atoi(e
); else n
= 0;
200 if (n
< 0) strcpy(sdhcp_lease
, "infinite");
201 else sprintf(sdhcp_lease
, "%dm", (n
> 0) ? n
: dhcp_lease
);
204 // if not using dnsmasq for dns
206 if ((dns
->count
== 0) && (nvram_get_int("dhcpd_llndns"))) {
207 // no DNS might be temporary. use a low lease time to force clients to update.
209 strcpy(sdhcp_lease
, "2m");
213 // pass the dns directly
215 for (n
= 0 ; n
< dns
->count
; ++n
) {
216 if (dns
->dns
[n
].port
== 53) { // check: option 6 doesn't seem to support other ports
217 sprintf(buf
+ strlen(buf
), ",%s", inet_ntoa(dns
->dns
[n
].addr
));
220 fprintf(f
, "dhcp-option=%s,6%s\n", nvram_safe_get(lanN_ifname
), buf
);
224 sprintf(dhcpdN_startip
, "dhcpd%s_startip", bridge
);
225 sprintf(dhcpdN_endip
, "dhcpd%s_endip", bridge
);
226 sprintf(lanN_netmask
, "lan%s_netmask", bridge
);
228 if ((p
= nvram_get(dhcpdN_startip
)) && (*p
) && (e
= nvram_get(dhcpdN_endip
)) && (*e
)) {
229 fprintf(f
, "dhcp-range=%s,%s,%s,%s,%dm\n", nvram_safe_get(lanN_ifname
), p
, e
, nvram_safe_get(lanN_netmask
), dhcp_lease
);
233 sprintf(dhcpN_start
, "dhcp%s_start", bridge
);
234 sprintf(dhcpN_num
, "dhcp%s_num", bridge
);
235 sprintf(lanN_netmask
, "lan%s_netmask", bridge
);
236 dhcp_start
= nvram_get_int(dhcpN_start
);
237 dhcp_count
= nvram_get_int(dhcpN_num
);
238 fprintf(f
, "dhcp-range=%s,%s%d,%s%d,%s,%dm\n",
239 nvram_safe_get(lanN_ifname
), lan
, dhcp_start
, lan
, dhcp_start
+ dhcp_count
- 1, nvram_safe_get(lanN_netmask
), dhcp_lease
);
242 nv
= nvram_safe_get(lanN_ipaddr
);
243 if ((nvram_get_int("dhcpd_gwmode") == 1) && (get_wan_proto() == WP_DISABLED
)) {
244 p
= nvram_safe_get("lan_gateway");
245 if ((*p
) && (strcmp(p
, "0.0.0.0") != 0)) nv
= p
;
249 "dhcp-option=%s,3,%s\n", // gateway
250 nvram_safe_get(lanN_ifname
), nv
);
252 if (((nv
= nvram_get("wan_wins")) != NULL
) && (*nv
) && (strcmp(nv
, "0.0.0.0") != 0)) {
253 fprintf(f
, "dhcp-option=%s,44,%s\n", nvram_safe_get(lanN_ifname
), nv
);
255 #ifdef TCONFIG_SAMBASRV
256 else if (nvram_get_int("smbd_enable") && nvram_invmatch("lan_hostname", "") && nvram_get_int("smbd_wins")) {
257 if ((nv
== NULL
) || (*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
258 // Samba will serve as a WINS server
259 fprintf(f
, "dhcp-option=%s,44,0.0.0.0\n", nvram_safe_get(lanN_ifname
));
264 if (strcmp(nvram_safe_get(lanN_ifname
),"")!=0) {
265 fprintf(f
, "interface=%s\n", nvram_safe_get(lanN_ifname
));
266 fprintf(f
, "no-dhcp-interface=%s\n", nvram_safe_get(lanN_ifname
));
270 // write static lease entries & create hosts file
272 mkdir_if_none(dmhosts
);
273 snprintf(buf
, sizeof(buf
), "%s/hosts", dmhosts
);
274 if ((hf
= fopen(buf
, "w")) != NULL
) {
275 if (((nv
= nvram_get("wan_hostname")) != NULL
) && (*nv
))
276 fprintf(hf
, "%s %s\n", router_ip
, nv
);
277 #ifdef TCONFIG_SAMBASRV
278 else if (((nv
= nvram_get("lan_hostname")) != NULL
) && (*nv
))
279 fprintf(hf
, "%s %s\n", router_ip
, nv
);
281 p
= (char *)get_wanip();
282 if ((*p
== 0) || strcmp(p
, "0.0.0.0") == 0)
284 fprintf(hf
, "%s wan-ip\n", p
);
286 fprintf(hf
, "%s %s-wan\n", p
, nv
);
289 mkdir_if_none(dmdhcp
);
290 snprintf(buf
, sizeof(buf
), "%s/dhcp-hosts", dmdhcp
);
291 df
= fopen(buf
, "w");
293 // PREVIOUS/OLD FORMAT:
294 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 53 w/ delim
295 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 85 w/ delim
296 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz> = 106 w/ delim
298 // NEW FORMAT (+static ARP binding after hostname):
299 // 00:aa:bb:cc:dd:ee<123<xxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 55 w/ delim
300 // 00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 87 w/ delim
301 // 00:aa:bb:cc:dd:ee,00:aa:bb:cc:dd:ee<123.123.123.123<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xyz<a> = 108 w/ delim
303 p
= nvram_safe_get("dhcpd_static");
304 while ((e
= strchr(p
, '>')) != NULL
) {
315 if ((e
= strchr(buf
, '<')) == NULL
) continue;
320 if ((e
= strchr(ip
, '<')) == NULL
) continue;
322 if (strchr(ip
, '.') == NULL
) {
324 if ((ipn
<= 0) || (ipn
> 255)) continue;
325 sprintf(ipbuf
, "%s%d", lan
, ipn
);
329 if (inet_addr(ip
) == INADDR_NONE
) continue;
334 if ((e
= strchr(name
, '<')) != NULL
) {
338 if ((hf
) && (*name
!= 0)) {
339 fprintf(hf
, "%s %s\n", ip
, name
);
342 if ((do_dhcpd_hosts
> 0) && (*mac
!= 0) && (strcmp(mac
, "00:00:00:00:00:00") != 0)) {
343 if (nvram_get_int("dhcpd_slt") == 0) {
344 fprintf(f
, "dhcp-host=%s,%s\n", mac
, ip
);
346 fprintf(f
, "dhcp-host=%s,%s,%s\n", mac
, ip
, sdhcp_lease
);
354 n
= nvram_get_int("dhcpd_lmax");
356 "dhcp-lease-max=%d\n",
358 if (nvram_get_int("dhcpd_auth") >= 0) {
359 fprintf(f
, "dhcp-authoritative\n");
362 #ifdef TCONFIG_DNSCRYPT
363 if (nvram_match("dnscrypt_proxy", "1")) {
364 fprintf(f
, "strict-order\n");
370 #ifdef TCONFIG_OPENVPN
371 write_vpn_dnsmasq_config(f
);
375 write_pptpd_dnsmasq_config(f
);
378 fprintf(f
, "%s\n\n", nvram_safe_get("dnsmasq_custom"));
380 fappend(f
, "/etc/dnsmasq.custom");
387 unlink("/etc/resolv.conf");
388 symlink("/rom/etc/resolv.conf", "/etc/resolv.conf"); // nameserver 127.0.0.1
391 TRACE_PT("run dnsmasq\n");
393 // Default to some values we like, but allow the user to override them.
394 eval("dnsmasq", "-c", "1500", "--log-async");
396 if (!nvram_contains_word("debug_norestart", "dnsmasq")) {
402 #ifdef TCONFIG_DNSCRYPT
403 //start dnscrypt-proxy
404 if (nvram_match("dnscrypt_proxy", "1")) {
406 eval("dnscrypt-proxy", "-d", "-P", "40");
412 void stop_dnsmasq(void)
417 stop_service("dnsmasq");
423 unlink("/etc/resolv.conf");
424 symlink(dmresolv
, "/etc/resolv.conf");
426 killall_tk("dnsmasq");
427 #ifdef TCONFIG_DNSCRYPT
428 killall_tk("dnscrypt-proxy");
434 void clear_resolv(void)
436 f_write(dmresolv
, NULL
, 0, 0, 0); // blank
440 static int write_ipv6_dns_servers(FILE *f
, const char *prefix
, char *dns
, const char *suffix
, int once
)
442 char p
[INET6_ADDRSTRLEN
+ 1], *next
= NULL
;
443 struct in6_addr addr
;
446 foreach(p
, dns
, next
) {
447 // verify that this is a valid IPv6 address
448 if (inet_pton(AF_INET6
, p
, &addr
) == 1) {
449 fprintf(f
, "%s%s%s", (once
&& cnt
) ? "" : prefix
, p
, suffix
);
458 void dns_to_resolv(void)
461 const dns_list_t
*dns
;
465 m
= umask(022); // 077 from pppoecd
466 if ((f
= fopen(dmresolv
, "w")) != NULL
) {
467 // Check for VPN DNS entries
468 if (!write_pptpvpn_resolv(f
) && !write_vpn_resolv(f
)) {
470 if (write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_dns"), "\n", 0) == 0 || nvram_get_int("dns_addget"))
471 write_ipv6_dns_servers(f
, "nameserver ", nvram_safe_get("ipv6_get_dns"), "\n", 0);
473 dns
= get_dns(); // static buffer
474 if (dns
->count
== 0) {
475 // Put a pseudo DNS IP to trigger Connect On Demand
476 if (nvram_match("ppp_demand", "1")) {
477 switch (get_wan_proto()) {
482 fprintf(f
, "nameserver 1.1.1.1\n");
488 for (i
= 0; i
< dns
->count
; i
++) {
489 if (dns
->dns
[i
].port
== 53) { // resolv.conf doesn't allow for an alternate port
490 fprintf(f
, "nameserver %s\n", inet_ntoa(dns
->dns
[i
].addr
));
500 // -----------------------------------------------------------------------------
502 void start_httpd(void)
505 start_service("httpd");
515 void stop_httpd(void)
518 stop_service("httpd");
525 // -----------------------------------------------------------------------------
528 static void add_ip6_lanaddr(void)
530 char ip
[INET6_ADDRSTRLEN
+ 4];
533 p
= ipv6_router_address(NULL
);
535 snprintf(ip
, sizeof(ip
), "%s/%d", p
, nvram_get_int("ipv6_prefix_length") ? : 64);
536 eval("ip", "-6", "addr", "add", ip
, "dev", nvram_safe_get("lan_ifname"));
540 void start_ipv6_tunnel(void)
542 char ip
[INET6_ADDRSTRLEN
+ 4];
543 struct in_addr addr4
;
544 struct in6_addr addr
;
545 const char *wanip
, *mtu
, *tun_dev
;
548 service
= get_ipv6_service();
549 tun_dev
= get_wan6face();
551 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
554 if (service
== IPV6_ANYCAST_6TO4
)
555 snprintf(ip
, sizeof(ip
), "192.88.99.%d", nvram_get_int("ipv6_relay"));
557 strlcpy(ip
, (char *)nvram_safe_get("ipv6_tun_v4end"), sizeof(ip
));
558 eval("ip", "tunnel", "add", (char *)tun_dev
, "mode", "sit",
560 "local", (char *)wanip
,
561 "ttl", nvram_safe_get("ipv6_tun_ttl"));
563 eval("ip", "link", "set", (char *)tun_dev
, "mtu", (char *)mtu
, "up");
564 nvram_set("ipv6_ifname", (char *)tun_dev
);
566 if (service
== IPV6_ANYCAST_6TO4
) {
569 memset(&addr
, 0, sizeof(addr
));
570 inet_aton(wanip
, &addr4
);
571 addr
.s6_addr16
[0] = htons(0x2002);
572 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
573 addr
.s6_addr16
[7] = htons(0x0001);
574 inet_ntop(AF_INET6
, &addr
, ip
, sizeof(ip
));
575 strncat(ip
, "/16", sizeof(ip
));
578 snprintf(ip
, sizeof(ip
), "%s/%d",
579 nvram_safe_get("ipv6_tun_addr"),
580 nvram_get_int("ipv6_tun_addrlen") ? : 64);
582 eval("ip", "addr", "add", ip
, "dev", (char *)tun_dev
);
583 eval("ip", "route", "add", "::/0", "dev", (char *)tun_dev
);
586 if (service
== IPV6_ANYCAST_6TO4
)
590 void stop_ipv6_tunnel(void)
592 eval("ip", "tunnel", "del", (char *)get_wan6face());
593 if (get_ipv6_service() == IPV6_ANYCAST_6TO4
) {
594 // get rid of old IPv6 address from lan iface
595 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
600 void start_6rd_tunnel(void)
602 const char *tun_dev
, *wanip
;
603 int service
, mask_len
, prefix_len
, local_prefix_len
;
604 char mtu
[10], prefix
[INET6_ADDRSTRLEN
], relay
[INET_ADDRSTRLEN
];
605 struct in_addr netmask_addr
, relay_addr
, relay_prefix_addr
, wanip_addr
;
606 struct in6_addr prefix_addr
, local_prefix_addr
;
607 char local_prefix
[INET6_ADDRSTRLEN
];
608 char tmp_ipv6
[INET6_ADDRSTRLEN
+ 4], tmp_ipv4
[INET_ADDRSTRLEN
+ 4];
612 service
= get_ipv6_service();
614 tun_dev
= get_wan6face();
615 sprintf(mtu
, "%d", (nvram_get_int("wan_mtu") > 0) ? (nvram_get_int("wan_mtu") - 20) : 1280);
617 // maybe we can merge the ipv6_6rd_* variables into a single ipv_6rd_string (ala wan_6rd)
618 // to save nvram space?
619 if (service
== IPV6_6RD
) {
620 _dprintf("starting 6rd tunnel using manual settings.\n");
621 mask_len
= nvram_get_int("ipv6_6rd_ipv4masklen");
622 prefix_len
= nvram_get_int("ipv6_6rd_prefix_length");
623 strcpy(prefix
, nvram_safe_get("ipv6_6rd_prefix"));
624 strcpy(relay
, nvram_safe_get("ipv6_6rd_borderrelay"));
627 _dprintf("starting 6rd tunnel using automatic settings.\n");
628 char *wan_6rd
= nvram_safe_get("wan_6rd");
629 if (sscanf(wan_6rd
, "%d %d %s %s", &mask_len
, &prefix_len
, prefix
, relay
) < 4) {
630 _dprintf("wan_6rd string is missing or invalid (%s)\n", wan_6rd
);
635 // validate values that were passed
636 if (mask_len
< 0 || mask_len
> 32) {
637 _dprintf("invalid mask_len value (%d)\n", mask_len
);
640 if (prefix_len
< 0 || prefix_len
> 128) {
641 _dprintf("invalid prefix_len value (%d)\n", prefix_len
);
644 if ((32 - mask_len
) + prefix_len
> 128) {
645 _dprintf("invalid combination of mask_len and prefix_len!\n");
649 sprintf(tmp
, "ping -q -c 2 %s | grep packet", relay
);
650 if ((f
= popen(tmp
, "r")) == NULL
) {
651 _dprintf("error obtaining data\n");
654 fgets(tmp
, sizeof(tmp
), f
);
656 if (strstr(tmp
, " 0% packet loss") == NULL
) {
657 _dprintf("failed to ping border relay\n");
661 // get relay prefix from border relay address and mask
662 netmask_addr
.s_addr
= htonl(0xffffffff << (32 - mask_len
));
663 inet_aton(relay
, &relay_addr
);
664 relay_prefix_addr
.s_addr
= relay_addr
.s_addr
& netmask_addr
.s_addr
;
666 // calculate the local prefix
667 inet_pton(AF_INET6
, prefix
, &prefix_addr
);
668 inet_pton(AF_INET
, wanip
, &wanip_addr
);
669 if (calc_6rd_local_prefix(&prefix_addr
, prefix_len
, mask_len
,
670 &wanip_addr
, &local_prefix_addr
, &local_prefix_len
) == 0) {
671 _dprintf("error calculating local prefix.");
674 inet_ntop(AF_INET6
, &local_prefix_addr
, local_prefix
, sizeof(local_prefix
));
676 snprintf(tmp_ipv6
, sizeof(tmp_ipv6
), "%s1", local_prefix
);
677 nvram_set("ipv6_rtr_addr", tmp_ipv6
);
678 nvram_set("ipv6_prefix", local_prefix
);
680 // load sit module needed for the 6rd tunnel
683 // creating the 6rd tunnel
684 eval("ip", "tunnel", "add", (char *)tun_dev
, "mode", "sit", "local", (char *)wanip
, "ttl", nvram_safe_get("ipv6_tun_ttl"));
686 snprintf(tmp_ipv6
, sizeof(tmp_ipv6
), "%s/%d", prefix
, prefix_len
);
687 snprintf(tmp_ipv4
, sizeof(tmp_ipv4
), "%s/%d", inet_ntoa(relay_prefix_addr
), mask_len
);
688 eval("ip", "tunnel" "6rd", "dev", (char *)tun_dev
, "6rd-prefix", tmp_ipv6
, "6rd-relay_prefix", tmp_ipv4
);
690 // bringing up the link
691 eval("ip", "link", "set", "dev", (char *)tun_dev
, "mtu", (char *)mtu
, "up");
693 // setting the WAN address Note: IPv6 WAN CIDR should be: ((32 - ip6rd_ipv4masklen) + ip6rd_prefixlen)
694 snprintf(tmp_ipv6
, sizeof(tmp_ipv6
), "%s1/%d", local_prefix
, local_prefix_len
);
695 eval("ip", "-6", "addr", "add", tmp_ipv6
, "dev", (char *)tun_dev
);
697 // setting the LAN address Note: IPv6 LAN CIDR should be 64
698 snprintf(tmp_ipv6
, sizeof(tmp_ipv6
), "%s1/%d", local_prefix
, nvram_get_int("ipv6_prefix_length") ? : 64);
699 eval("ip", "-6", "addr", "add", tmp_ipv6
, "dev", nvram_safe_get("lan_ifname"));
701 // adding default route via the border relay
702 snprintf(tmp_ipv6
, sizeof(tmp_ipv6
), "::%s", relay
);
703 eval("ip", "-6", "route", "add", "default", "via", tmp_ipv6
, "dev", (char *)tun_dev
);
705 nvram_set("ipv6_ifname", (char *)tun_dev
);
713 void stop_6rd_tunnel(void)
715 eval("ip", "tunnel", "del", (char *)get_wan6face());
716 eval("ip", "-6", "addr", "flush", "dev", nvram_safe_get("lan_ifname"), "scope", "global");
720 static pid_t pid_radvd
= -1;
722 void start_radvd(void)
725 char *prefix
, *ip
, *mtu
;
726 int do_dns
, do_6to4
, do_6rd
;
727 char *argv
[] = { "radvd", NULL
, NULL
, NULL
};
728 int pid
, argc
, service
, cnt
;
731 start_service("radvd");
737 if (ipv6_enabled() && nvram_get_int("ipv6_radvd")) {
738 service
= get_ipv6_service();
739 do_6to4
= (service
== IPV6_ANYCAST_6TO4
);
740 do_6rd
= (service
== IPV6_6RD
|| service
== IPV6_6RD_DHCP
);
744 case IPV6_NATIVE_DHCP
:
747 case IPV6_ANYCAST_6TO4
:
751 mtu
= (nvram_get_int("ipv6_tun_mtu") > 0) ? nvram_safe_get("ipv6_tun_mtu") : "1480";
754 prefix
= do_6to4
? "0:0:0:1::" : nvram_safe_get("ipv6_prefix");
757 if (!(*prefix
)) prefix
= "::";
760 if ((f
= fopen("/etc/radvd.conf", "w")) == NULL
) return;
762 ip
= (char *)ipv6_router_address(NULL
);
763 do_dns
= (*ip
) && nvram_match("dhcpd_dmdns", "1");
768 " IgnoreIfMissing on;\n"
769 " AdvSendAdvert on;\n"
770 " MaxRtrAdvInterval 60;\n"
771 " AdvHomeAgentFlag off;\n"
772 " AdvManagedFlag off;\n"
777 " AdvAutonomous on;\n"
781 nvram_safe_get("lan_ifname"),
782 mtu
? " AdvLinkMTU " : "", mtu
? : "", mtu
? ";\n" : "",
784 (do_6to4
|| do_6rd
) ? " AdvValidLifetime 300;\n AdvPreferredLifetime 120;\n" : "",
785 do_6to4
? " Base6to4Interface " : "",
786 do_6to4
? get_wanface() : "",
787 do_6to4
? ";\n" : "");
790 fprintf(f
, " RDNSS %s {};\n", ip
);
793 cnt
= write_ipv6_dns_servers(f
, " RDNSS ", nvram_safe_get("ipv6_dns"), " ", 1);
794 if (cnt
== 0 || nvram_get_int("dns_addget"))
795 cnt
+= write_ipv6_dns_servers(f
, (cnt
) ? "" : " RDNSS ", nvram_safe_get("ipv6_get_dns"), " ", 1);
796 if (cnt
) fprintf(f
, "{};\n");
800 "};\n"); // close "interface" section
805 if (nvram_get_int("debug_ipv6")) {
810 _eval(argv
, NULL
, 0, &pid
);
812 if (!nvram_contains_word("debug_norestart", "radvd")) {
818 void stop_radvd(void)
821 stop_service("radvd");
829 void start_ipv6(void)
833 service
= get_ipv6_service();
836 // Check if turned on
843 case IPV6_NATIVE_DHCP
:
844 case IPV6_ANYCAST_6TO4
:
845 nvram_set("ipv6_rtr_addr", "");
846 nvram_set("ipv6_prefix", "");
850 if (service
!= IPV6_DISABLED
) {
851 if ((nvram_get_int("ipv6_accept_ra") & 2) != 0 && !nvram_get_int("ipv6_radvd"))
852 accept_ra(nvram_safe_get("lan_ifname"));
860 eval("ip", "-6", "addr", "flush", "scope", "global");
865 // -----------------------------------------------------------------------------
867 void start_upnp(void)
870 start_service("upnp");
874 if (get_wan_proto() == WP_DISABLED
) return;
880 if (((enable
= nvram_get_int("upnp_enable")) & 3) != 0) {
881 mkdir("/etc/upnp", 0777);
882 if (f_exists("/etc/upnp/config.alt")) {
883 xstart("miniupnpd", "-f", "/etc/upnp/config.alt");
886 if ((f
= fopen("/etc/upnp/config", "w")) != NULL
) {
887 upnp_port
= nvram_get_int("upnp_port");
888 if ((upnp_port
< 0) || (upnp_port
>= 0xFFFF)) upnp_port
= 0;
897 "upnp_forward_chain=upnp\n"
898 "upnp_nat_chain=upnp\n"
899 "notify_interval=%d\n"
900 "system_uptime=yes\n"
905 (enable
& 1) ? "yes" : "no", // upnp enable
906 (enable
& 2) ? "yes" : "no", // natpmp enable
907 nvram_get_int("upnp_secure") ? "yes" : "no", // secure_mode (only forward to self)
908 nvram_get_int("upnp_ssdp_interval")
911 if (nvram_get_int("upnp_clean")) {
912 int interval
= nvram_get_int("upnp_clean_interval");
913 if (interval
< 60) interval
= 60;
915 "clean_ruleset_interval=%d\n"
916 "clean_ruleset_threshold=%d\n",
918 nvram_get_int("upnp_clean_threshold")
922 fprintf(f
,"clean_ruleset_interval=0\n");
924 if (nvram_match("upnp_mnp", "1")) {
925 int https
= nvram_get_int("https_enable");
926 fprintf(f
, "presentation_url=http%s://%s:%s/forward-upnp.asp\n",
927 https
? "s" : "", nvram_safe_get("lan_ipaddr"),
928 nvram_safe_get(https
? "https_lanport" : "http_lanport"));
931 // Empty parameters are not included into XML service description
932 fprintf(f
, "presentation_url=\n");
936 f_read_string("/proc/sys/kernel/random/uuid", uuid
, sizeof(uuid
));
937 fprintf(f
, "uuid=%s\n", uuid
);
939 char lanN_ipaddr
[] = "lanXX_ipaddr";
940 char lanN_netmask
[] = "lanXX_netmask";
941 char upnp_lanN
[] = "upnp_lanXX";
944 for(br
=0 ; br
<4 ; br
++) {
945 char bridge
[2] = "0";
951 sprintf(lanN_ipaddr
, "lan%s_ipaddr", bridge
);
952 sprintf(lanN_netmask
, "lan%s_netmask", bridge
);
953 sprintf(upnp_lanN
, "upnp_lan%s", bridge
);
955 char *lanip
= nvram_safe_get(lanN_ipaddr
);
956 char *lanmask
= nvram_safe_get(lanN_netmask
);
957 char *lanlisten
= nvram_safe_get(upnp_lanN
);
958 if((strcmp(lanlisten
,"1")==0) && (strcmp(lanip
,"")!=0) && (strcmp(lanip
,"0.0.0.0")!=0)) {
960 "listening_ip=%s/%s\n",
963 if ((ports
[0] = nvram_get_int("upnp_min_port_int")) > 0 &&
964 (ports
[1] = nvram_get_int("upnp_max_port_int")) > 0 &&
965 (ports
[2] = nvram_get_int("upnp_min_port_ext")) > 0 &&
966 (ports
[3] = nvram_get_int("upnp_max_port_ext")) > 0) {
968 "allow %d-%d %s/%s %d-%d\n",
975 // by default allow only redirection of ports above 1024
976 fprintf(f
, "allow 1024-65535 %s/%s 1024-65535\n", lanip
, lanmask
);
981 fappend(f
, "/etc/upnp/config.custom");
982 fprintf(f
, "\ndeny 0-65535 0.0.0.0/0 0-65535\n");
985 xstart("miniupnpd", "-f", "/etc/upnp/config");
994 stop_service("upnp");
998 killall_tk("miniupnpd");
1001 // -----------------------------------------------------------------------------
1003 static pid_t pid_crond
= -1;
1005 void start_cron(void)
1009 eval("crond", nvram_contains_word("log_events", "crond") ? NULL
: "-l", "9");
1010 if (!nvram_contains_word("debug_norestart", "crond")) {
1015 void stop_cron(void)
1018 killall_tk("crond");
1021 // -----------------------------------------------------------------------------
1024 static pid_t pid_hotplug2
= -1;
1026 void start_hotplug2()
1030 f_write_string("/proc/sys/kernel/hotplug", "", FW_NEWLINE
, 0);
1031 xstart("hotplug2", "--persistent", "--no-coldplug");
1032 // FIXME: Don't remember exactly why I put "sleep" here -
1033 // but it was not for a race with check_services()... - TB
1036 if (!nvram_contains_word("debug_norestart", "hotplug2")) {
1041 void stop_hotplug2(void)
1044 killall_tk("hotplug2");
1047 #endif /* LINUX26 */
1048 // -----------------------------------------------------------------------------
1050 // Written by Sparq in 2002/07/16
1051 void start_zebra(void)
1053 #ifdef TCONFIG_ZEBRA
1054 if (getpid() != 1) {
1055 start_service("zebra");
1061 char *lan_tx
= nvram_safe_get("dr_lan_tx");
1062 char *lan_rx
= nvram_safe_get("dr_lan_rx");
1063 char *lan1_tx
= nvram_safe_get("dr_lan1_tx");
1064 char *lan1_rx
= nvram_safe_get("dr_lan1_rx");
1065 char *lan2_tx
= nvram_safe_get("dr_lan2_tx");
1066 char *lan2_rx
= nvram_safe_get("dr_lan2_rx");
1067 char *lan3_tx
= nvram_safe_get("dr_lan3_tx");
1068 char *lan3_rx
= nvram_safe_get("dr_lan3_rx");
1069 char *wan_tx
= nvram_safe_get("dr_wan_tx");
1070 char *wan_rx
= nvram_safe_get("dr_wan_rx");
1072 if ((*lan_tx
== '0') && (*lan_rx
== '0') &&
1073 (*lan1_tx
== '0') && (*lan1_rx
== '0') &&
1074 (*lan2_tx
== '0') && (*lan2_rx
== '0') &&
1075 (*lan3_tx
== '0') && (*lan3_rx
== '0') &&
1076 (*wan_tx
== '0') && (*wan_rx
== '0')) {
1081 if ((fp
= fopen("/etc/zebra.conf", "w")) != NULL
) {
1086 if ((fp
= fopen("/etc/ripd.conf", "w")) != NULL
) {
1087 char *lan_ifname
= nvram_safe_get("lan_ifname");
1088 char *lan1_ifname
= nvram_safe_get("lan1_ifname");
1089 char *lan2_ifname
= nvram_safe_get("lan2_ifname");
1090 char *lan3_ifname
= nvram_safe_get("lan3_ifname");
1091 char *wan_ifname
= nvram_safe_get("wan_ifname");
1093 fprintf(fp
, "router rip\n");
1094 if(strcmp(lan_ifname
,"")!=0)
1095 fprintf(fp
, "network %s\n", lan_ifname
);
1096 if(strcmp(lan1_ifname
,"")!=0)
1097 fprintf(fp
, "network %s\n", lan1_ifname
);
1098 if(strcmp(lan2_ifname
,"")!=0)
1099 fprintf(fp
, "network %s\n", lan2_ifname
);
1100 if(strcmp(lan3_ifname
,"")!=0)
1101 fprintf(fp
, "network %s\n", lan3_ifname
);
1102 fprintf(fp
, "network %s\n", wan_ifname
);
1103 fprintf(fp
, "redistribute connected\n");
1104 //fprintf(fp, "redistribute static\n");
1106 // 43011: modify by zg 2006.10.18 for cdrouter3.3 item 173(cdrouter_rip_30) bug
1107 // fprintf(fp, "redistribute kernel\n"); // 1.11: removed, redistributes indirect -- zzz
1109 if(strcmp(lan_ifname
,"")!=0) {
1110 fprintf(fp
, "interface %s\n", lan_ifname
);
1111 if (*lan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan_tx
);
1112 if (*lan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan_rx
);
1114 if(strcmp(lan1_ifname
,"")!=0) {
1115 fprintf(fp
, "interface %s\n", lan1_ifname
);
1116 if (*lan1_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan1_tx
);
1117 if (*lan1_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan1_rx
);
1119 if(strcmp(lan2_ifname
,"")!=0) {
1120 fprintf(fp
, "interface %s\n", lan2_ifname
);
1121 if (*lan2_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan2_tx
);
1122 if (*lan2_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan2_rx
);
1124 if(strcmp(lan3_ifname
,"")!=0) {
1125 fprintf(fp
, "interface %s\n", lan3_ifname
);
1126 if (*lan3_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", lan3_tx
);
1127 if (*lan3_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", lan3_rx
);
1129 fprintf(fp
, "interface %s\n", wan_ifname
);
1130 if (*wan_tx
!= '0') fprintf(fp
, "ip rip send version %s\n", wan_tx
);
1131 if (*wan_rx
!= '0') fprintf(fp
, "ip rip receive version %s\n", wan_rx
);
1133 fprintf(fp
, "router rip\n");
1134 if(strcmp(lan_ifname
,"")!=0) {
1135 if (*lan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan_ifname
);
1136 if (*lan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan_ifname
);
1138 if(strcmp(lan1_ifname
,"")!=0) {
1139 if (*lan1_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan1_ifname
);
1140 if (*lan1_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan1_ifname
);
1142 if(strcmp(lan2_ifname
,"")!=0) {
1143 if (*lan2_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan2_ifname
);
1144 if (*lan2_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan2_ifname
);
1146 if(strcmp(lan3_ifname
,"")!=0) {
1147 if (*lan3_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", lan3_ifname
);
1148 if (*lan3_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", lan3_ifname
);
1150 if (*wan_tx
== '0') fprintf(fp
, "distribute-list private out %s\n", wan_ifname
);
1151 if (*wan_rx
== '0') fprintf(fp
, "distribute-list private in %s\n", wan_ifname
);
1152 fprintf(fp
, "access-list private deny any\n");
1154 //fprintf(fp, "debug rip events\n");
1155 //fprintf(fp, "log file /etc/ripd.log\n");
1159 xstart("zebra", "-d");
1160 xstart("ripd", "-d");
1164 void stop_zebra(void)
1166 #ifdef TCONFIG_ZEBRA
1167 if (getpid() != 1) {
1168 stop_service("zebra");
1172 killall("zebra", SIGTERM
);
1173 killall("ripd", SIGTERM
);
1175 unlink("/etc/zebra.conf");
1176 unlink("/etc/ripd.conf");
1180 // -----------------------------------------------------------------------------
1182 void start_syslog(void)
1192 char *rot_siz
= "50";
1193 char *rot_keep
= "1";
1194 char *log_file_path
;
1196 argv
[0] = "syslogd";
1199 if (nvram_match("log_remote", "1")) {
1200 nv
= nvram_safe_get("log_remoteip");
1202 snprintf(rem
, sizeof(rem
), "%s:%s", nv
, nvram_safe_get("log_remoteport"));
1203 argv
[argc
++] = "-R";
1208 if (nvram_match("log_file", "1")) {
1209 argv
[argc
++] = "-L";
1211 if (strcmp(nvram_safe_get("log_file_size"), "") != 0) {
1212 rot_siz
= nvram_safe_get("log_file_size");
1214 if (nvram_get_int("log_file_size") > 0) {
1215 rot_keep
= nvram_safe_get("log_file_keep");
1218 // log to custom path - shibby
1219 if (nvram_match("log_file_custom", "1")) {
1220 log_file_path
= nvram_safe_get("log_file_path");
1221 argv
[argc
++] = "-s";
1222 argv
[argc
++] = rot_siz
;
1223 argv
[argc
++] = "-O";
1224 argv
[argc
++] = log_file_path
;
1225 if (strcmp(nvram_safe_get("log_file_path"), "/var/log/messages") != 0) {
1226 remove("/var/log/messages");
1227 symlink(log_file_path
, "/var/log/messages");
1232 /* Read options: rotate_size(kb) num_backups logfilename.
1233 * Ignore these settings and use defaults if the logfile cannot be written to.
1235 if (f_read_string("/etc/syslogd.cfg", cfg
, sizeof(cfg
)) > 0) {
1236 if ((nv
= strchr(cfg
, '\n')))
1239 if ((nv
= strtok(cfg
, " \t"))) {
1244 if ((nv
= strtok(NULL
, " \t")))
1247 if ((nv
= strtok(NULL
, " \t")) && *nv
== '/') {
1248 if (f_write(nv
, cfg
, 0, FW_APPEND
, 0) >= 0) {
1249 argv
[argc
++] = "-O";
1259 if (nvram_match("log_file_custom", "0")) {
1260 argv
[argc
++] = "-s";
1261 argv
[argc
++] = rot_siz
;
1263 if (lstat("/var/log/messages", &sb
) != -1)
1264 if (S_ISLNK(sb
.st_mode
))
1265 remove("/var/log/messages");
1268 if (isdigit(*b_opt
)) {
1269 argv
[argc
++] = "-b";
1270 argv
[argc
++] = b_opt
;
1272 if (nvram_get_int("log_file_size") > 0) {
1273 argv
[argc
++] = "-b";
1274 argv
[argc
++] = rot_keep
;
1280 _eval(argv
, NULL
, 0, NULL
);
1284 _eval(argv
, NULL
, 0, NULL
);
1286 // used to be available in syslogd -m
1287 n
= nvram_get_int("log_mark");
1291 sprintf(rem
, "*/%d * * * *", n
);
1292 else if (n
< 60 * 24)
1293 sprintf(rem
, "0 */%d * * *", n
/ 60);
1295 sprintf(rem
, "0 0 */%d * *", n
/ (60 * 24));
1296 sprintf(s
, "%s logger -p syslog.info -- -- MARK --", rem
);
1297 eval("cru", "a", "syslogdmark", s
);
1300 eval("cru", "d", "syslogdmark");
1305 void stop_syslog(void)
1307 killall("klogd", SIGTERM
);
1308 killall("syslogd", SIGTERM
);
1311 // -----------------------------------------------------------------------------
1313 static pid_t pid_igmp
= -1;
1315 void start_igmp_proxy(void)
1320 if (nvram_match("multicast_pass", "1")) {
1321 if (get_wan_proto() == WP_DISABLED
)
1324 if (f_exists("/etc/igmp.alt")) {
1325 eval("igmpproxy", "/etc/igmp.alt");
1327 else if ((fp
= fopen("/etc/igmp.conf", "w")) != NULL
) {
1330 "phyint %s upstream\n"
1332 // "phyint %s downstream ratelimit 0\n",
1334 nvram_get("multicast_altnet") ? : "0.0.0.0/0");
1335 // nvram_safe_get("lan_ifname"));
1337 char lanN_ifname
[] = "lanXX_ifname";
1338 char multicast_lanN
[] = "multicast_lanXX";
1341 for(br
=0 ; br
<4 ; br
++) {
1342 char bridge
[2] = "0";
1348 sprintf(lanN_ifname
, "lan%s_ifname", bridge
);
1349 sprintf(multicast_lanN
, "multicast_lan%s", bridge
);
1351 if((strcmp(nvram_safe_get(multicast_lanN
),"1")==0) && (strcmp(nvram_safe_get(lanN_ifname
),"")!=0)) {
1353 "phyint %s downstream ratelimit 0\n",
1354 nvram_safe_get(lanN_ifname
));
1358 eval("igmpproxy", "/etc/igmp.conf");
1363 if (!nvram_contains_word("debug_norestart", "igmprt")) {
1369 void stop_igmp_proxy(void)
1372 killall_tk("igmpproxy");
1375 // -----------------------------------------------------------------------------
1377 void start_udpxy(void)
1379 if (nvram_match("udpxy_enable", "1")) {
1380 if (get_wan_proto() == WP_DISABLED
)
1382 eval("udpxy", (nvram_get_int("udpxy_stats") ? "-S" : ""), "-p", nvram_safe_get("udpxy_port"), "-c", nvram_safe_get("udpxy_clients"), "-m", nvram_safe_get("wan_ifname") );
1386 void stop_udpxy(void)
1388 killall_tk("udpxy");
1391 // -----------------------------------------------------------------------------
1393 #ifdef TCONFIG_NOCAT
1395 static pid_t pid_splashd
= -1;
1396 void start_splashd(void)
1400 if (!nvram_contains_word("debug_norestart", "splashd")) {
1405 void stop_splashd(void)
1413 // -----------------------------------------------------------------------------
1417 f_write_string("/etc/TZ", nvram_safe_get("tm_tz"), FW_CREATE
|FW_NEWLINE
, 0644);
1420 void start_ntpc(void)
1426 if (nvram_get_int("ntp_updates") >= 0) {
1427 xstart("ntpsync", "--init");
1431 void stop_ntpc(void)
1433 killall("ntpsync", SIGTERM
);
1436 // -----------------------------------------------------------------------------
1438 static void stop_rstats(void)
1448 while ((n
-- > 0) && ((pid
= pidof("rstats")) > 0)) {
1450 pidz
= pidof("gzip");
1451 if (pidz
< 1) pidz
= pidof("cp");
1452 ppidz
= ppid(ppid(pidz
));
1453 if ((m
> 0) && (pidz
> 0) && (pid
== ppidz
)) {
1454 syslog(LOG_DEBUG
, "rstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid
, pidz
, ppidz
);
1461 if ((w
== 1) && (n
> 0))
1462 syslog(LOG_DEBUG
, "rstats stopped.\n");
1465 static void start_rstats(int new)
1467 if (nvram_match("rstats_enable", "1")) {
1470 syslog(LOG_DEBUG
, "starting rstats (new datafile).\n");
1471 xstart("rstats", "--new");
1473 syslog(LOG_DEBUG
, "starting rstats.\n");
1479 static void stop_cstats(void)
1489 while ((n
-- > 0) && ((pid
= pidof("cstats")) > 0)) {
1491 pidz
= pidof("gzip");
1492 if (pidz
< 1) pidz
= pidof("cp");
1493 ppidz
= ppid(ppid(pidz
));
1494 if ((m
> 0) && (pidz
> 0) && (pid
== ppidz
)) {
1495 syslog(LOG_DEBUG
, "cstats(PID %d) shutting down, waiting for helper process to complete(PID %d, PPID %d).\n", pid
, pidz
, ppidz
);
1502 if ((w
== 1) && (n
> 0))
1503 syslog(LOG_DEBUG
, "cstats stopped.\n");
1506 static void start_cstats(int new)
1508 if (nvram_match("cstats_enable", "1")) {
1511 syslog(LOG_DEBUG
, "starting cstats (new datafile).\n");
1512 xstart("cstats", "--new");
1514 syslog(LOG_DEBUG
, "starting cstats.\n");
1520 // -----------------------------------------------------------------------------
1522 // !!TB - FTP Server
1525 static char *get_full_storage_path(char *val
)
1527 static char buf
[128];
1531 len
= sprintf(buf
, "%s", val
);
1533 len
= sprintf(buf
, "%s/%s", MOUNT_ROOT
, val
);
1535 if (len
> 1 && buf
[len
- 1] == '/')
1541 static char *nvram_storage_path(char *var
)
1543 char *val
= nvram_safe_get(var
);
1544 return get_full_storage_path(val
);
1547 char vsftpd_conf
[] = "/etc/vsftpd.conf";
1548 char vsftpd_users
[] = "/etc/vsftpd.users";
1549 char vsftpd_passwd
[] = "/etc/vsftpd.passwd";
1551 /* VSFTPD code mostly stolen from Oleg's ASUS Custom Firmware GPL sources */
1553 static void start_ftpd(void)
1560 char *user
, *pass
, *rights
, *root_dir
;
1562 if (getpid() != 1) {
1563 start_service("ftpd");
1567 if (!nvram_get_int("ftp_enable")) return;
1569 mkdir_if_none(vsftpd_users
);
1570 mkdir_if_none("/var/run/vsftpd");
1572 if ((fp
= fopen(vsftpd_conf
, "w")) == NULL
)
1575 if (nvram_get_int("ftp_super"))
1578 sprintf(tmp
, "%s/%s", vsftpd_users
, "admin");
1579 if ((f
= fopen(tmp
, "w")))
1582 "dirlist_enable=yes\n"
1583 "write_enable=yes\n"
1584 "download_enable=yes\n");
1589 #ifdef TCONFIG_SAMBASRV
1590 if (nvram_match("smbd_cset", "utf8"))
1591 fprintf(fp
, "utf8=yes\n");
1594 if (nvram_invmatch("ftp_anonymous", "0"))
1597 "anon_allow_writable_root=yes\n"
1598 "anon_world_readable_only=no\n"
1599 "anon_umask=022\n");
1602 sprintf(tmp
, "%s/ftp", vsftpd_users
);
1603 if ((f
= fopen(tmp
, "w")))
1605 if (nvram_match("ftp_dirlist", "0"))
1606 fprintf(f
, "dirlist_enable=yes\n");
1607 if (nvram_match("ftp_anonymous", "1") ||
1608 nvram_match("ftp_anonymous", "3"))
1609 fprintf(f
, "write_enable=yes\n");
1610 if (nvram_match("ftp_anonymous", "1") ||
1611 nvram_match("ftp_anonymous", "2"))
1612 fprintf(f
, "download_enable=yes\n");
1615 if (nvram_match("ftp_anonymous", "1") ||
1616 nvram_match("ftp_anonymous", "3"))
1618 "anon_upload_enable=yes\n"
1619 "anon_mkdir_write_enable=yes\n"
1620 "anon_other_write_enable=yes\n");
1622 fprintf(fp
, "anonymous_enable=no\n");
1626 "dirmessage_enable=yes\n"
1627 "download_enable=no\n"
1628 "dirlist_enable=no\n"
1630 "syslog_enable=yes\n"
1631 "local_enable=yes\n"
1634 "chroot_local_user=yes\n"
1636 "log_ftp_protocol=%s\n"
1637 "user_config_dir=%s\n"
1645 "max_login_fails=1\n"
1646 "idle_session_timeout=%s\n"
1648 "anon_max_rate=%d\n"
1649 "local_max_rate=%d\n"
1651 nvram_get_int("log_ftp") ? "yes" : "no",
1652 vsftpd_users
, vsftpd_passwd
,
1654 ipv6_enabled() ? "_ipv6" : "",
1658 nvram_get("ftp_port") ? : "21",
1659 nvram_get_int("ftp_max"),
1660 nvram_get_int("ftp_ipmax"),
1661 nvram_get("ftp_staytimeout") ? : "300",
1662 nvram_get_int("ftp_anonrate") * 1024,
1663 nvram_get_int("ftp_rate") * 1024,
1664 nvram_safe_get("ftp_custom"));
1668 /* prepare passwd file and default users */
1669 if ((fp
= fopen(vsftpd_passwd
, "w")) == NULL
)
1672 if (((user
= nvram_get("http_username")) == NULL
) || (*user
== 0)) user
= "admin";
1673 if (((pass
= nvram_get("http_passwd")) == NULL
) || (*pass
== 0)) pass
= "admin";
1675 fprintf(fp
, /* anonymous, admin, nobody */
1676 "ftp:x:0:0:ftp:%s:/sbin/nologin\n"
1677 "%s:%s:0:0:root:/:/sbin/nologin\n"
1678 "nobody:x:65534:65534:nobody:%s/:/sbin/nologin\n",
1679 nvram_storage_path("ftp_anonroot"), user
,
1680 nvram_get_int("ftp_super") ? crypt(pass
, "$1$") : "x",
1683 if ((buf
= strdup(nvram_safe_get("ftp_users"))) != NULL
)
1686 username<password<rights[<root_dir>]
1694 while ((q
= strsep(&p
, ">")) != NULL
) {
1695 i
= vstrsep(q
, "<", &user
, &pass
, &rights
, &root_dir
);
1696 if (i
< 3 || i
> 4) continue;
1697 if (!user
|| !pass
) continue;
1699 if (i
== 3 || !root_dir
|| !(*root_dir
))
1701 root_dir
= nvram_safe_get("ftp_pubroot");
1704 if (strncmp(rights
, "Private", 7) == 0)
1706 sprintf(tmp
, "%s/%s", nvram_storage_path("ftp_pvtroot"), user
);
1710 sprintf(tmp
, "%s", get_full_storage_path(root_dir
));
1712 fprintf(fp
, "%s:%s:0:0:%s:%s:/sbin/nologin\n",
1713 user
, crypt(pass
, "$1$"), user
, tmp
);
1716 sprintf(tmp
, "%s/%s", vsftpd_users
, user
);
1717 if ((f
= fopen(tmp
, "w")))
1720 if (nvram_invmatch("ftp_dirlist", "1"))
1721 strcat(tmp
, "dirlist_enable=yes\n");
1722 if (strstr(rights
, "Read") || !strcmp(rights
, "Private"))
1723 strcat(tmp
, "download_enable=yes\n");
1724 if (strstr(rights
, "Write") || !strncmp(rights
, "Private", 7))
1725 strcat(tmp
, "write_enable=yes\n");
1735 killall("vsftpd", SIGHUP
);
1737 /* start vsftpd if it's not already running */
1738 if (pidof("vsftpd") <= 0)
1742 static void stop_ftpd(void)
1744 if (getpid() != 1) {
1745 stop_service("ftpd");
1749 killall_tk("vsftpd");
1750 unlink(vsftpd_passwd
);
1751 unlink(vsftpd_conf
);
1752 eval("rm", "-rf", vsftpd_users
);
1754 #endif // TCONFIG_FTP
1756 // -----------------------------------------------------------------------------
1760 #ifdef TCONFIG_SAMBASRV
1761 static void kill_samba(int sig
)
1763 if (sig
== SIGTERM
) {
1768 killall("smbd", sig
);
1769 killall("nmbd", sig
);
1773 static void start_samba(void)
1782 if (getpid() != 1) {
1783 start_service("smbd");
1787 mode
= nvram_get_int("smbd_enable");
1788 if (!mode
|| !nvram_invmatch("lan_hostname", ""))
1791 if ((fp
= fopen("/etc/smb.conf", "w")) == NULL
)
1794 fprintf(fp
, "[global]\n"
1795 " interfaces = %s\n"
1796 " bind interfaces only = yes\n"
1798 " netbios name = %s\n"
1799 " server string = %s\n"
1800 " guest account = nobody\n"
1801 " security = user\n"
1804 " guest only = no\n"
1805 " browseable = yes\n"
1806 " syslog only = yes\n"
1807 " timestamp logs = no\n"
1809 " encrypt passwords = yes\n"
1810 " preserve case = yes\n"
1811 " short preserve case = yes\n",
1812 nvram_safe_get("lan_ifname"),
1813 nvram_get("smbd_wgroup") ? : "WORKGROUP",
1814 nvram_safe_get("lan_hostname"),
1815 nvram_get("router_name") ? : "Tomato",
1816 mode
== 2 ? "" : "map to guest = Bad User",
1817 mode
== 2 ? "no" : "yes" // guest ok
1820 if (nvram_get_int("smbd_wins")) {
1821 nv
= nvram_safe_get("wan_wins");
1822 if ((*nv
== 0) || (strcmp(nv
, "0.0.0.0") == 0)) {
1823 fprintf(fp
, " wins support = yes\n");
1827 if (nvram_get_int("smbd_master")) {
1829 " domain master = yes\n"
1830 " local master = yes\n"
1831 " preferred master = yes\n"
1832 " os level = 65\n");
1835 nv
= nvram_safe_get("smbd_cpage");
1837 #ifndef TCONFIG_SAMBA3
1838 fprintf(fp
, " client code page = %s\n", nv
);
1840 sprintf(nlsmod
, "nls_cp%s", nv
);
1842 nv
= nvram_safe_get("smbd_nlsmod");
1843 if ((*nv
) && (strcmp(nv
, nlsmod
) != 0))
1847 nvram_set("smbd_nlsmod", nlsmod
);
1850 #ifndef TCONFIG_SAMBA3
1851 if (nvram_match("smbd_cset", "utf8"))
1852 fprintf(fp
, " coding system = utf8\n");
1853 else if (nvram_invmatch("smbd_cset", ""))
1854 fprintf(fp
, " character set = %s\n", nvram_safe_get("smbd_cset"));
1857 nv
= nvram_safe_get("smbd_custom");
1858 /* add socket options unless overriden by the user */
1859 if (strstr(nv
, "socket options") == NULL
) {
1860 fprintf(fp
, " socket options = TCP_NODELAY SO_KEEPALIVE IPTOS_LOWDELAY SO_RCVBUF=65536 SO_SNDBUF=65536\n");
1862 fprintf(fp
, "%s\n\n", nv
);
1864 /* configure shares */
1868 char *name
, *path
, *comment
, *writeable
, *hidden
;
1871 if ((buf
= strdup(nvram_safe_get("smbd_shares"))) != NULL
)
1873 /* sharename<path<comment<writeable[0|1]<hidden[0|1] */
1876 while ((q
= strsep(&p
, ">")) != NULL
) {
1877 if (vstrsep(q
, "<", &name
, &path
, &comment
, &writeable
, &hidden
) != 5) continue;
1878 if (!path
|| !name
) continue;
1881 fprintf(fp
, "\n[%s]\n", name
);
1884 fprintf(fp
, " path = %s\n", path
);
1887 if (!strcmp(writeable
, "1"))
1888 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1889 if (!strcmp(hidden
, "1"))
1890 fprintf(fp
, " browseable = no\n");
1894 fprintf(fp
, " comment = %s\n", comment
);
1901 /* Share every mountpoint below MOUNT_ROOT */
1902 if (nvram_get_int("smbd_autoshare") && (dir
= opendir(MOUNT_ROOT
))) {
1903 while ((dp
= readdir(dir
))) {
1904 if (strcmp(dp
->d_name
, ".") && strcmp(dp
->d_name
, "..")) {
1906 /* Only if is a directory and is mounted */
1907 if (!dir_is_mountpoint(MOUNT_ROOT
, dp
->d_name
))
1910 /* smbd_autoshare: 0 - disable, 1 - read-only, 2 - writable, 3 - hidden writable */
1911 fprintf(fp
, "\n[%s]\n path = %s/%s\n comment = %s\n",
1912 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
, dp
->d_name
);
1913 if (nvram_match("smbd_autoshare", "3")) // Hidden
1914 fprintf(fp
, "\n[%s$]\n path = %s/%s\n browseable = no\n",
1915 dp
->d_name
, MOUNT_ROOT
, dp
->d_name
);
1916 if (nvram_match("smbd_autoshare", "2") || nvram_match("smbd_autoshare", "3")) // RW
1917 fprintf(fp
, " writable = yes\n delete readonly = yes\n force user = root\n");
1923 if (dir
) closedir(dir
);
1926 /* by default share MOUNT_ROOT as read-only */
1927 fprintf(fp
, "\n[share]\n"
1935 mkdir_if_none("/var/run/samba");
1936 mkdir_if_none("/etc/samba");
1938 /* write smbpasswd */
1939 #ifdef TCONFIG_SAMBA3
1940 eval("smbpasswd", "nobody", "\"\"");
1942 eval("smbpasswd", "-a", "nobody", "\"\"");
1946 if (((smbd_user
= nvram_get("smbd_user")) == NULL
) || (*smbd_user
== 0) || !strcmp(smbd_user
, "root"))
1948 #ifdef TCONFIG_SAMBA3
1949 eval("smbpasswd", smbd_user
, nvram_safe_get("smbd_passwd"));
1951 eval("smbpasswd", "-a", smbd_user
, nvram_safe_get("smbd_passwd"));
1956 int ret1
= 0, ret2
= 0;
1957 /* start samba if it's not already running */
1958 if (pidof("nmbd") <= 0)
1959 ret1
= xstart("nmbd", "-D");
1960 if (pidof("smbd") <= 0)
1961 ret2
= xstart("smbd", "-D");
1963 if (ret1
|| ret2
) kill_samba(SIGTERM
);
1966 static void stop_samba(void)
1968 if (getpid() != 1) {
1969 stop_service("smbd");
1973 kill_samba(SIGTERM
);
1975 unlink("/var/log/smb");
1976 unlink("/var/log/nmb");
1977 eval("rm", "-rf", "/var/run/samba");
1979 #endif // TCONFIG_SAMBASRV
1981 #ifdef TCONFIG_MEDIA_SERVER
1982 #define MEDIA_SERVER_APP "minidlna"
1984 static void start_media_server(void)
1987 int port
, pid
, https
;
1989 char *argv
[] = { MEDIA_SERVER_APP
, "-f", "/etc/"MEDIA_SERVER_APP
".conf", "-R", NULL
};
1990 static int once
= 1;
1992 if (getpid() != 1) {
1993 start_service("media");
1997 if (nvram_get_int("ms_sas") == 0)
2000 if (nvram_get_int("ms_enable") != 0) {
2001 if ((!once
) && (nvram_get_int("ms_rescan") == 0)) {
2005 nvram_unset("ms_rescan");
2007 if (f_exists("/etc/"MEDIA_SERVER_APP
".alt")) {
2008 argv
[2] = "/etc/"MEDIA_SERVER_APP
".alt";
2011 if ((f
= fopen(argv
[2], "w")) != NULL
) {
2012 port
= nvram_get_int("ms_port");
2013 https
= nvram_get_int("https_enable");
2014 dbdir
= nvram_safe_get("ms_dbdir");
2015 if (!(*dbdir
)) dbdir
= NULL
;
2016 mkdir_if_none(dbdir
? : "/var/run/"MEDIA_SERVER_APP
);
2019 "network_interface=%s\n"
2021 "friendly_name=%s\n"
2025 "presentation_url=http%s://%s:%s/nas-media.asp\n"
2027 "notify_interval=600\n"
2028 "album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg\n"
2030 nvram_safe_get("lan_ifname"),
2031 (port
< 0) || (port
>= 0xffff) ? 0 : port
,
2032 nvram_get("router_name") ? : "Tomato",
2033 dbdir
? : "/var/run/"MEDIA_SERVER_APP
,
2034 nvram_get_int("ms_tivo") ? "yes" : "no",
2035 nvram_get_int("ms_stdlna") ? "yes" : "no",
2036 https
? "s" : "", nvram_safe_get("lan_ipaddr"), nvram_safe_get(https
? "https_lanport" : "http_lanport")
2039 // media directories
2041 char *path
, *restrict
;
2043 if ((buf
= strdup(nvram_safe_get("ms_dirs"))) != NULL
) {
2044 /* path<restrict[A|V|P|] */
2047 while ((q
= strsep(&p
, ">")) != NULL
) {
2048 if (vstrsep(q
, "<", &path
, &restrict
) < 1 || !path
|| !(*path
))
2050 fprintf(f
, "media_dir=%s%s%s\n",
2051 restrict
? : "", (restrict
&& *restrict
) ? "," : "", path
);
2060 /* start media server if it's not already running */
2061 if (pidof(MEDIA_SERVER_APP
) <= 0) {
2062 if ((_eval(argv
, NULL
, 0, &pid
) == 0) && (once
)) {
2063 /* If we started the media server successfully, wait 1 sec
2064 * to let it die if it can't open the database file.
2065 * If it's still alive after that, assume it's running and
2066 * disable forced once-after-reboot rescan.
2069 if (pidof(MEDIA_SERVER_APP
) > 0)
2076 static void stop_media_server(void)
2078 if (getpid() != 1) {
2079 stop_service("media");
2083 killall_tk(MEDIA_SERVER_APP
);
2085 #endif // TCONFIG_MEDIA_SERVER
2088 static void start_nas_services(void)
2090 if (getpid() != 1) {
2091 start_service("usbapps");
2095 #ifdef TCONFIG_SAMBASRV
2101 #ifdef TCONFIG_MEDIA_SERVER
2102 start_media_server();
2106 static void stop_nas_services(void)
2108 if (getpid() != 1) {
2109 stop_service("usbapps");
2113 #ifdef TCONFIG_MEDIA_SERVER
2114 stop_media_server();
2119 #ifdef TCONFIG_SAMBASRV
2124 void restart_nas_services(int stop
, int start
)
2126 int fd
= file_lock("usb");
2127 /* restart all NAS applications */
2129 stop_nas_services();
2131 start_nas_services();
2134 #endif // TCONFIG_USB
2136 // -----------------------------------------------------------------------------
2138 /* -1 = Don't check for this program, it is not expected to be running.
2139 * Other = This program has been started and should be kept running. If no
2140 * process with the name is running, call func to restart it.
2141 * Note: At startup, dnsmasq forks a short-lived child which forks a
2142 * long-lived (grand)child. The parents terminate.
2143 * Many daemons use this technique.
2145 static void _check(pid_t pid
, const char *name
, void (*func
)(void))
2147 if (pid
== -1) return;
2149 if (pidof(name
) > 0) return;
2151 syslog(LOG_DEBUG
, "%s terminated unexpectedly, restarting.\n", name
);
2154 // Force recheck in 500 msec
2155 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
2158 void check_services(void)
2160 TRACE_PT("keep alive\n");
2162 // Periodically reap any zombies
2163 setitimer(ITIMER_REAL
, &zombie_tv
, NULL
);
2166 _check(pid_hotplug2
, "hotplug2", start_hotplug2
);
2168 _check(pid_dnsmasq
, "dnsmasq", start_dnsmasq
);
2169 _check(pid_crond
, "crond", start_cron
);
2170 _check(pid_igmp
, "igmpproxy", start_igmp_proxy
);
2172 _check(pid_radvd
, "radvd", start_radvd
);
2175 //#ifdef TCONFIG_NOCAT
2176 // if (nvram_get_int("NC_enable"))
2177 // _check(&pid_splashd, "splashd", start_splashd);
2182 // -----------------------------------------------------------------------------
2184 void start_services(void)
2186 static int once
= 1;
2191 if (nvram_get_int("telnetd_eas")) start_telnetd();
2192 if (nvram_get_int("sshd_eas")) start_sshd();
2209 #ifdef TCONFIG_PPTPD
2213 /* note: starting radvd here might be too early in case of
2214 * DHCPv6 or 6to4 because we won't have received a prefix and
2215 * so it will disable advertisements. To restart them, we have
2216 * to send radvd a SIGHUP, or restart it.
2220 restart_nas_services(1, 1); // !!TB - Samba, FTP and Media Server
2236 #ifdef TCONFIG_NOCAT
2245 void stop_services(void)
2253 #ifdef TCONFIG_NOCAT
2270 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2274 #ifdef TCONFIG_PPTPD
2293 // -----------------------------------------------------------------------------
2295 /* nvram "action_service" is: "service-action[-modifier]"
2296 * action is something like "stop" or "start" or "restart"
2297 * optional modifier is "c" for the "service" command-line command
2299 void exec_service(void)
2301 const int A_START
= 1;
2302 const int A_STOP
= 2;
2303 const int A_RESTART
= 1|2;
2312 strlcpy(buffer
, nvram_safe_get("action_service"), sizeof(buffer
));
2316 act
= strsep(&next
, ",");
2317 service
= strsep(&act
, "-");
2323 strsep(&modifier
, "-");
2325 TRACE_PT("service=%s action=%s modifier=%s\n", service
, act
, modifier
? : "");
2327 if (strcmp(act
, "start") == 0) action
= A_START
;
2328 else if (strcmp(act
, "stop") == 0) action
= A_STOP
;
2329 else if (strcmp(act
, "restart") == 0) action
= A_RESTART
;
2331 user
= (modifier
!= NULL
&& *modifier
== 'c');
2333 if (strcmp(service
, "dhcpc") == 0) {
2334 if (action
& A_STOP
) stop_dhcpc();
2335 if (action
& A_START
) start_dhcpc();
2339 if ((strcmp(service
, "dhcpd") == 0) || (strcmp(service
, "dns") == 0) || (strcmp(service
, "dnsmasq") == 0)) {
2340 if (action
& A_STOP
) stop_dnsmasq();
2341 if (action
& A_START
) {
2348 if (strcmp(service
, "firewall") == 0) {
2349 if (action
& A_STOP
) {
2354 if (action
& A_START
) {
2362 if (strcmp(service
, "restrict") == 0) {
2363 if (action
& A_STOP
) {
2366 if (action
& A_START
) {
2367 i
= nvram_get_int("rrules_radio"); // -1 = not used, 0 = enabled by rule, 1 = disabled by rule
2371 // if radio was disabled by access restriction, but no rule is handling it now, enable it
2373 if (nvram_get_int("rrules_radio") < 0) {
2374 eval("radio", "on");
2381 if (strcmp(service
, "arpbind") == 0) {
2382 if (action
& A_STOP
) stop_arpbind();
2383 if (action
& A_START
) start_arpbind();
2387 if (strcmp(service
, "qos") == 0) {
2388 if (action
& A_STOP
) {
2391 stop_firewall(); start_firewall(); // always restarted
2392 if (action
& A_START
) {
2394 if (nvram_match("qos_reset", "1")) f_write_string("/proc/net/clear_marks", "1", 0, 0);
2399 if (strcmp(service
, "qoslimit") == 0) {
2400 if (action
& A_STOP
) {
2401 new_qoslimit_stop();
2403 #ifdef TCONFIG_NOCAT
2406 stop_firewall(); start_firewall(); // always restarted
2407 if (action
& A_START
) {
2408 new_qoslimit_start();
2410 #ifdef TCONFIG_NOCAT
2416 if (strcmp(service
, "upnp") == 0) {
2417 if (action
& A_STOP
) {
2420 stop_firewall(); start_firewall(); // always restarted
2421 if (action
& A_START
) {
2427 if (strcmp(service
, "telnetd") == 0) {
2428 if (action
& A_STOP
) stop_telnetd();
2429 if (action
& A_START
) start_telnetd();
2433 if (strcmp(service
, "sshd") == 0) {
2434 if (action
& A_STOP
) stop_sshd();
2435 if (action
& A_START
) start_sshd();
2439 if (strcmp(service
, "httpd") == 0) {
2440 if (action
& A_STOP
) stop_httpd();
2441 if (action
& A_START
) start_httpd();
2446 if (strcmp(service
, "ipv6") == 0) {
2447 if (action
& A_STOP
) {
2451 if (action
& A_START
) {
2458 if (strcmp(service
, "radvd") == 0) {
2459 if (action
& A_STOP
) {
2462 if (action
& A_START
) {
2468 if (strncmp(service
, "dhcp6", 5) == 0) {
2469 if (action
& A_STOP
) {
2472 if (action
& A_START
) {
2479 if (strcmp(service
, "admin") == 0) {
2480 if (action
& A_STOP
) {
2485 stop_firewall(); start_firewall(); // always restarted
2486 if (action
& A_START
) {
2489 if (nvram_match("telnetd_eas", "1")) start_telnetd();
2490 if (nvram_match("sshd_eas", "1")) start_sshd();
2495 if (strcmp(service
, "ddns") == 0) {
2496 if (action
& A_STOP
) stop_ddns();
2497 if (action
& A_START
) start_ddns();
2501 if (strcmp(service
, "ntpc") == 0) {
2502 if (action
& A_STOP
) stop_ntpc();
2503 if (action
& A_START
) start_ntpc();
2507 if (strcmp(service
, "logging") == 0) {
2508 if (action
& A_STOP
) {
2511 if (action
& A_START
) {
2515 // always restarted except from "service" command
2516 stop_cron(); start_cron();
2517 stop_firewall(); start_firewall();
2522 if (strcmp(service
, "crond") == 0) {
2523 if (action
& A_STOP
) {
2526 if (action
& A_START
) {
2533 if (strncmp(service
, "hotplug", 7) == 0) {
2534 if (action
& A_STOP
) {
2537 if (action
& A_START
) {
2544 if (strcmp(service
, "upgrade") == 0) {
2545 if (action
& A_START
) {
2550 restart_nas_services(1, 0); // stop Samba, FTP and Media Server
2558 killall("rstats", SIGTERM
);
2559 killall("cstats", SIGTERM
);
2560 killall("buttons", SIGTERM
);
2562 remove_storage_main(1); // !!TB - USB Support
2563 stop_usb(); // !!TB - USB Support
2569 if (strcmp(service
, "cifs") == 0) {
2570 if (action
& A_STOP
) stop_cifs();
2571 if (action
& A_START
) start_cifs();
2576 #ifdef TCONFIG_JFFS2
2577 if (strncmp(service
, "jffs", 4) == 0) {
2578 if (action
& A_STOP
) stop_jffs2();
2579 if (action
& A_START
) start_jffs2();
2584 if (strcmp(service
, "zebra") == 0) {
2585 if (action
& A_STOP
) stop_zebra();
2586 if (action
& A_START
) start_zebra();
2591 if (strcmp(service
, "mmc") == 0) {
2592 if (action
& A_STOP
) stop_mmc();
2593 if (action
& A_START
) start_mmc();
2598 if (strcmp(service
, "routing") == 0) {
2599 if (action
& A_STOP
) {
2601 do_static_routes(0); // remove old '_saved'
2602 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
2603 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2604 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
2605 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2606 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
2607 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2608 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
2612 if (action
& A_START
) {
2613 do_static_routes(1); // add new
2615 eval("brctl", "stp", nvram_safe_get("lan_ifname"), nvram_safe_get("lan_stp"));
2616 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0)
2617 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), nvram_safe_get("lan1_stp"));
2618 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0)
2619 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), nvram_safe_get("lan2_stp"));
2620 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0)
2621 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), nvram_safe_get("lan3_stp"));
2626 if (strcmp(service
, "ctnf") == 0) {
2627 if (action
& A_START
) {
2635 if (strcmp(service
, "wan") == 0) {
2636 if (action
& A_STOP
) {
2640 if (action
& A_START
) {
2641 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
2649 if (strcmp(service
, "net") == 0) {
2650 if (action
& A_STOP
) {
2652 stop_nas_services();
2665 if (action
& A_START
) {
2678 start_nas_services();
2684 if (strcmp(service
, "wireless") == 0) {
2685 if(action
& A_STOP
) {
2688 if(action
& A_START
) {
2694 if (strcmp(service
, "wl") == 0) {
2695 if(action
& A_STOP
) {
2699 if(action
& A_START
) {
2708 if (strcmp(service
, "nas") == 0) {
2709 if (action
& A_STOP
) {
2712 if (action
& A_START
) {
2719 if (strcmp(service
, "rstats") == 0) {
2720 if (action
& A_STOP
) stop_rstats();
2721 if (action
& A_START
) start_rstats(0);
2725 if (strcmp(service
, "rstatsnew") == 0) {
2726 if (action
& A_STOP
) stop_rstats();
2727 if (action
& A_START
) start_rstats(1);
2731 if (strcmp(service
, "cstats") == 0) {
2732 if (action
& A_STOP
) stop_cstats();
2733 if (action
& A_START
) start_cstats(0);
2737 if (strcmp(service
, "cstatsnew") == 0) {
2738 if (action
& A_STOP
) stop_cstats();
2739 if (action
& A_START
) start_cstats(1);
2743 if (strcmp(service
, "sched") == 0) {
2744 if (action
& A_STOP
) stop_sched();
2745 if (action
& A_START
) start_sched();
2750 if (strcmp(service
, "bittorrent") == 0) {
2751 if (action
& A_STOP
) {
2754 stop_firewall(); start_firewall(); // always restarted
2755 if (action
& A_START
) {
2763 if (strcmp(service
, "nfs") == 0) {
2764 if (action
& A_STOP
) stop_nfs();
2765 if (action
& A_START
) start_nfs();
2771 if (strcmp(service
, "snmp") == 0) {
2772 if (action
& A_STOP
) stop_snmp();
2773 if (action
& A_START
) start_snmp();
2779 if (strcmp(service
, "tor") == 0) {
2780 if (action
& A_STOP
) stop_tor();
2782 stop_firewall(); start_firewall(); // always restarted
2784 if (action
& A_START
) start_tor();
2790 if (strcmp(service
, "ups") == 0) {
2791 if (action
& A_STOP
) stop_ups();
2792 if (action
& A_START
) start_ups();
2797 if (strcmp(service
, "tomatoanon") == 0) {
2798 if (action
& A_STOP
) stop_tomatoanon();
2799 if (action
& A_START
) start_tomatoanon();
2804 // !!TB - USB Support
2805 if (strcmp(service
, "usb") == 0) {
2806 if (action
& A_STOP
) stop_usb();
2807 if (action
& A_START
) {
2809 // restart Samba and ftp since they may be killed by stop_usb()
2810 restart_nas_services(0, 1);
2811 // remount all partitions by simulating hotplug event
2812 add_remove_usbhost("-1", 1);
2817 if (strcmp(service
, "usbapps") == 0) {
2818 if (action
& A_STOP
) stop_nas_services();
2819 if (action
& A_START
) start_nas_services();
2825 // !!TB - FTP Server
2826 if (strcmp(service
, "ftpd") == 0) {
2827 if (action
& A_STOP
) stop_ftpd();
2831 if (action
& A_START
) start_ftpd();
2836 #ifdef TCONFIG_MEDIA_SERVER
2837 if (strcmp(service
, "media") == 0 || strcmp(service
, "dlna") == 0) {
2838 if (action
& A_STOP
) stop_media_server();
2839 if (action
& A_START
) start_media_server();
2844 #ifdef TCONFIG_SAMBASRV
2846 if (strcmp(service
, "samba") == 0 || strcmp(service
, "smbd") == 0) {
2847 if (action
& A_STOP
) stop_samba();
2848 if (action
& A_START
) {
2858 #ifdef TCONFIG_OPENVPN
2859 if (strncmp(service
, "vpnclient", 9) == 0) {
2860 if (action
& A_STOP
) stop_vpnclient(atoi(&service
[9]));
2861 if (action
& A_START
) start_vpnclient(atoi(&service
[9]));
2865 if (strncmp(service
, "vpnserver", 9) == 0) {
2866 if (action
& A_STOP
) stop_vpnserver(atoi(&service
[9]));
2867 if (action
& A_START
) start_vpnserver(atoi(&service
[9]));
2872 #ifdef TCONFIG_NOCAT
2873 if (strcmp(service
, "splashd") == 0) {
2874 if (action
& A_STOP
) stop_splashd();
2875 if (action
& A_START
) start_splashd();
2880 #ifdef TCONFIG_PPTPD
2881 if (strcmp(service
, "pptpd") == 0) {
2882 if (action
& A_STOP
) stop_pptpd();
2883 if (action
& A_START
) start_pptpd();
2888 #ifdef TCONFIG_PPTPD
2889 if (strcmp(service
, "pptpclient") == 0) {
2890 if (action
& A_STOP
) stop_pptp_client();
2891 if (action
& A_START
) start_pptp_client();
2892 if (action
& (A_START
| A_STOP
))
2897 if ((action
& A_START
) == 0)
2907 // some functions check action_service and must be cleared at end -- zzz
2908 nvram_set("action_service", "");
2910 // Force recheck in 500 msec
2911 setitimer(ITIMER_REAL
, &pop_tv
, NULL
);
2914 static void do_service(const char *name
, const char *action
, int user
)
2920 while (!nvram_match("action_service", "")) {
2925 else if (--n
< 0) break;
2929 snprintf(s
, sizeof(s
), "%s-%s%s", name
, action
, (user
? "-c" : ""));
2930 nvram_set("action_service", s
);
2932 if (nvram_match("debug_rc_svc", "1")) {
2933 nvram_unset("debug_rc_svc");
2940 while (nvram_match("action_service", s
)) {
2952 int service_main(int argc
, char *argv
[])
2954 if (argc
!= 3) usage_exit(argv
[0], "<service> <action>");
2955 do_service(argv
[1], argv
[2], 1);
2956 printf("\nDone.\n");
2960 void start_service(const char *name
)
2962 do_service(name
, "start", 0);
2965 void stop_service(const char *name
)
2967 do_service(name
, "stop", 0);
2971 void restart_service(const char *name)
2973 do_service(name, "restart", 0);