Merge branch 'tomato-ND-usbmod-mixvpn' into tomato-ND-USBmod
[tomato.git] / release / src / router / rc / wan.c
blobd1c10be4c2cbf12e028167cb6db699489650fc19
1 /*
3 Copyright 2003, CyberTAN Inc. All Rights Reserved
5 This is UNPUBLISHED PROPRIETARY SOURCE CODE of CyberTAN Inc.
6 the contents of this file may not be disclosed to third parties,
7 copied or duplicated in any form without the prior written
8 permission of CyberTAN Inc.
10 This software should be used as a reference only, and it not
11 intended for production use!
13 THIS SOFTWARE IS OFFERED "AS IS", AND CYBERTAN GRANTS NO WARRANTIES OF ANY
14 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. CYBERTAN
15 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
16 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE
21 Copyright 2005, Broadcom Corporation
22 All Rights Reserved.
24 THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
25 KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
26 SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
27 FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
32 Modified for Tomato Firmware
33 Portions, Copyright (C) 2006-2009 Jonathan Zarate
37 #include "rc.h"
39 #include <sys/ioctl.h>
40 #include <arpa/inet.h>
41 #include <sys/sysinfo.h>
42 #include <time.h>
43 #include <bcmdevs.h>
46 #define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
47 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
50 static void make_secrets(void)
52 FILE *f;
53 char *user;
54 char *pass;
56 user = nvram_safe_get("ppp_username");
57 pass = nvram_safe_get("ppp_passwd");
58 if ((f = fopen("/tmp/ppp/pap-secrets", "w")) != NULL) {
59 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
60 fclose(f);
62 chmod("/tmp/ppp/pap-secrets", 0600);
64 if ((f = fopen("/tmp/ppp/chap-secrets", "w")) != NULL) {
65 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
66 fclose(f);
68 chmod("/tmp/ppp/chap-secrets", 0600);
72 // -----------------------------------------------------------------------------
74 int start_pptp(int mode)
76 _dprintf("%s: begin\n", __FUNCTION__);
78 FILE *fp;
79 char username[80];
80 char passwd[80];
82 stop_dhcpc();
83 stop_pppoe();
85 strlcpy(username, nvram_safe_get("ppp_username"), sizeof(username));
86 strlcpy(passwd, nvram_safe_get("ppp_passwd"), sizeof(passwd));
88 if (mode != REDIAL) {
89 mkdir("/tmp/ppp", 0777);
90 symlink("/sbin/rc", "/tmp/ppp/ip-up");
91 symlink("/sbin/rc", "/tmp/ppp/ip-down");
92 symlink("/dev/null", "/tmp/ppp/connect-errors");
94 // Generate options file
95 if ((fp = fopen("/tmp/ppp/options", "w")) == NULL) {
96 perror("/tmp/ppp/options");
97 return -1;
100 fprintf(fp, "defaultroute\n"); // Add a default route to the system routing tables, using the peer as the gateway
101 fprintf(fp, "usepeerdns\n"); // Ask the peer for up to 2 DNS server addresses
102 fprintf(fp, "pty 'pptp %s --nolaunchpppd'\n", nvram_safe_get("pptp_server_ip"));
103 fprintf(fp, "user '%s'\n", username);
104 //fprintf(fp, "persist\n"); // Do not exit after a connection is terminated.
106 fprintf(fp, "mtu %s\n",nvram_safe_get("wan_mtu"));
108 if (nvram_match("ppp_demand", "1")) {
109 //demand mode
110 fprintf(fp, "idle %d\n", nvram_get_int("ppp_idletime") * 60);
111 fprintf(fp, "demand\n"); // Dial on demand
112 fprintf(fp, "persist\n"); // Do not exit after a connection is terminated.
113 //43011: fprintf(fp, "%s:%s\n", PPP_PSEUDO_IP, PPP_PSEUDO_GW); // <local IP>:<remote IP>
114 fprintf(fp, "ipcp-accept-remote\n");
115 fprintf(fp, "ipcp-accept-local\n");
116 fprintf(fp, "connect true\n");
117 fprintf(fp, "noipdefault\n"); // Disables the default behaviour when no local IP address is specified
118 fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
120 else {
121 // keepalive mode
122 start_redial();
125 fprintf(fp, "default-asyncmap\n"); // Disable asyncmap negotiation
126 fprintf(fp, "nopcomp\n"); // Disable protocol field compression
127 fprintf(fp, "noaccomp\n"); // Disable Address/Control compression
128 fprintf(fp, "noccp\n"); // Disable CCP (Compression Control Protocol)
129 fprintf(fp, "novj\n"); // Disable Van Jacobson style TCP/IP header compression
130 fprintf(fp, "nobsdcomp\n"); // Disables BSD-Compress compression
131 fprintf(fp, "nodeflate\n"); // Disables Deflate compression
132 fprintf(fp, "lcp-echo-interval 0\n"); // Don't send an LCP echo-request frame to the peer
133 fprintf(fp, "lock\n");
134 fprintf(fp, "noauth\n");
136 if (nvram_match("debug_pppd", "1")) {
137 fprintf(fp, "debug\n");
140 fclose(fp);
142 make_secrets();
145 // Bring up WAN interface
146 ifconfig(nvram_safe_get("wan_ifname"), IFUP,
147 nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
149 eval("pppd");
151 if (nvram_match("ppp_demand", "1")) {
152 #if 1 // 43011: added by crazy 20070720
154 Fixed issue id 7887(or 7787):
155 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
157 stop_dnsmasq();
158 dns_to_resolv();
159 start_dnsmasq();
160 #endif
162 // Trigger Connect On Demand if user ping pptp server
163 eval("listen", nvram_safe_get("lan_ifname"));
166 _dprintf("%s: end\n", __FUNCTION__);
167 return 0;
170 int stop_pptp(void)
172 _dprintf("%s: begin\n", __FUNCTION__);
174 unlink("/tmp/ppp/link");
176 while ((killall("pppd", SIGKILL) == 0) || (killall("pptp", SIGKILL) == 0) || (killall("listen", SIGKILL) == 0)) {
177 sleep(1);
180 _dprintf("%s: end\n", __FUNCTION__);
181 return 0;
185 // -----------------------------------------------------------------------------
188 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
189 static void start_tmp_ppp(int num)
191 int timeout;
192 char *ifname;
193 struct ifreq ifr;
194 int s;
196 _dprintf("%s: num=%d\n", __FUNCTION__, num);
198 if (num != 0) return;
200 // Wait for ppp0 to be created
201 timeout = 15;
202 while ((ifconfig(ifname = nvram_safe_get("pppoe_ifname0"), IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
203 sleep(1);
204 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
207 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
208 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
210 // Set temporary IP address
211 timeout = 3;
212 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
213 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
214 sleep(1);
216 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
217 nvram_set("wan_netmask", "255.255.255.255");
219 // Set temporary P-t-P address
220 timeout = 3;
221 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
222 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
223 sleep(1);
225 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
227 close(s);
229 start_wan_done(ifname);
233 void start_pppoe(int num)
235 pid_t pid;
236 char idle[16];
237 char retry[16];
238 char lcp_echo_interval[16];
239 char lcp_echo_fails[16];
240 char *mtu;
241 int dod;
242 int n;
244 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
246 if (num != 0) return;
248 stop_pppoe();
250 nvram_set("pppoe_ifname0", "");
252 dod = nvram_match("ppp_demand", "1");
254 // -i
255 sprintf(idle, "%d", dod ? (nvram_get_int("ppp_idletime") * 60) : 0);
257 // [-N]
258 sprintf(retry, "%d", (nvram_get_int("ppp_redialperiod") / 5) - 1);
260 // [-r] [-t]
261 mtu = nvram_safe_get("wan_mtu");
263 // [-I n] Interval between LCP echo-requests
264 sprintf(lcp_echo_interval, "%d", ((n = nvram_get_int("pppoe_lei")) > 0) ? n : 30);
266 // [-T n] Tolerance to unanswered echo-requests
267 sprintf(lcp_echo_fails, "%d", ((n = nvram_get_int("pppoe_lef")) > 0) ? n : 5);
269 char *pppoe_argv[] = {
270 "pppoecd",
271 nvram_safe_get("wan_ifname"),
272 "-u", nvram_safe_get("ppp_username"),
273 "-p", nvram_safe_get("ppp_passwd"),
274 "-r", mtu,
275 "-t", mtu,
276 "-i", idle, // >0 == dial on demand
277 "-I", lcp_echo_interval, // Send an LCP echo-request frame to the server every X seconds
278 "-N", retry, // To avoid kill pppd when pppd has been connecting.
279 "-T", lcp_echo_fails, // pppd will presume the server to be dead if 3 LCP echo-requests are sent without receiving a valid LCP echo-reply
280 "-P", "0", // PPPOE session number.
281 "-C", "pppoe_down", // by tallest 0407
282 "-R", // set default route
283 NULL, // debug
284 NULL, NULL, // pppoe_service
285 NULL, NULL, // pppoe_ac
286 NULL, NULL, // static IP
287 NULL, // pppoe_keepalive
288 NULL, // -x extended logging
289 NULL
291 char **arg;
292 char *p;
294 for (arg = pppoe_argv; *arg; arg++) {
298 if (nvram_match("debug_pppoe", "1")) {
299 *arg++ = "-d"; // debug mode; compile ppp w/ -DDEBUG !
302 if (((p = nvram_get("ppp_service")) != NULL) && (*p != 0)) {
303 *arg++ = "-s";
304 *arg++ = p;
307 // ?? zzz
308 if (((p = nvram_get("ppp_ac")) != NULL) && (*p != 0)) {
309 *arg++ = "-a";
310 *arg++ = p;
313 if (nvram_match("ppp_static", "1")) {
314 *arg++ = "-L";
315 *arg++ = nvram_safe_get("ppp_static_ip");
318 // ?? zzz
319 //if (nvram_match("pppoe_demand", "1") || nvram_match("pppoe_keepalive", "1"))
320 *arg++ = "-k";
322 if (nvram_contains_word("log_events", "pppoe")) *arg++ = "-x";
324 mkdir("/tmp/ppp", 0777);
326 // ?? zzz
327 symlink("/sbin/rc", "/tmp/ppp/ip-up");
328 symlink("/sbin/rc", "/tmp/ppp/ip-down");
329 symlink("/sbin/rc", "/tmp/ppp/set-pppoepid"); // tallest 1219
331 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
333 _eval(pppoe_argv, NULL, 0, &pid);
335 if (dod) {
336 start_tmp_ppp(num);
340 void stop_pppoe(void)
342 _dprintf("%s\n", __FUNCTION__);
344 unlink("/tmp/ppp/link");
345 nvram_unset("pppoe_ifname0");
346 killall_tk("pppoecd");
347 killall_tk("ip-up");
348 killall_tk("ip-down");
351 void stop_singe_pppoe(int num)
353 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
355 int i;
357 if (num != 0) return;
359 i = nvram_get_int("pppoe_pid0");
360 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
361 do {
362 sleep(2);
363 } while (kill(i, SIGKILL) == 0);
366 unlink("/tmp/ppp/link");
367 nvram_unset("pppoe_ifname0");
369 nvram_set("wan_get_dns", "");
370 clear_resolv();
373 // -----------------------------------------------------------------------------
376 void start_l2tp(void)
378 _dprintf("%s: begin\n", __FUNCTION__);
380 int ret;
381 FILE *fp;
382 char *l2tp_argv[] = { "l2tpd", NULL };
383 char l2tpctrl[64];
384 char username[80];
385 char passwd[80];
387 stop_pppoe();
388 stop_pptp();
390 snprintf(username, sizeof(username), "%s", nvram_safe_get("ppp_username"));
391 snprintf(passwd, sizeof(passwd), "%s", nvram_safe_get("ppp_passwd"));
393 mkdir("/tmp/ppp", 0777);
394 symlink("/sbin/rc", "/tmp/ppp/ip-up");
395 symlink("/sbin/rc", "/tmp/ppp/ip-down");
396 symlink("/dev/null", "/tmp/ppp/connect-errors");
398 /* Generate L2TP configuration file */
399 if ((fp = fopen("/tmp/l2tp.conf", "w")) == NULL) {
400 return;
402 fprintf(fp, "global\n"); // Global section
403 fprintf(fp, "load-handler \"sync-pppd.so\"\n"); // Load handlers
404 fprintf(fp, "load-handler \"cmd.so\"\n");
405 fprintf(fp, "listen-port 1701\n"); // Bind address
406 fprintf(fp, "section sync-pppd\n"); // Configure the sync-pppd handler
407 fprintf(fp, "section peer\n"); // Peer section
408 fprintf(fp, "peer %s\n", nvram_safe_get("l2tp_server_ip"));
409 fprintf(fp, "port 1701\n");
410 fprintf(fp, "lac-handler sync-pppd\n");
411 fprintf(fp, "section cmd\n"); // Configure the cmd handler
412 fclose(fp);
414 /* Generate options file */
415 if ((fp = fopen("/tmp/ppp/options", "w")) == NULL) {
416 return;
418 fprintf(fp, "defaultroute\n"); // Add a default route to the system routing tables, using the peer as the gateway
419 fprintf(fp, "usepeerdns\n"); // Ask the peer for up to 2 DNS server addresses
420 //fprintf(fp, "pty 'pptp %s --nolaunchpppd'\n",nvram_safe_get("pptp_server_ip"));
421 fprintf(fp, "user '%s'\n", username);
422 //fprintf(fp, "persist\n"); // Do not exit after a connection is terminated.
424 fprintf(fp, "mtu %s\n",nvram_safe_get("wan_mtu"));
426 if (nvram_match("ppp_demand", "1")){ // demand mode
427 fprintf(fp, "idle %d\n", nvram_get_int("ppp_idletime") * 60);
428 //fprintf(fp, "demand\n"); // Dial on demand
429 //fprintf(fp, "persist\n"); // Do not exit after a connection is terminated.
430 //fprintf(fp, "%s:%s\n",PPP_PSEUDO_IP,PPP_PSEUDO_GW); // <local IP>:<remote IP>
431 fprintf(fp, "ipcp-accept-remote\n");
432 fprintf(fp, "ipcp-accept-local\n");
433 fprintf(fp, "connect true\n");
434 fprintf(fp, "noipdefault\n"); // Disables the default behaviour when no local IP address is specified
435 fprintf(fp, "ktune\n"); // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
437 else{ // keepalive mode
438 if (nvram_match("l2tp_test", "1")) {
439 fprintf(fp, "idle %d\n", 0);
440 fprintf(fp, "ipcp-accept-remote\n");
441 fprintf(fp, "ipcp-accept-local\n");
442 fprintf(fp, "connect true\n");
443 fprintf(fp, "noipdefault\n"); // Disables the default behaviour when no local IP address is specified
447 fprintf(fp, "default-asyncmap\n"); // Disable asyncmap negotiation
448 fprintf(fp, "nopcomp\n"); // Disable protocol field compression
449 fprintf(fp, "noaccomp\n"); // Disable Address/Control compression
450 fprintf(fp, "noccp\n"); // Disable CCP (Compression Control Protocol)
451 fprintf(fp, "novj\n"); // Disable Van Jacobson style TCP/IP header compression
452 fprintf(fp, "nobsdcomp\n"); // Disable BSD-Compress compression
453 fprintf(fp, "nodeflate\n"); // Disable Deflate compression
454 fprintf(fp, "lcp-echo-interval 0\n"); // Don't send an LCP echo-request frame to the peer
455 fprintf(fp, "lock\n");
456 fprintf(fp, "noauth");
458 if (nvram_match("debug_pppd", "1")) {
459 fprintf(fp, "debug\n");
461 fclose(fp);
463 make_secrets();
464 enable_ip_forward();
466 /* Bring up WAN interface */
467 //ifconfig(nvram_safe_get("wan_ifname"), IFUP,
468 // nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
470 ret = _eval(l2tp_argv, NULL, 0, NULL);
471 sleep(1);
473 if (nvram_match("ppp_demand", "1")) {
474 eval("listen", nvram_safe_get("lan_ifname"));
476 else {
477 snprintf(l2tpctrl, sizeof(l2tpctrl), "l2tp-control \"start-session %s\"", nvram_safe_get("l2tp_server_ip"));
478 system(l2tpctrl);
479 _dprintf("%s\n", l2tpctrl);
481 start_redial();
484 _dprintf("%s: end\n", __FUNCTION__);
487 void stop_l2tp(void)
489 _dprintf("%s: begin\n", __FUNCTION__);
491 unlink("/tmp/ppp/link");
492 while ((killall("pppd", SIGKILL) == 0) || (killall("l2tpd", SIGKILL) == 0) || (killall("listen", SIGKILL) == 0)) {
493 sleep(1);
495 _dprintf("%s: end\n", __FUNCTION__);
498 // -----------------------------------------------------------------------------
500 // trigger connect on demand
501 void force_to_dial(void)
503 char s[64];
505 _dprintf("%s: begin\n", __FUNCTION__);
507 sleep(1);
508 switch (get_wan_proto()) {
509 case WP_L2TP:
510 snprintf(s, sizeof(s), "/usr/sbin/l2tp-control \"start-session %s\"", nvram_safe_get("l2tp_server_ip"));
511 system(s);
512 _dprintf("%s\n", s);
513 break;
514 case WP_PPTP:
515 eval("ping", "-c", "2", "10.112.112.112");
516 break;
517 case WP_DISABLED:
518 case WP_STATIC:
519 break;
520 default:
521 eval("ping", "-c", "2", nvram_safe_get("wan_gateway"));
522 break;
525 _dprintf("%s: end\n", __FUNCTION__);
528 // -----------------------------------------------------------------------------
530 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
532 void start_wan(int mode)
534 int wan_proto;
535 char *wan_ifname;
536 char *p;
537 struct ifreq ifr;
538 int sd;
539 int max;
540 int mtu;
541 char buf[128];
543 f_write(wan_connecting, NULL, 0, 0, 0);
547 if (nvram_match("wl_mode", "sta")) {
548 p = nvram_safe_get("wl_ifname");
550 else {
551 p = nvram_safe_get("wan_ifnameX");
552 set_mac(p, "mac_wan", 1);
554 nvram_set("wan_ifname", p);
555 nvram_set("wan_ifnames", p);
559 wan_ifname = nvram_safe_get("wan_ifname");
560 if (wan_ifname[0] == 0) {
561 wan_ifname = "none";
562 nvram_set("wan_ifname", wan_ifname);
565 if (strcmp(wan_ifname, "none") == 0) {
566 nvram_set("wan_proto", "disabled");
567 syslog(LOG_INFO, "No WAN");
572 wan_proto = get_wan_proto();
574 if (wan_proto == WP_DISABLED) {
575 start_wan_done(wan_ifname);
576 return;
579 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
580 perror("socket");
581 return;
584 // MTU
586 switch (wan_proto) {
587 case WP_PPPOE:
588 max = 1492;
589 break;
590 case WP_PPTP:
591 case WP_L2TP:
592 max = 1460;
593 break;
594 default:
595 max = 1500;
596 break;
598 if (nvram_match("mtu_enable", "0")) {
599 mtu = max;
601 else {
602 mtu = nvram_get_int("wan_mtu");
603 if (mtu > max) mtu = max;
604 else if (mtu < 576) mtu = 576;
606 sprintf(buf, "%d", mtu);
607 nvram_set("wan_mtu", buf);
608 nvram_set("wan_run_mtu", buf);
610 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
611 /* if (wan_proto == WP_PPTP) {
612 mtu += 40;
613 } */ // commented out; checkme -- zzz
615 ifr.ifr_mtu = mtu;
616 strcpy(ifr.ifr_name, wan_ifname);
617 ioctl(sd, SIOCSIFMTU, &ifr);
621 ifconfig(wan_ifname, IFUP, NULL, NULL);
623 set_host_domain_name();
625 switch (wan_proto) {
626 case WP_PPPOE:
627 start_pppoe(PPPOE0);
628 if (nvram_invmatch("ppp_demand", "1")) {
629 if (mode != REDIAL) start_redial();
631 break;
632 case WP_DHCP:
633 case WP_L2TP:
634 stop_dhcpc();
635 start_dhcpc();
636 break;
637 case WP_PPTP:
638 start_pptp(mode);
639 break;
640 default: // static
641 nvram_set("wan_iface", wan_ifname);
642 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
644 int r = 10;
645 while ((!check_wanup()) && (r-- > 0)) {
646 sleep(1);
649 start_wan_done(wan_ifname);
650 break;
653 // Get current WAN hardware address
654 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
655 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
656 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
659 /* Set initial QoS mode again now that WAN port is ready. */
660 set_et_qos_mode(sd);
662 close(sd);
664 enable_ip_forward();
666 led(LED_DIAG, 0); // for 4712, 5325E (?)
667 led(LED_DMZ, nvram_match("dmz_enable", "1"));
669 _dprintf("%s: end\n", __FUNCTION__);
673 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
674 // wan_ifname: vlan1
675 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
678 void start_wan_done(char *wan_ifname)
680 int proto;
681 int n;
682 char *gw;
683 int dod;
684 struct sysinfo si;
685 int wanup;
687 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
689 sysinfo(&si);
690 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
692 proto = get_wan_proto();
693 dod = nvram_match("ppp_demand", "1");
695 if (proto == WP_L2TP) {
696 while (route_del(nvram_safe_get("wan_ifname"), 0, NULL, NULL, NULL) == 0) {
701 // delete all default routes
702 while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0) {
706 if (proto != WP_DISABLED) {
707 // set default route to gateway if specified
708 gw = (proto == WP_PPTP) ? nvram_safe_get("pptp_get_ip") : nvram_safe_get("wan_gateway");
709 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
710 n = 5;
711 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
712 sleep(1);
714 _dprintf("set default gateway=%s n=%d\n", gw, n);
717 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
719 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
720 if(nvram_match("wan_proto", "static"))
722 int ifindex;
723 u_int32_t wan_ip;
724 unsigned char wan_mac[6];
726 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
727 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
730 #endif
733 if (proto == WP_PPTP) {
734 // For PPTP protocol, we must use pptp_get_ip as gateway, not pptp_server_ip
735 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway"), NULL, "255.255.255.255");
736 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("pptp_server_ip"), NULL, "255.255.255.255");
737 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("pptp_get_ip"), NULL, "255.255.255.255");
739 else if (proto == WP_L2TP) {
740 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway"), NULL, "255.255.255.255");
741 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("l2tp_get_ip"), NULL, "255.255.255.255");
743 #if 1 // 43011: add by crazy 20070803
745 Fix these issues:
746 1. DUT can't response a L2TP ZLB Control message to L2TP server.
747 2. Configure DUT to be L2TP with Connect on demand in 5 minutes,
748 but DUT will disconnect L2TP before 5 minutes.
749 3. It also causes DUT could often disconnect from L2TP server in
750 L2TP Keep Alive mode.
752 struct in_addr l2tp_server_ip, wan_ipaddr_old, wan_netmask;
754 if (inet_aton(nvram_safe_get("l2tp_server_ip"), &l2tp_server_ip) &&
755 inet_aton(nvram_safe_get("wan_netmask"), &wan_netmask) &&
756 inet_aton(nvram_safe_get("wan_ipaddr"), &wan_ipaddr_old)) {
757 if ((l2tp_server_ip.s_addr & wan_netmask.s_addr) != (wan_ipaddr_old.s_addr & wan_netmask.s_addr)) {
758 // If DUT WAN IP and L2TP server IP are in different subnets, it could need this route.
759 route_add(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255"); // fixed routing problem in Israel by kanki
762 else {
763 // Fail to change IP from char to struct, still add this route.
764 route_add(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255"); // fixed routing problem in Israel by kanki
766 #else
767 route_add(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255"); // fixed routing problem in Israel by kanki
768 #endif
772 dns_to_resolv();
773 start_dnsmasq();
775 start_firewall();
776 start_qos();
778 stop_igmp_proxy();
779 start_igmp_proxy();
781 do_static_routes(1);
783 stop_zebra();
784 start_zebra();
786 stop_upnp();
787 start_upnp();
789 wanup = check_wanup();
791 if ((wanup) || (time(0) < Y2K)) {
792 stop_ntpc();
793 start_ntpc();
796 if ((wanup) || (proto == WP_DISABLED)) {
797 stop_ddns();
798 start_ddns();
801 if (wanup) {
802 SET_LED(GOT_IP);
803 notice_set("wan", "");
805 run_nvscript("script_wanup", NULL, 0);
808 #if 0 //!!TB
809 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
810 if (check_hw_type() == HW_BCM4702) {
811 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
812 if (nvram_match("lan_stp", "1")) eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
814 #endif
816 if (wanup)
817 start_vpn_eas();
819 unlink(wan_connecting);
821 TRACE_PT("end\n");
824 void stop_wan(void)
826 char name[80];
827 char *next;
829 _dprintf("%s: begin\n", __FUNCTION__);
831 stop_qos();
832 stop_upnp(); //!!TB - moved from stop_services()
833 stop_firewall();
834 stop_igmp_proxy();
835 stop_ntpc();
837 /* Kill any WAN client daemons or callbacks */
838 stop_singe_pppoe(PPPOE0);
839 stop_pppoe();
840 stop_l2tp();
841 stop_dhcpc();
842 stop_pptp();
843 stop_redial();
844 nvram_set("wan_get_dns", "");
846 /* Bring down WAN interfaces */
847 foreach(name, nvram_safe_get("wan_ifnames"), next)
848 ifconfig(name, 0, "0.0.0.0", NULL);
850 SET_LED(RELEASE_IP);
851 notice_set("wan", "");
852 unlink(wan_connecting);
854 _dprintf("%s: end\n", __FUNCTION__);