Ejemplo n.º 1
0
void start_pptp(int status)
{
	int ret;
	FILE *fp;
	char *pptp_argv[] = { "pppd",
		NULL
	};
	char username[80], passwd[80];

	stop_dhcpc();
#ifdef HAVE_PPPOE
	stop_pppoe();
#endif
	stop_vpn_modules();

	snprintf(username, sizeof(username), "%s",
		 nvram_safe_get("ppp_username"));
	snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));

	if (status != REDIAL) {
		create_pptp_config(nvram_safe_get("pptp_server_name"),
				   username);
		/*
		 * Generate pap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
			perror("/tmp/ppp/pap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/pap-secrets", 0600);

		/*
		 * Generate chap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
			perror("/tmp/ppp/chap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/chap-secrets", 0600);

		/*
		 * Enable Forwarding 
		 */
		if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
			fputc('1', fp);
			fclose(fp);
		} else
			perror("/proc/sys/net/ipv4/ip_forward");
	}
	char *wan_ifname = nvram_safe_get("wan_ifname");

	if (isClient()) {
		wan_ifname = getSTA();
	}

	nvram_set("pptp_ifname", wan_ifname);
	/*
	 * Bring up WAN interface 
	 */
	if (nvram_match("pptp_use_dhcp", "1")) {
		// pid_t pid;
		// char *wan_ipaddr;
		// char *wan_netmask;
		// char *wan_gateway;

		// char *pptp_server_ip = nvram_safe_get ("pptp_server_ip");
		// char *wan_hostname = nvram_safe_get ("wan_hostname");

		nvram_set("wan_get_dns", "");
		nvram_unset("dhcpc_done");
		//dirty hack
		start_dhcpc(wan_ifname, NULL, NULL, 1);
		int timeout;

		for (timeout = 60; !nvram_match("dhcpc_done", "1") && timeout > 0; --timeout) {	/* wait for info from dhcp server */
			sleep(1);
		}
		stop_dhcpc();	/* we don't need dhcp client anymore */
		create_pptp_config(nvram_safe_get("pptp_server_ip"), username);

	} else {
		ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"),
			 nvram_safe_get("wan_netmask"));
		struct dns_lists *dns_list = NULL;
		dns_to_resolv();
		dns_list = get_dns_list();
		int i = 0;

		if (dns_list) {
			for (i = 0; i < dns_list->num_servers; i++)
				route_add(wan_ifname, 0,
					  dns_list->dns_server[i],
					  nvram_safe_get("pptp_wan_gateway"),
					  "255.255.255.255");
		}
		route_add(wan_ifname, 0, "0.0.0.0",
			  nvram_safe_get("pptp_wan_gateway"), "0.0.0.0");
		char pptpip[64];
		getIPFromName(nvram_safe_get("pptp_server_name"), pptpip);
		route_del(wan_ifname, 0, "0.0.0.0",
			  nvram_safe_get("pptp_wan_gateway"), "0.0.0.0");
		if (dns_list) {
			for (i = 0; i < dns_list->num_servers; i++)
				route_del(wan_ifname, 0,
					  dns_list->dns_server[i],
					  nvram_safe_get("pptp_wan_gateway"),
					  "255.255.255.255");
			free(dns_list);
		}
		
		nvram_set("pptp_server_ip", pptpip);
		if (!nvram_match("pptp_wan_gateway", "0.0.0.0"))
			route_add(wan_ifname, 0,
				  nvram_safe_get("pptp_server_ip"),
				  nvram_safe_get("pptp_wan_gateway"),
				  "255.255.255.255");
	}
	ret = _evalpid(pptp_argv, NULL, 0, NULL);

	if (nvram_match("ppp_demand", "1")) {
		/*
		 * Trigger Connect On Demand if user press Connect button in Status
		 * page 
		 */
		if (nvram_match("action_service", "start_pptp")
		    || nvram_match("action_service", "start_l2tp")) {
			start_force_to_dial();
			// force_to_dial(nvram_safe_get("action_service"));
			nvram_unset("action_service");
		}
		/*
		 * Trigger Connect On Demand if user ping pptp server 
		 */
		else {
			eval("listen", nvram_safe_get("lan_ifname"));
		}
	}
	stop_wland();
	start_wshaper();
	start_wland();
	cprintf("done\n");
	return;
}
Ejemplo n.º 2
0
void start_l2tp(int status)
{
	int ret;
	FILE *fp;
	char *l2tp_argv[] = { "xl2tpd",
		NULL
	};
	char username[80], passwd[80];

	// stop_dhcpc();
#ifdef HAVE_PPPOE
	stop_pppoe();
#endif
#ifdef HAVE_PPTP
	stop_pptp();
#endif
	stop_l2tp();

	snprintf(username, sizeof(username), "%s",
		 nvram_safe_get("ppp_username"));
	snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));

	if (status != REDIAL) {
		insmod("ipv6");
		insmod("l2tp_core");
		insmod("l2tp_netlink");
		insmod("l2tp_ppp");
		mkdir("/tmp/ppp", 0777);
		mkdir("/var/run/xl2tpd", 0777);
		mkdir("/tmp/xl2tpd", 0777);
		symlink("/sbin/rc", "/tmp/ppp/ip-up");
		symlink("/sbin/rc", "/tmp/ppp/ip-down");
		symlink("/dev/null", "/tmp/ppp/connect-errors");

		/*
		 * Generate L2TP configuration file 
		 */
		if (!(fp = fopen("/tmp/xl2tpd/xl2tpd.conf", "w"))) {
			perror("/tmp/xl2tpd/xl2tpd.conf");
			return;
		}
/*[global]
port = 1701
;auth file = /etc/xl2tpd/xl2tp-secrets

[lac fbnl2tpserver]
lns = 10.64.1.237
require chap = yes
refuse pap = yes
require authentication = yes
; Name should be the same as the username in the PPP authentication!
name = dani
ppp debug = yes
pppoptfile = /etc/xl2tpd/options.l2tp
length bit = yes
*/

		fprintf(fp, "[global]\n");	// Global section
		fprintf(fp, "port = 1701\n");	// Bind address
		fprintf(fp, "[lac %s]\n", nvram_safe_get("l2tp_server_name"));
		fprintf(fp, "lns = %s\n", nvram_safe_get("l2tp_server_name"));
		fprintf(fp, "require chap = %s\n",
			nvram_default_get("l2tp_req_chap", "yes"));
		fprintf(fp, "refuse pap = %s\n",
			nvram_default_get("l2tp_ref_pap", "yes"));
		fprintf(fp, "redial = yes\n");
		fprintf(fp, "redial timeout = 15\n");
		fprintf(fp, "require authentication = %s\n",
			nvram_default_get("l2tp_req_auth", "yes"));
		fprintf(fp, "name = %s\n", username);
		fprintf(fp, "pppoptfile = /tmp/ppp/options\n");
		fprintf(fp, "length bit = yes\n");
		fclose(fp);

		/*
		 * Generate options file 
		 */
		if (!(fp = fopen("/tmp/ppp/options", "w"))) {
			perror("/tmp/ppp/options");
			return;
		}

		if (nvram_match("mtu_enable", "1")) {
			if (atoi(nvram_safe_get("wan_mtu")) > 0) {
				fprintf(fp, "mtu %s\n",
					nvram_safe_get("wan_mtu"));
				fprintf(fp, "mru %s\n",
					nvram_safe_get("wan_mtu"));
			}

		}

		fprintf(fp, "defaultroute\n");	// Add a default route to the 
		// system routing tables,
		// using the peer as the
		// gateway
		fprintf(fp, "usepeerdns\n");	// Ask the peer for up to 2 DNS
		// server addresses
		// fprintf(fp, "pty 'pptp %s
		// --nolaunchpppd'\n",nvram_safe_get("pptp_server_ip")); 
		fprintf(fp, "user '%s'\n", username);
		// fprintf(fp, "persist\n"); // Do not exit after a connection is
		// terminated.

		if (nvram_match("ppp_demand", "1")) {	// demand mode
			fprintf(fp, "idle %d\n",
				nvram_match("ppp_demand",
					    "1") ?
				atoi(nvram_safe_get("ppp_idletime")) * 60 : 0);
			// fprintf(fp, "demand\n"); // Dial on demand
			// fprintf(fp, "persist\n"); // Do not exit after a connection is 
			// terminated.
			// fprintf(fp, "%s:%s\n",PPP_PSEUDO_IP,PPP_PSEUDO_GW); // <local
			// IP>:<remote IP>
			fprintf(fp, "ipcp-accept-remote\n");
			fprintf(fp, "ipcp-accept-local\n");
			fprintf(fp, "connect true\n");
			fprintf(fp, "noipdefault\n");	// Disables the default
			// behaviour when no local IP 
			// address is specified
			fprintf(fp, "ktune\n");	// Set /proc/sys/net/ipv4/ip_dynaddr
			// to 1 in demand mode if the local
			// address changes
		} else {	// keepalive mode
			start_redial();
		}

		fprintf(fp, "default-asyncmap\n");	// Disable asyncmap
		fprintf(fp, "crtscts\n");	// Disable protocol field compression
		// negotiation
		fprintf(fp, "nopcomp\n");	// Disable protocol field compression
		fprintf(fp, "refuse-eap\n");	// Disable protocol field compression
		fprintf(fp, "noaccomp\n");	// Disable Address/Control
		// compression 
		fprintf(fp, "noccp\n");	// Disable CCP (Compression Control
		// Protocol)
		fprintf(fp, "novj\n");	// Disable Van Jacobson style TCP/IP
		// header compression
		fprintf(fp, "nobsdcomp\n");	// Disables BSD-Compress compression
		fprintf(fp, "nodeflate\n");	// Disables Deflate compression
		fprintf(fp, "lcp-echo-interval 0\n");	// Don't send an LCP
		// echo-request frame to the
		// peer
		fprintf(fp, "lock\n");
		fprintf(fp, "noauth\n");
//              fprintf(fp, "debug\n");

		fclose(fp);

		/*
		 * Generate pap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/pap-secrets", "w"))) {
			perror("/tmp/ppp/pap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/pap-secrets", 0600);

		/*
		 * Generate chap-secrets file 
		 */
		if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))) {
			perror("/tmp/ppp/chap-secrets");
			return;
		}
		fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
		fclose(fp);
		chmod("/tmp/ppp/chap-secrets", 0600);

		/*
		 * Enable Forwarding 
		 */
		if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) {
			fputc('1', fp);
			fclose(fp);
		} else
			perror("/proc/sys/net/ipv4/ip_forward");
	}

	/*
	 * Bring up WAN interface 
	 */
	// ifconfig(nvram_safe_get("wan_ifname"), IFUP,
	// nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));

	ret = _evalpid(l2tp_argv, NULL, 0, NULL);
	sleep(1);

	if (nvram_match("ppp_demand", "1")) {
		/*
		 * Trigger Connect On Demand if user press Connect button in Status
		 * page 
		 */
		if (nvram_match("action_service", "start_l2tp")) {
			start_force_to_dial();
			nvram_unset("action_service");
		}
		/*
		 * Trigger Connect On Demand if user ping pptp server 
		 */
		else
			eval("listen", nvram_safe_get("lan_ifname"));
	} else {
		sysprintf("echo \"c %s\" >  /var/run/xl2tpd/l2tp-control",
			  nvram_safe_get("l2tp_server_name"));
	}

	cprintf("done\n");
	return;
}
Ejemplo n.º 3
0
/*
 * Get the IP, Subnetmask, Geteway from WAN interface
 * and set to NV ram.
 */
void start_tmp_ppp(int num)
{

	int timeout = 5;
	char pppoeifname[15];
	char wanip[2][15] = { "wan_ipaddr", "wan_ipaddr_1" };
	char wanmask[2][15] = { "wan_netmask", "wan_netmask_1" };
	char wangw[2][15] = { "wan_gateway", "wan_gateway_1" };
	// char wanif[2][15]={"wan_ifname","wan_ifname_1"};
	// char *wan_ifname = nvram_safe_get("wan_ifname");
	struct ifreq ifr;
	int s;

	cprintf("start session %d\n", num);

	sprintf(pppoeifname, "pppoe_ifname%d", num);

	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
		return;

	/*
	 * Wait for ppp0 to be created 
	 */
	while (ifconfig(nvram_safe_get(pppoeifname), IFUP, NULL, NULL)
	       && timeout--)
		sleep(1);

	strncpy(ifr.ifr_name, nvram_safe_get(pppoeifname), IFNAMSIZ);

	/*
	 * Set temporary IP address 
	 */
	timeout = 3;
	while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--) {
		perror(nvram_safe_get(pppoeifname));
		printf("Wait %s inteface to init (1) ...\n",
		       nvram_safe_get(pppoeifname));
		sleep(1);
	};
	nvram_set(wanip[num], inet_ntoa(sin_addr(&(ifr.ifr_addr))));
	nvram_set(wanmask[num], "255.255.255.255");

	/*
	 * Set temporary P-t-P address 
	 */
	timeout = 3;
	while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--) {
		perror(nvram_safe_get(pppoeifname));
		printf("Wait %s inteface to init (2) ...\n",
		       nvram_safe_get(pppoeifname));
		sleep(1);
	}
	nvram_set(wangw[num], inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));

	start_wan_done(nvram_safe_get(pppoeifname));

	// if user press Connect" button from web, we must force to dial
	if (nvram_match("action_service", "start_pppoe")
	    || nvram_match("action_service", "start_pppoe_1")) {
		sleep(3);
		// force_to_dial(nvram_safe_get("action_service"));
		start_force_to_dial();
		nvram_unset("action_service");
	}

	close(s);
	cprintf("done session %d\n", num);
	return;
}