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

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

typo

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