ipv6: Other ISP Configuration button
[tomato.git] / release / src / router / rc / wan.c
blob834a9de36458723f03e87e0ebfdbb0506ce7b241
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 "novj\n" // Disable Van Jacobson style TCP/IP header compression
139 "nobsdcomp\n" // Disable BSD-Compress compression
140 "nodeflate\n" // Disable Deflate compression
141 "noauth\n" // Do not authenticate peer
142 "refuse-eap\n" // Do not use eap
143 "maxfail 0\n" // Never give up
144 "lcp-echo-interval %d\n"// Interval between LCP echo-requests
145 "lcp-echo-failure %d\n" // Tolerance to unanswered echo-requests
146 "%s", // Debug
147 nvram_get_int("pppoe_lei") ? : 10,
148 nvram_get_int("pppoe_lef") ? : 5,
149 nvram_get_int("debug_ppp") ? "debug\n" : "");
151 #ifdef LINUX26
152 #ifdef TCONFIG_USB
153 if (nvram_match("wan_proto", "ppp3g") && nvram_match("modem_dev", "ttyACM0") ) {
154 //don`t write nopcomp and noaccomp options
155 } else {
156 #endif
157 #endif
158 fprintf(fp,
159 "nopcomp\n" // Disable protocol field compression
160 "noaccomp\n" // Disable Address/Control compression
162 #ifdef LINUX26
163 #ifdef TCONFIG_USB
165 #endif
166 #endif
169 if (wan_proto != WP_L2TP) {
170 fprintf(fp,
171 "persist\n"
172 "holdoff %d\n",
173 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30));
176 switch (wan_proto) {
177 case WP_PPTP:
178 fprintf(fp,
179 "plugin pptp.so\n"
180 "pptp_server %s\n"
181 "nomppe-stateful\n"
182 "mtu %d\n",
183 nvram_safe_get("pptp_server_ip"),
184 nvram_get_int("mtu_enable") ? nvram_get_int("wan_mtu") : 1400);
185 break;
186 case WP_PPPOE:
187 fprintf(fp,
188 "password '%s'\n"
189 "plugin rp-pppoe.so\n"
190 "nomppe nomppc\n"
191 "nic-%s\n"
192 "mru %d mtu %d\n",
193 nvram_safe_get("ppp_passwd"),
194 nvram_safe_get("wan_ifname"),
195 nvram_get_int("wan_mtu"), nvram_get_int("wan_mtu"));
196 if (((p = nvram_get("ppp_service")) != NULL) && (*p)) {
197 fprintf(fp, "rp_pppoe_service '%s'\n", p);
199 if (((p = nvram_get("ppp_ac")) != NULL) && (*p)) {
200 fprintf(fp, "rp_pppoe_ac '%s'\n", p);
202 if (nvram_match("ppp_mlppp", "1")) {
203 fprintf(fp, "mp\n");
206 break;
207 #ifdef LINUX26
208 #ifdef TCONFIG_USB
209 case WP_PPP3G:
210 if ((cfp = fopen(ppp3g_chatfile, "w")) == NULL) {
211 perror(ppp3g_chatfile);
212 return -1;
214 fprintf(cfp,
215 "ABORT \"NO CARRIER\"\n"
216 "ABORT \"NO DIALTONE\"\n"
217 "ABORT \"NO ERROR\"\n"
218 "ABORT \"NO ANSWER\"\n"
219 "ABORT \"BUSY\"\n"
220 "REPORT CONNECT\n"
221 "\"\" \"AT\"\n");
222 /* moved to switch3g script
223 if (strlen(nvram_get("modem_pin")) >0 ) {
224 fprintf(cfp,
225 "TIMEOUT 60\n"
226 "OK \"AT+CPIN=%s\"\n"
227 "TIMEOUT 10\n",
228 nvram_get("modem_pin"));
231 fprintf(cfp,
232 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
233 "OK \"AT\"\n"
234 "OK \"ATS0=0\"\n"
235 "OK \"AT\"\n"
236 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
237 "OK \"AT\"\n"
238 "OK 'AT+CGDCONT=1,\"IP\",\"%s\"'\n"
239 "OK \"ATDT%s\"\n"
240 "CONNECT \\c\n",
241 nvram_safe_get("modem_apn"),
242 nvram_safe_get("modem_init")
244 fclose(cfp);
247 if (nvram_match("usb_3g", "1") && nvram_match("wan_proto", "ppp3g")) {
248 // clear old gateway
249 if (strlen(nvram_get("wan_gateway")) >0 ) {
250 nvram_set("wan_gateway", "");
253 // detect 3G Modem
254 xstart("switch3g");
256 break;
257 #endif
258 #endif
259 case WP_L2TP:
260 fprintf(fp, "nomppe nomppc\n");
261 if (nvram_get_int("mtu_enable"))
262 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
263 break;
266 if (demand) {
267 // demand mode
268 fprintf(fp,
269 "demand\n" // Dial on demand
270 "idle %d\n"
271 "ipcp-accept-remote\n"
272 "ipcp-accept-local\n"
273 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
274 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
275 nvram_get_int("ppp_idletime") * 60);
278 #ifdef TCONFIG_IPV6
279 switch (get_ipv6_service()) {
280 case IPV6_NATIVE:
281 case IPV6_NATIVE_DHCP:
282 fprintf(fp, "+ipv6\n");
283 break;
285 #endif
286 // User specific options
287 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
289 fclose(fp);
290 make_secrets();
292 TRACE_PT("end\n");
293 return 0;
296 static void stop_ppp(void)
298 TRACE_PT("begin\n");
300 unlink(ppp_linkfile);
302 killall_tk("ip-up");
303 killall_tk("ip-down");
304 #ifdef TCONFIG_IPV6
305 killall_tk("ipv6-up");
306 killall_tk("ipv6-down");
307 #endif
308 killall_tk("xl2tpd");
309 killall_tk("pppd");
310 killall_tk("listen");
312 TRACE_PT("end\n");
315 static void run_pppd(void)
318 eval("pppd", "file", (char *)ppp_optfile);
320 if (nvram_get_int("ppp_demand")) {
321 // demand mode
323 Fixed issue id 7887(or 7787):
324 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
326 stop_dnsmasq();
327 dns_to_resolv();
328 start_dnsmasq();
330 // Trigger Connect On Demand if user ping pptp server
331 eval("listen", nvram_safe_get("lan_ifname"));
333 else {
334 // keepalive mode
335 start_redial();
339 // -----------------------------------------------------------------------------
341 inline void stop_pptp(void)
343 stop_ppp();
346 void start_pptp(int mode)
348 TRACE_PT("begin\n");
350 if (!using_dhcpc()) stop_dhcpc();
351 stop_pptp();
353 if (config_pppd(WP_PPTP, 0) != 0)
354 return;
356 run_pppd();
358 TRACE_PT("end\n");
361 // -----------------------------------------------------------------------------
363 void preset_wan(char *ifname, char *gw, char *netmask)
365 int i;
367 /* Delete all default routes */
368 route_del(ifname, 0, NULL, NULL, NULL);
370 /* try adding a route to gateway first */
371 route_add(ifname, 0, gw, NULL, "255.255.255.255");
373 /* Set default route to gateway if specified */
374 i = 5;
375 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
376 sleep(1);
378 _dprintf("set default gateway=%s n=%d\n", gw, i);
380 /* Add routes to dns servers as well for demand ppp to work */
381 char word[100], *next;
382 in_addr_t mask = inet_addr(netmask);
383 foreach(word, nvram_safe_get("wan_get_dns"), next) {
384 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
385 route_add(ifname, 0, word, gw, "255.255.255.255");
388 dns_to_resolv();
389 start_dnsmasq();
390 sleep(1);
391 start_firewall();
394 // -----------------------------------------------------------------------------
397 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
398 static void start_tmp_ppp(int num, char *ifname)
400 int timeout;
401 struct ifreq ifr;
402 int s;
404 TRACE_PT("begin: num=%d\n", num);
406 if (num != 0) return;
408 // Wait for ppp0 to be created
409 timeout = 15;
410 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
411 sleep(1);
412 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
415 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
416 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
418 // Set temporary IP address
419 timeout = 3;
420 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
421 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
422 sleep(1);
424 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
425 nvram_set("wan_netmask", "255.255.255.255");
427 // Set temporary P-t-P address
428 timeout = 3;
429 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
430 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
431 sleep(1);
433 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
435 close(s);
437 start_wan_done(ifname);
438 TRACE_PT("end\n");
441 void start_pppoe(int num)
443 char ifname[8];
445 TRACE_PT("begin pppoe_num=%d\n", num);
447 if (num != 0) return;
449 stop_pppoe();
451 snprintf(ifname, sizeof(ifname), "ppp%d", num);
453 #ifdef LINUX26
454 #ifdef TCONFIG_USB
455 if (nvram_match( "wan_proto", "ppp3g") ) {
456 if (config_pppd(WP_PPP3G, num) != 0)
457 return;
458 } else {
459 #endif
460 #endif
461 if (config_pppd(WP_PPPOE, num) != 0)
462 return;
463 #ifdef LINUX26
464 #ifdef TCONFIG_USB
466 #endif
467 #endif
468 run_pppd();
470 if (nvram_get_int("ppp_demand"))
471 start_tmp_ppp(num, ifname);
472 else
473 ifconfig(ifname, IFUP, NULL, NULL);
475 TRACE_PT("end\n");
478 void stop_pppoe(void)
480 stop_ppp();
485 #if 0
486 void stop_singe_pppoe(int num)
488 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
490 int i;
492 if (num != 0) return;
494 i = nvram_get_int("pppoe_pid0");
495 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
496 do {
497 sleep(2);
498 } while (kill(i, SIGKILL) == 0);
501 unlink(ppp_linkfile);
502 nvram_unset("pppoe_ifname0");
504 nvram_set("wan_get_dns", "");
505 clear_resolv();
507 #endif
509 // -----------------------------------------------------------------------------
511 inline void stop_l2tp(void)
513 stop_ppp();
516 void start_l2tp(void)
518 TRACE_PT("begin\n");
520 FILE *fp;
521 int demand;
523 stop_l2tp();
525 if (config_pppd(WP_L2TP, 0) != 0)
526 return;
528 demand = nvram_get_int("ppp_demand");
530 /* Generate XL2TPD configuration file */
531 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
532 return;
533 fprintf(fp,
534 "[global]\n"
535 "access control = no\n"
536 "port = 1701\n"
537 "[lac l2tp]\n"
538 "lns = %s\n"
539 "tx bps = 100000000\n"
540 "pppoptfile = %s\n"
541 "redial = yes\n"
542 "max redials = 32767\n"
543 "redial timeout = %d\n"
544 "ppp debug = %s\n",
545 nvram_safe_get("l2tp_server_ip"),
546 ppp_optfile,
547 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
548 nvram_get_int("debug_ppp") ? "yes" : "no");
549 fclose(fp);
551 enable_ip_forward();
553 eval("xl2tpd");
555 if (demand) {
556 eval("listen", nvram_safe_get("lan_ifname"));
558 else {
559 force_to_dial();
560 start_redial();
563 TRACE_PT("end\n");
566 // -----------------------------------------------------------------------------
568 char *wan_gateway(void)
570 char *gw = nvram_safe_get("wan_gateway_get");
571 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
572 gw = nvram_safe_get("wan_gateway");
573 return gw;
576 // -----------------------------------------------------------------------------
578 // trigger connect on demand
579 void force_to_dial(void)
581 TRACE_PT("begin\n");
583 sleep(1);
584 switch (get_wan_proto()) {
585 case WP_L2TP:
586 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
587 break;
588 case WP_PPTP:
589 eval("ping", "-c", "2", "10.112.112.112");
590 break;
591 case WP_DISABLED:
592 case WP_STATIC:
593 break;
594 default:
595 eval("ping", "-c", "2", wan_gateway());
596 break;
599 TRACE_PT("end\n");
602 // -----------------------------------------------------------------------------
604 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
606 char *routes, *tmp;
607 int bits;
608 struct in_addr mask;
609 char netmask[16];
611 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
612 tmp = routes = strdup(nvram_safe_get(nvname));
613 while (tmp && *tmp) {
614 char *ipaddr, *gateway, *nmask;
616 ipaddr = nmask = strsep(&tmp, " ");
617 strcpy(netmask, "255.255.255.255");
619 if (nmask) {
620 ipaddr = strsep(&nmask, "/");
621 if (nmask && *nmask) {
622 bits = strtol(nmask, &nmask, 10);
623 if (bits >= 1 && bits <= 32) {
624 mask.s_addr = htonl(0xffffffff << (32 - bits));
625 strcpy(netmask, inet_ntoa(mask));
629 gateway = strsep(&tmp, " ");
631 if (gateway && *gateway) {
632 if (add)
633 route_add(ifname, metric, ipaddr, gateway, netmask);
634 else
635 route_del(ifname, metric, ipaddr, gateway, netmask);
638 free(routes);
641 void do_wan_routes(char *ifname, int metric, int add)
643 if (nvram_get_int("dhcp_routes")) {
644 // Static Routes: IP ROUTER IP2 ROUTER2 ...
645 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
646 _do_wan_routes(ifname, "wan_routes1", metric, add);
647 _do_wan_routes(ifname, "wan_routes2", metric, add);
651 // -----------------------------------------------------------------------------
653 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
655 static int is_sta(int idx, int unit, int subunit, void *param)
657 char **p = param;
659 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
660 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
661 return 1;
663 return 0;
666 void start_wan(int mode)
668 int wan_proto;
669 char *wan_ifname;
670 char *p = NULL;
671 struct ifreq ifr;
672 int sd;
673 int max;
674 int mtu;
675 char buf[128];
676 int vid;
677 int vid_map;
678 int vlan0tag;
680 TRACE_PT("begin\n");
682 f_write(wan_connecting, NULL, 0, 0, 0);
686 if (!foreach_wif(1, &p, is_sta)) {
687 p = nvram_safe_get("wan_ifnameX");
688 /* vlan ID mapping */
689 if (sscanf(p, "vlan%d", &vid) == 1) {
690 vlan0tag = nvram_get_int("vlan0tag");
691 snprintf(buf, sizeof(buf), "vlan%dvid", vid);
692 vid_map = nvram_get_int(buf);
693 if ((vid_map < 1) || (vid_map > 4094)) vid_map = vlan0tag | vid;
694 snprintf(buf, sizeof(buf), "vlan%d", vid_map);
695 p = buf;
697 set_mac(p, "mac_wan", 1);
699 nvram_set("wan_ifname", p);
700 nvram_set("wan_ifnames", p);
704 wan_ifname = nvram_safe_get("wan_ifname");
705 if (wan_ifname[0] == 0) {
706 wan_ifname = "none";
707 nvram_set("wan_ifname", wan_ifname);
710 if (strcmp(wan_ifname, "none") == 0) {
711 nvram_set("wan_proto", "disabled");
712 syslog(LOG_INFO, "No WAN");
717 wan_proto = get_wan_proto();
719 // set the default gateway for WAN interface
720 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
722 if (wan_proto == WP_DISABLED) {
723 start_wan_done(wan_ifname);
724 return;
727 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
728 perror("socket");
729 return;
732 // MTU
734 switch (wan_proto) {
735 case WP_PPPOE:
736 case WP_PPP3G:
737 max = 1492;
738 break;
739 case WP_PPTP:
740 case WP_L2TP:
741 max = 1460;
742 break;
743 default:
744 max = 1500;
745 break;
747 if (nvram_match("mtu_enable", "0")) {
748 mtu = max;
750 else {
751 // KDB If we've big fat frames enabled then we *CAN* break the
752 // max MTU on PPP link
753 mtu = nvram_get_int("wan_mtu");
754 if (!(nvram_get_int("jumbo_frame_enable")) && (mtu > max)) mtu = max;
755 else if (mtu < 576) mtu = 576;
757 sprintf(buf, "%d", mtu);
758 nvram_set("wan_mtu", buf);
759 nvram_set("wan_run_mtu", buf);
761 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
762 /* if (wan_proto == WP_PPTP) {
763 mtu += 40;
764 } */ // commented out; checkme -- zzz
766 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
767 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
768 ifr.ifr_mtu = mtu;
769 strcpy(ifr.ifr_name, wan_ifname);
770 ioctl(sd, SIOCSIFMTU, &ifr);
775 ifconfig(wan_ifname, IFUP, NULL, NULL);
777 start_firewall();
779 set_host_domain_name();
781 switch (wan_proto) {
782 case WP_PPPOE:
783 case WP_PPP3G:
784 start_pppoe(PPPOE0);
785 break;
786 case WP_DHCP:
787 case WP_LTE:
788 case WP_L2TP:
789 case WP_PPTP:
790 if (wan_proto == WP_LTE) {
791 // prepare LTE modem
792 xstart("switch4g");
794 if (using_dhcpc()) {
795 stop_dhcpc();
796 start_dhcpc();
798 else if (wan_proto != WP_DHCP && wan_proto != WP_LTE) {
799 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
800 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
802 p = nvram_safe_get("wan_gateway");
803 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
804 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
806 switch (wan_proto) {
807 case WP_PPTP:
808 start_pptp(mode);
809 break;
810 case WP_L2TP:
811 start_l2tp();
812 break;
815 break;
816 default: // static
817 nvram_set("wan_iface", wan_ifname);
818 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
820 int r = 10;
821 while ((!check_wanup()) && (r-- > 0)) {
822 sleep(1);
825 start_wan_done(wan_ifname);
826 break;
829 // Get current WAN hardware address
830 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
831 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
832 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
835 /* Set initial QoS mode again now that WAN port is ready. */
836 set_et_qos_mode(sd);
838 close(sd);
840 enable_ip_forward();
842 led(LED_DIAG, 0); // for 4712, 5325E (?)
843 led(LED_DMZ, nvram_match("dmz_enable", "1"));
845 TRACE_PT("end\n");
848 #ifdef TCONFIG_IPV6
849 void start_wan6_done(const char *wan_ifname)
851 struct in_addr addr4;
852 struct in6_addr addr;
853 static char addr6[INET6_ADDRSTRLEN];
855 int service = get_ipv6_service();
857 if (service != IPV6_DISABLED) {
858 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
859 accept_ra(wan_ifname);
862 switch (service) {
863 case IPV6_NATIVE:
864 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
865 break;
866 case IPV6_NATIVE_DHCP:
867 if (nvram_get_int("ipv6_isp_opt") == 1) {
868 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
870 stop_dhcp6c();
871 start_dhcp6c();
872 break;
873 case IPV6_ANYCAST_6TO4:
874 case IPV6_6IN4:
875 stop_ipv6_tunnel();
876 if (service == IPV6_ANYCAST_6TO4) {
877 addr4.s_addr = 0;
878 memset(&addr, 0, sizeof(addr));
879 inet_aton(get_wanip(), &addr4);
880 addr.s6_addr16[0] = htons(0x2002);
881 ipv6_mapaddr4(&addr, 16, &addr4, 0);
882 addr.s6_addr16[3] = htons(0x0001);
883 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
884 nvram_set("ipv6_prefix", addr6);
886 start_ipv6_tunnel();
887 // FIXME: give it a few seconds for DAD completion
888 sleep(2);
889 break;
890 case IPV6_6RD:
891 case IPV6_6RD_DHCP:
892 stop_6rd_tunnel();
893 start_6rd_tunnel();
894 // FIXME2?: give it a few seconds for DAD completion
895 sleep(2);
896 break;
899 #endif
901 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
902 // wan_ifname: vlan1
903 // wan_iface: ppp# (PPPOE, PPP3G, PPTP, L2TP), vlan1 (DHCP, HB, Static, LTE)
905 void start_wan_done(char *wan_ifname)
907 int proto;
908 int n;
909 char *gw;
910 struct sysinfo si;
911 int wanup;
913 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
915 sysinfo(&si);
916 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
918 proto = get_wan_proto();
920 // delete all default routes
921 route_del(wan_ifname, 0, NULL, NULL, NULL);
923 if (proto != WP_DISABLED) {
924 // set default route to gateway if specified
925 gw = wan_gateway();
926 #if 0
927 if (proto == WP_PPTP && !using_dhcpc()) {
928 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
929 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
931 #endif
932 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
933 if (proto == WP_DHCP || proto == WP_STATIC || proto == WP_LTE) {
934 // possibly gateway is over the bridge, try adding a route to gateway first
935 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
938 n = 5;
939 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
940 sleep(1);
942 _dprintf("set default gateway=%s n=%d\n", gw, n);
944 // hack: avoid routing cycles, when both peer and server have the same IP
945 if (proto == WP_PPTP || proto == WP_L2TP) {
946 // delete gateway route as it's no longer needed
947 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
951 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
953 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
954 if (proto == WP_STATIC)
956 int ifindex;
957 u_int32_t wan_ip;
958 unsigned char wan_mac[6];
960 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
961 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
964 #endif
966 if (proto == WP_PPTP || proto == WP_L2TP) {
967 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
968 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
970 if (proto == WP_L2TP) {
971 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
975 dns_to_resolv();
976 start_dnsmasq();
978 start_firewall();
979 start_qos();
981 do_static_routes(1);
982 // and routes supplied via DHCP
983 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
985 stop_zebra();
986 start_zebra();
988 wanup = check_wanup();
990 if ((wanup) || (time(0) < Y2K)) {
991 stop_ntpc();
992 start_ntpc();
995 if ((wanup) || (proto == WP_DISABLED)) {
996 stop_ddns();
997 start_ddns();
998 stop_igmp_proxy();
999 stop_udpxy();
1000 start_igmp_proxy();
1001 start_udpxy();
1004 #ifdef TCONFIG_IPV6
1005 start_wan6_done(get_wan6face());
1006 #endif
1008 #ifdef TCONFIG_DNSSEC
1009 if (nvram_match("dnssec_enable", "1")) {
1010 killall("dnsmasq", SIGHUP);
1012 #endif
1014 stop_upnp();
1015 start_upnp();
1017 // restart httpd
1018 start_httpd();
1020 if (wanup) {
1021 SET_LED(GOT_IP);
1022 notice_set("wan", "");
1024 run_nvscript("script_wanup", NULL, 0);
1027 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
1028 if (check_hw_type() == HW_BCM4702) {
1029 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1030 if (nvram_match("lan_stp", "1"))
1031 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
1032 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
1033 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
1034 if (nvram_match("lan1_stp", "1"))
1035 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
1037 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
1038 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
1039 if (nvram_match("lan2_stp", "1"))
1040 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
1042 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
1043 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
1044 if (nvram_match("lan3_stp", "1"))
1045 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
1049 if (wanup)
1050 start_vpn_eas();
1052 #ifdef TCONFIG_TINC
1053 if(wanup)
1054 start_tinc_wanup();
1055 #endif
1057 #ifdef TCONFIG_PPTPD
1058 if (wanup && nvram_get_int("pptp_client_enable"))
1059 start_pptp_client();
1060 #endif
1062 unlink(wan_connecting);
1064 new_qoslimit_start(); //!! RAF
1066 TRACE_PT("end\n");
1069 void stop_wan(void)
1071 char name[80];
1072 char *next;
1073 int wan_proto;
1075 TRACE_PT("begin\n");
1077 #ifdef TCONFIG_TINC
1078 stop_tinc();
1079 #endif
1081 #ifdef TCONFIG_PPTPD
1082 stop_pptp_client();
1083 stop_dnsmasq();
1084 dns_to_resolv();
1085 start_dnsmasq();
1086 #endif
1088 wan_proto = get_wan_proto();
1090 if (wan_proto == WP_LTE) {
1091 xstart("switch4g", "disconnect");
1094 new_qoslimit_stop(); //!! RAF
1096 stop_qos();
1097 stop_upnp(); //!!TB - moved from stop_services()
1098 stop_firewall();
1099 stop_igmp_proxy();
1100 stop_udpxy();
1101 stop_ntpc();
1103 #ifdef TCONFIG_IPV6
1104 stop_ipv6_tunnel();
1105 stop_dhcp6c();
1106 nvram_set("ipv6_get_dns", "");
1107 #endif
1109 /* Kill any WAN client daemons or callbacks */
1110 stop_redial();
1111 stop_pppoe();
1112 stop_ppp();
1113 stop_dhcpc();
1114 stop_vpn_eas();
1115 clear_resolv();
1116 nvram_set("wan_get_dns", "");
1118 /* Bring down WAN interfaces */
1119 foreach(name, nvram_safe_get("wan_ifnames"), next)
1120 ifconfig(name, 0, "0.0.0.0", NULL);
1122 SET_LED(RELEASE_IP);
1123 //notice_set("wan", "");
1124 unlink("/var/notice/wan");
1125 unlink(wan_connecting);
1127 TRACE_PT("end\n");