kill PPTP client when WAN is stopped
[tomato.git] / release / src / router / rc / wan.c
blob6bbcdc675eaec8fd8b206fc359a3c60b75776f41
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/wanoptions";
47 #ifdef LINUX26
48 #ifdef TCONFIG_USB
49 static const char ppp3g_chatfile[] = "/tmp/ppp/connect.chat";
50 #endif
51 #endif
53 static void make_secrets(void)
55 FILE *f;
56 char *user;
57 char *pass;
59 user = nvram_safe_get("ppp_username");
60 pass = nvram_safe_get("ppp_passwd");
61 if ((f = fopen("/tmp/ppp/pap-secrets", "w")) != NULL) {
62 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
63 fclose(f);
65 chmod("/tmp/ppp/pap-secrets", 0600);
67 if ((f = fopen("/tmp/ppp/chap-secrets", "w")) != NULL) {
68 fprintf(f, "\"%s\" * \"%s\" *\n", user, pass);
69 fclose(f);
71 chmod("/tmp/ppp/chap-secrets", 0600);
74 // -----------------------------------------------------------------------------
76 static int config_pppd(int wan_proto, int num)
78 TRACE_PT("begin\n");
80 FILE *fp;
81 FILE *cfp;
82 char *p;
83 int demand;
85 mkdir("/tmp/ppp", 0777);
86 symlink("/sbin/rc", "/tmp/ppp/ip-up");
87 symlink("/sbin/rc", "/tmp/ppp/ip-down");
88 #ifdef TCONFIG_IPV6
89 symlink("/sbin/rc", "/tmp/ppp/ipv6-up");
90 symlink("/sbin/rc", "/tmp/ppp/ipv6-down");
91 #endif
92 symlink("/dev/null", "/tmp/ppp/connect-errors");
94 demand = nvram_get_int("ppp_demand");
96 // Generate options file
97 if ((fp = fopen(ppp_optfile, "w")) == NULL) {
98 perror(ppp_optfile);
99 return -1;
102 #ifdef LINUX26
103 #ifdef TCONFIG_USB
104 if (nvram_match("wan_proto", "ppp3g") ) {
105 fprintf(fp,
106 "/dev/%s\n"
107 "460800\n"
108 "connect \"/usr/sbin/chat -V -t 60 -f %s\"\n"
109 "noipdefault\n"
110 "lock\n"
111 "crtscts\n"
112 "modem\n"
113 "ipcp-accept-local\n",
114 nvram_safe_get("modem_dev"),
115 ppp3g_chatfile);
117 if (strlen(nvram_get("ppp_username")) >0 )
118 fprintf(fp, "user '%s'\n", nvram_get("ppp_username"));
119 } else {
120 #endif
121 #endif
122 fprintf(fp,
123 "unit %d\n"
124 "user '%s'\n"
125 "lcp-echo-adaptive\n", // Suppress LCP echo-requests if traffic was received
126 num,
127 nvram_safe_get("ppp_username"));
128 #ifdef LINUX26
129 #ifdef TCONFIG_USB
131 #endif
132 #endif
134 fprintf(fp,
135 "defaultroute\n" // Add a default route to the system routing tables, using the peer as the gateway
136 "usepeerdns\n" // Ask the peer for up to 2 DNS server addresses
137 "default-asyncmap\n" // Disable asyncmap negotiation
138 "nopcomp\n" // Disable protocol field compression
139 "noaccomp\n" // Disable Address/Control compression
140 "novj\n" // Disable Van Jacobson style TCP/IP header compression
141 "nobsdcomp\n" // Disable BSD-Compress compression
142 "nodeflate\n" // Disable Deflate compression
143 "noauth\n" // Do not authenticate peer
144 "refuse-eap\n" // Do not use eap
145 "maxfail 0\n" // Never give up
146 "lcp-echo-interval %d\n"// Interval between LCP echo-requests
147 "lcp-echo-failure %d\n" // Tolerance to unanswered echo-requests
148 "%s", // Debug
149 nvram_get_int("pppoe_lei") ? : 10,
150 nvram_get_int("pppoe_lef") ? : 5,
151 nvram_get_int("debug_ppp") ? "debug\n" : "");
153 if (wan_proto != WP_L2TP) {
154 fprintf(fp,
155 "persist\n"
156 "holdoff %d\n",
157 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30));
160 switch (wan_proto) {
161 case WP_PPTP:
162 fprintf(fp,
163 "plugin pptp.so\n"
164 "pptp_server %s\n"
165 "nomppe-stateful\n"
166 "mtu %d\n",
167 nvram_safe_get("pptp_server_ip"),
168 nvram_get_int("mtu_enable") ? nvram_get_int("wan_mtu") : 1400);
169 break;
170 case WP_PPPOE:
171 fprintf(fp,
172 "password '%s'\n"
173 "plugin rp-pppoe.so\n"
174 "nomppe nomppc\n"
175 "nic-%s\n"
176 "mru %d mtu %d\n",
177 nvram_safe_get("ppp_passwd"),
178 nvram_safe_get("wan_ifname"),
179 nvram_get_int("wan_mtu"), nvram_get_int("wan_mtu"));
180 if (((p = nvram_get("ppp_service")) != NULL) && (*p)) {
181 fprintf(fp, "rp_pppoe_service '%s'\n", p);
183 if (((p = nvram_get("ppp_ac")) != NULL) && (*p)) {
184 fprintf(fp, "rp_pppoe_ac '%s'\n", p);
186 if (nvram_match("ppp_mlppp", "1")) {
187 fprintf(fp, "mp\n");
190 break;
191 #ifdef LINUX26
192 #ifdef TCONFIG_USB
193 case WP_PPP3G:
194 if ((cfp = fopen(ppp3g_chatfile, "w")) == NULL) {
195 perror(ppp3g_chatfile);
196 return -1;
198 fprintf(cfp,
199 "ABORT \"NO CARRIER\"\n"
200 "ABORT \"NO DIALTONE\"\n"
201 "ABORT \"NO ERROR\"\n"
202 "ABORT \"NO ANSWER\"\n"
203 "ABORT \"BUSY\"\n"
204 "REPORT CONNECT\n"
205 "\"\" \"AT\"\n");
206 /* moved to switch3g script
207 if (strlen(nvram_get("modem_pin")) >0 ) {
208 fprintf(cfp,
209 "TIMEOUT 60\n"
210 "OK \"AT+CPIN=%s\"\n"
211 "TIMEOUT 10\n",
212 nvram_get("modem_pin"));
215 fprintf(cfp,
216 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
217 "OK \"AT\"\n"
218 "OK \"ATS0=0\"\n"
219 "OK \"AT\"\n"
220 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
221 "OK \"AT\"\n"
222 "OK 'AT+CGDCONT=1,\"IP\",\"%s\"'\n"
223 "OK \"ATDT%s\"\n"
224 "CONNECT \c\n",
225 nvram_safe_get("modem_apn"),
226 nvram_safe_get("modem_init")
228 fclose(cfp);
231 if (nvram_match("usb_3g", "1")) {
232 // clear old gateway
233 if (strlen(nvram_get("wan_gateway")) >0 ) {
234 nvram_set("wan_gateway", "");
237 // detect 3G Modem
238 xstart("switch3g");
240 break;
241 #endif
242 #endif
243 case WP_L2TP:
244 fprintf(fp, "nomppe nomppc\n");
245 if (nvram_get_int("mtu_enable"))
246 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
247 break;
250 if (demand) {
251 // demand mode
252 fprintf(fp,
253 "demand\n" // Dial on demand
254 "idle %d\n"
255 "ipcp-accept-remote\n"
256 "ipcp-accept-local\n"
257 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
258 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
259 nvram_get_int("ppp_idletime") * 60);
262 #ifdef TCONFIG_IPV6
263 switch (get_ipv6_service()) {
264 case IPV6_NATIVE:
265 case IPV6_NATIVE_DHCP:
266 fprintf(fp, "+ipv6\n");
267 break;
269 #endif
270 // User specific options
271 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
273 fclose(fp);
274 make_secrets();
276 TRACE_PT("end\n");
277 return 0;
280 static void stop_ppp(void)
282 TRACE_PT("begin\n");
284 unlink(ppp_linkfile);
286 killall_tk("ip-up");
287 killall_tk("ip-down");
288 #ifdef TCONFIG_IPV6
289 killall_tk("ipv6-up");
290 killall_tk("ipv6-down");
291 #endif
292 killall_tk("xl2tpd");
293 killall_tk("pppd");
294 killall_tk("listen");
296 TRACE_PT("end\n");
299 static void run_pppd(void)
301 eval("pppd", "file", ppp_optfile);
303 if (nvram_get_int("ppp_demand")) {
304 // demand mode
306 Fixed issue id 7887(or 7787):
307 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
309 stop_dnsmasq();
310 dns_to_resolv();
311 start_dnsmasq();
313 // Trigger Connect On Demand if user ping pptp server
314 eval("listen", nvram_safe_get("lan_ifname"));
316 else {
317 // keepalive mode
318 start_redial();
322 // -----------------------------------------------------------------------------
324 inline void stop_pptp(void)
326 stop_ppp();
329 void start_pptp(int mode)
331 TRACE_PT("begin\n");
333 if (!using_dhcpc()) stop_dhcpc();
334 stop_pptp();
336 if (config_pppd(WP_PPTP, 0) != 0)
337 return;
339 run_pppd();
341 TRACE_PT("end\n");
344 // -----------------------------------------------------------------------------
346 void preset_wan(char *ifname, char *gw, char *netmask)
348 int i;
350 /* Delete all default routes */
351 route_del(ifname, 0, NULL, NULL, NULL);
353 /* try adding a route to gateway first */
354 route_add(ifname, 0, gw, NULL, "255.255.255.255");
356 /* Set default route to gateway if specified */
357 i = 5;
358 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
359 sleep(1);
361 _dprintf("set default gateway=%s n=%d\n", gw, i);
363 /* Add routes to dns servers as well for demand ppp to work */
364 char word[100], *next;
365 in_addr_t mask = inet_addr(netmask);
366 foreach(word, nvram_safe_get("wan_get_dns"), next) {
367 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
368 route_add(ifname, 0, word, gw, "255.255.255.255");
371 dns_to_resolv();
372 start_dnsmasq();
373 sleep(1);
374 start_firewall();
377 // -----------------------------------------------------------------------------
380 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
381 static void start_tmp_ppp(int num, char *ifname)
383 int timeout;
384 struct ifreq ifr;
385 int s;
387 TRACE_PT("begin: num=%d\n", num);
389 if (num != 0) return;
391 // Wait for ppp0 to be created
392 timeout = 15;
393 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
394 sleep(1);
395 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
398 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
399 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
401 // Set temporary IP address
402 timeout = 3;
403 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
404 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
405 sleep(1);
407 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
408 nvram_set("wan_netmask", "255.255.255.255");
410 // Set temporary P-t-P address
411 timeout = 3;
412 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
413 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
414 sleep(1);
416 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
418 close(s);
420 start_wan_done(ifname);
421 TRACE_PT("end\n");
424 void start_pppoe(int num)
426 char ifname[8];
428 TRACE_PT("begin pppoe_num=%d\n", num);
430 if (num != 0) return;
432 stop_pppoe();
434 snprintf(ifname, sizeof(ifname), "ppp%d", num);
436 #ifdef LINUX26
437 #ifdef TCONFIG_USB
438 if (nvram_match( "wan_proto", "ppp3g") ) {
439 if (config_pppd(WP_PPP3G, num) != 0)
440 return;
441 } else {
442 #endif
443 #endif
444 if (config_pppd(WP_PPPOE, num) != 0)
445 return;
446 #ifdef LINUX26
447 #ifdef TCONFIG_USB
449 #endif
450 #endif
451 run_pppd();
453 if (nvram_get_int("ppp_demand"))
454 start_tmp_ppp(num, ifname);
455 else
456 ifconfig(ifname, IFUP, NULL, NULL);
458 TRACE_PT("end\n");
461 void stop_pppoe(void)
463 stop_ppp();
468 #if 0
469 void stop_singe_pppoe(int num)
471 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
473 int i;
475 if (num != 0) return;
477 i = nvram_get_int("pppoe_pid0");
478 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
479 do {
480 sleep(2);
481 } while (kill(i, SIGKILL) == 0);
484 unlink(ppp_linkfile);
485 nvram_unset("pppoe_ifname0");
487 nvram_set("wan_get_dns", "");
488 clear_resolv();
490 #endif
492 // -----------------------------------------------------------------------------
494 inline void stop_l2tp(void)
496 stop_ppp();
499 void start_l2tp(void)
501 TRACE_PT("begin\n");
503 FILE *fp;
504 int demand;
506 stop_l2tp();
508 if (config_pppd(WP_L2TP, 0) != 0)
509 return;
511 demand = nvram_get_int("ppp_demand");
513 /* Generate XL2TPD configuration file */
514 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
515 return;
516 fprintf(fp,
517 "[global]\n"
518 "access control = no\n"
519 "port = 1701\n"
520 "[lac l2tp]\n"
521 "lns = %s\n"
522 "tx bps = 100000000\n"
523 "pppoptfile = %s\n"
524 "redial = yes\n"
525 "max redials = 32767\n"
526 "redial timeout = %d\n"
527 "ppp debug = %s\n",
528 nvram_safe_get("l2tp_server_ip"),
529 ppp_optfile,
530 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
531 nvram_get_int("debug_ppp") ? "yes" : "no");
532 fclose(fp);
534 enable_ip_forward();
536 eval("xl2tpd");
538 if (demand) {
539 eval("listen", nvram_safe_get("lan_ifname"));
541 else {
542 force_to_dial();
543 start_redial();
546 TRACE_PT("end\n");
549 // -----------------------------------------------------------------------------
551 char *wan_gateway(void)
553 char *gw = nvram_safe_get("wan_gateway_get");
554 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
555 gw = nvram_safe_get("wan_gateway");
556 return gw;
559 // -----------------------------------------------------------------------------
561 // trigger connect on demand
562 void force_to_dial(void)
564 TRACE_PT("begin\n");
566 sleep(1);
567 switch (get_wan_proto()) {
568 case WP_L2TP:
569 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
570 break;
571 case WP_PPTP:
572 eval("ping", "-c", "2", "10.112.112.112");
573 break;
574 case WP_DISABLED:
575 case WP_STATIC:
576 break;
577 default:
578 eval("ping", "-c", "2", wan_gateway());
579 break;
582 TRACE_PT("end\n");
585 // -----------------------------------------------------------------------------
587 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
589 char *routes, *tmp;
590 int bits;
591 struct in_addr mask;
592 char netmask[16];
594 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
595 tmp = routes = strdup(nvram_safe_get(nvname));
596 while (tmp && *tmp) {
597 char *ipaddr, *gateway, *nmask;
599 ipaddr = nmask = strsep(&tmp, " ");
600 strcpy(netmask, "255.255.255.255");
602 if (nmask) {
603 ipaddr = strsep(&nmask, "/");
604 if (nmask && *nmask) {
605 bits = strtol(nmask, &nmask, 10);
606 if (bits >= 1 && bits <= 32) {
607 mask.s_addr = htonl(0xffffffff << (32 - bits));
608 strcpy(netmask, inet_ntoa(mask));
612 gateway = strsep(&tmp, " ");
614 if (gateway && *gateway) {
615 if (add)
616 route_add(ifname, metric, ipaddr, gateway, netmask);
617 else
618 route_del(ifname, metric, ipaddr, gateway, netmask);
621 free(routes);
624 void do_wan_routes(char *ifname, int metric, int add)
626 if (nvram_get_int("dhcp_routes")) {
627 // Static Routes: IP ROUTER IP2 ROUTER2 ...
628 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
629 _do_wan_routes(ifname, "wan_routes1", metric, add);
630 _do_wan_routes(ifname, "wan_routes2", metric, add);
634 // -----------------------------------------------------------------------------
636 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
638 static int is_sta(int idx, int unit, int subunit, void *param)
640 char **p = param;
642 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
643 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
644 return 1;
646 return 0;
649 void start_wan(int mode)
651 int wan_proto;
652 char *wan_ifname;
653 char *p = NULL;
654 struct ifreq ifr;
655 int sd;
656 int max;
657 int mtu;
658 char buf[128];
659 int vid;
660 int vid_map;
661 int vlan0tag;
663 TRACE_PT("begin\n");
665 f_write(wan_connecting, NULL, 0, 0, 0);
669 if (!foreach_wif(1, &p, is_sta)) {
670 p = nvram_safe_get("wan_ifnameX");
671 /* vlan ID mapping */
672 if (sscanf(p, "vlan%d", &vid) == 1) {
673 vlan0tag = nvram_get_int("vlan0tag");
674 snprintf(buf, sizeof(buf), "vlan%dvid", vid);
675 vid_map = nvram_get_int(buf);
676 if ((vid_map < 1) || (vid_map > 4094)) vid_map = vlan0tag | vid;
677 snprintf(buf, sizeof(buf), "vlan%d", vid_map);
678 p = buf;
680 set_mac(p, "mac_wan", 1);
682 nvram_set("wan_ifname", p);
683 nvram_set("wan_ifnames", p);
687 wan_ifname = nvram_safe_get("wan_ifname");
688 if (wan_ifname[0] == 0) {
689 wan_ifname = "none";
690 nvram_set("wan_ifname", wan_ifname);
693 if (strcmp(wan_ifname, "none") == 0) {
694 nvram_set("wan_proto", "disabled");
695 syslog(LOG_INFO, "No WAN");
700 wan_proto = get_wan_proto();
702 // set the default gateway for WAN interface
703 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
705 if (wan_proto == WP_DISABLED) {
706 start_wan_done(wan_ifname);
707 return;
710 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
711 perror("socket");
712 return;
715 // MTU
717 switch (wan_proto) {
718 case WP_PPPOE:
719 case WP_PPP3G:
720 max = 1492;
721 break;
722 case WP_PPTP:
723 case WP_L2TP:
724 max = 1460;
725 break;
726 default:
727 max = 1500;
728 break;
730 if (nvram_match("mtu_enable", "0")) {
731 mtu = max;
733 else {
734 mtu = nvram_get_int("wan_mtu");
735 if (mtu > max) mtu = max;
736 else if (mtu < 576) mtu = 576;
738 sprintf(buf, "%d", mtu);
739 nvram_set("wan_mtu", buf);
740 nvram_set("wan_run_mtu", buf);
742 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
743 /* if (wan_proto == WP_PPTP) {
744 mtu += 40;
745 } */ // commented out; checkme -- zzz
747 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
748 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
749 ifr.ifr_mtu = mtu;
750 strcpy(ifr.ifr_name, wan_ifname);
751 ioctl(sd, SIOCSIFMTU, &ifr);
756 ifconfig(wan_ifname, IFUP, NULL, NULL);
758 start_firewall();
760 set_host_domain_name();
762 switch (wan_proto) {
763 case WP_PPPOE:
764 case WP_PPP3G:
765 start_pppoe(PPPOE0);
766 break;
767 case WP_DHCP:
768 case WP_L2TP:
769 case WP_PPTP:
770 if (using_dhcpc()) {
771 stop_dhcpc();
772 start_dhcpc();
774 else if (wan_proto != WP_DHCP) {
775 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
776 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
778 p = nvram_safe_get("wan_gateway");
779 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
780 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
782 switch (wan_proto) {
783 case WP_PPTP:
784 start_pptp(mode);
785 break;
786 case WP_L2TP:
787 start_l2tp();
788 break;
791 break;
792 default: // static
793 nvram_set("wan_iface", wan_ifname);
794 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
796 int r = 10;
797 while ((!check_wanup()) && (r-- > 0)) {
798 sleep(1);
801 start_wan_done(wan_ifname);
802 break;
805 // Get current WAN hardware address
806 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
807 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
808 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
811 /* Set initial QoS mode again now that WAN port is ready. */
812 set_et_qos_mode(sd);
814 close(sd);
816 enable_ip_forward();
818 led(LED_DIAG, 0); // for 4712, 5325E (?)
819 led(LED_DMZ, nvram_match("dmz_enable", "1"));
821 TRACE_PT("end\n");
824 #ifdef TCONFIG_IPV6
825 void start_wan6_done(const char *wan_ifname)
827 struct in_addr addr4;
828 struct in6_addr addr;
829 static char addr6[INET6_ADDRSTRLEN];
831 int service = get_ipv6_service();
833 if (service != IPV6_DISABLED) {
834 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
835 accept_ra(wan_ifname);
838 switch (service) {
839 case IPV6_NATIVE:
840 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
841 break;
842 case IPV6_NATIVE_DHCP:
843 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
844 stop_dhcp6c();
845 start_dhcp6c();
846 break;
847 case IPV6_ANYCAST_6TO4:
848 case IPV6_6IN4:
849 stop_ipv6_tunnel();
850 if (service == IPV6_ANYCAST_6TO4) {
851 addr4.s_addr = 0;
852 memset(&addr, 0, sizeof(addr));
853 inet_aton(get_wanip(), &addr4);
854 addr.s6_addr16[0] = htons(0x2002);
855 ipv6_mapaddr4(&addr, 16, &addr4, 0);
856 addr.s6_addr16[3] = htons(0x0001);
857 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
858 nvram_set("ipv6_prefix", addr6);
860 start_ipv6_tunnel();
861 // FIXME: give it a few seconds for DAD completion
862 sleep(2);
863 break;
866 #endif
868 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
869 // wan_ifname: vlan1
870 // wan_iface: ppp# (PPPOE, PPP3G, PPTP, L2TP), vlan1 (DHCP, HB, Static)
872 void start_wan_done(char *wan_ifname)
874 int proto;
875 int n;
876 char *gw;
877 struct sysinfo si;
878 int wanup;
880 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
882 sysinfo(&si);
883 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
885 proto = get_wan_proto();
887 // delete all default routes
888 route_del(wan_ifname, 0, NULL, NULL, NULL);
890 if (proto != WP_DISABLED) {
891 // set default route to gateway if specified
892 gw = wan_gateway();
893 #if 0
894 if (proto == WP_PPTP && !using_dhcpc()) {
895 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
896 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
898 #endif
899 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
900 if (proto == WP_DHCP || proto == WP_STATIC) {
901 // possibly gateway is over the bridge, try adding a route to gateway first
902 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
905 n = 5;
906 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
907 sleep(1);
909 _dprintf("set default gateway=%s n=%d\n", gw, n);
911 // hack: avoid routing cycles, when both peer and server have the same IP
912 if (proto == WP_PPTP || proto == WP_L2TP) {
913 // delete gateway route as it's no longer needed
914 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
918 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
920 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
921 if (proto == WP_STATIC)
923 int ifindex;
924 u_int32_t wan_ip;
925 unsigned char wan_mac[6];
927 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
928 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
931 #endif
933 if (proto == WP_PPTP || proto == WP_L2TP) {
934 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
935 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
937 if (proto == WP_L2TP) {
938 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
942 dns_to_resolv();
943 start_dnsmasq();
945 start_firewall();
946 start_qos();
948 do_static_routes(1);
949 // and routes supplied via DHCP
950 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
952 stop_zebra();
953 start_zebra();
955 wanup = check_wanup();
957 if ((wanup) || (time(0) < Y2K)) {
958 stop_ntpc();
959 start_ntpc();
962 if ((wanup) || (proto == WP_DISABLED)) {
963 stop_ddns();
964 start_ddns();
965 stop_igmp_proxy();
966 stop_udpxy();
967 start_igmp_proxy();
968 start_udpxy();
971 #ifdef TCONFIG_IPV6
972 start_wan6_done(get_wan6face());
973 #endif
975 stop_upnp();
976 start_upnp();
978 // restart httpd
979 start_httpd();
981 if (wanup) {
982 SET_LED(GOT_IP);
983 notice_set("wan", "");
985 run_nvscript("script_wanup", NULL, 0);
988 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
989 if (check_hw_type() == HW_BCM4702) {
990 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
991 if (nvram_match("lan_stp", "1"))
992 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
993 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
994 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
995 if (nvram_match("lan1_stp", "1"))
996 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
998 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
999 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
1000 if (nvram_match("lan2_stp", "1"))
1001 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
1003 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
1004 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
1005 if (nvram_match("lan3_stp", "1"))
1006 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
1010 if (wanup)
1011 start_vpn_eas();
1013 #ifdef TCONFIG_USERPPTP
1014 if (wanup && nvram_get_int("pptp_client_enable"))
1015 start_pptp_client();
1016 #endif
1018 unlink(wan_connecting);
1020 new_qoslimit_start(); //!! RAF
1022 TRACE_PT("end\n");
1025 void stop_wan(void)
1027 char name[80];
1028 char *next;
1030 TRACE_PT("begin\n");
1032 #ifdef TCONFIG_USERPPTP
1033 stop_pptp_client();
1034 stop_dnsmasq();
1035 dns_to_resolv();
1036 start_dnsmasq();
1037 #endif
1039 new_qoslimit_stop(); //!! RAF
1041 stop_qos();
1042 stop_upnp(); //!!TB - moved from stop_services()
1043 stop_firewall();
1044 stop_igmp_proxy();
1045 stop_udpxy();
1046 stop_ntpc();
1048 #ifdef TCONFIG_IPV6
1049 stop_ipv6_tunnel();
1050 stop_dhcp6c();
1051 nvram_set("ipv6_get_dns", "");
1052 #endif
1054 /* Kill any WAN client daemons or callbacks */
1055 stop_redial();
1056 stop_pppoe();
1057 stop_ppp();
1058 stop_dhcpc();
1059 if (nvram_get_int("pptp_client_enable"))
1060 stop_pptp_client();
1061 clear_resolv();
1062 nvram_set("wan_get_dns", "");
1064 /* Bring down WAN interfaces */
1065 foreach(name, nvram_safe_get("wan_ifnames"), next)
1066 ifconfig(name, 0, "0.0.0.0", NULL);
1068 SET_LED(RELEASE_IP);
1069 //notice_set("wan", "");
1070 unlink("/var/notice/wan");
1071 unlink(wan_connecting);
1073 TRACE_PT("end\n");