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>
46 #define IFUP (IFF_UP | IFF_RUNNING | IFF_BROADCAST | IFF_MULTICAST)
47 #define sin_addr(s) (((struct sockaddr_in *)(s))->sin_addr)
49 static const char ppp_linkfile
[] = "/tmp/ppp/link";
50 static const char ppp_optfile
[] = "/tmp/ppp/options";
52 static void make_secrets(void)
58 user
= nvram_safe_get("ppp_username");
59 pass
= nvram_safe_get("ppp_passwd");
60 if ((f
= fopen("/tmp/ppp/pap-secrets", "w")) != NULL
) {
61 fprintf(f
, "\"%s\" * \"%s\" *\n", user
, pass
);
64 chmod("/tmp/ppp/pap-secrets", 0600);
66 if ((f
= fopen("/tmp/ppp/chap-secrets", "w")) != NULL
) {
67 fprintf(f
, "\"%s\" * \"%s\" *\n", user
, pass
);
70 chmod("/tmp/ppp/chap-secrets", 0600);
73 // -----------------------------------------------------------------------------
75 static int config_pppd(int wan_proto
)
81 mkdir("/tmp/ppp", 0777);
82 symlink("/sbin/rc", "/tmp/ppp/ip-up");
83 symlink("/sbin/rc", "/tmp/ppp/ip-down");
84 symlink("/dev/null", "/tmp/ppp/connect-errors");
86 // Generate options file
87 if ((fp
= fopen(ppp_optfile
, "w")) == NULL
) {
93 "defaultroute\n" // Add a default route to the system routing tables, using the peer as the gateway
94 "usepeerdns\n" // Ask the peer for up to 2 DNS server addresses
96 "default-asyncmap\n" // Disable asyncmap negotiation
97 "nopcomp\n" // Disable protocol field compression
98 "noaccomp\n" // Disable Address/Control compression
99 "novj\n" // Disable Van Jacobson style TCP/IP header compression
100 "nobsdcomp\n" // Disable BSD-Compress compression
101 "nodeflate\n" // Disable Deflate compression
102 "noauth\n" // Do not authenticate peer
103 "refuse-eap\n" // Do not use eap
105 "lcp-echo-interval %d\n"
106 "lcp-echo-failure %d\n"
108 nvram_safe_get("ppp_username"),
109 nvram_get_int("pppoe_lei") ? : 10,
110 nvram_get_int("pppoe_lef") ? : 5,
111 nvram_get_int("debug_ppp") ? "debug\n" : "");
113 if (wan_proto
!= WP_L2TP
)
114 fprintf(fp
, "persist\n");
123 nvram_safe_get("pptp_server_ip"),
124 nvram_get_int("mtu_enable") ? nvram_get_int("wan_mtu") : 1400);
126 default: // l2tp, pppoe
127 fprintf(fp
, "nomppe nomppc\n");
128 if (nvram_get_int("mtu_enable"))
129 fprintf(fp
, "mtu %s\n", nvram_safe_get("wan_mtu"));
133 if (nvram_match("ppp_demand", "1")) {
136 "demand\n" // Dial on demand
138 "ipcp-accept-remote\n"
139 "ipcp-accept-local\n"
140 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
141 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
142 nvram_get_int("ppp_idletime") * 60);
145 // User specific options
146 fprintf(fp
, "%s\n", nvram_safe_get("ppp_custom"));
155 static void stop_ppp(void)
159 unlink(ppp_linkfile
);
162 killall_tk("ip-down");
163 killall_tk("xl2tpd");
165 killall_tk("listen");
170 // -----------------------------------------------------------------------------
172 inline void stop_pptp(void)
177 void start_pptp(int mode
)
181 if (!using_dhcpc()) stop_dhcpc();
185 if (config_pppd(WP_PPTP
) != 0)
188 if (!using_dhcpc()) {
189 // Bring up WAN interface
190 ifconfig(nvram_safe_get("wan_ifname"), IFUP
,
191 nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
196 if (nvram_get_int("ppp_demand")) {
199 Fixed issue id 7887(or 7787):
200 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
206 // Trigger Connect On Demand if user ping pptp server
207 eval("listen", nvram_safe_get("lan_ifname"));
217 // -----------------------------------------------------------------------------
220 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
221 static void start_tmp_ppp(int num
)
228 TRACE_PT("begin: num=%d\n", num
);
230 if (num
!= 0) return;
232 // Wait for ppp0 to be created
234 while ((ifconfig(ifname
= nvram_safe_get("pppoe_ifname0"), IFUP
, NULL
, NULL
) != 0) && (timeout
-- > 0)) {
236 _dprintf("[%d] waiting for %s %d...\n", __LINE__
, ifname
, timeout
);
239 if ((s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) return;
240 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
242 // Set temporary IP address
244 while (ioctl(s
, SIOCGIFADDR
, &ifr
) && timeout
--){
245 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
248 nvram_set("wan_ipaddr", inet_ntoa(sin_addr(&(ifr
.ifr_addr
))));
249 nvram_set("wan_netmask", "255.255.255.255");
251 // Set temporary P-t-P address
253 while (ioctl(s
, SIOCGIFDSTADDR
, &ifr
) && timeout
--){
254 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
257 nvram_set("wan_gateway", inet_ntoa(sin_addr(&(ifr
.ifr_dstaddr
))));
261 start_wan_done(ifname
);
265 void start_pppoe(int num
)
270 char lcp_echo_interval
[16];
271 char lcp_echo_fails
[16];
276 TRACE_PT("begin pppoe_num=%d\n", num
);
278 if (num
!= 0) return;
282 nvram_set("pppoe_ifname0", "");
284 dod
= nvram_match("ppp_demand", "1");
287 sprintf(idle
, "%d", dod
? (nvram_get_int("ppp_idletime") * 60) : 0);
290 sprintf(retry
, "%d", (nvram_get_int("ppp_redialperiod") / 5) - 1);
293 mtu
= nvram_safe_get("wan_mtu");
295 // [-I n] Interval between LCP echo-requests
296 sprintf(lcp_echo_interval
, "%d", ((n
= nvram_get_int("pppoe_lei")) > 0) ? n
: 30);
298 // [-T n] Tolerance to unanswered echo-requests
299 sprintf(lcp_echo_fails
, "%d", ((n
= nvram_get_int("pppoe_lef")) > 0) ? n
: 5);
301 char *pppoe_argv
[] = {
303 nvram_safe_get("wan_ifname"),
304 "-u", nvram_safe_get("ppp_username"),
305 "-p", nvram_safe_get("ppp_passwd"),
308 "-i", idle
, // >0 == dial on demand
309 "-I", lcp_echo_interval
, // Send an LCP echo-request frame to the server every X seconds
310 "-N", retry
, // To avoid kill pppd when pppd has been connecting.
311 "-T", lcp_echo_fails
, // pppd will presume the server to be dead if 3 LCP echo-requests are sent without receiving a valid LCP echo-reply
312 "-P", "0", // PPPOE session number.
313 "-C", "pppoe_down", // by tallest 0407
314 "-R", // set default route
316 NULL
, NULL
, // pppoe_service
317 NULL
, NULL
, // pppoe_ac
318 NULL
, NULL
, // static IP
319 NULL
, // pppoe_keepalive
320 NULL
, // -x extended logging
326 for (arg
= pppoe_argv
; *arg
; arg
++) {
330 if (nvram_get_int("debug_ppp")) {
331 *arg
++ = "-d"; // debug mode; compile ppp w/ -DDEBUG !
334 if (((p
= nvram_get("ppp_service")) != NULL
) && (*p
!= 0)) {
340 if (((p
= nvram_get("ppp_ac")) != NULL
) && (*p
!= 0)) {
345 if (nvram_match("ppp_static", "1")) {
347 *arg
++ = nvram_safe_get("ppp_static_ip");
351 //if (nvram_match("pppoe_demand", "1") || nvram_match("pppoe_keepalive", "1"))
354 if (nvram_contains_word("log_events", "pppoe")) *arg
++ = "-x";
356 mkdir("/tmp/ppp", 0777);
359 symlink("/sbin/rc", "/tmp/ppp/ip-up");
360 symlink("/sbin/rc", "/tmp/ppp/ip-down");
361 symlink("/sbin/rc", "/tmp/ppp/set-pppoepid"); // tallest 1219
363 rename("/tmp/ppp/log", "/tmp/ppp/log.~");
365 _eval(pppoe_argv
, NULL
, 0, &pid
);
367 if (dod
) start_tmp_ppp(num
);
372 void stop_pppoe(void)
376 unlink(ppp_linkfile
);
377 nvram_unset("pppoe_ifname0");
378 killall_tk("pppoecd");
380 killall_tk("ip-down");
385 void stop_singe_pppoe(int num
)
387 _dprintf("%s pppoe_num=%d\n", __FUNCTION__
, num
);
391 if (num
!= 0) return;
393 i
= nvram_get_int("pppoe_pid0");
394 if ((i
> 1) && (kill(i
, SIGTERM
) == 0)) {
397 } while (kill(i
, SIGKILL
) == 0);
400 unlink(ppp_linkfile
);
401 nvram_unset("pppoe_ifname0");
403 nvram_set("wan_get_dns", "");
407 // -----------------------------------------------------------------------------
409 inline void stop_l2tp(void)
414 void start_l2tp(void)
423 if (config_pppd(WP_L2TP
) != 0)
426 /* Generate XL2TPD configuration file */
427 if ((fp
= fopen("/etc/xl2tpd.conf", "w")) == NULL
)
431 "access control = no\n"
437 "max redials = 32767\n"
438 "redial timeout = %d\n"
440 nvram_safe_get("l2tp_server_ip"),
442 nvram_get_int("ppp_redialperiod") ? : 30,
443 nvram_get_int("debug_ppp") ? "yes" : "no");
450 if (nvram_match("ppp_demand", "1")) {
451 eval("listen", nvram_safe_get("lan_ifname"));
461 // -----------------------------------------------------------------------------
463 // trigger connect on demand
464 void force_to_dial(void)
469 switch (get_wan_proto()) {
471 f_write_string("/var/run/l2tp-control", "c l2tp", 0, 0);
474 eval("ping", "-c", "2", "10.112.112.112");
480 eval("ping", "-c", "2", nvram_safe_get("wan_gateway"));
487 // -----------------------------------------------------------------------------
489 static void _do_wan_routes(char *ifname
, char *nvname
, int metric
, int add
)
496 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
497 tmp
= routes
= strdup(nvram_safe_get(nvname
));
498 while (tmp
&& *tmp
) {
499 char *ipaddr
, *gateway
, *nmask
;
501 ipaddr
= nmask
= strsep(&tmp
, " ");
502 strcpy(netmask
, "255.255.255.255");
505 ipaddr
= strsep(&nmask
, "/");
506 if (nmask
&& *nmask
) {
507 bits
= strtol(nmask
, &nmask
, 10);
508 if (bits
>= 1 && bits
<= 32) {
509 mask
.s_addr
= htonl(0xffffffff << (32 - bits
));
510 strcpy(netmask
, inet_ntoa(mask
));
514 gateway
= strsep(&tmp
, " ");
516 if (gateway
&& *gateway
) {
517 if (add
) route_add(ifname
, metric
+ 1, ipaddr
, gateway
, netmask
);
518 else route_del(ifname
, metric
+ 1, ipaddr
, gateway
, netmask
);
524 void do_wan_routes(char *ifname
, int metric
, int add
)
526 if (nvram_get_int("dhcp_routes")) {
527 // Static Routes: IP ROUTER IP2 ROUTER2 ...
528 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
529 _do_wan_routes(ifname
, "wan_routes1", metric
, add
);
530 _do_wan_routes(ifname
, "wan_routes2", metric
, add
);
534 // -----------------------------------------------------------------------------
536 const char wan_connecting
[] = "/var/lib/misc/wan.connecting";
538 void start_wan(int mode
)
551 f_write(wan_connecting
, NULL
, 0, 0, 0);
555 if (nvram_match("wl_mode", "sta")) {
556 p
= nvram_safe_get("wl_ifname");
559 p
= nvram_safe_get("wan_ifnameX");
560 set_mac(p
, "mac_wan", 1);
562 nvram_set("wan_ifname", p
);
563 nvram_set("wan_ifnames", p
);
567 wan_ifname
= nvram_safe_get("wan_ifname");
568 if (wan_ifname
[0] == 0) {
570 nvram_set("wan_ifname", wan_ifname
);
573 if (strcmp(wan_ifname
, "none") == 0) {
574 nvram_set("wan_proto", "disabled");
575 syslog(LOG_INFO
, "No WAN");
580 wan_proto
= get_wan_proto();
582 if (wan_proto
== WP_DISABLED
) {
583 start_wan_done(wan_ifname
);
587 if ((sd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) {
606 if (nvram_match("mtu_enable", "0")) {
610 mtu
= nvram_get_int("wan_mtu");
611 if (mtu
> max
) mtu
= max
;
612 else if (mtu
< 576) mtu
= 576;
614 sprintf(buf
, "%d", mtu
);
615 nvram_set("wan_mtu", buf
);
616 nvram_set("wan_run_mtu", buf
);
618 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
619 /* if (wan_proto == WP_PPTP) {
621 } */ // commented out; checkme -- zzz
623 if ((wan_proto
!= WP_PPTP
&& wan_proto
!= WP_L2TP
) || nvram_get_int("mtu_enable")) {
625 strcpy(ifr
.ifr_name
, wan_ifname
);
626 ioctl(sd
, SIOCSIFMTU
, &ifr
);
631 ifconfig(wan_ifname
, IFUP
, NULL
, NULL
);
633 set_host_domain_name();
638 if (nvram_invmatch("ppp_demand", "1")) {
639 if (mode
!= REDIAL
) start_redial();
649 else if (wan_proto
== WP_PPTP
) {
654 nvram_set("wan_iface", wan_ifname
);
655 ifconfig(wan_ifname
, IFUP
, nvram_safe_get("wan_ipaddr"), nvram_safe_get("wan_netmask"));
658 while ((!check_wanup()) && (r
-- > 0)) {
662 start_wan_done(wan_ifname
);
666 // Get current WAN hardware address
667 strlcpy(ifr
.ifr_name
, wan_ifname
, IFNAMSIZ
);
668 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) == 0) {
669 nvram_set("wan_hwaddr", ether_etoa(ifr
.ifr_hwaddr
.sa_data
, buf
));
672 /* Set initial QoS mode again now that WAN port is ready. */
679 led(LED_DIAG
, 0); // for 4712, 5325E (?)
680 led(LED_DMZ
, nvram_match("dmz_enable", "1"));
686 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
688 // wan_iface: ppp# (PPPOE, PPTP, L2TP), vlan1 (DHCP, HB, Static)
691 void start_wan_done(char *wan_ifname
)
701 TRACE_PT("begin wan_ifname=%s\n", wan_ifname
);
704 f_write("/var/lib/misc/wantime", &si
.uptime
, sizeof(si
.uptime
), 0, 0);
706 proto
= get_wan_proto();
707 dod
= nvram_match("ppp_demand", "1");
711 while (route_del(nvram_safe_get("wan_ifname"), 0, NULL
, NULL
, NULL
) == 0) {
717 // delete all default routes
718 while (route_del(wan_ifname
, 0, NULL
, NULL
, NULL
) == 0) {
722 if (proto
!= WP_DISABLED
) {
723 // set default route to gateway if specified
724 gw
= (proto
== WP_PPTP
&& !using_dhcpc()) ? nvram_safe_get("ppp_get_ip") : nvram_safe_get("wan_gateway");
725 if ((*gw
!= 0) && (strcmp(gw
, "0.0.0.0") != 0)) {
726 if (proto
== WP_DHCP
|| proto
== WP_STATIC
) {
727 // possibly gateway is over the bridge, try adding a route to gateway first
728 route_add(wan_ifname
, 0, gw
, NULL
, "255.255.255.255");
732 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
733 if (nvram_get_int("ppp_defgw") || !using_dhcpc())
738 // we are not using default gateway on remote network,
739 // add route to the vpn subnet
740 char *netmask
= nvram_safe_get("wan_netmask");
741 struct in_addr net
, mask
;
742 if (strcmp(netmask
, "0.0.0.0") == 0 || !inet_aton(netmask
, &mask
)) {
743 netmask
= "255.255.255.0";
744 inet_aton(netmask
, &mask
);
746 if (inet_aton(gw
, &net
)) {
747 net
.s_addr
&= mask
.s_addr
;
748 route_add(wan_ifname
, 0, inet_ntoa(net
), gw
, netmask
);
750 // add routes to dns servers
751 char word
[100], *next
;
752 foreach(word
, nvram_safe_get("wan_get_dns"), next
) {
753 route_add(wan_ifname
, 0, word
, gw
, "255.255.255.255");
760 while ((route_add(wan_ifname
, metric
, "0.0.0.0", gw
, "0.0.0.0") == 1) && (n
--)) {
763 _dprintf("set default gateway=%s n=%d\n", gw
, n
);
765 // hack: avoid routing cycles, when both peer and server have the same IP
766 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
767 // delete gateway route as it's no longer needed
768 route_del(wan_ifname
, 0, gw
, "0.0.0.0", "255.255.255.255");
772 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
774 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
775 if (proto == WP_STATIC)
779 unsigned char wan_mac[6];
781 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
782 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
787 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
788 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip
789 route_del(nvram_safe_get("wan_iface"), 0, nvram_safe_get("wan_gateway"), NULL
, "255.255.255.255");
790 route_add(nvram_safe_get("wan_iface"), 0, nvram_safe_get("ppp_get_ip"), NULL
, "255.255.255.255");
792 if (proto
== WP_L2TP
) {
793 route_add(nvram_safe_get("wan_ifname"), 0, nvram_safe_get("l2tp_server_ip"), nvram_safe_get("wan_gateway_buf"), "255.255.255.255"); // fixed routing problem in Israel by kanki
802 new_qoslimit_start();
809 // and routes supplied via DHCP
810 do_wan_routes(using_dhcpc() ? nvram_safe_get("wan_ifname") : wan_ifname
, 0, 1);
815 wanup
= check_wanup();
817 if ((wanup
) || (time(0) < Y2K
)) {
822 if ((wanup
) || (proto
== WP_DISABLED
)) {
832 notice_set("wan", "");
834 run_nvscript("script_wanup", NULL
, 0);
837 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
838 if (check_hw_type() == HW_BCM4702
) {
839 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
840 if (nvram_match("lan_stp", "1")) eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
846 unlink(wan_connecting
);
861 stop_upnp(); //!!TB - moved from stop_services()
866 /* Kill any WAN client daemons or callbacks */
867 stop_singe_pppoe(PPPOE0
);
872 nvram_set("wan_get_dns", "");
874 /* Bring down WAN interfaces */
875 foreach(name
, nvram_safe_get("wan_ifnames"), next
)
876 ifconfig(name
, 0, "0.0.0.0", NULL
);
879 //notice_set("wan", "");
880 unlink("/var/notice/wan");
881 unlink(wan_connecting
);