Changeset 13414


Ignore:
Timestamp:
12/08/09 18:30:55 (3 years ago)
Author:
BrainSlayer
Message:

more modules

Location:
src/router/services
Files:
8 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/router/services/Makefile

    r13406 r13414  
    2626 
    2727OBJS = services.o network.o firewall.o mssid.o wshaper.o ddns.o sysinit.o sysinit-$(ARCHITECTURE).o interface.o udhcpc.o mkfiles.o iptable.o defaults.o \ 
    28        bridgetools.o igmp.o wpa.o vlantagging.o httpd.o setuserpw.o anchorfree.o gpio.o mask.o reset_defaults.o 
     28       bridgetools.o igmp.o wpa.o vlantagging.o httpd.o setuserpw.o anchorfree.o gpio.o mask.o reset_defaults.o vpn.o cron.o syslog.o redial.o radvd.o pppoe.o pptpclient.o l2tp.o 
    2929 
    3030 
  • src/router/services/include/services.h

    r12767 r13414  
    172172 
    173173void reset_hwaddr(char *ifname); 
     174void start_force_to_dial(void); 
     175 
     176 
     177#define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST) 
  • src/router/services/services/services.c

    r13124 r13414  
    6060#define TXPWR_DEFAULT 70 
    6161 
    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  
    69 void start_force_to_dial(void); 
     62 
     63 
    7064 
    7165static int alreadyInHost(char *host) 
     
    112106} 
    113107 
    114 void start_vpn_modules(void) 
    115 { 
    116 #if defined(HAVE_XSCALE) || defined(HAVE_FONERA) || defined(HAVE_WHRAG108) || defined(HAVE_X86) ||defined(HAVE_LS2) || defined(HAVE_CA8) || defined(HAVE_TW6600)  || defined(HAVE_LS5) || defined(HAVE_RT2880) 
    117  
    118         if ((nvram_match("pptp_pass", "1") || nvram_match("l2tp_pass", "1") 
    119              || nvram_match("ipsec_pass", "1"))) { 
    120                 insmod("nf_conntrack_proto_gre"); 
    121                 dd_syslog(LOG_INFO, 
    122                           "vpn modules : nf_conntrack_proto_gre successfully loaded\n"); 
    123                 insmod("nf_nat_proto_gre"); 
    124                 dd_syslog(LOG_INFO, 
    125                           "vpn modules : nf_nat_proto_gre successfully loaded\n"); 
    126         } 
    127         if (nvram_match("pptp_pass", "1")) { 
    128                 insmod("nf_conntrack_pptp"); 
    129                 dd_syslog(LOG_INFO, 
    130                           "vpn modules : nf_conntrack_pptp successfully loaded\n"); 
    131                 insmod("nf_nat_pptp"); 
    132                 dd_syslog(LOG_INFO, 
    133                           "vpn modules : nf_nat_pptp successfully loaded\n"); 
    134         } 
    135 #else 
    136         if ((nvram_match("pptp_pass", "1") || nvram_match("l2tp_pass", "1") 
    137              || nvram_match("ipsec_pass", "1"))) { 
    138                 insmod("ip_conntrack_proto_gre"); 
    139                 dd_syslog(LOG_INFO, 
    140                           "vpn modules : ip_conntrack_proto_gre successfully loaded\n"); 
    141                 insmod("ip_nat_proto_gre"); 
    142                 dd_syslog(LOG_INFO, 
    143                           "vpn modules : ip_nat_proto_gre successfully loaded\n"); 
    144         } 
    145         if (nvram_match("pptp_pass", "1")) { 
    146                 insmod("ip_conntrack_pptp"); 
    147                 dd_syslog(LOG_INFO, 
    148                           "vpn modules : ip_conntrack_pptp successfully loaded\n"); 
    149                 insmod("ip_nat_pptp"); 
    150                 dd_syslog(LOG_INFO, 
    151                           "vpn modules : ip_nat_pptp successfully loaded\n"); 
    152         } 
    153 #endif 
    154 } 
    155  
    156 void stop_vpn_modules(void) 
    157 { 
    158 #if defined(HAVE_XSCALE) || defined(HAVE_FONERA) || defined(HAVE_WHRAG108) || defined(HAVE_X86) || defined(HAVE_LS2) || defined(HAVE_CA8) || defined(HAVE_TW6600)  || defined(HAVE_LS5) || defined(HAVE_RT2880) 
    159         rmmod("nf_nat_pptp"); 
    160         rmmod("nf_conntrack_pptp"); 
    161         rmmod("nf_nat_proto_gre"); 
    162         rmmod("nf_conntrack_proto_gre"); 
    163         dd_syslog(LOG_INFO, 
    164                   "vpn modules : vpn modules successfully unloaded\n"); 
    165 #else 
    166         rmmod("ip_nat_pptp"); 
    167         rmmod("ip_nat_proto_gre"); 
    168         rmmod("ip_conntrack_pptp"); 
    169         rmmod("ip_conntrack_proto_gre"); 
    170         dd_syslog(LOG_INFO, 
    171                   "vpn modules : vpn modules successfully unloaded\n"); 
    172  
    173 #endif 
    174 } 
    175108 
    176109/* 
     
    306239} 
    307240 
    308 void start_cron(void) 
    309 { 
    310         int ret = 0; 
    311         struct stat buf; 
    312         FILE *fp; 
    313  
    314         if (nvram_match("cron_enable", "0")) 
    315                 return; 
    316  
    317         if (pidof("cron") > 0)  //cron already running 
    318                 return; 
    319  
    320         /* 
    321          * Create cron's database directory  
    322          */ 
    323         if (stat("/var/spool", &buf) != 0) { 
    324                 mkdir("/var/spool", 0700); 
    325                 mkdir("/var/spool/cron", 0700); 
    326         } 
    327         mkdir("/tmp/cron.d", 0700); 
    328  
    329         buf_to_file("/tmp/cron.d/check_ps", 
    330                     "*/2 * * * * root /sbin/check_ps\n"); 
    331         /* 
    332          * pppoe reconnect  
    333          */ 
    334         unlink("/tmp/cron.d/pppoe_reconnect"); 
    335         if (nvram_match("reconnect_enable", "1")) { 
    336  
    337                 fp = fopen("/tmp/cron.d/pppoe_reconnect", "w"); 
    338                 fprintf(fp, "%s %s * * * root /usr/bin/killall pppd\n", 
    339                         nvram_safe_get("reconnect_minutes"), 
    340                         nvram_safe_get("reconnect_hours")); 
    341                 fclose(fp); 
    342         } 
    343         /* 
    344          * reboot scheduler  
    345          */ 
    346         unlink("/tmp/cron.d/check_schedules"); 
    347         if (nvram_match("schedule_enable", "1") 
    348             && nvram_match("schedule_hour_time", "2")) { 
    349  
    350                 fp = fopen("/tmp/cron.d/check_schedules", "w"); 
    351                 fprintf(fp, "%s %s * * %s root /sbin/reboot\n", 
    352                         nvram_safe_get("schedule_minutes"), 
    353                         nvram_safe_get("schedule_hours"), 
    354                         nvram_safe_get("schedule_weekdays")); 
    355                 fclose(fp); 
    356         } 
    357  
    358         /* 
    359          * Additional cron jobs  
    360          */ 
    361         unlink("/tmp/cron.d/cron_jobs"); 
    362  
    363         if (nvram_invmatch("cron_jobs", "")) { 
    364  
    365                 fp = fopen("/tmp/cron.d/cron_jobs", "w"); 
    366                 fwritenvram("cron_jobs", fp); 
    367                 fprintf(fp, "\n");      // extra new line at the end 
    368                 fclose(fp); 
    369         } 
    370  
    371         /* 
    372          * Custom cron files  
    373          */ 
    374         eval("cp", "-af", "/tmp/mycron.d/*", "/tmp/cron.d/"); 
    375         eval("cp", "-af", "/jffs/mycron.d/*", "/tmp/cron.d/"); 
    376         eval("cp", "-af", "/mmc/mycron.d/*", "/tmp/cron.d/"); 
    377  
    378         cprintf("starting cron\n"); 
    379         if (pidof("cron") > 0)  //cron already running 
    380                 return; 
    381         ret = eval("cron"); 
    382         dd_syslog(LOG_INFO, "cron : cron daemon successfully started\n"); 
    383  
    384         cprintf("done\n"); 
    385         return; 
    386 } 
    387  
    388 void stop_cron(void) 
    389 { 
    390  
    391         if (pidof("cron") > 0) { 
    392                 dd_syslog(LOG_INFO, 
    393                           "cron : cron daemon successfully stopped\n"); 
    394                 killall("cron", SIGKILL); 
    395                 eval("rm", "-rf", "/tmp/cron.d"); 
    396         } 
    397         cprintf("done\n"); 
    398         return; 
    399 } 
    400  
    401 #ifdef HAVE_SYSLOG 
    402 void start_syslog(void) 
    403 { 
    404         int ret1 = 0, ret2 = 0; 
    405  
    406         if (!nvram_invmatch("syslogd_enable", "0")) 
    407                 return; 
    408  
    409         if (strlen(nvram_safe_get("syslogd_rem_ip")) > 0) 
    410                 ret1 = eval("syslogd", "-R", nvram_safe_get("syslogd_rem_ip")); 
    411         else 
    412                 ret1 = eval("syslogd", "-L"); 
    413  
    414         dd_syslog(LOG_INFO, "syslogd : syslog daemon successfully started\n"); 
    415         ret2 = eval("klogd"); 
    416         dd_syslog(LOG_INFO, "klogd : klog daemon successfully started\n"); 
    417  
    418         return; 
    419 } 
    420  
    421 void stop_syslog(void) 
    422 { 
    423  
    424         if (pidof("klogd") > 0) { 
    425                 dd_syslog(LOG_INFO, 
    426                           "klogd : klog daemon successfully stopped\n"); 
    427                 killall("klogd", SIGKILL); 
    428         } 
    429         if (pidof("syslogd") > 0) { 
    430                 dd_syslog(LOG_INFO, 
    431                           "syslogd : syslog daemon successfully stopped\n"); 
    432                 killall("syslogd", SIGKILL); 
    433         } 
    434         cprintf("done\n"); 
    435         return; 
    436 } 
    437 #endif 
    438  
    439 void stop_redial(void) 
    440 { 
    441  
    442         if (pidof("redial") > 0) { 
    443                 dd_syslog(LOG_INFO, 
    444                           "ppp_redial : redial daemon successfully stopped\n"); 
    445                 // ret = killps("redial","-9"); 
    446                 killall("redial", SIGKILL); 
    447         } 
    448         cprintf("done\n"); 
    449         return; 
    450 } 
    451  
    452 void start_redial(void) 
    453 { 
    454         int ret; 
    455         pid_t pid; 
    456         char *redial_argv[] = { "/tmp/ppp/redial", 
    457                 nvram_safe_get("ppp_redialperiod"), 
    458                 NULL 
    459         }; 
    460         if (pidof("redial") > 0) { 
    461                 return;         // not required, already running 
    462         } 
    463  
    464         symlink("/sbin/rc", "/tmp/ppp/redial"); 
    465  
    466         ret = _evalpid(redial_argv, NULL, 0, &pid); 
    467         dd_syslog(LOG_INFO, 
    468                   "ppp_redial : redial process successfully started\n"); 
    469  
    470         cprintf("done\n"); 
    471         return; 
    472 } 
    473  
    474 #ifdef HAVE_RADVD 
    475 void start_radvd(void) 
    476 { 
    477         int ret = 0; 
    478         int c = 0; 
    479         char *buf, *buf2; 
    480         int i; 
    481         FILE *fp; 
    482  
    483         if (!nvram_match("radvd_enable", "1")) 
    484                 return; 
    485         if (!nvram_match("ipv6_enable", "1")) 
    486                 return; 
    487         buf = nvram_safe_get("radvd_conf"); 
    488         if (buf != NULL) { 
    489                 buf2 = (char *)malloc(strlen(buf) + 1); 
    490                 memcpy(buf2, buf, strlen(buf)); 
    491                 buf2[strlen(buf)] = 0; 
    492  
    493                 i = 0; 
    494                 while (buf2[i++] != 0) { 
    495                         cprintf("."); 
    496                         if (buf2[i - 1] == '\r') 
    497                                 continue; 
    498                         buf2[c++] = buf2[i - 1]; 
    499                 } 
    500                 buf2[c++] = 0; 
    501                 fp = fopen("/tmp/radvd.conf", "wb"); 
    502                 fwrite(buf2, 1, c - 1, fp); 
    503                 fclose(fp); 
    504                 free(buf2); 
    505         } 
    506         // nvram2file("radvd_conf", "/tmp/radvd.conf"); 
    507  
    508         system2("sync"); 
    509  
    510         ret = eval("radvd"); 
    511         dd_syslog(LOG_INFO, "radvd : RADV daemon successfully started\n"); 
    512  
    513         cprintf("done\n"); 
    514         return; 
    515 } 
    516  
    517 void stop_radvd(void) 
    518 { 
    519  
    520         if (pidof("radvd") > 0) { 
    521                 dd_syslog(LOG_INFO, 
    522                           "radvd : RADV daemon successfully stopped\n"); 
    523                 killall("radvd", SIGKILL); 
    524         } 
    525         unlink("/var/run/radvd.pid"); 
    526  
    527         cprintf("done\n"); 
    528         return; 
    529 } 
    530 #endif 
     241 
     242 
     243 
    531244#ifdef HAVE_IPV6 
    532245void start_ipv6(void) 
     
    558271#endif 
    559272 
    560 #ifdef HAVE_PPPOE 
    561 void stop_pppoe(void) 
    562 { 
    563  
    564         unlink("/tmp/ppp/link"); 
    565         if (pidof("pppd") > 0) { 
    566                 dd_syslog(LOG_INFO, "pppoe process successfully stopped\n"); 
    567                 killall("pppd", SIGTERM); 
    568         } 
    569         if (nvram_match("wan_vdsl", "1")) { 
    570                 eval("ifconfig", nvram_safe_get("wan_iface"), "down"); 
    571                 eval("vconfig", "rem", nvram_safe_get("wan_iface")); 
    572         } 
    573         // ret += killall ("ip-up", SIGKILL); 
    574         // ret += killall ("ip-down", SIGKILL); 
    575  
    576         cprintf("done\n"); 
    577         return; 
    578 } 
    579  
    580 void stop_single_pppoe(int pppoe_num) 
    581 { 
    582         int ret; 
    583         char pppoe_pid[15], pppoe_ifname[15]; 
    584         char ppp_unlink[2][20] = { "/tmp/ppp/link", "/tmp/ppp/link_1" }; 
    585         char ppp_wan_dns[2][20] = { "wan_get_dns", "wan_get_dns_1" }; 
    586  
    587         sprintf(pppoe_pid, "pppoe_pid%d", pppoe_num); 
    588         sprintf(pppoe_ifname, "pppoe_ifname%d", pppoe_num); 
    589         dprintf("start! stop pppoe %d, pid %s \n", pppoe_num, 
    590                 nvram_safe_get(pppoe_pid)); 
    591  
    592         ret = eval("kill", nvram_safe_get(pppoe_pid)); 
    593         unlink(ppp_unlink[pppoe_num]); 
    594         nvram_unset(pppoe_ifname); 
    595  
    596         nvram_set(ppp_wan_dns[pppoe_num], ""); 
    597         stop_dns_clear_resolv(); 
    598  
    599         dprintf("done\n"); 
    600         return; 
    601 } 
    602 #endif 
    603273void stop_dhcpc(void) 
    604274{ 
     
    613283} 
    614284 
    615 #ifdef HAVE_PPTP 
    616  
    617 static void create_pptp_config(char *servername, char *username) 
    618 { 
    619  
    620         FILE *fp; 
    621  
    622         mkdir("/tmp/ppp", 0777); 
    623         symlink("/sbin/rc", "/tmp/ppp/ip-up"); 
    624         symlink("/sbin/rc", "/tmp/ppp/ip-down"); 
    625         symlink("/dev/null", "/tmp/ppp/connect-errors"); 
    626  
    627         /* 
    628          * Generate options file  
    629          */ 
    630         if (!(fp = fopen("/tmp/ppp/options", "w"))) { 
    631                 perror("/tmp/ppp/options"); 
    632                 return; 
    633         } 
    634         fprintf(fp, "defaultroute\n");  // Add a default route to the  
    635         // system routing tables, 
    636         // using the peer as the 
    637         // gateway 
    638         fprintf(fp, "usepeerdns\n");    // Ask the peer for up to 2 DNS 
    639         // server addresses 
    640         fprintf(fp, "pty 'pptp %s --nolaunchpppd", servername); 
    641  
    642         if (nvram_match("pptp_reorder", "1")) 
    643                 fprintf(fp, " --nobuffer"); 
    644  
    645         // PPTP client also supports synchronous mode. 
    646         // This should improve the speeds. 
    647         if (nvram_match("pptp_synchronous", "1")) 
    648                 fprintf(fp, " --sync'\nsync\n"); 
    649         else 
    650                 fprintf(fp, "'\n"); 
    651  
    652         fprintf(fp, "user '%s'\n", username); 
    653         // fprintf(fp, "persist\n"); // Do not exit after a connection is 
    654         // terminated. 
    655  
    656         if (nvram_match("mtu_enable", "1")) 
    657                 fprintf(fp, "mtu %s\n", nvram_safe_get("wan_mtu")); 
    658  
    659         if (nvram_match("ppp_demand", "1")) {   // demand mode 
    660                 fprintf(fp, "idle %d\n", 
    661                         nvram_match("ppp_demand", 
    662                                     "1") ? 
    663                         atoi(nvram_safe_get("ppp_idletime")) * 60 : 0); 
    664                 fprintf(fp, "demand\n");        // Dial on demand 
    665                 fprintf(fp, "persist\n");       // Do not exit after a connection is 
    666                 // terminated. 
    667                 fprintf(fp, "%s:%s\n", PPP_PSEUDO_IP, PPP_PSEUDO_GW);   // <local  
    668                 // IP>:<remote  
    669                 // IP> 
    670                 fprintf(fp, "ipcp-accept-remote\n"); 
    671                 fprintf(fp, "ipcp-accept-local\n"); 
    672                 fprintf(fp, "connect true\n"); 
    673                 fprintf(fp, "noipdefault\n");   // Disables the default 
    674                 // behaviour when no local IP  
    675                 // address is specified 
    676                 fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr 
    677                 // to 1 in demand mode if the local 
    678                 // address changes 
    679         } else {                // keepalive mode 
    680                 start_redial(); 
    681         } 
    682         if (nvram_match("pptp_encrypt", "0")) { 
    683                 fprintf(fp, "nomppe\n");        // Disable mppe negotiation 
    684                 fprintf(fp, "noccp\n"); // Disable CCP (Compression Control 
    685                 // Protocol) 
    686         } else { 
    687                 fprintf(fp, "mppe required,stateless\n"); 
    688         } 
    689         fprintf(fp, "default-asyncmap\n");      // Disable asyncmap 
    690         // negotiation 
    691         fprintf(fp, "nopcomp\n");       // Disable protocol field compression 
    692         fprintf(fp, "noaccomp\n");      // Disable Address/Control 
    693         // compression 
    694         fprintf(fp, "novj\n");  // Disable Van Jacobson style TCP/IP 
    695         // header compression 
    696         fprintf(fp, "nobsdcomp\n");     // Disables BSD-Compress compression 
    697         fprintf(fp, "nodeflate\n");     // Disables Deflate compression 
    698         fprintf(fp, "lcp-echo-failure 6\n"); 
    699         fprintf(fp, "lcp-echo-interval 3\n"); 
    700         // echo-request frame to the 
    701         // peer 
    702         fprintf(fp, "noipdefault\n"); 
    703         fprintf(fp, "lock\n"); 
    704         fprintf(fp, "noauth\n"); 
    705  
    706         if (nvram_invmatch("pptp_extraoptions", "")) 
    707                 fwritenvram("pptp_extraoptions", fp); 
    708  
    709         fclose(fp); 
    710  
    711 } 
    712  
    713 void start_pptp(int status) 
    714 { 
    715         int ret; 
    716         FILE *fp; 
    717         char *pptp_argv[] = { "pppd", 
    718                 NULL 
    719         }; 
    720         char username[80], passwd[80]; 
    721  
    722         stop_dhcpc(); 
    723 #ifdef HAVE_PPPOE 
    724         stop_pppoe(); 
    725 #endif 
    726         stop_vpn_modules(); 
    727  
    728         snprintf(username, sizeof(username), "%s", 
    729                  nvram_safe_get("ppp_username")); 
    730         snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd")); 
    731  
    732         if (status != REDIAL) { 
    733                 create_pptp_config(nvram_safe_get("pptp_server_name"), 
    734                                    username); 
    735                 /* 
    736                  * Generate pap-secrets file  
    737                  */ 
    738                 if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) { 
    739                         perror("/tmp/ppp/pap-secrets"); 
    740                         return; 
    741                 } 
    742                 fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd); 
    743                 fclose(fp); 
    744                 chmod("/tmp/ppp/pap-secrets", 0600); 
    745  
    746                 /* 
    747                  * Generate chap-secrets file  
    748                  */ 
    749                 if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) { 
    750                         perror("/tmp/ppp/chap-secrets"); 
    751                         return; 
    752                 } 
    753                 fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd); 
    754                 fclose(fp); 
    755                 chmod("/tmp/ppp/chap-secrets", 0600); 
    756  
    757                 /* 
    758                  * Enable Forwarding  
    759                  */ 
    760                 if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) { 
    761                         fputc('1', fp); 
    762                         fclose(fp); 
    763                 } else 
    764                         perror("/proc/sys/net/ipv4/ip_forward"); 
    765         } 
    766         char *wan_ifname = nvram_safe_get("wan_ifname"); 
    767  
    768         if (isClient()) { 
    769                 wan_ifname = getSTA(); 
    770         } 
    771  
    772         nvram_set("pptp_ifname", wan_ifname); 
    773         /* 
    774          * Bring up WAN interface  
    775          */ 
    776         if (nvram_match("pptp_use_dhcp", "1")) { 
    777                 // pid_t pid; 
    778                 // char *wan_ipaddr; 
    779                 // char *wan_netmask; 
    780                 // char *wan_gateway; 
    781  
    782                 // char *pptp_server_ip = nvram_safe_get ("pptp_server_ip"); 
    783                 // char *wan_hostname = nvram_safe_get ("wan_hostname"); 
    784  
    785                 nvram_set("wan_get_dns", ""); 
    786                 nvram_unset("dhcpc_done"); 
    787                 //dirty hack 
    788                 start_dhcpc(wan_ifname); 
    789                 int timeout; 
    790  
    791                 for (timeout = 60; !nvram_match("dhcpc_done", "1") && timeout > 0; --timeout) { /* wait for info from dhcp server */ 
    792                         sleep(1); 
    793                 } 
    794                 stop_dhcpc();   /* we don't need dhcp client anymore */ 
    795                 create_pptp_config(nvram_safe_get("pptp_server_ip"), username); 
    796  
    797         } else { 
    798                 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), 
    799                          nvram_safe_get("wan_netmask")); 
    800                 if (!nvram_match("wan_gateway", "0.0.0.0")) 
    801                         route_add(wan_ifname, 0, 
    802                                   nvram_safe_get("pptp_server_ip"), 
    803                                   nvram_safe_get("wan_gateway"), 
    804                                   "255.255.255.255"); 
    805         } 
    806         ret = _evalpid(pptp_argv, NULL, 0, NULL); 
    807  
    808         if (nvram_match("ppp_demand", "1")) { 
    809                 /* 
    810                  * Trigger Connect On Demand if user press Connect button in Status 
    811                  * page  
    812                  */ 
    813                 if (nvram_match("action_service", "start_pptp") 
    814                     || nvram_match("action_service", "start_l2tp")) { 
    815                         start_force_to_dial(); 
    816                         // force_to_dial(nvram_safe_get("action_service")); 
    817                         nvram_unset("action_service"); 
    818                 } 
    819                 /* 
    820                  * Trigger Connect On Demand if user ping pptp server  
    821                  */ 
    822                 else { 
    823                         eval("listen", nvram_safe_get("lan_ifname")); 
    824                 } 
    825         } 
    826         stop_wland(); 
    827         start_wshaper(); 
    828         start_wland(); 
    829         cprintf("done\n"); 
    830         return; 
    831 } 
    832  
    833 void stop_pptp(void) 
    834 { 
    835  
    836         route_del(nvram_safe_get("wan_ifname"), 0, 
    837                   nvram_safe_get("pptp_server_ip"), NULL, NULL); 
    838  
    839         unlink("/tmp/ppp/link"); 
    840         if (pidof("pppd") > 0) { 
    841                 killall("pppd", SIGTERM); 
    842                 killall("pptp", SIGKILL); 
    843                 killall("listen", SIGKILL); 
    844         } 
    845  
    846         cprintf("done\n"); 
    847         return; 
    848 } 
    849  
    850 #endif 
    851  
    852 // =========================================tallest============================================ 
    853 /* 
    854  * AhMan March 18 2005 Start the Original Linksys PPPoE  
    855  */ 
    856 /* 
    857  * This function build the pppoe instuction & execute it. 
    858  */ 
    859 #ifdef HAVE_PPPOE 
    860 void start_pppoe(int pppoe_num) 
    861 { 
    862         char idletime[20], retry_num[20], param[4]; 
    863         char username[80], passwd[80]; 
    864  
    865         char ppp_username[2][20] = { "ppp_username", "ppp_username_1" }; 
    866         char ppp_passwd[2][20] = { "ppp_passwd", "ppp_passwd_1" }; 
    867         char ppp_demand[2][20] = { "ppp_demand", "ppp_demand_1" }; 
    868         char ppp_service[2][20] = { "ppp_service", "ppp_service_1" }; 
    869         char ppp_ac[2][10] = { "ppp_ac", "ppp_ac_1" }; 
    870         // char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" }; 
    871         // char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" }; 
    872         // char wangw[2][15] = { "wan_gateway", "wan_gateway_1" }; 
    873         char pppoeifname[15]; 
    874         char *wan_ifname = nvram_safe_get("wan_ifname"); 
    875  
    876         if (isClient()) { 
    877                 wan_ifname = getSTA(); 
    878         } 
    879  
    880         pid_t pid; 
    881  
    882         sprintf(pppoeifname, "pppoe_ifname%d", pppoe_num); 
    883         nvram_set(pppoeifname, ""); 
    884  
    885         cprintf("start session %d\n", pppoe_num); 
    886         sprintf(idletime, "%d", atoi(nvram_safe_get("ppp_idletime")) * 60); 
    887         snprintf(retry_num, sizeof(retry_num), "%d", 
    888                  (atoi(nvram_safe_get("ppp_redialperiod")) / 5) - 1); 
    889  
    890         snprintf(username, sizeof(username), "%s", 
    891                  nvram_safe_get(ppp_username[pppoe_num])); 
    892         snprintf(passwd, sizeof(passwd), "%s", 
    893                  nvram_safe_get(ppp_passwd[pppoe_num])); 
    894         sprintf(param, "%d", pppoe_num); 
    895         /* 
    896          * add here  
    897          */ 
    898         char *pppoe_argv[] = { "pppoecd", 
    899                 wan_ifname, 
    900                 "-u", username, 
    901                 "-p", passwd, 
    902                 "-r", nvram_safe_get("wan_mtu"),        // del by honor, add by 
    903                 // tallest. 
    904                 "-t", nvram_safe_get("wan_mtu"), 
    905                 "-i", nvram_match(ppp_demand[pppoe_num], "1") ? idletime : "0", 
    906                 "-I", "30",     // Send an LCP echo-request frame to the 
    907                 // server every 30 seconds 
    908                 "-T", "3",      // pppd will presume the server to be dead if  
    909                 // 5 LCP echo-requests are sent without 
    910                 // receiving a valid LCP echo-reply 
    911                 "-P", param,    // PPPOE session number. 
    912                 "-N", retry_num,        // To avoid kill pppd when pppd has been 
    913                 // connecting. 
    914 #if LOG_PPPOE == 2 
    915                 "-d", 
    916 #endif 
    917                 "-C", "disconnected_pppoe",     // by tallest 0407 
    918                 NULL,           /* set default route */ 
    919                 NULL, NULL,     /* pppoe_service */ 
    920                 NULL, NULL,     /* pppoe_ac */ 
    921                 NULL,           /* pppoe_keepalive */ 
    922                 NULL 
    923         }, **arg; 
    924         /* 
    925          * Add optional arguments  
    926          */ 
    927         for (arg = pppoe_argv; *arg; arg++) ; 
    928  
    929         /* 
    930          * Removed by AhMan  
    931          */ 
    932  
    933         if (pppoe_num == PPPOE0) {      // PPPOE0 must set default route. 
    934                 *arg++ = "-R"; 
    935         } 
    936  
    937         if (nvram_invmatch(ppp_service[pppoe_num], "")) { 
    938                 *arg++ = "-s"; 
    939                 *arg++ = nvram_safe_get(ppp_service[pppoe_num]); 
    940         } 
    941         if (nvram_invmatch(ppp_ac[pppoe_num], "")) { 
    942                 *arg++ = "-a"; 
    943                 *arg++ = nvram_safe_get(ppp_ac[pppoe_num]); 
    944         } 
    945         if (nvram_match("ppp_static", "1")) { 
    946                 *arg++ = "-L"; 
    947                 *arg++ = nvram_safe_get("ppp_static_ip"); 
    948         } 
    949         // if (nvram_match("pppoe_demand", "1") || nvram_match("pppoe_keepalive",  
    950         // "1")) 
    951         *arg++ = "-k"; 
    952  
    953         mkdir("/tmp/ppp", 0777); 
    954         symlink("/sbin/rc", "/tmp/ppp/ip-up"); 
    955         symlink("/sbin/rc", "/tmp/ppp/ip-down"); 
    956         symlink("/sbin/rc", "/tmp/ppp/set-pppoepid");   // tallest 1219 
    957         unlink("/tmp/ppp/log"); 
    958  
    959         // Clean rpppoe client files - Added by ice-man (Wed Jun 1) 
    960         unlink("/tmp/ppp/options.pppoe"); 
    961         unlink("/tmp/ppp/connect-errors"); 
    962  
    963         _evalpid(pppoe_argv, NULL, 0, &pid); 
    964  
    965         if (nvram_match(ppp_demand[pppoe_num], "1")) { 
    966                 // int timeout = 5; 
    967                 start_tmp_ppp(pppoe_num); 
    968  
    969                 // This should be handled in start_wan_done 
    970                 // while (ifconfig (nvram_safe_get (pppoeifname), IFUP, NULL, NULL) 
    971                 // && timeout--) 
    972                 // sleep (1); 
    973                 // route_add (nvram_safe_get ("wan_iface"), 0, "0.0.0.0", 
    974                 // "10.112.112.112", 
    975                 // "0.0.0.0"); 
    976  
    977         } 
    978         cprintf("done. session %d\n", pppoe_num); 
    979         return; 
    980 } 
    981 #endif 
    982 /* 
    983  * AhMan March 18 2005  
    984  */ 
    985 /* 
    986  * Get the IP, Subnetmask, Geteway from WAN interface 
    987  * and set to NV ram. 
    988  */ 
    989 void start_tmp_ppp(int num) 
    990 { 
    991  
    992         int timeout = 5; 
    993         char pppoeifname[15]; 
    994         char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" }; 
    995         char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" }; 
    996         char wangw[2][15] = { "wan_gateway", "wan_gateway_1" }; 
    997         // char wanif[2][15]={"wan_ifname","wan_ifname_1"}; 
    998         // char *wan_ifname = nvram_safe_get("wan_ifname"); 
    999         struct ifreq ifr; 
    1000         int s; 
    1001  
    1002         cprintf("start session %d\n", num); 
    1003  
    1004         sprintf(pppoeifname, "pppoe_ifname%d", num); 
    1005  
    1006         if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 
    1007                 return; 
    1008  
    1009         /* 
    1010          * Wait for ppp0 to be created  
    1011          */ 
    1012         while (ifconfig(nvram_safe_get(pppoeifname), IFUP, NULL, NULL) 
    1013                && timeout--) 
    1014                 sleep(1); 
    1015  
    1016         strncpy(ifr.ifr_name, nvram_safe_get(pppoeifname), IFNAMSIZ); 
    1017  
    1018         /* 
    1019          * Set temporary IP address  
    1020          */ 
    1021         timeout = 3; 
    1022         while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) { 
    1023                 perror(nvram_safe_get(pppoeifname)); 
    1024                 printf("Wait %s inteface to init (1) ...\n", 
    1025                        nvram_safe_get(pppoeifname)); 
    1026                 sleep(1); 
    1027         }; 
    1028         nvram_set(wanip[num], inet_ntoa(sin_addr(&(ifr.ifr_addr)))); 
    1029         nvram_set(wanmask[num], "255.255.255.255"); 
    1030  
    1031         /* 
    1032          * Set temporary P-t-P address  
    1033          */ 
    1034         timeout = 3; 
    1035         while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) { 
    1036                 perror(nvram_safe_get(pppoeifname)); 
    1037                 printf("Wait %s inteface to init (2) ...\n", 
    1038                        nvram_safe_get(pppoeifname)); 
    1039                 sleep(1); 
    1040         } 
    1041         nvram_set(wangw[num], inet_ntoa(sin_addr(&(ifr.ifr_dstaddr)))); 
    1042  
    1043         start_wan_done(nvram_safe_get(pppoeifname)); 
    1044  
    1045         // if user press Connect" button from web, we must force to dial 
    1046         if (nvram_match("action_service", "start_pppoe") 
    1047             || nvram_match("action_service", "start_pppoe_1")) { 
    1048                 sleep(3); 
    1049                 // force_to_dial(nvram_safe_get("action_service")); 
    1050                 start_force_to_dial(); 
    1051                 nvram_unset("action_service"); 
    1052         } 
    1053  
    1054         close(s); 
    1055         cprintf("done session %d\n", num); 
    1056         return; 
    1057 } 
    1058  
    1059285// ===================================================================================================== 
    1060286 
    1061 #ifdef HAVE_L2TP 
    1062 void start_l2tp(int status) 
    1063 { 
    1064         int ret; 
    1065         FILE *fp; 
    1066         char *l2tp_argv[] = { "xl2tpd", 
    1067                 NULL 
    1068         }; 
    1069         char username[80], passwd[80]; 
    1070  
    1071         // stop_dhcpc(); 
    1072 #ifdef HAVE_PPPOE 
    1073         stop_pppoe(); 
    1074 #endif 
    1075 #ifdef HAVE_PPTP 
    1076         stop_pptp(); 
    1077 #endif 
    1078         stop_l2tp(); 
    1079         stop_l2tp(); 
    1080  
    1081         snprintf(username, sizeof(username), "%s", 
    1082                  nvram_safe_get("ppp_username")); 
    1083         snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd")); 
    1084  
    1085         if (status != REDIAL) { 
    1086                 mkdir("/tmp/ppp", 0777); 
    1087                 mkdir("/var/run/xl2tpd", 0777); 
    1088                 mkdir("/tmp/xl2tpd", 0777); 
    1089                 symlink("/sbin/rc", "/tmp/ppp/ip-up"); 
    1090                 symlink("/sbin/rc", "/tmp/ppp/ip-down"); 
    1091                 symlink("/dev/null", "/tmp/ppp/connect-errors"); 
    1092  
    1093                 /* 
    1094                  * Generate L2TP configuration file  
    1095                  */ 
    1096                 if (!(fp = fopen("/tmp/xl2tpd/xl2tpd.conf", "w"))) { 
    1097                         perror("/tmp/xl2tpd/xl2tpd.conf"); 
    1098                         return; 
    1099                 } 
    1100 /*[global] 
    1101 port = 1701 
    1102 ;auth file = /etc/xl2tpd/xl2tp-secrets 
    1103  
    1104 [lac fbnl2tpserver] 
    1105 lns = 10.64.1.237 
    1106 require chap = yes 
    1107 refuse pap = yes 
    1108 require authentication = yes 
    1109 ; Name should be the same as the username in the PPP authentication! 
    1110 name = dani 
    1111 ppp debug = yes 
    1112 pppoptfile = /etc/xl2tpd/options.l2tp 
    1113 length bit = yes 
    1114 */ 
    1115  
    1116                 fprintf(fp, "[global]\n");      // Global section 
    1117                 fprintf(fp, "port = 1701\n");   // Bind address 
    1118                 fprintf(fp, "[lac %s]\n", nvram_safe_get("l2tp_server_name")); 
    1119                 fprintf(fp, "lns = %s\n", nvram_safe_get("l2tp_server_name")); 
    1120                 fprintf(fp, "require chap = yes\n"); 
    1121                 fprintf(fp, "refuse pap = yes\n"); 
    1122                 fprintf(fp, "require authentication = yes\n"); 
    1123                 fprintf(fp, "name = %s\n", username); 
    1124                 fprintf(fp, "pppoptfile = /tmp/ppp/options\n"); 
    1125                 fprintf(fp, "length bit = yes\n"); 
    1126                 fclose(fp); 
    1127  
    1128                 /* 
    1129                  * Generate options file  
    1130                  */ 
    1131                 if (!(fp = fopen("/tmp/ppp/options", "w"))) { 
    1132                         perror("/tmp/ppp/options"); 
    1133                         return; 
    1134                 } 
    1135  
    1136                 if (nvram_match("mtu_enable", "1")) { 
    1137                         if (atoi(nvram_safe_get("wan_mtu")) > 0) { 
    1138                                 fprintf(fp, "mtu %s\n", 
    1139                                         nvram_safe_get("wan_mtu")); 
    1140                                 fprintf(fp, "mru %s\n", 
    1141                                         nvram_safe_get("wan_mtu")); 
    1142                         } 
    1143  
    1144                 } 
    1145  
    1146                 fprintf(fp, "defaultroute\n");  // Add a default route to the  
    1147                 // system routing tables, 
    1148                 // using the peer as the 
    1149                 // gateway 
    1150                 fprintf(fp, "usepeerdns\n");    // Ask the peer for up to 2 DNS 
    1151                 // server addresses 
    1152                 // fprintf(fp, "pty 'pptp %s 
    1153                 // --nolaunchpppd'\n",nvram_safe_get("pptp_server_ip"));  
    1154                 fprintf(fp, "user '%s'\n", username); 
    1155                 // fprintf(fp, "persist\n"); // Do not exit after a connection is 
    1156                 // terminated. 
    1157  
    1158                 if (nvram_match("ppp_demand", "1")) {   // demand mode 
    1159                         fprintf(fp, "idle %d\n", 
    1160                                 nvram_match("ppp_demand", 
    1161                                             "1") ? 
    1162                                 atoi(nvram_safe_get("ppp_idletime")) * 60 : 0); 
    1163                         // fprintf(fp, "demand\n"); // Dial on demand 
    1164                         // fprintf(fp, "persist\n"); // Do not exit after a connection is  
    1165                         // terminated. 
    1166                         // fprintf(fp, "%s:%s\n",PPP_PSEUDO_IP,PPP_PSEUDO_GW); // <local 
    1167                         // IP>:<remote IP> 
    1168                         fprintf(fp, "ipcp-accept-remote\n"); 
    1169                         fprintf(fp, "ipcp-accept-local\n"); 
    1170                         fprintf(fp, "connect true\n"); 
    1171                         fprintf(fp, "noipdefault\n");   // Disables the default 
    1172                         // behaviour when no local IP  
    1173                         // address is specified 
    1174                         fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr 
    1175                         // to 1 in demand mode if the local 
    1176                         // address changes 
    1177                 } else {        // keepalive mode 
    1178                         start_redial(); 
    1179                 } 
    1180  
    1181                 fprintf(fp, "default-asyncmap\n");      // Disable asyncmap 
    1182                 fprintf(fp, "crtscts\n");       // Disable protocol field compression 
    1183                 // negotiation 
    1184                 fprintf(fp, "nopcomp\n");       // Disable protocol field compression 
    1185                 fprintf(fp, "refuse-eap\n");    // Disable protocol field compression 
    1186                 fprintf(fp, "noaccomp\n");      // Disable Address/Control 
    1187                 // compression  
    1188                 fprintf(fp, "noccp\n"); // Disable CCP (Compression Control 
    1189                 // Protocol) 
    1190                 fprintf(fp, "novj\n");  // Disable Van Jacobson style TCP/IP 
    1191                 // header compression 
    1192                 fprintf(fp, "nobsdcomp\n");     // Disables BSD-Compress compression 
    1193                 fprintf(fp, "nodeflate\n");     // Disables Deflate compression 
    1194                 fprintf(fp, "lcp-echo-interval 0\n");   // Don't send an LCP 
    1195                 // echo-request frame to the 
    1196                 // peer 
    1197                 fprintf(fp, "lock\n"); 
    1198                 fprintf(fp, "noauth\n"); 
    1199 //              fprintf(fp, "debug\n"); 
    1200  
    1201                 fclose(fp); 
    1202  
    1203                 /* 
    1204                  * Generate pap-secrets file  
    1205                  */ 
    1206                 if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) { 
    1207                         perror("/tmp/ppp/pap-secrets"); 
    1208                         return; 
    1209                 } 
    1210                 fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd); 
    1211                 fclose(fp); 
    1212                 chmod("/tmp/ppp/pap-secrets", 0600); 
    1213  
    1214                 /* 
    1215                  * Generate chap-secrets file  
    1216                  */ 
    1217                 if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) { 
    1218                         perror("/tmp/ppp/chap-secrets"); 
    1219                         return; 
    1220                 } 
    1221                 fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd); 
    1222                 fclose(fp); 
    1223                 chmod("/tmp/ppp/chap-secrets", 0600); 
    1224  
    1225                 /* 
    1226                  * Enable Forwarding  
    1227                  */ 
    1228                 if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) { 
    1229                         fputc('1', fp); 
    1230                         fclose(fp); 
    1231                 } else 
    1232                         perror("/proc/sys/net/ipv4/ip_forward"); 
    1233         } 
    1234  
    1235         /* 
    1236          * Bring up WAN interface  
    1237          */ 
    1238         // ifconfig(nvram_safe_get("wan_ifname"), IFUP, 
    1239         // nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask")); 
    1240  
    1241         ret = _evalpid(l2tp_argv, NULL, 0, NULL); 
    1242         sleep(1); 
    1243  
    1244         if (nvram_match("ppp_demand", "1")) { 
    1245                 /* 
    1246                  * Trigger Connect On Demand if user press Connect button in Status 
    1247                  * page  
    1248                  */ 
    1249                 if (nvram_match("action_service", "start_l2tp")) { 
    1250                         start_force_to_dial(); 
    1251                         nvram_unset("action_service"); 
    1252                 } 
    1253                 /* 
    1254                  * Trigger Connect On Demand if user ping pptp server  
    1255                  */ 
    1256                 else 
    1257                         eval("listen", nvram_safe_get("lan_ifname")); 
    1258         } else { 
    1259                 sysprintf("echo \"c %s\" >  /var/run/xl2tpd/l2tp-control", 
    1260                           nvram_safe_get("l2tp_server_name")); 
    1261         } 
    1262  
    1263         cprintf("done\n"); 
    1264         return; 
    1265 } 
    1266  
    1267 void start_l2tp_redial(void) 
    1268 { 
    1269         start_l2tp(REDIAL); 
    1270 } 
    1271  
    1272 void start_l2tp_boot(void) 
    1273 { 
    1274         start_l2tp(BOOT); 
    1275 } 
    1276  
    1277 void stop_l2tp(void) 
    1278 { 
    1279         route_del(nvram_safe_get("wan_ifname"), 0, 
    1280                   nvram_safe_get("l2tp_server_ip"), NULL, NULL); 
    1281  
    1282         unlink("/tmp/ppp/link"); 
    1283         // ret = killps("pppd","-9"); 
    1284         // ret += killps("l2tpd","-9"); 
    1285         // ret += killps("listen","-9"); 
    1286  
    1287         if (pidof("pppd") > 0 || pidof("xl2tpd")) { 
    1288                 killall("pppd", SIGTERM); 
    1289                 killall("xl2tpd", SIGKILL); 
    1290                 killall("listen", SIGKILL); 
    1291         } 
    1292         cprintf("done\n"); 
    1293         return; 
    1294 } 
    1295 #endif 
    1296287 
    1297288void stop_wland(void) 
Note: See TracChangeset for help on using the changeset viewer.