1/*
2 * Copyright 2004, ASUSTek Inc.
3 * All Rights Reserved.
4 *
5 * THIS SOFTWARE IS OFFERED "AS IS", AND ASUS GRANTS NO WARRANTIES OF ANY
6 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
7 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
8 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
9 *
10 * $Id: network_ex.c,v 1.3 2009/03/03 02:39:54 james26_jang Exp $
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <errno.h>
16#include <syslog.h>
17#include <ctype.h>
18#include <string.h>
19#include <unistd.h>
20#include <sys/stat.h>
21#include <sys/ioctl.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <net/if.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <net/if_arp.h>
28#include <signal.h>
29
30#include <bcmnvram.h>
31#include <netconf.h>
32#include <shutils.h>
33#include <wlutils.h>
34#include <nvparse.h>
35#include <rc.h>
36#include <bcmutils.h>
37
38#ifdef REMOVE
39int start_pppoe(void)
40{
41
42	int timeout = 5;
43	char pppunit[] = "XXXXXXXXXXXX";
44
45	/* Add optional arguments */
46	for (arg = pppoe_argv; *arg; arg++);
47	if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), "")) {
48		*arg++ = "-s";
49				*arg++ = nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp));
50			}
51			if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) {
52				*arg++ = "-a";
53				*arg++ = nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp));
54			}
55			if (nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1") ||
56			    nvram_match(strcat_r(prefix, "pppoe_keepalive", tmp), "1"))
57				*arg++ = "-k";
58			snprintf(pppunit, sizeof(pppunit), "%d", unit);
59			*arg++ = "-U";
60			*arg++ = pppunit;
61
62			/* launch pppoe client daemon */
63
64
65			/* ppp interface name is referenced from this point on */
66			wan_ifname = nvram_safe_get(strcat_r(prefix, "pppoe_ifname", tmp));
67
68			/* Pretend that the WAN interface is up */
69			if (nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1")) {
70				/* Wait for pppx to be created */
71				while (ifconfig(wan_ifname, IFUP, NULL, NULL) && timeout--)
72					sleep(1);
73
74				/* Retrieve IP info */
75				if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
76					continue;
77				strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
78
79				/* Set temporary IP address */
80				if (ioctl(s, SIOCGIFADDR, &ifr))
81					perror(wan_ifname);
82				nvram_set(strcat_r(prefix, "ipaddr", tmp), inet_ntoa(sin_addr(&ifr.ifr_addr)));
83				nvram_set(strcat_r(prefix, "netmask", tmp), "255.255.255.255");
84
85				/* Set temporary P-t-P address */
86				if (ioctl(s, SIOCGIFDSTADDR, &ifr))
87					perror(wan_ifname);
88				nvram_set(strcat_r(prefix, "gateway", tmp), inet_ntoa(sin_addr(&ifr.ifr_dstaddr)));
89
90				close(s);
91
92				/*
93				* Preset routes so that traffic can be sent to proper pppx even before
94				* the link is brought up.
95				*/
96				preset_wan_routes(wan_ifname);
97}
98#endif
99
100int start_pppd(char *prefix)
101{
102	int ret;
103	FILE *fp;
104	char options[80];
105	char *pppd_argv[] = { "pppd", "file", options, NULL};
106	char tmp[100];
107	mode_t mask;
108
109	sprintf(options, "/tmp/ppp/options.wan%s", nvram_safe_get(strcat_r(prefix, "unit", tmp)));
110
111	mask = umask(066);
112
113	/* Generate options file */
114	if (!(fp = fopen(options, "w"))) {
115		perror(options);
116		umask(mask);
117		return -1;
118	}
119
120	umask(mask);
121
122	/* do not authenticate peer and do not use eap */
123	fprintf(fp, "noauth refuse-eap\n");
124	fprintf(fp, "user '%s'\n", nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp)));
125	fprintf(fp, "password '%s'\n", nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp)));
126
127	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp"))
128	{
129		fprintf(fp, "connect true\n");
130		/*fprintf(fp, "sync pty '/usr/sbin/pptp %s --nolaunchpppd --nobuffer --sync'\n",
131				nvram_invmatch("wan_heartbeat_x", "")?nvram_safe_get("wan_heartbeat_x"):nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)));
132		fprintf(fp, "lock\n");//*/
133		fprintf(fp, "plugin pptp.so\n");
134		fprintf(fp, "pptp_server '%s'\n",
135				nvram_invmatch("wan_heartbeat_x", "")?nvram_safe_get("wan_heartbeat_x"):nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)));//*/
136
137		/* see KB Q189595 -- historyless & mtu */
138		fprintf(fp, "nomppe-stateful %s mtu 1400\n", nvram_safe_get(strcat_r(prefix, "pptp_options_x", tmp)));
139	}
140	else {
141		fprintf(fp, "nomppe nomppc\n");
142	}
143
144	if (nvram_match(strcat_r(prefix, "proto", tmp), "pppoe"))
145	{
146		fprintf(fp, "plugin rp-pppoe.so");
147
148		if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), "")) {
149			fprintf(fp, " rp_pppoe_service '%s'\n",
150					nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp)));
151		}
152
153		if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) {
154			fprintf(fp, " rp_pppoe_ac '%s'\n",
155					nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp)));
156		}
157
158		fprintf(fp, " nic-%s\n", nvram_safe_get(strcat_r(prefix, "ifname", tmp)));
159
160		fprintf(fp, "mru %s mtu %s\n",
161				nvram_safe_get(strcat_r(prefix, "pppoe_mru", tmp)),
162				nvram_safe_get(strcat_r(prefix, "pppoe_mtu", tmp)));
163	}
164
165	if (atoi(nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp))) &&
166			nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1"))
167	{
168		fprintf(fp, "idle %s ", nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp)));
169		if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0")) {
170			fprintf(fp, "tx_only ");
171		}
172		fprintf(fp, "demand\n");
173	}
174
175	fprintf(fp, "maxfail 0\n");
176
177	if(nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0"))
178		fprintf(fp, "usepeerdns\n");
179
180	if (nvram_invmatch(strcat_r(prefix, "proto", tmp), "l2tp"))
181		fprintf(fp, "persist\n");
182
183	fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n");
184	fprintf(fp, "ktune\n");
185
186	/* pppoe set these options automatically */
187	/* looks like pptp also likes them */
188	fprintf(fp, "default-asyncmap nopcomp noaccomp\n");
189
190	/* pppoe disables "vj bsdcomp deflate" automagically */
191	/* ccp should still be enabled - mppe/mppc requires this */
192	fprintf(fp, "novj nobsdcomp nodeflate\n");
193
194	/* echo failures */
195	fprintf(fp, "lcp-echo-interval 10\n");
196	fprintf(fp, "lcp-echo-failure 6\n");
197
198	fprintf(fp, "unit %s\n", nvram_get(strcat_r(prefix, "unit", tmp)) ? : "0");
199
200	/* user specific options */
201	fprintf(fp, "%s\n", nvram_safe_get(strcat_r(prefix, "pppoe_options_x", tmp)));
202
203	fclose(fp);
204
205	if (nvram_match(strcat_r(prefix, "proto", tmp), "l2tp"))
206	{
207		if (!(fp = fopen("/tmp/l2tp.conf", "w"))) {
208			perror(options);
209			return -1;
210		}
211
212		fprintf(fp, "# automagically generated\n"
213				"global\n\n"
214				"load-handler \"sync-pppd.so\"\n"
215				"load-handler \"cmd.so\"\n\n"
216				"section sync-pppd\n\n"
217				"lac-pppd-opts \"file %s\"\n\n"
218				"section peer\n"
219				"peername %s\n"
220				"lac-handler sync-pppd\n"
221				"persist yes\n"
222				"maxfail %s\n"
223				"holdoff %s\n"
224				"section cmd\n\n",
225				options,
226				nvram_invmatch("wan_heartbeat_x", "")?nvram_safe_get("wan_heartbeat_x"):nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)),
227				nvram_invmatch(strcat_r(prefix, "pppoe_maxfail", tmp), "")?nvram_safe_get(strcat_r(prefix, "pppoe_maxfail", tmp)):"32767",
228				nvram_invmatch(strcat_r(prefix, "pppoe_holdoff", tmp), "")?nvram_safe_get(strcat_r(prefix, "pppoe_holdoff", tmp)):"30");
229
230		fclose(fp);
231
232		/* launch l2tp */
233		eval("l2tpd");
234
235		sleep(1);
236
237		/* start-session */
238		ret = eval("l2tp-control", "start-session 0.0.0.0");
239
240		/* pppd sync nodetach noaccomp nobsdcomp nodeflate */
241		/* nopcomp novj novjccomp file /tmp/ppp/options.l2tp */
242	} else{
243		eval("killall", "pppd");
244		ret = _eval(pppd_argv, NULL, 0, NULL);
245	}
246
247	return ret;
248}
249
250void start_pppoe_relay(char *wan_if)
251{
252	if (nvram_match("wan_pppoe_relay_x", "1"))
253	{
254		char *pppoerelay_argv[] = {"pppoe-relay", "-C", "br0", "-S", wan_if, NULL};
255		int ret;
256		pid_t pid;
257
258		ret = _eval(pppoerelay_argv, NULL, 0, &pid);
259	}
260}
261
262void setup_ethernet(char *wan_if)
263{
264	//if (nvram_invmatch("wan_etherspeed_x", "auto"))
265}
266
267#ifdef CDMA
268int write_cdma_conf(){
269	FILE *fp;
270	char username[32], passwd[32];
271	char ttyUSB_num_str[2];
272	int ttyUSB_num = -1;
273
274	/* Get user name and password*/
275	memset(username, 0, 32);
276	strcpy(username, nvram_safe_get("hsdpa_username"));
277	memset(passwd, 0, 32);
278	strcpy(passwd, nvram_safe_get("hsdpa_passwd"));
279
280	/* Setting pppd config file */
281	if(!(fp = fopen("/tmp/ppp/peers/cdma", "w"))){
282		perror("/tmp/ppp/peers/cdma");
283		return -1;
284	}
285
286//	fprintf(fp, "-detach\n");
287	if(nvram_match("hsdpa_combo", "1")){
288		memset(ttyUSB_num_str, 0, 2);
289		strcpy(ttyUSB_num_str, nvram_safe_get("hsdpa_ttyUSB_num"));
290		ttyUSB_num = atoi(ttyUSB_num_str);
291
292		if(!strcmp(nvram_safe_get("got_HSDPA"), "1")){
293			;
294		}
295		else if(strlen(ttyUSB_num_str) > 0){
296			++ttyUSB_num;
297			ttyUSB_num %= 4;
298			ttyUSB_num_str[0] = '0'+ttyUSB_num;
299			nvram_set("hsdpa_ttyUSB_num", ttyUSB_num_str);
300		}
301		else{
302			ttyUSB_num = 0;
303			nvram_set("hsdpa_ttyUSB_num", "0");
304		}
305printf("--- ttyUSB_num=%d. ---\n", ttyUSB_num);
306		fprintf(fp, "/dev/ttyUSB%d\n", ttyUSB_num);
307	}
308	else
309		fprintf(fp, "/dev/ttyACM0\n");
310	fprintf(fp, "460800\n");
311	fprintf(fp, "modem\n");
312	fprintf(fp, "noauth\n");
313	fprintf(fp, "defaultroute\n");
314	fprintf(fp, "noipdefault\n");
315	if(nvram_match("wan_dnsenable_x", "1"))
316		fprintf(fp, "usepeerdns\n");
317//	fprintf(fp, "-detach\n");
318//	fprintf(fp, "usehostname\n");
319	fprintf(fp, "debug\n");
320//	fprintf(fp, "local\n");
321	fprintf(fp, "user %s\n", username);
322	fprintf(fp, "password %s\n", passwd);
323//	fprintf(fp, "persist\n");
324//	fprintf(fp, "show-password\n");
325	fprintf(fp, "crtscts\n");
326	fprintf(fp, "noccp\n");
327	fprintf(fp, "novj\n");
328	fprintf(fp, "nodeflate\n");
329	fprintf(fp, "nobsdcomp\n");
330	fprintf(fp, "connect-delay 5000\n");
331	//fprintf(fp, "connect '/usr/sbin/chat -vf /tmp/ppp/peers/cdma_chat'\n");
332	fprintf(fp, "connect '/usr/sbin/chat -svf /tmp/ppp/peers/cdma_chat'\n");
333	fprintf(fp, "mru %s\n", nvram_safe_get("wan_hsdpa_mru"));
334	fprintf(fp, "mtu %s\n", nvram_safe_get("wan_hsdpa_mtu"));
335//	fprintf(fp, "disconnect '/usr/sbin/chat -vf /tmp/ppp/peers/cdma_disconnect'\n");
336	fprintf(fp, "lcp-echo-interval 10\n");
337	fprintf(fp, "lcp-echo-failure 6\n");
338
339	fclose(fp);
340
341	/* Create auth file */
342	if(!(fp = fopen("/tmp/ppp/chap-secrets", "w"))){
343		perror("/tmp/ppp/chap-secrets");
344		return -1;
345	}
346
347	fprintf(fp, "\"%s\" * \"%s\" *\n", username, passwd);
348
349	fclose(fp);
350
351	/* Writing chat file */
352	if(!(fp = fopen("/tmp/ppp/peers/cdma_chat", "w"))){
353		perror("/tmp/ppp/peers/cdma_chat");
354		return -1;
355	}
356
357//	fprintf(fp, "'' 'AT'\n");
358//	fprintf(fp, "'OK' 'ATE0V1&F&D2&C1&C2S0=0'\n");
359//	fprintf(fp, "'OK' 'ATE0V1'\n");
360//	fprintf(fp, "'OK' 'ATS7=60'\n");
361//	fprintf(fp, "'OK' 'ATDT#777'\n");
362//	fprintf(fp, "ABORT BUSY\n");
363//	fprintf(fp, "ABORT ERROR\n");
364//	fprintf(fp, "ABORT VOICE\n");
365	fprintf(fp, "ABORT 'NO CARRIER'\n");
366	fprintf(fp, "ABORT 'NO DAILTONE'\n");
367//	fprintf(fp, "ABORT 'NO DAIL TONE'\n");
368//	fprintf(fp, "ABORT 'NO ANSWER'\n");
369//	fprintf(fp, "REPORT CONNECT\n");
370//	fprintf(fp, "TIMEOUT 10\n");
371//	fprintf(fp, "OK ATZ\n");
372	fprintf(fp, "\"\" AT\n");
373	fprintf(fp, "OK AT+CFUN=1\n");
374	//if(nvram_match("enable_apn", "1"))
375	if(strcmp(nvram_safe_get("private_apn"), ""))
376		fprintf(fp, "OK \'AT+CGDCONT=1, \"ip\",\"%s\"\'\n", nvram_safe_get("private_apn"));
377//	fprintf(fp, "OK \'AT+CGDCONT=1, \"ip\",\"internet\"\'\n");
378	/*if(nvram_match("pin_code_enable", "1"))
379		fprintf(fp, "OK AT+CPIN=\"%s\"\n", nvram_safe_get("pin_code"));//*/
380//	if(nvram_invmatch("wan_pppoe_passwd", ""))
381//		fprintf(fp, "OK AT+CPIN=\"%s\"\n", passwd);
382//	fprintf(fp, "\\d \\d \\d \\d \\d \n");
383//	fprintf(fp, "OK ATQ0V1E1S0=0&C1&D2+FCLASS=0\n");
384	fprintf(fp, "OK ATQ0V1E1S0=0&C1&D2\n");
385//	fprintf(fp, "OK \"AT+IPR=115200\"\n");
386//	fprintf(fp, "OK \"ATE1\"\n");
387//	fprintf(fp, "TIMEOUT 60\n");
388	fprintf(fp, "\"\" ATDT%s\n", nvram_safe_get("hsdpa_dial_number"));
389	fprintf(fp, "CONNECT \"\"\n");
390//	fprintf("\n");
391
392	fclose(fp);
393
394	return 0;
395}
396
397int start_cdma(void) // HSDPA
398{
399	int ret;
400	char *cdma_argv[] = {"pppd", "call", "cdma", NULL};
401
402	write_cdma_conf();
403
404	eval("killall", "pppd");
405
406	// Call CMDA connection
407csprintf("--- Wait to start HSDPA... ---\n");
408	//sleep(10);
409	sleep(2);
410csprintf("--- Start HSDPA... ---\n");
411	_eval(cdma_argv, NULL, 0, NULL);
412
413	return 0;
414}
415
416int
417stop_cdma(void)
418{
419        int ret;
420
421	ret = eval("killall", "pppd");
422	ret += eval("killall", "chat");
423        dprintf("done\n");
424
425        return ret;
426}
427#endif
428