Merge branch 'tomato-RT' into tomato-1.28-Toastman-RT
[tomato.git] / release / src / router / rc / wan.c
blobbcf4b45eb70083ecaf32967bc5f73966110c7aa4
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)
49 static const char ppp_linkfile[] = "/tmp/ppp/link";
50 static const char ppp_optfile[] = "/tmp/ppp/options";
52 static void make_secrets(void)
54 FILE *f;
55 char *user;
56 char *pass;
58 user = nvram_safe_get("ppp_username");
59 pass = nvram_safe_get("ppp_passwd");
60 if ((f = fopen("/tmp/ppp/pap-secrets", "w")) != NULL) {
61 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
62 fclose(f);
64 chmod("/tmp/ppp/pap-secrets", 0600);
66 if ((f = fopen("/tmp/ppp/chap-secrets", "w")) != NULL) {
67 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
68 fclose(f);
70 chmod("/tmp/ppp/chap-secrets", 0600);
73 // -----------------------------------------------------------------------------
75 static int config_pppd(int wan_proto)
77 TRACE_PT("begin\n");
79 FILE *fp;
81 mkdir("/tmp/ppp", 0777);
82 symlink("/sbin/rc", "/tmp/ppp/ip-up");
83 symlink("/sbin/rc", "/tmp/ppp/ip-down");
84 symlink("/dev/null", "/tmp/ppp/connect-errors");
86 // Generate options file
87 if ((fp = fopen(ppp_optfile, "w")) == NULL) {
88 perror(ppp_optfile);
89 return -1;
92 fprintf(fp,
93 "defaultroute\n" // Add a default route to the system routing tables, using the peer as the gateway
94 "usepeerdns\n" // Ask the peer for up to 2 DNS server addresses
95 "user '%s'\n"
96 "default-asyncmap\n" // Disable asyncmap negotiation
97 "nopcomp\n" // Disable protocol field compression
98 "noaccomp\n" // Disable Address/Control compression
99 "novj\n" // Disable Van Jacobson style TCP/IP header compression
100 "nobsdcomp\n" // Disable BSD-Compress compression
101 "nodeflate\n" // Disable Deflate compression
102 "noauth\n" // Do not authenticate peer
103 "refuse-eap\n" // Do not use eap
104 "maxfail 0\n"
105 "lcp-echo-interval %d\n"
106 "lcp-echo-failure %d\n"
107 "%s", // Debug
108 nvram_safe_get("ppp_username"),
109 nvram_get_int("pppoe_lei") ? : 10,
110 nvram_get_int("pppoe_lef") ? : 5,
111 nvram_get_int("debug_ppp") ? "debug\n" : "");
113 if (wan_proto != WP_L2TP)
114 fprintf(fp, "persist\n");
116 switch (wan_proto) {
117 case WP_PPTP:
118 fprintf(fp,
119 "plugin pptp.so\n"
120 "pptp_server %s\n"
121 "nomppe-stateful\n"
122 "mtu %d\n",
123 nvram_safe_get("pptp_server_ip"),
124 nvram_get_int("mtu_enable") ? nvram_get_int("wan_mtu") : 1400);
125 break;
126 default: // l2tp, pppoe
127 fprintf(fp, "nomppe nomppc\n");
128 if (nvram_get_int("mtu_enable"))
129 fprintf(fp, "mtu %s\n", nvram_safe_get("wan_mtu"));
130 break;
133 if (nvram_match("ppp_demand", "1")) {
134 // demand mode
135 fprintf(fp,
136 "demand\n" // Dial on demand
137 "idle %d\n"
138 "ipcp-accept-remote\n"
139 "ipcp-accept-local\n"
140 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
141 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
142 nvram_get_int("ppp_idletime") * 60);
145 // User specific options
146 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
148 fclose(fp);
149 make_secrets();
151 TRACE_PT("end\n");
152 return 0;
155 static void stop_ppp(void)
157 TRACE_PT("begin\n");
159 unlink(ppp_linkfile);
161 killall_tk("ip-up");
162 killall_tk("ip-down");
163 killall_tk("xl2tpd");
164 killall_tk("pppd");
165 killall_tk("listen");
167 TRACE_PT("end\n");
170 // -----------------------------------------------------------------------------
172 inline void stop_pptp(void)
174 stop_ppp();
177 void start_pptp(int mode)
179 TRACE_PT("begin\n");
181 if (!using_dhcpc()) stop_dhcpc();
182 stop_pppoe();
183 stop_pptp();
185 if (config_pppd(WP_PPTP) != 0)
186 return;
188 if (!using_dhcpc()) {
189 // Bring up WAN interface
190 ifconfig(nvram_safe_get("wan_ifname"), IFUP,
191 nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
194 eval("pppd");
196 if (nvram_get_int("ppp_demand")) {
197 // demand mode
199 Fixed issue id 7887(or 7787):
200 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
202 stop_dnsmasq();
203 dns_to_resolv();
204 start_dnsmasq();
206 // Trigger Connect On Demand if user ping pptp server
207 eval("listen", nvram_safe_get("lan_ifname"));
209 else {
210 // keepalive mode
211 start_redial();
214 TRACE_PT("end\n");
217 // -----------------------------------------------------------------------------
220 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
221 static void start_tmp_ppp(int num)
223 int timeout;
224 char *ifname;
225 struct ifreq ifr;
226 int s;
228 TRACE_PT("begin: num=%d\n", num);
230 if (num != 0) return;
232 // Wait for ppp0 to be created
233 timeout = 15;
234 while ((ifconfig(ifname = nvram_safe_get("pppoe_ifname0"), IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
235 sleep(1);
236 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
239 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
240 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
242 // Set temporary IP address
243 timeout = 3;
244 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
245 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
246 sleep(1);
248 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
249 nvram_set("wan_netmask", "255.255.255.255");
251 // Set temporary P-t-P address
252 timeout = 3;
253 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
254 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
255 sleep(1);
257 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
259 close(s);
261 start_wan_done(ifname);
262 TRACE_PT("end\n");
265 void start_pppoe(int num)
267 pid_t pid;
268 char idle[16];
269 char retry[16];
270 char lcp_echo_interval[16];
271 char lcp_echo_fails[16];
272 char *mtu;
273 int dod;
274 int n;
276 TRACE_PT("begin pppoe_num=%d\n", num);
278 if (num != 0) return;
280 stop_pppoe();
282 nvram_set("pppoe_ifname0", "");
284 dod = nvram_match("ppp_demand", "1");
286 // -i
287 sprintf(idle, "%d", dod ? (nvram_get_int("ppp_idletime") * 60) : 0);
289 // [-N]
290 sprintf(retry, "%d", (nvram_get_int("ppp_redialperiod") / 5) - 1);
292 // [-r] [-t]
293 mtu = nvram_safe_get("wan_mtu");
295 // [-I n] Interval between LCP echo-requests
296 sprintf(lcp_echo_interval, "%d", ((n = nvram_get_int("pppoe_lei")) > 0) ? n : 30);
298 // [-T n] Tolerance to unanswered echo-requests
299 sprintf(lcp_echo_fails, "%d", ((n = nvram_get_int("pppoe_lef")) > 0) ? n : 5);
301 char *pppoe_argv[] = {
302 "pppoecd",
303 nvram_safe_get("wan_ifname"),
304 "-u", nvram_safe_get("ppp_username"),
305 "-p", nvram_safe_get("ppp_passwd"),
306 "-r", mtu,
307 "-t", mtu,
308 "-i", idle, // >0 == dial on demand
309 "-I", lcp_echo_interval, // Send an LCP echo-request frame to the server every X seconds
310 "-N", retry, // To avoid kill pppd when pppd has been connecting.
311 "-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
312 "-P", "0", // PPPOE session number.
313 "-C", "pppoe_down", // by tallest 0407
314 "-R", // set default route
315 NULL, // debug
316 NULL, NULL, // pppoe_service
317 NULL, NULL, // pppoe_ac
318 NULL, NULL, // static IP
319 NULL, // pppoe_keepalive
320 NULL, // -x extended logging
321 NULL
323 char **arg;
324 char *p;
326 for (arg = pppoe_argv; *arg; arg++) {
330 if (nvram_get_int("debug_ppp")) {
331 *arg++ = "-d"; // debug mode; compile ppp w/ -DDEBUG !
334 if (((p = nvram_get("ppp_service")) != NULL) && (*p != 0)) {
335 *arg++ = "-s";
336 *arg++ = p;
339 // ?? zzz
340 if (((p = nvram_get("ppp_ac")) != NULL) && (*p != 0)) {
341 *arg++ = "-a";
342 *arg++ = p;
345 if (nvram_match("ppp_static", "1")) {
346 *arg++ = "-L";
347 *arg++ = nvram_safe_get("ppp_static_ip");
350 // ?? zzz
351 //if (nvram_match("pppoe_demand", "1") || nvram_match("pppoe_keepalive", "1"))
352 *arg++ = "-k";
354 if (nvram_contains_word("log_events", "pppoe")) *arg++ = "-x";
356 mkdir("/tmp/ppp", 0777);
358 // ?? zzz
359 symlink("/sbin/rc", "/tmp/ppp/ip-up");
360 symlink("/sbin/rc", "/tmp/ppp/ip-down");
361 symlink("/sbin/rc", "/tmp/ppp/set-pppoepid"); // tallest 1219
363 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
365 _eval(pppoe_argv, NULL, 0, &pid);
367 if (dod) start_tmp_ppp(num);
369 TRACE_PT("end\n");
372 void stop_pppoe(void)
374 TRACE_PT("begin\n");
376 unlink(ppp_linkfile);
377 nvram_unset("pppoe_ifname0");
378 killall_tk("pppoecd");
379 killall_tk("ip-up");
380 killall_tk("ip-down");
382 TRACE_PT("end\n");
385 void stop_singe_pppoe(int num)
387 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
389 int i;
391 if (num != 0) return;
393 i = nvram_get_int("pppoe_pid0");
394 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
395 do {
396 sleep(2);
397 } while (kill(i, SIGKILL) == 0);
400 unlink(ppp_linkfile);
401 nvram_unset("pppoe_ifname0");
403 nvram_set("wan_get_dns", "");
404 clear_resolv();
407 // -----------------------------------------------------------------------------
409 inline void stop_l2tp(void)
411 stop_ppp();
414 void start_l2tp(void)
416 TRACE_PT("begin\n");
418 FILE *fp;
420 stop_pppoe();
421 stop_l2tp();
423 if (config_pppd(WP_L2TP) != 0)
424 return;
426 /* Generate XL2TPD configuration file */
427 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
428 return;
429 fprintf(fp,
430 "[global]\n"
431 "access control = no\n"
432 "port = 1701\n"
433 "[lac l2tp]\n"
434 "lns = %s\n"
435 "pppoptfile = %s\n"
436 "redial = yes\n"
437 "max redials = 32767\n"
438 "redial timeout = %d\n"
439 "ppp debug = %s\n",
440 nvram_safe_get("l2tp_server_ip"),
441 ppp_optfile,
442 nvram_get_int("ppp_redialperiod") ? : 30,
443 nvram_get_int("debug_ppp") ? "yes" : "no");
444 fclose(fp);
446 enable_ip_forward();
448 eval("xl2tpd");
450 if (nvram_match("ppp_demand", "1")) {
451 eval("listen", nvram_safe_get("lan_ifname"));
453 else {
454 force_to_dial();
455 start_redial();
458 TRACE_PT("end\n");
461 // -----------------------------------------------------------------------------
463 // trigger connect on demand
464 void force_to_dial(void)
466 TRACE_PT("begin\n");
468 sleep(1);
469 switch (get_wan_proto()) {
470 case WP_L2TP:
471 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
472 break;
473 case WP_PPTP:
474 eval("ping", "-c", "2", "10.112.112.112");
475 break;
476 case WP_DISABLED:
477 case WP_STATIC:
478 break;
479 default:
480 eval("ping", "-c", "2", nvram_safe_get("wan_gateway"));
481 break;
484 TRACE_PT("end\n");
487 // -----------------------------------------------------------------------------
489 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
491 char *routes, *tmp;
492 int bits;
493 struct in_addr mask;
494 char netmask[16];
496 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
497 tmp = routes = strdup(nvram_safe_get(nvname));
498 while (tmp && *tmp) {
499 char *ipaddr, *gateway, *nmask;
501 ipaddr = nmask = strsep(&tmp, " ");
502 strcpy(netmask, "255.255.255.255");
504 if (nmask) {
505 ipaddr = strsep(&nmask, "/");
506 if (nmask && *nmask) {
507 bits = strtol(nmask, &nmask, 10);
508 if (bits >= 1 && bits <= 32) {
509 mask.s_addr = htonl(0xffffffff << (32 - bits));
510 strcpy(netmask, inet_ntoa(mask));
514 gateway = strsep(&tmp, " ");
516 if (gateway && *gateway) {
517 if (add) route_add(ifname, metric + 1, ipaddr, gateway, netmask);
518 else route_del(ifname, metric + 1, ipaddr, gateway, netmask);
521 free(routes);
524 void do_wan_routes(char *ifname, int metric, int add)
526 if (nvram_get_int("dhcp_routes")) {
527 // Static Routes: IP ROUTER IP2 ROUTER2 ...
528 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
529 _do_wan_routes(ifname, "wan_routes1", metric, add);
530 _do_wan_routes(ifname, "wan_routes2", metric, add);
534 // -----------------------------------------------------------------------------
536 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
538 void start_wan(int mode)
540 int wan_proto;
541 char *wan_ifname;
542 char *p;
543 struct ifreq ifr;
544 int sd;
545 int max;
546 int mtu;
547 char buf[128];
549 TRACE_PT("begin\n");
551 f_write(wan_connecting, NULL, 0, 0, 0);
555 if (nvram_match("wl_mode", "sta")) {
556 p = nvram_safe_get("wl_ifname");
558 else {
559 p = nvram_safe_get("wan_ifnameX");
560 set_mac(p, "mac_wan", 1);
562 nvram_set("wan_ifname", p);
563 nvram_set("wan_ifnames", p);
567 wan_ifname = nvram_safe_get("wan_ifname");
568 if (wan_ifname[0] == 0) {
569 wan_ifname = "none";
570 nvram_set("wan_ifname", wan_ifname);
573 if (strcmp(wan_ifname, "none") == 0) {
574 nvram_set("wan_proto", "disabled");
575 syslog(LOG_INFO, "No WAN");
580 wan_proto = get_wan_proto();
582 if (wan_proto == WP_DISABLED) {
583 start_wan_done(wan_ifname);
584 return;
587 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
588 perror("socket");
589 return;
592 // MTU
594 switch (wan_proto) {
595 case WP_PPPOE:
596 max = 1492;
597 break;
598 case WP_PPTP:
599 case WP_L2TP:
600 max = 1460;
601 break;
602 default:
603 max = 1500;
604 break;
606 if (nvram_match("mtu_enable", "0")) {
607 mtu = max;
609 else {
610 mtu = nvram_get_int("wan_mtu");
611 if (mtu > max) mtu = max;
612 else if (mtu < 576) mtu = 576;
614 sprintf(buf, "%d", mtu);
615 nvram_set("wan_mtu", buf);
616 nvram_set("wan_run_mtu", buf);
618 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
619 /* if (wan_proto == WP_PPTP) {
620 mtu += 40;
621 } */ // commented out; checkme -- zzz
623 if ((wan_proto != WP_PPTP && wan_proto != WP_L2TP) || nvram_get_int("mtu_enable")) {
624 ifr.ifr_mtu = mtu;
625 strcpy(ifr.ifr_name, wan_ifname);
626 ioctl(sd, SIOCSIFMTU, &ifr);
631 ifconfig(wan_ifname, IFUP, NULL, NULL);
633 set_host_domain_name();
635 switch (wan_proto) {
636 case WP_PPPOE:
637 start_pppoe(PPPOE0);
638 if (nvram_invmatch("ppp_demand", "1")) {
639 if (mode != REDIAL) start_redial();
641 break;
642 case WP_DHCP:
643 case WP_L2TP:
644 case WP_PPTP:
645 if (using_dhcpc()) {
646 stop_dhcpc();
647 start_dhcpc();
649 else if (wan_proto == WP_PPTP) {
650 start_pptp(mode);
652 break;
653 default: // static
654 nvram_set("wan_iface", wan_ifname);
655 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
657 int r = 10;
658 while ((!check_wanup()) && (r-- > 0)) {
659 sleep(1);
662 start_wan_done(wan_ifname);
663 break;
666 // Get current WAN hardware address
667 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
668 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
669 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
672 /* Set initial QoS mode again now that WAN port is ready. */
673 set_et_qos_mode(sd);
675 close(sd);
677 enable_ip_forward();
679 led(LED_DIAG, 0); // for 4712, 5325E (?)
680 led(LED_DMZ, nvram_match("dmz_enable", "1"));
682 TRACE_PT("end\n");
686 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
687 // wan_ifname: vlan1
688 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
691 void start_wan_done(char *wan_ifname)
693 int proto;
694 int n;
695 char *gw;
696 int dod;
697 struct sysinfo si;
698 int wanup;
699 int metric;
701 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
703 sysinfo(&si);
704 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
706 proto = get_wan_proto();
707 dod = nvram_match("ppp_demand", "1");
709 #if 0
710 if (using_dhcpc()) {
711 while (route_del(nvram_safe_get("wan_ifname"), 0, NULL, NULL, NULL) == 0) {
715 #endif
717 // delete all default routes
718 while (route_del(wan_ifname, 0, NULL, NULL, NULL) == 0) {
722 if (proto != WP_DISABLED) {
723 // set default route to gateway if specified
724 gw = (proto == WP_PPTP && !using_dhcpc()) ? nvram_safe_get("ppp_get_ip") : nvram_safe_get("wan_gateway");
725 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
726 if (proto == WP_DHCP || proto == WP_STATIC) {
727 // possibly gateway is over the bridge, try adding a route to gateway first
728 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
731 metric = 0;
732 if (proto == WP_PPTP || proto == WP_L2TP) {
733 if (nvram_get_int("ppp_defgw") || !using_dhcpc())
734 metric = 0;
735 else {
736 metric = 2;
738 // we are not using default gateway on remote network,
739 // add route to the vpn subnet
740 char *netmask = nvram_safe_get("wan_netmask");
741 struct in_addr net, mask;
742 if (strcmp(netmask, "0.0.0.0") == 0 || !inet_aton(netmask, &mask)) {
743 netmask = "255.255.255.0";
744 inet_aton(netmask, &mask);
746 if (inet_aton(gw, &net)) {
747 net.s_addr &= mask.s_addr;
748 route_add(wan_ifname, 0, inet_ntoa(net), gw, netmask);
750 // add routes to dns servers
751 char word[100], *next;
752 foreach(word, nvram_safe_get("wan_get_dns"), next) {
753 route_add(wan_ifname, 0, word, gw, "255.255.255.255");
759 n = 5;
760 while ((route_add(wan_ifname, metric, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
761 sleep(1);
763 _dprintf("set default gateway=%s n=%d\n", gw, n);
765 // hack: avoid routing cycles, when both peer and server have the same IP
766 if (proto == WP_PPTP || proto == WP_L2TP) {
767 // delete gateway route as it's no longer needed
768 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
772 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
774 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
775 if (proto == WP_STATIC)
777 int ifindex;
778 u_int32_t wan_ip;
779 unsigned char wan_mac[6];
781 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
782 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
785 #endif
787 if (proto == WP_PPTP || proto == WP_L2TP) {
788 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip
789 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway"), NULL, "255.255.255.255");
790 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
792 if (proto == WP_L2TP) {
793 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
797 dns_to_resolv();
798 start_dnsmasq();
800 start_firewall();
801 start_qos();
802 new_qoslimit_start();
803 new_arpbind_start();
805 stop_igmp_proxy();
806 start_igmp_proxy();
808 do_static_routes(1);
809 // and routes supplied via DHCP
810 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
812 stop_zebra();
813 start_zebra();
815 wanup = check_wanup();
817 if ((wanup) || (time(0) < Y2K)) {
818 stop_ntpc();
819 start_ntpc();
822 if ((wanup) || (proto == WP_DISABLED)) {
823 stop_ddns();
824 start_ddns();
827 stop_upnp();
828 start_upnp();
830 if (wanup) {
831 SET_LED(GOT_IP);
832 notice_set("wan", "");
834 run_nvscript("script_wanup", NULL, 0);
837 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
838 if (check_hw_type() == HW_BCM4702) {
839 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
840 if (nvram_match("lan_stp", "1")) eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
843 if (wanup)
844 start_vpn_eas();
846 unlink(wan_connecting);
848 TRACE_PT("end\n");
851 void stop_wan(void)
853 char name[80];
854 char *next;
856 TRACE_PT("begin\n");
858 new_arpbind_stop();
859 new_qoslimit_stop();
860 stop_qos();
861 stop_upnp(); //!!TB - moved from stop_services()
862 stop_firewall();
863 stop_igmp_proxy();
864 stop_ntpc();
866 /* Kill any WAN client daemons or callbacks */
867 stop_singe_pppoe(PPPOE0);
868 stop_pppoe();
869 stop_ppp();
870 stop_dhcpc();
871 stop_redial();
872 nvram_set("wan_get_dns", "");
874 /* Bring down WAN interfaces */
875 foreach(name, nvram_safe_get("wan_ifnames"), next)
876 ifconfig(name, 0, "0.0.0.0", NULL);
878 SET_LED(RELEASE_IP);
879 //notice_set("wan", "");
880 unlink("/var/notice/wan");
881 unlink(wan_connecting);
883 TRACE_PT("end\n");