source: src/router/services/services/services.c @ 12247

Last change on this file since 12247 was 12247, checked in by eko, 4 years ago

try this cron startup fix

File size: 34.9 KB
Line 
1/*
2 * services.c
3 *
4 * Copyright (C) 2006 Sebastian Gottschall <gottschall@dd-wrt.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 *
20 * $Id:
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <dirent.h>
27#include <signal.h>
28#include <unistd.h>
29#include <errno.h>
30#include <ctype.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/ioctl.h>          /* AhMan March 18 2005 */
34#include <sys/socket.h>
35#include <sys/mount.h>
36
37#include <netinet/in.h>
38#include <arpa/inet.h>
39#include <wait.h>
40#include <net/route.h>          /* AhMan March 18 2005 */
41#include <sys/types.h>
42#include <signal.h>
43
44#include <bcmnvram.h>
45#include <bcmconfig.h>
46#include <netconf.h>
47#include <shutils.h>
48#include <utils.h>
49#include <cy_conf.h>
50#include <code_pattern.h>
51#include <rc.h>
52#include <wlutils.h>
53#include <nvparse.h>
54#include <syslog.h>
55#include <services.h>
56
57#define WL_IOCTL(name, cmd, buf, len) (wl_ioctl((name), (cmd), (buf), (len)))
58
59#define TXPWR_MAX 251
60#define TXPWR_DEFAULT 70
61
62#define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
63
64/*
65 * AhMan March 18 2005
66 */
67#define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
68
69void start_force_to_dial(void);
70
71static int alreadyInHost(char *host)
72{
73        FILE *in = fopen("/tmp/hosts", "rb");
74
75        if (in == NULL)
76                return 0;
77        char buf[100];
78
79        while (1) {
80                fscanf(in, "%s", buf);
81                if (!strcmp(buf, host)) {
82                        fclose(in);
83                        return 1;
84                }
85                if (feof(in)) {
86                        fclose(in);
87                        return 0;
88                }
89        }
90}
91
92void addHost(char *host, char *ip)
93{
94        char buf[100];
95        char newhost[100];
96
97        if (host == NULL)
98                return;
99        if (ip == NULL)
100                return;
101        strcpy(newhost, host);
102        char *domain = nvram_safe_get("lan_domain");
103
104        if (domain != NULL && strlen(domain) > 0 && strcmp(host, "localhost")) {
105                sprintf(newhost, "%s.%s", host, domain);
106        } else
107                sprintf(newhost, "%s", host);
108
109        if (alreadyInHost(newhost))
110                return;
111        sysprintf("echo \"%s\t%s\">>/tmp/hosts", ip, newhost);
112}
113
114void start_vpn_modules(void)
115{
116#if defined(HAVE_XSCALE) || defined(HAVE_FONERA) || defined(HAVE_WHRAG108) || defined(HAVE_X86) ||defined(HAVE_LS2) || defined(HAVE_CA8) || defined(HAVE_TW6600)  || defined(HAVE_LS5) || defined(HAVE_RT2880)
117
118        if ((nvram_match("pptp_pass", "1") || nvram_match("l2tp_pass", "1")
119             || nvram_match("ipsec_pass", "1"))) {
120                insmod("nf_conntrack_proto_gre");
121                dd_syslog(LOG_INFO,
122                          "vpn modules : nf_conntrack_proto_gre successfully loaded\n");
123                insmod("nf_nat_proto_gre");
124                dd_syslog(LOG_INFO,
125                          "vpn modules : nf_nat_proto_gre successfully loaded\n");
126        }
127        if (nvram_match("pptp_pass", "1")) {
128                insmod("nf_conntrack_pptp");
129                dd_syslog(LOG_INFO,
130                          "vpn modules : nf_conntrack_pptp successfully loaded\n");
131                insmod("nf_nat_pptp");
132                dd_syslog(LOG_INFO,
133                          "vpn modules : nf_nat_pptp successfully loaded\n");
134        }
135#else
136        if ((nvram_match("pptp_pass", "1") || nvram_match("l2tp_pass", "1")
137             || nvram_match("ipsec_pass", "1"))) {
138                insmod("ip_conntrack_proto_gre");
139                dd_syslog(LOG_INFO,
140                          "vpn modules : ip_conntrack_proto_gre successfully loaded\n");
141                insmod("ip_nat_proto_gre");
142                dd_syslog(LOG_INFO,
143                          "vpn modules : ip_nat_proto_gre successfully loaded\n");
144        }
145        if (nvram_match("pptp_pass", "1")) {
146                insmod("ip_conntrack_pptp");
147                dd_syslog(LOG_INFO,
148                          "vpn modules : ip_conntrack_pptp successfully loaded\n");
149                insmod("ip_nat_pptp");
150                dd_syslog(LOG_INFO,
151                          "vpn modules : ip_nat_pptp successfully loaded\n");
152        }
153#endif
154}
155
156void stop_vpn_modules(void)
157{
158#if defined(HAVE_XSCALE) || defined(HAVE_FONERA) || defined(HAVE_WHRAG108) || defined(HAVE_X86) || defined(HAVE_LS2) || defined(HAVE_CA8) || defined(HAVE_TW6600)  || defined(HAVE_LS5) || defined(HAVE_RT2880)
159        rmmod("nf_nat_pptp");
160        rmmod("nf_conntrack_pptp");
161        rmmod("nf_nat_proto_gre");
162        rmmod("nf_conntrack_proto_gre");
163        dd_syslog(LOG_INFO,
164                  "vpn modules : vpn modules successfully unloaded\n");
165#else
166        rmmod("ip_nat_pptp");
167        rmmod("ip_nat_proto_gre");
168        rmmod("ip_conntrack_pptp");
169        rmmod("ip_conntrack_proto_gre");
170        dd_syslog(LOG_INFO,
171                  "vpn modules : vpn modules successfully unloaded\n");
172
173#endif
174}
175
176/*
177 * AhMan March 18 2005
178 */
179void start_tmp_ppp(int num);
180
181int write_nvram(char *name, char *nv)
182{
183        if (nvram_invmatch(nv, "")) {
184                FILE *fp = fopen(name, "wb");
185
186                if (fp) {
187                        fwritenvram(nv, fp);
188                        fprintf(fp, "\n");
189                        fclose(fp);
190                }
191        } else
192                return -1;
193        return 0;
194}
195
196int usejffs = 0;
197
198void stop_dns_clear_resolv(void)
199{
200        FILE *fp_w;
201
202        if (pidof("dnsmasq") > 0)
203                dd_syslog(LOG_INFO,
204                          "dnsmasq : dnsmasq daemon successfully stopped\n");
205        // int ret = killps("dnsmasq",NULL);
206        int ret = killall("dnsmasq", SIGTERM);
207
208        /*
209         * Save DNS to resolv.conf
210         */
211        if (!(fp_w = fopen(RESOLV_FILE, "w"))) {
212                perror(RESOLV_FILE);
213                return;
214        }
215        fprintf(fp_w, " ");
216        fclose(fp_w);
217
218        cprintf("done\n");
219        return;
220}
221
222#if 0
223void start_ntpc(void)
224{
225        char *servers = nvram_safe_get("ntp_server");
226
227        if (!nvram_invmatch("ntpd_enable", "0"))
228                return;
229
230        if (strlen(servers)) {
231                char *nas_argv[] =
232                    { "ntpclient", "-h", servers, "-i", "5", "-l", "-s", "-c",
233                        "2",
234                        NULL
235                };
236                pid_t pid;
237
238                _evalpid(nas_argv, NULL, 0, &pid);
239                dd_syslog(LOG_INFO,
240                          "ntpclient : ntp client successfully started\n");
241        }
242
243        cprintf("done\n");
244        return;
245}
246#endif
247void stop_ntpc(void)
248{
249        if (pidof("ntpclient") > 0)
250                dd_syslog(LOG_INFO,
251                          "ntpclient : ntp client successfully stopped\n");
252        int ret = killall("ntpclient", SIGTERM);
253
254        cprintf("done\n");
255        return;
256}
257
258// ///////////////////////////////////////////////////
259void start_resetbutton(void)
260{
261        int ret = 0;
262
263        ret = eval("resetbutton");
264        dd_syslog(LOG_INFO,
265                  "reset button : resetbutton daemon successfully started\n");
266
267        cprintf("done\n");
268        return;
269}
270
271void stop_resetbutton(void)
272{
273        int ret = 0;
274
275        if (pidof("resetbutton") > 0)
276                dd_syslog(LOG_INFO,
277                          "reset button : resetbutton daemon successfully stopped\n");
278        ret = killall("resetbutton", SIGKILL);
279
280        cprintf("done\n");
281        return;
282}
283
284void start_iptqueue(void)
285{
286        int ret = 0;
287
288        if (!nvram_invmatch("iptqueue_enable", "0"))
289                return;
290
291        ret = eval("iptqueue");
292        dd_syslog(LOG_INFO, "iptqueue successfully started\n");
293
294        cprintf("done\n");
295        return;
296}
297
298void stop_iptqueue(void)
299{
300        int ret = 0;
301
302        if (pidof("iptqueue") > 0)
303                dd_syslog(LOG_INFO,
304                          "iptqueue : iptqueue daemon successfully stopped\n");
305        ret = killall("iptqueue", SIGKILL);
306
307        cprintf("done\n");
308        return;
309}
310
311void start_cron(void)
312{
313        int ret = 0;
314        struct stat buf;
315        FILE *fp;
316
317        if (nvram_match("cron_enable", "0"))
318                return;
319
320        /*
321         * Create cron's database directory
322         */
323        if (stat("/var/spool", &buf) != 0) {
324                mkdir("/var/spool", 0700);
325                mkdir("/var/spool/cron", 0700);
326        }
327        mkdir("/tmp/cron.d", 0700);
328
329        buf_to_file("/tmp/cron.d/check_ps",
330                    "*/2 * * * * root /sbin/check_ps\n");
331        /*
332         * pppoe reconnect
333         */
334        unlink("/tmp/cron.d/pppoe_reconnect");   
335        if (nvram_match("reconnect_enable", "1")) {
336
337                fp = fopen("/tmp/cron.d/pppoe_reconnect", "w");
338                fprintf(fp, "%s %s * * * root /usr/bin/killall pppd\n",
339                        nvram_safe_get("reconnect_minutes"),
340                        nvram_safe_get("reconnect_hours"));
341                fclose(fp);
342        }
343        /*
344         * reboot scheduler
345         */
346        unlink("/tmp/cron.d/check_schedules");
347        if (nvram_match("schedule_enable", "1")
348            && nvram_match("schedule_hour_time", "2")) {
349
350                fp = fopen("/tmp/cron.d/check_schedules", "w");
351                fprintf(fp, "%s %s * * %s root /sbin/reboot\n",
352                        nvram_safe_get("schedule_minutes"),
353                        nvram_safe_get("schedule_hours"),
354                        nvram_safe_get("schedule_weekdays"));
355                fclose(fp);
356        }
357
358        /*
359         * Additional cron jobs
360         */
361        unlink("/tmp/cron.d/cron_jobs");
362
363        if (nvram_invmatch("cron_jobs", "")) {
364
365                fp = fopen("/tmp/cron.d/cron_jobs", "w");
366                fwritenvram("cron_jobs", fp);
367                fprintf(fp, "\n");      // extra new line at the end
368                fclose(fp);
369        }
370
371        /*
372         * Custom cron files
373         */
374        eval("cp", "-af", "/tmp/mycron.d/*", "/tmp/cron.d/");
375        eval("cp", "-af", "/jffs/mycron.d/*", "/tmp/cron.d/");
376        eval("cp", "-af", "/mmc/mycron.d/*", "/tmp/cron.d/");
377
378        cprintf("starting cron\n");
379        ret = eval("cron");
380        dd_syslog(LOG_INFO, "cron : cron daemon successfully started\n");
381
382        cprintf("done\n");
383        return;
384}
385
386void stop_cron(void)
387{
388        int ret = 0;
389
390        if (pidof("cron") > 0)
391                dd_syslog(LOG_INFO,
392                          "cron : cron daemon successfully stopped\n");
393        // ret = killps("cron","-9");
394        ret = killall("cron", SIGKILL);
395        eval("rm", "-rf", "/tmp/cron.d");
396        cprintf("done\n");
397        return;
398}
399
400#ifdef HAVE_SYSLOG
401void start_syslog(void)
402{
403        int ret1 = 0, ret2 = 0;
404
405        if (!nvram_invmatch("syslogd_enable", "0"))
406                return;
407
408        if (strlen(nvram_safe_get("syslogd_rem_ip")) > 0)
409                ret1 = eval("syslogd", "-R", nvram_safe_get("syslogd_rem_ip"));
410        else
411                ret1 = eval("syslogd", "-L");
412
413        dd_syslog(LOG_INFO, "syslogd : syslog daemon successfully started\n");
414        ret2 = eval("klogd");
415        dd_syslog(LOG_INFO, "klogd : klog daemon successfully started\n");
416
417        return;
418}
419
420void stop_syslog(void)
421{
422        int ret;
423
424        if (pidof("klogd") > 0)
425                dd_syslog(LOG_INFO,
426                          "klogd : klog daemon successfully stopped\n");
427        ret = killall("klogd", SIGKILL);
428        if (pidof("syslogd") > 0)
429                dd_syslog(LOG_INFO,
430                          "syslogd : syslog daemon successfully stopped\n");
431        ret += killall("syslogd", SIGKILL);
432
433        cprintf("done\n");
434        return;
435}
436#endif
437
438void stop_redial(void)
439{
440        int ret;
441
442        if (pidof("redial") > 0)
443                dd_syslog(LOG_INFO,
444                          "ppp_redial : redial daemon successfully stopped\n");
445        // ret = killps("redial","-9");
446        ret = killall("redial", SIGKILL);
447
448        cprintf("done\n");
449        return;
450}
451
452void start_redial(void)
453{
454        int ret;
455        pid_t pid;
456        char *redial_argv[] = { "/tmp/ppp/redial",
457                nvram_safe_get("ppp_redialperiod"),
458                NULL
459        };
460        if (pidof("redial") > 0) {
461                return;         // not required, already running
462        }
463
464        symlink("/sbin/rc", "/tmp/ppp/redial");
465
466        ret = _evalpid(redial_argv, NULL, 0, &pid);
467        dd_syslog(LOG_INFO,
468                  "ppp_redial : redial process successfully started\n");
469
470        cprintf("done\n");
471        return;
472}
473
474#ifdef HAVE_RADVD
475void start_radvd(void)
476{
477        int ret = 0;
478        int c = 0;
479        char *buf, *buf2;
480        int i;
481        FILE *fp;
482
483        if (!nvram_match("radvd_enable", "1"))
484                return;
485        if (!nvram_match("ipv6_enable", "1"))
486                return;
487        buf = nvram_safe_get("radvd_conf");
488        if (buf != NULL) {
489                buf2 = (char *)malloc(strlen(buf) + 1);
490                memcpy(buf2, buf, strlen(buf));
491                buf2[strlen(buf)] = 0;
492
493                i = 0;
494                while (buf2[i++] != 0) {
495                        cprintf(".");
496                        if (buf2[i - 1] == '\r')
497                                continue;
498                        buf2[c++] = buf2[i - 1];
499                }
500                buf2[c++] = 0;
501                fp = fopen("/tmp/radvd.conf", "wb");
502                fwrite(buf2, 1, c - 1, fp);
503                fclose(fp);
504                free(buf2);
505        }
506        // nvram2file("radvd_conf", "/tmp/radvd.conf");
507
508        system2("sync");
509
510        ret = eval("radvd");
511        dd_syslog(LOG_INFO, "radvd : RADV daemon successfully started\n");
512
513        cprintf("done\n");
514        return;
515}
516
517void stop_radvd(void)
518{
519        int ret = 0;
520
521        if (pidof("radvd") > 0)
522                dd_syslog(LOG_INFO,
523                          "radvd : RADV daemon successfully stopped\n");
524        // ret = killps("radvd",NULL);
525        ret = killall("radvd", SIGKILL);
526
527        unlink("/var/run/radvd.pid");
528
529        cprintf("done\n");
530        return;
531}
532#endif
533#ifdef HAVE_IPV6
534void start_ipv6(void)
535{
536        int ret = 0;
537
538        if (!nvram_invmatch("ipv6_enable", "0"))
539                return;
540
541        ret = insmod("ipv6");
542        dd_syslog(LOG_INFO, "ipv6 successfully started\n");
543
544        cprintf("done\n");
545        return;
546}
547#endif
548
549#ifdef HAVE_3G
550void stop_3g(void)
551{
552        int ret;
553
554        unlink("/tmp/ppp/link");
555        if (pidof("pppd") > 0)
556                dd_syslog(LOG_INFO, "3g/umts process successfully stopped\n");
557        ret = killall("pppd", SIGTERM);
558
559        cprintf("done\n");
560}
561
562#endif
563
564#ifdef HAVE_PPPOE
565void stop_pppoe(void)
566{
567        int ret;
568
569        unlink("/tmp/ppp/link");
570        if (pidof("pppd") > 0)
571                dd_syslog(LOG_INFO, "pppoe process successfully stopped\n");
572        ret = killall("pppd", SIGTERM);
573        if (nvram_match("wan_vdsl", "1")) {
574                eval("ifconfig", nvram_safe_get("wan_iface"), "down");
575                eval("vconfig", "rem", nvram_safe_get("wan_iface"));
576        }
577        // ret += killall ("ip-up", SIGKILL);
578        // ret += killall ("ip-down", SIGKILL);
579
580        cprintf("done\n");
581        return;
582}
583
584void stop_single_pppoe(int pppoe_num)
585{
586        int ret;
587        char pppoe_pid[15], pppoe_ifname[15];
588        char ppp_unlink[2][20] = { "/tmp/ppp/link", "/tmp/ppp/link_1" };
589        char ppp_wan_dns[2][20] = { "wan_get_dns", "wan_get_dns_1" };
590
591        sprintf(pppoe_pid, "pppoe_pid%d", pppoe_num);
592        sprintf(pppoe_ifname, "pppoe_ifname%d", pppoe_num);
593        dprintf("start! stop pppoe %d, pid %s \n", pppoe_num,
594                nvram_safe_get(pppoe_pid));
595
596        ret = eval("kill", nvram_safe_get(pppoe_pid));
597        unlink(ppp_unlink[pppoe_num]);
598        nvram_unset(pppoe_ifname);
599
600        nvram_set(ppp_wan_dns[pppoe_num], "");
601        stop_dns_clear_resolv();
602
603        dprintf("done\n");
604        return;
605}
606#endif
607void stop_dhcpc(void)
608{
609        int ret = 0;
610
611        if (pidof("udhcpc") > 0)
612                dd_syslog(LOG_INFO,
613                          "udhcpc : udhcp client process successfully stopped\n");
614        ret = killall("udhcpc", SIGTERM);
615
616        cprintf("done\n");
617        return;
618}
619
620#ifdef HAVE_PPTP
621
622static void create_pptp_config(char *servername, char *username)
623{
624
625        FILE *fp;
626
627        mkdir("/tmp/ppp", 0777);
628        symlink("/sbin/rc", "/tmp/ppp/ip-up");
629        symlink("/sbin/rc", "/tmp/ppp/ip-down");
630        symlink("/dev/null", "/tmp/ppp/connect-errors");
631
632        /*
633         * Generate options file
634         */
635        if (!(fp = fopen("/tmp/ppp/options", "w"))) {
636                perror("/tmp/ppp/options");
637                return;
638        }
639        fprintf(fp, "defaultroute\n");  // Add a default route to the
640        // system routing tables,
641        // using the peer as the
642        // gateway
643        fprintf(fp, "usepeerdns\n");    // Ask the peer for up to 2 DNS
644        // server addresses
645        fprintf(fp, "pty 'pptp %s --nolaunchpppd", servername);
646
647        if (nvram_match("pptp_reorder", "1"))
648                fprintf(fp, " --nobuffer");
649
650        // PPTP client also supports synchronous mode.
651        // This should improve the speeds.
652        if (nvram_match("pptp_synchronous", "1"))
653                fprintf(fp, " --sync'\nsync\n");
654        else
655                fprintf(fp, "'\n");
656
657        fprintf(fp, "user '%s'\n", username);
658        // fprintf(fp, "persist\n"); // Do not exit after a connection is
659        // terminated.
660
661        if (nvram_match("mtu_enable", "1"))
662                fprintf(fp, "mtu %s\n", nvram_safe_get("wan_mtu"));
663
664        if (nvram_match("ppp_demand", "1")) {   // demand mode
665                fprintf(fp, "idle %d\n",
666                        nvram_match("ppp_demand",
667                                    "1") ?
668                        atoi(nvram_safe_get("ppp_idletime")) * 60 : 0);
669                fprintf(fp, "demand\n");        // Dial on demand
670                fprintf(fp, "persist\n");       // Do not exit after a connection is
671                // terminated.
672                fprintf(fp, "%s:%s\n", PPP_PSEUDO_IP, PPP_PSEUDO_GW);   // <local
673                // IP>:<remote
674                // IP>
675                fprintf(fp, "ipcp-accept-remote\n");
676                fprintf(fp, "ipcp-accept-local\n");
677                fprintf(fp, "connect true\n");
678                fprintf(fp, "noipdefault\n");   // Disables the default
679                // behaviour when no local IP
680                // address is specified
681                fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr
682                // to 1 in demand mode if the local
683                // address changes
684        } else {                // keepalive mode
685                start_redial();
686        }
687        if (nvram_match("pptp_encrypt", "0")) {
688                fprintf(fp, "nomppe\n");        // Disable mppe negotiation
689                fprintf(fp, "noccp\n"); // Disable CCP (Compression Control
690                // Protocol)
691        } else {
692                fprintf(fp, "mppe required,stateless\n");
693        }
694        fprintf(fp, "default-asyncmap\n");      // Disable asyncmap
695        // negotiation
696        fprintf(fp, "nopcomp\n");       // Disable protocol field compression
697        fprintf(fp, "noaccomp\n");      // Disable Address/Control
698        // compression
699        fprintf(fp, "novj\n");  // Disable Van Jacobson style TCP/IP
700        // header compression
701        fprintf(fp, "nobsdcomp\n");     // Disables BSD-Compress compression
702        fprintf(fp, "nodeflate\n");     // Disables Deflate compression
703        fprintf(fp, "lcp-echo-failure 6\n");
704        fprintf(fp, "lcp-echo-interval 3\n");
705        // echo-request frame to the
706        // peer
707        fprintf(fp, "noipdefault\n");
708        fprintf(fp, "lock\n");
709        fprintf(fp, "noauth\n");
710
711        if (nvram_invmatch("pptp_extraoptions", ""))
712                fwritenvram("pptp_extraoptions", fp);
713
714        fclose(fp);
715
716}
717
718void start_pptp(int status)
719{
720        int ret;
721        FILE *fp;
722        char *pptp_argv[] = { "pppd",
723                NULL
724        };
725        char username[80], passwd[80];
726
727        stop_dhcpc();
728#ifdef HAVE_PPPOE
729        stop_pppoe();
730#endif
731        stop_vpn_modules();
732
733        snprintf(username, sizeof(username), "%s",
734                 nvram_safe_get("ppp_username"));
735        snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));
736
737        if (status != REDIAL) {
738                create_pptp_config(nvram_safe_get("pptp_server_name"),
739                                   username);
740                /*
741                 * Generate pap-secrets file
742                 */
743                if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
744                        perror("/tmp/ppp/pap-secrets");
745                        return;
746                }
747                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
748                fclose(fp);
749                chmod("/tmp/ppp/pap-secrets", 0600);
750
751                /*
752                 * Generate chap-secrets file
753                 */
754                if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
755                        perror("/tmp/ppp/chap-secrets");
756                        return;
757                }
758                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
759                fclose(fp);
760                chmod("/tmp/ppp/chap-secrets", 0600);
761
762                /*
763                 * Enable Forwarding
764                 */
765                if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
766                        fputc('1', fp);
767                        fclose(fp);
768                } else
769                        perror("/proc/sys/net/ipv4/ip_forward");
770        }
771        char *wan_ifname = nvram_safe_get("wan_ifname");
772
773        if (isClient()) {
774                wan_ifname = getSTA();
775        }
776
777        nvram_set("pptp_ifname", wan_ifname);
778        /*
779         * Bring up WAN interface
780         */
781        if (nvram_match("pptp_use_dhcp", "1")) {
782                // pid_t pid;
783                // char *wan_ipaddr;
784                // char *wan_netmask;
785                // char *wan_gateway;
786
787                // char *pptp_server_ip = nvram_safe_get ("pptp_server_ip");
788                // char *wan_hostname = nvram_safe_get ("wan_hostname");
789
790                nvram_set("wan_get_dns", "");
791                nvram_unset("dhcpc_done");
792                //dirty hack
793                start_dhcpc(wan_ifname);
794                int timeout;
795
796                for (timeout = 60; !nvram_match("dhcpc_done", "1") && timeout > 0; --timeout) { /* wait for info from dhcp server */
797                        sleep(1);
798                }
799                stop_dhcpc();   /* we don't need dhcp client anymore */
800                create_pptp_config(nvram_safe_get("pptp_server_ip"), username);
801
802                /*
803                 * //this stuff has already been configured in dhcpc->bound
804                 * wan_ipaddr = nvram_safe_get ("wan_ipaddr"); wan_netmask =
805                 * nvram_safe_get ("wan_netmask"); wan_gateway = nvram_safe_get
806                 * ("wan_gateway"); pptp_server_ip = nvram_safe_get
807                 * ("pptp_server_ip");
808                 *
809                 * while (route_del (wan_ifname, 0, NULL, NULL, NULL) == 0);
810                 *
811                 *
812                 * for (timeout = 10; ifconfig (wan_ifname, IFUP, wan_ipaddr,
813                 * wan_netmask) && timeout > 0; --timeout) { sleep (1); } for
814                 * (timeout = 10; route_add (wan_ifname, 0, pptp_server_ip,
815                 * wan_gateway, "255.255.255.255") && timeout > 0; --timeout) { sleep
816                 * (1); }
817                 */
818        } else {
819                ifconfig(wan_ifname, IFUP,
820                         nvram_safe_get("wan_ipaddr"),
821                         nvram_safe_get("wan_netmask"));
822        }
823        ret = _evalpid(pptp_argv, NULL, 0, NULL);
824
825        /*
826         * if(nvram_match("pptp_usedhcp", "1")){ char *wan_hostname =
827         * nvram_get("wan_hostname"); char *dhcp_argv[] = { "udhcpc", "-i",
828         * nvram_safe_get("wan_ifname"), "-p", "/var/run/udhcpc.pid", "-s",
829         * "/tmp/udhcpc", wan_hostname && *wan_hostname ? "-H" : NULL,
830         * wan_hostname && *wan_hostname ? wan_hostname : NULL, NULL };
831         *
832         * ifconfig(nvram_safe_get("wan_ifname"), IFUP, NULL, NULL);
833         *
834         * symlink("/sbin/rc", "/tmp/udhcpc"); nvram_set("wan_get_dns","");
835         * //killps("udhcpc",NULL);
836         *
837         * eval("killall","udhcpc");
838         *
839         * _eval(dhcp_argv, NULL, 0, &pid);
840         *
841         * // Give enough time for DHCP to get IP address. sleep(2);
842         *
843         * } else ifconfig(nvram_safe_get("wan_ifname"), IFUP,
844         * nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
845         *
846         * // Start pptp client on wan interface ret = _eval(pptp_argv, NULL, 0,
847         * NULL);
848         */
849        if (nvram_match("ppp_demand", "1")) {
850                /*
851                 * Trigger Connect On Demand if user press Connect button in Status
852                 * page
853                 */
854                if (nvram_match("action_service", "start_pptp")
855                    || nvram_match("action_service", "start_l2tp")) {
856                        start_force_to_dial();
857                        // force_to_dial(nvram_safe_get("action_service"));
858                        nvram_unset("action_service");
859                }
860                /*
861                 * Trigger Connect On Demand if user ping pptp server
862                 */
863                else {
864                        eval("listen", nvram_safe_get("lan_ifname"));
865                }
866        }
867        stop_wland();
868        start_wshaper();
869        start_wland();
870        cprintf("done\n");
871        return;
872}
873
874void stop_pptp(void)
875{
876        int ret;
877
878        route_del(nvram_safe_get("wan_ifname"), 0,
879                  nvram_safe_get("pptp_server_ip"), NULL, NULL);
880
881        unlink("/tmp/ppp/link");
882        // ret = killps("pppd","-9");
883        // ret += killps("pptp","-9");
884        // ret += killps("listen","-9");
885        ret = killall("pppd", SIGTERM);
886        ret += killall("pptp", SIGKILL);
887        ret += killall("listen", SIGKILL);
888
889        cprintf("done\n");
890        return;
891}
892
893#endif
894
895// =========================================tallest============================================
896/*
897 * AhMan March 18 2005 Start the Original Linksys PPPoE
898 */
899/*
900 * This function build the pppoe instuction & execute it.
901 */
902#ifdef HAVE_PPPOE
903void start_pppoe(int pppoe_num)
904{
905        char idletime[20], retry_num[20], param[4];
906        char username[80], passwd[80];
907
908        char ppp_username[2][20] = { "ppp_username", "ppp_username_1" };
909        char ppp_passwd[2][20] = { "ppp_passwd", "ppp_passwd_1" };
910        char ppp_demand[2][20] = { "ppp_demand", "ppp_demand_1" };
911        char ppp_service[2][20] = { "ppp_service", "ppp_service_1" };
912        char ppp_ac[2][10] = { "ppp_ac", "ppp_ac_1" };
913        // char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" };
914        // char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" };
915        // char wangw[2][15] = { "wan_gateway", "wan_gateway_1" };
916        char pppoeifname[15];
917        char *wan_ifname = nvram_safe_get("wan_ifname");
918
919        if (isClient()) {
920                wan_ifname = getSTA();
921        }
922
923        pid_t pid;
924
925        sprintf(pppoeifname, "pppoe_ifname%d", pppoe_num);
926        nvram_set(pppoeifname, "");
927
928        cprintf("start session %d\n", pppoe_num);
929        sprintf(idletime, "%d", atoi(nvram_safe_get("ppp_idletime")) * 60);
930        snprintf(retry_num, sizeof(retry_num), "%d",
931                 (atoi(nvram_safe_get("ppp_redialperiod")) / 5) - 1);
932
933        snprintf(username, sizeof(username), "%s",
934                 nvram_safe_get(ppp_username[pppoe_num]));
935        snprintf(passwd, sizeof(passwd), "%s",
936                 nvram_safe_get(ppp_passwd[pppoe_num]));
937        sprintf(param, "%d", pppoe_num);
938        /*
939         * add here
940         */
941        char *pppoe_argv[] = { "pppoecd",
942                wan_ifname,
943                "-u", username,
944                "-p", passwd,
945                "-r", nvram_safe_get("wan_mtu"),        // del by honor, add by
946                // tallest.
947                "-t", nvram_safe_get("wan_mtu"),
948                "-i", nvram_match(ppp_demand[pppoe_num], "1") ? idletime : "0",
949                "-I", "30",     // Send an LCP echo-request frame to the
950                // server every 30 seconds
951                "-T", "3",      // pppd will presume the server to be dead if
952                // 5 LCP echo-requests are sent without
953                // receiving a valid LCP echo-reply
954                "-P", param,    // PPPOE session number.
955                "-N", retry_num,        // To avoid kill pppd when pppd has been
956                // connecting.
957#if LOG_PPPOE == 2
958                "-d",
959#endif
960                "-C", "disconnected_pppoe",     // by tallest 0407
961                NULL,           /* set default route */
962                NULL, NULL,     /* pppoe_service */
963                NULL, NULL,     /* pppoe_ac */
964                NULL,           /* pppoe_keepalive */
965                NULL
966        }, **arg;
967        /*
968         * Add optional arguments
969         */
970        for (arg = pppoe_argv; *arg; arg++) ;
971
972        /*
973         * Removed by AhMan
974         */
975
976        if (pppoe_num == PPPOE0) {      // PPPOE0 must set default route.
977                *arg++ = "-R";
978        }
979
980        if (nvram_invmatch(ppp_service[pppoe_num], "")) {
981                *arg++ = "-s";
982                *arg++ = nvram_safe_get(ppp_service[pppoe_num]);
983        }
984        if (nvram_invmatch(ppp_ac[pppoe_num], "")) {
985                *arg++ = "-a";
986                *arg++ = nvram_safe_get(ppp_ac[pppoe_num]);
987        }
988        if (nvram_match("ppp_static", "1")) {
989                *arg++ = "-L";
990                *arg++ = nvram_safe_get("ppp_static_ip");
991        }
992        // if (nvram_match("pppoe_demand", "1") || nvram_match("pppoe_keepalive",
993        // "1"))
994        *arg++ = "-k";
995
996        mkdir("/tmp/ppp", 0777);
997        symlink("/sbin/rc", "/tmp/ppp/ip-up");
998        symlink("/sbin/rc", "/tmp/ppp/ip-down");
999        symlink("/sbin/rc", "/tmp/ppp/set-pppoepid");   // tallest 1219
1000        unlink("/tmp/ppp/log");
1001
1002        // Clean rpppoe client files - Added by ice-man (Wed Jun 1)
1003        unlink("/tmp/ppp/options.pppoe");
1004        unlink("/tmp/ppp/connect-errors");
1005
1006        _evalpid(pppoe_argv, NULL, 0, &pid);
1007
1008        if (nvram_match(ppp_demand[pppoe_num], "1")) {
1009                // int timeout = 5;
1010                start_tmp_ppp(pppoe_num);
1011
1012                // This should be handled in start_wan_done
1013                // while (ifconfig (nvram_safe_get (pppoeifname), IFUP, NULL, NULL)
1014                // && timeout--)
1015                // sleep (1);
1016                // route_add (nvram_safe_get ("wan_iface"), 0, "0.0.0.0",
1017                // "10.112.112.112",
1018                // "0.0.0.0");
1019
1020        }
1021        cprintf("done. session %d\n", pppoe_num);
1022        return;
1023}
1024#endif
1025/*
1026 * AhMan March 18 2005
1027 */
1028/*
1029 * Get the IP, Subnetmask, Geteway from WAN interface
1030 * and set to NV ram.
1031 */
1032void start_tmp_ppp(int num)
1033{
1034
1035        int timeout = 5;
1036        char pppoeifname[15];
1037        char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" };
1038        char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" };
1039        char wangw[2][15] = { "wan_gateway", "wan_gateway_1" };
1040        // char wanif[2][15]={"wan_ifname","wan_ifname_1"};
1041        // char *wan_ifname = nvram_safe_get("wan_ifname");
1042        struct ifreq ifr;
1043        int s;
1044
1045        cprintf("start session %d\n", num);
1046
1047        sprintf(pppoeifname, "pppoe_ifname%d", num);
1048
1049        if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
1050                return;
1051
1052        /*
1053         * Wait for ppp0 to be created
1054         */
1055        while (ifconfig(nvram_safe_get(pppoeifname), IFUP, NULL, NULL)
1056               && timeout--)
1057                sleep(1);
1058
1059        strncpy(ifr.ifr_name, nvram_safe_get(pppoeifname), IFNAMSIZ);
1060
1061        /*
1062         * Set temporary IP address
1063         */
1064        timeout = 3;
1065        while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
1066                perror(nvram_safe_get(pppoeifname));
1067                printf("Wait %s inteface to init (1) ...\n",
1068                       nvram_safe_get(pppoeifname));
1069                sleep(1);
1070        };
1071        nvram_set(wanip[num], inet_ntoa(sin_addr(&(ifr.ifr_addr))));
1072        nvram_set(wanmask[num], "255.255.255.255");
1073
1074        /*
1075         * Set temporary P-t-P address
1076         */
1077        timeout = 3;
1078        while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
1079                perror(nvram_safe_get(pppoeifname));
1080                printf("Wait %s inteface to init (2) ...\n",
1081                       nvram_safe_get(pppoeifname));
1082                sleep(1);
1083        }
1084        nvram_set(wangw[num], inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
1085
1086        start_wan_done(nvram_safe_get(pppoeifname));
1087
1088        // if user press Connect" button from web, we must force to dial
1089        if (nvram_match("action_service", "start_pppoe")
1090            || nvram_match("action_service", "start_pppoe_1")) {
1091                sleep(3);
1092                // force_to_dial(nvram_safe_get("action_service"));
1093                start_force_to_dial();
1094                nvram_unset("action_service");
1095        }
1096
1097        close(s);
1098        cprintf("done session %d\n", num);
1099        return;
1100}
1101
1102// =====================================================================================================
1103
1104#ifdef HAVE_L2TP
1105void start_l2tp(int status)
1106{
1107        int ret;
1108        FILE *fp;
1109        char *l2tp_argv[] = { "l2tpd",
1110                NULL
1111        };
1112        char username[80], passwd[80];
1113
1114        // stop_dhcpc();
1115#ifdef HAVE_PPPOE
1116        stop_pppoe();
1117#endif
1118#ifdef HAVE_PPTP
1119        stop_pptp();
1120#endif
1121
1122        snprintf(username, sizeof(username), "%s",
1123                 nvram_safe_get("ppp_username"));
1124        snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));
1125
1126        if (status != REDIAL) {
1127                mkdir("/tmp/ppp", 0777);
1128                symlink("/sbin/rc", "/tmp/ppp/ip-up");
1129                symlink("/sbin/rc", "/tmp/ppp/ip-down");
1130                symlink("/dev/null", "/tmp/ppp/connect-errors");
1131
1132                /*
1133                 * Generate L2TP configuration file
1134                 */
1135                if (!(fp = fopen("/tmp/l2tp.conf", "w"))) {
1136                        perror("/tmp/l2tp.conf");
1137                        return;
1138                }
1139                fprintf(fp, "global\n");        // Global section
1140                fprintf(fp, "load-handler \"sync-pppd.so\"\n"); // Load
1141                // handlers
1142                fprintf(fp, "load-handler \"cmd.so\"\n");
1143                fprintf(fp, "listen-port 1701\n");      // Bind address
1144                fprintf(fp, "section sync-pppd\n");     // Configure the sync-pppd
1145                // handler
1146                fprintf(fp, "section peer\n");  // Peer section
1147                fprintf(fp, "peer %s\n", nvram_safe_get("l2tp_server_ip"));
1148                fprintf(fp, "port 1701\n");
1149                fprintf(fp, "lac-handler sync-pppd\n");
1150                fprintf(fp, "section cmd\n");   // Configure the cmd handler
1151                fclose(fp);
1152
1153                /*
1154                 * Generate options file
1155                 */
1156                if (!(fp = fopen("/tmp/ppp/options", "w"))) {
1157                        perror("/tmp/ppp/options");
1158                        return;
1159                }
1160                fprintf(fp, "defaultroute\n");  // Add a default route to the
1161                // system routing tables,
1162                // using the peer as the
1163                // gateway
1164                fprintf(fp, "usepeerdns\n");    // Ask the peer for up to 2 DNS
1165                // server addresses
1166                // fprintf(fp, "pty 'pptp %s
1167                // --nolaunchpppd'\n",nvram_safe_get("pptp_server_ip"));
1168                fprintf(fp, "user '%s'\n", username);
1169                // fprintf(fp, "persist\n"); // Do not exit after a connection is
1170                // terminated.
1171
1172                if (nvram_match("mtu_enable", "1")) {
1173                        fprintf(fp, "mtu %s\n", nvram_safe_get("wan_mtu"));
1174                }
1175
1176                if (nvram_match("ppp_demand", "1")) {   // demand mode
1177                        fprintf(fp, "idle %d\n",
1178                                nvram_match("ppp_demand",
1179                                            "1") ?
1180                                atoi(nvram_safe_get("ppp_idletime")) * 60 : 0);
1181                        // fprintf(fp, "demand\n"); // Dial on demand
1182                        // fprintf(fp, "persist\n"); // Do not exit after a connection is
1183                        // terminated.
1184                        // fprintf(fp, "%s:%s\n",PPP_PSEUDO_IP,PPP_PSEUDO_GW); // <local
1185                        // IP>:<remote IP>
1186                        fprintf(fp, "ipcp-accept-remote\n");
1187                        fprintf(fp, "ipcp-accept-local\n");
1188                        fprintf(fp, "connect true\n");
1189                        fprintf(fp, "noipdefault\n");   // Disables the default
1190                        // behaviour when no local IP
1191                        // address is specified
1192                        fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr
1193                        // to 1 in demand mode if the local
1194                        // address changes
1195                } else {        // keepalive mode
1196                        start_redial();
1197                }
1198
1199                fprintf(fp, "default-asyncmap\n");      // Disable asyncmap
1200                // negotiation
1201                fprintf(fp, "nopcomp\n");       // Disable protocol field compression
1202                fprintf(fp, "noaccomp\n");      // Disable Address/Control
1203                // compression
1204                fprintf(fp, "noccp\n"); // Disable CCP (Compression Control
1205                // Protocol)
1206                fprintf(fp, "novj\n");  // Disable Van Jacobson style TCP/IP
1207                // header compression
1208                fprintf(fp, "nobsdcomp\n");     // Disables BSD-Compress compression
1209                fprintf(fp, "nodeflate\n");     // Disables Deflate compression
1210                fprintf(fp, "lcp-echo-interval 0\n");   // Don't send an LCP
1211                // echo-request frame to the
1212                // peer
1213                fprintf(fp, "lock\n");
1214                fprintf(fp, "noauth\n");
1215
1216                fclose(fp);
1217
1218                /*
1219                 * Generate pap-secrets file
1220                 */
1221                if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
1222                        perror("/tmp/ppp/pap-secrets");
1223                        return;
1224                }
1225                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
1226                fclose(fp);
1227                chmod("/tmp/ppp/pap-secrets", 0600);
1228
1229                /*
1230                 * Generate chap-secrets file
1231                 */
1232                if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
1233                        perror("/tmp/ppp/chap-secrets");
1234                        return;
1235                }
1236                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
1237                fclose(fp);
1238                chmod("/tmp/ppp/chap-secrets", 0600);
1239
1240                /*
1241                 * Enable Forwarding
1242                 */
1243                if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
1244                        fputc('1', fp);
1245                        fclose(fp);
1246                } else
1247                        perror("/proc/sys/net/ipv4/ip_forward");
1248        }
1249
1250        /*
1251         * Bring up WAN interface
1252         */
1253        // ifconfig(nvram_safe_get("wan_ifname"), IFUP,
1254        // nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
1255
1256        ret = _evalpid(l2tp_argv, NULL, 0, NULL);
1257        sleep(1);
1258
1259        if (nvram_match("ppp_demand", "1")) {
1260                /*
1261                 * Trigger Connect On Demand if user press Connect button in Status
1262                 * page
1263                 */
1264                if (nvram_match("action_service", "start_l2tp")) {
1265                        start_force_to_dial();
1266                        nvram_unset("action_service");
1267                }
1268                /*
1269                 * Trigger Connect On Demand if user ping pptp server
1270                 */
1271                else
1272                        eval("listen", nvram_safe_get("lan_ifname"));
1273        } else
1274                sysprintf("l2tp-control \"start-session %s\"",
1275                          nvram_safe_get("l2tp_server_ip"));
1276
1277        cprintf("done\n");
1278        return;
1279}
1280
1281void start_l2tp_redial(void)
1282{
1283        start_l2tp(REDIAL);
1284}
1285
1286void start_l2tp_boot(void)
1287{
1288        start_l2tp(BOOT);
1289}
1290
1291void stop_l2tp(void)
1292{
1293        int ret = 0;
1294
1295        unlink("/tmp/ppp/link");
1296        // ret = killps("pppd","-9");
1297        // ret += killps("l2tpd","-9");
1298        // ret += killps("listen","-9");
1299
1300        ret = killall("pppd", SIGTERM);
1301        ret += killall("l2tpd", SIGKILL);
1302        ret += killall("listen", SIGKILL);
1303
1304        cprintf("done\n");
1305        return;
1306}
1307#endif
1308
1309void stop_wland(void)
1310{
1311        if (pidof("wland") > 0)
1312                dd_syslog(LOG_INFO,
1313                          "wland : WLAN daemon successfully stopped\n");
1314        int ret = killall("wland", SIGKILL);
1315
1316        cprintf("done\n");
1317        return;
1318}
1319
1320void start_wland(void)
1321{
1322        int ret;
1323        pid_t pid;
1324        char *wland_argv[] = { "wland",
1325                NULL
1326        };
1327
1328        stop_wland();
1329
1330        // if( nvram_match("apwatchdog_enable", "0") )
1331        // return 0;
1332
1333        ret = _evalpid(wland_argv, NULL, 0, &pid);
1334        dd_syslog(LOG_INFO, "wland : WLAN daemon successfully started\n");
1335        cprintf("done\n");
1336        return;
1337}
1338
1339void start_process_monitor(void)
1340{
1341        if (nvram_match("pmonitor_enable", "0"))
1342                return;
1343
1344        pid_t pid;
1345
1346        char *argv[] = { "process_monitor", NULL };
1347        int ret = _evalpid(argv, NULL, 0, &pid);
1348
1349        dd_syslog(LOG_INFO, "process_monitor successfully started\n");
1350
1351        cprintf("done");
1352
1353        return;
1354}
1355
1356void stop_process_monitor(void)
1357{
1358        int ret;
1359
1360        if (pidof("process_monitor") > 0)
1361                dd_syslog(LOG_INFO, "process_monitor successfully stopped\n");
1362        ret = killall("process_monitor", SIGKILL);
1363
1364        cprintf("done\n");
1365
1366        return;
1367}
1368
1369void start_radio_timer(void)
1370{
1371        if (nvram_match("radio0_timer_enable", "0")
1372            && nvram_match("radio1_timer_enable", "0"))
1373                return;
1374#ifdef HAVE_MADWIFI
1375        if (nvram_match("ath0_net_mode", "disabled"))
1376#else
1377        if (nvram_match("wl0_net_mode", "disabled")
1378            && nvram_match("wl1_net_mode", "disabled"))
1379#endif
1380                return;
1381
1382        pid_t pid;
1383
1384        char *argv[] = { "radio_timer", NULL };
1385        int ret = _evalpid(argv, NULL, 0, &pid);
1386
1387        dd_syslog(LOG_INFO,
1388                  "radio_timer : radio timer daemon successfully started\n");
1389
1390        cprintf("done");
1391
1392        return;
1393}
1394
1395void stop_radio_timer(void)
1396{
1397        int ret;
1398
1399        if (pidof("radio_timer") > 0)
1400                dd_syslog(LOG_INFO,
1401                          "radio_timer : radio timer daemon successfully stopped\n");
1402        ret = killall("radio_timer", SIGKILL);
1403
1404        cprintf("done\n");
1405
1406        return;
1407}
1408
1409void start_ttraff(void)
1410{
1411        if (!nvram_match("ttraff_enable", "1"))
1412                return;
1413
1414        if ((nvram_match("ttraff_iface", "") || !nvram_get("ttraff_iface"))
1415            && (nvram_match("wan_proto", "disabled")
1416                || nvram_match("wl0_mode", "wet")
1417                || nvram_match("wl0_mode", "apstawet")))
1418                return;
1419
1420        pid_t pid;
1421
1422        char *argv[] = { "ttraff", NULL };
1423        int ret = _evalpid(argv, NULL, 0, &pid);
1424
1425        dd_syslog(LOG_INFO,
1426                  "ttraff : traffic counter daemon successfully started\n");
1427
1428        cprintf("done");
1429
1430        return;
1431}
1432
1433void stop_ttraff(void)
1434{
1435        int ret;
1436
1437        if (pidof("ttraff") > 0)
1438                dd_syslog(LOG_INFO,
1439                          "ttraff : traffic counter daemon successfully stopped\n");
1440        ret = killall("ttraff", SIGKILL);
1441
1442        cprintf("done\n");
1443
1444        return;
1445}
1446
1447extern void start_heartbeat_boot(void);
1448
1449/*
1450 * Trigger Connect On Demand
1451 */
1452void start_force_to_dial(void)
1453{
1454        // force_to_dial( char *whichone){
1455        int ret = 0;
1456        char dst[50];
1457
1458        strcpy(&dst[0], nvram_safe_get("wan_gateway"));
1459
1460        char *ping_argv[] = { "ping",
1461                "-c", "1",
1462                dst,
1463                NULL
1464        };
1465
1466        sleep(1);
1467#ifdef HAVE_L2TP
1468        if (nvram_match("wan_proto", "l2tp")) {
1469
1470                sysprintf("l2tp-control \"start-session %s\"",
1471                          nvram_safe_get("l2tp_server_ip"));
1472                return;
1473        }
1474#endif
1475#ifdef HAVE_HEARTBEAT
1476        if (nvram_match("wan_proto", "heartbeat")) {
1477                start_heartbeat_boot();
1478                return;
1479        }
1480#endif
1481        _evalpid(ping_argv, NULL, 3, NULL);
1482
1483        return;
1484}
1485
1486#ifdef HAVE_CPUTEMP
1487
1488#ifdef HAVE_GATEWORX
1489#define TEMP_PATH "/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0028"
1490// #define TEMP_PATH "/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0028"
1491#define TEMP_PREFIX "temp"
1492#define TEMP_MUL 100
1493#else
1494#ifdef HAVE_X86
1495#define TEMP_PATH "/sys/devices/platform/i2c-1/1-0048"
1496#else
1497#define TEMP_PATH "/sys/devices/platform/i2c-0/0-0048"
1498#endif
1499#define TEMP_PREFIX "temp1"
1500#define TEMP_MUL 1000
1501#endif
1502
1503void start_hwmon(void)
1504{
1505        int temp_max = atoi(nvram_safe_get("hwmon_temp_max")) * TEMP_MUL;
1506        int temp_hyst = atoi(nvram_safe_get("hwmon_temp_hyst")) * TEMP_MUL;
1507
1508        sysprintf("/bin/echo %d > %s/%s_max", temp_max, TEMP_PATH, TEMP_PREFIX);
1509        sysprintf("/bin/echo %d > %s/%s_max_hyst", temp_hyst, TEMP_PATH,
1510                  TEMP_PREFIX);
1511        dd_syslog(LOG_INFO, "hwmon successfully started\n");
1512}
1513
1514#endif
Note: See TracBrowser for help on using the repository browser.