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

Last change on this file since 17720 was 17720, checked in by BrainSlayer, 21 months ago

should solve pptp problems with static ip's

File size: 8.4 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
150                if (nvram_match("wan_proto", "pptp")) {
151                        if (nvram_match("pptp_use_dhcp", "1")) {
152                                nvram_set("wan_gateway", value);
153                        }
154                        eval("route", "del", "default");
155                        route_add(wan_ifname, 0, "0.0.0.0", value, "0.0.0.0");
156                } else {
157                        nvram_set("wan_gateway", value);
158
159                }
160        }
161        strcpy(buf, "");
162        if (getenv("DNS1"))
163                sprintf(buf, "%s", getenvs("DNS1"));
164        if (getenv("DNS2"))
165                sprintf(buf + strlen(buf), "%s%s", strlen(buf) ? " " : "",
166                        getenvs("DNS2"));
167        nvram_set("wan_get_dns", buf);
168
169        if ((value = getenv("AC_NAME")))
170                nvram_set("ppp_get_ac", value);
171        if ((value = getenv("SRV_NAME")))
172                nvram_set("ppp_get_srv", value);
173        if ((value = getenv("MTU")))
174                nvram_set("wan_run_mtu", value);
175        start_wan_done(wan_ifname);
176        cprintf("done\n");
177        return 0;
178}
179
180/*
181 * Called when link goes down
182 */
183int ipdown_main(int argc, char **argv)
184{
185        if (check_action() != ACT_IDLE)
186                return -1;
187        stop_ddns();
188        stop_ntpc();
189
190        unlink("/tmp/ppp/link");
191
192        if (nvram_match("wan_proto", "l2tp")) {
193                /*
194                 * clear dns from the resolv.conf
195                 */
196                nvram_set("wan_get_dns", "");
197                dns_to_resolv();
198
199                // todo
200                route_del(nvram_safe_get("wan_ifname"), 0,
201                          nvram_safe_get("l2tp_server_ip"),
202                          nvram_safe_get("wan_gateway_buf"), "255.255.255.255");
203                /*
204                 * Restore the default gateway for WAN interface
205                 */
206                nvram_set("wan_gateway", nvram_safe_get("wan_gateway_buf"));
207
208                /*
209                 * Set default route to gateway if specified
210                 */
211                route_add(nvram_safe_get("wan_ifname"), 0, "0.0.0.0",
212                          nvram_safe_get("wan_gateway"), "0.0.0.0");
213        }
214        if (nvram_match("wan_proto", "pptp")) {
215                eval("route", "del", "default");
216                if (nvram_match("pptp_use_dhcp", "1")) {
217                        nvram_set("wan_gateway",
218                                  nvram_safe_get("wan_gateway_buf"));
219                        eval("route", "add", "default", "gw",
220                             nvram_safe_get("wan_gateway"));
221                }
222                sysprintf
223                    ("iptables -t nat -A POSTROUTING -o %s -j MASQUERADE\n",
224                     nvram_safe_get("pptp_ifname"));
225        }
226#ifdef HAVE_3G
227#if defined(HAVE_TMK) || defined(HAVE_BKM)
228        else if (nvram_match("wan_proto", "3g")) {
229                char *gpio3g = nvram_get("gpio3g");
230                if (gpio3g != NULL)
231                        set_gpio(atoi(gpio3g), 0);
232        }
233#endif
234#endif
235
236        nvram_set("pppoe_ifname", "");
237        nvram_set("pppd_pppifname", "");
238
239        // write PPP traffic statistics to nvram if wanted
240        if (nvram_match("ppp_traffic", "1")) {
241                char buffer[64];
242                long long old_in, old_out;
243                long long in, out;
244                char *pin;
245                char *pout;
246                time_t stamp;
247
248                old_in = atol(nvram_safe_get("ppp_byte_in"));
249                old_out = atol(nvram_safe_get("ppp_byte_out"));
250
251                if ((pin = getenv("BYTES_RCVD")))
252                        in = atol(pin);
253                else
254                        in = 0;
255
256                if ((pout = getenv("BYTES_SENT")))
257                        out = atol(pout);
258                else
259                        out = 0;
260
261                in += old_in;
262                out += old_out;
263                snprintf(buffer, 63, "%lld", in);
264                nvram_set("ppp_byte_in", buffer);
265                snprintf(buffer, 63, "%lld", out);
266                nvram_set("ppp_byte_out", buffer);
267                if ((stamp = time(NULL)) < 1087818160)  // clock is not set
268                        // properly
269                        stamp = 0;
270                snprintf(buffer, 63, "%ld", stamp);
271                nvram_set("ppp_byte_stamp", buffer);
272                nvram_commit();
273        }
274
275        if (nvram_match("ppp_demand", "1")
276            && (nvram_match("wan_proto", "pptp")
277                || nvram_match("wan_proto", "l2tp"))) {
278                stop_process("listen", "activity listener");
279                eval("listen", nvram_safe_get("lan_ifname"));
280        }
281
282        return 1;
283}
284
285/*
286 * int pppevent_main (int argc, char **argv) { int argn; char *type = NULL;
287 *
288 * argn = 1; while (argn < argc && argv[argn][0] == '-') { if (strcmp
289 * (argv[argn], "-t") == 0) { ++argn; type = argv[argn]; } ++argn; }
290 *
291 * if (!type) return 1;
292 *
293 *
294 * if (!strcmp (type, "PAP_AUTH_FAIL") || !strcmp (type, "CHAP_AUTH_FAIL")) {
295 *
296 * buf_to_file ("/tmp/ppp/log", type);
297 *
298 * if (check_hw_type () == BCM4704_BCM5325F_CHIP) SET_LED (GET_IP_ERROR); }
299 *
300 * return 0; }
301 *
302 * //=============================================================================
303 * int set_pppoepid_to_nv_main (int argc, char **argv) // tallest 1219 { if
304 * (!strcmp (argv[1], "0")) { nvram_set ("pppoe_pid0", getenv ("PPPD_PID"));
305 * nvram_set ("pppoe_ifname0", getenv ("IFNAME")); } else if (!strcmp (argv[1],
306 * "1")) { nvram_set ("pppoe_pid1", getenv ("PPPD_PID")); nvram_set
307 * ("pppoe_ifname1", getenv ("IFNAME")); }
308 *
309 * dprintf ("done.( IFNAME = %s DEVICE = %s )\n", getenv ("IFNAME"), getenv
310 * ("DEVICE")); return 0; }
311 */
312
313// by tallest 0407
314int disconnected_pppoe_main(int argc, char **argv)
315{
316        int pppoe_num = atoi(argv[1]);
317        char ppp_demand[2][20] = { "ppp_demand", "ppp_demand_1" };
318
319        if (nvram_match(ppp_demand[pppoe_num], "1")
320            && nvram_match("action_service", "")) {
321                cprintf("tallest:=====( kill pppoe %d )=====\n", pppoe_num);
322                stop_single_pppoe(pppoe_num);
323                start_pppoe(pppoe_num);
324                dns_to_resolv();
325
326                stop_dnsmasq();
327                start_dnsmasq();
328
329                return 0;
330        }
331        cprintf("tallest:=====( PPPOE Dial On Demand Error!! )=====\n");
332        return 0;
333}
334
335// =============================================================================
Note: See TracBrowser for help on using the repository browser.