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

Last change on this file since 7470 was 7470, checked in by BrainSlayer, 6 years ago

2.6.22 Kernel Port changes

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