1/*
2 * ppp scripts
3 *
4 * Copyright 2004, Broadcom Corporation
5 * All Rights Reserved.
6 *
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11 *
12 * $Id: ppp.c,v 1.1.1.1 2008/10/15 03:28:48 james26_jang Exp $
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <string.h>
19#include <net/route.h>
20#include <sys/ioctl.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24#include <errno.h>
25#include <ctype.h>
26
27#include <bcmnvram.h>
28#include <netconf.h>
29#include <shutils.h>
30#include <rc.h>
31
32/*
33* parse ifname to retrieve unit #
34*/
35int
36ppp_ifunit(char *ifname)
37{
38	if (strncmp(ifname, "ppp", 3))
39		return -1;
40	if (!isdigit(ifname[3]))
41		return -1;
42	return atoi(&ifname[3]);
43}
44
45/*
46 * Called when link comes up
47 */
48int
49ipup_main(int argc, char **argv)
50{
51	FILE *fp;
52	char *wan_ifname = safe_getenv("IFNAME");
53	char *value;
54	char buf[256];
55	int unit;
56	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
57
58	dprintf("%s\n", argv[0]);
59
60	umask(022);
61
62	if ((unit = ppp_ifunit(wan_ifname)) < 0)
63		return -1;
64
65	snprintf(prefix, sizeof(prefix), "wan%d_", unit);
66
67	/* Touch connection file */
68	if (!(fp = fopen(strcat_r("/tmp/ppp/link.", wan_ifname, tmp), "a"))) {
69		perror(tmp);
70		return errno;
71	}
72	fclose(fp);
73
74	if (!nvram_get(strcat_r(prefix, "ifname", tmp)))
75		return -1;
76
77	if ((value = getenv("IPLOCAL"))) {
78		ifconfig(wan_ifname, IFUP,
79			 value, "255.255.255.255");
80		nvram_set(strcat_r(prefix, "ipaddr", tmp), value);
81		nvram_set(strcat_r(prefix, "netmask", tmp), "255.255.255.255");
82	}
83
84        if ((value = getenv("IPREMOTE")))
85		nvram_set(strcat_r(prefix, "gateway", tmp), value);
86	strcpy(buf, "");
87	if (getenv("DNS1"))
88		sprintf(buf, "%s", getenv("DNS1"));
89	if (getenv("DNS2"))
90		sprintf(buf + strlen(buf), "%s%s", strlen(buf) ? " " : "", getenv("DNS2"));
91	nvram_set(strcat_r(prefix, "dns", tmp), buf);
92
93	wan_up(wan_ifname);
94
95	logmessage(nvram_safe_get("wan_proto_t"), "connect to ISP");
96	wanmessage("");
97
98	dprintf("done\n");
99	return 0;
100}
101
102/*
103 * Called when link goes down
104 */
105int
106ipdown_main(int argc, char **argv)
107{
108	char *wan_ifname = safe_getenv("IFNAME");
109	int unit;
110	char tmp[100], prefix[] = "wanXXXXXXXXXX_";
111
112	umask(022);
113
114	if ((unit = ppp_ifunit(wan_ifname)) < 0)
115		return -1;
116
117	snprintf(prefix, sizeof(prefix), "wan%d_", unit);
118
119	if (!nvram_get(strcat_r(prefix, "ifname", tmp)))
120		return -1;
121
122	wan_down(wan_ifname);
123
124	unlink(strcat_r("/tmp/ppp/link.", wan_ifname, tmp));
125
126	preset_wan_routes(wan_ifname);
127
128	logmessage(nvram_safe_get("wan_proto_t"), "Disconnected");
129	wanmessage(pppstatus(tmp));
130
131	return 0;
132}
133
134