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
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
39 #include <sys/ioctl.h>
40 #include <arpa/inet.h>
41 #include <sys/sysinfo.h>
45 static const char ppp_linkfile
[] = "/tmp/ppp/link";
46 static const char ppp_optfile
[] = "/tmp/ppp/wanoptions";
48 static void make_secrets(void)
54 user
= nvram_safe_get("ppp_username");
55 pass
= nvram_safe_get("ppp_passwd");
56 if ((f
= fopen("/tmp/ppp/pap-secrets", "w")) != NULL
) {
57 fprintf(f
, "\"%s\" * \"%s\" *\n", user
, pass
);
60 chmod("/tmp/ppp/pap-secrets", 0600);
62 if ((f
= fopen("/tmp/ppp/chap-secrets", "w")) != NULL
) {
63 fprintf(f
, "\"%s\" * \"%s\" *\n", user
, pass
);
66 chmod("/tmp/ppp/chap-secrets", 0600);
69 // -----------------------------------------------------------------------------
71 static int config_pppd(int wan_proto
, int num
)
79 mkdir("/tmp/ppp", 0777);
80 symlink("/sbin/rc", "/tmp/ppp/ip-up");
81 symlink("/sbin/rc", "/tmp/ppp/ip-down");
83 symlink("/sbin/rc", "/tmp/ppp/ipv6-up");
84 symlink("/sbin/rc", "/tmp/ppp/ipv6-down");
86 symlink("/dev/null", "/tmp/ppp/connect-errors");
88 demand
= nvram_get_int("ppp_demand");
90 // Generate options file
91 if ((fp
= fopen(ppp_optfile
, "w")) == NULL
) {
98 "defaultroute\n" // Add a default route to the system routing tables, using the peer as the gateway
99 "usepeerdns\n" // Ask the peer for up to 2 DNS server addresses
101 "default-asyncmap\n" // Disable asyncmap negotiation
102 "nopcomp\n" // Disable protocol field compression
103 "noaccomp\n" // Disable Address/Control compression
104 "novj\n" // Disable Van Jacobson style TCP/IP header compression
105 "nobsdcomp\n" // Disable BSD-Compress compression
106 "nodeflate\n" // Disable Deflate compression
107 "noauth\n" // Do not authenticate peer
108 "refuse-eap\n" // Do not use eap
109 "maxfail 0\n" // Never give up
110 "lcp-echo-interval %d\n"// Interval between LCP echo-requests
111 "lcp-echo-failure %d\n" // Tolerance to unanswered echo-requests
112 "lcp-echo-adaptive\n" // Suppress LCP echo-requests if traffic was received
115 nvram_safe_get("ppp_username"),
116 nvram_get_int("pppoe_lei") ? : 10,
117 nvram_get_int("pppoe_lef") ? : 5,
118 nvram_get_int("debug_ppp") ? "debug\n" : "");
120 if (wan_proto
!= WP_L2TP
) {
124 demand
? 30 : (nvram_get_int("ppp_redialperiod") ? : 30));
134 nvram_safe_get("pptp_server_ip"),
135 nvram_get_int("mtu_enable") ? nvram_get_int("wan_mtu") : 1400);
140 "plugin rp-pppoe.so\n"
144 nvram_safe_get("ppp_passwd"),
145 nvram_safe_get("wan_ifname"),
146 nvram_get_int("wan_mtu"), nvram_get_int("wan_mtu"));
147 if (((p
= nvram_get("ppp_service")) != NULL
) && (*p
)) {
148 fprintf(fp
, "rp_pppoe_service '%s'\n", p
);
150 if (((p
= nvram_get("ppp_ac")) != NULL
) && (*p
)) {
151 fprintf(fp
, "rp_pppoe_ac '%s'\n", p
);
153 if (nvram_match("ppp_mlppp", "1")) {
158 fprintf(fp
, "nomppe nomppc\n");
159 if (nvram_get_int("mtu_enable"))
160 fprintf(fp
, "mtu %d\n", nvram_get_int("wan_mtu"));
167 "demand\n" // Dial on demand
169 "ipcp-accept-remote\n"
170 "ipcp-accept-local\n"
171 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
172 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
173 nvram_get_int("ppp_idletime") * 60);
177 switch (get_ipv6_service()) {
179 case IPV6_NATIVE_DHCP
:
180 fprintf(fp
, "+ipv6\n");
184 // User specific options
185 fprintf(fp
, "%s\n", nvram_safe_get("ppp_custom"));
194 static void stop_ppp(void)
198 unlink(ppp_linkfile
);
201 killall_tk("ip-down");
203 killall_tk("ipv6-up");
204 killall_tk("ipv6-down");
206 killall_tk("xl2tpd");
208 killall_tk("listen");
213 static void run_pppd(void)
215 eval("pppd", "file", ppp_optfile
);
217 if (nvram_get_int("ppp_demand")) {
220 Fixed issue id 7887(or 7787):
221 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
227 // Trigger Connect On Demand if user ping pptp server
228 eval("listen", nvram_safe_get("lan_ifname"));
236 // -----------------------------------------------------------------------------
238 inline void stop_pptp(void)
243 void start_pptp(int mode
)
247 if (!using_dhcpc()) stop_dhcpc();
250 if (config_pppd(WP_PPTP
, 0) != 0)
258 // -----------------------------------------------------------------------------
260 void preset_wan(char *ifname
, char *gw
, char *netmask
)
264 /* Delete all default routes */
265 route_del(ifname
, 0, NULL
, NULL
, NULL
);
267 /* try adding a route to gateway first */
268 route_add(ifname
, 0, gw
, NULL
, "255.255.255.255");
270 /* Set default route to gateway if specified */
272 while ((route_add(ifname
, 1, "0.0.0.0", gw
, "0.0.0.0") == 1) && (i
--)) {
275 _dprintf("set default gateway=%s n=%d\n", gw
, i
);
277 /* Add routes to dns servers as well for demand ppp to work */
278 char word
[100], *next
;
279 in_addr_t mask
= inet_addr(netmask
);
280 foreach(word
, nvram_safe_get("wan_get_dns"), next
) {
281 if ((inet_addr(word
) & mask
) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask
))
282 route_add(ifname
, 0, word
, gw
, "255.255.255.255");
291 // -----------------------------------------------------------------------------
294 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
295 static void start_tmp_ppp(int num
, char *ifname
)
301 TRACE_PT("begin: num=%d\n", num
);
303 if (num
!= 0) return;
305 // Wait for ppp0 to be created
307 while ((ifconfig(ifname
, IFUP
, NULL
, NULL
) != 0) && (timeout
-- > 0)) {
309 _dprintf("[%d] waiting for %s %d...\n", __LINE__
, ifname
, timeout
);
312 if ((s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) return;
313 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
315 // Set temporary IP address
317 while (ioctl(s
, SIOCGIFADDR
, &ifr
) && timeout
--){
318 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
321 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr
.ifr_addr
))));
322 nvram_set("wan_netmask", "255.255.255.255");
324 // Set temporary P-t-P address
326 while (ioctl(s
, SIOCGIFDSTADDR
, &ifr
) && timeout
--){
327 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
330 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr
.ifr_dstaddr
))));
334 start_wan_done(ifname
);
338 void start_pppoe(int num
)
342 TRACE_PT("begin pppoe_num=%d\n", num
);
344 if (num
!= 0) return;
348 snprintf(ifname
, sizeof(ifname
), "ppp%d", num
);
350 if (config_pppd(WP_PPPOE
, num
) != 0)
355 if (nvram_get_int("ppp_demand"))
356 start_tmp_ppp(num
, ifname
);
358 ifconfig(ifname
, IFUP
, NULL
, NULL
);
363 void stop_pppoe(void)
369 void stop_singe_pppoe(int num
)
371 _dprintf("%s pppoe_num=%d\n", __FUNCTION__
, num
);
375 if (num
!= 0) return;
377 i
= nvram_get_int("pppoe_pid0");
378 if ((i
> 1) && (kill(i
, SIGTERM
) == 0)) {
381 } while (kill(i
, SIGKILL
) == 0);
384 unlink(ppp_linkfile
);
385 nvram_unset("pppoe_ifname0");
387 nvram_set("wan_get_dns", "");
392 // -----------------------------------------------------------------------------
394 inline void stop_l2tp(void)
399 void start_l2tp(void)
408 if (config_pppd(WP_L2TP
, 0) != 0)
411 demand
= nvram_get_int("ppp_demand");
413 /* Generate XL2TPD configuration file */
414 if ((fp
= fopen("/etc/xl2tpd.conf", "w")) == NULL
)
418 "access control = no\n"
422 "tx bps = 100000000\n"
425 "max redials = 32767\n"
426 "redial timeout = %d\n"
430 nvram_safe_get("l2tp_server_ip"),
432 demand
? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
433 nvram_get_int("debug_ppp") ? "yes" : "no",
434 nvram_safe_get("xl2tpd_custom"));
435 fappend(fp
, "/etc/xl2tpd.custom");
443 eval("listen", nvram_safe_get("lan_ifname"));
453 // -----------------------------------------------------------------------------
455 char *wan_gateway(void)
457 char *gw
= nvram_safe_get("wan_gateway_get");
458 if ((*gw
== 0) || (strcmp(gw
, "0.0.0.0") == 0))
459 gw
= nvram_safe_get("wan_gateway");
463 // -----------------------------------------------------------------------------
465 // trigger connect on demand
466 void force_to_dial(void)
471 switch (get_wan_proto()) {
473 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
476 eval("ping", "-c", "2", "10.112.112.112");
482 eval("ping", "-c", "2", wan_gateway());
489 // -----------------------------------------------------------------------------
491 static void _do_wan_routes(char *ifname
, char *nvname
, int metric
, int add
)
498 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
499 tmp
= routes
= strdup(nvram_safe_get(nvname
));
500 while (tmp
&& *tmp
) {
501 char *ipaddr
, *gateway
, *nmask
;
503 ipaddr
= nmask
= strsep(&tmp
, " ");
504 strcpy(netmask
, "255.255.255.255");
507 ipaddr
= strsep(&nmask
, "/");
508 if (nmask
&& *nmask
) {
509 bits
= strtol(nmask
, &nmask
, 10);
510 if (bits
>= 1 && bits
<= 32) {
511 mask
.s_addr
= htonl(0xffffffff << (32 - bits
));
512 strcpy(netmask
, inet_ntoa(mask
));
516 gateway
= strsep(&tmp
, " ");
518 if (gateway
&& *gateway
) {
520 route_add(ifname
, metric
, ipaddr
, gateway
, netmask
);
522 route_del(ifname
, metric
, ipaddr
, gateway
, netmask
);
528 void do_wan_routes(char *ifname
, int metric
, int add
)
530 if (nvram_get_int("dhcp_routes")) {
531 // Static Routes: IP ROUTER IP2 ROUTER2 ...
532 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
533 _do_wan_routes(ifname
, "wan_routes1", metric
, add
);
534 _do_wan_routes(ifname
, "wan_routes2", metric
, add
);
538 // -----------------------------------------------------------------------------
540 const char wan_connecting
[] = "/var/lib/misc/wan.connecting";
542 static int is_sta(int idx
, int unit
, int subunit
, void *param
)
546 if (nvram_match(wl_nvname("mode", unit
, subunit
), "sta")) {
547 *p
= nvram_safe_get(wl_nvname("ifname", unit
, subunit
));
553 void start_wan(int mode
)
566 f_write(wan_connecting
, NULL
, 0, 0, 0);
570 if (!foreach_wif(1, &p
, is_sta
)) {
571 p
= nvram_safe_get("wan_ifnameX");
572 set_mac(p
, "mac_wan", 1);
574 nvram_set("wan_ifname", p
);
575 nvram_set("wan_ifnames", p
);
579 wan_ifname
= nvram_safe_get("wan_ifname");
580 if (wan_ifname
[0] == 0) {
582 nvram_set("wan_ifname", wan_ifname
);
585 if (strcmp(wan_ifname
, "none") == 0) {
586 nvram_set("wan_proto", "disabled");
587 syslog(LOG_INFO
, "No WAN");
592 wan_proto
= get_wan_proto();
594 // set the default gateway for WAN interface
595 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
597 if (wan_proto
== WP_DISABLED
) {
598 start_wan_done(wan_ifname
);
602 if ((sd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) {
621 if (nvram_match("mtu_enable", "0")) {
625 mtu
= nvram_get_int("wan_mtu");
626 if (mtu
> max
) mtu
= max
;
627 else if (mtu
< 576) mtu
= 576;
629 sprintf(buf
, "%d", mtu
);
630 nvram_set("wan_mtu", buf
);
631 nvram_set("wan_run_mtu", buf
);
633 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
634 /* if (wan_proto == WP_PPTP) {
636 } */ // commented out; checkme -- zzz
638 if (wan_proto
!= WP_PPTP
&& wan_proto
!= WP_L2TP
&& wan_proto
!= WP_PPPOE
) {
639 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
641 strcpy(ifr
.ifr_name
, wan_ifname
);
642 ioctl(sd
, SIOCSIFMTU
, &ifr
);
647 ifconfig(wan_ifname
, IFUP
, NULL
, NULL
);
651 set_host_domain_name();
664 else if (wan_proto
!= WP_DHCP
) {
665 ifconfig(wan_ifname
, IFUP
, "0.0.0.0", NULL
);
666 ifconfig(wan_ifname
, IFUP
, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
668 p
= nvram_safe_get("wan_gateway");
669 if ((*p
!= 0) && (strcmp(p
, "0.0.0.0") != 0))
670 preset_wan(wan_ifname
, p
, nvram_safe_get("wan_netmask"));
683 nvram_set("wan_iface", wan_ifname
);
684 ifconfig(wan_ifname
, IFUP
, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
687 while ((!check_wanup()) && (r
-- > 0)) {
691 start_wan_done(wan_ifname
);
695 // Get current WAN hardware address
696 strlcpy(ifr
.ifr_name
, wan_ifname
, IFNAMSIZ
);
697 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) == 0) {
698 nvram_set("wan_hwaddr", ether_etoa(ifr
.ifr_hwaddr
.sa_data
, buf
));
701 /* Set initial QoS mode again now that WAN port is ready. */
708 led(LED_DIAG
, 0); // for 4712, 5325E (?)
709 led(LED_DMZ
, nvram_match("dmz_enable", "1"));
715 void start_wan6_done(const char *wan_ifname
)
717 struct in_addr addr4
;
718 struct in6_addr addr
;
719 static char addr6
[INET6_ADDRSTRLEN
];
721 int service
= get_ipv6_service();
723 if (service
!= IPV6_DISABLED
) {
724 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
725 accept_ra(wan_ifname
);
730 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname
, "metric", "2048");
732 case IPV6_NATIVE_DHCP
:
733 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname
);
737 case IPV6_ANYCAST_6TO4
:
740 if (service
== IPV6_ANYCAST_6TO4
) {
742 memset(&addr
, 0, sizeof(addr
));
743 inet_aton(get_wanip(), &addr4
);
744 addr
.s6_addr16
[0] = htons(0x2002);
745 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
746 addr
.s6_addr16
[3] = htons(0x0001);
747 inet_ntop(AF_INET6
, &addr
, addr6
, sizeof(addr6
));
748 nvram_set("ipv6_prefix", addr6
);
751 // FIXME: give it a few seconds for DAD completion
758 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
760 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
762 void start_wan_done(char *wan_ifname
)
770 TRACE_PT("begin wan_ifname=%s\n", wan_ifname
);
773 f_write("/var/lib/misc/wantime", &si
.uptime
, sizeof(si
.uptime
), 0, 0);
775 proto
= get_wan_proto();
777 // delete all default routes
778 route_del(wan_ifname
, 0, NULL
, NULL
, NULL
);
780 if (proto
!= WP_DISABLED
) {
781 // set default route to gateway if specified
784 if (proto
== WP_PPTP
&& !using_dhcpc()) {
785 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
786 if (*gw
== 0 || strcmp(gw
, "0.0.0.0") == 0) gw
= nvram_safe_get("ppp_get_ip");
789 if ((*gw
!= 0) && (strcmp(gw
, "0.0.0.0") != 0)) {
790 if (proto
== WP_DHCP
|| proto
== WP_STATIC
) {
791 // possibly gateway is over the bridge, try adding a route to gateway first
792 route_add(wan_ifname
, 0, gw
, NULL
, "255.255.255.255");
796 while ((route_add(wan_ifname
, 0, "0.0.0.0", gw
, "0.0.0.0") == 1) && (n
--)) {
799 _dprintf("set default gateway=%s n=%d\n", gw
, n
);
801 // hack: avoid routing cycles, when both peer and server have the same IP
802 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
803 // delete gateway route as it's no longer needed
804 route_del(wan_ifname
, 0, gw
, "0.0.0.0", "255.255.255.255");
808 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
810 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
811 if (proto == WP_STATIC)
815 unsigned char wan_mac[6];
817 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
818 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
823 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
824 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL
, "255.255.255.255");
825 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL
, "255.255.255.255");
827 if (proto
== WP_L2TP
) {
828 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
842 // and routes supplied via DHCP
843 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname
, 0, 1);
848 wanup
= check_wanup();
850 if ((wanup
) || (time(0) < Y2K
)) {
855 if ((wanup
) || (proto
== WP_DISABLED
)) {
865 start_wan6_done(get_wan6face());
876 notice_set("wan", "");
878 run_nvscript("script_wanup", NULL
, 0);
881 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
882 if (check_hw_type() == HW_BCM4702
) {
883 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
884 if (nvram_match("lan_stp", "1"))
885 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
887 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
888 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
889 if (nvram_match("lan1_stp", "1"))
890 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
892 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
893 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
894 if (nvram_match("lan2_stp", "1"))
895 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
897 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
898 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
899 if (nvram_match("lan3_stp", "1"))
900 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
908 #ifdef TCONFIG_USERPPTP
909 if (wanup
&& nvram_get_int("pptp_client_enable"))
913 unlink(wan_connecting
);
925 #ifdef TCONFIG_USERPPTP
935 stop_upnp(); //!!TB - moved from stop_services()
944 nvram_set("ipv6_get_dns", "");
947 /* Kill any WAN client daemons or callbacks */
953 nvram_set("wan_get_dns", "");
955 /* Bring down WAN interfaces */
956 foreach(name
, nvram_safe_get("wan_ifnames"), next
)
957 ifconfig(name
, 0, "0.0.0.0", NULL
);
960 //notice_set("wan", "");
961 unlink("/var/notice/wan");
962 unlink(wan_connecting
);