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

Last change on this file since 10612 was 10612, checked in by BrainSlayer, 5 years ago

formating and pptp dns fixes

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