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.1.1.1 2008/10/15 03:28:35 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 *pptp_argv[] = { "pppd", "file", options, NULL};
106 	char tmp[100];
107	mode_t mask;
108
109	sprintf(options, "/tmp/ppp/options.wan%s",
110		nvram_safe_get(strcat_r(prefix, "unit", tmp)));
111
112	mask = umask(066);
113
114 	/* Generate options file */
115	if (!(fp = fopen(options, "w")))
116	{
117       	        perror(options);
118		umask(mask);
119        	return -1;
120 	}
121
122	umask(mask);
123
124#ifdef NOMPPE
125	fprintf(fp, "noauth\n");
126#else
127	/* do not authenticate peer and do not use eap */
128	fprintf(fp, "noauth refuse-eap\n");
129#endif
130	fprintf(fp, "user '%s'\n",
131       		nvram_safe_get(strcat_r(prefix, "pppoe_username", tmp)));
132	fprintf(fp, "password '%s'\n",
133		nvram_safe_get(strcat_r(prefix, "pppoe_passwd", tmp)));
134
135       	if (nvram_match(strcat_r(prefix, "proto", tmp), "pptp"))
136       	{
137		fprintf(fp, "connect true\n");
138		fprintf(fp, "pty '/usr/sbin/pptp %s --nolaunchpppd'\n",
139   			nvram_invmatch("wan_heartbeat_x", "") ?
140   			nvram_safe_get("wan_heartbeat_x") :
141   			nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)));
142		fprintf(fp, "lock\n");
143 	}
144	else if (nvram_match(strcat_r(prefix, "proto", tmp), "pppoe"))
145	{
146		fprintf(fp, "plugin rp-pppoe.so nic-%s\n", nvram_safe_get(strcat_r(prefix, "ifname", tmp)));
147
148		fprintf(fp, "mru %s\n", nvram_safe_get(strcat_r(prefix, "pppoe_mru", tmp)));
149		fprintf(fp, "mtu %s\n", nvram_safe_get(strcat_r(prefix, "pppoe_mtu", tmp)));
150
151		if (nvram_invmatch(strcat_r(prefix, "pppoe_service", tmp), ""))			{
152			fprintf(fp, "rp_pppoe_service '%s'\n",
153			nvram_safe_get(strcat_r(prefix, "pppoe_service", tmp)));
154		}
155		if (nvram_invmatch(strcat_r(prefix, "pppoe_ac", tmp), "")) {
156			fprintf(fp, "rp_pppoe_ac '%s'\n",
157			nvram_safe_get(strcat_r(prefix, "pppoe_ac", tmp)));
158		}
159	}
160
161	if (atoi(nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp))) &&
162	     nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1"))
163	{
164		fprintf(fp, "idle %s ", nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp)));
165		if (nvram_invmatch(strcat_r(prefix, "pppoe_txonly_x", tmp), "0"))
166		{
167			fprintf(fp, "tx_only ");
168		}
169		fprintf(fp, "demand\n");
170	}
171
172      	if (nvram_invmatch(strcat_r(prefix, "dnsenable_x", tmp), "0"))
173		fprintf(fp, "usepeerdns\n");
174
175	fprintf(fp, "defaultroute\n");
176       	fprintf(fp, "persist\n");
177	fprintf(fp, "ipcp-accept-remote ipcp-accept-local noipdefault\n");
178	fprintf(fp, "ktune\n");
179       	/* pppoe set these options automatically */
180       	/* looks like pptp also likes them */
181    	fprintf(fp, "default-asyncmap nopcomp noaccomp\n");
182	/* pppoe disables "vj bsdcomp deflate" automagically */
183	/* ccp should still be enabled - mppe/mppc requires this */
184       	fprintf(fp, "novj nobsdcomp nodeflate\n");
185       	/* echo failures */
186 	fprintf(fp, "lcp-echo-interval 10\n");
187	fprintf(fp, "lcp-echo-failure 6\n");
188	fprintf(fp, "unit %s\n",
189		nvram_get(strcat_r(prefix, "unit", tmp)) ? : "0");
190       	fclose(fp);
191
192	ret = _eval(pptp_argv, NULL, 0, NULL);
193}
194
195void start_pppoe_relay(char *wan_if)
196{
197	if (nvram_match("wan_pppoe_relay_x", "1"))
198	{
199		char *pppoerelay_argv[] = {"pppoe-relay", "-C", "br0", "-S", wan_if, "-F", NULL};
200		int ret;
201		pid_t pid;
202
203		ret = _eval(pppoerelay_argv, NULL, 0, &pid);
204	}
205}
206
207#ifdef CDMA
208int start_cdma(void)
209{
210	int ret;
211	FILE *fp;
212	char username[32],passwd[32];
213	char *cdma_argv[] = {"pppd","call","cdma", NULL};
214	pid_t pid;
215
216	/* Get user name and password*/
217
218	strcpy(username, nvram_safe_get("wan_pppoe_username"));
219	strcpy(passwd, nvram_safe_get("wan_pppoe_passwd"));
220
221	/* Setting pppd config file */
222
223//	printf("\nStart CDMA Configure!!\n");
224
225	if (!(fp = fopen("/tmp/ppp/peers/cdma", "w"))) {
226		perror("/tmp/ppp/peers/cdma");
227		return -1;
228	}
229
230	fprintf(fp, "-detach\n");
231	fprintf(fp, "ttyUSB0\n");
232	fprintf(fp, "230400\n");
233	fprintf(fp, "debug\n");
234	fprintf(fp, "noauth\n");
235	fprintf(fp, "defaultroute\n");
236	fprintf(fp, "usepeerdns\n");
237	fprintf(fp, "user %s\n", username);
238	fprintf(fp, "show-password\n");
239	fprintf(fp, "crtscts\n");
240	fprintf(fp, "lock\n");
241	fprintf(fp, "connect '/usr/sbin/chat -v -t3 -f /tmp/ppp/peers/cdma_chat'\n");
242
243	fclose(fp);
244
245	/* Create auth file */
246
247	if (!(fp = fopen("/tmp/ppp/chap-secrets", "w"))){
248		perror("/tmp/ppp/chap-secrets");
249		return -1;
250	}
251
252	fprintf(fp, "\"%s\" * \"%s\" *\n",
253		username,
254		passwd);
255	fclose(fp);
256
257
258	/* Writing chat file */
259	if (!(fp = fopen("/tmp/ppp/peers/cdma_chat", "w"))){
260		perror("/tmp/ppp/peers/cdma_chat");
261		return -1;
262	}
263
264	fprintf(fp, "'' 'AT'\n");
265	fprintf(fp, "'OK' 'ATE0V1&F&D2&C1&C2S0=0'\n");
266	fprintf(fp, "'OK' 'ATE0V1'\n");
267	fprintf(fp, "'OK' 'ATS7=60'\n");
268	fprintf(fp, "'OK' 'ATDT#777'\n");
269
270	fclose(fp);
271
272	/* Make connection nodes for USB modem*/
273
274	eval ("mknod", "/dev/ttyUSB0", "c", "188", "0");
275	eval ("mknod", "/dev/ttyUSB1", "c", "188", "1");
276
277	/*Call CMDA connection*/
278	sleep(10);
279	_eval (cdma_argv, NULL, 0, &pid);
280
281}
282
283int
284stop_cdma(void)
285{
286        int ret;
287
288        ret = eval("killall", "pppd");
289        ret += eval("killall", "pppd");
290        dprintf("done\n");
291
292        return ret;
293}
294#endif
295
296void setup_ethernet(char *wan_if)
297{
298#ifndef CHIP5350
299	//if (nvram_invmatch("wan_etherspeed_x", "auto"))
300	{
301		eval("et", "-i", wan_if, "speed", nvram_safe_get("wan_etherspeed_x"));
302	}
303#endif
304}
305