Beispiel #1
0
static int bound(void)
{
	nvram_unset("dhcpc_done");
	char *wan_ifname = safe_getenv("interface");
	char *value;
	static char temp_wan_ipaddr[16], temp_wan_netmask[16],
	    temp_wan_gateway[16];
	int changed = 0;
	static char *cidr;
	if (nvram_match("wan_proto", "iphone"))
		stop_process("ipheth-loop", "IPhone Pairing Daemon");

	cidr = getenv("cidrroute");
	if (cidr && wan_ifname) {
		char *callbuffer = malloc(strlen(cidr) + 128);
		sprintf(callbuffer,
			"export cidrroute=\"%s\";export interface=\"%s\";/etc/cidrroute.sh",
			cidr, wan_ifname);
		system(callbuffer);
		free(callbuffer);
	}

	if ((value = getenv("ip"))) {
		chomp(value);
		if (nvram_match("wan_proto", "pptp")
		    && nvram_match("pptp_use_dhcp", "1"))
			strcpy(temp_wan_ipaddr, value);
		else {
			if (nvram_invmatch("wan_ipaddr", value))
				changed = 1;
		}
		nvram_set("wan_ipaddr", value);
	}
	if ((value = getenv("subnet"))) {
		chomp(value);
		if (nvram_match("wan_proto", "pptp")
		    && nvram_match("pptp_use_dhcp", "1"))
			strcpy(temp_wan_netmask, value);
		else {
			if (nvram_invmatch("wan_netmask", value))
				changed = 1;
			nvram_set("wan_netmask", value);
		}
	}
	if ((value = getenv("router"))) {
		chomp(value);
		if (nvram_invmatch("wan_gateway", value))
			changed = 1;
		nvram_set("wan_gateway", value);
	}
	if ((value = getenv("dns"))) {
		chomp(value);
		// if (nvram_invmatch("wan_get_dns",value))
		// changed=1; 
		nvram_set("wan_get_dns", value);
	}
	/*
	 * Don't care for linksys spec if ((value = getenv("wins")))
	 * nvram_set("wan_wins", value); if ((value = getenv("hostname")))
	 * sethostname(value, strlen(value) + 1); 
	 */
	if ((value = getenv("domain"))) {
		chomp(value);
		if (nvram_invmatch("wan_get_domain", value))
			changed = 1;
		nvram_set("wan_get_domain", value);	// HeartBeat need to use
	}
	if ((value = getenv("lease"))) {
		chomp(value);
		nvram_set("wan_lease", value);
		expires(atoi(value));
	}
	if (!changed) {
		cprintf("interface hasnt changed, do nothing\n");
		return 0;
	}
	stop_firewall();
	cprintf("configure to IF[%s] , IP[%s], MASK[%s]\n", wan_ifname,
		nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));

	if (nvram_match("wan_proto", "pptp")
	    && nvram_match("pptp_use_dhcp", "1"))
		eval("ifconfig", wan_ifname, temp_wan_ipaddr, "netmask",
		     temp_wan_netmask, "up");
	else
		eval("ifconfig", wan_ifname, nvram_safe_get("wan_ipaddr"),
		     "netmask", nvram_safe_get("wan_netmask"), "up");

	/*
	 * We only want to exec bellow functions after dhcp get ip if the
	 * wan_proto is heartbeat 
	 */
#ifdef HAVE_HEARTBEAT
	if (nvram_match("wan_proto", "heartbeat")) {
		int i = 0;

		/*
		 * Delete all default routes 
		 */
		while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0
		       || i++ < 10) ;

		/*
		 * Set default route to gateway if specified 
		 */
		route_add(wan_ifname, 0, "0.0.0.0",
			  nvram_safe_get("wan_gateway"), "0.0.0.0");

		/*
		 * save dns to resolv.conf 
		 */
		dns_to_resolv();
		stop_udhcpd();
		start_udhcpd();
		start_firewall();
		stop_wland();
		start_wshaper();
		start_wland();
		start_heartbeat_boot();
	}
#else
	if (0) {
		// nothing
	}
#endif
#ifdef HAVE_PPTP
	else if (nvram_match("wan_proto", "pptp")
		 && nvram_match("pptp_use_dhcp", "1")) {
		char pptpip[64];
		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("wan_gateway"),
					  "255.255.255.255");
			free(dns_list);
		}
		route_add(wan_ifname, 0, "0.0.0.0",
			  nvram_safe_get("wan_gateway"), "0.0.0.0");

		nvram_set("wan_gateway_buf", nvram_get("wan_gateway"));

		getIPFromName(nvram_safe_get("pptp_server_name"), pptpip);
		nvram_set("pptp_server_ip", pptpip);

		// Add the route to the PPTP server on the wan interface for pptp
		// client to reach it
		if (nvram_match("wan_gateway", "0.0.0.0")
		    || nvram_match("wan_netmask", "0.0.0.0"))
			route_add(wan_ifname, 0,
				  nvram_safe_get("pptp_server_ip"),
				  nvram_safe_get("wan_gateway"),
				  "255.255.255.255");
		else
			route_add(wan_ifname, 0,
				  nvram_safe_get("pptp_server_ip"),
				  nvram_safe_get("wan_gateway"),
				  nvram_safe_get("wan_netmask"));

	}
#endif
#ifdef HAVE_L2TP
	else if (nvram_match("wan_proto", "l2tp")) {
		char l2tpip[64];
		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("wan_gateway"),
					  "255.255.255.255");
			free(dns_list);
		}

		/*
		 * Backup the default gateway. It should be used if L2TP connection
		 * is broken 
		 */
		nvram_set("wan_gateway_buf", nvram_get("wan_gateway"));

		getIPFromName(nvram_safe_get("l2tp_server_name"), l2tpip);

		nvram_set("l2tp_server_ip", l2tpip);

		route_add(wan_ifname, 0,
			  nvram_safe_get("l2tp_server_ip"),
			  nvram_safe_get("wan_gateway"), "255.255.255.255");

		start_firewall();
		start_l2tp_boot();
	}
#endif
	else {
		cprintf("start wan done\n");
		start_wan_done(wan_ifname);
	}
	nvram_set("dhcpc_done", "1");
	cprintf("done\n");
	return 0;
}
Beispiel #2
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;
}