Merge branch 'Toastman-NRT' into Toastman-RT-AC
[tomato/Zahal24.git] / release / src / router / rc / wan.c
blobaa1d5714be882ffadc8a7a18ba3d61ac3693b7c7
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");
205 break;
206 #ifdef LINUX26
207 #ifdef TCONFIG_USB
208 case WP_PPP3G:
209 if ((cfp = fopen(ppp3g_chatfile, "w")) == NULL) {
210 perror(ppp3g_chatfile);
211 return -1;
213 fprintf(cfp,
214 "ABORT \"NO CARRIER\"\n"
215 "ABORT \"NO DIALTONE\"\n"
216 "ABORT \"NO ERROR\"\n"
217 "ABORT \"NO ANSWER\"\n"
218 "ABORT \"BUSY\"\n"
219 "REPORT CONNECT\n"
220 "\"\" \"AT\"\n");
221 /* moved to switch3g script
222 if (strlen(nvram_get("modem_pin")) >0 ) {
223 fprintf(cfp,
224 "TIMEOUT 60\n"
225 "OK \"AT+CPIN=%s\"\n"
226 "TIMEOUT 10\n",
227 nvram_get("modem_pin"));
230 fprintf(cfp,
231 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
232 "OK \"AT\"\n"
233 "OK \"ATS0=0\"\n"
234 "OK \"AT\"\n"
235 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
236 "OK \"AT\"\n"
239 /* Only send the AT+CGDCONT (define PDP context) command to set
240 * the APN if modem_apn is defined and non-empty. Some ISPs
241 * (ex. BSNL EVDO in India) don't need this (the modem returns
242 * ERROR if issued).
244 if (((p = nvram_get("modem_apn")) != NULL) && (*p)) {
245 fprintf(cfp, "OK 'AT+CGDCONT=1,\"IP\",\"%s\"'\n", p);
248 fprintf(cfp,
249 "OK \"ATDT%s\"\n"
250 "CONNECT \\c\n",
251 nvram_safe_get("modem_init")
253 fclose(cfp);
256 if (nvram_match("usb_3g", "1") && nvram_match("wan_proto", "ppp3g")) {
257 // clear old gateway
258 if (strlen(nvram_get("wan_gateway")) >0 ) {
259 nvram_set("wan_gateway", "");
262 // detect 3G Modem
263 xstart("switch3g");
265 break;
266 #endif
267 #endif
268 case WP_L2TP:
269 fprintf(fp, "nomppe nomppc\n");
270 if (nvram_get_int("mtu_enable"))
271 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
272 break;
275 if (demand) {
276 // demand mode
277 fprintf(fp,
278 "demand\n" // Dial on demand
279 "idle %d\n"
280 "ipcp-accept-remote\n"
281 "ipcp-accept-local\n"
282 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
283 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
284 nvram_get_int("ppp_idletime") * 60);
287 #ifdef TCONFIG_IPV6
288 switch (get_ipv6_service()) {
289 case IPV6_NATIVE:
290 case IPV6_NATIVE_DHCP:
291 fprintf(fp, "+ipv6\n");
292 break;
294 #endif
295 // User specific options
296 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
298 fclose(fp);
299 make_secrets();
301 TRACE_PT("end\n");
302 return 0;
305 static void stop_ppp(void)
307 TRACE_PT("begin\n");
309 unlink(ppp_linkfile);
311 killall_tk("ip-up");
312 killall_tk("ip-down");
313 #ifdef TCONFIG_IPV6
314 killall_tk("ipv6-up");
315 killall_tk("ipv6-down");
316 #endif
317 killall_tk("xl2tpd");
318 killall_tk("pppd");
319 killall_tk("listen");
321 TRACE_PT("end\n");
324 static void run_pppd(void)
327 eval("pppd", "file", (char *)ppp_optfile);
329 if (nvram_get_int("ppp_demand")) {
330 // demand mode
332 Fixed issue id 7887(or 7787):
333 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
335 stop_dnsmasq();
336 dns_to_resolv();
337 start_dnsmasq();
339 // Trigger Connect On Demand if user ping pptp server
340 eval("listen", nvram_safe_get("lan_ifname"));
342 else {
343 // keepalive mode
344 start_redial();
348 // -----------------------------------------------------------------------------
350 inline void stop_pptp(void)
352 stop_ppp();
355 void start_pptp(int mode)
357 TRACE_PT("begin\n");
359 if (!using_dhcpc()) stop_dhcpc();
360 stop_pptp();
362 if (config_pppd(WP_PPTP, 0) != 0)
363 return;
365 run_pppd();
367 TRACE_PT("end\n");
370 // -----------------------------------------------------------------------------
372 void preset_wan(char *ifname, char *gw, char *netmask)
374 int i;
376 /* Delete all default routes */
377 route_del(ifname, 0, NULL, NULL, NULL);
379 /* try adding a route to gateway first */
380 route_add(ifname, 0, gw, NULL, "255.255.255.255");
382 /* Set default route to gateway if specified */
383 i = 5;
384 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
385 sleep(1);
387 _dprintf("set default gateway=%s n=%d\n", gw, i);
389 /* Add routes to dns servers as well for demand ppp to work */
390 char word[100], *next;
391 in_addr_t mask = inet_addr(netmask);
392 foreach(word, nvram_safe_get("wan_get_dns"), next) {
393 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
394 route_add(ifname, 0, word, gw, "255.255.255.255");
397 dns_to_resolv();
398 start_dnsmasq();
399 sleep(1);
400 start_firewall();
403 // -----------------------------------------------------------------------------
406 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
407 static void start_tmp_ppp(int num, char *ifname)
409 int timeout;
410 struct ifreq ifr;
411 int s;
413 TRACE_PT("begin: num=%d\n", num);
415 if (num != 0) return;
417 // Wait for ppp0 to be created
418 timeout = 15;
419 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
420 sleep(1);
421 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
424 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
425 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
427 // Set temporary IP address
428 timeout = 3;
429 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
430 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
431 sleep(1);
433 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
434 nvram_set("wan_netmask", "255.255.255.255");
436 // Set temporary P-t-P address
437 timeout = 3;
438 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
439 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
440 sleep(1);
442 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
444 close(s);
446 start_wan_done(ifname);
447 TRACE_PT("end\n");
450 void start_pppoe(int num)
452 char ifname[8];
454 TRACE_PT("begin pppoe_num=%d\n", num);
456 if (num != 0) return;
458 stop_pppoe();
460 snprintf(ifname, sizeof(ifname), "ppp%d", num);
462 #ifdef LINUX26
463 #ifdef TCONFIG_USB
464 if (nvram_match( "wan_proto", "ppp3g") ) {
465 if (config_pppd(WP_PPP3G, num) != 0)
466 return;
467 } else {
468 #endif
469 #endif
470 if (config_pppd(WP_PPPOE, num) != 0)
471 return;
472 #ifdef LINUX26
473 #ifdef TCONFIG_USB
475 #endif
476 #endif
477 run_pppd();
479 if (nvram_get_int("ppp_demand"))
480 start_tmp_ppp(num, ifname);
481 else
482 ifconfig(ifname, IFUP, NULL, NULL);
484 TRACE_PT("end\n");
487 void stop_pppoe(void)
489 stop_ppp();
494 #if 0
495 void stop_singe_pppoe(int num)
497 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
499 int i;
501 if (num != 0) return;
503 i = nvram_get_int("pppoe_pid0");
504 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
505 do {
506 sleep(2);
507 } while (kill(i, SIGKILL) == 0);
510 unlink(ppp_linkfile);
511 nvram_unset("pppoe_ifname0");
513 nvram_set("wan_get_dns", "");
514 clear_resolv();
516 #endif
518 // -----------------------------------------------------------------------------
520 inline void stop_l2tp(void)
522 stop_ppp();
525 void start_l2tp(void)
527 TRACE_PT("begin\n");
529 FILE *fp;
530 int demand;
532 stop_l2tp();
534 if (config_pppd(WP_L2TP, 0) != 0)
535 return;
537 demand = nvram_get_int("ppp_demand");
539 /* Generate XL2TPD configuration file */
540 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
541 return;
542 fprintf(fp,
543 "[global]\n"
544 "access control = no\n"
545 "port = 1701\n"
546 "[lac l2tp]\n"
547 "lns = %s\n"
548 "tx bps = 100000000\n"
549 "pppoptfile = %s\n"
550 "redial = yes\n"
551 "max redials = 32767\n"
552 "redial timeout = %d\n"
553 "tunnel rws = 8\n"
554 "ppp debug = %s\n"
555 "%s\n",
556 nvram_safe_get("l2tp_server_ip"),
557 ppp_optfile,
558 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
559 nvram_get_int("debug_ppp") ? "yes" : "no",
560 nvram_safe_get("xl2tpd_custom"));
561 fappend(fp, "/etc/xl2tpd.custom");
562 fclose(fp);
564 enable_ip_forward();
566 eval("xl2tpd");
568 if (demand) {
569 eval("listen", nvram_safe_get("lan_ifname"));
571 else {
572 force_to_dial();
573 start_redial();
576 TRACE_PT("end\n");
579 // -----------------------------------------------------------------------------
581 char *wan_gateway(void)
583 char *gw = nvram_safe_get("wan_gateway_get");
584 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
585 gw = nvram_safe_get("wan_gateway");
586 return gw;
589 // -----------------------------------------------------------------------------
591 // trigger connect on demand
592 void force_to_dial(void)
594 TRACE_PT("begin\n");
596 sleep(1);
597 switch (get_wan_proto()) {
598 case WP_L2TP:
599 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
600 break;
601 case WP_PPTP:
602 eval("ping", "-c", "2", "10.112.112.112");
603 break;
604 case WP_DISABLED:
605 case WP_STATIC:
606 break;
607 default:
608 eval("ping", "-c", "2", wan_gateway());
609 break;
612 TRACE_PT("end\n");
615 // -----------------------------------------------------------------------------
617 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
619 char *routes, *tmp;
620 int bits;
621 struct in_addr mask;
622 char netmask[16];
624 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
625 tmp = routes = strdup(nvram_safe_get(nvname));
626 while (tmp && *tmp) {
627 char *ipaddr, *gateway, *nmask;
629 ipaddr = nmask = strsep(&tmp, " ");
630 strcpy(netmask, "255.255.255.255");
632 if (nmask) {
633 ipaddr = strsep(&nmask, "/");
634 if (nmask && *nmask) {
635 bits = strtol(nmask, &nmask, 10);
636 if (bits >= 1 && bits <= 32) {
637 mask.s_addr = htonl(0xffffffff << (32 - bits));
638 strcpy(netmask, inet_ntoa(mask));
642 gateway = strsep(&tmp, " ");
644 if (gateway && *gateway) {
645 if (add)
646 route_add(ifname, metric, ipaddr, gateway, netmask);
647 else
648 route_del(ifname, metric, ipaddr, gateway, netmask);
651 free(routes);
654 void do_wan_routes(char *ifname, int metric, int add)
656 if (nvram_get_int("dhcp_routes")) {
657 // Static Routes: IP ROUTER IP2 ROUTER2 ...
658 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
659 _do_wan_routes(ifname, "wan_routes1", metric, add);
660 _do_wan_routes(ifname, "wan_routes2", metric, add);
664 // -----------------------------------------------------------------------------
666 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
668 static int is_sta(int idx, int unit, int subunit, void *param)
670 char **p = param;
672 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
673 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
674 return 1;
676 return 0;
679 void start_wan(int mode)
681 int wan_proto;
682 char *wan_ifname;
683 char *p = NULL;
684 struct ifreq ifr;
685 int sd;
686 int max;
687 int mtu;
688 char buf[128];
689 int vid;
690 int vid_map;
691 int vlan0tag;
693 TRACE_PT("begin\n");
695 f_write(wan_connecting, NULL, 0, 0, 0);
699 if (!foreach_wif(1, &p, is_sta)) {
700 p = nvram_safe_get("wan_ifnameX");
701 /* vlan ID mapping */
702 if (sscanf(p, "vlan%d", &vid) == 1) {
703 vlan0tag = nvram_get_int("vlan0tag");
704 snprintf(buf, sizeof(buf), "vlan%dvid", vid);
705 vid_map = nvram_get_int(buf);
706 if ((vid_map < 1) || (vid_map > 4094)) vid_map = vlan0tag | vid;
707 snprintf(buf, sizeof(buf), "vlan%d", vid_map);
708 p = buf;
710 set_mac(p, "mac_wan", 1);
712 nvram_set("wan_ifname", p);
713 nvram_set("wan_ifnames", p);
717 wan_ifname = nvram_safe_get("wan_ifname");
718 if (wan_ifname[0] == 0) {
719 wan_ifname = "none";
720 nvram_set("wan_ifname", wan_ifname);
723 if (strcmp(wan_ifname, "none") == 0) {
724 nvram_set("wan_proto", "disabled");
725 syslog(LOG_INFO, "No WAN");
730 wan_proto = get_wan_proto();
732 // set the default gateway for WAN interface
733 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
735 if (wan_proto == WP_DISABLED) {
736 start_wan_done(wan_ifname);
737 return;
740 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
741 perror("socket");
742 return;
745 // MTU
747 switch (wan_proto) {
748 case WP_PPPOE:
749 case WP_PPP3G:
750 max = 1492;
751 break;
752 case WP_PPTP:
753 case WP_L2TP:
754 max = 1460;
755 break;
756 default:
757 max = 1500;
758 break;
760 if (nvram_match("mtu_enable", "0")) {
761 mtu = max;
763 else {
764 // KDB If we've big fat frames enabled then we *CAN* break the
765 // max MTU on PPP link
766 mtu = nvram_get_int("wan_mtu");
767 if (!(nvram_get_int("jumbo_frame_enable")) && (mtu > max)) mtu = max;
768 else if (mtu < 576) mtu = 576;
770 sprintf(buf, "%d", mtu);
771 nvram_set("wan_mtu", buf);
772 nvram_set("wan_run_mtu", buf);
774 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
775 /* if (wan_proto == WP_PPTP) {
776 mtu += 40;
777 } */ // commented out; checkme -- zzz
779 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
780 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
781 ifr.ifr_mtu = mtu;
782 strcpy(ifr.ifr_name, wan_ifname);
783 ioctl(sd, SIOCSIFMTU, &ifr);
788 ifconfig(wan_ifname, IFUP, NULL, NULL);
790 start_firewall();
792 set_host_domain_name();
794 switch (wan_proto) {
795 case WP_PPPOE:
796 case WP_PPP3G:
797 start_pppoe(PPPOE0);
798 break;
799 case WP_DHCP:
800 case WP_LTE:
801 case WP_L2TP:
802 case WP_PPTP:
803 if (wan_proto == WP_LTE) {
804 // prepare LTE modem
805 xstart("switch4g");
807 if (using_dhcpc()) {
808 stop_dhcpc();
809 start_dhcpc();
811 else if (wan_proto != WP_DHCP && wan_proto != WP_LTE) {
812 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
813 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
815 p = nvram_safe_get("wan_gateway");
816 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
817 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
819 switch (wan_proto) {
820 case WP_PPTP:
821 start_pptp(mode);
822 break;
823 case WP_L2TP:
824 start_l2tp();
825 break;
828 break;
829 default: // static
830 nvram_set("wan_iface", wan_ifname);
831 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
833 int r = 10;
834 while ((!check_wanup()) && (r-- > 0)) {
835 sleep(1);
838 start_wan_done(wan_ifname);
839 break;
842 // Get current WAN hardware address
843 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
844 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
845 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
848 /* Set initial QoS mode again now that WAN port is ready. */
849 set_et_qos_mode(sd);
851 close(sd);
853 enable_ip_forward();
855 led(LED_DIAG, 0); // for 4712, 5325E (?)
856 led(LED_DMZ, nvram_match("dmz_enable", "1"));
858 TRACE_PT("end\n");
861 #ifdef TCONFIG_IPV6
862 void start_wan6_done(const char *wan_ifname)
864 struct in_addr addr4;
865 struct in6_addr addr;
866 static char addr6[INET6_ADDRSTRLEN];
868 int service = get_ipv6_service();
870 if (service != IPV6_DISABLED) {
871 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
872 accept_ra(wan_ifname);
875 switch (service) {
876 case IPV6_NATIVE:
877 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
878 break;
879 case IPV6_NATIVE_DHCP:
880 if (nvram_get_int("ipv6_pdonly") == 1) {
881 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
883 stop_dhcp6c();
884 start_dhcp6c();
885 break;
886 case IPV6_ANYCAST_6TO4:
887 case IPV6_6IN4:
888 stop_ipv6_tunnel();
889 if (service == IPV6_ANYCAST_6TO4) {
890 addr4.s_addr = 0;
891 memset(&addr, 0, sizeof(addr));
892 inet_aton(get_wanip(), &addr4);
893 addr.s6_addr16[0] = htons(0x2002);
894 ipv6_mapaddr4(&addr, 16, &addr4, 0);
895 addr.s6_addr16[3] = htons(0x0001);
896 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
897 nvram_set("ipv6_prefix", addr6);
899 start_ipv6_tunnel();
900 // FIXME: give it a few seconds for DAD completion
901 sleep(2);
902 break;
903 case IPV6_6RD:
904 case IPV6_6RD_DHCP:
905 stop_6rd_tunnel();
906 start_6rd_tunnel();
907 // FIXME2?: give it a few seconds for DAD completion
908 sleep(2);
909 break;
912 #endif
914 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
915 // wan_ifname: vlan1
916 // wan_iface: ppp# (PPPOE, PPP3G, PPTP, L2TP), vlan1 (DHCP, HB, Static, LTE)
918 void start_wan_done(char *wan_ifname)
920 int proto;
921 int n;
922 char *gw;
923 struct sysinfo si;
924 int wanup;
926 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
928 sysinfo(&si);
929 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
931 proto = get_wan_proto();
933 // delete all default routes
934 route_del(wan_ifname, 0, NULL, NULL, NULL);
936 if (proto != WP_DISABLED) {
937 // set default route to gateway if specified
938 gw = wan_gateway();
939 #if 0
940 if (proto == WP_PPTP && !using_dhcpc()) {
941 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
942 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
944 #endif
945 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
946 if (proto == WP_DHCP || proto == WP_STATIC || proto == WP_LTE) {
947 // possibly gateway is over the bridge, try adding a route to gateway first
948 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
951 n = 5;
952 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
953 sleep(1);
955 _dprintf("set default gateway=%s n=%d\n", gw, n);
957 // hack: avoid routing cycles, when both peer and server have the same IP
958 if (proto == WP_PPTP || proto == WP_L2TP) {
959 // delete gateway route as it's no longer needed
960 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
964 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
966 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
967 if (proto == WP_STATIC)
969 int ifindex;
970 u_int32_t wan_ip;
971 unsigned char wan_mac[6];
973 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
974 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
977 #endif
979 if (proto == WP_PPTP || proto == WP_L2TP) {
980 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
981 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
983 if (proto == WP_L2TP) {
984 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
988 dns_to_resolv();
989 start_dnsmasq();
991 start_firewall();
992 start_qos();
994 do_static_routes(1);
995 // and routes supplied via DHCP
996 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
998 stop_zebra();
999 start_zebra();
1001 wanup = check_wanup();
1003 if ((wanup) || (time(0) < Y2K)) {
1004 stop_ntpc();
1005 start_ntpc();
1008 if ((wanup) || (proto == WP_DISABLED)) {
1009 stop_ddns();
1010 start_ddns();
1011 stop_igmp_proxy();
1012 stop_udpxy();
1013 start_igmp_proxy();
1014 start_udpxy();
1017 #ifdef TCONFIG_IPV6
1018 start_wan6_done(get_wan6face());
1019 #endif
1021 #ifdef TCONFIG_DNSSEC
1022 if (nvram_match("dnssec_enable", "1")) {
1023 killall("dnsmasq", SIGHUP);
1025 #endif
1027 stop_upnp();
1028 start_upnp();
1030 // restart httpd
1031 start_httpd();
1033 if (wanup) {
1034 SET_LED(GOT_IP);
1035 notice_set("wan", "");
1037 run_nvscript("script_wanup", NULL, 0);
1040 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
1041 if (check_hw_type() == HW_BCM4702) {
1042 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1043 if (nvram_match("lan_stp", "1"))
1044 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
1045 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
1046 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
1047 if (nvram_match("lan1_stp", "1"))
1048 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
1050 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
1051 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
1052 if (nvram_match("lan2_stp", "1"))
1053 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
1055 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
1056 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
1057 if (nvram_match("lan3_stp", "1"))
1058 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
1062 if (wanup)
1063 start_vpn_eas();
1065 #ifdef TCONFIG_TINC
1066 if(wanup)
1067 start_tinc_wanup();
1068 #endif
1070 #ifdef TCONFIG_PPTPD
1071 if (wanup && nvram_get_int("pptp_client_enable"))
1072 start_pptp_client();
1073 #endif
1075 unlink(wan_connecting);
1077 new_qoslimit_start(); //!! RAF
1079 TRACE_PT("end\n");
1082 void stop_wan(void)
1084 char name[80];
1085 char *next;
1086 int wan_proto;
1088 TRACE_PT("begin\n");
1090 #ifdef TCONFIG_TINC
1091 stop_tinc();
1092 #endif
1094 #ifdef TCONFIG_PPTPD
1095 stop_pptp_client();
1096 stop_dnsmasq();
1097 dns_to_resolv();
1098 start_dnsmasq();
1099 #endif
1101 wan_proto = get_wan_proto();
1103 if (wan_proto == WP_LTE) {
1104 xstart("switch4g", "disconnect");
1107 new_qoslimit_stop(); //!! RAF
1109 stop_qos();
1110 stop_upnp(); //!!TB - moved from stop_services()
1111 stop_firewall();
1112 stop_igmp_proxy();
1113 stop_udpxy();
1114 stop_ntpc();
1116 #ifdef TCONFIG_IPV6
1117 stop_ipv6_tunnel();
1118 stop_dhcp6c();
1119 nvram_set("ipv6_get_dns", "");
1120 #endif
1122 /* Kill any WAN client daemons or callbacks */
1123 stop_redial();
1124 stop_pppoe();
1125 stop_ppp();
1126 stop_dhcpc();
1127 stop_vpn_eas();
1128 clear_resolv();
1129 nvram_set("wan_get_dns", "");
1131 /* Bring down WAN interfaces */
1132 foreach(name, nvram_safe_get("wan_ifnames"), next)
1133 ifconfig(name, 0, "0.0.0.0", NULL);
1135 SET_LED(RELEASE_IP);
1136 //notice_set("wan", "");
1137 unlink("/var/notice/wan");
1138 unlink(wan_connecting);
1140 TRACE_PT("end\n");