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 #define mwanlog(level,x...) if(nvram_get_int("mwan_debug")>=level) syslog(level, x)
47 static void make_secrets(char *prefix
) //static void make_secrets(void)
53 char secrets_file
[256];
56 user
= nvram_safe_get(strcat_r(prefix
, "_ppp_username", tmp
)); //"ppp_username" -> strcat_r(prefix, "_ppp_username", tmp)
57 pass
= nvram_safe_get(strcat_r(prefix
, "_ppp_passwd", tmp
)); //"ppp_passwd" -> strcat_r(prefix, "_ppp_passwd", tmp)
59 memset(secrets_file
, 0, 256);
60 sprintf(secrets_file
, "/tmp/ppp/%s_pap-secrets", prefix
);
61 if ((f
= fopen(secrets_file
, "w")) != NULL
) {
62 fprintf(f
, "\"%s\" * \"%s\" *\n", user
, pass
);
65 chmod(secrets_file
, 0600);
67 memset(secrets_file
, 0, 256);
68 sprintf(secrets_file
, "/tmp/ppp/%s_chap-secrets", prefix
);
69 if ((f
= fopen(secrets_file
, "w")) != NULL
) {
70 fprintf(f
, "\"%s\" * \"%s\" *\n", user
, pass
);
73 chmod(secrets_file
, 0600);
76 // -----------------------------------------------------------------------------
78 static int config_pppd(int wan_proto
, int num
, char *prefix
) //static int config_pppd(int wan_proto, int num)
87 char ppp_optfile
[256];
90 mkdir("/tmp/ppp", 0777);
91 symlink("/sbin/rc", "/tmp/ppp/ip-up");
92 symlink("/sbin/rc", "/tmp/ppp/ip-down");
94 symlink("/sbin/rc", "/tmp/ppp/ipv6-up");
95 symlink("/sbin/rc", "/tmp/ppp/ipv6-down");
97 symlink("/dev/null", "/tmp/ppp/connect-errors");
99 demand
= nvram_get_int(strcat_r(prefix
, "_ppp_demand", tmp
)); //"ppp_demand" -> strcat_r(prefix, "_ppp_demand", tmp)
101 // Generate options file
102 memset(ppp_optfile
, 0, 256);
103 sprintf(ppp_optfile
, "/tmp/ppp/%s_options", prefix
);
104 if ((fp
= fopen(ppp_optfile
, "w")) == NULL
) {
111 char ppp3g_chatfile
[256];
112 memset(ppp3g_chatfile
, 0, 256);
113 sprintf(ppp3g_chatfile
, "/tmp/ppp/%s_connect.chat", prefix
);
114 if (nvram_match(strcat_r(prefix
, "_proto", tmp
), "ppp3g") ) { //"_proto" -> strcat_r(prefix, "_proto", tmp)
118 "connect \"/usr/sbin/chat -V -t 60 -f %s\"\n"
123 "ipcp-accept-local\n",
124 nvram_safe_get(strcat_r(prefix
, "_modem_dev", tmp
)), //"modem_dev" -> strcat_r(prefix, "_modem_dev", tmp)
127 if (strlen(nvram_get(strcat_r(prefix
, "_ppp_username", tmp
))) >0 ) //if (strlen(nvram_get("ppp_username")) >0 )
128 fprintf(fp
, "user \"%s\"\n", nvram_get(strcat_r(prefix
, "_ppp_username", tmp
)));// "ppp_username" -> strcat_r(prefix, "_ppp_username", tmp)
129 if (strlen(nvram_get(strcat_r(prefix
, "_ppp_passwd", tmp
))) >0 )
130 fprintf(fp
, "password \"%s\"\n", nvram_get(strcat_r(prefix
, "_ppp_passwd", tmp
)));
131 fprintf(fp
, "linkname %s\n", prefix
); // link name for WAN ID
138 "password \"%s\"\n" // Don't rely on pap/chap secrets (useless)
139 "linkname %s\n" // link name for WAN ID
140 "lcp-echo-adaptive\n", // Suppress LCP echo-requests if traffic was received
142 nvram_safe_get(strcat_r(prefix
, "_ppp_username", tmp
)), //"ppp_usrename" -> strcat_r(prefix, "_ppp_username", tmp
143 nvram_safe_get(strcat_r(prefix
, "_ppp_passwd", tmp
)), //"ppp_passwd" -> strcat_r(prefix, "_ppp_passwd", tmp
144 prefix
); // link name for WAN ID
151 "defaultroute\n" // Add a default route to the system routing tables, using the peer as the gateway
152 "usepeerdns\n" // Ask the peer for up to 2 DNS server addresses
153 "default-asyncmap\n" // Disable asyncmap negotiation
154 "novj\n" // Disable Van Jacobson style TCP/IP header compression
155 "nobsdcomp\n" // Disable BSD-Compress compression
156 "nodeflate\n" // Disable Deflate compression
157 "noauth\n" // Do not authenticate peer
158 "refuse-eap\n" // Do not use eap
159 "maxfail 0\n" // Never give up
160 "lcp-echo-interval %d\n"// Interval between LCP echo-requests
161 "lcp-echo-failure %d\n" // Tolerance to unanswered echo-requests
163 nvram_get_int(strcat_r(prefix
, "_pppoe_lei", tmp
)) ? : 10, //"pppoe_lei" -> strcat_r(prefix, "_pppoe_lei", tmp)
164 nvram_get_int(strcat_r(prefix
, "_pppoe_lef", tmp
)) ? : 5, //"pppoe_lef" -> strcat_r(prefix, "_pppoe_lef", tmp)
165 nvram_get_int("debug_ppp") ? "debug\n" : ""); //"debug_ppp"
169 if (nvram_match(strcat_r(prefix
, "_wan_proto", tmp
), "ppp3g") && nvram_match(strcat_r(prefix
, "_modem_dev", tmp
), "ttyACM0") ) { //"wan_proto", "modem_dev"
170 //don`t write nopcomp and noaccomp options
175 "nopcomp\n" // Disable protocol field compression
176 "noaccomp\n" // Disable Address/Control compression
184 if (wan_proto
!= WP_L2TP
) {
188 demand
? 30 : (nvram_get_int(strcat_r(prefix
, "ppp_redialperiod", tmp
)) ? : 30)); //"ppp_redialperiod"
197 "require-mschap-v2\n"
198 "noauth\n" // No authenticate peer (i dunno why it doesn't apply from shared params)
200 nvram_safe_get(strcat_r(prefix
, "_pptp_server_ip", tmp
)), //"pptp_server_ip"
201 nvram_get_int(strcat_r(prefix
, "_mtu_enable", tmp
)) ? nvram_get_int(strcat_r(prefix
, "_wan_mtu", tmp
)) : 1400);
202 //"mtu_enable", "wan_mtu"
207 "plugin rp-pppoe.so\n"
211 // nvram_safe_get(strcat_r(prefix, "_ppp_passwd", tmp)), //"ppp_passwd"
212 nvram_safe_get(strcat_r(prefix
, "_ifname", tmp
)), //"wan_ifname"
213 nvram_get_int(strcat_r(prefix
, "_mtu", tmp
)),
214 nvram_get_int(strcat_r(prefix
, "_mtu", tmp
))); //"wan_mtu
215 if (((p
= nvram_get(strcat_r(prefix
, "_ppp_service", tmp
))) != NULL
) && (*p
)) { //"ppp_service"
216 fprintf(fp
, "rp_pppoe_service '%s'\n", p
);
218 if (((p
= nvram_get(strcat_r(prefix
, "_ppp_ac", tmp
))) != NULL
) && (*p
)) { //"ppp_ac"
219 fprintf(fp
, "rp_pppoe_ac '%s'\n", p
);
221 if (nvram_match(strcat_r(prefix
, "_ppp_mlppp", tmp
), "1")) { //"ppp_mlppp"
228 memset(ppp3g_chatfile
, 0, 256);
229 sprintf(ppp3g_chatfile
, "/tmp/ppp/%s_connect.chat", prefix
);
230 if ((cfp
= fopen(ppp3g_chatfile
, "w")) == NULL
) {
231 perror(ppp3g_chatfile
);
235 "ABORT \"NO CARRIER\"\n"
236 "ABORT \"NO DIALTONE\"\n"
237 "ABORT \"NO ERROR\"\n"
238 "ABORT \"NO ANSWER\"\n"
242 /* moved to switch3g script
243 if (strlen(nvram_get("modem_pin")) >0 ) {
246 "OK \"AT+CPIN=%s\"\n"
248 nvram_get("modem_pin"));
252 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
256 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
260 /* Only send the AT+CGDCONT (define PDP context) command to set
261 * the APN if modem_apn is defined and non-empty. Some ISPs
262 * (ex. BSNL EVDO in India) don't need this (the modem returns
265 if (((p
= nvram_safe_get(strcat_r(prefix
, "_modem_apn", tmp
))) != NULL
) && (*p
)) {
266 fprintf(cfp
, "OK 'AT+CGDCONT=1,\"IP\",\"%s\"'\n", p
);
272 nvram_safe_get(strcat_r(prefix
, "_modem_init", tmp
)) //"modem_init"
277 if (nvram_match("usb_3g", "1") && nvram_match(strcat_r(prefix
, "_proto", tmp
), "ppp3g")) {
279 if (strlen(nvram_get(strcat_r(prefix
, "_gateway", tmp
))) >0 ) { //"wan_gateway"
280 nvram_set(strcat_r(prefix
, "_gateway", tmp
), "");
284 xstart("switch3g", prefix
);
290 fprintf(fp
, "nomppe nomppc\n");
291 if (nvram_get_int(strcat_r(prefix
, "_mtu_enable", tmp
))) //"mtu_enable"
292 fprintf(fp
, "mtu %d\n", nvram_get_int(strcat_r(prefix
, "_mtu", tmp
))); //"wan_mtu"
299 "demand\n" // Dial on demand
301 "ipcp-accept-remote\n"
302 "ipcp-accept-local\n"
303 "noipdefault\n" // Disables the default behaviour when no local IP address is specified
304 "ktune\n", // Set /proc/sys/net/ipv4/ip_dynaddr to 1 in demand mode if the local address changes
305 nvram_get_int(strcat_r(prefix
, "_ppp_idletime", tmp
)) * 60); //"ppp_idletime"
309 switch (get_ipv6_service()) {
311 case IPV6_NATIVE_DHCP
:
312 fprintf(fp
, "+ipv6\n");
316 // User specific options
317 fprintf(fp
, "%s\n", nvram_safe_get(strcat_r(prefix
, "_ppp_custom", tmp
))); //"ppp_custom"
320 // make_secrets(prefix);
326 static void stop_ppp(char *prefix
)
329 char ppp_linkfile
[256];
333 memset(ppp_linkfile
, 0, 256);
334 sprintf(ppp_linkfile
, "/tmp/ppp/%s_link", prefix
);
335 memset(pppd_name
, 0, 256);
336 sprintf(pppd_name
, "pppd%s", prefix
);
338 unlink(ppp_linkfile
);
341 killall_tk("ip-down");
343 killall_tk("ipv6-up");
344 killall_tk("ipv6-down");
346 killall_tk("xl2tpd");
347 //kill(nvram_get_int(strcat_r(prefix, "_pppd_pid", tmp)),1);
348 killall_tk((char *)pppd_name
);
349 killall_tk("listen");
354 static void run_pppd(char *prefix
)
359 memset(pppd_path
, 0, 256);
360 sprintf(pppd_path
, "/tmp/ppp/pppd%s", prefix
);
361 char ppp_optfile
[256];
362 memset(ppp_optfile
, 0, 256);
363 sprintf(ppp_optfile
, "/tmp/ppp/%s_options", prefix
);
364 symlink("/usr/sbin/pppd", pppd_path
);
365 eval(pppd_path
, "file", ppp_optfile
);
367 if (nvram_get_int(strcat_r(prefix
, "_ppp_demand", tmp
))) { //"ppp_demand"
370 Fixed issue id 7887(or 7787):
371 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
377 // Trigger Connect On Demand if user ping pptp server
378 eval("listen", nvram_safe_get("lan_ifname"), prefix
);
382 start_redial(prefix
);
386 // -----------------------------------------------------------------------------
388 inline void stop_pptp(char *prefix
)
393 void start_pptp(int mode
, char *prefix
)
398 if (!using_dhcpc(prefix
)) stop_dhcpc(prefix
);
401 if (config_pppd(WP_PPTP
, 0, prefix
) != 0)
409 // -----------------------------------------------------------------------------
411 void preset_wan(char *ifname
, char *gw
, char *netmask
, char *prefix
)
416 /* Delete all default routes */
417 route_del(ifname
, 0, NULL
, NULL
, NULL
);
419 /* try adding a route to gateway first */
420 route_add(ifname
, 0, gw
, NULL
, "255.255.255.255");
422 /* Set default route to gateway if specified */
424 while ((route_add(ifname
, 1, "0.0.0.0", gw
, "0.0.0.0") == 1) && (i
--)) {
427 _dprintf("set default gateway=%s n=%d\n", gw
, i
);
429 /* Add routes to dns servers as well for demand ppp to work */
430 char word
[100], *next
;
431 in_addr_t mask
= inet_addr(netmask
);
432 foreach(word
, nvram_safe_get(strcat_r(prefix
, "_get_dns", tmp
)), next
) { //"wan_get_dns"
433 if ((inet_addr(word
) & mask
) != (inet_addr(nvram_safe_get(strcat_r(prefix
, "_ipaddr", tmp
))) & mask
)) //"wan_ipaddr"
434 route_add(ifname
, 0, word
, gw
, "255.255.255.255");
436 if(!strcmp(prefix
,"wan")){
444 // -----------------------------------------------------------------------------
447 // Get the IP, Subnetmask, Geteway from WAN interface and set nvram
448 static void start_tmp_ppp(int num
, char *ifname
, char *prefix
)
456 TRACE_PT("begin: num=%d\n", num
);
458 if (num
!= 0) return;
460 // Wait for ppp0 to be created
462 while ((ifconfig(ifname
, IFUP
, NULL
, NULL
) != 0) && (timeout
-- > 0)) {
464 _dprintf("[%d] waiting for %s %d...\n", __LINE__
, ifname
, timeout
);
467 if ((s
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) return;
468 strlcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
470 // Set temporary IP address
472 while (ioctl(s
, SIOCGIFADDR
, &ifr
) && timeout
--){
473 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
476 nvram_set(strcat_r(prefix
, "_ipaddr", tmp
), inet_ntoa(sin_addr(&(ifr
.ifr_addr
)))); //"wan_ipaddr"
477 nvram_set(strcat_r(prefix
, "_netmask", tmp
), "255.255.255.255"); //"wan_netmask"
479 // Set temporary P-t-P address
481 while (ioctl(s
, SIOCGIFDSTADDR
, &ifr
) && timeout
--){
482 _dprintf("[%d] waiting for %s...\n", __LINE__
, ifname
);
485 nvram_set(strcat_r(prefix
, "_gateway", tmp
), inet_ntoa(sin_addr(&(ifr
.ifr_dstaddr
)))); //"wan_getaway"
489 start_wan_done(ifname
,prefix
);
493 void start_pppoe(int num
, char *prefix
)
498 TRACE_PT("begin pppoe_num=%d\n", num
);
500 if (num
< 0 || num
>3) return;
504 snprintf(ifname
, sizeof(ifname
), "ppp%d", num
);
508 if (nvram_match( strcat_r(prefix
, "_proto", tmp
), "ppp3g") ) { //wan_proto
509 if (config_pppd(WP_PPP3G
, num
, prefix
) != 0)
514 if (config_pppd(WP_PPPOE
, num
, prefix
) != 0)
523 if (nvram_get_int(strcat_r(prefix
, "_ppp_demand", tmp
))){ //"ppp_demand"
524 start_tmp_ppp(num
, ifname
, prefix
);
527 ifconfig(ifname
, IFUP
, NULL
, NULL
);
533 void stop_pppoe(char *prefix
)
539 void stop_singe_pppoe(int num
, char *prefix
)
542 _dprintf("%s pppoe_num=%d\n", __FUNCTION__
, num
);
546 if (num
!= 0) return;
548 i
= nvram_get_int(strcat_r(prefix
, "_pppoe_pid0", tmp
); //"pppoe_pid0"
549 if ((i
> 1) && (kill(i
, SIGTERM
) == 0)) {
552 } while (kill(i
, SIGKILL
) == 0);
555 char ppp_linkfile
[256] ;
556 memset(ppp_optfile
, 0, 256);
557 sprintf(ppp_linkfile
, "/tmp/ppp/%s_link", prefix
);
559 unlink(ppp_linkfile
);
560 nvram_unset(strcat_r(prefix
, "_pppoe_ifname0", tmp
); //"pppoe_pid0"
562 nvram_set(strcat_r(prefix
, "_get_dns", ""); //"wan_get_dns"
567 // -----------------------------------------------------------------------------
569 static int config_l2tp(void) { // shared xl2tpd.conf for all WAN
574 char xl2tp_file
[256];
576 const char *names
[] = { // FIXME: hardcoded to 4 WANs
579 #ifdef TCONFIG_MULTIWAN
585 mwanlog(LOG_DEBUG
, "IN config_l2tp");
587 /* Generate XL2TPD configuration file */
588 memset(xl2tp_file
, 0, 256);
589 sprintf(xl2tp_file
, "/etc/xl2tpd.conf");
590 if ((fp
= fopen(xl2tp_file
, "w")) == NULL
) {
591 mwanlog(LOG_DEBUG
, "config_l2tp: error open /etc/xl2tpd.conf for writing.");
595 mwanlog(LOG_DEBUG
, "config_l2tp: GLOBAL");
598 "access control = no\n"
600 "debug avp = no\n" // TEMP DEBUG
601 "debug network = no\n" // TEMP DEBUG
602 "debug packet = no\n" // TEMP DEBUG
603 "debug state = no\n" // TEMP DEBUG
604 "debug tunnel = no\n" // TEMP DEBUG
608 for (i
= 0; names
[i
] != NULL
; ++i
) {
609 if (!strcmp(nvram_safe_get(strcat_r(names
[i
], "_proto", tmp
)), "l2tp")) {
610 mwanlog(LOG_DEBUG
, "config_l2tp: create LAC for %s", names
[i
]);
611 demand
= nvram_get_int(strcat_r(names
[i
], "_ppp_demand", tmp
)); //"ppp_demand"
612 char ppp_optfile
[256];
613 memset(ppp_optfile
, 0, 256);
614 sprintf(ppp_optfile
, "/tmp/ppp/%s_options", names
[i
]);
618 "tx bps = 100000000\n"
621 "max redials = 32767\n"
622 "redial timeout = %d\n"
626 names
[i
], // LAC name
627 nvram_safe_get(strcat_r(names
[i
], "_l2tp_server_ip", tmp
)), //"l2tp_server_ip"
629 demand
? 30 : (nvram_get_int(strcat_r(names
[i
], "_ppp_redialperiod", tmp
)) ? : 30), //"ppp_redialperiod"
630 (nvram_get_int("debug_ppp") ? "yes" : "no"), //"debug_ppp"
631 nvram_safe_get(strcat_r(names
[i
], "_xl2tpd_custom", tmp
))); //"xl2tpd_custom"
633 memset(xl2tp_file
, 0, 256);
634 sprintf(xl2tp_file
, "/etc/%s_xl2tpd.custom", names
[i
]);
635 fappend(fp
, xl2tp_file
);
641 mwanlog(LOG_DEBUG
, "OUT config_l2tp");
645 inline void stop_l2tp(char *prefix
)
650 void start_l2tp(char *prefix
)
659 if (config_l2tp() != 0) // generate L2TP daemon config
662 if (config_pppd(WP_L2TP
, 0, prefix
) != 0) // ppp options
665 demand
= nvram_get_int(strcat_r(prefix
, "_ppp_demand", tmp
)); //"ppp_demand"
669 mwanlog(LOG_DEBUG
, "start_l2tp, cmd: xl2tpd -c /etc/xl2tpd.conf");
670 eval("xl2tpd", "-c", "/etc/xl2tpd.conf");
673 eval("listen", nvram_safe_get("lan_ifname"), prefix
);
676 force_to_dial(prefix
); // connect request
677 start_redial(prefix
);
683 // -----------------------------------------------------------------------------
685 char *wan_gateway(char *prefix
)
688 char *gw
= nvram_safe_get(strcat_r(prefix
, "_gateway_get", tmp
)); //"wan_getway_get"
689 if ((*gw
== 0) || (strcmp(gw
, "0.0.0.0") == 0))
690 gw
= nvram_safe_get(strcat_r(prefix
, "_gateway", tmp
)); //"wan_getway"
694 // -----------------------------------------------------------------------------
696 // trigger connect on demand
697 void force_to_dial(char *prefix
)
706 switch (get_wanx_proto(prefix
)) {
708 memset(l2tp_file
, 0, 256);
709 sprintf(l2tp_file
, "/var/run/l2tp-control");
710 // sprintf(connects, "c %s", nvram_safe_get(strcat_r(prefix, "_l2tp_server_name", tmp))); // connect control command
711 sprintf(connects
, "c %s", prefix
);
712 mwanlog(LOG_DEBUG
, "force_to_dial, L2TP connect string = %s", connects
);
713 f_write_string(l2tp_file
, connects
, 0, 0);
716 eval("ping", "-c", "2", "10.112.112.112");
722 eval("ping", "-c", "2", wan_gateway(prefix
));
729 // -----------------------------------------------------------------------------
731 static void _do_wan_routes(char *ifname
, char *nvname
, int metric
, int add
)
738 // IP[/MASK] ROUTER IP2[/MASK2] ROUTER2 ...
739 tmp
= routes
= strdup(nvram_safe_get(nvname
));
740 while (tmp
&& *tmp
) {
741 char *ipaddr
, *gateway
, *nmask
;
743 ipaddr
= nmask
= strsep(&tmp
, " ");
744 strcpy(netmask
, "255.255.255.255");
747 ipaddr
= strsep(&nmask
, "/");
748 if (nmask
&& *nmask
) {
749 bits
= strtol(nmask
, &nmask
, 10);
750 if (bits
>= 1 && bits
<= 32) {
751 mask
.s_addr
= htonl(0xffffffff << (32 - bits
));
752 strcpy(netmask
, inet_ntoa(mask
));
756 gateway
= strsep(&tmp
, " ");
758 if (gateway
&& *gateway
) {
760 mwanlog(LOG_DEBUG
, "MultiWAN: route_add(ifname=%s, metric=%d, ipaddr=%s, gateway=%s, netmask=%s)", ifname
, metric
, ipaddr
, gateway
, netmask
);
761 route_add(ifname
, metric
, ipaddr
, gateway
, netmask
);
763 mwanlog(LOG_DEBUG
, "MultiWAN: route_del(ifname=%s, metric=%d, ipaddr=%s, gateway=%s, netmask=%s)", ifname
, metric
, ipaddr
, gateway
, netmask
);
764 route_del(ifname
, metric
, ipaddr
, gateway
, netmask
);
771 void do_wan_routes(char *ifname
, int metric
, int add
, char *prefix
)
773 if (nvram_get_int("dhcp_routes")) {
774 mwanlog(LOG_DEBUG
, "MultiWAN: do_wan_routes(interface=%s, metric=%d, add=%d, prefix=%s)", ifname
, metric
, add
, prefix
);
776 // Static Routes: IP ROUTER IP2 ROUTER2 ...
777 // Classless Static Routes: IP/MASK ROUTER IP2/MASK2 ROUTER2 ...
778 _do_wan_routes(ifname
, strcat_r(prefix
, "_routes1", tmp
), metric
, add
); //"wan_routes1"
779 _do_wan_routes(ifname
, strcat_r(prefix
, "_routes2", tmp
), metric
, add
); //"wan_routes2"
783 // -----------------------------------------------------------------------------
785 //const char wan_connecting[] = "/var/lib/misc/wan.connecting";
787 static int is_sta(int idx
, int unit
, int subunit
, void *param
)
791 if (nvram_match(wl_nvname("mode", unit
, subunit
), "sta")) {
792 *p
= nvram_safe_get(wl_nvname("ifname", unit
, subunit
));
798 void start_wan_if(int mode
, char *prefix
)
814 char wanconn_file
[256];
819 wan_unit
= get_wan_unit(prefix
);
821 mwanlog(LOG_DEBUG
, "MultiWAN: IN start_wan_if (%s).", prefix
);
823 memset(wanconn_file
, 0, 256);
824 sprintf(wanconn_file
, "/var/lib/misc/%s.connecting", prefix
);
825 f_write(wanconn_file
, NULL
, 0, 0, 0);
827 if (!foreach_wif(1, &p
, is_sta
)) {
828 p
= nvram_safe_get(strcat_r(prefix
, "_ifnameX", tmp
)); //"wan_ifnameX"
829 if (sscanf(p
, "vlan%d", &vid
) == 1) {
830 vlan0tag
= nvram_get_int("vlan0tag");
831 snprintf(buf
, sizeof(buf
), "vlan%dvid", vid
);
832 vid_map
= nvram_get_int(buf
);
833 if ((vid_map
< 1) || (vid_map
> 4094)) vid_map
= vlan0tag
| vid
;
834 snprintf(buf
, sizeof(buf
), "vlan%d", vid_map
);
839 // shibby fix wireless client
840 if (nvram_invmatch(strcat_r(prefix
, "_sta", tmp
), "")) { //wireless client as wan
841 w
= nvram_safe_get(strcat_r(prefix
, "_sta", tmp
));
842 p
= nvram_safe_get(strcat_r(w
, "_ifname", tmp
));
845 if(!strcmp(prefix
,"wan")) {
846 set_mac(p
, "wan_mac", 1);
848 set_mac(p
, strcat_r(prefix
, "_mac", tmp
), wan_unit
+ 15);
851 nvram_set(strcat_r(prefix
, "_ifname", tmp
), p
); //"wan_ifname"
852 nvram_set(strcat_r(prefix
, "_ifnames", tmp
), p
); //"wan_ifnames"
854 wan_ifname
= nvram_safe_get(strcat_r(prefix
, "_ifname", tmp
)); //"wan_ifname"
855 if (wan_ifname
[0] == 0) {
857 nvram_set(strcat_r(prefix
, "_ifname", tmp
), wan_ifname
); //"wan_ifname"
860 if (strcmp(wan_ifname
, "none") == 0) {
861 nvram_set(strcat_r(prefix
, "_proto", tmp
), "disabled"); //"wan_proto"
862 syslog(LOG_WARNING
, "%s ifname is NONE, please check you vlan settings!", prefix
);
865 // defined in shared.h, misc.c
866 wan_proto
= get_wanx_proto(prefix
);
868 // set the default gateway for WAN interface
869 nvram_set(strcat_r(prefix
, "_gateway_get", tmp
), nvram_safe_get(strcat_r(prefix
, "_gateway", tmp
))); //"wan_getway_get","wan_gateway"
871 if (wan_proto
== WP_DISABLED
) {
872 start_wan_done(wan_ifname
,prefix
);
876 if ((sd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
)) < 0) {
896 if (nvram_match(strcat_r(prefix
, "_mtu_enable", tmp
), "0")) { //"mtu_enable"
900 // KDB If we've big fat frames enabled then we *CAN* break the
901 // max MTU on PPP link
902 mtu
= nvram_get_int(strcat_r(prefix
, "_mtu", tmp
)); //"wan_mtu"
903 if (!(nvram_get_int("jumbo_frame_enable")) && (mtu
> max
)) mtu
= max
;
904 else if (mtu
< 576) mtu
= 576;
906 sprintf(buf
, "%d", mtu
);
907 nvram_set(strcat_r(prefix
, "_mtu", tmp
), buf
); //"wan_mtu"
908 nvram_set(strcat_r(prefix
, "_run_mtu", tmp
), buf
); //"wan_run_mtu"
910 // 43011: zhijian 2006-12-25 for CD-Router v3.4 mtu bug of PPTP connection mode
911 /* if (wan_proto == WP_PPTP) {
913 } */ // commented out; checkme -- zzz
915 if (wan_proto
!= WP_PPTP
&& wan_proto
!= WP_L2TP
&& wan_proto
!= WP_PPPOE
&& wan_proto
!= WP_PPP3G
) {
916 // Don't set the MTU on the port for PPP connections, it will be set on the link instead
918 strcpy(ifr
.ifr_name
, wan_ifname
);
919 ioctl(sd
, SIOCSIFMTU
, &ifr
);
923 ifconfig(wan_ifname
, IFUP
, NULL
, NULL
);
928 if (wan_proto
= WP_PPPOE
&& using_dhcpc(prefix
)) { // PPPoE with DHCP MAN
930 mwanlog(LOG_DEBUG
, "MultiWAN: start_wan_if: start_dhcpc(%s) for PPPoE ...", prefix
);
933 if(!strcmp(prefix
,"wan")) start_pppoe(PPPOEWAN
, prefix
);
934 if(!strcmp(prefix
,"wan2")) start_pppoe(PPPOEWAN2
, prefix
);
935 #ifdef TCONFIG_MULTIWAN
936 if(!strcmp(prefix
,"wan3")) start_pppoe(PPPOEWAN3
, prefix
);
937 if(!strcmp(prefix
,"wan4")) start_pppoe(PPPOEWAN4
, prefix
);
944 if (wan_proto
== WP_LTE
) {
946 xstart("switch4g", prefix
);
948 else if (using_dhcpc(prefix
)) {
952 else if (wan_proto
!= WP_DHCP
&& wan_proto
!= WP_LTE
) {
953 ifconfig(wan_ifname
, IFUP
, "0.0.0.0", NULL
);
954 ifconfig(wan_ifname
, IFUP
, nvram_safe_get(strcat_r(prefix
, "_ipaddr", tmp
)), nvram_safe_get(strcat_r(prefix
, "_netmask", tmp
))); //"wan_ipaddr","wan_netmask"
956 p
= nvram_safe_get(strcat_r(prefix
, "_gateway", tmp
)); //"wan_getaway"
957 if ((*p
!= 0) && (strcmp(p
, "0.0.0.0") != 0))
958 preset_wan(wan_ifname
, p
, nvram_safe_get(strcat_r(prefix
, "_netmask", tmp
)), prefix
); //"wan_netmask"
962 mwanlog(LOG_DEBUG
, "MultiWAN: start_wan_if: start_pptp (%d, %s) ...", mode
, prefix
);
963 start_pptp(mode
, prefix
);
966 mwanlog(LOG_DEBUG
, "MultiWAN: start_wan_if: start_l2tp (%s) ...", prefix
);
973 nvram_set(strcat_r(prefix
, "_iface", tmp
), wan_ifname
); //"wan_iface"
974 ifconfig(wan_ifname
, IFUP
, nvram_safe_get(strcat_r(prefix
, "_ipaddr", tmp
)), nvram_safe_get(strcat_r(prefix
, "_netmask", tmp
))); //"wan_ipaddr","wan_netmask"
975 mwanlog(LOG_DEBUG
, "### IN start_wan_if (default: static, %s, %s)", wan_ifname
, prefix
);
977 while ((!check_wanup(prefix
)) && (r
-- > 0)) {
980 start_wan_done(wan_ifname
,prefix
);
981 mwanlog(LOG_DEBUG
, "### OUT start_wan_if (default: static, %s, %s)", wan_ifname
, prefix
);
985 // Get current WAN hardware address
986 strlcpy(ifr
.ifr_name
, wan_ifname
, IFNAMSIZ
);
987 if (ioctl(sd
, SIOCGIFHWADDR
, &ifr
) == 0) {
988 nvram_set(strcat_r(prefix
, "_hwaddr", tmp
), ether_etoa(ifr
.ifr_hwaddr
.sa_data
, buf
)); //"wan_hwaddr"
991 /* Set initial QoS mode again now that WAN port is ready. */
996 mwanlog(LOG_DEBUG
, "MultiWAN: OUT start_wan_if (%s).", prefix
);
1001 void start_wan(int mode
)
1005 char prefix
[] = "wanXX";
1007 mwan_num
= atoi(nvram_safe_get("mwan_num"));
1008 if(mwan_num
< 1 || mwan_num
> MWAN_MAX
){
1012 syslog(LOG_INFO
, "MultiWAN: MWAN is %d (max %d).", mwan_num
, MWAN_MAX
);
1013 for(wan_unit
= 1; wan_unit
<= mwan_num
; ++wan_unit
)
1015 get_wan_prefix(wan_unit
, prefix
);
1016 mwanlog(LOG_DEBUG
, "MultiWAN: start_wan (unit: %d), mode = %d, prefix = %s", wan_unit
, mode
, prefix
);
1017 start_wan_if(mode
, prefix
);
1023 set_host_domain_name();
1025 enable_ip_forward();
1027 killall_tk("mwanroute");
1028 xstart("mwanroute");
1030 if(nvram_get_int("mwan_cktime") > 0)
1031 xstart("watchdog", "add");
1033 led(LED_DIAG
, 0); // for 4712, 5325E (?)
1034 led(LED_DMZ
, nvram_match("dmz_enable", "1"));
1038 void start_wan6_done(const char *wan_ifname
)
1040 struct in_addr addr4
;
1041 struct in6_addr addr
;
1042 static char addr6
[INET6_ADDRSTRLEN
];
1044 int service
= get_ipv6_service();
1046 if (service
!= IPV6_DISABLED
) {
1047 if ((nvram_get_int("ipv6_accept_ra") & 1) != 0)
1048 accept_ra(wan_ifname
);
1053 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname
, "metric", "2048");
1055 case IPV6_NATIVE_DHCP
:
1056 if (nvram_get_int("ipv6_pdonly") == 1) {
1057 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname
);
1062 case IPV6_ANYCAST_6TO4
:
1065 if (service
== IPV6_ANYCAST_6TO4
) {
1067 memset(&addr
, 0, sizeof(addr
));
1068 inet_aton(get_wanip("wan"), &addr4
);
1069 addr
.s6_addr16
[0] = htons(0x2002);
1070 ipv6_mapaddr4(&addr
, 16, &addr4
, 0);
1071 addr
.s6_addr16
[3] = htons(0x0001);
1072 inet_ntop(AF_INET6
, &addr
, addr6
, sizeof(addr6
));
1073 nvram_set("ipv6_prefix", addr6
);
1075 start_ipv6_tunnel();
1076 // FIXME: give it a few seconds for DAD completion
1083 // FIXME2?: give it a few seconds for DAD completion
1090 // ppp_demand: 0=keep alive, 1=connect on demand (run 'listen')
1091 // wan_ifname: vlan1
1092 // wan_iface: ppp# (PPPOE, PPP3G, PPTP, L2TP), vlan1 (DHCP, HB, Static, LTE)
1094 void start_wan_done(char *wan_ifname
, char *prefix
)
1102 char wantime_file
[256];
1103 char wanconn_file
[256];
1106 TRACE_PT("begin %s_ifname=%s\n", prefix
, wan_ifname
);
1110 memset(wantime_file
, 0, 256);
1111 sprintf(wantime_file
, "/var/lib/misc/%s_time", prefix
);
1112 f_write(wantime_file
, &si
.uptime
, sizeof(si
.uptime
), 0, 0);
1114 proto
= get_wanx_proto(prefix
); //proto = get_wanx_proto(prefix);
1116 mwanlog(LOG_DEBUG
, "start_wan_done, interface=%s, wan_prefix=%s, proto=%d", wan_ifname
, prefix
, proto
);
1118 // delete all default routes
1119 route_del(wan_ifname
, 0, NULL
, NULL
, NULL
);
1121 if (proto
!= WP_DISABLED
) {
1123 // set default route to gateway if specified
1124 gw
= wan_gateway(prefix
);
1126 if (proto
== WP_PPTP
&& !using_dhcpc(prefix
)) {
1127 // For PPTP protocol, we must use ppp_get_ip as gateway, not pptp_server_ip (why ??)
1128 if (*gw
== 0 || strcmp(gw
, "0.0.0.0") == 0) gw
= nvram_safe_get(strcat_r(prefix
, "_ppp_get_ip", tmp
)); //"ppp_get_ip"
1131 if ((*gw
!= 0) && (strcmp(gw
, "0.0.0.0") != 0)) {
1132 if (proto
== WP_DHCP
|| proto
== WP_STATIC
|| proto
== WP_LTE
) {
1133 // possibly gateway is over the bridge, try adding a route to gateway first
1134 route_add(wan_ifname
, 0, gw
, NULL
, "255.255.255.255");
1138 while ((route_add(wan_ifname
, 0, "0.0.0.0", gw
, "0.0.0.0") == 1) && (n
--)) {
1141 _dprintf("set default gateway=%s n=%d\n", gw
, n
);
1143 // hack: avoid routing cycles, when both peer and server have the same IP
1144 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
1145 // delete gateway route as it's no longer needed
1146 route_del(wan_ifname
, 0, gw
, "0.0.0.0", "255.255.255.255");
1150 #ifdef THREE_ARP_GRATUATOUS_SUPPORT // from 43011; checkme; commented-out -- zzz
1152 // 43011: Alpha add to send Gratuitous ARP when wan_proto is Static IP 2007-04-09
1153 if (proto == WP_STATIC)
1157 unsigned char wan_mac[6];
1159 if (read_iface(nvram_safe_get("wan_iface"), &ifindex, &wan_ip, wan_mac) >= 0)
1160 arpping(wan_ip, wan_ip, wan_mac, nvram_safe_get("wan_iface"));
1164 if (proto
== WP_PPTP
|| proto
== WP_L2TP
) {
1165 route_del(nvram_safe_get(strcat_r(prefix
, "_iface", tmp
)), 0, nvram_safe_get(strcat_r(prefix
, "_gateway_get", tmp
)), NULL
, "255.255.255.255"); //"wan_iface","wan_gateway_get"
1166 route_add(nvram_safe_get(strcat_r(prefix
, "_iface", tmp
)), 0, nvram_safe_get(strcat_r(prefix
, "_ppp_get_ip", tmp
)), NULL
, "255.255.255.255"); //"wan_iface","ppp_get_ip"
1168 if (proto
== WP_L2TP
) {
1169 route_add(nvram_safe_get(strcat_r(prefix
, "_ifname", tmp
)), 0, nvram_safe_get(strcat_r(prefix
, "_l2tp_server_ip", tmp
)), nvram_safe_get(strcat_r(prefix
, "_gateway", tmp
)), "255.255.255.255"); // fixed routing problem in Israel by kanki
1170 // "wan_ifname","l2tp_server_ip","wan_getaway"
1176 if(nvram_match("adblock_enable", "1"))
1177 xstart("/usr/sbin/adblock");
1184 do_static_routes(1);
1185 // and routes supplied via DHCP
1186 do_wan_routes(using_dhcpc(prefix
) ? nvram_safe_get(strcat_r(prefix
, "_ifname",tmp
)) : wan_ifname
, 0, 1, prefix
); //"wan_ifname"
1187 if(!strcmp(prefix
,"wan")){
1191 wanup
= check_wanup(prefix
);
1193 if ((wanup
) || (time(0) < Y2K
)) {
1198 if ((wanup
) || (proto
== WP_DISABLED
)) {
1208 start_wan6_done(get_wan6face());
1211 #ifdef TCONFIG_DNSSEC
1212 if (nvram_match("dnssec_enable", "1")) {
1213 killall("dnsmasq", SIGHUP
);
1225 notice_set("wan", "");
1227 run_nvscript("script_wanup", NULL
, 0);
1230 // We don't need STP after wireless led is lighted // no idea why... toggling it if necessary -- zzz
1231 if (check_hw_type() == HW_BCM4702
) {
1232 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "0");
1233 if (nvram_match("lan_stp", "1"))
1234 eval("brctl", "stp", nvram_safe_get("lan_ifname"), "1");
1235 if(strcmp(nvram_safe_get("lan1_ifname"),"")!=0) {
1236 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "0");
1237 if (nvram_match("lan1_stp", "1"))
1238 eval("brctl", "stp", nvram_safe_get("lan1_ifname"), "1");
1240 if(strcmp(nvram_safe_get("lan2_ifname"),"")!=0) {
1241 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "0");
1242 if (nvram_match("lan2_stp", "1"))
1243 eval("brctl", "stp", nvram_safe_get("lan2_ifname"), "1");
1245 if(strcmp(nvram_safe_get("lan3_ifname"),"")!=0) {
1246 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "0");
1247 if (nvram_match("lan3_stp", "1"))
1248 eval("brctl", "stp", nvram_safe_get("lan3_ifname"), "1");
1260 #ifdef TCONFIG_PPTPD
1261 if (wanup
&& nvram_get_int("pptp_client_enable"))
1262 start_pptp_client();
1265 new_qoslimit_start(); //!! RAF
1268 mwan_table_add(prefix
);
1269 mwan_load_balance();
1271 memset(wanconn_file
, 0, 256);
1272 sprintf(wanconn_file
, "/var/lib/misc/%s.connecting", prefix
);
1273 unlink(wanconn_file
);
1278 void stop_wan_if(char *prefix
)
1285 char wanconn_file
[256];
1286 char wannotice_file
[256];
1288 mwan_table_del(prefix
);
1290 TRACE_PT("begin\n");
1293 /* Kill any WAN client daemons or callbacks */
1294 stop_redial(prefix
);
1295 //stop_pppoe(prefix);
1296 stop_ppp(prefix
); // one for all
1298 nvram_set(strcat_r(prefix
, "_get_dns", tmp
), ""); //"wan_get_dns"
1300 wan_proto
= get_wanx_proto(prefix
);
1302 if (wan_proto
== WP_LTE
) {
1303 xstart("switch4g", prefix
, "disconnect");
1306 /* Bring down WAN interfaces */
1307 foreach(name
, nvram_safe_get(strcat_r(prefix
, "_ifnames", tmp
)), next
) //"wan_ifnames"
1308 ifconfig(name
, 0, "0.0.0.0", NULL
);
1310 //notice_set(prefix, "");
1311 memset(wannotice_file
, 0, 256);
1312 sprintf(wannotice_file
, "/var/notice/%s", prefix
);
1313 memset(wanconn_file
, 0, 256);
1314 sprintf(wanconn_file
, "/var/lib/misc/%s.connecting", prefix
);
1315 unlink(wannotice_file
);
1316 unlink(wanconn_file
);
1318 mwan_load_balance();
1320 /* clear old IP params from nvram on stop */
1321 /* but only if WAN is not as STATIC - shibby */
1322 if (wan_proto
!= WP_STATIC
) {
1323 nvram_set(strcat_r(prefix
, "_netmask", tmp
), "0.0.0.0");
1325 nvram_set(strcat_r(prefix
, "_gateway_get", tmp
), "0.0.0.0");
1338 #ifdef TCONFIG_PPTPD
1345 new_qoslimit_stop(); //!! RAF
1348 stop_upnp(); //!!TB - moved from stop_services()
1357 nvram_set("ipv6_get_dns", "");
1363 stop_wan_if("wan2");
1364 #ifdef TCONFIG_MULTIWAN
1365 stop_wan_if("wan3");
1366 stop_wan_if("wan4");
1369 mwanlog(LOG_DEBUG
, "MultiWAN: watchdog disabled");
1370 xstart("watchdog", "del");
1372 SET_LED(RELEASE_IP
);