1/* 2 * Network services 3 * 4 * Copyright 2004, Broadcom Corporation 5 * All Rights Reserved. 6 * 7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY 8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM 9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. 11 * 12 * $Id: network.c,v 1.7 2009/03/11 14:29:09 james26_jang Exp $ 13 */ 14 15#include <stdio.h> 16#include <stdlib.h> 17#include <errno.h> 18#include <syslog.h> 19#include <ctype.h> 20#include <string.h> 21#include <sys/stat.h> 22#include <sys/ioctl.h> 23#include <sys/types.h> 24#include <sys/socket.h> 25#include <net/if.h> 26#include <netinet/in.h> 27#include <arpa/inet.h> 28#include <net/if_arp.h> 29#include <signal.h> 30typedef u_int64_t u64; 31typedef u_int32_t u32; 32typedef u_int16_t u16; 33typedef u_int8_t u8; 34#include <linux/sockios.h> 35#include <linux/ethtool.h> 36#include <bcmnvram.h> 37#include <netconf.h> 38#include <shutils.h> 39#include <wlutils.h> 40#include <nvparse.h> 41#include <rc.h> 42#include <bcmutils.h> 43#include <etioctl.h> 44#include <bcmparams.h> 45 46extern int qos_get_wan_rate(void); 47void lan_up(char *); 48 49static int 50add_routes(char *prefix, char *var, char *ifname) 51{ 52 char word[80], *next; 53 char *ipaddr, *netmask, *gateway, *metric; 54 char tmp[100]; 55 56 foreach(word, nvram_safe_get(strcat_r(prefix, var, tmp)), next) { 57 58 netmask = word; 59 ipaddr = strsep(&netmask, ":"); 60 if (!ipaddr || !netmask) 61 continue; 62 gateway = netmask; 63 netmask = strsep(&gateway, ":"); 64 if (!netmask || !gateway) 65 continue; 66 metric = gateway; 67 gateway = strsep(&metric, ":"); 68 if (!gateway || !metric) 69 continue; 70 if (inet_addr_(gateway) == INADDR_ANY) 71 gateway = nvram_safe_get("wanx_gateway"); 72 73 74 dprintf("\n\n\nadd %s %d %s %s %s\n\n\n", ifname, atoi(metric), ipaddr, gateway, netmask); 75 76 route_add(ifname, atoi(metric) + 1, ipaddr, gateway, netmask); 77 } 78 79 return 0; 80} 81 82void add_wanx_routes(char *prefix, char *ifname, int metric) 83{ 84 char *routes, *msroutes, *tmp; 85 char buf[30]; 86 87 char ipaddr[] = "255.255.255.255"; 88 char gateway[] = "255.255.255.255"; 89 char netmask[] = "255.255.255.255"; 90 91 if (!nvram_match("dr_enable_x", "1")) 92 return; 93 94 /* routes */ 95 routes = strdup(nvram_safe_get(strcat_r(prefix, "routes", buf))); 96 for (tmp = routes; tmp && *tmp; ) 97 { 98 char *ipaddr = strsep(&tmp, " "); 99 char *gateway = strsep(&tmp, " "); 100 if (gateway) { 101 route_add(ifname, metric + 1, ipaddr, gateway, netmask); 102 } 103 } 104 free(routes); 105 106 /* ms routes */ 107 for (msroutes = nvram_get(strcat_r(prefix, "msroutes", buf)); msroutes && isdigit(*msroutes); ) 108 { 109 /* read net length */ 110 int bit, bits = strtol(msroutes, &msroutes, 10); 111 struct in_addr ip, gw, mask; 112 113 if (bits < 1 || bits > 32 || *msroutes != ' ') 114 break; 115 116 mask.s_addr = htonl(0xffffffff << (32 - bits)); 117 118 /* read network address */ 119 for (ip.s_addr = 0, bit = 24; bit > (24 - bits); bit -= 8) 120 { 121 if (*msroutes++ != ' ' || !isdigit(*msroutes)) 122 goto bad_data; 123 124 ip.s_addr |= htonl(strtol(msroutes, &msroutes, 10) << bit); 125 } 126 127 /* read gateway */ 128 for (gw.s_addr = 0, bit = 24; bit >= 0 && *msroutes; bit -= 8) 129 { 130 if (*msroutes++ != ' ' || !isdigit(*msroutes)) 131 goto bad_data; 132 133 gw.s_addr |= htonl(strtol(msroutes, &msroutes, 10) << bit); 134 } 135 136 /* clear bits per RFC */ 137 ip.s_addr &= mask.s_addr; 138 139 strcpy(ipaddr, inet_ntoa(ip)); 140 strcpy(gateway, inet_ntoa(gw)); 141 strcpy(netmask, inet_ntoa(mask)); 142 143 route_add(ifname, metric + 1, ipaddr, gateway, netmask); 144 145 if (*msroutes == ' ') 146 msroutes++; 147 } 148bad_data: 149 return; 150} 151 152static int 153del_routes(char *prefix, char *var, char *ifname) 154{ 155 char word[80], *next; 156 char *ipaddr, *netmask, *gateway, *metric; 157 char tmp[100]; 158 159 foreach(word, nvram_safe_get(strcat_r(prefix, var, tmp)), next) { 160 dprintf("add %s\n", word); 161 162 netmask = word; 163 ipaddr = strsep(&netmask, ":"); 164 if (!ipaddr || !netmask) 165 continue; 166 gateway = netmask; 167 netmask = strsep(&gateway, ":"); 168 if (!netmask || !gateway) 169 continue; 170 metric = gateway; 171 gateway = strsep(&metric, ":"); 172 if (!gateway || !metric) 173 continue; 174 175 if (inet_addr_(gateway) == INADDR_ANY) 176 gateway = nvram_safe_get("wanx_gateway"); 177 178 dprintf("add %s\n", ifname); 179 180 route_del(ifname, atoi(metric) + 1, ipaddr, gateway, netmask); 181 } 182 183 return 0; 184} 185 186static int 187add_lan_routes(char *lan_ifname) 188{ 189 return add_routes("lan_", "route", lan_ifname); 190} 191 192static int 193del_lan_routes(char *lan_ifname) 194{ 195 return del_routes("lan_", "route", lan_ifname); 196} 197 198static void 199start_igmpproxy(char *wan_ifname) 200{ 201 static char *igmpproxy_conf = "/tmp/igmpproxy.conf"; 202 struct stat st_buf; 203 FILE *fp; 204 205 if (!nvram_match("mr_enable_x", "1")) 206 return; 207 208 if ((fp = fopen(igmpproxy_conf, "w")) == NULL) { 209 perror(igmpproxy_conf); 210 return; 211 } 212 213 fprintf(fp, "# automagically generated from web settings\n" 214 "quickleave\n\n" 215 "phyint %s upstream\n" 216 "\taltnet %s\n\n" 217 "phyint %s downstream\n\n", 218 wan_ifname, 219 nvram_get("mr_altnet_x") ? : "0.0.0.0/0", 220 nvram_get("lan_ifname") ? : "br0"); 221 222 fclose(fp); 223 224 eval("/usr/sbin/igmpproxy", "-c", igmpproxy_conf); 225} 226 227void 228start_lan(void) 229{ 230 char *lan_ifname = nvram_safe_get("lan_ifname"); 231// char br0_ifnames[255]; 232 char name[80], *next; 233// char tmpstr[48]; 234// int i, j; 235 int s; 236 struct ifreq ifr; 237 238 dprintf("%s\n", lan_ifname); 239 240#ifdef GUEST_ACCOUNT 241 memset(br0_ifnames,0,sizeof(br0_ifnames)); 242 243 nvram_unset("unbridged_ifnames"); 244 nvram_unset("br0_ifnames"); 245 246 /* If we're a travel router... then we need to make sure we get 247 the primary wireless interface up before trying to attach slave 248 interface(s) to the bridge */ 249#endif 250 251#ifdef URE 252 if(nvram_match("ure_disable", "0")) 253 { 254 eval("wlconf", nvram_get("wan0_ifname"), "up"); 255 } 256#endif 257 258 /* Bring up bridged interfaces */ 259 if (strncmp(lan_ifname, "br", 2) == 0) { 260 eval("brctl", "addbr", lan_ifname); 261 eval("brctl", "setfd", lan_ifname, "0"); 262 if (nvram_match("router_disable", "1") || nvram_match("lan_stp", "0")) 263 eval("brctl", "stp", lan_ifname, "dis"); 264#ifdef ASUS_EXT 265 foreach(name, nvram_safe_get("lan_ifnames_t"), next) { 266#else 267 foreach(name, nvram_safe_get("lan_ifnames"), next) { 268#endif 269 /* Bring up interface */ 270 ifconfig(name, IFUP, NULL, NULL); 271 /* Set the logical bridge address to that of the first interface */ 272 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 273 continue; 274 strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ); 275 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0 && 276 memcmp(ifr.ifr_hwaddr.sa_data, "\0\0\0\0\0\0", ETHER_ADDR_LEN) == 0) { 277 strncpy(ifr.ifr_name, name, IFNAMSIZ); 278 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) { 279 strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ); 280 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; 281 ioctl(s, SIOCSIFHWADDR, &ifr); 282 } 283 } 284 close(s); 285 /* If not a wl i/f then simply add it to the bridge */ 286 if (eval("wlconf", name, "up")) 287 { 288#ifdef RT2400_SUPPORT 289 if (strcmp(name, "eth2")==0) 290 { 291 // added by Joey for WL500b + WL127 292 if (nvram_match("wl_channel", "0")) 293 nvram_set("wl_channel", "6"); 294 295 sprintf(tmpstr, "mac_address=%s", nvram_safe_get("et0macaddr")); 296 eval("insmod","rt2400.o",tmpstr); 297 eval("brctl","addif",lan_ifname,"ra0"); 298 ifconfig("ra0",IFUP,NULL,NULL); 299 nvram_set("nobr","1"); 300 301 j = atoi(nvram_safe_get("wl_wdsnum_x")); 302 for(i=1;i<=j;i++) 303 { 304 sprintf(tmpstr, "ra%d", i); ifconfig(tmpstr, IFUP, NULL, NULL); 305 eval("brctl","addif",lan_ifname,tmpstr); 306 } 307 } 308 else 309#endif 310 311#ifdef GUEST_ACCOUNT 312 { 313 if (eval("brctl", "addif", lan_ifname, name)) 314 perror("brctl"); 315 else{ 316 char buf[255],*ptr; 317 ptr = nvram_get("br0_ifnames"); 318 if (ptr) 319 snprintf(buf,sizeof(buf),"%s %s",ptr,name); 320 else 321 strncpy(buf,name,sizeof(buf)); 322 nvram_set("br0_ifnames",buf); 323 } 324 } 325#else 326 eval("brctl", "addif", lan_ifname, name); 327#endif 328 329 } 330 else 331 { 332#ifdef GUEST_ACCOUNT 333 char wl_guest[] = "wlXXXXXXXXXX_guest"; 334 char wl_vifs[]= "wlXXXXXXXXXX_vifs"; 335 char mode[] = "wlXXXXXXXXXX_mode"; 336 char *vifs; 337#endif 338 339 /* get the instance number of the wl i/f */ 340 char wl_name[] = "wlXXXXXXXXXX_mode"; 341 int unit; 342#ifdef ASUS_EXT 343 sync_mac(name, nvram_safe_get("et0macaddr")); 344#endif 345 wl_ioctl(name, WLC_GET_INSTANCE, &unit, sizeof(unit)); 346 snprintf(wl_name, sizeof(wl_name), "wl%d_mode", unit); 347 348#ifdef GUEST_ACCOUNT 349 snprintf(wl_guest, sizeof(wl_guest), "wl%d_guest", unit); 350 snprintf(wl_vifs, sizeof(wl_vifs), "wl%d_vifs", unit); 351 352 /* Multi-SSID specific configuration */ 353 /* Virtual interfaces are created with the master interface 354 by wlconf() . Only copy those that have wlX.Y_guest set*/ 355 356 vifs = nvram_get(wl_vifs); 357 358 if (vifs){ 359 char buf[255]; 360 char name[32]; 361 char vif_guest[32]; 362 char *ptr=NULL,*next=NULL; 363 364 memset(buf,0,sizeof(buf)); 365 366 ptr = nvram_get("unbridged_ifnames"); 367 368 if (ptr) snprintf(buf,sizeof(buf),"%s",ptr); 369 370 /*Loop thru wlX_vifs to get the virtual interfaces 371 wlX.Y_guest must be set for IP to configure it*/ 372 foreach(name,vifs,next){ 373 snprintf(vif_guest,sizeof(vif_guest),"%s_guest",name); 374 cprintf("vif_guest=%s\n", vif_guest); 375 if (nvram_match(vif_guest,"1")){ 376 if(*buf) 377 snprintf(buf,sizeof(buf),"%s %s",buf,name); 378 else 379 strncpy(buf,name,sizeof(buf)); 380 } 381 } 382 383 if (*buf) nvram_set("unbridged_ifnames",buf); 384 } 385#endif 386 /* Receive all multicast frames in WET mode */ 387 if (nvram_match(wl_name, "wet")) 388 { 389 ifconfig(name, IFUP | IFF_ALLMULTI, NULL, NULL); 390#ifdef GUEST_ACCOUNT 391 /* Enable host DHCP relay */ 392 if (nvram_match("lan_dhcp", "1")) 393 wl_iovar_set(name, "wet_host_mac", ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); 394#endif 395 } 396 397 /* Do not attach the main wl i/f if in wds mode */ 398#ifdef GUEST_ACCOUNT 399 if ( !nvram_match(mode, "wds") && !nvram_match(wl_guest, "1")){ 400 char buf[255],*ptr; 401 402 eval("brctl", "addif", lan_ifname, name); 403 404 ptr = nvram_get("br0_ifnames"); 405 if (ptr) 406 snprintf(buf,sizeof(buf),"%s %s",ptr,name); 407 else 408 strncpy(buf,name,sizeof(buf)); 409 nvram_set("br0_ifnames",buf); 410 } 411#else 412 if (nvram_invmatch(wl_name, "wds")) 413 eval("brctl", "addif",lan_ifname,name); 414#endif 415 } 416 } 417 } 418 /* specific non-bridged lan i/f */ 419 else if (strcmp(lan_ifname, "")) { 420 /* Bring up interface */ 421 ifconfig(lan_ifname, IFUP, NULL, NULL); 422 /* config wireless i/f */ 423 if (!eval("wlconf", lan_ifname, "up")) { 424 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 425 int unit; 426 /* get the instance number of the wl i/f */ 427 wl_ioctl(lan_ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)); 428 snprintf(prefix, sizeof(prefix), "wl%d_", unit); 429 /* Receive all multicast frames in WET mode */ 430 if (nvram_match(strcat_r(prefix, "mode", tmp), "wet")) 431 ifconfig(lan_ifname, IFUP | IFF_ALLMULTI, NULL, NULL); 432 } 433 } 434#ifdef GUEST_ACCOUNT 435 /* build unbridged ifnames NVRAM var from wl_guest list */ 436 foreach(name, nvram_safe_get("lan_ifnames"), next) { 437 438 char wl_guest[] = "wlXXXXXXXXXX_guest"; 439 int unit; 440 441 if (!eval("wlconf", name, "up")) { 442 wl_ioctl(name, WLC_GET_INSTANCE, &unit, sizeof(unit)); 443 snprintf(wl_guest, sizeof(wl_guest), "wl%d_guest", unit); 444 445 if (nvram_match(wl_guest,"1")){ 446 char buf[255],*ptr; 447 448 ptr = nvram_get("unbridged_ifnames"); 449 if (ptr) 450 snprintf(buf,sizeof(buf),"%s %s",ptr,name); 451 else 452 strncpy(buf,name,sizeof(buf)); 453 454 nvram_set("unbridged_ifnames",buf); 455 } 456 } 457 } 458#endif 459 /* Get current LAN hardware address */ 460 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { 461 char eabuf[32]; 462 strncpy(ifr.ifr_name, lan_ifname, IFNAMSIZ); 463 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) 464 nvram_set("lan_hwaddr", ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf)); 465 close(s); 466 } 467 468#ifdef WPA2_WMM 469 /* Set QoS mode */ 470 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { 471 int i, qos; 472 caddr_t ifrdata; 473 struct ethtool_drvinfo info; 474 475 qos = (strcmp(nvram_safe_get("wl_wme"), "on")) ? 0 : 1; 476 for (i = 1; i <= DEV_NUMIFS; i ++) { 477 ifr.ifr_ifindex = i; 478 if (ioctl(s, SIOCGIFNAME, &ifr)) 479 continue; 480 if (ioctl(s, SIOCGIFHWADDR, &ifr)) 481 continue; 482 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) 483 continue; 484 /* get flags */ 485 if (ioctl(s, SIOCGIFFLAGS, &ifr)) 486 continue; 487 /* if up(wan not up yet at this point) */ 488 if (ifr.ifr_flags & IFF_UP) { 489 ifrdata = ifr.ifr_data; 490 memset(&info, 0, sizeof(info)); 491 info.cmd = ETHTOOL_GDRVINFO; 492 ifr.ifr_data = (caddr_t)&info; 493 if (ioctl(s, SIOCETHTOOL, &ifr) >= 0) { 494 /* currently only need to set QoS to et devices */ 495 if (!strncmp(info.driver, "et", 2)) { 496 ifr.ifr_data = (caddr_t)&qos; 497 ioctl(s, SIOCSETCQOS, &ifr); 498 } 499 } 500 ifr.ifr_data = ifrdata; 501 } 502 } 503 } 504 close(s); 505#endif 506 507#ifdef ASUS_EXT 508#ifndef FLASH2M 509 /* 510 * Configure DHCP connection. The DHCP client will run 511 * 'udhcpc bound'/'udhcpc deconfig' upon finishing IP address 512 * renew and release. 513 */ 514 if (nvram_match("router_disable", "1")) 515 { 516 if (nvram_match("lan_proto_x", "1")) 517 { 518 char *dhcp_argv[] = { "udhcpc", 519 "-i", lan_ifname, 520 "-p", "/var/run/udhcpc_lan.pid", 521 "-s", "/tmp/landhcpc", 522 NULL 523 }; 524 pid_t pid; 525 526 527 /* Bring up and configure LAN interface */ 528 ifconfig(lan_ifname, IFUP, 529 nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask")); 530 531 symlink("/sbin/rc", "/tmp/landhcpc"); 532 533 /* Start dhcp daemon */ 534 _eval(dhcp_argv, NULL, 0, &pid); 535 } 536 else 537 { 538 /* Bring up and configure LAN interface */ 539 ifconfig(lan_ifname, IFUP, 540 nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask")); 541 lan_up(lan_ifname); 542 543 update_lan_status(1); 544 } 545 } 546 else 547#endif // end of FLASH2M 548 { 549 /* Bring up and configure LAN interface */ 550 ifconfig(lan_ifname, IFUP, 551 nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask")); 552 /* Install lan specific static routes */ 553 add_lan_routes(lan_ifname); 554 555 update_lan_status(1); 556 } 557#else 558 /* Bring up and configure LAN interface */ 559 ifconfig(lan_ifname, IFUP, 560 nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask")); 561 562 /* Install lan specific static routes */ 563 add_lan_routes(lan_ifname); 564#endif 565 566#ifdef GUEST_ACCOUNT 567 /* Bring up unbridged LAN interfaces (if they exist)*/ 568 { 569 char *interfaces,*ifname, *ptr; 570 char word[64], *next; 571 int index =1 ; 572 573 interfaces = nvram_get("unbridged_ifnames"); 574 if (interfaces) 575 foreach(word,interfaces,next){ 576 577 char interface[32], mask[32]; 578 int s; 579 580 ptr=word; 581 ifname = word; 582 index = get_ipconfig_index(ifname); 583 584 if ( index < 0) { 585 cprintf("Cannot find index for interface:%s\n",ifname); 586 continue; 587 } 588 589 snprintf(interface,sizeof(interface),"lan%d_ipaddr",index); 590 snprintf(mask,sizeof(mask),"lan%d_netmask",index); 591 ifconfig(ifname, IFUP,nvram_safe_get(interface), nvram_safe_get(mask)); 592 593 /* Get Ethernet hardware address. Note this value NOT is committed to NVRAM */ 594 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { 595 struct ifreq ifr; 596 char macaddr[]="00:00:00:00:00:00"; 597 char mac[32]; 598 599 memset(&ifr,0,sizeof(ifr)); 600 strncpy(ifr.ifr_name, ifname, IFNAMSIZ); 601 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0){ 602 ether_etoa(ifr.ifr_hwaddr.sa_data, macaddr); 603 snprintf(mac,sizeof(mac),"lan%d_hwaddr",index); 604 nvram_set(mac,macaddr); 605 } 606 close(s); 607 } 608 609 } 610 } 611#endif 612 613#ifndef ASUS_EXT 614 /* Start syslogd if either log_ipaddr or log_ram_enable is set */ 615 if (nvram_invmatch("log_ipaddr", "") || nvram_match("log_ram_enable", "1")) { 616 char *argv[] = { 617 "syslogd", 618 NULL, /* -C */ 619 NULL, NULL, /* -R host */ 620 NULL 621 }; 622 int pid; 623 int argc = 1; 624 625 if (nvram_match("log_ram_enable", "1")) { 626 argv[argc++] = "-C"; 627 } 628 else if (!nvram_match("log_ram_enable", "0")) { 629 nvram_set("log_ram_enable", "0"); 630 } 631 632 if (nvram_invmatch("log_ipaddr", "")) { 633 argv[argc++] = "-R"; 634 argv[argc++] = nvram_get("log_ipaddr"); 635 } 636 637 _eval(argv, NULL, 0, &pid); 638 } 639#endif 640 641 dprintf("%s %s\n", 642 nvram_safe_get("lan_ipaddr"), 643 nvram_safe_get("lan_netmask")); 644} 645 646void 647stop_lan(void) 648{ 649 char *lan_ifname = nvram_safe_get("lan_ifname"); 650 char name[80], *next; 651 652 dprintf("%s\n", lan_ifname); 653 654 /* Stop the syslogd daemon */ 655 eval("killall", "syslogd"); 656 657 /* Remove static routes */ 658 del_lan_routes(lan_ifname); 659 660 /* Bring down LAN interface */ 661 ifconfig(lan_ifname, 0, NULL, NULL); 662 663 /* Bring down bridged interfaces */ 664 if (strncmp(lan_ifname, "br", 2) == 0) { 665#ifdef ASUS_EXT 666 foreach(name, nvram_safe_get("lan_ifnames_t"), next) { 667#else 668 foreach(name, nvram_safe_get("lan_ifnames"), next) { 669#endif 670 eval("wlconf", name, "down"); 671 ifconfig(name, 0, NULL, NULL); 672 eval("brctl", "delif", lan_ifname, name); 673 } 674 eval("brctl", "delbr", lan_ifname); 675 } 676 /* Bring down specific interface */ 677 else if (strcmp(lan_ifname, "")) 678 eval("wlconf", lan_ifname, "down"); 679 680 dprintf("done\n"); 681} 682 683static int 684wan_prefix(char *ifname, char *prefix) 685{ 686 int unit; 687 688 if ((unit = wan_ifunit(ifname)) < 0) 689 return -1; 690 691 sprintf(prefix, "wan%d_", unit); 692 return 0; 693} 694 695static int 696add_wan_routes(char *wan_ifname) 697{ 698 char prefix[] = "wanXXXXXXXXXX_"; 699 700 /* Figure out nvram variable name prefix for this i/f */ 701 if (wan_prefix(wan_ifname, prefix) < 0) 702 return -1; 703 704 return add_routes(prefix, "route", wan_ifname); 705} 706 707static int 708del_wan_routes(char *wan_ifname) 709{ 710 char prefix[] = "wanXXXXXXXXXX_"; 711 712 /* Figure out nvram variable name prefix for this i/f */ 713 if (wan_prefix(wan_ifname, prefix) < 0) 714 return -1; 715 716 return del_routes(prefix, "route", wan_ifname); 717} 718 719int 720wan_valid(char *ifname) 721{ 722 char name[80], *next; 723 724 foreach(name, nvram_safe_get("wan_ifnames"), next) 725 if (ifname && !strcmp(ifname, name)) 726 return 1; 727 728#ifdef WIRELESS_WAN 729 if (nvram_invmatch("wl_mode_ex", "ap")) { 730 return nvram_match("wl0_ifname", ifname); 731 } 732#endif 733 return 0; 734} 735 736void 737start_wan(void) 738{ 739 char *wan_ifname; 740 char *wan_proto; 741 int unit; 742 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 743 char eabuf[32]; 744 int s; 745 struct ifreq ifr; 746 pid_t pid; 747 748 /* check if we need to setup WAN */ 749 if (nvram_match("router_disable", "1") 750#ifdef BTN_SETUP 751 || is_ots()/* Cherry Cho unmarked in 2007/2/13. */ 752#endif 753 ) 754 return; 755 756#ifdef ASUS_EXT 757 update_wan_status(0); 758#else 759 /* start connection independent firewall */ 760 start_firewall(); 761#endif 762 763#if 0 764// 2008.08 James. If wan port wasn't plugged, the filter also can be used. { 765 char logaccept[16], logdrop[16], wan_if[8]; 766 767 memset(logaccept, 0, 16); 768 memset(logdrop, 0, 16); 769 memset(wan_if, 0, 8); 770 771 /* Determine the log type */ 772 if(nvram_match("fw_log_x", "accept") || nvram_match("fw_log_x", "both")) 773 strcpy(logaccept, "logaccept"); 774 else 775 strcpy(logaccept, "ACCEPT"); 776 777 if(nvram_match("fw_log_x", "drop") || nvram_match("fw_log_x", "both")) 778 strcpy(logdrop, "logdrop"); 779 else 780 strcpy(logdrop, "DROP"); 781 782 if(nvram_match("wan_proto", "pppoe") 783 || nvram_match("wan_proto", "pptp") 784 || nvram_match("wan_proto", "l2tp") 785#ifdef CDMA // HSDPA 786 || strcmp(nvram_safe_get("hsdpa_product"), "") != 0 787#endif 788 ) 789 strcpy(wan_if, "ppp0"); 790 else 791 strcpy(wan_if, "eth0"); 792 793 /* Filter setting */ 794 filter_setting(wan_if, NULL, "br0", NULL, logaccept, logdrop); 795// 2008.08 James. } 796#endif 797 /* Create links */ 798 mkdir("/tmp/ppp", 0777); 799#ifdef CDMA 800 mkdir("/tmp/ppp/peers", 0777); // HSDPA 801#endif 802 symlink("/sbin/rc", "/tmp/ppp/ip-up"); 803 symlink("/sbin/rc", "/tmp/ppp/ip-down"); 804 symlink("/sbin/rc", "/tmp/udhcpc"); 805 806 //symlink("/dev/null", "/tmp/ppp/connect-errors"); 807 808 /* Start each configured and enabled wan connection and its undelying i/f */ 809 for (unit = 0; unit < MAX_NVPARSE; unit ++) 810 { 811#ifdef ASUS_EXT // Only multiple pppoe is allowed 812 if (unit>0 && nvram_invmatch("wan_proto", "pppoe")) break; 813#endif 814 815 snprintf(prefix, sizeof(prefix), "wan%d_", unit); 816 817 /* make sure the connection exists and is enabled */ 818 wan_ifname = nvram_get(strcat_r(prefix, "ifname", tmp)); 819 if (!wan_ifname) 820 continue; 821 wan_proto = nvram_get(strcat_r(prefix, "proto", tmp)); 822 if (!wan_proto || !strcmp(wan_proto, "disabled")) 823 continue; 824 825 /* disable the connection if the i/f is not in wan_ifnames */ 826 if (!wan_valid(wan_ifname)) { 827 nvram_set(strcat_r(prefix, "proto", tmp), "disabled"); 828 continue; 829 } 830 831 dprintf("%s %s\n\n\n\n\n", wan_ifname, wan_proto); 832 833 /* Set i/f hardware address before bringing it up */ 834 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 835 continue; 836 837 strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ); 838 839 /* Since WAN interface may be already turned up (by vlan.c), 840 if WAN hardware address is specified (and different than the current one), 841 we need to make it down for synchronizing hwaddr. */ 842 if (ioctl(s, SIOCGIFHWADDR, &ifr)) { 843 close(s); 844 continue; 845 } 846 847 ether_atoe(nvram_safe_get(strcat_r(prefix, "hwaddr", tmp)), eabuf); 848 if (bcmp(eabuf, ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN)) 849 { 850 /* current hardware address is different than user specified */ 851 ifconfig(wan_ifname, 0, NULL, NULL); 852 } 853 854 /* Configure i/f only once, specially for wireless i/f shared by multiple connections */ 855 if (ioctl(s, SIOCGIFFLAGS, &ifr)) { 856 close(s); 857 continue; 858 } 859 if (!(ifr.ifr_flags & IFF_UP)) { 860 /* Sync connection nvram address and i/f hardware address */ 861 memset(ifr.ifr_hwaddr.sa_data, 0, ETHER_ADDR_LEN); 862 863 if (!nvram_invmatch(strcat_r(prefix, "hwaddr", tmp), "") || 864 !ether_atoe(nvram_safe_get(strcat_r(prefix, "hwaddr", tmp)), ifr.ifr_hwaddr.sa_data) || 865 !memcmp(ifr.ifr_hwaddr.sa_data, "\0\0\0\0\0\0", ETHER_ADDR_LEN)) { 866 if (ioctl(s, SIOCGIFHWADDR, &ifr)) { 867 close(s); 868 continue; 869 } 870 nvram_set(strcat_r(prefix, "hwaddr", tmp), ether_etoa(ifr.ifr_hwaddr.sa_data, eabuf)); 871 } 872 else { 873 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; 874 ioctl(s, SIOCSIFHWADDR, &ifr); 875 } 876 877 /* Bring up i/f */ 878 ifconfig(wan_ifname, IFUP, NULL, NULL); 879 880#ifdef URE 881 /* do wireless specific config */ 882 if(nvram_match("ure_disable", "1")) 883 { 884 eval("wlconf", wan_ifname, "up"); 885 } 886#else 887 /* do wireless specific config */ 888 eval("wlconf", wan_ifname, "up"); 889#endif 890 } 891 892 close(s); 893 894#ifdef ASUS_EXT 895 if (unit==0) 896 { 897 FILE *fp; 898 899 setup_ethernet(nvram_safe_get("wan_ifname")); 900 start_pppoe_relay(wan_ifname); 901 902 /* Enable Forwarding */ 903 if ((fp = fopen("/proc/sys/net/ipv4/ip_forward", "r+"))) { 904 fputc('1', fp); 905 fclose(fp); 906 } else 907 { 908 perror("/proc/sys/net/ipv4/ip_forward"); 909 } 910 } 911 912 /* 913 * Configure PPPoE connection. The PPPoE client will run 914 * ip-up/ip-down scripts upon link's connect/disconnect. 915 */ 916#ifdef CDMA // HSDPA { 917 if (!nvram_match("hsdpa_product", "") && nvram_match("cdma_down", "1")) 918 { 919 nvram_set("cdma_down", "0"); 920 start_cdma(); 921 update_wan_status(0); 922 } 923 else 924#endif // HSDPA } 925 if (strcmp(wan_proto, "pppoe") == 0 || strcmp(wan_proto, "pptp") == 0 || strcmp(wan_proto, "l2tp") == 0) 926 { 927 int demand = atoi(nvram_safe_get(strcat_r(prefix, "pppoe_idletime", tmp))) && 928 strcmp(wan_proto, "l2tp") /* L2TP does not support idling */ ; 929 930 /* update demand option */ 931 nvram_set(strcat_r(prefix, "pppoe_demand", tmp), demand ? "1" : "0"); 932 933 /* Bring up WAN interface */ 934 ifconfig(wan_ifname, IFUP, 935 nvram_get(strcat_r(prefix, "pppoe_ipaddr", tmp)), 936 nvram_get(strcat_r(prefix, "pppoe_netmask", tmp))); 937 938 /* start firewall */ 939 start_firewall_ex(nvram_safe_get(strcat_r(prefix, "pppoe_ifname", tmp)), 940 "0.0.0.0", "br0", nvram_safe_get("lan_ipaddr")); 941 942 /* launch dhcp client and wait for lease forawhile */ 943 if (nvram_match(strcat_r(prefix, "pppoe_ipaddr", tmp), "0.0.0.0")) 944 { 945printf("*** when pppoe_ipaddr=0.0.0.0. ***\n"); 946 char *wan_hostname = nvram_get(strcat_r(prefix, "hostname", tmp)); 947 char *dhcp_argv[] = { "udhcpc", 948 "-i", wan_ifname, 949 "-p", (sprintf(tmp, "/var/run/udhcpc%d.pid", unit), tmp), 950 "-s", "/tmp/udhcpc", 951 "-b", 952 wan_hostname && *wan_hostname ? "-H" : NULL, 953 wan_hostname && *wan_hostname ? wan_hostname : NULL, 954 NULL 955 }; 956 /* Start dhcp daemon */ 957 _eval(dhcp_argv, NULL, 0, NULL); 958 } 959 else { 960 /* setup static wan routes via physical device */ 961 add_routes("wan_", "route", wan_ifname); 962 /* and set default route if specified with metric 1 */ 963 if (inet_addr_(nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp))) && 964 !nvram_match("wan_heartbeat_x", "")) 965 route_add(wan_ifname, 2, "0.0.0.0", 966 nvram_safe_get(strcat_r(prefix, "pppoe_gateway", tmp)), "0.0.0.0"); 967 /* start multicast router */ 968 start_igmpproxy(wan_ifname); 969 } 970 971 /* launch pppoe client daemon */ 972 start_pppd(prefix); 973 974 /* ppp interface name is referenced from this point on */ 975 wan_ifname = nvram_safe_get(strcat_r(prefix, "pppoe_ifname", tmp)); 976 977 /* Pretend that the WAN interface is up */ 978 if (nvram_match(strcat_r(prefix, "pppoe_demand", tmp), "1")) 979 { 980 int timeout = 5; 981 /* Wait for pppx to be created */ 982 while (ifconfig(wan_ifname, IFUP, NULL, NULL) && timeout--) 983 sleep(1); 984 985 /* Retrieve IP info */ 986 if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 987 continue; 988 strncpy(ifr.ifr_name, wan_ifname, IFNAMSIZ); 989 990 /* Set temporary IP address */ 991 if (ioctl(s, SIOCGIFADDR, &ifr)) 992 perror(wan_ifname); 993 nvram_set(strcat_r(prefix, "ipaddr", tmp), inet_ntoa(sin_addr(&ifr.ifr_addr))); 994 nvram_set(strcat_r(prefix, "netmask", tmp), "255.255.255.255"); 995 996 /* Set temporary P-t-P address */ 997 if (ioctl(s, SIOCGIFDSTADDR, &ifr)) 998 perror(wan_ifname); 999 nvram_set(strcat_r(prefix, "gateway", tmp), inet_ntoa(sin_addr(&ifr.ifr_dstaddr))); 1000 1001 close(s); 1002 1003 /* 1004 * Preset routes so that traffic can be sent to proper pppx even before 1005 * the link is brought up. 1006 */ 1007 preset_wan_routes(wan_ifname); 1008 } 1009#ifdef ASUS_EXT 1010 nvram_set("wan_ifname_t", wan_ifname); 1011#endif 1012 } 1013#endif 1014 /* 1015 * Configure DHCP connection. The DHCP client will run 1016 * 'udhcpc bound'/'udhcpc deconfig' upon finishing IP address 1017 * renew and release. 1018 */ 1019 else if (!strcmp(wan_proto, "dhcp") 1020#ifdef BIGPOND 1021 || !strcmp(wan_proto, "bigpond") 1022#endif 1023 ) { 1024 char *wan_hostname = nvram_get(strcat_r(prefix, "hostname", tmp)); 1025 char *dhcp_argv[] = { "udhcpc", 1026 "-i", wan_ifname, 1027 "-p", (sprintf(tmp, "/var/run/udhcpc%d.pid", unit), tmp), 1028 "-s", "/tmp/udhcpc", 1029 wan_hostname && *wan_hostname ? "-H" : NULL, 1030 wan_hostname && *wan_hostname ? wan_hostname : NULL, 1031 NULL 1032 }; 1033 /* start firewall */ 1034 start_firewall_ex(wan_ifname, "0.0.0.0", "br0", nvram_safe_get("lan_ipaddr")); 1035 1036 /* Start dhcp daemon */ 1037 _eval(dhcp_argv, NULL, 0, &pid); 1038 /* Update wan information for null DNS server */ 1039 update_wan_status(1); 1040 1041#ifdef ASUS_EXT 1042 wanmessage("Can not get IP from server"); 1043 nvram_set("wan_ifname_t", wan_ifname); 1044#endif 1045 } 1046 /* Configure static IP connection. */ 1047 else if (strcmp(wan_proto, "static") == 0) { 1048 /* Assign static IP address to i/f */ 1049 ifconfig(wan_ifname, IFUP, 1050 nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), 1051 nvram_safe_get(strcat_r(prefix, "netmask", tmp))); 1052 /* We are done configuration */ 1053 wan_up(wan_ifname); 1054#ifdef ASUS_EXT 1055 nvram_set("wan_ifname_t", wan_ifname); 1056#endif 1057 } 1058 1059#ifndef ASUS_EXT 1060 /* Start connection dependent firewall */ 1061 start_firewall2(wan_ifname); 1062#endif 1063 1064 dprintf("%s %s\n", 1065 nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), 1066 nvram_safe_get(strcat_r(prefix, "netmask", tmp))); 1067 } 1068 1069 /* Report stats */ 1070 if (nvram_invmatch("stats_server", "")) { 1071 char *stats_argv[] = { "stats", nvram_get("stats_server"), NULL }; 1072 _eval(stats_argv, NULL, 5, NULL); 1073 } 1074} 1075 1076void 1077stop_wan(void) 1078{ 1079 char name[80], *next, signal[] = "XXXX"; 1080 1081 eval("killall", "stats"); 1082 eval("killall", "ntpclient"); 1083 1084 /* Shutdown and kill all possible tasks */ 1085 eval("killall", "ip-up"); 1086 eval("killall", "ip-down"); 1087#if 0 1088 snprintf(signal, sizeof(signal), "-%d", SIGHUP); 1089 eval("killall", signal, "pppoecd"); 1090 eval("killall", signal, "pppd"); 1091 eval("killall", "pppoecd"); 1092#endif 1093 eval("killall", "l2tpd"); 1094 eval("killall", "pppd"); 1095#ifdef CDMA 1096 eval("killall", "chat"); 1097#endif 1098 snprintf(signal, sizeof(signal), "-%d", SIGUSR2); 1099 eval("killall", signal, "udhcpc"); 1100 eval("killall", "udhcpc"); 1101 eval("killall", "igmpproxy"); 1102 1103 /* Bring down WAN interfaces */ 1104 foreach(name, nvram_safe_get("wan_ifnames"), next) 1105 { 1106 ifconfig(name, 0, NULL, NULL); 1107 } 1108 1109 /* Remove dynamically created links */ 1110 unlink("/tmp/udhcpc"); 1111 1112 unlink("/tmp/ppp/ip-up"); 1113 unlink("/tmp/ppp/ip-down"); 1114// unlink("/tmp/ppp/options"); 1115 rmdir("/tmp/ppp"); 1116 1117#ifdef ASUS_EXT 1118 update_wan_status(0); 1119#endif 1120 dprintf("done\n"); 1121} 1122 1123void 1124stop_wan2(void) 1125{ 1126 char name[80], *next, signal[] = "XXXX"; 1127 1128 eval("killall", "stats"); 1129 eval("killall", "ntpclient"); 1130 1131 /* Shutdown and kill all possible tasks */ 1132 eval("killall", "ip-up"); 1133 eval("killall", "ip-down"); 1134 /*snprintf(signal, sizeof(signal), "-%d", SIGHUP); 1135 eval("killall", signal, "pppoecd"); 1136 eval("killall", signal, "pppd");//*/ 1137 eval("killall", "pppoecd"); 1138 eval("killall", "l2tpd"); 1139 eval("killall", "pppd"); 1140 1141 snprintf(signal, sizeof(signal), "-%d", SIGUSR2); 1142 eval("killall", signal, "udhcpc"); 1143 eval("killall", "udhcpc"); 1144 1145 /* Remove dynamically created links */ 1146 unlink("/tmp/udhcpc"); 1147 1148 unlink("/tmp/ppp/ip-up"); 1149 unlink("/tmp/ppp/ip-down"); 1150 unlink("/tmp/ppp/options"); 1151 rmdir("/tmp/ppp"); 1152 1153#ifdef ASUS_EXT 1154 if(nvram_invmatch("wan_ifname_t", "")) wan_down(nvram_safe_get("wan_ifname_t")); 1155#endif 1156 1157 dprintf("done\n"); 1158} 1159 1160int 1161update_resolvconf(void) 1162{ 1163 FILE *fp; 1164 char word[100], *next; 1165 1166 /* check if auto dns enabled */ 1167 if (!nvram_match("wan_dnsenable_x", "1")) 1168 return 0; 1169 1170 if (!(fp = fopen("/tmp/resolv.conf", "w+"))) { 1171 perror("/tmp/resolv.conf"); 1172 return errno; 1173 } 1174 1175 foreach(word, (*nvram_safe_get("wan0_dns") ? nvram_safe_get("wan0_dns") : 1176 nvram_safe_get("wanx_dns")), next) 1177 { 1178 fprintf(fp, "nameserver %s\n", word); 1179 } 1180 fclose(fp); 1181 1182#ifdef ASUS_EXT 1183 stop_dns(); 1184 start_dns(); 1185#else 1186 /* notify dnsmasq */ 1187 snprintf(tmp, sizeof(tmp), "-%d", SIGHUP); 1188 eval("killall", tmp, "dnsmasq"); 1189#endif 1190 1191 return 0; 1192} 1193 1194void 1195wan_up(char *wan_ifname) 1196{ 1197 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 1198 char *wan_proto, *gateway; 1199 1200 /* Figure out nvram variable name prefix for this i/f */ 1201 if (wan_prefix(wan_ifname, prefix) < 0) 1202 { 1203 /* called for dhcp+ppp */ 1204 if (!nvram_match("wan0_ifname", wan_ifname)) 1205 return; 1206 1207 /* re-start firewall with old ppp0 address or 0.0.0.0 */ 1208 start_firewall_ex("ppp0", nvram_safe_get("wan0_ipaddr"), 1209 "br0", nvram_safe_get("lan_ipaddr")); 1210 1211 /* setup static wan routes via physical device */ 1212 add_routes("wan_", "route", wan_ifname); 1213 /* and one supplied via DHCP */ 1214 add_wanx_routes("wanx_", wan_ifname, 0); 1215 1216 gateway = inet_addr_(nvram_safe_get("wan_gateway")) != INADDR_ANY ? 1217 nvram_get("wan_gateway") : nvram_safe_get("wanx_gateway"); 1218 1219 /* and default route with metric 1 */ 1220 if (inet_addr_(gateway) != INADDR_ANY) 1221 { 1222 char word[100], *next; 1223 1224 route_add(wan_ifname, 2, "0.0.0.0", gateway, "0.0.0.0"); 1225 1226 /* ... and to dns servers as well for demand ppp to work */ 1227 if (nvram_match("wan_dnsenable_x", "1")) 1228 foreach(word, nvram_safe_get("wanx_dns"), next) 1229 { 1230 in_addr_t mask = inet_addr(nvram_safe_get("wanx_netmask")); 1231 if ((inet_addr(word) & mask) != (inet_addr(nvram_safe_get("wanx_ipaddr")) & mask)) 1232 route_add(wan_ifname, 2, word, gateway, "255.255.255.255"); 1233 } 1234 } 1235 1236 /* start multicast router */ 1237 start_igmpproxy(wan_ifname); 1238 1239 update_resolvconf(); 1240 1241 return; 1242 } 1243 1244 wan_proto = nvram_safe_get(strcat_r(prefix, "proto", tmp)); 1245 1246 dprintf("%s %s\n", wan_ifname, wan_proto); 1247 1248 /* Set default route to gateway if specified */ 1249 if (nvram_match(strcat_r(prefix, "primary", tmp), "1")) 1250 { 1251 if (strcmp(wan_proto, "dhcp") == 0 || strcmp(wan_proto, "static") == 0) 1252 { 1253 /* the gateway is in the local network */ 1254 route_add(wan_ifname, 0, nvram_safe_get(strcat_r(prefix, "gateway", tmp)), NULL, "255.255.255.255"); 1255 } 1256 /* default route via default gateway */ 1257 route_add(wan_ifname, 0, "0.0.0.0", nvram_safe_get(strcat_r(prefix, "gateway", tmp)), "0.0.0.0"); 1258 /* hack: avoid routing cycles, when both peer and server has the same IP */ 1259 if (strcmp(wan_proto, "pptp") == 0 1260 || strcmp(wan_proto, "l2tp") == 0 1261#ifdef CDMA // HSDPA 1262 || strcmp(nvram_safe_get("hsdpa_product"), "") != 0 1263#endif 1264 ) { 1265 /* delete gateway route as it's no longer needed */ 1266 route_del(wan_ifname, 0, nvram_safe_get(strcat_r(prefix, "gateway", tmp)), "0.0.0.0", "255.255.255.255"); 1267 } 1268 } 1269 1270 /* Install interface dependent static routes */ 1271 add_wan_routes(wan_ifname); 1272 1273 /* setup static wan routes via physical device */ 1274 if (strcmp(wan_proto, "dhcp") == 0 || strcmp(wan_proto, "static") == 0) 1275 { 1276 nvram_set("wanx_gateway", nvram_safe_get(strcat_r(prefix, "gateway", tmp))); 1277 add_routes("wan_", "route", wan_ifname); 1278 } 1279 1280 /* and one supplied via DHCP */ 1281 if (strcmp(wan_proto, "dhcp") == 0) 1282 add_wanx_routes(prefix, wan_ifname, 0); 1283 1284 /* Add dns servers to resolv.conf */ 1285 update_resolvconf(); 1286 1287 /* Sync time */ 1288 //start_ntpc(); 1289#ifdef ASUS_EXT 1290 update_wan_status(1); 1291 1292 start_firewall_ex(wan_ifname, nvram_safe_get(strcat_r(prefix, "ipaddr", tmp)), 1293 "br0", nvram_safe_get("lan_ipaddr")); 1294 1295 start_ddns(); 1296 stop_upnp(); 1297 start_upnp(); 1298#endif 1299 1300#ifdef BIGPOND 1301 if (strcmp(wan_proto, "bigpond")==0) 1302 { 1303 stop_bpalogin(); 1304 start_bpalogin(); 1305 } 1306#endif 1307 1308#ifdef CDMA 1309 if (!nvram_match("hsdpa_product", "")) // HSDPA 1310 { 1311 nvram_set("cdma_down", "2"); 1312 nvram_set("got_HSDPA", "1"); 1313 } 1314#endif 1315#ifdef QOS 1316 nvram_set("qos_userspec_app", "0"); 1317 nvram_set("qos_global_enable", "0"); 1318 nvram_set("qos_userdef_enable", "0"); 1319 nvram_set("qos_enable", "0"); 1320 set_clean_conntrack_flag(0, 0); 1321 1322 unsigned long wanip = 0; 1323 1324 if(nvram_invmatch("qos_rulenum_x", "0")) 1325 nvram_set("qos_userspec_app", "1"); 1326 1327 if(nvram_match("qos_pshack_prio", "1") 1328 || nvram_match("qos_shortpkt_prio", "1") 1329 || nvram_match("qos_service_enable", "1") 1330 || nvram_match("qos_tos_prio", "1") 1331 ) 1332 nvram_set("qos_global_enable", "1"); 1333 1334 if(nvram_match("qos_userspec_app", "1") || nvram_match("qos_dfragment_enable", "1")) 1335 nvram_set("qos_userdef_enable", "1"); 1336 1337 if(nvram_match("qos_global_enable", "1") || nvram_match("qos_userdef_enable", "1")){ 1338 nvram_set("qos_enable", "1"); 1339 wanip = inet_network(nvram_safe_get("wan0_ipaddr")); 1340 set_clean_conntrack_flag(1, wanip); 1341 } 1342 else{ 1343 nvram_set("qos_enable", "0"); 1344 set_clean_conntrack_flag(0, 0); 1345 } 1346 1347 if(nvram_match("qos_enable", "1")){ 1348 nvram_set("qos_ubw", "0"); 1349 nvram_set("qos_ubw_tmp", "0"); 1350 1351 if(nvram_invmatch("qos_manual_ubw", "0") && nvram_invmatch("qos_manual_ubw", "")){ 1352 nvram_set("qos_ubw", nvram_get("qos_manual_ubw")); 1353 nvram_set("qos_ubw_tmp", nvram_get("qos_manual_ubw")); 1354 } 1355 else{ 1356 qos_get_wan_rate(); 1357 nvram_set("qos_ubw_tmp", nvram_get("qos_ubw")); 1358 } 1359 1360 Speedtest_Init(); 1361 } 1362#endif 1363 1364 /* start multicast router */ 1365 if(!strcmp(wan_proto, "dhcp") 1366#ifdef BIGPOND 1367 || !strcmp(wan_proto, "bigpond") 1368#endif 1369 || !strcmp(wan_proto, "static")) 1370 { 1371 start_igmpproxy(wan_ifname); 1372 } 1373 1374 dprintf("done\n"); 1375} 1376 1377void 1378wan_down(char *wan_ifname) 1379{ 1380 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 1381 char *wan_proto; 1382 1383 /* Figure out nvram variable name prefix for this i/f */ 1384 if (wan_prefix(wan_ifname, prefix) < 0) 1385 return; 1386 1387 wan_proto = nvram_safe_get(strcat_r(prefix, "proto", tmp)); 1388 1389 dprintf("%s %s\n", wan_ifname, wan_proto); 1390 1391 /* Remove default route to gateway if specified */ 1392 if (nvram_match(strcat_r(prefix, "primary", tmp), "1")) 1393 route_del(wan_ifname, 0, "0.0.0.0", 1394 nvram_safe_get(strcat_r(prefix, "gateway", tmp)), 1395 "0.0.0.0"); 1396 1397 /* Del route while static IP */ 1398 if (strcmp(wan_proto, "static") == 0) 1399 ifconfig(wan_ifname, IFUP, "0.0.0.0", NULL); 1400 1401 /* Remove interface dependent static routes */ 1402 del_wan_routes(wan_ifname); 1403 1404 /* Update resolv.conf -- leave as is if no dns servers left for demand to work */ 1405 if (*nvram_safe_get("wanx_dns")) 1406 nvram_unset(strcat_r(prefix, "dns", tmp)); 1407 update_resolvconf(); 1408 1409#ifdef ASUS_EXT 1410 //printf("update wan status\n"); 1411 update_wan_status(0); 1412#endif 1413 1414#ifdef BIGPOND 1415 if(!strcmp(wan_proto, "bigpond")) 1416 stop_bpalogin(); 1417#endif 1418 1419#ifdef CDMA 1420 if (!nvram_match("hsdpa_product", "")) // HSDPA 1421 { 1422 stop_cdma(); 1423csprintf("--- network: CDMA=%s! ---\n", nvram_safe_get("cdma_down")); 1424 if(nvram_invmatch("cdma_down", "99") 1425 && nvram_invmatch("cdma_down", "3")){ 1426 nvram_set("cdma_down", "1"); 1427csprintf("--- network: Wrong! ---\n"); 1428 } 1429 } 1430#endif 1431 1432 nvram_set("wan_ready", "0"); 1433 1434 dprintf("done\n"); 1435} 1436 1437#ifdef ASUS_EXT 1438#ifndef FLASH2M 1439void 1440lan_up(char *lan_ifname) 1441{ 1442 FILE *fp; 1443 char word[100], *next; 1444 char line[100]; 1445 1446 /* Set default route to gateway if specified */ 1447 route_add(lan_ifname, 0, "0.0.0.0", 1448 nvram_safe_get("lan_gateway"), 1449 "0.0.0.0"); 1450 1451 /* Open resolv.conf to read */ 1452 if (!(fp = fopen("/tmp/resolv.conf", "w"))) { 1453 perror("/tmp/resolv.conf"); 1454 return; 1455 } 1456 1457 if (nvram_invmatch("lan_gateway", "")) 1458 fprintf(fp, "nameserver %s\n", nvram_safe_get("lan_gateway")); 1459 1460 foreach(word, nvram_safe_get("lan_dns"), next) 1461 { 1462 fprintf(fp, "nameserver %s\n", word); 1463 } 1464 fclose(fp); 1465 1466 /* Sync time */ 1467 //start_ntpc(); 1468} 1469 1470void 1471lan_down(char *lan_ifname) 1472{ 1473 /* Remove default route to gateway if specified */ 1474 route_del(lan_ifname, 0, "0.0.0.0", 1475 nvram_safe_get("lan_gateway"), 1476 "0.0.0.0"); 1477 1478 /* remove resolv.conf */ 1479 unlink("/tmp/resolv.conf"); 1480} 1481 1482 1483void 1484lan_up_ex(char *lan_ifname) 1485{ 1486 FILE *fp; 1487 char word[100], *next; 1488 char line[100]; 1489 1490 /* Set default route to gateway if specified */ 1491 route_add(lan_ifname, 0, "0.0.0.0", 1492 nvram_safe_get("lan_gateway_t"), 1493 "0.0.0.0"); 1494 1495 /* Open resolv.conf to read */ 1496 if (!(fp = fopen("/tmp/resolv.conf", "w"))) { 1497 perror("/tmp/resolv.conf"); 1498 return; 1499 } 1500 1501 if (nvram_invmatch("lan_gateway_t", "")) 1502 fprintf(fp, "nameserver %s\n", nvram_safe_get("lan_gateway_t")); 1503 1504 foreach(word, nvram_safe_get("lan_dns_t"), next) 1505 { 1506 fprintf(fp, "nameserver %s\n", word); 1507 } 1508 fclose(fp); 1509 1510 /* Sync time */ 1511 //start_ntpc(); 1512 //update_lan_status(1); 1513} 1514 1515void 1516lan_down_ex(char *lan_ifname) 1517{ 1518 /* Remove default route to gateway if specified */ 1519 route_del(lan_ifname, 0, "0.0.0.0", 1520 nvram_safe_get("lan_gateway_t"), 1521 "0.0.0.0"); 1522 1523 /* remove resolv.conf */ 1524 unlink("/tmp/resolv.conf"); 1525 1526 update_lan_status(0); 1527} 1528#endif 1529#endif 1530 1531static int 1532notify_nas(char *type, char *ifname, char *action) 1533{ 1534 char *argv[] = {"nas4not", type, ifname, action, 1535 NULL, /* role */ 1536 NULL, /* crypto */ 1537 NULL, /* auth */ 1538 NULL, /* passphrase */ 1539 NULL, /* ssid */ 1540 NULL}; 1541 char *str = NULL; 1542 int retries = 10; 1543 char tmp[100], prefix[] = "wlXXXXXXXXXX_"; 1544 int unit; 1545 char remote[ETHER_ADDR_LEN]; 1546 char ssid[48], pass[80], auth[16], crypto[16], role[8]; 1547 int i; 1548 1549 /* the wireless interface must be configured to run NAS */ 1550 wl_ioctl(ifname, WLC_GET_INSTANCE, &unit, sizeof(unit)); 1551 snprintf(prefix, sizeof(prefix), "wl%d_", unit); 1552 if (nvram_match(strcat_r(prefix, "akm", tmp), "") && 1553 nvram_match(strcat_r(prefix, "auth_mode", tmp), "none")) 1554 return 0; 1555 1556 /* find WDS link configuration */ 1557 wl_ioctl(ifname, WLC_WDS_GET_REMOTE_HWADDR, remote, ETHER_ADDR_LEN); 1558 for (i = 0; i < MAX_NVPARSE; i ++) { 1559 char mac[ETHER_ADDR_STR_LEN]; 1560 uint8 ea[ETHER_ADDR_LEN]; 1561 1562 if (get_wds_wsec(unit, i, mac, role, crypto, auth, ssid, pass) && 1563 ether_atoe(mac, ea) && !bcmp(ea, remote, ETHER_ADDR_LEN)) { 1564 argv[4] = role; 1565 argv[5] = crypto; 1566 argv[6] = auth; 1567 argv[7] = pass; 1568 argv[8] = ssid; 1569 break; 1570 } 1571 } 1572 1573 /* did not find WDS link configuration, use wireless' */ 1574 if (i == MAX_NVPARSE) { 1575 /* role */ 1576 argv[4] = "auto"; 1577 /* crypto */ 1578 argv[5] = nvram_safe_get(strcat_r(prefix, "crypto", tmp)); 1579 /* auth mode */ 1580 argv[6] = nvram_safe_get(strcat_r(prefix, "akm", tmp)); 1581 /* passphrase */ 1582 argv[7] = nvram_safe_get(strcat_r(prefix, "wpa_psk", tmp)); 1583 /* ssid */ 1584 argv[8] = nvram_safe_get(strcat_r(prefix, "ssid", tmp)); 1585 } 1586 1587 /* wait till nas is started */ 1588 while (retries -- > 0 && !(str = file2str("/tmp/nas.lan.pid"))) 1589 sleep(1); 1590 if (str) { 1591 int pid; 1592 free(str); 1593 return _eval(argv, ">/dev/console", 0, &pid); 1594 } 1595 return -1; 1596} 1597 1598int 1599hotplug_net(void) 1600{ 1601 char *lan_ifname = nvram_safe_get("lan_ifname"); 1602 char *interface, *action; 1603 1604 if (!(interface = getenv("INTERFACE")) || 1605 !(action = getenv("ACTION"))) 1606 return EINVAL; 1607 1608 if (strncmp(interface, "wds", 3)) 1609 return 0; 1610 1611 if (!strcmp(action, "register")) { 1612 /* Bring up the interface and add to the bridge */ 1613 ifconfig(interface, IFUP, NULL, NULL); 1614 1615 /* Bridge WDS interfaces */ 1616 if (!strncmp(lan_ifname, "br", 2) && 1617 eval("brctl", "addif", lan_ifname, interface)) 1618 return 0; 1619 1620 /* Notify NAS of adding the interface */ 1621 notify_nas("lan", interface, "up"); 1622 } 1623 return 0; 1624} 1625 1626 1627int 1628wan_ifunit(char *wan_ifname) 1629{ 1630 int unit; 1631 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 1632 1633 if ((unit = ppp_ifunit(wan_ifname)) >= 0) 1634 return unit; 1635 else { 1636 for (unit = 0; unit < MAX_NVPARSE; unit ++) { 1637 snprintf(prefix, sizeof(prefix), "wan%d_", unit); 1638 if (nvram_match(strcat_r(prefix, "ifname", tmp), wan_ifname) && 1639 (nvram_match(strcat_r(prefix, "proto", tmp), "dhcp") || 1640#ifdef BIGPOND 1641 nvram_match(strcat_r(prefix, "proto", tmp), "bigpond") || 1642#endif 1643#ifdef CDMA 1644 nvram_match(strcat_r(prefix, "proto", tmp), "cdma") || 1645#endif 1646 nvram_match(strcat_r(prefix, "proto", tmp), "static"))) 1647 return unit; 1648 } 1649 } 1650 return -1; 1651} 1652 1653int 1654preset_wan_routes(char *wan_ifname) 1655{ 1656 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 1657 1658 1659 /* Figure out nvram variable name prefix for this i/f */ 1660 if (wan_prefix(wan_ifname, prefix) < 0) 1661 return -1; 1662 1663 /* Set default route to gateway if specified */ 1664 if (nvram_match(strcat_r(prefix, "primary", tmp), "1")) 1665 { 1666 route_add(wan_ifname, 0, "0.0.0.0", "0.0.0.0", "0.0.0.0"); 1667 } 1668 1669 /* Install interface dependent static routes */ 1670 add_wan_routes(wan_ifname); 1671 return 0; 1672} 1673 1674int 1675wan_primary_ifunit(void) 1676{ 1677 int unit; 1678 1679 for (unit = 0; unit < MAX_NVPARSE; unit ++) { 1680 char tmp[100], prefix[] = "wanXXXXXXXXXX_"; 1681 snprintf(prefix, sizeof(prefix), "wan%d_", unit); 1682 if (nvram_match(strcat_r(prefix, "primary", tmp), "1")) 1683 return unit; 1684 } 1685 1686 return 0; 1687} 1688 1689