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

Last change on this file since 12347 was 12347, checked in by BrainSlayer, 4 years ago

it does not make much sense to stop something which does not even exist, so we take care about it

File size: 34.6 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                killall("dnsmasq", SIGTERM);
206        }
207        /*
208         * Save DNS to resolv.conf
209         */
210        if (!(fp_w = fopen(RESOLV_FILE, "w"))) {
211                perror(RESOLV_FILE);
212                return;
213        }
214        fprintf(fp_w, " ");
215        fclose(fp_w);
216
217        cprintf("done\n");
218        return;
219}
220
221#if 0
222void start_ntpc(void)
223{
224        char *servers = nvram_safe_get("ntp_server");
225
226        if (!nvram_invmatch("ntpd_enable", "0"))
227                return;
228
229        if (strlen(servers)) {
230                char *nas_argv[] =
231                    { "ntpclient", "-h", servers, "-i", "5", "-l", "-s", "-c",
232                        "2",
233                        NULL
234                };
235                pid_t pid;
236
237                _evalpid(nas_argv, NULL, 0, &pid);
238                dd_syslog(LOG_INFO,
239                          "ntpclient : ntp client successfully started\n");
240        }
241
242        cprintf("done\n");
243        return;
244}
245#endif
246void stop_ntpc(void)
247{
248        if (pidof("ntpclient") > 0) {
249                dd_syslog(LOG_INFO,
250                          "ntpclient : ntp client successfully stopped\n");
251                killall("ntpclient", SIGTERM);
252        }
253        cprintf("done\n");
254        return;
255}
256
257// ///////////////////////////////////////////////////
258void start_resetbutton(void)
259{
260        int ret = 0;
261
262        ret = eval("resetbutton");
263        dd_syslog(LOG_INFO,
264                  "reset button : resetbutton daemon successfully started\n");
265
266        cprintf("done\n");
267        return;
268}
269
270void stop_resetbutton(void)
271{
272
273        if (pidof("resetbutton") > 0) {
274                dd_syslog(LOG_INFO,
275                          "reset button : resetbutton daemon successfully stopped\n");
276                killall("resetbutton", SIGKILL);
277        }
278        cprintf("done\n");
279        return;
280}
281
282void start_iptqueue(void)
283{
284        int ret = 0;
285
286        if (!nvram_invmatch("iptqueue_enable", "0"))
287                return;
288
289        ret = eval("iptqueue");
290        dd_syslog(LOG_INFO, "iptqueue successfully started\n");
291
292        cprintf("done\n");
293        return;
294}
295
296void stop_iptqueue(void)
297{
298
299        if (pidof("iptqueue") > 0) {
300                dd_syslog(LOG_INFO,
301                          "iptqueue : iptqueue daemon successfully stopped\n");
302                killall("iptqueue", SIGKILL);
303        }
304        cprintf("done\n");
305        return;
306}
307
308void start_cron(void)
309{
310        int ret = 0;
311        struct stat buf;
312        FILE *fp;
313
314        if (nvram_match("cron_enable", "0"))
315                return;
316
317        if (pidof("cron") > 0)  //cron already running
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        if (pidof("cron") > 0)  //cron already running
380                return;
381        ret = eval("cron");
382        dd_syslog(LOG_INFO, "cron : cron daemon successfully started\n");
383
384        cprintf("done\n");
385        return;
386}
387
388void stop_cron(void)
389{
390
391        if (pidof("cron") > 0) {
392                dd_syslog(LOG_INFO,
393                          "cron : cron daemon successfully stopped\n");
394                killall("cron", SIGKILL);
395                eval("rm", "-rf", "/tmp/cron.d");
396        }
397        cprintf("done\n");
398        return;
399}
400
401#ifdef HAVE_SYSLOG
402void start_syslog(void)
403{
404        int ret1 = 0, ret2 = 0;
405
406        if (!nvram_invmatch("syslogd_enable", "0"))
407                return;
408
409        if (strlen(nvram_safe_get("syslogd_rem_ip")) > 0)
410                ret1 = eval("syslogd", "-R", nvram_safe_get("syslogd_rem_ip"));
411        else
412                ret1 = eval("syslogd", "-L");
413
414        dd_syslog(LOG_INFO, "syslogd : syslog daemon successfully started\n");
415        ret2 = eval("klogd");
416        dd_syslog(LOG_INFO, "klogd : klog daemon successfully started\n");
417
418        return;
419}
420
421void stop_syslog(void)
422{
423
424        if (pidof("klogd") > 0) {
425                dd_syslog(LOG_INFO,
426                          "klogd : klog daemon successfully stopped\n");
427                killall("klogd", SIGKILL);
428        }
429        if (pidof("syslogd") > 0) {
430                dd_syslog(LOG_INFO,
431                          "syslogd : syslog daemon successfully stopped\n");
432                killall("syslogd", SIGKILL);
433        }
434        cprintf("done\n");
435        return;
436}
437#endif
438
439void stop_redial(void)
440{
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                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
520        if (pidof("radvd") > 0) {
521                dd_syslog(LOG_INFO,
522                          "radvd : RADV daemon successfully stopped\n");
523                killall("radvd", SIGKILL);
524        }
525        unlink("/var/run/radvd.pid");
526
527        cprintf("done\n");
528        return;
529}
530#endif
531#ifdef HAVE_IPV6
532void start_ipv6(void)
533{
534        int ret = 0;
535
536        if (!nvram_invmatch("ipv6_enable", "0"))
537                return;
538
539        ret = insmod("ipv6");
540        dd_syslog(LOG_INFO, "ipv6 successfully started\n");
541
542        cprintf("done\n");
543        return;
544}
545#endif
546
547#ifdef HAVE_3G
548void stop_3g(void)
549{
550        unlink("/tmp/ppp/link");
551        if (pidof("pppd") > 0) {
552                dd_syslog(LOG_INFO, "3g/umts process successfully stopped\n");
553                killall("pppd", SIGTERM);
554        }
555        cprintf("done\n");
556}
557
558#endif
559
560#ifdef HAVE_PPPOE
561void stop_pppoe(void)
562{
563
564        unlink("/tmp/ppp/link");
565        if (pidof("pppd") > 0) {
566                dd_syslog(LOG_INFO, "pppoe process successfully stopped\n");
567                killall("pppd", SIGTERM);
568        }
569        if (nvram_match("wan_vdsl", "1")) {
570                eval("ifconfig", nvram_safe_get("wan_iface"), "down");
571                eval("vconfig", "rem", nvram_safe_get("wan_iface"));
572        }
573        // ret += killall ("ip-up", SIGKILL);
574        // ret += killall ("ip-down", SIGKILL);
575
576        cprintf("done\n");
577        return;
578}
579
580void stop_single_pppoe(int pppoe_num)
581{
582        int ret;
583        char pppoe_pid[15], pppoe_ifname[15];
584        char ppp_unlink[2][20] = { "/tmp/ppp/link", "/tmp/ppp/link_1" };
585        char ppp_wan_dns[2][20] = { "wan_get_dns", "wan_get_dns_1" };
586
587        sprintf(pppoe_pid, "pppoe_pid%d", pppoe_num);
588        sprintf(pppoe_ifname, "pppoe_ifname%d", pppoe_num);
589        dprintf("start! stop pppoe %d, pid %s \n", pppoe_num,
590                nvram_safe_get(pppoe_pid));
591
592        ret = eval("kill", nvram_safe_get(pppoe_pid));
593        unlink(ppp_unlink[pppoe_num]);
594        nvram_unset(pppoe_ifname);
595
596        nvram_set(ppp_wan_dns[pppoe_num], "");
597        stop_dns_clear_resolv();
598
599        dprintf("done\n");
600        return;
601}
602#endif
603void stop_dhcpc(void)
604{
605
606        if (pidof("udhcpc") > 0) {
607                dd_syslog(LOG_INFO,
608                          "udhcpc : udhcp client process successfully stopped\n");
609                killall("udhcpc", SIGTERM);
610        }
611        cprintf("done\n");
612        return;
613}
614
615#ifdef HAVE_PPTP
616
617static void create_pptp_config(char *servername, char *username)
618{
619
620        FILE *fp;
621
622        mkdir("/tmp/ppp", 0777);
623        symlink("/sbin/rc", "/tmp/ppp/ip-up");
624        symlink("/sbin/rc", "/tmp/ppp/ip-down");
625        symlink("/dev/null", "/tmp/ppp/connect-errors");
626
627        /*
628         * Generate options file
629         */
630        if (!(fp = fopen("/tmp/ppp/options", "w"))) {
631                perror("/tmp/ppp/options");
632                return;
633        }
634        fprintf(fp, "defaultroute\n");  // Add a default route to the
635        // system routing tables,
636        // using the peer as the
637        // gateway
638        fprintf(fp, "usepeerdns\n");    // Ask the peer for up to 2 DNS
639        // server addresses
640        fprintf(fp, "pty 'pptp %s --nolaunchpppd", servername);
641
642        if (nvram_match("pptp_reorder", "1"))
643                fprintf(fp, " --nobuffer");
644
645        // PPTP client also supports synchronous mode.
646        // This should improve the speeds.
647        if (nvram_match("pptp_synchronous", "1"))
648                fprintf(fp, " --sync'\nsync\n");
649        else
650                fprintf(fp, "'\n");
651
652        fprintf(fp, "user '%s'\n", username);
653        // fprintf(fp, "persist\n"); // Do not exit after a connection is
654        // terminated.
655
656        if (nvram_match("mtu_enable", "1"))
657                fprintf(fp, "mtu %s\n", nvram_safe_get("wan_mtu"));
658
659        if (nvram_match("ppp_demand", "1")) {   // demand mode
660                fprintf(fp, "idle %d\n",
661                        nvram_match("ppp_demand",
662                                    "1") ?
663                        atoi(nvram_safe_get("ppp_idletime")) * 60 : 0);
664                fprintf(fp, "demand\n");        // Dial on demand
665                fprintf(fp, "persist\n");       // Do not exit after a connection is
666                // terminated.
667                fprintf(fp, "%s:%s\n", PPP_PSEUDO_IP, PPP_PSEUDO_GW);   // <local
668                // IP>:<remote
669                // IP>
670                fprintf(fp, "ipcp-accept-remote\n");
671                fprintf(fp, "ipcp-accept-local\n");
672                fprintf(fp, "connect true\n");
673                fprintf(fp, "noipdefault\n");   // Disables the default
674                // behaviour when no local IP
675                // address is specified
676                fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr
677                // to 1 in demand mode if the local
678                // address changes
679        } else {                // keepalive mode
680                start_redial();
681        }
682        if (nvram_match("pptp_encrypt", "0")) {
683                fprintf(fp, "nomppe\n");        // Disable mppe negotiation
684                fprintf(fp, "noccp\n"); // Disable CCP (Compression Control
685                // Protocol)
686        } else {
687                fprintf(fp, "mppe required,stateless\n");
688        }
689        fprintf(fp, "default-asyncmap\n");      // Disable asyncmap
690        // negotiation
691        fprintf(fp, "nopcomp\n");       // Disable protocol field compression
692        fprintf(fp, "noaccomp\n");      // Disable Address/Control
693        // compression
694        fprintf(fp, "novj\n");  // Disable Van Jacobson style TCP/IP
695        // header compression
696        fprintf(fp, "nobsdcomp\n");     // Disables BSD-Compress compression
697        fprintf(fp, "nodeflate\n");     // Disables Deflate compression
698        fprintf(fp, "lcp-echo-failure 6\n");
699        fprintf(fp, "lcp-echo-interval 3\n");
700        // echo-request frame to the
701        // peer
702        fprintf(fp, "noipdefault\n");
703        fprintf(fp, "lock\n");
704        fprintf(fp, "noauth\n");
705
706        if (nvram_invmatch("pptp_extraoptions", ""))
707                fwritenvram("pptp_extraoptions", fp);
708
709        fclose(fp);
710
711}
712
713void start_pptp(int status)
714{
715        int ret;
716        FILE *fp;
717        char *pptp_argv[] = { "pppd",
718                NULL
719        };
720        char username[80], passwd[80];
721
722        stop_dhcpc();
723#ifdef HAVE_PPPOE
724        stop_pppoe();
725#endif
726        stop_vpn_modules();
727
728        snprintf(username, sizeof(username), "%s",
729                 nvram_safe_get("ppp_username"));
730        snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));
731
732        if (status != REDIAL) {
733                create_pptp_config(nvram_safe_get("pptp_server_name"),
734                                   username);
735                /*
736                 * Generate pap-secrets file
737                 */
738                if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
739                        perror("/tmp/ppp/pap-secrets");
740                        return;
741                }
742                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
743                fclose(fp);
744                chmod("/tmp/ppp/pap-secrets", 0600);
745
746                /*
747                 * Generate chap-secrets file
748                 */
749                if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
750                        perror("/tmp/ppp/chap-secrets");
751                        return;
752                }
753                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
754                fclose(fp);
755                chmod("/tmp/ppp/chap-secrets", 0600);
756
757                /*
758                 * Enable Forwarding
759                 */
760                if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
761                        fputc('1', fp);
762                        fclose(fp);
763                } else
764                        perror("/proc/sys/net/ipv4/ip_forward");
765        }
766        char *wan_ifname = nvram_safe_get("wan_ifname");
767
768        if (isClient()) {
769                wan_ifname = getSTA();
770        }
771
772        nvram_set("pptp_ifname", wan_ifname);
773        /*
774         * Bring up WAN interface
775         */
776        if (nvram_match("pptp_use_dhcp", "1")) {
777                // pid_t pid;
778                // char *wan_ipaddr;
779                // char *wan_netmask;
780                // char *wan_gateway;
781
782                // char *pptp_server_ip = nvram_safe_get ("pptp_server_ip");
783                // char *wan_hostname = nvram_safe_get ("wan_hostname");
784
785                nvram_set("wan_get_dns", "");
786                nvram_unset("dhcpc_done");
787                //dirty hack
788                start_dhcpc(wan_ifname);
789                int timeout;
790
791                for (timeout = 60; !nvram_match("dhcpc_done", "1") && timeout > 0; --timeout) { /* wait for info from dhcp server */
792                        sleep(1);
793                }
794                stop_dhcpc();   /* we don't need dhcp client anymore */
795                create_pptp_config(nvram_safe_get("pptp_server_ip"), username);
796
797                /*
798                 * //this stuff has already been configured in dhcpc->bound
799                 * wan_ipaddr = nvram_safe_get ("wan_ipaddr"); wan_netmask =
800                 * nvram_safe_get ("wan_netmask"); wan_gateway = nvram_safe_get
801                 * ("wan_gateway"); pptp_server_ip = nvram_safe_get
802                 * ("pptp_server_ip");
803                 *
804                 * while (route_del (wan_ifname, 0, NULL, NULL, NULL) == 0);
805                 *
806                 *
807                 * for (timeout = 10; ifconfig (wan_ifname, IFUP, wan_ipaddr,
808                 * wan_netmask) && timeout > 0; --timeout) { sleep (1); } for
809                 * (timeout = 10; route_add (wan_ifname, 0, pptp_server_ip,
810                 * wan_gateway, "255.255.255.255") && timeout > 0; --timeout) { sleep
811                 * (1); }
812                 */
813        } else {
814                ifconfig(wan_ifname, IFUP,
815                         nvram_safe_get("wan_ipaddr"),
816                         nvram_safe_get("wan_netmask"));
817        }
818        ret = _evalpid(pptp_argv, NULL, 0, NULL);
819
820        /*
821         * if(nvram_match("pptp_usedhcp", "1")){ char *wan_hostname =
822         * nvram_get("wan_hostname"); char *dhcp_argv[] = { "udhcpc", "-i",
823         * nvram_safe_get("wan_ifname"), "-p", "/var/run/udhcpc.pid", "-s",
824         * "/tmp/udhcpc", wan_hostname && *wan_hostname ? "-H" : NULL,
825         * wan_hostname && *wan_hostname ? wan_hostname : NULL, NULL };
826         *
827         * ifconfig(nvram_safe_get("wan_ifname"), IFUP, NULL, NULL);
828         *
829         * symlink("/sbin/rc", "/tmp/udhcpc"); nvram_set("wan_get_dns","");
830         * //killps("udhcpc",NULL);
831         *
832         * eval("killall","udhcpc");
833         *
834         * _eval(dhcp_argv, NULL, 0, &pid);
835         *
836         * // Give enough time for DHCP to get IP address. sleep(2);
837         *
838         * } else ifconfig(nvram_safe_get("wan_ifname"), IFUP,
839         * nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
840         *
841         * // Start pptp client on wan interface ret = _eval(pptp_argv, NULL, 0,
842         * NULL);
843         */
844        if (nvram_match("ppp_demand", "1")) {
845                /*
846                 * Trigger Connect On Demand if user press Connect button in Status
847                 * page
848                 */
849                if (nvram_match("action_service", "start_pptp")
850                    || nvram_match("action_service", "start_l2tp")) {
851                        start_force_to_dial();
852                        // force_to_dial(nvram_safe_get("action_service"));
853                        nvram_unset("action_service");
854                }
855                /*
856                 * Trigger Connect On Demand if user ping pptp server
857                 */
858                else {
859                        eval("listen", nvram_safe_get("lan_ifname"));
860                }
861        }
862        stop_wland();
863        start_wshaper();
864        start_wland();
865        cprintf("done\n");
866        return;
867}
868
869void stop_pptp(void)
870{
871
872        route_del(nvram_safe_get("wan_ifname"), 0,
873                  nvram_safe_get("pptp_server_ip"), NULL, NULL);
874
875        unlink("/tmp/ppp/link");
876        if (pidof("pppd") > 0) {
877                killall("pppd", SIGTERM);
878                killall("pptp", SIGKILL);
879                killall("listen", SIGKILL);
880        }
881
882        cprintf("done\n");
883        return;
884}
885
886#endif
887
888// =========================================tallest============================================
889/*
890 * AhMan March 18 2005 Start the Original Linksys PPPoE
891 */
892/*
893 * This function build the pppoe instuction & execute it.
894 */
895#ifdef HAVE_PPPOE
896void start_pppoe(int pppoe_num)
897{
898        char idletime[20], retry_num[20], param[4];
899        char username[80], passwd[80];
900
901        char ppp_username[2][20] = { "ppp_username", "ppp_username_1" };
902        char ppp_passwd[2][20] = { "ppp_passwd", "ppp_passwd_1" };
903        char ppp_demand[2][20] = { "ppp_demand", "ppp_demand_1" };
904        char ppp_service[2][20] = { "ppp_service", "ppp_service_1" };
905        char ppp_ac[2][10] = { "ppp_ac", "ppp_ac_1" };
906        // char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" };
907        // char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" };
908        // char wangw[2][15] = { "wan_gateway", "wan_gateway_1" };
909        char pppoeifname[15];
910        char *wan_ifname = nvram_safe_get("wan_ifname");
911
912        if (isClient()) {
913                wan_ifname = getSTA();
914        }
915
916        pid_t pid;
917
918        sprintf(pppoeifname, "pppoe_ifname%d", pppoe_num);
919        nvram_set(pppoeifname, "");
920
921        cprintf("start session %d\n", pppoe_num);
922        sprintf(idletime, "%d", atoi(nvram_safe_get("ppp_idletime")) * 60);
923        snprintf(retry_num, sizeof(retry_num), "%d",
924                 (atoi(nvram_safe_get("ppp_redialperiod")) / 5) - 1);
925
926        snprintf(username, sizeof(username), "%s",
927                 nvram_safe_get(ppp_username[pppoe_num]));
928        snprintf(passwd, sizeof(passwd), "%s",
929                 nvram_safe_get(ppp_passwd[pppoe_num]));
930        sprintf(param, "%d", pppoe_num);
931        /*
932         * add here
933         */
934        char *pppoe_argv[] = { "pppoecd",
935                wan_ifname,
936                "-u", username,
937                "-p", passwd,
938                "-r", nvram_safe_get("wan_mtu"),        // del by honor, add by
939                // tallest.
940                "-t", nvram_safe_get("wan_mtu"),
941                "-i", nvram_match(ppp_demand[pppoe_num], "1") ? idletime : "0",
942                "-I", "30",     // Send an LCP echo-request frame to the
943                // server every 30 seconds
944                "-T", "3",      // pppd will presume the server to be dead if
945                // 5 LCP echo-requests are sent without
946                // receiving a valid LCP echo-reply
947                "-P", param,    // PPPOE session number.
948                "-N", retry_num,        // To avoid kill pppd when pppd has been
949                // connecting.
950#if LOG_PPPOE == 2
951                "-d",
952#endif
953                "-C", "disconnected_pppoe",     // by tallest 0407
954                NULL,           /* set default route */
955                NULL, NULL,     /* pppoe_service */
956                NULL, NULL,     /* pppoe_ac */
957                NULL,           /* pppoe_keepalive */
958                NULL
959        }, **arg;
960        /*
961         * Add optional arguments
962         */
963        for (arg = pppoe_argv; *arg; arg++) ;
964
965        /*
966         * Removed by AhMan
967         */
968
969        if (pppoe_num == PPPOE0) {      // PPPOE0 must set default route.
970                *arg++ = "-R";
971        }
972
973        if (nvram_invmatch(ppp_service[pppoe_num], "")) {
974                *arg++ = "-s";
975                *arg++ = nvram_safe_get(ppp_service[pppoe_num]);
976        }
977        if (nvram_invmatch(ppp_ac[pppoe_num], "")) {
978                *arg++ = "-a";
979                *arg++ = nvram_safe_get(ppp_ac[pppoe_num]);
980        }
981        if (nvram_match("ppp_static", "1")) {
982                *arg++ = "-L";
983                *arg++ = nvram_safe_get("ppp_static_ip");
984        }
985        // if (nvram_match("pppoe_demand", "1") || nvram_match("pppoe_keepalive",
986        // "1"))
987        *arg++ = "-k";
988
989        mkdir("/tmp/ppp", 0777);
990        symlink("/sbin/rc", "/tmp/ppp/ip-up");
991        symlink("/sbin/rc", "/tmp/ppp/ip-down");
992        symlink("/sbin/rc", "/tmp/ppp/set-pppoepid");   // tallest 1219
993        unlink("/tmp/ppp/log");
994
995        // Clean rpppoe client files - Added by ice-man (Wed Jun 1)
996        unlink("/tmp/ppp/options.pppoe");
997        unlink("/tmp/ppp/connect-errors");
998
999        _evalpid(pppoe_argv, NULL, 0, &pid);
1000
1001        if (nvram_match(ppp_demand[pppoe_num], "1")) {
1002                // int timeout = 5;
1003                start_tmp_ppp(pppoe_num);
1004
1005                // This should be handled in start_wan_done
1006                // while (ifconfig (nvram_safe_get (pppoeifname), IFUP, NULL, NULL)
1007                // && timeout--)
1008                // sleep (1);
1009                // route_add (nvram_safe_get ("wan_iface"), 0, "0.0.0.0",
1010                // "10.112.112.112",
1011                // "0.0.0.0");
1012
1013        }
1014        cprintf("done. session %d\n", pppoe_num);
1015        return;
1016}
1017#endif
1018/*
1019 * AhMan March 18 2005
1020 */
1021/*
1022 * Get the IP, Subnetmask, Geteway from WAN interface
1023 * and set to NV ram.
1024 */
1025void start_tmp_ppp(int num)
1026{
1027
1028        int timeout = 5;
1029        char pppoeifname[15];
1030        char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" };
1031        char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" };
1032        char wangw[2][15] = { "wan_gateway", "wan_gateway_1" };
1033        // char wanif[2][15]={"wan_ifname","wan_ifname_1"};
1034        // char *wan_ifname = nvram_safe_get("wan_ifname");
1035        struct ifreq ifr;
1036        int s;
1037
1038        cprintf("start session %d\n", num);
1039
1040        sprintf(pppoeifname, "pppoe_ifname%d", num);
1041
1042        if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
1043                return;
1044
1045        /*
1046         * Wait for ppp0 to be created
1047         */
1048        while (ifconfig(nvram_safe_get(pppoeifname), IFUP, NULL, NULL)
1049               && timeout--)
1050                sleep(1);
1051
1052        strncpy(ifr.ifr_name, nvram_safe_get(pppoeifname), IFNAMSIZ);
1053
1054        /*
1055         * Set temporary IP address
1056         */
1057        timeout = 3;
1058        while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
1059                perror(nvram_safe_get(pppoeifname));
1060                printf("Wait %s inteface to init (1) ...\n",
1061                       nvram_safe_get(pppoeifname));
1062                sleep(1);
1063        };
1064        nvram_set(wanip[num], inet_ntoa(sin_addr(&(ifr.ifr_addr))));
1065        nvram_set(wanmask[num], "255.255.255.255");
1066
1067        /*
1068         * Set temporary P-t-P address
1069         */
1070        timeout = 3;
1071        while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
1072                perror(nvram_safe_get(pppoeifname));
1073                printf("Wait %s inteface to init (2) ...\n",
1074                       nvram_safe_get(pppoeifname));
1075                sleep(1);
1076        }
1077        nvram_set(wangw[num], inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
1078
1079        start_wan_done(nvram_safe_get(pppoeifname));
1080
1081        // if user press Connect" button from web, we must force to dial
1082        if (nvram_match("action_service", "start_pppoe")
1083            || nvram_match("action_service", "start_pppoe_1")) {
1084                sleep(3);
1085                // force_to_dial(nvram_safe_get("action_service"));
1086                start_force_to_dial();
1087                nvram_unset("action_service");
1088        }
1089
1090        close(s);
1091        cprintf("done session %d\n", num);
1092        return;
1093}
1094
1095// =====================================================================================================
1096
1097#ifdef HAVE_L2TP
1098void start_l2tp(int status)
1099{
1100        int ret;
1101        FILE *fp;
1102        char *l2tp_argv[] = { "l2tpd",
1103                NULL
1104        };
1105        char username[80], passwd[80];
1106
1107        // stop_dhcpc();
1108#ifdef HAVE_PPPOE
1109        stop_pppoe();
1110#endif
1111#ifdef HAVE_PPTP
1112        stop_pptp();
1113#endif
1114
1115        snprintf(username, sizeof(username), "%s",
1116                 nvram_safe_get("ppp_username"));
1117        snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));
1118
1119        if (status != REDIAL) {
1120                mkdir("/tmp/ppp", 0777);
1121                symlink("/sbin/rc", "/tmp/ppp/ip-up");
1122                symlink("/sbin/rc", "/tmp/ppp/ip-down");
1123                symlink("/dev/null", "/tmp/ppp/connect-errors");
1124
1125                /*
1126                 * Generate L2TP configuration file
1127                 */
1128                if (!(fp = fopen("/tmp/l2tp.conf", "w"))) {
1129                        perror("/tmp/l2tp.conf");
1130                        return;
1131                }
1132                fprintf(fp, "global\n");        // Global section
1133                fprintf(fp, "load-handler \"sync-pppd.so\"\n"); // Load
1134                // handlers
1135                fprintf(fp, "load-handler \"cmd.so\"\n");
1136                fprintf(fp, "listen-port 1701\n");      // Bind address
1137                fprintf(fp, "section sync-pppd\n");     // Configure the sync-pppd
1138                // handler
1139                fprintf(fp, "section peer\n");  // Peer section
1140                fprintf(fp, "peer %s\n", nvram_safe_get("l2tp_server_ip"));
1141                fprintf(fp, "port 1701\n");
1142                fprintf(fp, "lac-handler sync-pppd\n");
1143                fprintf(fp, "section cmd\n");   // Configure the cmd handler
1144                fclose(fp);
1145
1146                /*
1147                 * Generate options file
1148                 */
1149                if (!(fp = fopen("/tmp/ppp/options", "w"))) {
1150                        perror("/tmp/ppp/options");
1151                        return;
1152                }
1153                fprintf(fp, "defaultroute\n");  // Add a default route to the
1154                // system routing tables,
1155                // using the peer as the
1156                // gateway
1157                fprintf(fp, "usepeerdns\n");    // Ask the peer for up to 2 DNS
1158                // server addresses
1159                // fprintf(fp, "pty 'pptp %s
1160                // --nolaunchpppd'\n",nvram_safe_get("pptp_server_ip"));
1161                fprintf(fp, "user '%s'\n", username);
1162                // fprintf(fp, "persist\n"); // Do not exit after a connection is
1163                // terminated.
1164
1165                if (nvram_match("mtu_enable", "1")) {
1166                        fprintf(fp, "mtu %s\n", nvram_safe_get("wan_mtu"));
1167                }
1168
1169                if (nvram_match("ppp_demand", "1")) {   // demand mode
1170                        fprintf(fp, "idle %d\n",
1171                                nvram_match("ppp_demand",
1172                                            "1") ?
1173                                atoi(nvram_safe_get("ppp_idletime")) * 60 : 0);
1174                        // fprintf(fp, "demand\n"); // Dial on demand
1175                        // fprintf(fp, "persist\n"); // Do not exit after a connection is
1176                        // terminated.
1177                        // fprintf(fp, "%s:%s\n",PPP_PSEUDO_IP,PPP_PSEUDO_GW); // <local
1178                        // IP>:<remote IP>
1179                        fprintf(fp, "ipcp-accept-remote\n");
1180                        fprintf(fp, "ipcp-accept-local\n");
1181                        fprintf(fp, "connect true\n");
1182                        fprintf(fp, "noipdefault\n");   // Disables the default
1183                        // behaviour when no local IP
1184                        // address is specified
1185                        fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr
1186                        // to 1 in demand mode if the local
1187                        // address changes
1188                } else {        // keepalive mode
1189                        start_redial();
1190                }
1191
1192                fprintf(fp, "default-asyncmap\n");      // Disable asyncmap
1193                // negotiation
1194                fprintf(fp, "nopcomp\n");       // Disable protocol field compression
1195                fprintf(fp, "noaccomp\n");      // Disable Address/Control
1196                // compression
1197                fprintf(fp, "noccp\n"); // Disable CCP (Compression Control
1198                // Protocol)
1199                fprintf(fp, "novj\n");  // Disable Van Jacobson style TCP/IP
1200                // header compression
1201                fprintf(fp, "nobsdcomp\n");     // Disables BSD-Compress compression
1202                fprintf(fp, "nodeflate\n");     // Disables Deflate compression
1203                fprintf(fp, "lcp-echo-interval 0\n");   // Don't send an LCP
1204                // echo-request frame to the
1205                // peer
1206                fprintf(fp, "lock\n");
1207                fprintf(fp, "noauth\n");
1208
1209                fclose(fp);
1210
1211                /*
1212                 * Generate pap-secrets file
1213                 */
1214                if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
1215                        perror("/tmp/ppp/pap-secrets");
1216                        return;
1217                }
1218                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
1219                fclose(fp);
1220                chmod("/tmp/ppp/pap-secrets", 0600);
1221
1222                /*
1223                 * Generate chap-secrets file
1224                 */
1225                if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
1226                        perror("/tmp/ppp/chap-secrets");
1227                        return;
1228                }
1229                fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
1230                fclose(fp);
1231                chmod("/tmp/ppp/chap-secrets", 0600);
1232
1233                /*
1234                 * Enable Forwarding
1235                 */
1236                if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
1237                        fputc('1', fp);
1238                        fclose(fp);
1239                } else
1240                        perror("/proc/sys/net/ipv4/ip_forward");
1241        }
1242
1243        /*
1244         * Bring up WAN interface
1245         */
1246        // ifconfig(nvram_safe_get("wan_ifname"), IFUP,
1247        // nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
1248
1249        ret = _evalpid(l2tp_argv, NULL, 0, NULL);
1250        sleep(1);
1251
1252        if (nvram_match("ppp_demand", "1")) {
1253                /*
1254                 * Trigger Connect On Demand if user press Connect button in Status
1255                 * page
1256                 */
1257                if (nvram_match("action_service", "start_l2tp")) {
1258                        start_force_to_dial();
1259                        nvram_unset("action_service");
1260                }
1261                /*
1262                 * Trigger Connect On Demand if user ping pptp server
1263                 */
1264                else
1265                        eval("listen", nvram_safe_get("lan_ifname"));
1266        } else
1267                sysprintf("l2tp-control \"start-session %s\"",
1268                          nvram_safe_get("l2tp_server_ip"));
1269
1270        cprintf("done\n");
1271        return;
1272}
1273
1274void start_l2tp_redial(void)
1275{
1276        start_l2tp(REDIAL);
1277}
1278
1279void start_l2tp_boot(void)
1280{
1281        start_l2tp(BOOT);
1282}
1283
1284void stop_l2tp(void)
1285{
1286
1287        unlink("/tmp/ppp/link");
1288        // ret = killps("pppd","-9");
1289        // ret += killps("l2tpd","-9");
1290        // ret += killps("listen","-9");
1291
1292        if (pidof("pppd") > 0) {
1293                killall("pppd", SIGTERM);
1294                killall("l2tpd", SIGKILL);
1295                killall("listen", SIGKILL);
1296        }
1297        cprintf("done\n");
1298        return;
1299}
1300#endif
1301
1302void stop_wland(void)
1303{
1304        if (pidof("wland") > 0) {
1305                dd_syslog(LOG_INFO,
1306                          "wland : WLAN daemon successfully stopped\n");
1307                killall("wland", SIGKILL);
1308        }
1309        cprintf("done\n");
1310        return;
1311}
1312
1313void start_wland(void)
1314{
1315        int ret;
1316        pid_t pid;
1317        char *wland_argv[] = { "wland",
1318                NULL
1319        };
1320
1321        stop_wland();
1322
1323        // if( nvram_match("apwatchdog_enable", "0") )
1324        // return 0;
1325
1326        ret = _evalpid(wland_argv, NULL, 0, &pid);
1327        dd_syslog(LOG_INFO, "wland : WLAN daemon successfully started\n");
1328        cprintf("done\n");
1329        return;
1330}
1331
1332void start_process_monitor(void)
1333{
1334        if (nvram_match("pmonitor_enable", "0"))
1335                return;
1336
1337        pid_t pid;
1338
1339        char *argv[] = { "process_monitor", NULL };
1340        int ret = _evalpid(argv, NULL, 0, &pid);
1341
1342        dd_syslog(LOG_INFO, "process_monitor successfully started\n");
1343
1344        cprintf("done");
1345
1346        return;
1347}
1348
1349void stop_process_monitor(void)
1350{
1351
1352        if (pidof("process_monitor") > 0) {
1353                dd_syslog(LOG_INFO, "process_monitor successfully stopped\n");
1354                killall("process_monitor", SIGKILL);
1355        }
1356        cprintf("done\n");
1357
1358        return;
1359}
1360
1361void start_radio_timer(void)
1362{
1363        if (nvram_match("radio0_timer_enable", "0")
1364            && nvram_match("radio1_timer_enable", "0"))
1365                return;
1366#ifdef HAVE_MADWIFI
1367        if (nvram_match("ath0_net_mode", "disabled"))
1368#else
1369        if (nvram_match("wl0_net_mode", "disabled")
1370            && nvram_match("wl1_net_mode", "disabled"))
1371#endif
1372                return;
1373
1374        pid_t pid;
1375
1376        char *argv[] = { "radio_timer", NULL };
1377        int ret = _evalpid(argv, NULL, 0, &pid);
1378
1379        dd_syslog(LOG_INFO,
1380                  "radio_timer : radio timer daemon successfully started\n");
1381
1382        cprintf("done");
1383
1384        return;
1385}
1386
1387void stop_radio_timer(void)
1388{
1389
1390        if (pidof("radio_timer") > 0) {
1391                dd_syslog(LOG_INFO,
1392                          "radio_timer : radio timer daemon successfully stopped\n");
1393                killall("radio_timer", SIGKILL);
1394        }
1395        cprintf("done\n");
1396
1397        return;
1398}
1399
1400void start_ttraff(void)
1401{
1402        if (!nvram_match("ttraff_enable", "1"))
1403                return;
1404
1405        if ((nvram_match("ttraff_iface", "") || !nvram_get("ttraff_iface"))
1406            && (nvram_match("wan_proto", "disabled")
1407                || nvram_match("wl0_mode", "wet")
1408                || nvram_match("wl0_mode", "apstawet")))
1409                return;
1410
1411        pid_t pid;
1412
1413        char *argv[] = { "ttraff", NULL };
1414        int ret = _evalpid(argv, NULL, 0, &pid);
1415
1416        dd_syslog(LOG_INFO,
1417                  "ttraff : traffic counter daemon successfully started\n");
1418
1419        cprintf("done");
1420
1421        return;
1422}
1423
1424void stop_ttraff(void)
1425{
1426
1427        if (pidof("ttraff") > 0) {
1428                dd_syslog(LOG_INFO,
1429                          "ttraff : traffic counter daemon successfully stopped\n");
1430                killall("ttraff", SIGKILL);
1431        }
1432        cprintf("done\n");
1433
1434        return;
1435}
1436
1437extern void start_heartbeat_boot(void);
1438
1439/*
1440 * Trigger Connect On Demand
1441 */
1442void start_force_to_dial(void)
1443{
1444        // force_to_dial( char *whichone){
1445        int ret = 0;
1446        char dst[50];
1447
1448        strcpy(&dst[0], nvram_safe_get("wan_gateway"));
1449
1450        char *ping_argv[] = { "ping",
1451                "-c", "1",
1452                dst,
1453                NULL
1454        };
1455
1456        sleep(1);
1457#ifdef HAVE_L2TP
1458        if (nvram_match("wan_proto", "l2tp")) {
1459
1460                sysprintf("l2tp-control \"start-session %s\"",
1461                          nvram_safe_get("l2tp_server_ip"));
1462                return;
1463        }
1464#endif
1465#ifdef HAVE_HEARTBEAT
1466        if (nvram_match("wan_proto", "heartbeat")) {
1467                start_heartbeat_boot();
1468                return;
1469        }
1470#endif
1471        _evalpid(ping_argv, NULL, 3, NULL);
1472
1473        return;
1474}
1475
1476#ifdef HAVE_CPUTEMP
1477
1478#ifdef HAVE_GATEWORX
1479#define TEMP_PATH "/sys/devices/platform/IXP4XX-I2C.0/i2c-adapter:i2c-0/0-0028"
1480// #define TEMP_PATH "/sys/devices/platform/IXP4XX-I2C.0/i2c-0/0-0028"
1481#define TEMP_PREFIX "temp"
1482#define TEMP_MUL 100
1483#else
1484#ifdef HAVE_X86
1485#define TEMP_PATH "/sys/devices/platform/i2c-1/1-0048"
1486#else
1487#define TEMP_PATH "/sys/devices/platform/i2c-0/0-0048"
1488#endif
1489#define TEMP_PREFIX "temp1"
1490#define TEMP_MUL 1000
1491#endif
1492
1493void start_hwmon(void)
1494{
1495        int temp_max = atoi(nvram_safe_get("hwmon_temp_max")) * TEMP_MUL;
1496        int temp_hyst = atoi(nvram_safe_get("hwmon_temp_hyst")) * TEMP_MUL;
1497
1498        sysprintf("/bin/echo %d > %s/%s_max", temp_max, TEMP_PATH, TEMP_PREFIX);
1499        sysprintf("/bin/echo %d > %s/%s_max_hyst", temp_hyst, TEMP_PATH,
1500                  TEMP_PREFIX);
1501        dd_syslog(LOG_INFO, "hwmon successfully started\n");
1502}
1503
1504#endif
Note: See TracBrowser for help on using the repository browser.