function handle_stateful($inf, $devnam, $opt) { //$stsp = XNODE_getpathbytarget("/runtime", "inf", "uid", $inf, 0); $stsp = XNODE_getpathbytarget("/runtime", "inf", "uid", $inf, 1); //for wizard detection if ($stsp == "" || $devnam == "") { return; } /* Preparing ... */ //$conf = "/proc/sys/net/ipv6/conf/".$devnam; $conf = "/var/run/" . $devnam; $child = query($stsp . "/child/uid"); $childgz = query($stsp . "/childgz/uid"); /* Strip the tailing spaces. */ $DNS = strip($_GLOBALS["DNS"]); $NAMESERVERS = strip($_GLOBALS["NAMESERVERS"]); $DOMAIN = strip($_GLOBALS["DOMAIN"]); $NEW_ADDR = strip($_GLOBALS["NEW_ADDR"]); $NEW_PD_PREFIX = strip($_GLOBALS["NEW_PD_PREFIX"]); $NEW_PD_PLEN = strip($_GLOBALS["NEW_PD_PLEN"]); $NEW_AFTR_NAME = strip($_GLOBALS["NEW_AFTR_NAME"]); $NEW_PD_PLFT = strip($_GLOBALS["NEW_PD_PLTIME"]); $NEW_PD_VLFT = strip($_GLOBALS["NEW_PD_VLTIME"]); if ($DOMAIN != "") { XNODE_set_var($child . "_DOMAIN", $DOMAIN); XNODE_set_var($childgz . "_DOMAIN", $DOMAIN); } XNODE_set_var($child . "_PDPLFT", $NEW_PD_PLFT); XNODE_set_var($child . "_PDVLFT", $NEW_PD_VLFT); XNODE_set_var($childgz . "_PDPLFT", $NEW_PD_PLFT); XNODE_set_var($childgz . "_PDVLFT", $NEW_PD_VLFT); /* Combine the user config and DHCP server setting. */ $dns = $DNS; /* get primary dns and secondary dns */ $pdns = ''; $sdns = ''; $dns_cnt = scut_count($dns, ""); if ($dns_cnt != 0) { $pdns = scut($dns, 0, ""); $sdns = scut($dns, 1, ""); } $nameserver = ''; if ($NAMESERVERS != "") { $i = 0; $cnt = scut_count($NAMESERVERS, ""); while ($i < $cnt) { $val = scut($NAMESERVERS, $i, ""); if ($val == $pdns || $val == $sdns) { msg("Got the same dns server as dns manual setting"); } else { if ($nameserver == "") { $nameserver = $val; } else { $nameserver = $nameserver . " " . $val; } } $i++; } $NAMESERVERS = $nameserver; } $oflag = fread("e", $conf . ".ra_oflag"); //if($oflag=="1") // for M flag = 1, O flag = 0 case //{ if ($NAMESERVERS != "") { if ($dns == "") { $dns = $NAMESERVERS; } else { $dns = $dns . " " . $NAMESERVERS; } } //} /* Get RA info. */ if ($_GLOBALS["MODE"] != "PPPDHCP") { $prefix = fread("e", $conf . ".ra_prefix"); $pfxlen = fread("e", $conf . ".ra_prefix_len"); $router = fread("e", $conf . ".ra_saddr"); //if ($router=="" || $router=="NULL") {msg("no ra_saddr"); return;}/*TR-124 issue 2 */ //if ($prefix=="" || $prefix=="NULL") {msg("no ra_prefix"); return;} //if ($pfxlen==0) {msg("no ra_prefix_len"); return;} } /* Check renew, do nothing if we got the same prefix and prefix len as previous ones */ $ip = query($stsp . "/inet/ipv6/ipaddr"); $pfl = query($stsp . "/inet/ipv6/prefix"); //IOL if already do renew IA-PD if ($ip == "") { $ip = query($stsp . "/oldinet/ipv6/ipaddr"); $pfl = query($stsp . "/oldinet/ipv6/prefix"); } $childip = query($stsp . "/child/ipaddr"); $childpfl = query($stsp . "/child/prefix"); if ($opt == "IA-NA+IA-PD") { if ($NEW_ADDR != "" && $NEW_PD_PREFIX == "" && $NEW_PD_PLEN == "") { $renew = 1; $renewip = 1; msg("STATEFUL - opt: " . $opt . " RENEW IANA..."); } if ($NEW_ADDR == "" && $NEW_PD_PREFIX != "" && $NEW_PD_PLEN != "") { $renew = 1; $renewip = 0; msg("STATEFUL - opt: " . $opt . " RENEW IAPD..."); } } if ($opt == "IA-NA") { if ($NEW_ADDR != "" && $NEW_PD_PREFIX == "" && $NEW_PD_PLEN == "") { $renew = 1; msg("STATEFUL - opt: " . $opt . " RENEW IANA..."); } } if ($opt == "IA-PD") { if ($child != "") { $mac = PHYINF_getphymac($child); $hostid = ipv6eui64($mac); if ($NEW_PD_PLEN < 64) { $slalen = 64 - $NEW_PD_PLEN; $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 1, $slalen); $cpfxlen = 64; } else { $slalen = 1; $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 1, 1); $cpfxlen = $NEW_PD_PLEN + 1; } if ($childip == $ipaddr && $childpfl == $NEW_PD_PLEN + 1) { msg("STATEFUL - Renew but do nothing"); return; } msg("PREFIX=" . $NEW_PD_PREFIX . ", PLEN=" . $NEW_PD_PLEN . ", HOSTID=" . $hostid . ", SLA=1, SLALEN=" . $slalen); msg("IPADDR=" . $ipaddr); set($stsp . "/child/ipaddr", $ipaddr); set($stsp . "/child/prefix", $cpfxlen); set($stsp . "/child/pdnetwork", $NEW_PD_PREFIX); set($stsp . "/child/pdprefix", $NEW_PD_PLEN); set($stsp . "/child/pdplft", $NEW_PD_PLFT); set($stsp . "/child/pdvlft", $NEW_PD_VLFT); } if ($childgz != "") { $mac = PHYINF_getphymac($childgz); $hostid = ipv6eui64($mac); if ($NEW_PD_PLEN < 64) { $slalen = 64 - $NEW_PD_PLEN; $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 0, $slalen); $cpfxlen = 64; } else { $slalen = 1; $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 2, 1); $cpfxlen = $NEW_PD_PLEN + 1; } if ($childip == $ipaddr && $childpfl == $NEW_PD_PLEN + 1) { msg("STATEFUL - Renew but do nothing"); return; } msg("PREFIX=" . $NEW_PD_PREFIX . ", PLEN=" . $NEW_PD_PLEN . ", HOSTID=" . $hostid . ", SLA=1, SLALEN=" . $slalen); msg("IPADDR=" . $ipaddr); set($stsp . "/childgz/ipaddr", $ipaddr); set($stsp . "/childgz/prefix", $cpfxlen); set($stsp . "/childgz/pdnetwork", $NEW_PD_PREFIX); set($stsp . "/childgz/pdprefix", $NEW_PD_PLEN); set($stsp . "/childgz/pdplft", $NEW_PD_PLFT); set($stsp . "/childgz/pdvlft", $NEW_PD_VLFT); } $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, 1, 0, 0); $pfxlen = $NEW_PD_PLEN; if ($ip == $ipaddr && $pfl == $NEW_PD_PLEN) { msg("STATEFUL - opt: " . $opt . " Renew but do nothing"); set($stsp . "/inet/ipv6/ipaddr", $ip); set($stsp . "/inet/ipv6/prefix", $pfl); return; } } else { if ($child != "" && strstr($opt, "IA-PD") != "") { $mac = PHYINF_getphymac($child); $hostid = ipv6eui64($mac); if ($NEW_PD_PLEN <= 64) { msg("STATEFUL - Record " . $NEW_PD_PREFIX . "/" . $NEW_PD_PLEN . " to blackhole"); /* handle blackhole issue */ //cmd("ip -6 route add blackhole ".$NEW_PD_PREFIX."/".$NEW_PD_PLEN." dev lo"); //set($stsp."/blackhole/prefix", $NEW_PD_PREFIX); //set($stsp."/blackhole/plen", $NEW_PD_PLEN); $holecnt = query($stsp . "/blackhole/count"); if ($holecnt == "") { $holecnt = 1; set($stsp . "/blackhole/count", $holecnt); set($stsp . "/blackhole/entry:" . $holecnt . "/prefix", $NEW_PD_PREFIX); set($stsp . "/blackhole/entry:" . $holecnt . "/plen", $NEW_PD_PLEN); if ($NEW_PD_PLEN != 64) { cmd("ip -6 route add blackhole " . $NEW_PD_PREFIX . "/" . $NEW_PD_PLEN . " dev lo"); } } $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 1, 64 - $NEW_PD_PLEN); $cpfxlen = 64; } else { $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 0, 0); $cpfxlen = $NEW_PD_PLEN; } if ($renew == 1 && $childip == $ipaddr && $childpfl == $cpfxlen) { msg("STATEFUL - opt: " . $opt . " Renew but do nothing"); return; } if ($renew == 1 && $NEW_PD_PREFIX != "" && $NEW_PD_PLEN != "") { //IOL test, add blackhoel to ip6tables table FORWARD if ($NEW_PD_PLEN <= 64) { $OLD_PD_PREFIX = query($stsp . "/blackhole/entry:" . $holecnt . "/prefix"); $OLD_PD_PLEN = query($stsp . "/blackhole/entry:" . $holecnt . "/plen"); } msg("STATEFUL - Renew and record " . $OLD_PD_PREFIX . "/" . $OLD_PD_PLEN . " to oldblackhole"); set($stsp . "/oldblackhole/entry:1/prefix", $OLD_PD_PREFIX); set($stsp . "/oldblackhole/entry:1/plen", $OLD_PD_PLEN); if ($OLD_PD_PREFIX != 64) { cmd("ip -6 route del blackhole " . $OLD_PD_PREFIX . "/" . $OLD_PD_PLEN . " dev lo"); } cmd("ip -6 route add " . $OLD_PD_PREFIX . "/" . $OLD_PD_PLEN . " dev br0"); //renew and get the different prefix //$holecnt = $holecnt + 1; //set($stsp."/blackhole/entry:".$holecnt."/prefix", $NEW_PD_PREFIX); //set($stsp."/blackhole/entry:".$holecnt."/plen", $NEW_PD_PLEN); //set($stsp."/blackhole/count", $holecnt); //cmd("ip -6 route add blackhole ".$NEW_PD_PREFIX."/".$NEW_PD_PLEN." dev lo"); if ($NEW_PD_PLEN <= 64) { msg("STATEFUL - Add new prefix " . $NEW_PD_PREFIX . "/" . $NEW_PD_PLEN . " to blackhole"); $holecnt = $holecnt + 1; set($stsp . "/blackhole/entry:" . $holecnt . "/prefix", $NEW_PD_PREFIX); set($stsp . "/blackhole/entry:" . $holecnt . "/plen", $NEW_PD_PLEN); set($stsp . "/blackhole/count", $holecnt); if ($NEW_PD_PLEN != 64) { cmd("ip -6 route add blackhole " . $NEW_PD_PREFIX . "/" . $NEW_PD_PLEN . " dev lo"); } } } if ($ipaddr != "") { set($stsp . "/child/ipaddr", $ipaddr); set($stsp . "/child/prefix", $cpfxlen); set($stsp . "/child/pdnetwork", $NEW_PD_PREFIX); set($stsp . "/child/pdprefix", $NEW_PD_PLEN); set($stsp . "/child/pdplft", $NEW_PD_PLFT); set($stsp . "/child/pdvlft", $NEW_PD_VLFT); } } if ($childgz != "" && strstr($opt, "IA-PD") != "") { $mac = PHYINF_getphymac($childgz); $hostid = ipv6eui64($mac); if ($NEW_PD_PLEN < 64) { $ipaddr = ipv6ip($NEW_PD_PREFIX, $NEW_PD_PLEN, $hostid, 0, 64 - $NEW_PD_PLEN); $cpfxlen = 64; } else { //If pd length is bigger than 63, don't set to guest zone $ipaddr = ""; $cpfxlen = ""; } if ($ipaddr != "") { set($stsp . "/childgz/ipaddr", $ipaddr); set($stsp . "/childgz/prefix", $cpfxlen); set($stsp . "/childgz/pdnetwork", $NEW_PD_PREFIX); set($stsp . "/childgz/pdprefix", $NEW_PD_PLEN); set($stsp . "/childgz/pdplft", $NEW_PD_PLFT); set($stsp . "/childgz/pdvlft", $NEW_PD_VLFT); } } if (strstr($opt, "IA-NA") == "") { msg("no IA-NA"); return; } if ($renew == 1 && $renewip == 1 && $ip == $NEW_ADDR) { msg("STATEFUL - opt: " . $opt . " Renew but do nothing"); set($stsp . "/inet/ipv6/ipaddr", $ip); set($stsp . "/inet/ipv6/prefix", $pfl); return; } if ($NEW_ADDR == "" && $renewip == 1) { msg("no NEW_ADDR"); return; } $ipaddr = $NEW_ADDR; $pfxlen = 128; } set($stsp . "/inet/ipv6/dhcpopt", $opt); /* DS-Lite info */ $remote = strip($_GLOBALS["NEW_AFTR_NAME"]); if ($remote != "") { set($stsp . "/inet/ipv4/ipv4in6/remote", $remote); } /* domain info */ if ($DOMAIN != "") { set($stsp . "/inet/ipv6/domain", $DOMAIN); } //IOL test, backup if ($ipaddr != "") { set($stsp . "/oldinet/ipv6/ipaddr", $ipaddr); set($stsp . "/oldinet/ipv6/prefix", $pfxlen); } cmd("phpsh /etc/scripts/IPV6.INET.php ACTION=ATTACH" . " INF=" . $_GLOBALS["INF"] . " MODE=" . $_GLOBALS["MODE"] . " DEVNAM=" . $devnam . " IPADDR=" . $ipaddr . " PREFIX=" . $pfxlen . " GATEWAY=" . $router . ' "DNS=' . $dns . '"'); return; }
include "/htdocs/phplib/xnode.php"; $XMLBASE = "/runtime/device/storage"; $PHPFILE = "/etc/events/FORMAT.php"; $UID = toupper($dev); $prefix = substr($dev, 0, 3); $pid = substr($dev, 3, ""); if ($pid == "") { $pid = 0; $UID = $UID . $pid; } $diskp = XNODE_getpathbytarget($XMLBASE, "disk", "uid", toupper($prefix), 0); $path = XNODE_getpathbytarget($diskp, "entry", "uid", $UID, 0); if ($path != "") { if ($action == "try_unmount") { $mntstatus = fread("", "/proc/mounts"); if (scut_count($mntstatus, $dev) != "0") { if ($counter == "30") { echo "event UNMOUNT." . toupper($dev) . "\n"; } $counter -= 2; if ($counter > 0) { echo "xmldbc -t \"TRYUNMOUNT:2:phpsh /etc/events/FORMAT.php dev=" . $dev . " action=try_unmount counter=" . $counter . "\"\n"; } else { echo "phpsh " . $PHPFILE . " dev=" . $dev . " action=update state=FAILED\n"; } } else { echo "phpsh " . $PHPFILE . " dev=" . $dev . " action=format\n"; } setattr($path . "/space/used", "get", ""); setattr($path . "/space/available", "get", ""); set($path . "/state", "FORMATTING");
if ($cnt == "") { $cnt = 0; } $i = 0; while ($i < $cnt) { $i++; $value = query($inetp . "/ppp4/dns/entry:" . $i); if ($value != "") { add($stsp . "/inet/ppp4/dns", $value); } } } /* auto dns */ if ($cnt == 0 && isfile("/etc/ppp/resolv.conf." . $PARAM) == 1) { $dnstext = fread("r", "/etc/ppp/resolv.conf." . $PARAM); $cnt = scut_count($dnstext, ""); $i = 0; while ($i < $cnt) { $token = scut($dnstext, $i, ""); if ($token == "nameserver") { $i++; $token = scut($dnstext, $i, ""); add($stsp . "/inet/ppp4/dns", $token); } $i++; } } /* We use PING peer IP to trigger the dailup at 'ondemand' mode. * So we need to update the command to PING the new gateway. */ $dial = XNODE_get_var($PARAM . ".DIALUP"); if ($dial == "") {