TOR: fix compilation
[tomato.git] / release / src / router / rc / wan.c
blobe5a681135ed801ff3a9e14b249952c4400a33678
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 #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)
49 FILE *f;
50 char *user;
51 char *pass;
53 char secrets_file[256];
54 char tmp[100];
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);
63 fclose(f);
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);
71 fclose(f);
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)
80 TRACE_PT("begin\n");
82 FILE *fp;
83 FILE *cfp;
84 char *p;
85 int demand;
87 char ppp_optfile[256];
88 char tmp[100];
90 mkdir("/tmp/ppp", 0777);
91 symlink("/sbin/rc", "/tmp/ppp/ip-up");
92 symlink("/sbin/rc", "/tmp/ppp/ip-down");
93 #ifdef TCONFIG_IPV6
94 symlink("/sbin/rc", "/tmp/ppp/ipv6-up");
95 symlink("/sbin/rc", "/tmp/ppp/ipv6-down");
96 #endif
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) {
105 perror(ppp_optfile);
106 return -1;
109 #ifdef LINUX26
110 #ifdef TCONFIG_USB
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)
115 fprintf(fp,
116 "/dev/%s\n"
117 "460800\n"
118 "connect \"/usr/sbin/chat -V -t 60 -f %s\"\n"
119 "noipdefault\n"
120 "lock\n"
121 "crtscts\n"
122 "modem\n"
123 "ipcp-accept-local\n",
124 nvram_safe_get(strcat_r(prefix, "_modem_dev", tmp)), //"modem_dev" -> strcat_r(prefix, "_modem_dev", tmp)
125 ppp3g_chatfile);
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
132 } else {
133 #endif
134 #endif
135 fprintf(fp,
136 "unit %d\n"
137 "user \"%s\"\n"
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
141 num,
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
145 #ifdef LINUX26
146 #ifdef TCONFIG_USB
148 #endif
149 #endif
150 fprintf(fp,
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
162 "%s", // Debug
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"
167 #ifdef LINUX26
168 #ifdef TCONFIG_USB
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
171 } else {
172 #endif
173 #endif
174 fprintf(fp,
175 "nopcomp\n" // Disable protocol field compression
176 "noaccomp\n" // Disable Address/Control compression
178 #ifdef LINUX26
179 #ifdef TCONFIG_USB
181 #endif
182 #endif
184 if (wan_proto != WP_L2TP) {
185 fprintf(fp,
186 "persist\n"
187 "holdoff %d\n",
188 demand ? 30 : (nvram_get_int(strcat_r(prefix, "ppp_redialperiod", tmp)) ? : 30)); //"ppp_redialperiod"
191 switch (wan_proto) {
192 case WP_PPTP:
193 fprintf(fp,
194 "plugin pptp.so\n"
195 "pptp_server %s\n"
196 "nomppe-stateful\n"
197 "require-mschap-v2\n"
198 "noauth\n" // No authenticate peer (i dunno why it doesn't apply from shared params)
199 "mtu %d\n",
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"
203 break;
204 case WP_PPPOE:
205 fprintf(fp,
206 // "password '%s'\n"
207 "plugin rp-pppoe.so\n"
208 "nomppe nomppc\n"
209 "nic-%s\n"
210 "mru %d mtu %d\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"
222 fprintf(fp, "mp\n");
224 break;
225 #ifdef LINUX26
226 #ifdef TCONFIG_USB
227 case WP_PPP3G:
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);
232 return -1;
234 fprintf(cfp,
235 "ABORT \"NO CARRIER\"\n"
236 "ABORT \"NO DIALTONE\"\n"
237 "ABORT \"NO ERROR\"\n"
238 "ABORT \"NO ANSWER\"\n"
239 "ABORT \"BUSY\"\n"
240 "REPORT CONNECT\n"
241 "\"\" \"AT\"\n");
242 /* moved to switch3g script
243 if (strlen(nvram_get("modem_pin")) >0 ) {
244 fprintf(cfp,
245 "TIMEOUT 60\n"
246 "OK \"AT+CPIN=%s\"\n"
247 "TIMEOUT 10\n",
248 nvram_get("modem_pin"));
251 fprintf(cfp,
252 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
253 "OK \"AT\"\n"
254 "OK \"ATS0=0\"\n"
255 "OK \"AT\"\n"
256 "OK \"AT&FE0V1X1&D2&C1S0=0\"\n"
257 "OK \"AT\"\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
263 * ERROR if issued).
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);
269 fprintf(cfp,
270 "OK \"ATDT%s\"\n"
271 "CONNECT \\c\n",
272 nvram_safe_get(strcat_r(prefix, "_modem_init", tmp)) //"modem_init"
274 fclose(cfp);
277 if (nvram_match("usb_3g", "1") && nvram_match(strcat_r(prefix, "_proto", tmp), "ppp3g")) {
278 // clear old gateway
279 if (strlen(nvram_get(strcat_r(prefix, "_gateway", tmp))) >0 ) { //"wan_gateway"
280 nvram_set(strcat_r(prefix, "_gateway", tmp), "");
283 // detect 3G Modem
284 xstart("switch3g", prefix);
286 break;
287 #endif
288 #endif
289 case WP_L2TP:
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"
293 break;
296 if (demand) {
297 // demand mode
298 fprintf(fp,
299 "demand\n" // Dial on demand
300 "idle %d\n"
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"
308 #ifdef TCONFIG_IPV6
309 switch (get_ipv6_service()) {
310 case IPV6_NATIVE:
311 case IPV6_NATIVE_DHCP:
312 fprintf(fp, "+ipv6\n");
313 break;
315 #endif
316 // User specific options
317 fprintf(fp, "%s\n", nvram_safe_get(strcat_r(prefix, "_ppp_custom", tmp))); //"ppp_custom"
319 fclose(fp);
320 // make_secrets(prefix);
322 TRACE_PT("end\n");
323 return 0;
326 static void stop_ppp(char *prefix)
328 TRACE_PT("begin\n");
329 char ppp_linkfile[256];
330 char pppd_name[256];
331 //char tmp[100];
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);
340 killall_tk("ip-up");
341 killall_tk("ip-down");
342 #ifdef TCONFIG_IPV6
343 killall_tk("ipv6-up");
344 killall_tk("ipv6-down");
345 #endif
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");
351 TRACE_PT("end\n");
354 static void run_pppd(char *prefix)
356 char tmp[100];
358 char pppd_path[256];
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"
368 // demand mode
370 Fixed issue id 7887(or 7787):
371 When DUT is PPTP Connect on Demand mode, it couldn't be trigger from LAN.
373 stop_dnsmasq();
374 dns_to_resolv();
375 start_dnsmasq();
377 // Trigger Connect On Demand if user ping pptp server
378 eval("listen", nvram_safe_get("lan_ifname"), prefix);
380 else {
381 // keepalive mode
382 start_redial(prefix);
386 // -----------------------------------------------------------------------------
388 inline void stop_pptp(char *prefix)
390 stop_ppp(prefix);
393 void start_pptp(int mode, char *prefix)
396 TRACE_PT("begin\n");
398 if (!using_dhcpc(prefix)) stop_dhcpc(prefix);
399 stop_pptp(prefix);
401 if (config_pppd(WP_PPTP, 0, prefix) != 0)
402 return;
404 run_pppd(prefix);
406 TRACE_PT("end\n");
409 // -----------------------------------------------------------------------------
411 void preset_wan(char *ifname, char *gw, char *netmask, char *prefix)
413 int i;
414 char tmp[100];
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 */
423 i = 5;
424 while ((route_add(ifname, 1, "0.0.0.0", gw, "0.0.0.0") == 1) && (i--)) {
425 sleep(1);
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")){
437 dns_to_resolv();
438 start_dnsmasq();
439 sleep(1);
440 start_firewall();
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)
450 int timeout;
451 struct ifreq ifr;
452 int s;
454 char tmp[100];
456 TRACE_PT("begin: num=%d\n", num);
458 if (num != 0) return;
460 // Wait for ppp0 to be created
461 timeout = 15;
462 while ((ifconfig(ifname, IFUP, NULL, NULL) != 0) && (timeout-- > 0)) {
463 sleep(1);
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
471 timeout = 3;
472 while (ioctl(s, SIOCGIFADDR, &ifr) && timeout--){
473 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
474 sleep(1);
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
480 timeout = 3;
481 while (ioctl(s, SIOCGIFDSTADDR, &ifr) && timeout--){
482 _dprintf("[%d] waiting for %s...\n", __LINE__, ifname);
483 sleep(1);
485 nvram_set(strcat_r(prefix, "_gateway", tmp), inet_ntoa(sin_addr(&(ifr.ifr_dstaddr)))); //"wan_getaway"
487 close(s);
489 start_wan_done(ifname,prefix);
490 TRACE_PT("end\n");
493 void start_pppoe(int num, char *prefix)
495 char ifname[8];
496 char tmp[100];
498 TRACE_PT("begin pppoe_num=%d\n", num);
500 if (num < 0 || num >3) return;
502 stop_pppoe(prefix);
504 snprintf(ifname, sizeof(ifname), "ppp%d", num);
506 #ifdef LINUX26
507 #ifdef TCONFIG_USB
508 if (nvram_match( strcat_r(prefix, "_proto", tmp), "ppp3g") ) { //wan_proto
509 if (config_pppd(WP_PPP3G, num, prefix) != 0)
510 return;
511 } else {
512 #endif
513 #endif
514 if (config_pppd(WP_PPPOE, num, prefix) != 0)
515 return;
516 #ifdef LINUX26
517 #ifdef TCONFIG_USB
519 #endif
520 #endif
521 run_pppd(prefix);
523 if (nvram_get_int(strcat_r(prefix, "_ppp_demand", tmp))){ //"ppp_demand"
524 start_tmp_ppp(num, ifname, prefix);
526 else {
527 ifconfig(ifname, IFUP, NULL, NULL);
530 TRACE_PT("end\n");
533 void stop_pppoe(char *prefix)
535 stop_ppp(prefix);
538 #if 0
539 void stop_singe_pppoe(int num, char *prefix)
541 char tmp[100];
542 _dprintf("%s pppoe_num=%d\n", __FUNCTION__, num);
544 int i;
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)) {
550 do {
551 sleep(2);
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"
563 clear_resolv();
565 #endif
567 // -----------------------------------------------------------------------------
569 static int config_l2tp(void) { // shared xl2tpd.conf for all WAN
571 FILE *fp;
572 int i;
573 int demand;
574 char xl2tp_file[256];
575 char tmp[100];
576 const char *names[] = { // FIXME: hardcoded to 4 WANs
577 "wan",
578 "wan2",
579 #ifdef TCONFIG_MULTIWAN
580 "wan3",
581 "wan4",
582 #endif
583 NULL
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.");
592 return -1;
594 // GLOBAL
595 mwanlog(LOG_DEBUG, "config_l2tp: GLOBAL");
596 fprintf(fp,
597 "[global]\n"
598 "access control = no\n"
599 "port = 1701\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
605 "\n"
607 // LACS
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]);
615 fprintf(fp,
616 "[lac %s]\n"
617 "lns = %s\n"
618 "tx bps = 100000000\n"
619 "pppoptfile = %s\n"
620 "redial = yes\n"
621 "max redials = 32767\n"
622 "redial timeout = %d\n"
623 "tunnel rws = 8\n"
624 "ppp debug = %s\n"
625 "%s\n",
626 names[i], // LAC name
627 nvram_safe_get(strcat_r(names[i], "_l2tp_server_ip", tmp)), //"l2tp_server_ip"
628 ppp_optfile,
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);
639 fclose(fp);
641 mwanlog(LOG_DEBUG, "OUT config_l2tp");
642 return 0;
645 inline void stop_l2tp(char *prefix)
647 stop_ppp(prefix);
650 void start_l2tp(char *prefix)
652 char tmp[100];
653 int demand;
655 TRACE_PT("begin\n");
657 stop_l2tp(prefix);
659 if (config_l2tp() != 0) // generate L2TP daemon config
660 return;
662 if (config_pppd(WP_L2TP, 0, prefix) != 0) // ppp options
663 return;
665 demand = nvram_get_int(strcat_r(prefix, "_ppp_demand", tmp)); //"ppp_demand"
667 enable_ip_forward();
669 mwanlog(LOG_DEBUG, "start_l2tp, cmd: xl2tpd -c /etc/xl2tpd.conf");
670 eval("xl2tpd", "-c", "/etc/xl2tpd.conf");
672 if (demand) {
673 eval("listen", nvram_safe_get("lan_ifname"), prefix);
675 else {
676 force_to_dial(prefix); // connect request
677 start_redial(prefix);
680 TRACE_PT("end\n");
683 // -----------------------------------------------------------------------------
685 char *wan_gateway(char *prefix)
687 char tmp[100];
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"
691 return gw;
694 // -----------------------------------------------------------------------------
696 // trigger connect on demand
697 void force_to_dial(char *prefix)
699 char l2tp_file[256];
700 // char tmp[64];
701 char connects[64];
703 TRACE_PT("begin\n");
705 sleep(1);
706 switch (get_wanx_proto(prefix)) {
707 case WP_L2TP:
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);
714 break;
715 case WP_PPTP:
716 eval("ping", "-c", "2", "10.112.112.112");
717 break;
718 case WP_DISABLED:
719 case WP_STATIC:
720 break;
721 default:
722 eval("ping", "-c", "2", wan_gateway(prefix));
723 break;
726 TRACE_PT("end\n");
729 // -----------------------------------------------------------------------------
731 static void _do_wan_routes(char *ifname, char *nvname, int metric, int add)
733 char *routes, *tmp;
734 int bits;
735 struct in_addr mask;
736 char netmask[16];
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");
746 if (nmask) {
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) {
759 if (add) {
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);
762 } else {
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);
768 free(routes);
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);
775 char tmp[100];
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)
789 char **p = param;
791 if (nvram_match(wl_nvname("mode", unit, subunit), "sta")) {
792 *p = nvram_safe_get(wl_nvname("ifname", unit, subunit));
793 return 1;
795 return 0;
798 void start_wan_if(int mode, char *prefix)
800 int wan_proto;
801 char *wan_ifname;
802 char *p = NULL;
803 char *w = NULL;
804 struct ifreq ifr;
805 int sd;
806 int max;
807 int mtu;
808 char buf[128];
809 int vid;
810 int vid_map;
811 int vlan0tag;
812 int wan_unit;
814 char wanconn_file[256];
815 char tmp[100];
817 TRACE_PT("begin\n");
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);
835 p = buf;
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);
847 } else {
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) {
856 wan_ifname = "none";
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);
873 return;
876 if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
877 perror("socket");
878 return;
881 // MTU
883 switch (wan_proto) {
884 case WP_PPPOE:
885 case WP_PPP3G:
886 max = 1492;
887 break;
888 case WP_PPTP:
889 case WP_L2TP:
890 max = 1460;
891 break;
892 default:
893 max = 1500;
894 break;
896 if (nvram_match(strcat_r(prefix, "_mtu_enable", tmp), "0")) { //"mtu_enable"
897 mtu = max;
899 else {
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) {
912 mtu += 40;
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
917 ifr.ifr_mtu = mtu;
918 strcpy(ifr.ifr_name, wan_ifname);
919 ioctl(sd, SIOCSIFMTU, &ifr);
923 ifconfig(wan_ifname, IFUP, NULL, NULL);
925 switch (wan_proto) {
926 case WP_PPPOE:
927 case WP_PPP3G:
928 if (wan_proto = WP_PPPOE && using_dhcpc(prefix)) { // PPPoE with DHCP MAN
929 stop_dhcpc(prefix);
930 mwanlog(LOG_DEBUG, "MultiWAN: start_wan_if: start_dhcpc(%s) for PPPoE ...", prefix);
931 start_dhcpc(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);
938 #endif
939 break;
940 case WP_DHCP:
941 case WP_LTE:
942 case WP_L2TP:
943 case WP_PPTP:
944 if (wan_proto == WP_LTE) {
945 // prepare LTE modem
946 xstart("switch4g", prefix);
948 else if (using_dhcpc(prefix)) {
949 stop_dhcpc(prefix);
950 start_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"
960 switch (wan_proto) {
961 case WP_PPTP:
962 mwanlog(LOG_DEBUG, "MultiWAN: start_wan_if: start_pptp (%d, %s) ...", mode, prefix);
963 start_pptp(mode, prefix);
964 break;
965 case WP_L2TP:
966 mwanlog(LOG_DEBUG, "MultiWAN: start_wan_if: start_l2tp (%s) ...", prefix);
967 start_l2tp(prefix);
968 break;
971 break;
972 default: // static
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);
976 int r = 10;
977 while ((!check_wanup(prefix)) && (r-- > 0)) {
978 sleep(1);
980 start_wan_done(wan_ifname,prefix);
981 mwanlog(LOG_DEBUG, "### OUT start_wan_if (default: static, %s, %s)", wan_ifname, prefix);
982 break;
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. */
992 set_et_qos_mode(sd);
994 close(sd);
996 mwanlog(LOG_DEBUG, "MultiWAN: OUT start_wan_if (%s).", prefix);
998 TRACE_PT("end\n");
1001 void start_wan(int mode)
1003 int mwan_num;
1004 int wan_unit;
1005 char prefix[] = "wanXX";
1007 mwan_num = atoi(nvram_safe_get("mwan_num"));
1008 if(mwan_num < 1 || mwan_num > MWAN_MAX){
1009 mwan_num = 1;
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);
1020 stop_wireless();
1021 start_wireless();
1022 start_firewall();
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"));
1037 #ifdef TCONFIG_IPV6
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);
1051 switch (service) {
1052 case IPV6_NATIVE:
1053 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname, "metric", "2048");
1054 break;
1055 case IPV6_NATIVE_DHCP:
1056 if (nvram_get_int("ipv6_pdonly") == 1) {
1057 eval("ip", "route", "add", "::/0", "dev", (char *)wan_ifname);
1059 stop_dhcp6c();
1060 start_dhcp6c();
1061 break;
1062 case IPV6_ANYCAST_6TO4:
1063 case IPV6_6IN4:
1064 stop_ipv6_tunnel();
1065 if (service == IPV6_ANYCAST_6TO4) {
1066 addr4.s_addr = 0;
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
1077 sleep(2);
1078 break;
1079 case IPV6_6RD:
1080 case IPV6_6RD_DHCP:
1081 stop_6rd_tunnel();
1082 start_6rd_tunnel();
1083 // FIXME2?: give it a few seconds for DAD completion
1084 sleep(2);
1085 break;
1088 #endif
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)
1096 int proto;
1097 int n;
1098 char *gw;
1099 struct sysinfo si;
1100 int wanup;
1102 char wantime_file[256];
1103 char wanconn_file[256];
1104 char tmp[100];
1106 TRACE_PT("begin %s_ifname=%s\n", prefix, wan_ifname);
1108 sysinfo(&si);
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);
1125 #if 0
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"
1130 #endif
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");
1137 n = 5;
1138 while ((route_add(wan_ifname, 0, "0.0.0.0", gw, "0.0.0.0") == 1) && (n--)) {
1139 sleep(1);
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)
1155 int ifindex;
1156 u_int32_t wan_ip;
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"));
1163 #endif
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"
1174 dns_to_resolv();
1176 if(nvram_match("adblock_enable", "1"))
1177 xstart("/usr/sbin/adblock");
1179 start_dnsmasq();
1181 start_firewall();
1182 start_qos(prefix);
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")){
1188 stop_zebra();
1189 start_zebra();
1191 wanup = check_wanup(prefix);
1193 if ((wanup) || (time(0) < Y2K)) {
1194 stop_ntpc();
1195 start_ntpc();
1198 if ((wanup) || (proto == WP_DISABLED)) {
1199 stop_ddns();
1200 start_ddns();
1201 stop_igmp_proxy();
1202 stop_udpxy();
1203 start_igmp_proxy();
1204 start_udpxy();
1207 #ifdef TCONFIG_IPV6
1208 start_wan6_done(get_wan6face());
1209 #endif
1211 #ifdef TCONFIG_DNSSEC
1212 if (nvram_match("dnssec_enable", "1")) {
1213 killall("dnsmasq", SIGHUP);
1215 #endif
1217 stop_upnp();
1218 start_upnp();
1220 // restart httpd
1221 start_httpd();
1223 if (wanup) {
1224 SET_LED(GOT_IP);
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");
1252 if (wanup)
1253 start_vpn_eas();
1255 #ifdef TCONFIG_TINC
1256 if(wanup)
1257 start_tinc_wanup();
1258 #endif
1260 #ifdef TCONFIG_PPTPD
1261 if (wanup && nvram_get_int("pptp_client_enable"))
1262 start_pptp_client();
1263 #endif
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);
1275 TRACE_PT("end\n");
1278 void stop_wan_if(char *prefix)
1280 char name[80];
1281 char *next;
1282 int wan_proto;
1284 char tmp[100];
1285 char wanconn_file[256];
1286 char wannotice_file[256];
1288 mwan_table_del(prefix);
1290 TRACE_PT("begin\n");
1292 stop_qos(prefix);
1293 /* Kill any WAN client daemons or callbacks */
1294 stop_redial(prefix);
1295 //stop_pppoe(prefix);
1296 stop_ppp(prefix); // one for all
1297 stop_dhcpc(prefix);
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");
1328 TRACE_PT("end\n");
1331 void stop_wan(void)
1334 #ifdef TCONFIG_TINC
1335 stop_tinc();
1336 #endif
1338 #ifdef TCONFIG_PPTPD
1339 stop_pptp_client();
1340 stop_dnsmasq();
1341 dns_to_resolv();
1342 start_dnsmasq();
1343 #endif
1345 new_qoslimit_stop(); //!! RAF
1347 //stop_qos();
1348 stop_upnp(); //!!TB - moved from stop_services()
1349 stop_firewall();
1350 stop_igmp_proxy();
1351 stop_udpxy();
1352 stop_ntpc();
1354 #ifdef TCONFIG_IPV6
1355 stop_ipv6_tunnel();
1356 stop_dhcp6c();
1357 nvram_set("ipv6_get_dns", "");
1358 #endif
1360 stop_vpn_eas();
1361 clear_resolv();
1362 stop_wan_if("wan");
1363 stop_wan_if("wan2");
1364 #ifdef TCONFIG_MULTIWAN
1365 stop_wan_if("wan3");
1366 stop_wan_if("wan4");
1367 #endif
1369 mwanlog(LOG_DEBUG, "MultiWAN: watchdog disabled");
1370 xstart("watchdog", "del");
1372 SET_LED(RELEASE_IP);