root/src/router/services/networking/ppp.c

Revision 12432, 7.6 kB (checked in by BrainSlayer, 5 months ago)

hso support

Line 
1 /*
2  * ppp.c
3  *
4  * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * $Id:
21  */
22
23 #include <stdio.h>
24 #include <syslog.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <net/route.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <errno.h>
34 #include <time.h>
35
36 #include <bcmnvram.h>
37 #include <netconf.h>
38 #include <shutils.h>
39 #include <rc.h>
40 #include "ledcontrol.h"
41
42 #include <code_pattern.h>
43 #include <cy_conf.h>
44 #include <utils.h>
45 #include <services.h>
46
47 #define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
48
49
50 char *getenvs(char *env)
51 {
52 static unsigned char r[64];
53 char *e=getenv(env);
54 int c=0;
55 int i;
56 for (i=0;i<strlen(e);i++)
57     {
58     if (e[i]!=' ')
59         r[c++]=e[i];
60     }
61 return r;
62 }
63 /*
64  * Called when link comes up
65  */
66 int ipup_main(int argc, char **argv)
67 {
68         FILE *fp;
69         char *wan_ifname = safe_getenv("IFNAME");
70
71         // char *wan_proto = nvram_safe_get("wan_proto");
72         char *value;
73         char buf[256];
74
75         cprintf("%s\n", argv[0]);
76
77         killall("listen", SIGKILL);
78         nvram_set("wan_iface", wan_ifname);
79         if (check_action() != ACT_IDLE)
80                 return -1;
81
82         /*
83          * ipup will receive bellow six arguments
84          */
85         /*
86          * interface-name tty-device speed local-IP-address remote-IP-address
87          * ipparam
88          */
89
90         /*
91          * Touch connection file
92          */
93         if (!(fp = fopen("/tmp/ppp/link", "a"))) {
94                 perror("/tmp/ppp/link");
95                 return errno;
96         }
97         fprintf(fp, "%s", argv[1]);
98         fclose(fp);
99
100         nvram_set("pppd_pppifname", wan_ifname);
101
102         if (nvram_match("wan_proto", "pppoe"))
103                 nvram_set("pppoe_ifname", wan_ifname);
104
105         if ((value = getenvs("IPLOCAL"))) {
106                 ifconfig(wan_ifname, IFUP, value, "255.255.255.255");
107                 if (nvram_match("wan_proto", "pppoe")) {
108                         nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
109                         nvram_set("wan_ipaddr", value);
110                         nvram_set("wan_netmask", "255.255.255.255");
111                 } else if (nvram_match("wan_proto", "pptp")) {
112                         nvram_set("wan_ipaddr_buf", nvram_safe_get("pptp_get_ip"));     // Store
113                         // last
114                         // ip
115                         // address
116                         nvram_set("wan_ipaddr", value);
117                         nvram_set("wan_netmask", "255.255.255.255");
118                         nvram_set("pptp_get_ip", value);
119                 } else if (nvram_match("wan_proto", "l2tp")) {
120                         nvram_set("wan_ipaddr_buf", nvram_safe_get("l2tp_get_ip"));     // Store
121                         // last
122                         // ip
123                         // address
124                         nvram_set("l2tp_get_ip", value);
125                 }
126 #ifdef HAVE_3G
127                 else if (nvram_match("wan_proto", "3g")) {
128                         nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
129                         nvram_set("wan_ipaddr", value);
130                         nvram_set("wan_netmask", "255.255.255.255");
131                 }
132 #endif
133         }
134
135         if ((value = getenvs("IPREMOTE"))) {
136                 nvram_set("wan_gateway", value);
137                 if (nvram_match("wan_proto", "pptp")) {
138                         eval("route", "del", "default");
139                         route_add(wan_ifname, 0, "0.0.0.0", value, "0.0.0.0");
140                 }
141         }
142         strcpy(buf, "");
143         if (getenvs("DNS1"))
144                 sprintf(buf, "%s", getenvs("DNS1"));
145         if (getenvs("DNS2"))
146                 sprintf(buf + strlen(buf), "%s%s", strlen(buf) ? " " : "",
147                         getenvs("DNS2"));
148         nvram_set("wan_get_dns", buf);
149
150         if ((value = getenv("AC_NAME")))
151                 nvram_set("ppp_get_ac", value);
152         if ((value = getenv("SRV_NAME")))
153                 nvram_set("ppp_get_srv", value);
154         if ((value = getenv("MTU")))
155                 nvram_set("wan_run_mtu", value);
156         start_wan_done(wan_ifname);
157         cprintf("done\n");
158         return 0;
159 }
160
161 /*
162  * Called when link goes down
163  */
164 int ipdown_main(int argc, char **argv)
165 {
166         if (check_action() != ACT_IDLE)
167                 return -1;
168         stop_ddns();
169         stop_ntpc();
170
171         unlink("/tmp/ppp/link");
172
173         if (nvram_match("wan_proto", "l2tp")) {
174                 /*
175                  * clear dns from the resolv.conf
176                  */
177                 nvram_set("wan_get_dns", "");
178                 dns_to_resolv();
179
180                 // todo
181                 route_del(nvram_safe_get("wan_ifname"), 0,
182                           nvram_safe_get("l2tp_server_ip"),
183                           nvram_safe_get("wan_gateway_buf"), "255.255.255.255");
184                 /*
185                  * Restore the default gateway for WAN interface
186                  */
187                 nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));
188
189                 /*
190                  * Set default route to gateway if specified
191                  */
192                 route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0",
193                           nvram_safe_get("wan_gateway"), "0.0.0.0");
194         }
195         if (nvram_match("wan_proto", "pptp")) {
196                 eval("route", "del", "default");
197                 nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));
198                 eval("route", "add", "default", "gw",
199                      nvram_safe_get("wan_gateway"));
200                 sysprintf
201                     ("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE\n",
202                      nvram_safe_get("pptp_ifname"));
203         }
204
205         nvram_set("pppoe_ifname", "");
206         nvram_set("pppd_pppifname", "");
207
208         // write PPP traffic statistics to nvram if wanted
209         if (nvram_match("ppp_traffic", "1")) {
210                 char buffer[64];
211                 long long old_in, old_out;
212                 long long in, out;
213                 char *pin;
214                 char *pout;
215                 time_t stamp;
216
217                 old_in = atol(nvram_safe_get("ppp_byte_in"));
218                 old_out = atol(nvram_safe_get("ppp_byte_out"));
219
220                 if ((pin = getenv("BYTES_RCVD")))
221                         in = atol(pin);
222                 else
223                         in = 0;
224
225                 if ((pout = getenv("BYTES_SENT")))
226                         out = atol(pout);
227                 else
228                         out = 0;
229
230                 in += old_in;
231                 out += old_out;
232                 snprintf(buffer, 63, "%lld", in);
233                 nvram_set("ppp_byte_in", buffer);
234                 snprintf(buffer, 63, "%lld", out);
235                 nvram_set("ppp_byte_out", buffer);
236                 if ((stamp = time(NULL)) < 1087818160)  // clock is not set
237                         // properly
238                         stamp = 0;
239                 snprintf(buffer, 63, "%ld", stamp);
240                 nvram_set("ppp_byte_stamp", buffer);
241                 nvram_commit();
242         }
243
244         if (nvram_match("ppp_demand", "1")
245             && (nvram_match("wan_proto", "pptp")
246                 || nvram_match("wan_proto", "l2tp"))) {
247                 killall("listen", SIGKILL);
248                 eval("listen", nvram_safe_get("lan_ifname"));
249         }
250
251         return 1;
252 }
253
254 /*
255  * int pppevent_main (int argc, char **argv) { int argn; char *type = NULL;
256  *
257  * argn = 1; while (argn < argc && argv[argn][0] == '-') { if (strcmp
258  * (argv[argn], "-t") == 0) { ++argn; type = argv[argn]; } ++argn; }
259  *
260  * if (!type) return 1;
261  *
262  *
263  * if (!strcmp (type, "PAP_AUTH_FAIL") || !strcmp (type, "CHAP_AUTH_FAIL")) {
264  *
265  * buf_to_file ("/tmp/ppp/log", type);
266  *
267  * if (check_hw_type () == BCM4704_BCM5325F_CHIP) SET_LED (GET_IP_ERROR); }
268  *
269  * return 0; }
270  *
271  * //=============================================================================
272  * int set_pppoepid_to_nv_main (int argc, char **argv) // tallest 1219 { if
273  * (!strcmp (argv[1], "0")) { nvram_set ("pppoe_pid0", getenv ("PPPD_PID"));
274  * nvram_set ("pppoe_ifname0", getenv ("IFNAME")); } else if (!strcmp (argv[1],
275  * "1")) { nvram_set ("pppoe_pid1", getenv ("PPPD_PID")); nvram_set
276  * ("pppoe_ifname1", getenv ("IFNAME")); }
277  *
278  * dprintf ("done.( IFNAME = %s DEVICE = %s )\n", getenv ("IFNAME"), getenv
279  * ("DEVICE")); return 0; }
280  */
281
282 // by tallest 0407
283 int disconnected_pppoe_main(int argc, char **argv)
284 {
285         int pppoe_num = atoi(argv[1]);
286         char ppp_demand[2][20] = { "ppp_demand", "ppp_demand_1" };
287
288         if (nvram_match(ppp_demand[pppoe_num], "1")
289             && nvram_match("action_service", "")) {
290                 cprintf("tallest:=====( kill pppoe %d )=====\n", pppoe_num);
291                 stop_single_pppoe(pppoe_num);
292                 start_pppoe(pppoe_num);
293                 dns_to_resolv();
294
295                 stop_dnsmasq();
296                 start_dnsmasq();
297
298                 return 0;
299         }
300         cprintf("tallest:=====( PPPOE Dial On Demand Error!! )=====\n");
301         return 0;
302 }
303
304 // =============================================================================
Note: See TracBrowser for help on using the browser.