Merge branch 'Teaman-ND' into Teaman-RT
[tomato.git] / release / src / router / rc / wan.c
blob4934d741400943650a3231c543196d54a0cd617a
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");
189 break;
190 #ifdef LINUX26
191 #ifdef TCONFIG_USB
192 case WP_PPP3G:
193 if ((cfp = fopen(ppp3g_chatfile, "w")) == NULL) {
194 perror(ppp3g_chatfile);
195 return -1;
197 fprintf(cfp,
198 "ABORT \"NO CARRIER\"\n"
199 "ABORT \"NO DIALTONE\"\n"
200 "ABORT \"NO ERROR\"\n"
201 "ABORT \"NO ANSWER\"\n"
202 "ABORT \"BUSY\"\n"
203 "REPORT CONNECT\n"
204 "\"\" \"AT\"\n");
205 /* moved to switch3g script
206 if (strlen(nvram_get("modem_pin")) >0 ) {
207 fprintf(cfp,
208 "TIMEOUT 60\n"
209 "OK \"AT+CPIN=%s\"\n"
210 "TIMEOUT 10\n",
211 nvram_get("modem_pin"));
214 fprintf(cfp,
215 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
216 "OK \"AT\"\n"
217 "OK \"ATS0=0\"\n"
218 "OK \"AT\"\n"
219 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
220 "OK \"AT\"\n"
221 "OK 'AT+CGDCONT=1,\"IP\",\"%s\"'\n"
222 "OK \"ATDT%s\"\n"
223 "CONNECT \c\n",
224 nvram_safe_get("modem_apn"),
225 nvram_safe_get("modem_init")
227 fclose(cfp);
230 if (nvram_match("usb_3g", "1")) {
231 // clear old gateway
232 if (strlen(nvram_get("wan_gateway")) >0 ) {
233 nvram_set("wan_gateway", "");
236 // detect 3G Modem
237 xstart("switch3g");
239 break;
240 #endif
241 #endif
242 case WP_L2TP:
243 fprintf(fp, "nomppe nomppc\n");
244 if (nvram_get_int("mtu_enable"))
245 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
246 break;
249 if (demand) {
250 // demand mode
251 fprintf(fp,
252 "demand\n" // Dial on demand
253 "idle %d\n"
254 "ipcp-accept-remote\n"
255 "ipcp-accept-local\n"
256 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
257 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
258 nvram_get_int("ppp_idletime") * 60);
261 #ifdef TCONFIG_IPV6
262 switch (get_ipv6_service()) {
263 case IPV6_NATIVE:
264 case IPV6_NATIVE_DHCP:
265 fprintf(fp, "+ipv6\n");
266 break;
268 #endif
269 // User specific options
270 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
272 fclose(fp);
273 make_secrets();
275 TRACE_PT("end\n");
276 return 0;
279 static void stop_ppp(void)
281 TRACE_PT("begin\n");
283 unlink(ppp_linkfile);
285 killall_tk("ip-up");
286 killall_tk("ip-down");
287 #ifdef TCONFIG_IPV6
288 killall_tk("ipv6-up");
289 killall_tk("ipv6-down");
290 #endif
291 killall_tk("xl2tpd");
292 killall_tk("pppd");
293 killall_tk("listen");
295 TRACE_PT("end\n");
298 static void run_pppd(void)
300 eval("pppd", "file", ppp_optfile);
302 if (nvram_get_int("ppp_demand")) {
303 // demand mode
305 Fixed issue id 7887(or 7787):
306 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
308 stop_dnsmasq();
309 dns_to_resolv();
310 start_dnsmasq();
312 // Trigger Connect On Demand if user ping pptp server
313 eval("listen", nvram_safe_get("lan_ifname"));
315 else {
316 // keepalive mode
317 start_redial();
321 // -----------------------------------------------------------------------------
323 inline void stop_pptp(void)
325 stop_ppp();
328 void start_pptp(int mode)
330 TRACE_PT("begin\n");
332 if (!using_dhcpc()) stop_dhcpc();
333 stop_pptp();
335 if (config_pppd(WP_PPTP, 0) != 0)
336 return;
338 run_pppd();
340 TRACE_PT("end\n");
343 // -----------------------------------------------------------------------------
345 void preset_wan(char *ifname, char *gw, char *netmask)
347 int i;
349 /* Delete all default routes */
350 route_del(ifname, 0, NULL, NULL, NULL);
352 /* try adding a route to gateway first */
353 route_add(ifname, 0, gw, NULL, "255.255.255.255");
355 /* Set default route to gateway if specified */
356 i = 5;
357 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
358 sleep(1);
360 _dprintf("set default gateway=%s n=%d\n", gw, i);
362 /* Add routes to dns servers as well for demand ppp to work */
363 char word[100], *next;
364 in_addr_t mask = inet_addr(netmask);
365 foreach(word, nvram_safe_get("wan_get_dns"), next) {
366 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
367 route_add(ifname, 0, word, gw, "255.255.255.255");
370 dns_to_resolv();
371 start_dnsmasq();
372 sleep(1);
373 start_firewall();
376 // -----------------------------------------------------------------------------
379 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
380 static void start_tmp_ppp(int num, char *ifname)
382 int timeout;
383 struct ifreq ifr;
384 int s;
386 TRACE_PT("begin: num=%d\n", num);
388 if (num != 0) return;
390 // Wait for ppp0 to be created
391 timeout = 15;
392 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
393 sleep(1);
394 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
397 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
398 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
400 // Set temporary IP address
401 timeout = 3;
402 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
403 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
404 sleep(1);
406 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
407 nvram_set("wan_netmask", "255.255.255.255");
409 // Set temporary P-t-P address
410 timeout = 3;
411 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
412 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
413 sleep(1);
415 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
417 close(s);
419 start_wan_done(ifname);
420 TRACE_PT("end\n");
423 void start_pppoe(int num)
425 char ifname[8];
427 TRACE_PT("begin pppoe_num=%d\n", num);
429 if (num != 0) return;
431 stop_pppoe();
433 snprintf(ifname, sizeof(ifname), "ppp%d", num);
435 #ifdef LINUX26
436 #ifdef TCONFIG_USB
437 if (nvram_match( "wan_proto", "ppp3g") ) {
438 if (config_pppd(WP_PPP3G, num) != 0)
439 return;
440 } else {
441 #endif
442 #endif
443 if (config_pppd(WP_PPPOE, num) != 0)
444 return;
445 #ifdef LINUX26
446 #ifdef TCONFIG_USB
448 #endif
449 #endif
450 run_pppd();
452 if (nvram_get_int("ppp_demand"))
453 start_tmp_ppp(num, ifname);
454 else
455 ifconfig(ifname, IFUP, NULL, NULL);
457 TRACE_PT("end\n");
460 void stop_pppoe(void)
462 stop_ppp();
467 #if 0
468 void stop_singe_pppoe(int num)
470 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
472 int i;
474 if (num != 0) return;
476 i = nvram_get_int("pppoe_pid0");
477 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
478 do {
479 sleep(2);
480 } while (kill(i, SIGKILL) == 0);
483 unlink(ppp_linkfile);
484 nvram_unset("pppoe_ifname0");
486 nvram_set("wan_get_dns", "");
487 clear_resolv();
489 #endif
491 // -----------------------------------------------------------------------------
493 inline void stop_l2tp(void)
495 stop_ppp();
498 void start_l2tp(void)
500 TRACE_PT("begin\n");
502 FILE *fp;
503 int demand;
505 stop_l2tp();
507 if (config_pppd(WP_L2TP, 0) != 0)
508 return;
510 demand = nvram_get_int("ppp_demand");
512 /* Generate XL2TPD configuration file */
513 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
514 return;
515 fprintf(fp,
516 "[global]\n"
517 "access control = no\n"
518 "port = 1701\n"
519 "[lac l2tp]\n"
520 "lns = %s\n"
521 "tx bps = 100000000\n"
522 "pppoptfile = %s\n"
523 "redial = yes\n"
524 "max redials = 32767\n"
525 "redial timeout = %d\n"
526 "tunnel rws = 8\n"
527 "ppp debug = %s\n"
528 "%s\n",
529 nvram_safe_get("l2tp_server_ip"),
530 ppp_optfile,
531 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
532 nvram_get_int("debug_ppp") ? "yes" : "no",
533 nvram_safe_get("xl2tpd_custom"));
534 fappend(fp, "/etc/xl2tpd.custom");
535 fclose(fp);
537 enable_ip_forward();
539 eval("xl2tpd");
541 if (demand) {
542 eval("listen", nvram_safe_get("lan_ifname"));
544 else {
545 force_to_dial();
546 start_redial();
549 TRACE_PT("end\n");
552 // -----------------------------------------------------------------------------
554 char *wan_gateway(void)
556 char *gw = nvram_safe_get("wan_gateway_get");
557 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
558 gw = nvram_safe_get("wan_gateway");
559 return gw;
562 // -----------------------------------------------------------------------------
564 // trigger connect on demand
565 void force_to_dial(void)
567 TRACE_PT("begin\n");
569 sleep(1);
570 switch (get_wan_proto()) {
571 case WP_L2TP:
572 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
573 break;
574 case WP_PPTP:
575 eval("ping", "-c", "2", "10.112.112.112");
576 break;
577 case WP_DISABLED:
578 case WP_STATIC:
579 break;
580 default:
581 eval("ping", "-c", "2", wan_gateway());
582 break;
585 TRACE_PT("end\n");
588 // -----------------------------------------------------------------------------
590 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
592 char *routes, *tmp;
593 int bits;
594 struct in_addr mask;
595 char netmask[16];
597 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
598 tmp = routes = strdup(nvram_safe_get(nvname));
599 while (tmp && *tmp) {
600 char *ipaddr, *gateway, *nmask;
602 ipaddr = nmask = strsep(&tmp, " ");
603 strcpy(netmask, "255.255.255.255");
605 if (nmask) {
606 ipaddr = strsep(&nmask, "/");
607 if (nmask && *nmask) {
608 bits = strtol(nmask, &nmask, 10);
609 if (bits >= 1 && bits <= 32) {
610 mask.s_addr = htonl(0xffffffff << (32 - bits));
611 strcpy(netmask, inet_ntoa(mask));
615 gateway = strsep(&tmp, " ");
617 if (gateway && *gateway) {
618 if (add)
619 route_add(ifname, metric, ipaddr, gateway, netmask);
620 else
621 route_del(ifname, metric, ipaddr, gateway, netmask);
624 free(routes);
627 void do_wan_routes(char *ifname, int metric, int add)
629 if (nvram_get_int("dhcp_routes")) {
630 // Static Routes: IP ROUTER IP2 ROUTER2 ...
631 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
632 _do_wan_routes(ifname, "wan_routes1", metric, add);
633 _do_wan_routes(ifname, "wan_routes2", metric, add);
637 // -----------------------------------------------------------------------------
639 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
641 static int is_sta(int idx, int unit, int subunit, void *param)
643 char **p = param;
645 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
646 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
647 return 1;
649 return 0;
652 void start_wan(int mode)
654 int wan_proto;
655 char *wan_ifname;
656 char *p = NULL;
657 struct ifreq ifr;
658 int sd;
659 int max;
660 int mtu;
661 char buf[128];
662 int vid;
663 int vid_map;
664 int vlan0tag;
666 TRACE_PT("begin\n");
668 f_write(wan_connecting, NULL, 0, 0, 0);
672 if (!foreach_wif(1, &p, is_sta)) {
673 p = nvram_safe_get("wan_ifnameX");
674 /* vlan ID mapping */
675 if (sscanf(p, "vlan%d", &vid) == 1) {
676 vlan0tag = nvram_get_int("vlan0tag");
677 snprintf(buf, sizeof(buf), "vlan%dvid", vid);
678 vid_map = nvram_get_int(buf);
679 if ((vid_map < 1) || (vid_map > 4094)) vid_map = vlan0tag | vid;
680 snprintf(buf, sizeof(buf), "vlan%d", vid_map);
681 p = buf;
683 set_mac(p, "mac_wan", 1);
685 nvram_set("wan_ifname", p);
686 nvram_set("wan_ifnames", p);
690 wan_ifname = nvram_safe_get("wan_ifname");
691 if (wan_ifname[0] == 0) {
692 wan_ifname = "none";
693 nvram_set("wan_ifname", wan_ifname);
696 if (strcmp(wan_ifname, "none") == 0) {
697 nvram_set("wan_proto", "disabled");
698 syslog(LOG_INFO, "No WAN");
703 wan_proto = get_wan_proto();
705 // set the default gateway for WAN interface
706 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
708 if (wan_proto == WP_DISABLED) {
709 start_wan_done(wan_ifname);
710 return;
713 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
714 perror("socket");
715 return;
718 // MTU
720 switch (wan_proto) {
721 case WP_PPPOE:
722 case WP_PPP3G:
723 max = 1492;
724 break;
725 case WP_PPTP:
726 case WP_L2TP:
727 max = 1460;
728 break;
729 default:
730 max = 1500;
731 break;
733 if (nvram_match("mtu_enable", "0")) {
734 mtu = max;
736 else {
737 mtu = nvram_get_int("wan_mtu");
738 if (mtu > max) mtu = max;
739 else if (mtu < 576) mtu = 576;
741 sprintf(buf, "%d", mtu);
742 nvram_set("wan_mtu", buf);
743 nvram_set("wan_run_mtu", buf);
745 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
746 /* if (wan_proto == WP_PPTP) {
747 mtu += 40;
748 } */ // commented out; checkme -- zzz
750 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
751 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
752 ifr.ifr_mtu = mtu;
753 strcpy(ifr.ifr_name, wan_ifname);
754 ioctl(sd, SIOCSIFMTU, &ifr);
759 ifconfig(wan_ifname, IFUP, NULL, NULL);
761 start_firewall();
763 set_host_domain_name();
765 switch (wan_proto) {
766 case WP_PPPOE:
767 case WP_PPP3G:
768 start_pppoe(PPPOE0);
769 break;
770 case WP_DHCP:
771 case WP_L2TP:
772 case WP_PPTP:
773 if (using_dhcpc()) {
774 stop_dhcpc();
775 start_dhcpc();
777 else if (wan_proto != WP_DHCP) {
778 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
779 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
781 p = nvram_safe_get("wan_gateway");
782 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
783 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
785 switch (wan_proto) {
786 case WP_PPTP:
787 start_pptp(mode);
788 break;
789 case WP_L2TP:
790 start_l2tp();
791 break;
794 break;
795 default: // static
796 nvram_set("wan_iface", wan_ifname);
797 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
799 int r = 10;
800 while ((!check_wanup()) && (r-- > 0)) {
801 sleep(1);
804 start_wan_done(wan_ifname);
805 break;
808 // Get current WAN hardware address
809 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
810 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
811 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
814 /* Set initial QoS mode again now that WAN port is ready. */
815 set_et_qos_mode(sd);
817 close(sd);
819 enable_ip_forward();
821 led(LED_DIAG, 0); // for 4712, 5325E (?)
822 led(LED_DMZ, nvram_match("dmz_enable", "1"));
824 TRACE_PT("end\n");
827 #ifdef TCONFIG_IPV6
828 void start_wan6_done(const char *wan_ifname)
830 struct in_addr addr4;
831 struct in6_addr addr;
832 static char addr6[INET6_ADDRSTRLEN];
834 int service = get_ipv6_service();
836 if (service != IPV6_DISABLED) {
837 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
838 accept_ra(wan_ifname);
841 switch (service) {
842 case IPV6_NATIVE:
843 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
844 break;
845 case IPV6_NATIVE_DHCP:
846 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
847 stop_dhcp6c();
848 start_dhcp6c();
849 break;
850 case IPV6_ANYCAST_6TO4:
851 case IPV6_6IN4:
852 stop_ipv6_tunnel();
853 if (service == IPV6_ANYCAST_6TO4) {
854 addr4.s_addr = 0;
855 memset(&addr, 0, sizeof(addr));
856 inet_aton(get_wanip(), &addr4);
857 addr.s6_addr16[0] = htons(0x2002);
858 ipv6_mapaddr4(&addr, 16, &addr4, 0);
859 addr.s6_addr16[3] = htons(0x0001);
860 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
861 nvram_set("ipv6_prefix", addr6);
863 start_ipv6_tunnel();
864 // FIXME: give it a few seconds for DAD completion
865 sleep(2);
866 break;
869 #endif
871 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
872 // wan_ifname: vlan1
873 // wan_iface: ppp# (PPPOE, PPP3G, PPTP, L2TP), vlan1 (DHCP, HB, Static)
875 void start_wan_done(char *wan_ifname)
877 int proto;
878 int n;
879 char *gw;
880 struct sysinfo si;
881 int wanup;
883 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
885 sysinfo(&si);
886 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
888 proto = get_wan_proto();
890 // delete all default routes
891 route_del(wan_ifname, 0, NULL, NULL, NULL);
893 if (proto != WP_DISABLED) {
894 // set default route to gateway if specified
895 gw = wan_gateway();
896 #if 0
897 if (proto == WP_PPTP && !using_dhcpc()) {
898 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
899 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
901 #endif
902 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
903 if (proto == WP_DHCP || proto == WP_STATIC) {
904 // possibly gateway is over the bridge, try adding a route to gateway first
905 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
908 n = 5;
909 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
910 sleep(1);
912 _dprintf("set default gateway=%s n=%d\n", gw, n);
914 // hack: avoid routing cycles, when both peer and server have the same IP
915 if (proto == WP_PPTP || proto == WP_L2TP) {
916 // delete gateway route as it's no longer needed
917 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
921 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
923 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
924 if (proto == WP_STATIC)
926 int ifindex;
927 u_int32_t wan_ip;
928 unsigned char wan_mac[6];
930 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
931 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
934 #endif
936 if (proto == WP_PPTP || proto == WP_L2TP) {
937 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
938 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
940 if (proto == WP_L2TP) {
941 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
945 dns_to_resolv();
946 start_dnsmasq();
947 start_firewall();
948 start_qos();
949 start_qoslimit();
950 start_arpbind();
953 do_static_routes(1);
954 // and routes supplied via DHCP
955 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
957 stop_zebra();
958 start_zebra();
960 wanup = check_wanup();
962 if ((wanup) || (time(0) < Y2K)) {
963 stop_ntpc();
964 start_ntpc();
967 if ((wanup) || (proto == WP_DISABLED)) {
968 stop_ddns();
969 start_ddns();
970 stop_igmp_proxy();
971 stop_udpxy();
972 start_igmp_proxy();
973 start_udpxy();
976 #ifdef TCONFIG_IPV6
977 start_wan6_done(get_wan6face());
978 #endif
980 stop_upnp();
981 start_upnp();
983 // restart httpd
984 start_httpd();
986 if (wanup) {
987 SET_LED(GOT_IP);
988 notice_set("wan", "");
990 run_nvscript("script_wanup", NULL, 0);
993 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
994 if (check_hw_type() == HW_BCM4702) {
995 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
996 if (nvram_match("lan_stp", "1"))
997 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
998 #ifdef TCONFIG_VLAN
999 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
1000 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
1001 if (nvram_match("lan1_stp", "1"))
1002 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
1004 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
1005 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
1006 if (nvram_match("lan2_stp", "1"))
1007 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
1009 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
1010 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
1011 if (nvram_match("lan3_stp", "1"))
1012 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
1014 #endif
1017 if (wanup)
1018 start_vpn_eas();
1020 #ifdef TCONFIG_USERPPTP
1021 if (wanup && nvram_get_int("pptp_client_enable"))
1022 start_pptp_client();
1023 #endif
1025 unlink(wan_connecting);
1027 TRACE_PT("end\n");
1030 void stop_wan(void)
1032 char name[80];
1033 char *next;
1035 TRACE_PT("begin\n");
1037 #ifdef TCONFIG_USERPPTP
1038 stop_pptp_client();
1039 stop_dnsmasq();
1040 dns_to_resolv();
1041 start_dnsmasq();
1042 #endif
1043 stop_vpn_eas();
1044 stop_arpbind();
1045 stop_qoslimit();
1046 stop_qos();
1047 stop_upnp(); //!!TB - moved from stop_services()
1048 stop_firewall();
1049 stop_igmp_proxy();
1050 stop_udpxy();
1051 stop_ntpc();
1053 #ifdef TCONFIG_IPV6
1054 stop_ipv6_tunnel();
1055 stop_dhcp6c();
1056 nvram_set("ipv6_get_dns", "");
1057 #endif
1059 /* Kill any WAN client daemons or callbacks */
1060 stop_redial();
1061 stop_pppoe();
1062 stop_ppp();
1063 stop_dhcpc();
1064 clear_resolv();
1065 nvram_set("wan_get_dns", "");
1067 /* Bring down WAN interfaces */
1068 foreach(name, nvram_safe_get("wan_ifnames"), next)
1069 ifconfig(name, 0, "0.0.0.0", NULL);
1071 SET_LED(RELEASE_IP);
1072 //notice_set("wan", "");
1073 unlink("/var/notice/wan");
1074 unlink(wan_connecting);
1076 TRACE_PT("end\n");