rc, httpd: cosmetics
[tomato.git] / release / src / router / rc / wan.c
blobcf3b44be60714d7105fc4949cac45c08e7aa1b2f
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 int service;
711 switch ((service = get_ipv6_service())) {
712 case IPV6_NATIVE:
713 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
714 break;
715 case IPV6_NATIVE_DHCP:
716 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
717 stop_dhcp6c();
718 start_dhcp6c();
719 break;
720 case IPV6_6IN4:
721 stop_ipv6_sit_tunnel();
722 start_ipv6_sit_tunnel();
723 break;
726 if (service != IPV6_DISABLED) {
727 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
728 accept_ra(wan_ifname);
731 #endif
733 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
734 // wan_ifname: vlan1
735 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
737 void start_wan_done(char *wan_ifname)
739 int proto;
740 int n;
741 char *gw;
742 struct sysinfo si;
743 int wanup;
745 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
747 sysinfo(&si);
748 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
750 proto = get_wan_proto();
752 // delete all default routes
753 route_del(wan_ifname, 0, NULL, NULL, NULL);
755 if (proto != WP_DISABLED) {
756 // set default route to gateway if specified
757 gw = wan_gateway();
758 #if 0
759 if (proto == WP_PPTP && !using_dhcpc()) {
760 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
761 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
763 #endif
764 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
765 if (proto == WP_DHCP || proto == WP_STATIC) {
766 // possibly gateway is over the bridge, try adding a route to gateway first
767 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
770 n = 5;
771 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
772 sleep(1);
774 _dprintf("set default gateway=%s n=%d\n", gw, n);
776 // hack: avoid routing cycles, when both peer and server have the same IP
777 if (proto == WP_PPTP || proto == WP_L2TP) {
778 // delete gateway route as it's no longer needed
779 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
783 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
785 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
786 if (proto == WP_STATIC)
788 int ifindex;
789 u_int32_t wan_ip;
790 unsigned char wan_mac[6];
792 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
793 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
796 #endif
798 if (proto == WP_PPTP || proto == WP_L2TP) {
799 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
800 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
802 if (proto == WP_L2TP) {
803 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
807 dns_to_resolv();
808 start_dnsmasq();
810 start_firewall();
811 start_qos();
813 do_static_routes(1);
814 // and routes supplied via DHCP
815 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
817 stop_zebra();
818 start_zebra();
820 wanup = check_wanup();
822 if ((wanup) || (time(0) < Y2K)) {
823 stop_ntpc();
824 start_ntpc();
827 if ((wanup) || (proto == WP_DISABLED)) {
828 stop_ddns();
829 start_ddns();
830 stop_igmp_proxy();
831 start_igmp_proxy();
834 #ifdef TCONFIG_IPV6
835 start_wan6_done(get_wan6face());
836 #endif
838 // restart httpd
839 start_httpd();
841 stop_upnp();
842 start_upnp();
844 if (wanup) {
845 SET_LED(GOT_IP);
846 notice_set("wan", "");
848 run_nvscript("script_wanup", NULL, 0);
851 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
852 if (check_hw_type() == HW_BCM4702) {
853 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
854 if (nvram_match("lan_stp", "1")) eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
857 if (wanup)
858 start_vpn_eas();
860 unlink(wan_connecting);
862 TRACE_PT("end\n");
865 void stop_wan(void)
867 char name[80];
868 char *next;
870 TRACE_PT("begin\n");
872 stop_qos();
873 stop_upnp(); //!!TB - moved from stop_services()
874 stop_firewall();
875 stop_igmp_proxy();
876 stop_ntpc();
878 #ifdef TCONFIG_IPV6
879 stop_ipv6_sit_tunnel();
880 stop_dhcp6c();
881 nvram_set("ipv6_get_dns", "");
882 #endif
884 /* Kill any WAN client daemons or callbacks */
885 stop_redial();
886 stop_pppoe();
887 stop_ppp();
888 stop_dhcpc();
889 clear_resolv();
890 nvram_set("wan_get_dns", "");
892 /* Bring down WAN interfaces */
893 foreach(name, nvram_safe_get("wan_ifnames"), next)
894 ifconfig(name, 0, "0.0.0.0", NULL);
896 SET_LED(RELEASE_IP);
897 //notice_set("wan", "");
898 unlink("/var/notice/wan");
899 unlink(wan_connecting);
901 TRACE_PT("end\n");