int start_vpnc(void) { FILE *fp; char options[80]; char *pppd_argv[] = { "/usr/sbin/pppd", "file", options, NULL}; char tmp[100], prefix[] = "vpnc_", wan_prefix[] = "wanXXXXXXXXXX_"; char buf[256]; /* although maximum length of pppoe_username/pppoe_passwd is 64. pppd accepts up to 256 characters. */ mode_t mask; int ret = 0; // _dprintf("%s: unit=%d.\n", __FUNCTION__, unit); // snprintf(prefix, sizeof(prefix), "vpn%d_", unit); snprintf(wan_prefix, sizeof(wan_prefix), "wan%d_", wan_primary_ifunit()); if (nvram_match(strcat_r(wan_prefix, "proto", tmp), "pptp") || nvram_match(strcat_r(wan_prefix, "proto", tmp), "l2tp")) return 0; if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp")) sprintf(options, "/tmp/ppp/vpnc_options.pptp"); else if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp")) sprintf(options, "/tmp/ppp/vpnc_options.l2tp"); else return 0; update_vpnc_state(prefix, WAN_STATE_INITIALIZING, 0); mask = umask(0000); /* Generate options file */ if (!(fp = fopen(options, "w"))) { perror(options); umask(mask); return -1; } umask(mask); /* do not authenticate peer and do not use eap */ fprintf(fp, "noauth\n"); fprintf(fp, "refuse-eap\n"); handle_special_char_for_vpnclient(buf, sizeof(buf), nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp))); fprintf(fp, "user '%s'\n", buf); handle_special_char_for_vpnclient(buf, sizeof(buf), nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp))); fprintf(fp, "password '%s'\n", buf); if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp")) { fprintf(fp, "plugin pptp.so\n"); fprintf(fp, "pptp_server '%s'\n", nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ? nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) : nvram_safe_get(strcat_r(prefix, "gateway_x", tmp))); fprintf(fp, "vpnc 1\n"); /* see KB Q189595 -- historyless & mtu */ fprintf(fp, "nomppe-stateful mtu 1400\n"); if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "-mppc")) { fprintf(fp, "nomppe nomppc\n"); } else if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-40")) { fprintf(fp, "nomppe-56\n" "nomppe-128\n" "require-mppe\n" "require-mppe-40\n"); } else if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-56")) { fprintf(fp, "nomppe-40\n" "nomppe-128\n" "require-mppe\n" "require-mppe-56\n"); } else if (nvram_match(strcat_r(prefix, "pptp_options_x", tmp), "+mppe-128")) { fprintf(fp, "nomppe-40\n" "nomppe-56\n" "require-mppe\n" "require-mppe-128\n"); } } else { fprintf(fp, "nomppe nomppc\n"); } if (nvram_invmatch(strcat_r(prefix, "proto", tmp), "l2tp")) { ret = nvram_get_int(strcat_r(prefix, "pppoe_idletime", tmp)); if (ret && nvram_get_int(strcat_r(prefix, "pppoe_demand", tmp))) { fprintf(fp, "idle %d ", ret); if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0")) fprintf(fp, "tx_only "); fprintf(fp, "demand\n"); } fprintf(fp, "persist\n"); } fprintf(fp, "holdoff %d\n", nvram_get_int(strcat_r(prefix, "pppoe_holdoff", tmp)) ? : 10); fprintf(fp, "maxfail %d\n", nvram_get_int(strcat_r(prefix, "pppoe_maxfail", tmp))); if (nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0")) fprintf(fp, "usepeerdns\n"); fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n"); fprintf(fp, "ktune\n"); /* pppoe set these options automatically */ /* looks like pptp also likes them */ fprintf(fp, "default-asyncmap nopcomp noaccomp\n"); /* pppoe disables "vj bsdcomp deflate" automagically */ /* ccp should still be enabled - mppe/mppc requires this */ fprintf(fp, "novj nobsdcomp nodeflate\n"); /* echo failures */ fprintf(fp, "lcp-echo-interval 6\n"); fprintf(fp, "lcp-echo-failure 10\n"); /* pptp has Echo Request/Reply, l2tp has Hello packets */ if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp") || nvram_match(strcat_r(prefix, "proto", tmp), "l2tp")) fprintf(fp, "lcp-echo-adaptive\n"); fprintf(fp, "unit %d\n", vpnc_unit); fprintf(fp, "linkname vpn%d\n", vpnc_unit); fprintf(fp, "ip-up-script %s\n", "/tmp/ppp/vpnc-ip-up"); fprintf(fp, "ip-down-script %s\n", "/tmp/ppp/vpnc-ip-down"); fprintf(fp, "ip-pre-up-script %s\n", "/tmp/ppp/vpnc-ip-pre-up"); fprintf(fp, "auth-fail-script %s\n", "/tmp/ppp/vpnc-auth-fail"); #ifdef RTCONFIG_IPV6 switch (get_ipv6_service()) { case IPV6_NATIVE: case IPV6_NATIVE_DHCP: case IPV6_MANUAL: fprintf(fp, "+ipv6\n"); break; } #endif /* user specific options */ fprintf(fp, "%s\n", nvram_safe_get(strcat_r(prefix, "pppoe_options_x", tmp))); fclose(fp); /* shut down previous instance if any */ stop_vpnc(); if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp")) { if (!(fp = fopen("/tmp/l2tp.conf", "w"))) { perror(options); return -1; } fprintf(fp, "# automagically generated\n" "global\n\n" "load-handler \"sync-pppd.so\"\n" "load-handler \"cmd.so\"\n\n" "section sync-pppd\n\n" "lac-pppd-opts \"file %s\"\n\n" "section peer\n" "port 1701\n" "peername %s\n" "vpnc 1\n" "hostname %s\n" "lac-handler sync-pppd\n" "persist yes\n" "maxfail %d\n" "holdoff %d\n" "hide-avps no\n" "section cmd\n\n", options, nvram_invmatch(strcat_r(prefix, "heartbeat_x", tmp), "") ? nvram_safe_get(strcat_r(prefix, "heartbeat_x", tmp)) : nvram_safe_get(strcat_r(prefix, "gateway_x", tmp)), nvram_invmatch(strcat_r(prefix, "hostname", tmp), "") ? nvram_safe_get(strcat_r(prefix, "hostname", tmp)) : "localhost", nvram_get_int(strcat_r(prefix, "pppoe_maxfail", tmp)) ? : 32767, nvram_get_int(strcat_r(prefix, "pppoe_holdoff", tmp)) ? : 10); fclose(fp); /* launch l2tp */ eval("/usr/sbin/l2tpd"); ret = 3; do { _dprintf("%s: wait l2tpd up at %d seconds...\n", __FUNCTION__, ret); usleep(1000*1000); } while (!pids("l2tpd") && ret--); /* start-session */ ret = eval("/usr/sbin/l2tp-control", "start-session 0.0.0.0"); /* pppd sync nodetach noaccomp nobsdcomp nodeflate */ /* nopcomp novj novjccomp file /tmp/ppp/options.l2tp */ } else
// used for various testing static int rctest_main(int argc, char *argv[]) { int on; if (argc < 2) { _dprintf("test what?\n"); } else if (strcmp(argv[1], "rc_service")==0) { notify_rc(argv[2]); } else if(strcmp(argv[1], "get_phy_status")==0) { int mask; mask = atoi(argv[2]); TRACE_PT("debug for phy_status %x\n", get_phy_status(mask)); } else if(strcmp(argv[1], "get_phy_speed")==0) { int mask; mask = atoi(argv[2]); TRACE_PT("debug for phy_speed %x\n", get_phy_speed(mask)); } else if(strcmp(argv[1], "set_phy_ctrl")==0) { int mask, ctrl; mask = atoi(argv[2]); ctrl = atoi(argv[3]); TRACE_PT("debug for phy_speed %x\n", set_phy_ctrl(mask, ctrl)); } else if(strcmp(argv[1], "handle_notifications")==0) { handle_notifications(); } else if(strcmp(argv[1], "check_action")==0) { _dprintf("check: %d\n", check_action()); } else if(strcmp(argv[1], "nvramhex")==0) { int i; char *nv; nv = nvram_safe_get(argv[2]); _dprintf("nvram %s(%d): ", nv, strlen(nv)); for(i=0;i<strlen(nv);i++) { _dprintf(" %x", (unsigned char)*(nv+i)); } _dprintf("\n"); } else { on = atoi(argv[2]); _dprintf("%s %d\n", argv[1], on); if (strcmp(argv[1], "vlan") == 0) { if(on) start_vlan(); else stop_vlan(); } else if (strcmp(argv[1], "lan") == 0) { if(on) start_lan(); else stop_lan(); } else if (strcmp(argv[1], "wl") == 0) { if(on) { start_wl(); lanaccess_wl(); } } else if (strcmp(argv[1], "wan") == 0) { if(on) start_wan(); else stop_wan(); } else if (strcmp(argv[1], "wan_port") == 0) { if(on) start_wan_port(); else stop_wan_port(); } else if (strcmp(argv[1], "firewall") == 0) { //if(on) start_firewall(); //else stop_firewall(); } else if (strcmp(argv[1], "watchdog") == 0) { if(on) start_watchdog(); else stop_watchdog(); } #if ! (defined(RTCONFIG_QCA) || defined(RTCONFIG_RALINK)) else if (strcmp(argv[1], "watchdog02") == 0) { if(on) start_watchdog02(); else stop_watchdog02(); } #endif /* ! (RTCONFIG_QCA || RTCONFIG_RALINK) */ else if (strcmp(argv[1], "sw_devled") == 0) { if(on) start_sw_devled(); else stop_sw_devled(); } #ifdef RTCONFIG_FANCTRL else if (strcmp(argv[1], "phy_tempsense") == 0) { if(on) start_phy_tempsense(); else stop_phy_tempsense(); } #endif #ifdef RTCONFIG_BCMWL6 #ifdef RTCONFIG_PROXYSTA else if (strcmp(argv[1], "psta_monitor") == 0) { if(on) start_psta_monitor(); else stop_psta_monitor(); } #endif #endif #ifdef RTCONFIG_IPERF else if (strcmp(argv[1], "monitor") == 0) { if(on) start_monitor(); else stop_monitor(); } #endif else if (strcmp(argv[1], "qos") == 0) {//qos test if(on){ #ifdef RTCONFIG_RALINK if (module_loaded("hw_nat")) { modprobe_r("hw_nat"); sleep(1); #if 0 f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "0", 0, 0); f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "0", 0, 0); #endif } #endif add_iQosRules(get_wan_ifname(wan_primary_ifunit())); #ifdef RTCONFIG_BWDPI if(nvram_get_int("qos_type") == 1) { start_dpi_engine_service(); // force to rebuild firewall to avoid some loopback issue if (nvram_match("fw_nat_loopback", "2")) start_firewall(wan_primary_ifunit(), 0); } else #endif start_iQos(); } else { #ifdef RTCONFIG_RALINK if (nvram_get_int("hwnat") && /* TODO: consider RTCONFIG_DUALWAN case */ // !nvram_match("wan0_proto", "l2tp") && // !nvram_match("wan0_proto", "pptp") && // !(nvram_get_int("fw_pt_l2tp") || nvram_get_int("fw_pt_ipsec") && // (nvram_match("wl0_radio", "0") || nvram_get_int("wl0_mrate_x")) && // (nvram_match("wl1_radio", "0") || nvram_get_int("wl1_mrate_x")) && !module_loaded("hw_nat")) { #if 0 f_write_string("/proc/sys/net/ipv4/conf/default/force_igmp_version", "2", 0, 0); f_write_string("/proc/sys/net/ipv4/conf/all/force_igmp_version", "2", 0, 0); #endif #if defined(RTN14U) || defined(RTAC52U) || defined(RTAC51U) || defined(RTN11P) || defined(RTN300) || defined(RTN54U) || defined(RTAC1200HP) || defined(RTN56UB1) || defined(RTAC54U) || defined(RTN56UB2) if (!(!nvram_match("switch_wantag", "none")&&!nvram_match("switch_wantag", ""))) #endif { modprobe("hw_nat"); sleep(1); } } #endif #ifdef RTCONFIG_BWDPI if(nvram_get_int("qos_type") == 1){ stop_dpi_engine_service(1); } else #endif stop_iQos(); del_iQosRules(); } } #ifdef RTCONFIG_WEBDAV else if (strcmp(argv[1], "webdav") == 0) { if(on) start_webdav(); } #endif #ifdef RTCONFIG_TUNNEL else if (strcmp(argv[1], "mastiff") == 0) { if(on) start_mastiff(); } #endif else if (strcmp(argv[1], "gpiow") == 0) { if(argc>=4) set_gpio(atoi(argv[2]), atoi(argv[3])); } else if (strcmp(argv[1], "gpior") == 0) { printf("%d\n", get_gpio(atoi(argv[2]))); } else if (strcmp(argv[1], "gpiod") == 0) { if(argc>=4) gpio_dir(atoi(argv[2]), atoi(argv[3])); } else if (strcmp(argv[1], "init_switch") == 0) { init_switch(); } else if (strcmp(argv[1], "set_action") == 0) { set_action(on); } else if (strcmp(argv[1], "pwr_usb") == 0) { set_pwr_usb(atoi(argv[2])); _dprintf("done.\n"); } else if (strcmp(argv[1], "enc_chk") == 0) { unsigned char enc_buf[ENC_WORDS_LEN]; unsigned char dec_buf[DATA_WORDS_LEN + 1]; _dprintf("get enc str:[%s]\n", enc_str(argv[2], (char *) enc_buf)); _dprintf("get dec str:[%s]\n", dec_str((char *) enc_buf, (char *) dec_buf)); _dprintf("done(%d)\n", strcmp(argv[2], (const char *) dec_buf)); } #ifdef RTCONFIG_BCMFA else if (strcmp(argv[1], "fa_rev") == 0) { _dprintf("(%d) done.\n", get_fa_rev()); } else if (strcmp(argv[1], "fa_dump") == 0) { _dprintf("(%d) done.\n", get_fa_dump()); } #endif else { printf("what?\n"); } } return 0; }