source: src/router/services/networking/ppp.c @ 17272

Last change on this file since 17272 was 17272, checked in by BrainSlayer, 23 months ago

do it the right way

File size: 8.3 KB
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
49char *getenvs(char *env)
50{
51        static unsigned char r[64];
52        char *e = getenv(env);
53        if (!e)
54            return NULL;       
55        int c = 0;
56        int i;
57        for (i = 0; i < strlen(e); i++) {
58                if (e[i] != ' ')
59                        r[c++] = e[i];
60        }
61        r[c++] = 0;             //terminate string
62        return r;
63}
64
65/*
66 * Called when link comes up
67 */
68int ipup_main(int argc, char **argv)
69{
70        FILE *fp;
71        char *wan_ifname = safe_getenv("IFNAME");
72
73        // char *wan_proto = nvram_safe_get("wan_proto");
74        char *value;
75        char buf[256];
76
77        cprintf("%s\n", argv[0]);
78
79        stop_process("listen", "activity listener");
80        nvram_set("wan_iface", wan_ifname);
81        if (check_action() != ACT_IDLE)
82                return -1;
83
84        /*
85         * ipup will receive bellow six arguments
86         */
87        /*
88         * interface-name tty-device speed local-IP-address remote-IP-address
89         * ipparam
90         */
91
92        /*
93         * Touch connection file
94         */
95        if (!(fp = fopen("/tmp/ppp/link", "a"))) {
96                perror("/tmp/ppp/link");
97                return errno;
98        }
99        fprintf(fp, "%s", argv[1]);
100        fclose(fp);
101
102        nvram_set("pppd_pppifname", wan_ifname);
103
104        if (nvram_match("wan_proto", "pppoe"))
105                nvram_set("pppoe_ifname", wan_ifname);
106
107        if (getenv("IPLOCAL")) {
108                value = getenvs("IPLOCAL");
109                ifconfig(wan_ifname, IFUP, value, "255.255.255.255");
110                if (nvram_match("wan_proto", "pppoe")) {
111                        nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
112                        nvram_set("wan_ipaddr", value);
113                        nvram_set("wan_netmask", "255.255.255.255");
114#ifdef HAVE_PPPOATM
115                } else if (nvram_match("wan_proto", "pppoa")) {
116                        nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
117                        nvram_set("wan_ipaddr", value);
118                        nvram_set("wan_netmask", "255.255.255.255");
119#endif
120#ifdef HAVE_PPTP
121                } else if (nvram_match("wan_proto", "pptp")) {
122                        nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
123                        nvram_set("pptp_get_ip", value);
124#endif
125#ifdef HAVE_L2TP
126                } else if (nvram_match("wan_proto", "l2tp")) {
127                        nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
128                        nvram_set("l2tp_get_ip", value);
129#endif
130                }
131#ifdef HAVE_3G
132                else if (nvram_match("wan_proto", "3g")) {
133                        nvram_set("wan_ipaddr_buf", nvram_safe_get("wan_ipaddr"));      // Store
134                        nvram_set("wan_ipaddr", value);
135                        nvram_set("wan_netmask", "255.255.255.255");
136                        // reset 3gnetmodetoggle for mode 5
137                        nvram_unset("3gnetmodetoggle");
138#if defined(HAVE_TMK) || defined(HAVE_BKM)
139                        char *gpio3g = nvram_get("gpio3g");
140                        if (gpio3g != NULL)
141                                set_gpio(atoi(gpio3g), 1);
142#endif
143                }
144#endif
145        }
146
147        if (getenv("IPREMOTE")) {
148                value = getenvs("IPREMOTE");
149                nvram_set("wan_gateway", value);
150                if (nvram_match("wan_proto", "pptp")) {
151                        eval("route", "del", "default");
152                        route_add(wan_ifname, 0, "0.0.0.0", value, "0.0.0.0");
153                }
154        }
155        strcpy(buf, "");
156        if (getenv("DNS1"))
157                sprintf(buf, "%s", getenvs("DNS1"));
158        if (getenv("DNS2"))
159                sprintf(buf + strlen(buf), "%s%s", strlen(buf) ? " " : "",
160                        getenvs("DNS2"));
161        nvram_set("wan_get_dns", buf);
162
163        if ((value = getenv("AC_NAME")))
164                nvram_set("ppp_get_ac", value);
165        if ((value = getenv("SRV_NAME")))
166                nvram_set("ppp_get_srv", value);
167        if ((value = getenv("MTU")))
168                nvram_set("wan_run_mtu", value);
169        start_wan_done(wan_ifname);
170        cprintf("done\n");
171        return 0;
172}
173
174/*
175 * Called when link goes down
176 */
177int ipdown_main(int argc, char **argv)
178{
179        if (check_action() != ACT_IDLE)
180                return -1;
181        stop_ddns();
182        stop_ntpc();
183
184        unlink("/tmp/ppp/link");
185
186        if (nvram_match("wan_proto", "l2tp")) {
187                /*
188                 * clear dns from the resolv.conf
189                 */
190                nvram_set("wan_get_dns", "");
191                dns_to_resolv();
192
193                // todo
194                route_del(nvram_safe_get("wan_ifname"), 0,
195                          nvram_safe_get("l2tp_server_ip"),
196                          nvram_safe_get("wan_gateway_buf"), "255.255.255.255");
197                /*
198                 * Restore the default gateway for WAN interface
199                 */
200                nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));
201
202                /*
203                 * Set default route to gateway if specified
204                 */
205                route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0",
206                          nvram_safe_get("wan_gateway"), "0.0.0.0");
207        }
208        if (nvram_match("wan_proto", "pptp")) {
209                eval("route", "del", "default");
210                nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));
211                eval("route", "add", "default", "gw",
212                     nvram_safe_get("wan_gateway"));
213                sysprintf
214                    ("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE\n",
215                     nvram_safe_get("pptp_ifname"));
216        }
217#ifdef HAVE_3G
218#if defined(HAVE_TMK) || defined(HAVE_BKM)
219        else if (nvram_match("wan_proto", "3g")) {
220                char *gpio3g = nvram_get("gpio3g");
221                if (gpio3g != NULL)
222                        set_gpio(atoi(gpio3g), 0);
223        }
224#endif
225#endif
226
227        nvram_set("pppoe_ifname", "");
228        nvram_set("pppd_pppifname", "");
229
230        // write PPP traffic statistics to nvram if wanted
231        if (nvram_match("ppp_traffic", "1")) {
232                char buffer[64];
233                long long old_in, old_out;
234                long long in, out;
235                char *pin;
236                char *pout;
237                time_t stamp;
238
239                old_in = atol(nvram_safe_get("ppp_byte_in"));
240                old_out = atol(nvram_safe_get("ppp_byte_out"));
241
242                if ((pin = getenv("BYTES_RCVD")))
243                        in = atol(pin);
244                else
245                        in = 0;
246
247                if ((pout = getenv("BYTES_SENT")))
248                        out = atol(pout);
249                else
250                        out = 0;
251
252                in += old_in;
253                out += old_out;
254                snprintf(buffer, 63, "%lld", in);
255                nvram_set("ppp_byte_in", buffer);
256                snprintf(buffer, 63, "%lld", out);
257                nvram_set("ppp_byte_out", buffer);
258                if ((stamp = time(NULL)) < 1087818160)  // clock is not set
259                        // properly
260                        stamp = 0;
261                snprintf(buffer, 63, "%ld", stamp);
262                nvram_set("ppp_byte_stamp", buffer);
263                nvram_commit();
264        }
265
266        if (nvram_match("ppp_demand", "1")
267            && (nvram_match("wan_proto", "pptp")
268                || nvram_match("wan_proto", "l2tp"))) {
269                stop_process("listen", "activity listener");
270                eval("listen", nvram_safe_get("lan_ifname"));
271        }
272
273        return 1;
274}
275
276/*
277 * int pppevent_main (int argc, char **argv) { int argn; char *type = NULL;
278 *
279 * argn = 1; while (argn < argc && argv[argn][0] == '-') { if (strcmp
280 * (argv[argn], "-t") == 0) { ++argn; type = argv[argn]; } ++argn; }
281 *
282 * if (!type) return 1;
283 *
284 *
285 * if (!strcmp (type, "PAP_AUTH_FAIL") || !strcmp (type, "CHAP_AUTH_FAIL")) {
286 *
287 * buf_to_file ("/tmp/ppp/log", type);
288 *
289 * if (check_hw_type () == BCM4704_BCM5325F_CHIP) SET_LED (GET_IP_ERROR); }
290 *
291 * return 0; }
292 *
293 * //=============================================================================
294 * int set_pppoepid_to_nv_main (int argc, char **argv) // tallest 1219 { if
295 * (!strcmp (argv[1], "0")) { nvram_set ("pppoe_pid0", getenv ("PPPD_PID"));
296 * nvram_set ("pppoe_ifname0", getenv ("IFNAME")); } else if (!strcmp (argv[1],
297 * "1")) { nvram_set ("pppoe_pid1", getenv ("PPPD_PID")); nvram_set
298 * ("pppoe_ifname1", getenv ("IFNAME")); }
299 *
300 * dprintf ("done.( IFNAME = %s DEVICE = %s )\n", getenv ("IFNAME"), getenv
301 * ("DEVICE")); return 0; }
302 */
303
304// by tallest 0407
305int disconnected_pppoe_main(int argc, char **argv)
306{
307        int pppoe_num = atoi(argv[1]);
308        char ppp_demand[2][20] = { "ppp_demand", "ppp_demand_1" };
309
310        if (nvram_match(ppp_demand[pppoe_num], "1")
311            && nvram_match("action_service", "")) {
312                cprintf("tallest:=====( kill pppoe %d )=====\n", pppoe_num);
313                stop_single_pppoe(pppoe_num);
314                start_pppoe(pppoe_num);
315                dns_to_resolv();
316
317                stop_dnsmasq();
318                start_dnsmasq();
319
320                return 0;
321        }
322        cprintf("tallest:=====( PPPOE Dial On Demand Error!! )=====\n");
323        return 0;
324}
325
326// =============================================================================
Note: See TracBrowser for help on using the repository browser.