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/options";
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
);
155 fprintf(fp
, "nomppe nomppc\n");
156 if (nvram_get_int("mtu_enable"))
157 fprintf(fp
, "mtu %d\n", nvram_get_int("wan_mtu"));
164 "demand\n" // Dial on demand
166 "ipcp-accept-remote\n"
167 "ipcp-accept-local\n"
168 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
169 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
170 nvram_get_int("ppp_idletime") * 60);
174 switch (get_ipv6_service()) {
176 case IPV6_NATIVE_DHCP
:
177 fprintf(fp
, "+ipv6\n");
181 // User specific options
182 fprintf(fp
, "%s\n", nvram_safe_get("ppp_custom"));
191 static void stop_ppp(void)
195 unlink(ppp_linkfile
);
198 killall_tk("ip-down");
200 killall_tk("ipv6-up");
201 killall_tk("ipv6-down");
203 killall_tk("xl2tpd");
205 killall_tk("listen");
210 static void run_pppd(void)
214 if (nvram_get_int("ppp_demand")) {
217 Fixed issue id 7887(or 7787):
218 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
224 // Trigger Connect On Demand if user ping pptp server
225 eval("listen", nvram_safe_get("lan_ifname"));
233 // -----------------------------------------------------------------------------
235 inline void stop_pptp(void)
240 void start_pptp(int mode
)
244 if (!using_dhcpc()) stop_dhcpc();
247 if (config_pppd(WP_PPTP
, 0) != 0)
255 // -----------------------------------------------------------------------------
257 void preset_wan(char *ifname
, char *gw
, char *netmask
)
261 /* Delete all default routes */
262 route_del(ifname
, 0, NULL
, NULL
, NULL
);
264 /* try adding a route to gateway first */
265 route_add(ifname
, 0, gw
, NULL
, "255.255.255.255");
267 /* Set default route to gateway if specified */
269 while ((route_add(ifname
, 1, "0.0.0.0", gw
, "0.0.0.0") == 1) && (i
--)) {
272 _dprintf("set default gateway=%s n=%d\n", gw
, i
);
274 /* Add routes to dns servers as well for demand ppp to work */
275 char word
[100], *next
;
276 in_addr_t mask
= inet_addr(netmask
);
277 foreach(word
, nvram_safe_get("wan_get_dns"), next
) {
278 if ((inet_addr(word
) & mask
) != (inet_addr(nvram_safe_get("wan_ipaddr")) & mask
))
279 route_add(ifname
, 0, word
, gw
, "255.255.255.255");
288 // -----------------------------------------------------------------------------
291 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
292 static void start_tmp_ppp(int num
, char *ifname
)
298 TRACE_PT("begin: num=%d\n", num
);
300 if (num
!= 0) return;
302 // Wait for ppp0 to be created
304 while ((ifconfig(ifname
, IFUP
, NULL
, NULL
) != 0) && (timeout
-- > 0)) {
306 _dprintf("[%d] waiting for %s %d...\n", __LINE__
, ifname
, timeout
);
309 if ((s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) return;
310 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
312 // Set temporary IP address
314 while (ioctl(s
, SIOCGIFADDR
, &ifr
) && timeout
--){
315 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
318 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr
.ifr_addr
))));
319 nvram_set("wan_netmask", "255.255.255.255");
321 // Set temporary P-t-P address
323 while (ioctl(s
, SIOCGIFDSTADDR
, &ifr
) && timeout
--){
324 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
327 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr
.ifr_dstaddr
))));
331 start_wan_done(ifname
);
335 void start_pppoe(int num
)
339 TRACE_PT("begin pppoe_num=%d\n", num
);
341 if (num
!= 0) return;
345 snprintf(ifname
, sizeof(ifname
), "ppp%d", num
);
347 if (config_pppd(WP_PPPOE
, num
) != 0)
352 if (nvram_get_int("ppp_demand"))
353 start_tmp_ppp(num
, ifname
);
355 ifconfig(ifname
, IFUP
, NULL
, NULL
);
360 void stop_pppoe(void)
366 void stop_singe_pppoe(int num
)
368 _dprintf("%s pppoe_num=%d\n", __FUNCTION__
, num
);
372 if (num
!= 0) return;
374 i
= nvram_get_int("pppoe_pid0");
375 if ((i
> 1) && (kill(i
, SIGTERM
) == 0)) {
378 } while (kill(i
, SIGKILL
) == 0);
381 unlink(ppp_linkfile
);
382 nvram_unset("pppoe_ifname0");
384 nvram_set("wan_get_dns", "");
389 // -----------------------------------------------------------------------------
391 inline void stop_l2tp(void)
396 void start_l2tp(void)
405 if (config_pppd(WP_L2TP
, 0) != 0)
408 demand
= nvram_get_int("ppp_demand");
410 /* Generate XL2TPD configuration file */
411 if ((fp
= fopen("/etc/xl2tpd.conf", "w")) == NULL
)
415 "access control = no\n"
421 "max redials = 32767\n"
422 "redial timeout = %d\n"
424 nvram_safe_get("l2tp_server_ip"),
426 demand
? 30 : (nvram_get_int("ppp_redialperiod") ? : 30),
427 nvram_get_int("debug_ppp") ? "yes" : "no");
435 eval("listen", nvram_safe_get("lan_ifname"));
445 // -----------------------------------------------------------------------------
447 static char *wan_gateway(void)
449 char *gw
= nvram_safe_get("wan_gateway_get");
450 if ((*gw
== 0) || (strcmp(gw
, "0.0.0.0") == 0))
451 gw
= nvram_safe_get("wan_gateway");
455 // -----------------------------------------------------------------------------
457 // trigger connect on demand
458 void force_to_dial(void)
463 switch (get_wan_proto()) {
465 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
468 eval("ping", "-c", "2", "10.112.112.112");
474 eval("ping", "-c", "2", wan_gateway());
481 // -----------------------------------------------------------------------------
483 static void _do_wan_routes(char *ifname
, char *nvname
, int metric
, int add
)
490 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
491 tmp
= routes
= strdup(nvram_safe_get(nvname
));
492 while (tmp
&& *tmp
) {
493 char *ipaddr
, *gateway
, *nmask
;
495 ipaddr
= nmask
= strsep(&tmp
, " ");
496 strcpy(netmask
, "255.255.255.255");
499 ipaddr
= strsep(&nmask
, "/");
500 if (nmask
&& *nmask
) {
501 bits
= strtol(nmask
, &nmask
, 10);
502 if (bits
>= 1 && bits
<= 32) {
503 mask
.s_addr
= htonl(0xffffffff << (32 - bits
));
504 strcpy(netmask
, inet_ntoa(mask
));
508 gateway
= strsep(&tmp
, " ");
510 if (gateway
&& *gateway
) {
512 route_add(ifname
, metric
, ipaddr
, gateway
, netmask
);
514 route_del(ifname
, metric
, ipaddr
, gateway
, netmask
);
520 void do_wan_routes(char *ifname
, int metric
, int add
)
522 if (nvram_get_int("dhcp_routes")) {
523 // Static Routes: IP ROUTER IP2 ROUTER2 ...
524 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
525 _do_wan_routes(ifname
, "wan_routes1", metric
, add
);
526 _do_wan_routes(ifname
, "wan_routes2", metric
, add
);
530 // -----------------------------------------------------------------------------
532 const char wan_connecting
[] = "/var/lib/misc/wan.connecting";
534 static int is_sta(int idx
, int unit
, int subunit
, void *param
)
538 if (nvram_match(wl_nvname("mode", unit
, subunit
), "sta")) {
539 *p
= nvram_safe_get(wl_nvname("ifname", unit
, subunit
));
545 void start_wan(int mode
)
558 f_write(wan_connecting
, NULL
, 0, 0, 0);
562 if (!foreach_wif(1, &p
, is_sta
)) {
563 p
= nvram_safe_get("wan_ifnameX");
564 set_mac(p
, "mac_wan", 1);
566 nvram_set("wan_ifname", p
);
567 nvram_set("wan_ifnames", p
);
571 wan_ifname
= nvram_safe_get("wan_ifname");
572 if (wan_ifname
[0] == 0) {
574 nvram_set("wan_ifname", wan_ifname
);
577 if (strcmp(wan_ifname
, "none") == 0) {
578 nvram_set("wan_proto", "disabled");
579 syslog(LOG_INFO
, "No WAN");
584 wan_proto
= get_wan_proto();
586 // set the default gateway for WAN interface
587 nvram_set("wan_gateway_get", nvram_safe_get("wan_gateway"));
589 if (wan_proto
== WP_DISABLED
) {
590 start_wan_done(wan_ifname
);
594 if ((sd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) {
613 if (nvram_match("mtu_enable", "0")) {
617 mtu
= nvram_get_int("wan_mtu");
618 if (mtu
> max
) mtu
= max
;
619 else if (mtu
< 576) mtu
= 576;
621 sprintf(buf
, "%d", mtu
);
622 nvram_set("wan_mtu", buf
);
623 nvram_set("wan_run_mtu", buf
);
625 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
626 /* if (wan_proto == WP_PPTP) {
628 } */ // commented out; checkme -- zzz
630 if (wan_proto
!= WP_PPTP
&& wan_proto
!= WP_L2TP
&& wan_proto
!= WP_PPPOE
) {
631 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
633 strcpy(ifr
.ifr_name
, wan_ifname
);
634 ioctl(sd
, SIOCSIFMTU
, &ifr
);
639 ifconfig(wan_ifname
, IFUP
, NULL
, NULL
);
643 set_host_domain_name();
656 else if (wan_proto
!= WP_DHCP
) {
657 ifconfig(wan_ifname
, IFUP
, "0.0.0.0", NULL
);
658 ifconfig(wan_ifname
, IFUP
, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
660 p
= nvram_safe_get("wan_gateway");
661 if ((*p
!= 0) && (strcmp(p
, "0.0.0.0") != 0))
662 preset_wan(wan_ifname
, p
, nvram_safe_get("wan_netmask"));
675 nvram_set("wan_iface", wan_ifname
);
676 ifconfig(wan_ifname
, IFUP
, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
679 while ((!check_wanup()) && (r
-- > 0)) {
683 start_wan_done(wan_ifname
);
687 // Get current WAN hardware address
688 strlcpy(ifr
.ifr_name
, wan_ifname
, IFNAMSIZ
);
689 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) == 0) {
690 nvram_set("wan_hwaddr", ether_etoa(ifr
.ifr_hwaddr
.sa_data
, buf
));
693 /* Set initial QoS mode again now that WAN port is ready. */
700 led(LED_DIAG
, 0); // for 4712, 5325E (?)
701 led(LED_DMZ
, nvram_match("dmz_enable", "1"));
707 void start_wan6_done(const char *wan_ifname
)
709 struct in_addr addr4
;
710 struct in6_addr addr
;
711 static char addr6
[INET6_ADDRSTRLEN
];
713 int service
= get_ipv6_service();
715 if (service
!= IPV6_DISABLED
) {
716 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
717 accept_ra(wan_ifname
);
722 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname
, "metric", "2048");
724 case IPV6_NATIVE_DHCP
:
725 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname
);
729 case IPV6_ANYCAST_6TO4
:
732 if (service
== IPV6_ANYCAST_6TO4
) {
734 memset(&addr
, 0, sizeof(addr
));
735 inet_aton(get_wanip(), &addr4
);
736 addr
.s6_addr16
[0] = htons(0x2002);
737 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
738 addr
.s6_addr16
[3] = htons(0x0001);
739 inet_ntop(AF_INET6
, &addr
, addr6
, sizeof(addr6
));
740 nvram_set("ipv6_prefix", addr6
);
743 // FIXME: give it a few seconds for DAD completion
750 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
752 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
754 void start_wan_done(char *wan_ifname
)
762 TRACE_PT("begin wan_ifname=%s\n", wan_ifname
);
765 f_write("/var/lib/misc/wantime", &si
.uptime
, sizeof(si
.uptime
), 0, 0);
767 proto
= get_wan_proto();
769 // delete all default routes
770 route_del(wan_ifname
, 0, NULL
, NULL
, NULL
);
772 if (proto
!= WP_DISABLED
) {
773 // set default route to gateway if specified
776 if (proto
== WP_PPTP
&& !using_dhcpc()) {
777 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
778 if (*gw
== 0 || strcmp(gw
, "0.0.0.0") == 0) gw
= nvram_safe_get("ppp_get_ip");
781 if ((*gw
!= 0) && (strcmp(gw
, "0.0.0.0") != 0)) {
782 if (proto
== WP_DHCP
|| proto
== WP_STATIC
) {
783 // possibly gateway is over the bridge, try adding a route to gateway first
784 route_add(wan_ifname
, 0, gw
, NULL
, "255.255.255.255");
788 while ((route_add(wan_ifname
, 0, "0.0.0.0", gw
, "0.0.0.0") == 1) && (n
--)) {
791 _dprintf("set default gateway=%s n=%d\n", gw
, n
);
793 // hack: avoid routing cycles, when both peer and server have the same IP
794 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
795 // delete gateway route as it's no longer needed
796 route_del(wan_ifname
, 0, gw
, "0.0.0.0", "255.255.255.255");
800 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
802 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
803 if (proto == WP_STATIC)
807 unsigned char wan_mac[6];
809 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
810 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
815 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
816 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway_get"), NULL
, "255.255.255.255");
817 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL
, "255.255.255.255");
819 if (proto
== WP_L2TP
) {
820 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
832 // and routes supplied via DHCP
833 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname
, 0, 1);
838 wanup
= check_wanup();
840 if ((wanup
) || (time(0) < Y2K
)) {
845 if ((wanup
) || (proto
== WP_DISABLED
)) {
853 start_wan6_done(get_wan6face());
864 notice_set("wan", "");
866 run_nvscript("script_wanup", NULL
, 0);
869 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
870 if (check_hw_type() == HW_BCM4702
) {
871 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
872 if (nvram_match("lan_stp", "1")) eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
878 unlink(wan_connecting
);
880 new_qoslimit_start(); //!! RAF
881 new_arpbind_start(); //!! RAF
893 new_arpbind_stop(); //!! RAF
894 new_qoslimit_stop(); //!! RAF
897 stop_upnp(); //!!TB - moved from stop_services()
906 nvram_set("ipv6_get_dns", "");
909 /* Kill any WAN client daemons or callbacks */
915 nvram_set("wan_get_dns", "");
917 /* Bring down WAN interfaces */
918 foreach(name
, nvram_safe_get("wan_ifnames"), next
)
919 ifconfig(name
, 0, "0.0.0.0", NULL
);
922 //notice_set("wan", "");
923 unlink("/var/notice/wan");
924 unlink(wan_connecting
);