Don't send AT+CGDCONT if APN field is empty
[tomato.git] / release / src / router / rc / wan.c
blob22d900231bc33227c3c0e0c0be768833d9892493
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"
240 /* Only send the AT+CGDCONT (define PDP context) command to set
241 * the APN if modem_apn is defined and non-empty. Some ISPs
242 * (ex. BSNL EVDO in India) don't need this (the modem returns
243 * ERROR if issued).
245 if (((p = nvram_get("modem_apn")) != NULL) && (*p)) {
246 fprintf(cfp, "OK 'AT+CGDCONT=1,\"IP\",\"%s\"'\n", p);
249 fprintf(cfp,
250 "OK \"ATDT%s\"\n"
251 "CONNECT \\c\n",
252 nvram_safe_get("modem_init")
254 fclose(cfp);
257 if (nvram_match("usb_3g", "1") && nvram_match("wan_proto", "ppp3g")) {
258 // clear old gateway
259 if (strlen(nvram_get("wan_gateway")) >0 ) {
260 nvram_set("wan_gateway", "");
263 // detect 3G Modem
264 xstart("switch3g");
266 break;
267 #endif
268 #endif
269 case WP_L2TP:
270 fprintf(fp, "nomppe nomppc\n");
271 if (nvram_get_int("mtu_enable"))
272 fprintf(fp, "mtu %d\n", nvram_get_int("wan_mtu"));
273 break;
276 if (demand) {
277 // demand mode
278 fprintf(fp,
279 "demand\n" // Dial on demand
280 "idle %d\n"
281 "ipcp-accept-remote\n"
282 "ipcp-accept-local\n"
283 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
284 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
285 nvram_get_int("ppp_idletime") * 60);
288 #ifdef TCONFIG_IPV6
289 switch (get_ipv6_service()) {
290 case IPV6_NATIVE:
291 case IPV6_NATIVE_DHCP:
292 fprintf(fp, "+ipv6\n");
293 break;
295 #endif
296 // User specific options
297 fprintf(fp, "%s\n", nvram_safe_get("ppp_custom"));
299 fclose(fp);
300 make_secrets();
302 TRACE_PT("end\n");
303 return 0;
306 static void stop_ppp(void)
308 TRACE_PT("begin\n");
310 unlink(ppp_linkfile);
312 killall_tk("ip-up");
313 killall_tk("ip-down");
314 #ifdef TCONFIG_IPV6
315 killall_tk("ipv6-up");
316 killall_tk("ipv6-down");
317 #endif
318 killall_tk("xl2tpd");
319 killall_tk("pppd");
320 killall_tk("listen");
322 TRACE_PT("end\n");
325 static void run_pppd(void)
328 eval("pppd", "file", (char *)ppp_optfile);
330 if (nvram_get_int("ppp_demand")) {
331 // demand mode
333 Fixed issue id 7887(or 7787):
334 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
336 stop_dnsmasq();
337 dns_to_resolv();
338 start_dnsmasq();
340 // Trigger Connect On Demand if user ping pptp server
341 eval("listen", nvram_safe_get("lan_ifname"));
343 else {
344 // keepalive mode
345 start_redial();
349 // -----------------------------------------------------------------------------
351 inline void stop_pptp(void)
353 stop_ppp();
356 void start_pptp(int mode)
358 TRACE_PT("begin\n");
360 if (!using_dhcpc()) stop_dhcpc();
361 stop_pptp();
363 if (config_pppd(WP_PPTP, 0) != 0)
364 return;
366 run_pppd();
368 TRACE_PT("end\n");
371 // -----------------------------------------------------------------------------
373 void preset_wan(char *ifname, char *gw, char *netmask)
375 int i;
377 /* Delete all default routes */
378 route_del(ifname, 0, NULL, NULL, NULL);
380 /* try adding a route to gateway first */
381 route_add(ifname, 0, gw, NULL, "255.255.255.255");
383 /* Set default route to gateway if specified */
384 i = 5;
385 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
386 sleep(1);
388 _dprintf("set default gateway=%s n=%d\n", gw, i);
390 /* Add routes to dns servers as well for demand ppp to work */
391 char word[100], *next;
392 in_addr_t mask = inet_addr(netmask);
393 foreach(word, nvram_safe_get("wan_get_dns"), next) {
394 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask))
395 route_add(ifname, 0, word, gw, "255.255.255.255");
398 dns_to_resolv();
399 start_dnsmasq();
400 sleep(1);
401 start_firewall();
404 // -----------------------------------------------------------------------------
407 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
408 static void start_tmp_ppp(int num, char *ifname)
410 int timeout;
411 struct ifreq ifr;
412 int s;
414 TRACE_PT("begin: num=%d\n", num);
416 if (num != 0) return;
418 // Wait for ppp0 to be created
419 timeout = 15;
420 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
421 sleep(1);
422 _dprintf("[%d] waiting for %s %d...\n", __LINE__, ifname, timeout);
425 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) return;
426 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
428 // Set temporary IP address
429 timeout = 3;
430 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
431 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
432 sleep(1);
434 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr.ifr_addr))));
435 nvram_set("wan_netmask", "255.255.255.255");
437 // Set temporary P-t-P address
438 timeout = 3;
439 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
440 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
441 sleep(1);
443 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr.ifr_dstaddr))));
445 close(s);
447 start_wan_done(ifname);
448 TRACE_PT("end\n");
451 void start_pppoe(int num)
453 char ifname[8];
455 TRACE_PT("begin pppoe_num=%d\n", num);
457 if (num != 0) return;
459 stop_pppoe();
461 snprintf(ifname, sizeof(ifname), "ppp%d", num);
463 #ifdef LINUX26
464 #ifdef TCONFIG_USB
465 if (nvram_match( "wan_proto", "ppp3g") ) {
466 if (config_pppd(WP_PPP3G, num) != 0)
467 return;
468 } else {
469 #endif
470 #endif
471 if (config_pppd(WP_PPPOE, num) != 0)
472 return;
473 #ifdef LINUX26
474 #ifdef TCONFIG_USB
476 #endif
477 #endif
478 run_pppd();
480 if (nvram_get_int("ppp_demand"))
481 start_tmp_ppp(num, ifname);
482 else
483 ifconfig(ifname, IFUP, NULL, NULL);
485 TRACE_PT("end\n");
488 void stop_pppoe(void)
490 stop_ppp();
495 #if 0
496 void stop_singe_pppoe(int num)
498 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
500 int i;
502 if (num != 0) return;
504 i = nvram_get_int("pppoe_pid0");
505 if ((i > 1) && (kill(i, SIGTERM) == 0)) {
506 do {
507 sleep(2);
508 } while (kill(i, SIGKILL) == 0);
511 unlink(ppp_linkfile);
512 nvram_unset("pppoe_ifname0");
514 nvram_set("wan_get_dns", "");
515 clear_resolv();
517 #endif
519 // -----------------------------------------------------------------------------
521 inline void stop_l2tp(void)
523 stop_ppp();
526 void start_l2tp(void)
528 TRACE_PT("begin\n");
530 FILE *fp;
531 int demand;
533 stop_l2tp();
535 if (config_pppd(WP_L2TP, 0) != 0)
536 return;
538 demand = nvram_get_int("ppp_demand");
540 /* Generate XL2TPD configuration file */
541 if ((fp = fopen("/etc/xl2tpd.conf", "w")) == NULL)
542 return;
543 fprintf(fp,
544 "[global]\n"
545 "access control = no\n"
546 "port = 1701\n"
547 "[lac l2tp]\n"
548 "lns = %s\n"
549 "tx bps = 100000000\n"
550 "pppoptfile = %s\n"
551 "redial = yes\n"
552 "max redials = 32767\n"
553 "redial timeout = %d\n"
554 "ppp debug = %s\n",
555 nvram_safe_get("l2tp_server_ip"),
556 ppp_optfile,
557 demand ? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
558 nvram_get_int("debug_ppp") ? "yes" : "no");
559 fclose(fp);
561 enable_ip_forward();
563 eval("xl2tpd");
565 if (demand) {
566 eval("listen", nvram_safe_get("lan_ifname"));
568 else {
569 force_to_dial();
570 start_redial();
573 TRACE_PT("end\n");
576 // -----------------------------------------------------------------------------
578 char *wan_gateway(void)
580 char *gw = nvram_safe_get("wan_gateway_get");
581 if ((*gw == 0) || (strcmp(gw, "0.0.0.0") == 0))
582 gw = nvram_safe_get("wan_gateway");
583 return gw;
586 // -----------------------------------------------------------------------------
588 // trigger connect on demand
589 void force_to_dial(void)
591 TRACE_PT("begin\n");
593 sleep(1);
594 switch (get_wan_proto()) {
595 case WP_L2TP:
596 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
597 break;
598 case WP_PPTP:
599 eval("ping", "-c", "2", "10.112.112.112");
600 break;
601 case WP_DISABLED:
602 case WP_STATIC:
603 break;
604 default:
605 eval("ping", "-c", "2", wan_gateway());
606 break;
609 TRACE_PT("end\n");
612 // -----------------------------------------------------------------------------
614 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
616 char *routes, *tmp;
617 int bits;
618 struct in_addr mask;
619 char netmask[16];
621 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
622 tmp = routes = strdup(nvram_safe_get(nvname));
623 while (tmp && *tmp) {
624 char *ipaddr, *gateway, *nmask;
626 ipaddr = nmask = strsep(&tmp, " ");
627 strcpy(netmask, "255.255.255.255");
629 if (nmask) {
630 ipaddr = strsep(&nmask, "/");
631 if (nmask && *nmask) {
632 bits = strtol(nmask, &nmask, 10);
633 if (bits >= 1 && bits <= 32) {
634 mask.s_addr = htonl(0xffffffff << (32 - bits));
635 strcpy(netmask, inet_ntoa(mask));
639 gateway = strsep(&tmp, " ");
641 if (gateway && *gateway) {
642 if (add)
643 route_add(ifname, metric, ipaddr, gateway, netmask);
644 else
645 route_del(ifname, metric, ipaddr, gateway, netmask);
648 free(routes);
651 void do_wan_routes(char *ifname, int metric, int add)
653 if (nvram_get_int("dhcp_routes")) {
654 // Static Routes: IP ROUTER IP2 ROUTER2 ...
655 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
656 _do_wan_routes(ifname, "wan_routes1", metric, add);
657 _do_wan_routes(ifname, "wan_routes2", metric, add);
661 // -----------------------------------------------------------------------------
663 const char wan_connecting[] = "/var/lib/misc/wan.connecting";
665 static int is_sta(int idx, int unit, int subunit, void *param)
667 char **p = param;
669 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
670 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
671 return 1;
673 return 0;
676 void start_wan(int mode)
678 int wan_proto;
679 char *wan_ifname;
680 char *p = NULL;
681 struct ifreq ifr;
682 int sd;
683 int max;
684 int mtu;
685 char buf[128];
686 int vid;
687 int vid_map;
688 int vlan0tag;
690 TRACE_PT("begin\n");
692 f_write(wan_connecting, NULL, 0, 0, 0);
696 if (!foreach_wif(1, &p, is_sta)) {
697 p = nvram_safe_get("wan_ifnameX");
698 /* vlan ID mapping */
699 if (sscanf(p, "vlan%d", &vid) == 1) {
700 vlan0tag = nvram_get_int("vlan0tag");
701 snprintf(buf, sizeof(buf), "vlan%dvid", vid);
702 vid_map = nvram_get_int(buf);
703 if ((vid_map < 1) || (vid_map > 4094)) vid_map = vlan0tag | vid;
704 snprintf(buf, sizeof(buf), "vlan%d", vid_map);
705 p = buf;
707 set_mac(p, "mac_wan", 1);
709 nvram_set("wan_ifname", p);
710 nvram_set("wan_ifnames", p);
714 wan_ifname = nvram_safe_get("wan_ifname");
715 if (wan_ifname[0] == 0) {
716 wan_ifname = "none";
717 nvram_set("wan_ifname", wan_ifname);
720 if (strcmp(wan_ifname, "none") == 0) {
721 nvram_set("wan_proto", "disabled");
722 syslog(LOG_INFO, "No WAN");
727 wan_proto = get_wan_proto();
729 // set the default gateway for WAN interface
730 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
732 if (wan_proto == WP_DISABLED) {
733 start_wan_done(wan_ifname);
734 return;
737 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
738 perror("socket");
739 return;
742 // MTU
744 switch (wan_proto) {
745 case WP_PPPOE:
746 case WP_PPP3G:
747 max = 1492;
748 break;
749 case WP_PPTP:
750 case WP_L2TP:
751 max = 1460;
752 break;
753 default:
754 max = 1500;
755 break;
757 if (nvram_match("mtu_enable", "0")) {
758 mtu = max;
760 else {
761 // KDB If we've big fat frames enabled then we *CAN* break the
762 // max MTU on PPP link
763 mtu = nvram_get_int("wan_mtu");
764 if (!(nvram_get_int("jumbo_frame_enable")) && (mtu > max)) mtu = max;
765 else if (mtu < 576) mtu = 576;
767 sprintf(buf, "%d", mtu);
768 nvram_set("wan_mtu", buf);
769 nvram_set("wan_run_mtu", buf);
771 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
772 /* if (wan_proto == WP_PPTP) {
773 mtu += 40;
774 } */ // commented out; checkme -- zzz
776 if (wan_proto != WP_PPTP && wan_proto != WP_L2TP && wan_proto != WP_PPPOE) {
777 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
778 ifr.ifr_mtu = mtu;
779 strcpy(ifr.ifr_name, wan_ifname);
780 ioctl(sd, SIOCSIFMTU, &ifr);
785 ifconfig(wan_ifname, IFUP, NULL, NULL);
787 start_firewall();
789 set_host_domain_name();
791 switch (wan_proto) {
792 case WP_PPPOE:
793 case WP_PPP3G:
794 start_pppoe(PPPOE0);
795 break;
796 case WP_DHCP:
797 case WP_LTE:
798 case WP_L2TP:
799 case WP_PPTP:
800 if (wan_proto == WP_LTE) {
801 // prepare LTE modem
802 xstart("switch4g");
804 if (using_dhcpc()) {
805 stop_dhcpc();
806 start_dhcpc();
808 else if (wan_proto != WP_DHCP && wan_proto != WP_LTE) {
809 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL);
810 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
812 p = nvram_safe_get("wan_gateway");
813 if ((*p != 0) && (strcmp(p, "0.0.0.0") != 0))
814 preset_wan(wan_ifname, p, nvram_safe_get("wan_netmask"));
816 switch (wan_proto) {
817 case WP_PPTP:
818 start_pptp(mode);
819 break;
820 case WP_L2TP:
821 start_l2tp();
822 break;
825 break;
826 default: // static
827 nvram_set("wan_iface", wan_ifname);
828 ifconfig(wan_ifname, IFUP, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
830 int r = 10;
831 while ((!check_wanup()) && (r-- > 0)) {
832 sleep(1);
835 start_wan_done(wan_ifname);
836 break;
839 // Get current WAN hardware address
840 strlcpy(ifr.ifr_name, wan_ifname, IFNAMSIZ);
841 if (ioctl(sd, SIOCGIFHWADDR, &ifr) == 0) {
842 nvram_set("wan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, buf));
845 /* Set initial QoS mode again now that WAN port is ready. */
846 set_et_qos_mode(sd);
848 close(sd);
850 enable_ip_forward();
852 led(LED_DIAG, 0); // for 4712, 5325E (?)
853 led(LED_DMZ, nvram_match("dmz_enable", "1"));
855 TRACE_PT("end\n");
858 #ifdef TCONFIG_IPV6
859 void start_wan6_done(const char *wan_ifname)
861 struct in_addr addr4;
862 struct in6_addr addr;
863 static char addr6[INET6_ADDRSTRLEN];
865 int service = get_ipv6_service();
867 if (service != IPV6_DISABLED) {
868 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
869 accept_ra(wan_ifname);
872 switch (service) {
873 case IPV6_NATIVE:
874 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
875 break;
876 case IPV6_NATIVE_DHCP:
877 if (nvram_get_int("ipv6_pdonly") == 1) {
878 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
880 stop_dhcp6c();
881 start_dhcp6c();
882 break;
883 case IPV6_ANYCAST_6TO4:
884 case IPV6_6IN4:
885 stop_ipv6_tunnel();
886 if (service == IPV6_ANYCAST_6TO4) {
887 addr4.s_addr = 0;
888 memset(&addr, 0, sizeof(addr));
889 inet_aton(get_wanip(), &addr4);
890 addr.s6_addr16[0] = htons(0x2002);
891 ipv6_mapaddr4(&addr, 16, &addr4, 0);
892 addr.s6_addr16[3] = htons(0x0001);
893 inet_ntop(AF_INET6, &addr, addr6, sizeof(addr6));
894 nvram_set("ipv6_prefix", addr6);
896 start_ipv6_tunnel();
897 // FIXME: give it a few seconds for DAD completion
898 sleep(2);
899 break;
900 case IPV6_6RD:
901 case IPV6_6RD_DHCP:
902 stop_6rd_tunnel();
903 start_6rd_tunnel();
904 // FIXME2?: give it a few seconds for DAD completion
905 sleep(2);
906 break;
909 #endif
911 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
912 // wan_ifname: vlan1
913 // wan_iface: ppp# (PPPOE, PPP3G, PPTP, L2TP), vlan1 (DHCP, HB, Static, LTE)
915 void start_wan_done(char *wan_ifname)
917 int proto;
918 int n;
919 char *gw;
920 struct sysinfo si;
921 int wanup;
923 TRACE_PT("begin wan_ifname=%s\n", wan_ifname);
925 sysinfo(&si);
926 f_write("/var/lib/misc/wantime", &si.uptime, sizeof(si.uptime), 0, 0);
928 proto = get_wan_proto();
930 // delete all default routes
931 route_del(wan_ifname, 0, NULL, NULL, NULL);
933 if (proto != WP_DISABLED) {
934 // set default route to gateway if specified
935 gw = wan_gateway();
936 #if 0
937 if (proto == WP_PPTP && !using_dhcpc()) {
938 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
939 if (*gw == 0 || strcmp(gw, "0.0.0.0") == 0) gw = nvram_safe_get("ppp_get_ip");
941 #endif
942 if ((*gw != 0) && (strcmp(gw, "0.0.0.0") != 0)) {
943 if (proto == WP_DHCP || proto == WP_STATIC || proto == WP_LTE) {
944 // possibly gateway is over the bridge, try adding a route to gateway first
945 route_add(wan_ifname, 0, gw, NULL, "255.255.255.255");
948 n = 5;
949 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
950 sleep(1);
952 _dprintf("set default gateway=%s n=%d\n", gw, n);
954 // hack: avoid routing cycles, when both peer and server have the same IP
955 if (proto == WP_PPTP || proto == WP_L2TP) {
956 // delete gateway route as it's no longer needed
957 route_del(wan_ifname, 0, gw, "0.0.0.0", "255.255.255.255");
961 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
963 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
964 if (proto == WP_STATIC)
966 int ifindex;
967 u_int32_t wan_ip;
968 unsigned char wan_mac[6];
970 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
971 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
974 #endif
976 if (proto == WP_PPTP || proto == WP_L2TP) {
977 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL, "255.255.255.255");
978 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL, "255.255.255.255");
980 if (proto == WP_L2TP) {
981 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
985 dns_to_resolv();
986 start_dnsmasq();
988 start_firewall();
989 start_qos();
991 do_static_routes(1);
992 // and routes supplied via DHCP
993 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname, 0, 1);
995 stop_zebra();
996 start_zebra();
998 wanup = check_wanup();
1000 if ((wanup) || (time(0) < Y2K)) {
1001 stop_ntpc();
1002 start_ntpc();
1005 if ((wanup) || (proto == WP_DISABLED)) {
1006 stop_ddns();
1007 start_ddns();
1008 stop_igmp_proxy();
1009 stop_udpxy();
1010 start_igmp_proxy();
1011 start_udpxy();
1014 #ifdef TCONFIG_IPV6
1015 start_wan6_done(get_wan6face());
1016 #endif
1018 #ifdef TCONFIG_DNSSEC
1019 if (nvram_match("dnssec_enable", "1")) {
1020 killall("dnsmasq", SIGHUP);
1022 #endif
1024 stop_upnp();
1025 start_upnp();
1027 // restart httpd
1028 start_httpd();
1030 if (wanup) {
1031 SET_LED(GOT_IP);
1032 notice_set("wan", "");
1034 run_nvscript("script_wanup", NULL, 0);
1037 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
1038 if (check_hw_type() == HW_BCM4702) {
1039 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1040 if (nvram_match("lan_stp", "1"))
1041 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
1042 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
1043 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
1044 if (nvram_match("lan1_stp", "1"))
1045 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
1047 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
1048 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
1049 if (nvram_match("lan2_stp", "1"))
1050 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
1052 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
1053 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
1054 if (nvram_match("lan3_stp", "1"))
1055 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
1059 if (wanup)
1060 start_vpn_eas();
1062 #ifdef TCONFIG_TINC
1063 if(wanup)
1064 start_tinc_wanup();
1065 #endif
1067 #ifdef TCONFIG_PPTPD
1068 if (wanup && nvram_get_int("pptp_client_enable"))
1069 start_pptp_client();
1070 #endif
1072 unlink(wan_connecting);
1074 new_qoslimit_start(); //!! RAF
1076 TRACE_PT("end\n");
1079 void stop_wan(void)
1081 char name[80];
1082 char *next;
1083 int wan_proto;
1085 TRACE_PT("begin\n");
1087 #ifdef TCONFIG_TINC
1088 stop_tinc();
1089 #endif
1091 #ifdef TCONFIG_PPTPD
1092 stop_pptp_client();
1093 stop_dnsmasq();
1094 dns_to_resolv();
1095 start_dnsmasq();
1096 #endif
1098 wan_proto = get_wan_proto();
1100 if (wan_proto == WP_LTE) {
1101 xstart("switch4g", "disconnect");
1104 new_qoslimit_stop(); //!! RAF
1106 stop_qos();
1107 stop_upnp(); //!!TB - moved from stop_services()
1108 stop_firewall();
1109 stop_igmp_proxy();
1110 stop_udpxy();
1111 stop_ntpc();
1113 #ifdef TCONFIG_IPV6
1114 stop_ipv6_tunnel();
1115 stop_dhcp6c();
1116 nvram_set("ipv6_get_dns", "");
1117 #endif
1119 /* Kill any WAN client daemons or callbacks */
1120 stop_redial();
1121 stop_pppoe();
1122 stop_ppp();
1123 stop_dhcpc();
1124 stop_vpn_eas();
1125 clear_resolv();
1126 nvram_set("wan_get_dns", "");
1128 /* Bring down WAN interfaces */
1129 foreach(name, nvram_safe_get("wan_ifnames"), next)
1130 ifconfig(name, 0, "0.0.0.0", NULL);
1132 SET_LED(RELEASE_IP);
1133 //notice_set("wan", "");
1134 unlink("/var/notice/wan");
1135 unlink(wan_connecting);
1137 TRACE_PT("end\n");