MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / rc / wan.c
blobc27d2fb814a9369283a6dca7e2ab8f372c1fbf80
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 break;
154 case WP_L2TP:
155 fprintf(fp, "nomppe nomppc\n");
156 if (nvram_get_int("mtu_enable"))
157 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
158 break;
161 if (demand) {
162 // demand mode
163 fprintf(fp,
164 "demand\n" // Dial on demand
165 "idle %d\n"
166 "ipcp-accept-remote\n"
167 "ipcp-accept-local\n"
168 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
169 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
170 nvram_get_int("ppp_idletime") * 60);
173 #ifdef TCONFIG_IPV6
174 switch (get_ipv6_service()) {
175 case IPV6_NATIVE:
176 case IPV6_NATIVE_DHCP:
177 fprintf(fp, "+ipv6\n");
178 break;
180 #endif
181 // User specific options
182 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
184 fclose(fp);
185 make_secrets();
187 TRACE_PT("end\n");
188 return 0;
191 static void stop_ppp(void)
193 TRACE_PT("begin\n");
195 unlink(ppp_linkfile);
197 killall_tk("ip-up");
198 killall_tk("ip-down");
199 #ifdef TCONFIG_IPV6
200 killall_tk("ipv6-up");
201 killall_tk("ipv6-down");
202 #endif
203 killall_tk("xl2tpd");
204 killall_tk("pppd");
205 killall_tk("listen");
207 TRACE_PT("end\n");
210 static void run_pppd(void)
212 eval("pppd");
214 if (nvram_get_int("ppp_demand")) {
215 // demand mode
217 Fixed issue id 7887(or 7787):
218 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
220 stop_dnsmasq();
221 dns_to_resolv();
222 start_dnsmasq();
224 // Trigger Connect On Demand if user ping pptp server
225 eval("listen", nvram_safe_get("lan_ifname"));
227 else {
228 // keepalive mode
229 start_redial();
233 // -----------------------------------------------------------------------------
235 inline void stop_pptp(void)
237 stop_ppp();
240 void start_pptp(int mode)
242 TRACE_PT("begin\n");
244 if (!using_dhcpc()) stop_dhcpc();
245 stop_pptp();
247 if (config_pppd(WP_PPTP, 0) != 0)
248 return;
250 run_pppd();
252 TRACE_PT("end\n");
255 // -----------------------------------------------------------------------------
257 void preset_wan(char *ifname, char *gw, char *netmask)
259 int i;
261 /* Delete all default routes */
262 route_del(ifname, 0, NULL, NULL, NULL);
264 /* try adding a route to gateway first */
265 route_add(ifname, 0, gw, NULL, "255.255.255.255");
267 /* Set default route to gateway if specified */
268 i = 5;
269 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
270 sleep(1);
272 _dprintf("set default gateway=%s n=%d\n", gw, i);
274 /* Add routes to dns servers as well for demand ppp to work */
275 char word[100], *next;
276 in_addr_t mask = inet_addr(netmask);
277 foreach(word, nvram_safe_get("wan_get_dns"), next) {
278 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
279 route_add(ifname, 0, word, gw, "255.255.255.255");
282 dns_to_resolv();
283 start_dnsmasq();
284 sleep(1);
285 start_firewall();
288 // -----------------------------------------------------------------------------
291 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
292 static void start_tmp_ppp(int num, char *ifname)
294 int timeout;
295 struct ifreq ifr;
296 int s;
298 TRACE_PT("begin: num=%d\n", num);
300 if (num != 0) return;
302 // Wait for ppp0 to be created
303 timeout = 15;
304 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
305 sleep(1);
306 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
309 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
310 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
312 // Set temporary IP address
313 timeout = 3;
314 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
315 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
316 sleep(1);
318 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
319 nvram_set("wan_netmask", "255.255.255.255");
321 // Set temporary P-t-P address
322 timeout = 3;
323 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
324 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
325 sleep(1);
327 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
329 close(s);
331 start_wan_done(ifname);
332 TRACE_PT("end\n");
335 void start_pppoe(int num)
337 char ifname[8];
339 TRACE_PT("begin pppoe_num=%d\n", num);
341 if (num != 0) return;
343 stop_pppoe();
345 snprintf(ifname, sizeof(ifname), "ppp%d", num);
347 if (config_pppd(WP_PPPOE, num) != 0)
348 return;
350 run_pppd();
352 if (nvram_get_int("ppp_demand"))
353 start_tmp_ppp(num, ifname);
354 else
355 ifconfig(ifname, IFUP, NULL, NULL);
357 TRACE_PT("end\n");
360 void stop_pppoe(void)
362 stop_ppp();
365 #if 0
366 void stop_singe_pppoe(int num)
368 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
370 int i;
372 if (num != 0) return;
374 i = nvram_get_int("pppoe_pid0");
375 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
376 do {
377 sleep(2);
378 } while (kill(i, SIGKILL) == 0);
381 unlink(ppp_linkfile);
382 nvram_unset("pppoe_ifname0");
384 nvram_set("wan_get_dns", "");
385 clear_resolv();
387 #endif
389 // -----------------------------------------------------------------------------
391 inline void stop_l2tp(void)
393 stop_ppp();
396 void start_l2tp(void)
398 TRACE_PT("begin\n");
400 FILE *fp;
401 int demand;
403 stop_l2tp();
405 if (config_pppd(WP_L2TP, 0) != 0)
406 return;
408 demand = nvram_get_int("ppp_demand");
410 /* Generate XL2TPD configuration file */
411 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
412 return;
413 fprintf(fp,
414 "[global]\n"
415 "access control = no\n"
416 "port = 1701\n"
417 "[lac l2tp]\n"
418 "lns = %s\n"
419 "pppoptfile = %s\n"
420 "redial = yes\n"
421 "max redials = 32767\n"
422 "redial timeout = %d\n"
423 "ppp debug = %s\n",
424 nvram_safe_get("l2tp_server_ip"),
425 ppp_optfile,
426 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
427 nvram_get_int("debug_ppp") ? "yes" : "no");
428 fclose(fp);
430 enable_ip_forward();
432 eval("xl2tpd");
434 if (demand) {
435 eval("listen", nvram_safe_get("lan_ifname"));
437 else {
438 force_to_dial();
439 start_redial();
442 TRACE_PT("end\n");
445 // -----------------------------------------------------------------------------
447 static char *wan_gateway(void)
449 char *gw = nvram_safe_get("wan_gateway_get");
450 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
451 gw = nvram_safe_get("wan_gateway");
452 return gw;
455 // -----------------------------------------------------------------------------
457 // trigger connect on demand
458 void force_to_dial(void)
460 TRACE_PT("begin\n");
462 sleep(1);
463 switch (get_wan_proto()) {
464 case WP_L2TP:
465 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
466 break;
467 case WP_PPTP:
468 eval("ping", "-c", "2", "10.112.112.112");
469 break;
470 case WP_DISABLED:
471 case WP_STATIC:
472 break;
473 default:
474 eval("ping", "-c", "2", wan_gateway());
475 break;
478 TRACE_PT("end\n");
481 // -----------------------------------------------------------------------------
483 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
485 char *routes, *tmp;
486 int bits;
487 struct in_addr mask;
488 char netmask[16];
490 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
491 tmp = routes = strdup(nvram_safe_get(nvname));
492 while (tmp && *tmp) {
493 char *ipaddr, *gateway, *nmask;
495 ipaddr = nmask = strsep(&tmp, " ");
496 strcpy(netmask, "255.255.255.255");
498 if (nmask) {
499 ipaddr = strsep(&nmask, "/");
500 if (nmask && *nmask) {
501 bits = strtol(nmask, &nmask, 10);
502 if (bits >= 1 && bits <= 32) {
503 mask.s_addr = htonl(0xffffffff << (32 - bits));
504 strcpy(netmask, inet_ntoa(mask));
508 gateway = strsep(&tmp, " ");
510 if (gateway && *gateway) {
511 if (add)
512 route_add(ifname, metric, ipaddr, gateway, netmask);
513 else
514 route_del(ifname, metric, ipaddr, gateway, netmask);
517 free(routes);
520 void do_wan_routes(char *ifname, int metric, int add)
522 if (nvram_get_int("dhcp_routes")) {
523 // Static Routes: IP ROUTER IP2 ROUTER2 ...
524 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
525 _do_wan_routes(ifname, "wan_routes1", metric, add);
526 _do_wan_routes(ifname, "wan_routes2", metric, add);
530 // -----------------------------------------------------------------------------
532 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
534 static int is_sta(int idx, int unit, int subunit, void *param)
536 char **p = param;
538 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
539 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
540 return 1;
542 return 0;
545 void start_wan(int mode)
547 int wan_proto;
548 char *wan_ifname;
549 char *p = NULL;
550 struct ifreq ifr;
551 int sd;
552 int max;
553 int mtu;
554 char buf[128];
556 TRACE_PT("begin\n");
558 f_write(wan_connecting, NULL, 0, 0, 0);
562 if (!foreach_wif(1, &p, is_sta)) {
563 p = nvram_safe_get("wan_ifnameX");
564 set_mac(p, "mac_wan", 1);
566 nvram_set("wan_ifname", p);
567 nvram_set("wan_ifnames", p);
571 wan_ifname = nvram_safe_get("wan_ifname");
572 if (wan_ifname[0] == 0) {
573 wan_ifname = "none";
574 nvram_set("wan_ifname", wan_ifname);
577 if (strcmp(wan_ifname, "none") == 0) {
578 nvram_set("wan_proto", "disabled");
579 syslog(LOG_INFO, "No WAN");
584 wan_proto = get_wan_proto();
586 // set the default gateway for WAN interface
587 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
589 if (wan_proto == WP_DISABLED) {
590 start_wan_done(wan_ifname);
591 return;
594 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
595 perror("socket");
596 return;
599 // MTU
601 switch (wan_proto) {
602 case WP_PPPOE:
603 max = 1492;
604 break;
605 case WP_PPTP:
606 case WP_L2TP:
607 max = 1460;
608 break;
609 default:
610 max = 1500;
611 break;
613 if (nvram_match("mtu_enable", "0")) {
614 mtu = max;
616 else {
617 mtu = nvram_get_int("wan_mtu");
618 if (mtu > max) mtu = max;
619 else if (mtu < 576) mtu = 576;
621 sprintf(buf, "%d", mtu);
622 nvram_set("wan_mtu", buf);
623 nvram_set("wan_run_mtu", buf);
625 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
626 /* if (wan_proto == WP_PPTP) {
627 mtu += 40;
628 } */ // commented out; checkme -- zzz
630 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
631 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
632 ifr.ifr_mtu = mtu;
633 strcpy(ifr.ifr_name, wan_ifname);
634 ioctl(sd, SIOCSIFMTU, &ifr);
639 ifconfig(wan_ifname, IFUP, NULL, NULL);
641 start_firewall();
643 set_host_domain_name();
645 switch (wan_proto) {
646 case WP_PPPOE:
647 start_pppoe(PPPOE0);
648 break;
649 case WP_DHCP:
650 case WP_L2TP:
651 case WP_PPTP:
652 if (using_dhcpc()) {
653 stop_dhcpc();
654 start_dhcpc();
656 else if (wan_proto != WP_DHCP) {
657 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
658 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
660 p = nvram_safe_get("wan_gateway");
661 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
662 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
664 switch (wan_proto) {
665 case WP_PPTP:
666 start_pptp(mode);
667 break;
668 case WP_L2TP:
669 start_l2tp();
670 break;
673 break;
674 default: // static
675 nvram_set("wan_iface", wan_ifname);
676 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
678 int r = 10;
679 while ((!check_wanup()) && (r-- > 0)) {
680 sleep(1);
683 start_wan_done(wan_ifname);
684 break;
687 // Get current WAN hardware address
688 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
689 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
690 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
693 /* Set initial QoS mode again now that WAN port is ready. */
694 set_et_qos_mode(sd);
696 close(sd);
698 enable_ip_forward();
700 led(LED_DIAG, 0); // for 4712, 5325E (?)
701 led(LED_DMZ, nvram_match("dmz_enable", "1"));
703 TRACE_PT("end\n");
706 #ifdef TCONFIG_IPV6
707 void start_wan6_done(const char *wan_ifname)
709 struct in_addr addr4;
710 struct in6_addr addr;
711 static char addr6[INET6_ADDRSTRLEN];
713 int service = get_ipv6_service();
715 if (service != IPV6_DISABLED) {
716 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
717 accept_ra(wan_ifname);
720 switch (service) {
721 case IPV6_NATIVE:
722 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
723 break;
724 case IPV6_NATIVE_DHCP:
725 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
726 stop_dhcp6c();
727 start_dhcp6c();
728 break;
729 case IPV6_ANYCAST_6TO4:
730 case IPV6_6IN4:
731 stop_ipv6_tunnel();
732 if (service == IPV6_ANYCAST_6TO4) {
733 addr4.s_addr = 0;
734 memset(&addr, 0, sizeof(addr));
735 inet_aton(get_wanip(), &addr4);
736 addr.s6_addr16[0] = htons(0x2002);
737 ipv6_mapaddr4(&addr, 16, &addr4, 0);
738 addr.s6_addr16[3] = htons(0x0001);
739 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
740 nvram_set("ipv6_prefix", addr6);
742 start_ipv6_tunnel();
743 // FIXME: give it a few seconds for DAD completion
744 sleep(2);
745 break;
748 #endif
750 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
751 // wan_ifname: vlan1
752 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
754 void start_wan_done(char *wan_ifname)
756 int proto;
757 int n;
758 char *gw;
759 struct sysinfo si;
760 int wanup;
762 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
764 sysinfo(&si);
765 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
767 proto = get_wan_proto();
769 // delete all default routes
770 route_del(wan_ifname, 0, NULL, NULL, NULL);
772 if (proto != WP_DISABLED) {
773 // set default route to gateway if specified
774 gw = wan_gateway();
775 #if 0
776 if (proto == WP_PPTP && !using_dhcpc()) {
777 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
778 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
780 #endif
781 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
782 if (proto == WP_DHCP || proto == WP_STATIC) {
783 // possibly gateway is over the bridge, try adding a route to gateway first
784 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
787 n = 5;
788 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
789 sleep(1);
791 _dprintf("set default gateway=%s n=%d\n", gw, n);
793 // hack: avoid routing cycles, when both peer and server have the same IP
794 if (proto == WP_PPTP || proto == WP_L2TP) {
795 // delete gateway route as it's no longer needed
796 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
800 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
802 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
803 if (proto == WP_STATIC)
805 int ifindex;
806 u_int32_t wan_ip;
807 unsigned char wan_mac[6];
809 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
810 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
813 #endif
815 if (proto == WP_PPTP || proto == WP_L2TP) {
816 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
817 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
819 if (proto == WP_L2TP) {
820 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
824 dns_to_resolv();
825 start_dnsmasq();
827 start_firewall();
828 start_qos();
829 start_cmon();
831 do_static_routes(1);
832 // and routes supplied via DHCP
833 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
835 stop_zebra();
836 start_zebra();
838 wanup = check_wanup();
840 if ((wanup) || (time(0) < Y2K)) {
841 stop_ntpc();
842 start_ntpc();
845 if ((wanup) || (proto == WP_DISABLED)) {
846 stop_ddns();
847 start_ddns();
848 stop_igmp_proxy();
849 start_igmp_proxy();
852 #ifdef TCONFIG_IPV6
853 start_wan6_done(get_wan6face());
854 #endif
856 stop_upnp();
857 start_upnp();
859 // restart httpd
860 start_httpd();
862 if (wanup) {
863 SET_LED(GOT_IP);
864 notice_set("wan", "");
866 run_nvscript("script_wanup", NULL, 0);
869 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
870 if (check_hw_type() == HW_BCM4702) {
871 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
872 if (nvram_match("lan_stp", "1")) eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
875 if (wanup)
876 start_vpn_eas();
878 unlink(wan_connecting);
880 new_qoslimit_start(); //!! RAF
881 new_arpbind_start(); //!! RAF
883 TRACE_PT("end\n");
886 void stop_wan(void)
888 char name[80];
889 char *next;
891 TRACE_PT("begin\n");
893 new_arpbind_stop(); //!! RAF
894 new_qoslimit_stop(); //!! RAF
896 stop_qos();
897 stop_upnp(); //!!TB - moved from stop_services()
898 stop_firewall();
899 stop_cmon();
900 stop_igmp_proxy();
901 stop_ntpc();
903 #ifdef TCONFIG_IPV6
904 stop_ipv6_tunnel();
905 stop_dhcp6c();
906 nvram_set("ipv6_get_dns", "");
907 #endif
909 /* Kill any WAN client daemons or callbacks */
910 stop_redial();
911 stop_pppoe();
912 stop_ppp();
913 stop_dhcpc();
914 clear_resolv();
915 nvram_set("wan_get_dns", "");
917 /* Bring down WAN interfaces */
918 foreach(name, nvram_safe_get("wan_ifnames"), next)
919 ifconfig(name, 0, "0.0.0.0", NULL);
921 SET_LED(RELEASE_IP);
922 //notice_set("wan", "");
923 unlink("/var/notice/wan");
924 unlink(wan_connecting);
926 TRACE_PT("end\n");