Merge Toastman-RT into Toastman-RT-N
[tomato.git] / release / src / router / rc / wan.c
blobd9d5fbb2c8cc6a773abd8b6f1ee151e768442010
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>
45 static const char ppp_linkfile[] = "/tmp/ppp/link";
46 static const char ppp_optfile[] = "/tmp/ppp/options";
48 static void make_secrets(void)
50 FILE *f;
51 char *user;
52 char *pass;
54 user = nvram_safe_get("ppp_username");
55 pass = nvram_safe_get("ppp_passwd");
56 if ((f = fopen("/tmp/ppp/pap-secrets", "w")) != NULL) {
57 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
58 fclose(f);
60 chmod("/tmp/ppp/pap-secrets", 0600);
62 if ((f = fopen("/tmp/ppp/chap-secrets", "w")) != NULL) {
63 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
64 fclose(f);
66 chmod("/tmp/ppp/chap-secrets", 0600);
69 // -----------------------------------------------------------------------------
71 static int config_pppd(int wan_proto, int num)
73 TRACE_PT("begin\n");
75 FILE *fp;
76 char *p;
77 int demand;
79 mkdir("/tmp/ppp", 0777);
80 symlink("/sbin/rc", "/tmp/ppp/ip-up");
81 symlink("/sbin/rc", "/tmp/ppp/ip-down");
82 #ifdef TCONFIG_IPV6
83 symlink("/sbin/rc", "/tmp/ppp/ipv6-up");
84 symlink("/sbin/rc", "/tmp/ppp/ipv6-down");
85 #endif
86 symlink("/dev/null", "/tmp/ppp/connect-errors");
88 demand = nvram_get_int("ppp_demand");
90 // Generate options file
91 if ((fp = fopen(ppp_optfile, "w")) == NULL) {
92 perror(ppp_optfile);
93 return -1;
96 fprintf(fp,
97 "unit %d\n"
98 "defaultroute\n" // Add a default route to the system routing tables, using the peer as the gateway
99 "usepeerdns\n" // Ask the peer for up to 2 DNS server addresses
100 "user '%s'\n"
101 "default-asyncmap\n" // Disable asyncmap negotiation
102 "nopcomp\n" // Disable protocol field compression
103 "noaccomp\n" // Disable Address/Control compression
104 "novj\n" // Disable Van Jacobson style TCP/IP header compression
105 "nobsdcomp\n" // Disable BSD-Compress compression
106 "nodeflate\n" // Disable Deflate compression
107 "noauth\n" // Do not authenticate peer
108 "refuse-eap\n" // Do not use eap
109 "maxfail 0\n" // Never give up
110 "lcp-echo-interval %d\n"// Interval between LCP echo-requests
111 "lcp-echo-failure %d\n" // Tolerance to unanswered echo-requests
112 "lcp-echo-adaptive\n" // Suppress LCP echo-requests if traffic was received
113 "%s", // Debug
114 num,
115 nvram_safe_get("ppp_username"),
116 nvram_get_int("pppoe_lei") ? : 10,
117 nvram_get_int("pppoe_lef") ? : 5,
118 nvram_get_int("debug_ppp") ? "debug\n" : "");
120 if (wan_proto != WP_L2TP) {
121 fprintf(fp,
122 "persist\n"
123 "holdoff %d\n",
124 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30));
127 switch (wan_proto) {
128 case WP_PPTP:
129 fprintf(fp,
130 "plugin pptp.so\n"
131 "pptp_server %s\n"
132 "nomppe-stateful\n"
133 "mtu %d\n",
134 nvram_safe_get("pptp_server_ip"),
135 nvram_get_int("mtu_enable") ? nvram_get_int("wan_mtu") : 1400);
136 break;
137 case WP_PPPOE:
138 fprintf(fp,
139 "password '%s'\n"
140 "plugin rp-pppoe.so\n"
141 "nomppe nomppc\n"
142 "nic-%s\n"
143 "mru %d mtu %d\n",
144 nvram_safe_get("ppp_passwd"),
145 nvram_safe_get("wan_ifname"),
146 nvram_get_int("wan_mtu"), nvram_get_int("wan_mtu"));
147 if (((p = nvram_get("ppp_service")) != NULL) && (*p)) {
148 fprintf(fp, "rp_pppoe_service '%s'\n", p);
150 if (((p = nvram_get("ppp_ac")) != NULL) && (*p)) {
151 fprintf(fp, "rp_pppoe_ac '%s'\n", p);
153 if (nvram_match("ppp_mlppp", "1")) {
154 fprintf(fp, "mp\n");
156 break;
157 case WP_L2TP:
158 fprintf(fp, "nomppe nomppc\n");
159 if (nvram_get_int("mtu_enable"))
160 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
161 break;
164 if (demand) {
165 // demand mode
166 fprintf(fp,
167 "demand\n" // Dial on demand
168 "idle %d\n"
169 "ipcp-accept-remote\n"
170 "ipcp-accept-local\n"
171 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
172 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
173 nvram_get_int("ppp_idletime") * 60);
176 #ifdef TCONFIG_IPV6
177 switch (get_ipv6_service()) {
178 case IPV6_NATIVE:
179 case IPV6_NATIVE_DHCP:
180 fprintf(fp, "+ipv6\n");
181 break;
183 #endif
184 // User specific options
185 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
187 fclose(fp);
188 make_secrets();
190 TRACE_PT("end\n");
191 return 0;
194 static void stop_ppp(void)
196 TRACE_PT("begin\n");
198 unlink(ppp_linkfile);
200 killall_tk("ip-up");
201 killall_tk("ip-down");
202 #ifdef TCONFIG_IPV6
203 killall_tk("ipv6-up");
204 killall_tk("ipv6-down");
205 #endif
206 killall_tk("xl2tpd");
207 killall_tk("pppd");
208 killall_tk("listen");
210 TRACE_PT("end\n");
213 static void run_pppd(void)
215 eval("pppd");
217 if (nvram_get_int("ppp_demand")) {
218 // demand mode
220 Fixed issue id 7887(or 7787):
221 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
223 stop_dnsmasq();
224 dns_to_resolv();
225 start_dnsmasq();
227 // Trigger Connect On Demand if user ping pptp server
228 eval("listen", nvram_safe_get("lan_ifname"));
230 else {
231 // keepalive mode
232 start_redial();
236 // -----------------------------------------------------------------------------
238 inline void stop_pptp(void)
240 stop_ppp();
243 void start_pptp(int mode)
245 TRACE_PT("begin\n");
247 if (!using_dhcpc()) stop_dhcpc();
248 stop_pptp();
250 if (config_pppd(WP_PPTP, 0) != 0)
251 return;
253 run_pppd();
255 TRACE_PT("end\n");
258 // -----------------------------------------------------------------------------
260 void preset_wan(char *ifname, char *gw, char *netmask)
262 int i;
264 /* Delete all default routes */
265 route_del(ifname, 0, NULL, NULL, NULL);
267 /* try adding a route to gateway first */
268 route_add(ifname, 0, gw, NULL, "255.255.255.255");
270 /* Set default route to gateway if specified */
271 i = 5;
272 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
273 sleep(1);
275 _dprintf("set default gateway=%s n=%d\n", gw, i);
277 /* Add routes to dns servers as well for demand ppp to work */
278 char word[100], *next;
279 in_addr_t mask = inet_addr(netmask);
280 foreach(word, nvram_safe_get("wan_get_dns"), next) {
281 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
282 route_add(ifname, 0, word, gw, "255.255.255.255");
285 dns_to_resolv();
286 start_dnsmasq();
287 sleep(1);
288 start_firewall();
291 // -----------------------------------------------------------------------------
294 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
295 static void start_tmp_ppp(int num, char *ifname)
297 int timeout;
298 struct ifreq ifr;
299 int s;
301 TRACE_PT("begin: num=%d\n", num);
303 if (num != 0) return;
305 // Wait for ppp0 to be created
306 timeout = 15;
307 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
308 sleep(1);
309 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
312 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
313 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
315 // Set temporary IP address
316 timeout = 3;
317 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
318 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
319 sleep(1);
321 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
322 nvram_set("wan_netmask", "255.255.255.255");
324 // Set temporary P-t-P address
325 timeout = 3;
326 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
327 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
328 sleep(1);
330 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
332 close(s);
334 start_wan_done(ifname);
335 TRACE_PT("end\n");
338 void start_pppoe(int num)
340 char ifname[8];
342 TRACE_PT("begin pppoe_num=%d\n", num);
344 if (num != 0) return;
346 stop_pppoe();
348 snprintf(ifname, sizeof(ifname), "ppp%d", num);
350 if (config_pppd(WP_PPPOE, num) != 0)
351 return;
353 run_pppd();
355 if (nvram_get_int("ppp_demand"))
356 start_tmp_ppp(num, ifname);
357 else
358 ifconfig(ifname, IFUP, NULL, NULL);
360 TRACE_PT("end\n");
363 void stop_pppoe(void)
365 stop_ppp();
368 #if 0
369 void stop_singe_pppoe(int num)
371 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
373 int i;
375 if (num != 0) return;
377 i = nvram_get_int("pppoe_pid0");
378 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
379 do {
380 sleep(2);
381 } while (kill(i, SIGKILL) == 0);
384 unlink(ppp_linkfile);
385 nvram_unset("pppoe_ifname0");
387 nvram_set("wan_get_dns", "");
388 clear_resolv();
390 #endif
392 // -----------------------------------------------------------------------------
394 inline void stop_l2tp(void)
396 stop_ppp();
399 void start_l2tp(void)
401 TRACE_PT("begin\n");
403 FILE *fp;
404 int demand;
406 stop_l2tp();
408 if (config_pppd(WP_L2TP, 0) != 0)
409 return;
411 demand = nvram_get_int("ppp_demand");
413 /* Generate XL2TPD configuration file */
414 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
415 return;
416 fprintf(fp,
417 "[global]\n"
418 "access control = no\n"
419 "port = 1701\n"
420 "[lac l2tp]\n"
421 "lns = %s\n"
422 "pppoptfile = %s\n"
423 "redial = yes\n"
424 "max redials = 32767\n"
425 "redial timeout = %d\n"
426 "tunnel rws = 8\n"
427 "ppp debug = %s\n"
428 "%s\n",
429 nvram_safe_get("l2tp_server_ip"),
430 ppp_optfile,
431 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
432 nvram_get_int("debug_ppp") ? "yes" : "no",
433 nvram_safe_get("xl2tpd_custom"));
434 fappend(fp, "/etc/xl2tpd.custom");
435 fclose(fp);
437 enable_ip_forward();
439 eval("xl2tpd");
441 if (demand) {
442 eval("listen", nvram_safe_get("lan_ifname"));
444 else {
445 force_to_dial();
446 start_redial();
449 TRACE_PT("end\n");
452 // -----------------------------------------------------------------------------
454 static char *wan_gateway(void)
456 char *gw = nvram_safe_get("wan_gateway_get");
457 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
458 gw = nvram_safe_get("wan_gateway");
459 return gw;
462 // -----------------------------------------------------------------------------
464 // trigger connect on demand
465 void force_to_dial(void)
467 TRACE_PT("begin\n");
469 sleep(1);
470 switch (get_wan_proto()) {
471 case WP_L2TP:
472 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
473 break;
474 case WP_PPTP:
475 eval("ping", "-c", "2", "10.112.112.112");
476 break;
477 case WP_DISABLED:
478 case WP_STATIC:
479 break;
480 default:
481 eval("ping", "-c", "2", wan_gateway());
482 break;
485 TRACE_PT("end\n");
488 // -----------------------------------------------------------------------------
490 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
492 char *routes, *tmp;
493 int bits;
494 struct in_addr mask;
495 char netmask[16];
497 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
498 tmp = routes = strdup(nvram_safe_get(nvname));
499 while (tmp && *tmp) {
500 char *ipaddr, *gateway, *nmask;
502 ipaddr = nmask = strsep(&tmp, " ");
503 strcpy(netmask, "255.255.255.255");
505 if (nmask) {
506 ipaddr = strsep(&nmask, "/");
507 if (nmask && *nmask) {
508 bits = strtol(nmask, &nmask, 10);
509 if (bits >= 1 && bits <= 32) {
510 mask.s_addr = htonl(0xffffffff << (32 - bits));
511 strcpy(netmask, inet_ntoa(mask));
515 gateway = strsep(&tmp, " ");
517 if (gateway && *gateway) {
518 if (add)
519 route_add(ifname, metric, ipaddr, gateway, netmask);
520 else
521 route_del(ifname, metric, ipaddr, gateway, netmask);
524 free(routes);
527 void do_wan_routes(char *ifname, int metric, int add)
529 if (nvram_get_int("dhcp_routes")) {
530 // Static Routes: IP ROUTER IP2 ROUTER2 ...
531 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
532 _do_wan_routes(ifname, "wan_routes1", metric, add);
533 _do_wan_routes(ifname, "wan_routes2", metric, add);
537 // -----------------------------------------------------------------------------
539 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
541 static int is_sta(int idx, int unit, int subunit, void *param)
543 char **p = param;
545 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
546 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
547 return 1;
549 return 0;
552 void start_wan(int mode)
554 int wan_proto;
555 char *wan_ifname;
556 char *p = NULL;
557 struct ifreq ifr;
558 int sd;
559 int max;
560 int mtu;
561 char buf[128];
563 TRACE_PT("begin\n");
565 f_write(wan_connecting, NULL, 0, 0, 0);
569 if (!foreach_wif(1, &p, is_sta)) {
570 p = nvram_safe_get("wan_ifnameX");
571 set_mac(p, "mac_wan", 1);
573 nvram_set("wan_ifname", p);
574 nvram_set("wan_ifnames", p);
578 wan_ifname = nvram_safe_get("wan_ifname");
579 if (wan_ifname[0] == 0) {
580 wan_ifname = "none";
581 nvram_set("wan_ifname", wan_ifname);
584 if (strcmp(wan_ifname, "none") == 0) {
585 nvram_set("wan_proto", "disabled");
586 syslog(LOG_INFO, "No WAN");
591 wan_proto = get_wan_proto();
593 // set the default gateway for WAN interface
594 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
596 if (wan_proto == WP_DISABLED) {
597 start_wan_done(wan_ifname);
598 return;
601 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
602 perror("socket");
603 return;
606 // MTU
608 switch (wan_proto) {
609 case WP_PPPOE:
610 max = 1492;
611 break;
612 case WP_PPTP:
613 case WP_L2TP:
614 max = 1460;
615 break;
616 default:
617 max = 1500;
618 break;
620 if (nvram_match("mtu_enable", "0")) {
621 mtu = max;
623 else {
624 mtu = nvram_get_int("wan_mtu");
625 if (mtu > max) mtu = max;
626 else if (mtu < 576) mtu = 576;
628 sprintf(buf, "%d", mtu);
629 nvram_set("wan_mtu", buf);
630 nvram_set("wan_run_mtu", buf);
632 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
633 /* if (wan_proto == WP_PPTP) {
634 mtu += 40;
635 } */ // commented out; checkme -- zzz
637 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
638 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
639 ifr.ifr_mtu = mtu;
640 strcpy(ifr.ifr_name, wan_ifname);
641 ioctl(sd, SIOCSIFMTU, &ifr);
646 ifconfig(wan_ifname, IFUP, NULL, NULL);
648 start_firewall();
650 set_host_domain_name();
652 switch (wan_proto) {
653 case WP_PPPOE:
654 start_pppoe(PPPOE0);
655 break;
656 case WP_DHCP:
657 case WP_L2TP:
658 case WP_PPTP:
659 if (using_dhcpc()) {
660 stop_dhcpc();
661 start_dhcpc();
663 else if (wan_proto != WP_DHCP) {
664 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
665 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
667 p = nvram_safe_get("wan_gateway");
668 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
669 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
671 switch (wan_proto) {
672 case WP_PPTP:
673 start_pptp(mode);
674 break;
675 case WP_L2TP:
676 start_l2tp();
677 break;
680 break;
681 default: // static
682 nvram_set("wan_iface", wan_ifname);
683 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
685 int r = 10;
686 while ((!check_wanup()) && (r-- > 0)) {
687 sleep(1);
690 start_wan_done(wan_ifname);
691 break;
694 // Get current WAN hardware address
695 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
696 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
697 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
700 /* Set initial QoS mode again now that WAN port is ready. */
701 set_et_qos_mode(sd);
703 close(sd);
705 enable_ip_forward();
707 led(LED_DIAG, 0); // for 4712, 5325E (?)
708 led(LED_DMZ, nvram_match("dmz_enable", "1"));
710 TRACE_PT("end\n");
713 #ifdef TCONFIG_IPV6
714 void start_wan6_done(const char *wan_ifname)
716 struct in_addr addr4;
717 struct in6_addr addr;
718 static char addr6[INET6_ADDRSTRLEN];
720 int service = get_ipv6_service();
722 if (service != IPV6_DISABLED) {
723 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
724 accept_ra(wan_ifname);
727 switch (service) {
728 case IPV6_NATIVE:
729 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
730 break;
731 case IPV6_NATIVE_DHCP:
732 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
733 stop_dhcp6c();
734 start_dhcp6c();
735 break;
736 case IPV6_ANYCAST_6TO4:
737 case IPV6_6IN4:
738 stop_ipv6_tunnel();
739 if (service == IPV6_ANYCAST_6TO4) {
740 addr4.s_addr = 0;
741 memset(&addr, 0, sizeof(addr));
742 inet_aton(get_wanip(), &addr4);
743 addr.s6_addr16[0] = htons(0x2002);
744 ipv6_mapaddr4(&addr, 16, &addr4, 0);
745 addr.s6_addr16[3] = htons(0x0001);
746 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
747 nvram_set("ipv6_prefix", addr6);
749 start_ipv6_tunnel();
750 // FIXME: give it a few seconds for DAD completion
751 sleep(2);
752 break;
755 #endif
757 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
758 // wan_ifname: vlan1
759 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
761 void start_wan_done(char *wan_ifname)
763 int proto;
764 int n;
765 char *gw;
766 struct sysinfo si;
767 int wanup;
769 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
771 sysinfo(&si);
772 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
774 proto = get_wan_proto();
776 // delete all default routes
777 route_del(wan_ifname, 0, NULL, NULL, NULL);
779 if (proto != WP_DISABLED) {
780 // set default route to gateway if specified
781 gw = wan_gateway();
782 #if 0
783 if (proto == WP_PPTP && !using_dhcpc()) {
784 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
785 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
787 #endif
788 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
789 if (proto == WP_DHCP || proto == WP_STATIC) {
790 // possibly gateway is over the bridge, try adding a route to gateway first
791 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
794 n = 5;
795 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
796 sleep(1);
798 _dprintf("set default gateway=%s n=%d\n", gw, n);
800 // hack: avoid routing cycles, when both peer and server have the same IP
801 if (proto == WP_PPTP || proto == WP_L2TP) {
802 // delete gateway route as it's no longer needed
803 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
807 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
809 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
810 if (proto == WP_STATIC)
812 int ifindex;
813 u_int32_t wan_ip;
814 unsigned char wan_mac[6];
816 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
817 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
820 #endif
822 if (proto == WP_PPTP || proto == WP_L2TP) {
823 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
824 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
826 if (proto == WP_L2TP) {
827 route_add(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway"), "255.255.255.255"); // fixed routing problem in Israel by kanki
831 dns_to_resolv();
832 start_dnsmasq();
833 start_firewall();
834 start_qos();
835 start_qoslimit();
836 start_arpbind();
840 do_static_routes(1);
841 // and routes supplied via DHCP
842 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
844 stop_zebra();
845 start_zebra();
847 wanup = check_wanup();
849 if ((wanup) || (time(0) < Y2K)) {
850 stop_ntpc();
851 start_ntpc();
854 if ((wanup) || (proto == WP_DISABLED)) {
855 stop_ddns();
856 start_ddns();
857 stop_igmp_proxy();
858 start_igmp_proxy();
861 #ifdef TCONFIG_IPV6
862 start_wan6_done(get_wan6face());
863 #endif
865 stop_upnp();
866 start_upnp();
868 // restart httpd
869 start_httpd();
871 if (wanup) {
872 SET_LED(GOT_IP);
873 notice_set("wan", "");
875 run_nvscript("script_wanup", NULL, 0);
878 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
879 if (check_hw_type() == HW_BCM4702) {
880 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
881 if (nvram_match("lan_stp", "1"))
882 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
883 #ifdef TCONFIG_VLAN
884 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
885 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
886 if (nvram_match("lan1_stp", "1"))
887 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
889 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
890 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
891 if (nvram_match("lan2_stp", "1"))
892 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
894 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
895 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
896 if (nvram_match("lan3_stp", "1"))
897 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
899 #endif
902 if (wanup)
903 start_vpn_eas();
905 unlink(wan_connecting);
907 TRACE_PT("end\n");
910 void stop_wan(void)
912 char name[80];
913 char *next;
915 TRACE_PT("begin\n");
917 stop_arpbind();
918 stop_qoslimit();
919 stop_qos();
920 stop_upnp(); //!!TB - moved from stop_services()
921 stop_firewall();
922 stop_igmp_proxy();
923 stop_ntpc();
925 #ifdef TCONFIG_IPV6
926 stop_ipv6_tunnel();
927 stop_dhcp6c();
928 nvram_set("ipv6_get_dns", "");
929 #endif
931 /* Kill any WAN client daemons or callbacks */
932 stop_redial();
933 stop_pppoe();
934 stop_ppp();
935 stop_dhcpc();
936 clear_resolv();
937 nvram_set("wan_get_dns", "");
939 /* Bring down WAN interfaces */
940 foreach(name, nvram_safe_get("wan_ifnames"), next)
941 ifconfig(name, 0, "0.0.0.0", NULL);
943 SET_LED(RELEASE_IP);
944 //notice_set("wan", "");
945 unlink("/var/notice/wan");
946 unlink(wan_connecting);
948 TRACE_PT("end\n");