Exemple #1
0
function unsolclic_airos($dev)
{
    $version = "1.1";
    $loc = node_load(array('nid' => $dev->nid));
    $zone = node_load(array('nid' => $loc->zone_id));
    $wan = guifi_unsolclic_if($dev->id, 'Wan');
    list($primary_dns, $secondary_dns) = explode(' ', guifi_get_dns($zone, 2));
    $dns[] .= $primary_dns;
    $dns[] .= $secondary_dns;
    foreach ($dev->radios[0]['interfaces'] as $interface_id => $interface) {
        foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
            if (isset($ipv4['links'])) {
                foreach ($ipv4['links'] as $key => $link) {
                    $gateway = $link['interface']['ipv4']['ipv4'];
                }
            }
        }
    }
    $apssid = guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']);
    if (empty($dev->radios[0][antenna_mode])) {
        $dev->radios[0][antenna_mode] = 'Main';
    }
    if ($dev->radios[0][antenna_mode] == 'Main') {
        if ($dev->variable['model_id'] == '34') {
            // NanoStation Loco2.
            $dev->radios[0][antenna_mode] = '1';
        } else {
            $dev->radios[0][antenna_mode] = '2';
        }
        // Main on NanoStation2, Nanostation5 and  Loco5.
    } else {
        if ($dev->variable['model_id'] == '34') {
            // NanoStation Loco2.
            $dev->radios[0][antenna_mode] = '2';
        } else {
            $dev->radios[0][antenna_mode] = '1';
        }
        // External on NanoStation2, Nanostation5 and  Loco5.
    }
    $radiorx = $dev->radios[0][antenna_mode];
    $radiotx = $dev->radios[0][antenna_mode];
    switch ($dev->variable['model_id']) {
        case "25":
            //NanoStation2
            $net_mode = 'b';
            $lnet_mode = 'B Only (2,4Ghz 11MB)';
            $rate_max = '11M';
            $txpower = '6';
            $ack = '45';
            $extant = 'disabled';
            $mcastrate = '11';
            break;
        case "26":
            //NanoStation5
            $net_mode = 'a';
            $lnet_mode = 'A (5Ghz)';
            $rate_max = '54M';
            $txpower = '6';
            $ack = '25';
            $extant = 'disabled';
            $mcastrate = '54';
            break;
        case "34":
            //NanoStation Loco2
            $net_mode = 'b';
            $lnet_mode = 'B Only (2,4Ghz 11MB)';
            $rate_max = '11M';
            $txpower = '6';
            $ack = '44';
            $extant = 'enabled';
            $mcastrate = '11';
            break;
        case "35":
            //NanoStation Loco5
            $net_mode = 'a';
            $lnet_mode = 'A (5Ghz)';
            $rate_max = '54M';
            $txpower = '6';
            $ack = '25';
            $extant = 'disabled';
            $mcastrate = '54';
    }
    ## Create Script file
    $File = 'files/nanostation/' . $dev->nick . '.cfg';
    $Handle = fopen($File, 'w');
    $Data = "aaa.1.status=disabled\naaa.status=disabled\nbridge.1.devname=br0\nbridge.1.fd=1\nbridge.1.port.1.devname=eth0\nbridge.1.port.2.devname=ath0\nbridge.status=disabled\ndhcpc.1.devname=br0\ndhcpc.1.status=disabled\ndhcpc.status=disabled\ndhcpd.1.devname=eth0\ndhcpd.1.end=192.168.1.254\ndhcpd.1.lease_time=3600\ndhcpd.1.netmask=255.255.255.0\ndhcpd.1.start=192.168.1.33\ndhcpd.1.status=enabled\ndhcpd.status=enabled\nebtables.1.cmd=-t nat -A PREROUTING --in-interface ath0 -j arpnat --arpnat-target ACCEPT\nebtables.1.status=enabled\nebtables.2.cmd=-t nat -A POSTROUTING --out-interface ath0 -j arpnat --arpnat-target ACCEPT\nebtables.2.status=enabled\nebtables.3.cmd=-t broute -A BROUTING --protocol 0x888e --in-interface ath0 -j DROP\nebtables.3.status=enabled\nebtables.status=disabled\nhttpd.https.status=enabled\nhttpd.port.http=80\nhttpd.status=enabled\niptables.1.status=enabled\niptables.1.cmd=-t nat -I POSTROUTING -o ath0 -j MASQUERADE\niptables.2.status=disabled\niptables.status=enabled\nnetconf.1.devname=eth0\nnetconf.1.ip=192.168.1.1\nnetconf.1.netmask=255.255.255.0\nnetconf.1.promisc=enabled\nnetconf.1.status=enabled\nnetconf.1.up=enabled\nnetconf.2.allmulti=enabled\nnetconf.2.devname=ath0\nnetconf.2.status=enabled\nnetconf.2.up=enabled\nnetconf.3.devname=br0\nnetconf.3.ip=192.168.1.20\nnetconf.3.netmask=255.255.255.0\nnetconf.3.status=disabled\nnetconf.3.up=enabled\nnetconf.status=enabled\nnetmode=router\nppp.1.password=\nppp.1.status=disabled\nppp.status=disabled\nradio.1.ack.auto=enabled\nradio.1.ackdistance=450\nradio.1.ani.status=enabled\nradio.1.chanshift=0\nradio.1.clksel=0\nradio.1.countrycode=724\nradio.1.devname=ath0\nradio.1.frag=off\nradio.1.mode=managed\nradio.1.rate.auto=enabled\nradio.1.rts=off\nradio.1.tx_antenna_diversity=disabled\nradio.1.rx_antenna_diversity=disabled\nradio.1.status=enabled\nradio.1.thresh62a=28\nradio.1.thresh62b=28\nradio.1.thresh62g=28\nradio.ratemodule=ath_rate_minstrel\nradio.countrycode=724\nradio.status=enabled\nresolv.host.1.status=enabled\nresolv.nameserver.1.status=enabled\nresolv.nameserver.2.status=enabled\nresolv.status=enabled\nroute.1.devname=ath0\nroute.1.ip=0.0.0.0\nroute.1.netmask=0\nroute.1.status=enabled\nroute.status=enabled\nsnmp.community=public\nsnmp.contact=guifi@guifi.net\nsnmp.status=enabled\ntelnetd.status=enabled\nsshd.status=enabled\ntshaper.status=disabled\nusers.1.name=root\nusers.1.password=JjYNUu92yMZd.\nusers.1.status=enabled\nusers.status=enabled\nwireless.1.ap=\nwireless.1.authmode=1\nwireless.1.compression=0\nwireless.1.devname=ath0\nwireless.1.fastframes=0\nwireless.1.frameburst=0\nwireless.1.hide_ssid=disabled\nwireless.1.l2_isolation=enabled\nwireless.1.macclone=disabled\nwireless.1.rssi_led1=1\nwireless.1.rssi_led2=15\nwireless.1.rssi_led3=22\nwireless.1.rssi_led4=30\nwireless.1.security=none\nwireless.1.status=enabled\nwireless.1.wds=disabled\nwireless.1.wmm=disabled\nwireless.1.wmmlevel=-1\nwireless.status=enabled\nwpasupplicant.device.1.status=disabled\nwpasupplicant.status=disabled\nwireless.1.ssid=guifi.net-{$apssid}\nnetconf.2.ip={$wan->ipv4}\nnetconf.2.netmask={$wan->netmask}\nroute.1.gateway={$gateway}\nresolv.nameserver.1.ip={$primary_dns}\nresolv.nameserver.2.ip={$secondary_dns}\nresolv.host.1.name={$dev->nick}\nsnmp.location={$loc->nick}\nradio.1.ieee_mode={$net_mode}\nradio.1.rate.max={$rate_max}\nradio.1.txpower={$txpower}\nradio.1.acktimeout={$ack}\nradio.1.rx_antenna={$radiorx}\nradio.1.tx_antenna={$radiotx}\nradio.1.ext_antenna={$extant}\nradio.1.mcastrate={$mcastrate}\n";
    fwrite($Handle, $Data);
    print '<br/><a href="' . base_path() . 'files/nanostation/' . $dev->nick . '.cfg"> Click here to download configuration file for: ' . $dev->nick . ' </a><br />';
    print 'Put the mouse cursor over the link. Right click the link and select "Save Link/Target As..." to save to your Desktop.<br /><br />';
    fclose($Handle);
    if ($radiorx == '2') {
        if ($dev->variable['model_id'] == '34') {
            // NanoStation Loco2.
            $ant = 'Horizontal';
        }
        if ($dev->variable['model_id'] == '35') {
            // NanoStation Loco5.
            $ant = 'Vertical';
        }
        if ($dev->variable['model_id'] == '25' || $dev->variable['model_id'] == '26') {
            // NanoStation2 and NanoStation5.
            $ant = 'Main/Internal - Vertical';
        }
    } else {
        if ($dev->variable['model_id'] == '34') {
            // NanoStation Loco2.
            $ant = 'Vertical';
        }
        if ($dev->variable['model_id'] == '35') {
            // NanoStation Loco5.
            $ant = 'Horizontal';
        }
        if ($dev->variable['model_id'] == '25' || $dev->variable['model_id'] == '26') {
            // NanoStation2 and NanoStation5.
            $ant = 'Aux/External - Vertical';
        }
    }
    _outln_comment('Configuration for AirOs> Unsolclic version:' . $version . ' !! WARNING: Beta version !!');
    _outln_comment(' Device: ' . $dev->nick . '');
    _outln_comment();
    _outln_comment(' Methods to upload/execute the file:');
    _outln_comment(' 1.- As a file. Upload this through web management:');
    _outln_comment('     a.System->Configuration Management->Locate file');
    _outln_comment('     b.Upload');
    _outln_comment(' 2.- Telnet: Open a terminal session, create new /tmp/system.cfg file and cut&paste');
    _outln_comment('     the contents of the file. Save it an execute the command:');
    _outln_comment();
    _outln_comment('     /usr/etc/rc.d/rc.softrestart save');
    _outln_comment();
    _outln_comment(' Notes:');
    _outln_comment(' -Web access method is recommended');
    _outln_comment('   (the script reconfigures some IP addresses, so communication can be lost.');
    _outln_comment('   192.168.1.1 will be the new one)');
    _outln_comment(' -Changes are done in user passwords on the device, default user and password are');
    _outln_comment('  changed to root/guifi.');
    _outln_comment(' -The ACK is set to 45 for 802.11b mode, and to 25 for 802.11a (600 meters aprox,)');
    _outln_comment();
    _outln('## Link to AP info');
    _outln('
    Ap SSID = guifi.net-' . $apssid . '<br />
    WAN Ip address = ' . $wan->ipv4 . '<br />
    WAN Netmask = ' . $wan->netmask . '<br />
    WAN Gateway = ' . $gateway . '<br />
    Primary DNS Server = ' . $primary_dns . '<br />
    Secondary DNS Server = ' . $secondary_dns . '<br />
    Device HostName = ' . $dev->nick . '<br />
    IEEE 802.11 Mode: = ' . $lnet_mode . '<br />
    Antenna Selection or/and Polarization: = ' . $ant . '<br />
        ');
}
function guifi_kamikaze_files($dev, $zone)
{
    //SOME VARIABLES
    $dns = guifi_get_dns($zone, 2);
    $lan = guifi_unsolclic_if($dev->id, 'wLan/Lan');
    $lan_network = _ipcalc($lan->ipv4, $lan->netmask);
    switch ($dev->variable['model_id']) {
        case "38":
            // RouterStation
            $wireless_model = 'atheros';
            $lan_iface = 'eth0';
            $lan2_iface = 'eth1';
            $packages = 'ar71xx/packages';
            break;
        default:
            _outln_comment('model id not supported');
            exit;
    }
    // SECTION FILES
    _outln_comment();
    _outln_comment();
    _outln_comment(t('Wireless Settings'));
    print '<pre>';
    function wds_add($dev, $radio, $radio_id)
    {
        $wds_links = array();
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wds/p2p') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['link_type'] == 'wds') {
                            $wds_links[] = $link;
                        }
                    }
                }
                $ifcount = 0;
                foreach ($wds_links as $key => $wds) {
                    if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                        $status = 'active';
                        print 'uci set wireless.@wifi-iface[' . $radio_id . '].bssid' . $ifcount . '=' . $wds['interface']['mac'] . '<br />';
                        $ifcount++;
                    } else {
                        $status = 'disabled';
                        print '# uci set wireless.@wifi-iface[' . $radio_id . '].bssidX=' . $wds['interface']['mac'] . ' # ' . t($wds['flag']) . '<br />';
                    }
                }
            }
        }
    }
    function wds_network($dev, $radio)
    {
        $ifcount = '0';
        foreach ($radio[interfaces] as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    if ($link['link_type'] == 'wds') {
                        $wds_links = array();
                        $wds_links[] = $link;
                        foreach ($wds_links as $key => $wds) {
                            $hostname = guifi_get_hostname($wds['device_id']);
                            if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                                $status = 'active';
                                print '
## wds_' . $hostname . '
uci delete network.wds_' . $hostname . '
uci set network.wds_' . $hostname . '=interface
uci set network.wds_' . $hostname . '.ifname=ath' . $radio[radiodev_counter] . '.wds' . $ifcount . '
uci set network.wds_' . $hostname . '.proto=static
';
                                $ifcount++;
                            } else {
                                $status = 'disabled';
                                print '
##### ' . t($wds['flag']) . ' ####
## wds_' . $hostname . '
# uci set network.wds_' . $hostname . '.ifname=ath' . $radio[radiodev_counter] . '.wdsX
# uci set network.wds_' . $hostname . '.proto=none
';
                            }
                        }
                        if ($status == 'active') {
                            print 'uci set network.wds_' . $hostname . '.ipaddr=' . $ipv4[ipv4] . '
uci set network.wds_' . $hostname . '.netmask=' . $ipv4['netmask'] . '
';
                        } else {
                            print '# uci set network.wds_' . $hostname . '.ipaddr=' . $ipv4[ipv4] . '
# uci set network.wds_' . $hostname . '.netmask=' . $ipv4['netmask'] . '
';
                        }
                    }
                }
            }
        }
    }
    if (isset($dev->radios)) {
        foreach ($dev->radios as $radio_id => $radio) {
            $mode = 'apwds';
            $ssid = $radio[ssid];
            $channel = atheros_channel($radio);
            if ($channel <= 14) {
                $band = '11b';
            } else {
                $band = '11a';
            }
            if (empty($radio[antenna_mode])) {
                $radio[antenna_mode] = '1';
            } else {
                if ($radio[antenna_mode] != 'Main') {
                    $radio[antenna_mode] = '2';
                } else {
                    $radio[antenna_mode] = '1';
                }
            }
            if ($radio_id == '0') {
                $wireless_iface = 'wifi0';
                $network = 'wlanLan';
            } else {
                $wireless_iface = 'wifi' . $radio_id;
                $network = 'wlan' . ($radio_id + 1);
            }
            $wireless_model = 'atheros';
            $txant = $radio[antenna_mode];
            $rxant = $radio[antenna_mode];
            print '
## Radio: ' . $radio[ssid] . '
uci delete wireless.' . $wireless_iface . '
uci set wireless.' . $wireless_iface . '=wifi-device
uci set wireless.' . $wireless_iface . '.type=' . $wireless_model . '
uci set wireless.' . $wireless_iface . '.channel=' . $channel . '
uci set wireless.' . $wireless_iface . '.disabled=0
uci set wireless.' . $wireless_iface . '.hwmode=' . $band . '
uci set wireless.' . $wireless_iface . '.diversity=0
uci set wireless.' . $wireless_iface . '.txantenna=' . $txant . '
uci set wireless.' . $wireless_iface . '.rxantenna=' . $rxant . '
uci set wireless.' . $wireless_iface . '.txpower=16
uci set wireless.' . $wireless_iface . '.regdomain=31
uci set wireless.' . $wireless_iface . '.country=724
uci set wireless.' . $wireless_iface . '.distance=2000
uci delete wireless.@wifi-iface[' . $radio_id . ']
uci add wireless wifi-iface
uci set wireless.@wifi-iface[' . $radio_id . ']=wifi-iface
uci set wireless.@wifi-iface[' . $radio_id . '].device=' . $wireless_iface . '
uci set wireless.@wifi-iface[' . $radio_id . '].network=' . $network . '
uci set wireless.@wifi-iface[' . $radio_id . '].mode=' . $mode . '
uci set wireless.@wifi-iface[' . $radio_id . '].ssid=guifi.net-' . $radio[ssid] . '
uci set wireless.@wifi-iface[' . $radio_id . '].encryption=none
';
            wds_add($dev, $radio, $radio_id);
        }
    }
    print 'sleep 1</pre>';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('Network Settings'));
    print '<pre>';
    function cable_network($dev)
    {
        foreach ($dev->interfaces as $interface_id => $interface) {
            switch ($interface[interface_type]) {
                case 'vlan1':
                    $iname = 'eth0:1';
                    break;
                case 'vlan2':
                    $iname = 'eth1';
                    break;
                case 'vlan3':
                    $iname = 'eth1:1';
                    break;
                default:
                    $iname = $interface[interface_type];
                    break;
            }
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    $network = guifi_get_hostname($link[device_id]);
                    if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                        $status = 'active';
                        if ($interface[interface_type] != 'wLan/Lan') {
                            print '
## cable_' . $network . '
uci delete network.' . $network . '
uci set network.' . $network . '=interface
uci set network.' . $network . '.ifname=' . $iname . '
uci set network.' . $network . '.proto=static
uci set network.' . $network . '.ipaddr=' . $ipv4[ipv4] . '
uci set network.' . $network . '.netmask=' . $ipv4[netmask] . '
';
                        }
                    } else {
                        $status = 'disabled';
                        print '
##### ' . t($link['flag']) . ' ####
## cable_' . $network . '
# uci set network.' . $network . '=interface
# uci set network.' . $network . '.ifname=' . $iname . '
# uci set network.' . $network . '.proto=static
# uci set network.' . $network . '.ipaddr=' . $ipv4[ipv4] . '
# uci set network.' . $network . '.netmask=' . $ipv4[netmask] . '
';
                    }
                }
            }
        }
    }
    print '
uci set network.loopback=interface
uci set network.loopback.ifname=lo
uci set network.loopback.proto=static
uci set network.loopback.ipaddr=127.0.0.1
uci set network.loopback.netmask=255.0.0.0

uci delete network.lan
uci delete network.wan
';
    if (isset($dev->radios)) {
        foreach ($dev->radios as $radio_id => $radio) {
            if (isset($radio[interfaces])) {
                foreach ($radio[interfaces] as $interface_id => $interface) {
                    if ($interface[interface_type] != 'wds/p2p') {
                        if (isset($interface[ipv4])) {
                            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                                if ($interface[interface_type] == 'wLan/Lan') {
                                    $iface = '"ath0 eth0"';
                                    $network = 'wlanLan';
                                } else {
                                    $iface = 'wifi' . $radio_id;
                                    $network = 'wlan' . ($radio_id + 1);
                                }
                                $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                                // FILE NETWORK
                                print '
uci delete network.' . $network . '
uci set network.' . $network . '=interface
';
                                if ($interface[interface_type] == 'wLan/Lan') {
                                    print 'uci set network.' . $network . '.type=bridge
';
                                }
                                print 'uci set network.' . $network . '.ifname=' . $iface . '
uci set network.' . $network . '.proto=static
uci set network.' . $network . '.ipaddr=' . $ipv4[ipv4] . '
uci set network.' . $network . '.netmask=' . $ipv4[netmask] . '
uci set network.' . $network . '.dns="' . $dns . '"
';
                            }
                        }
                    }
                }
            }
            wds_network($dev, $radio);
        }
    }
    cable_network($dev);
    print 'sleep 1</pre>';
    //FILE FIREWALL
    _outln_comment();
    _outln_comment();
    _outln_comment(t('Firewall Settings'));
    print '<pre>';
    print 'uci set firewall.@defaults[0]=defaults
uci set firewall.@defaults[0].syn_flood=1
uci set firewall.@defaults[0].input=ACCEPT
uci set firewall.@defaults[0].output=ACCEPT
uci set firewall.@defaults[0].forward=ACCEPT
';
    print 'COUNTER=0
while [  $COUNTER -lt 64 ]; do
  uci delete firewall.@zone[0] > /dev/null 2>&1
  let COUNTER=COUNTER+1 
done
uci delete firewall.@forwarding[0]
';
    $icount = '0';
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    if ($interface[interface_type] == 'wLan/Lan') {
                        $network = 'wlanLan';
                    } else {
                        $network = 'wlan' . ($radio_id + 1);
                    }
                    print 'uci add firewall zone
uci set firewall.@zone[' . $icount . ']=zone
uci set firewall.@zone[' . $icount . '].name=' . $network . '
uci set firewall.@zone[' . $icount . '].input=ACCEPT
uci set firewall.@zone[' . $icount . '].output=ACCEPT
uci set firewall.@zone[' . $icount . '].forward=ACCEPT
';
                    $icount++;
                }
            }
        }
    }
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wds/p2p') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['link_type'] == 'wds') {
                            $wds_links = array();
                            $wds_links[] = $link;
                            foreach ($wds_links as $key => $wds) {
                                $hostname = guifi_get_hostname($wds['device_id']);
                                if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                                    print 'uci add firewall zone
uci set firewall.@zone[' . $icount . ']=zone
uci set firewall.@zone[' . $icount . '].name=wds_' . $hostname . '
uci set firewall.@zone[' . $icount . '].input=ACCEPT
uci set firewall.@zone[' . $icount . '].output=ACCEPT
uci set firewall.@zone[' . $icount . '].forward=ACCEPT
';
                                    $icount++;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    foreach ($dev->interfaces as $interface_id => $interface) {
        foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
            foreach ($ipv4[links] as $link_id => $link) {
                $hostname = guifi_get_hostname($link['device_id']);
                if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                    print 'uci add firewall zone
uci set firewall.@zone[' . $icount . ']=zone
uci set firewall.@zone[' . $icount . '].name=' . $hostname . '
uci set firewall.@zone[' . $icount . '].input=ACCEPT
uci set firewall.@zone[' . $icount . '].output=ACCEPT
uci set firewall.@zone[' . $icount . '].forward=ACCEPT
';
                    $icount++;
                }
            }
        }
    }
    print 'sleep 1</pre>';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('DHCP Static Leases'));
    print '<pre>';
    $dhcp_statics = array();
    $max = explode(".", $dev->ipv4);
    function merge_static($link, &$dhcp_statics, &$max, &$curr)
    {
        if (empty($link['interface'][mac])) {
            $link['interface'][mac] = 'FF:FF:FF:FF:FF:FF';
        }
        $dhcp_statics[] = array($link['interface'][ipv4][ipv4], $link['interface'][mac], guifi_get_hostname($link['interface'][device_id]));
        $curr = explode(".", $link['interface'][ipv4][ipv4]);
        if ($curr[3] > $max[3]) {
            $max[3] = $curr[3];
        }
    }
    $main_ip = guifi_main_ip($dev->id);
    $item = _ipcalc_by_netbits($main_ip[ipv4], $main_ip[maskbits]);
    $max = explode(".", $main_ip[ipv4]);
    // cable links
    foreach ($dev->interfaces as $interface) {
        foreach ($interface[ipv4] as $ipv4) {
            foreach ($ipv4[links] as $link) {
                if ($link['interface'][ipv4][ipv4] != '') {
                    $item2 = _ipcalc($link['interface'][ipv4][ipv4], $link['interface'][ipv4][netmask]);
                    if ($item[netid] == $item2[netid]) {
                        merge_static($link, $dhcp_statics, $max, $cur);
                    }
                }
            }
        }
    }
    // ap/client links
    foreach ($dev->radios as $radio) {
        foreach ($radio[interfaces] as $interface) {
            foreach ($interface[ipv4] as $ipv4) {
                foreach ($ipv4[links] as $link) {
                    if ($link['link_type'] == 'ap/client' and !empty($link['interface'][ipv4][ipv4])) {
                        merge_static($link, $dhcp_statics, $max, $cur);
                    }
                }
            }
        }
    }
    $statics = count($dhcp_statics) - 1;
    $totalstatics = count($dhcp_statics);
    $first = explode(".", $item[netid]);
    $last = explode(".", $item[broadcast]);
    $limit = $last[3] - 1 - ($first[3] + 3) - $totalstatics;
    $counter = '0';
    print 'COUNTER=0
while [  $COUNTER -lt 64 ]; do
  uci delete luci_ethers.@static_lease[0] > /dev/null 2>&1
  let COUNTER=COUNTER+1 
done
';
    foreach ($dhcp_statics as $static) {
        print '
## Device: ' . $static[2] . '
uci add luci_ethers static_lease
uci set luci_ethers.@static_lease[' . $counter . ']=static_lease
uci set luci_ethers.@static_lease[' . $counter . '].macaddr=' . $static[1] . '
uci set luci_ethers.@static_lease[' . $counter . '].ipaddr=' . $static[0] . '
';
        $counter++;
    }
    print 'sleep 1</pre>';
    // FILE DHCP
    _outln_comment();
    _outln_comment();
    _outln_comment(t('DHCP Settings'));
    print '<pre>';
    print 'uci set dhcp.@dnsmasq[0]=dnsmasq                                                                                                               
uci set dhcp.@dnsmasq[0].domainneeded=1
uci set dhcp.@dnsmasq[0].boguspriv=1
uci set dhcp.@dnsmasq[0].filterwin2k=0
uci set dhcp.@dnsmasq[0].localise_queries=1
uci set dhcp.@dnsmasq[0].local=/lan/
uci set dhcp.@dnsmasq[0].domain=lan
uci set dhcp.@dnsmasq[0].expandhosts=1
uci set dhcp.@dnsmasq[0].nonegcache=0
uci set dhcp.@dnsmasq[0].authoritative=1
uci set dhcp.@dnsmasq[0].readethers=1
uci set dhcp.@dnsmasq[0].leasefile=/tmp/dhcp.leases
uci set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto
';
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    if ($interface[interface_type] == 'wLan/Lan') {
                        $iface = 'wlanLan';
                        $network = 'wlanLan';
                    } else {
                        $network = 'wlan' . ($radio_id + 1);
                    }
                    $max = explode(".", $ipv4[ipv4]);
                    foreach ($ipv4[links] as $link) {
                        if ($link['link_type'] == 'ap/client' and !empty($link['interface'][ipv4][ipv4])) {
                            $totalstaticss = count($ipv4[links]);
                        }
                    }
                    $first = explode(".", $item[netid]);
                    $last = explode(".", $item[broadcast]);
                    $limit = $last[3] - 1 - ($first[3] + 3) - $totalstaticss;
                    $totalstaticss = '0';
                    print '
uci delete dhcp.lan
uci delete dhcp.wan
uci set dhcp.' . $network . '=dhcp
uci set dhcp.' . $network . '.interface=' . $network . '
uci set dhcp.' . $network . '.leasetime=12h
uci set dhcp.' . $network . '.start=' . ($max[3] + 2) . '
uci set dhcp.' . $network . '.limit=' . $limit . '
uci set dhcp.' . $network . '.netmask=' . $ipv4[netmask] . '
';
                }
            }
        }
    }
    print 'sleep 1</pre>';
    $wds_links = array();
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wds/p2p') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['link_type'] == 'wds') {
                            $wds_links[] = $link;
                        }
                    }
                }
            }
        }
    }
    foreach ($wds_links as $key => $wds) {
        if ($wds['routing'] == 'BGP') {
            $wds_bgpd = '1';
        }
        if ($wds['routing'] == 'OSPF') {
            $wds_ospfd = '1';
        }
    }
    $cable_links = array();
    foreach ($dev->interfaces as $interface_id => $interface) {
        foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
            foreach ($ipv4[links] as $link_id => $link) {
                if ($link['link_type'] == 'cable') {
                    $cable_links[] = $link;
                }
            }
        }
    }
    foreach ($cable_links as $key => $cable) {
        if ($cable['routing'] == 'BGP') {
            $cable_bgpd = '1';
        }
        if ($cable['routing'] == 'OSPF') {
            $cable_ospfd = '1';
        }
    }
    // QUAGGA CONFIG FILES
    $file_zebra = '';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/quagga/zebra.conf'));
    print '<pre>';
    print 'true > /etc/quagga/zebra.conf';
    print '</pre>';
    // FILE OSPFD
    if ($wds_ospfd == '1' || $cable_ospfd == '1') {
        _outln_comment();
        _outln_comment();
        _outln_comment(t('File /etc/quagga/ospfd.conf'));
        print '<pre>';
        print 'cat > /etc/quagga/ospfd.conf << EOF
!
interface br-lan
!
router ospf
 ospf router-id ' . $lan->ipv4 . '
 redistribute bgp
';
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                    foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                        $network = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    }
                    if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                        print ' network ' . $network[netid] . '/' . $network[maskbits] . ' area 0<br />';
                    }
                }
            }
        }
        foreach ($wds_links as $key => $wds) {
            $iplocal[] = $wds['interface']['ipv4'];
            if ($wds['routing'] == 'OSPF') {
                $wds_network = _ipcalc($iplocal[$key][ipv4], $iplocal[$key][netmask]);
                if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                    print ' network ' . $wds_network[netid] . '/' . $wds_network[maskbits] . ' area 0<br />';
                }
            }
        }
        foreach ($dev->interfaces as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    if ($link['routing'] == 'OSPF') {
                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                            print ' network ' . $item[netid] . '/' . $item[maskbits] . ' area 0<br />';
                        }
                    }
                }
            }
        }
        print 'default-information originate
!
EOF

';
        print 'sleep 1</pre>';
    }
    // FILE BGPD
    if ($wds_bgpd == '1' || $cable_bgpd == '1') {
        _outln_comment();
        _outln_comment();
        _outln_comment(t('File /etc/quagga/bgpd.conf'));
        print '<pre>';
        print 'cat > /etc/quagga/bgpd.conf << EOF
!
interface br-lan
!
router bgp ' . $dev->id . '
bgp router-id ' . $lan->ipv4 . '
';
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                    foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                        $network = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    }
                    print ' network ' . $network[netid] . '/' . $network[maskbits] . '<br />';
                }
            }
        }
        print 'redistribute ospf
';
        foreach ($wds_links as $key => $wds) {
            $iplocal[] = $wds['interface']['ipv4'];
            if ($wds['routing'] == 'BGP') {
                $wds_network = _ipcalc($iplocal[$key][ipv4], $iplocal[$key][netmask]);
                if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                    print ' network ' . $wds_network[netid] . '/' . $wds_network[maskbits] . '<br />';
                }
            }
        }
        foreach ($dev->interfaces as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    if ($link['routing'] == 'BGP') {
                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                            print ' network ' . $item[netid] . '/' . $item[maskbits] . '<br />';
                        }
                    }
                }
            }
        }
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['routing'] == 'BGP') {
                            if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                                print ' neighbor ' . $link['interface']['ipv4']['ipv4'] . ' remote-as ' . $link['device_id'] . '
';
                            }
                        }
                    }
                }
            }
        }
        foreach ($dev->interfaces as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    if ($link['routing'] == 'BGP') {
                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                            print ' neighbor ' . $link['interface']['ipv4']['ipv4'] . ' remote-as ' . $link['device_id'] . '
';
                        }
                    }
                }
            }
        }
        print 'EOF

';
        print 'sleep 1</pre>';
    }
    //FILE OPKG
    $opkg_conf = '
src/gz snapshots http://downloads.openwrt.org/snapshots/' . $packages . '
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /jffs
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/opkg.conf'));
    openwrt_out_file($opkg_conf, '/etc/opkg.conf');
}
function unsolclic_guifistationos($dev)
{
    $version = "1.0";
    $loc = node_load(array('nid' => $dev->nid));
    $zone = node_load(array('nid' => $loc->zone_id));
    $wan = guifi_unsolclic_if($dev->id, 'Wan');
    list($primary_dns, $secondary_dns) = explode(' ', guifi_get_dns($zone, 2));
    $dns[] .= $primary_dns;
    $dns[] .= $secondary_dns;
    list($ntp1, $ntp2) = explode(' ', guifi_get_ntp($zone, 2));
    $ntp[] .= $ntp1;
    $ntp[] .= $ntp2;
    foreach ($dev->radios[0]['interfaces'] as $interface_id => $interface) {
        foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
            if (isset($ipv4['links'])) {
                foreach ($ipv4['links'] as $key => $link) {
                    $gateway = $link['interface']['ipv4']['ipv4'];
                }
            }
        }
    }
    $apssid = guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']);
    if (empty($dev->radios[0][antenna_mode])) {
        $dev->radios[0][antenna_mode] = 'Main';
    }
    if ($dev->radios[0][antenna_mode] == 'Main') {
        $dev->radios[0][antenna_mode] = '1';
    } else {
        $dev->radios[0][antenna_mode] = '2';
    }
    // External on GuifiStation2 and GuifiStation5.
    $radiorx = $dev->radios[0][antenna_mode];
    $radiotx = $dev->radios[0][antenna_mode];
    switch ($dev->variable['model_id']) {
        case "49":
            // GuifiStation2
            $skin = 'skin.active=guifi-station-2';
            $net_mode = 'B';
            $lnet_mode = 'B Only (2,4Ghz 11MB)';
            $rate_max = '11M';
            $txpower = '10';
            $ack = '45';
            $extant = 'disabled';
            $mcastrate = '11';
            $iface = 'eth0';
            $wiface = 'ath0';
            $lanip = '192.168.2.66';
            $lanmask = '255.255.255.0';
            $wanip = $wan->ipv4;
            $wanmask = $wan->netmask;
            $iiface = '1';
            $specs = 'device.limitband.1.bands.1.band=B
device.limitband.1.bands.2.band=G
device.limitband.1.bands.3.band=PUREG';
            break;
        case "50":
            // GuifiStation5
            $skin = 'skin.active=guifi-station-5';
            $net_mode = 'A';
            $lnet_mode = 'A (5Ghz)';
            $rate_max = '54M';
            $txpower = '10';
            $ack = '45';
            $extant = 'disabled';
            $mcastrate = '54';
            $iface = 'ath0';
            $wiface = 'eth0';
            $lanip = $wan->ipv4;
            $lanmask = $wan->netmask;
            $wanip = '192.168.2.66';
            $wanmask = '255.255.255.0';
            $iiface = '2';
            $specs = 'device.limitband.1.bands.1.band=A
device.limitband.1.bands.1.status=enabled
device.limitband.1.bands.2.band=AST';
            break;
    }
    ## Create Script file
    $File = 'files/guifistation/' . $dev->nick . '.cfg';
    $Handle = fopen($File, 'w');
    $Data = "netconf.status=enabled\nnetconf.1.status=enabled\nnetconf.2.status=enabled\nradio.countrycode=es\nradio.status=enabled\nradio.1.channel=0\nradio.1.devname=ath0\nradio.1.frag=off\nradio.1.mode=managed\nradio.1.parent=wifi0\nradio.1.rate.auto=enabled\nradio.1.rts=off\nradio.1.rx_antenna_diversity=disabled\nradio.1.status=enabled\nradio.1.turbo=disabled\nradio.1.tx_antenna_diversity=disabled\nwireless.status=enabled\nwireless.1.devname=ath0\nwireless.1.fastframes=disabled\nwireless.1.frameburst=disabled\nwireless.1.l2_isolation=disabled\nwireless.1.max_clients=64\nwireless.1.ssid_broadcast=enabled\nwireless.1.status=enabled\nwireless.1.wmm=disabled\nroute.status=enabled\nroute.1.devname=ath0\nroute.1.ip=0.0.0.0\nroute.1.netmask=0\nroute.1.status=enabled\nfirewall.status=enabled\nfirewall.rule.1.chain=POSTROUTING\nfirewall.rule.1.out=ath0\nfirewall.rule.1.status=enabled\nfirewall.rule.1.table=nat\nfirewall.rule.1.target=MASQUERADE\ndhcpd.status=enabled\ndhcpd.1.devname=eth0\ndhcpd.1.dns.1.server=192.168.2.66\ndhcpd.1.end=192.168.2.254\ndhcpd.1.gateway=192.168.2.66\ndhcpd.1.lease_time=600\ndhcpd.1.netmask=255.255.255.0\ndhcpd.1.start=192.168.2.100\ndhcpd.1.status=enabled\nsyslog.file=/var/log/messages\nsyslog.file.msg.level=info\nsyslog.file.umask=077\nsyslog.status=enabled\nsnmpd.contact=guifi@guifi.net\nsnmpd.rocommunity=public\nsnmpd.status=enabled\nresolv.status=enabled\ndate.status=enabled\ndate.timezone=GMT-1\nntpd.status=enabled\nntpd.1.status=enabled\nusers.status=enabled\nusers.1.name=admin\nusers.1.password=84OZbhpCnpRZI\nusers.1.status=enabled\ndevice.mode=router\ndevice.status=enabled\ndiscoveryd.status=enabled\nhttpd.backlog=100\nhttpd.external.status=disabled\nhttpd.max.connections=50\nhttpd.max.request=51200\nhttpd.port.admin=444\nhttpd.port.http=80\nhttpd.port.https=443\nhttpd.status=enabled\nhttpd.verbose=disabled\nsshd.port=22\nsshd.status=enabled\ndnsmasq.status=enabled\ndnsmasq.1.status=enabled\ndnsmasq.1.devname=eth0\ndevice.limitband.status=enabled\ndevice.limitband.1.status=enabled\ndevice.limitband.1.devname=ath0\nwireless.1.ssid=guifi.net-{$apssid}\nnetconf.1.devname={$iface}\nnetconf.1.ip={$lanip}\nnetconf.1.netmask={$lanmask}\nnetconf.2.devname={$wiface}\nnetconf.2.ip={$wanip}\nnetconf.2.netmask={$wanmask}\nroute.1.gateway={$gateway}\nresolv.nameserver.1.ip={$primary_dns}\nresolv.nameserver.2.ip={$secondary_dns}\nsnmpd.name={$dev->nick}\nsnmpd.location={$loc->nick}\nradio.1.ieee_mode={$net_mode}\nradio.1.rate.max={$rate_max}\nradio.1.txpower={$txpower}\nradio.1.acktimeout={$ack}\nradio.1.rx_antenna={$radiorx}\nradio.1.tx_antenna={$radiotx}\nntpd.1.server={$ntp1}\nskin.active={$skin}\nnetconf.{$iiface}.duplex=full\nnetconf.{$iiface}.speed=100\nnetconf.{$iiface}.up=enabled\n{$specs}\n";
    fwrite($Handle, $Data);
    _outln_comment('Unsolclic version: ' . $version);
    print '<br/><a href="' . base_path() . 'files/guifistation/' . $dev->nick . '.cfg"> Click here to download configuration file for: ' . $dev->nick . ' </a><br />';
    print 'Put the mouse cursor over the link. Right click the link and select "Save Link/Target As..." to save to your Desktop.<br /><br />';
    fclose($Handle);
    _outln_comment(' Method to upload/execute the file:');
    _outln('     1. Open your web browser and type the router IP address (Usually 192.168.2.66) and login');
    _outln('     2. Go to System Tab');
    _outln('     3. Press on restore button');
    _outln('     4. Select downloaded file and upload it');
    _outln('     5. When the saved new settings message appears on the screen, click on Reboot button');
    _outln('     6. Wait aproximate 2 minutes, then you can surf the network!');
    _outln();
    _outln_comment(' Notes:');
    _outln('   The script reconfigures IP addresses, so communication can be lost.');
    _outln(' -Changes are done in user passwords on the device,');
    _outln('  default user and password are changed to admin/guifi.');
    _outln(' -The ACK is set to 45 for 802.11b mode, and to 45 for 802.11a (600 meters aprox,)');
    _outln();
    _outln_comment(' Link to AP info:');
    _outln('
    Ap SSID = guifi.net-' . $apssid . '<br />
    WAN Ip address = ' . $wan->ipv4 . '<br />
    WAN Netmask = ' . $wan->netmask . '<br />
    WAN Gateway = ' . $gateway . '<br />
    Primary DNS Server = ' . $primary_dns . '<br />
    Secondary DNS Server = ' . $secondary_dns . '<br />
    Device HostName = ' . $dev->nick . '<br />
    IEEE 802.11 Mode: = ' . $lnet_mode . '<br />
        ');
}
function guifi_kamikaze_files($dev, $zone)
{
    //SOME VARIABLES
    $dns = guifi_get_dns($zone, 2);
    $lan = guifi_unsolclic_if($dev->id, 'wLan/Lan');
    $lan_network = _ipcalc($lan->ipv4, $lan->netmask);
    switch ($dev->variable['model_id']) {
        case "39":
            // Avila GW2348-4
            $wireless_model = 'atheros';
            $lan_iface = 'eth0';
            $lan2_iface = 'eth1';
            $packages = 'ixp4xx/packages';
            break;
        default:
            _outln_comment('model id not supported');
            exit;
    }
    // SECTION FILES
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/wireless'));
    function wds_add($dev, $radio)
    {
        $wds_links = array();
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wds/p2p') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['link_type'] == 'wds') {
                            $wds_links[] = $link;
                        }
                    }
                }
                $ifcount = 0;
                foreach ($wds_links as $key => $wds) {
                    if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                        $status = 'active';
                        print '        option \'wds_add' . $ifcount . '\' \'' . $wds['interface']['mac'] . '\'<br />';
                        $ifcount++;
                    } else {
                        $status = 'disabled';
                        print '# option \'wds_addX\' \'' . $wds['interface']['mac'] . '\'# ' . t($wds['flag']) . '<br />';
                    }
                }
            }
        }
    }
    function wds_network($dev, $radio)
    {
        $ifcount = '0';
        foreach ($radio[interfaces] as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    if ($link['link_type'] == 'wds') {
                        $wds_links = array();
                        $wds_links[] = $link;
                        foreach ($wds_links as $key => $wds) {
                            $hostname = guifi_get_hostname($wds['device_id']);
                            if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                                $status = 'active';
                                print '
## wds_' . $hostname . '
config \'interface\' \'wds_' . $hostname . '\'
        option \'ifname\' \'ath' . $radio[radiodev_counter] . '.wds' . $ifcount . '\'
        option \'proto\' \'static\'
';
                                $ifcount++;
                            } else {
                                $status = 'disabled';
                                print '
##### ' . t($wds['flag']) . ' ####
## wds_' . $hostname . '
# config \'interface\' \'ath' . $radio[radiodev_counter] . '.wdsX\'
#       option \'proto\' \'none\'
';
                            }
                        }
                        if ($status == 'active') {
                            print '        option \'ipaddr\' \'' . $ipv4[ipv4] . '\'
        option \'netmask\' \'' . $ipv4['netmask'] . '\'
';
                        } else {
                            print '#       option \'ipaddr\' \'' . $ipv4[ipv4] . '\'
#       option \'netmask\' \'' . $ipv4['netmask'] . '\'
';
                        }
                    }
                }
            }
        }
    }
    function cable_network($dev)
    {
        foreach ($dev->interfaces as $interface_id => $interface) {
            switch ($interface[interface_type]) {
                case 'vlan1':
                    $iname = 'eth0:1';
                    break;
                case 'vlan2':
                    $iname = 'eth1';
                    break;
                case 'vlan3':
                    $iname = 'eth2';
                    break;
                default:
                    $iname = $interface[interface_type];
                    break;
            }
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    $network = guifi_get_hostname($link[device_id]);
                    if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                        $status = 'active';
                        if ($interface[interface_type] != 'wLan/Lan') {
                            print '
## cable_' . $network . '
config \'interface\' \'' . $network . '\'
        option \'ifname\'   \'' . $iname . '\'
';
                            print '        option \'proto\'    \'static\'
        option \'ipaddr\'   \'' . $ipv4[ipv4] . '\'
        option \'netmask\'  \'' . $ipv4[netmask] . '\'

';
                        }
                    } else {
                        $status = 'disabled';
                        print '
##### ' . t($link['flag']) . ' ####
## cable_' . $network . '
# config \'interface\' \'' . $network . '\'
#        option \'ifname\'   \'' . $iname . '\'
';
                        print '#        option \'proto\'    \'static\'
#        option \'ipaddr\'   \'' . $ipv4[ipv4] . '\'
#        option \'netmask\'  \'' . $ipv4[netmask] . '\'

';
                    }
                }
            }
        }
    }
    print '<pre>
echo "
';
    if (isset($dev->radios)) {
        foreach ($dev->radios as $radio_id => $radio) {
            $mode = 'ap';
            $ssid = $radio[ssid];
            $channel = atheros_channel($radio);
            if ($channel < 14) {
                $band = '11b';
            } else {
                $band = '11a';
            }
            if (empty($radio[antenna_mode])) {
                $radio[antenna_mode] = '1';
            } else {
                if ($radio[antenna_mode] != 'Main') {
                    $radio[antenna_mode] = '2';
                } else {
                    $radio[antenna_mode] = '1';
                }
            }
            if ($radio_id == '0') {
                $wireless_iface = 'wifi0';
                $network = 'lan';
            } else {
                $wireless_iface = 'wifi' . $radio_id;
                $network = 'wlan' . ($radio_id + 1);
            }
            $wireless_model = 'atheros';
            $txant = 'txantenna';
            $rxant = 'rxantenna';
            print '
## Radio: ' . $radio[ssid] . '
config \'wifi-device\' \'' . $wireless_iface . '\'
        option \'type\' \'' . $wireless_model . '\'
        option \'channel\' \'' . $channel . '\'
        option \'disabled\' \'0\'
        option \'hwmode\' \'' . $band . '\'
        option \'diversity\' \'0\'
        option \'' . $txant . '\' \'' . $radio[antenna_mode] . '\'
        option \'' . $rxant . '\' \'' . $radio[antenna_mode] . '\'
        option \'txpower\' \'16\'

config wifi-iface
        option \'device\' \'' . $wireless_iface . '\'
        option \'network\' \'' . $network . '\'
        option \'agmode\' \'' . $mode . '\'
        option \'ssid\' \'guifi.net-' . $radio[ssid] . '\'
        option \'encryption\' \'none\'
';
            wds_add($dev, $radio);
        }
    }
    print '
" > /etc/config/wireless </pre>
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/network'));
    print '<pre>
echo "
config interface loopback
        option \'ifname\'  \'lo\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'127.0.0.1\'
        option \'netmask\'  \'255.0.0.0\'

';
    if (isset($dev->radios)) {
        foreach ($dev->radios as $radio_id => $radio) {
            if (isset($radio[interfaces])) {
                foreach ($radio[interfaces] as $interface_id => $interface) {
                    if ($interface[interface_type] != 'wds/p2p') {
                        if (isset($interface[ipv4])) {
                            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                                if ($interface[interface_type] == 'wLan/Lan') {
                                    $iface = 'ath0 eth0';
                                    $network = 'lan';
                                } else {
                                    $iface = 'wifi' . $radio_id;
                                    $network = 'wlan' . ($radio_id + 1);
                                }
                                $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                                // FILE NETWORK
                                print '
config interface ' . $network . '
        option \'ifname\'   \'' . $iface . '\'
';
                                if ($interface[interface_type] == 'wLan/Lan') {
                                    print '        option \'type\'     \'bridge\'
';
                                }
                                print '        option \'proto\'    \'static\'
        option \'ipaddr\'   \'' . $ipv4[ipv4] . '\'
        option \'netmask\'  \'' . $ipv4[netmask] . '\'
        option \'dns\'      \'' . $dns . '\'

';
                            }
                        }
                    }
                }
            }
            wds_network($dev, $radio);
        }
    }
    cable_network($dev);
    print '
" > /etc/config/network </pre>
';
    $wds_links = array();
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wds/p2p') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['link_type'] == 'wds') {
                            $wds_links[] = $link;
                        }
                    }
                }
            }
        }
    }
    foreach ($wds_links as $key => $wds) {
        if ($wds['routing'] == 'BGP') {
            $wds_bgpd = '1';
        }
        if ($wds['routing'] == 'OSPF') {
            $wds_ospfd = '1';
        }
    }
    $cable_links = array();
    foreach ($dev->interfaces as $interface_id => $interface) {
        foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
            foreach ($ipv4[links] as $link_id => $link) {
                if ($link['link_type'] == 'cable') {
                    $cable_links[] = $link;
                }
            }
        }
    }
    foreach ($cable_links as $key => $cable) {
        if ($cable['routing'] == 'BGP') {
            $cable_bgpd = '1';
        }
        if ($cable['routing'] == 'OSPF') {
            $cable_ospfd = '1';
        }
    }
    // QUAGGA CONFIG FILES
    $file_zebra = '';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/quagga/zebra.conf'));
    _out_file($file_zebra, '/etc/quagga/zebra.conf');
    // FILE OSPFD
    if ($wds_ospfd == '1' || $cable_ospfd == '1') {
        _outln_comment();
        _outln_comment();
        _outln_comment(t('File /etc/quagga/ospfd.conf'));
        print '<pre>mv /etc/quagga/ospfd.conf /etc/quagga/ospfd.conf.bak
echo "
!
interface br-lan
!
router ospf
 ospf router-id ' . $lan->ipv4 . '
 redistribute bgp
';
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                    foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                        $network = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    }
                    if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                        print ' network ' . $network[netid] . '/' . $network[maskbits] . ' area 0<br />';
                    }
                }
            }
        }
        foreach ($wds_links as $key => $wds) {
            $iplocal[] = $wds['interface']['ipv4'];
            if ($wds['routing'] == 'OSPF') {
                $wds_network = _ipcalc($iplocal[$key][ipv4], $iplocal[$key][netmask]);
                if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                    print ' network ' . $wds_network[netid] . '/' . $wds_network[maskbits] . ' area 0<br />';
                }
            }
        }
        foreach ($dev->interfaces as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    if ($link['routing'] == 'OSPF') {
                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                            print ' network ' . $item[netid] . '/' . $item[maskbits] . ' area 0<br />';
                        }
                    }
                }
            }
        }
        print 'default-information originate
!
" > /etc/quagga/ospfd.conf</pre>';
    }
    // FILE BGPD
    if ($wds_bgpd == '1' || $cable_bgpd == '1') {
        _outln_comment();
        _outln_comment();
        _outln_comment(t('File /etc/quagga/bgpd.conf'));
        print '<pre>mv /etc/quagga/bgpd.conf /etc/quagga/bgpd.conf.bak
echo "
!
interface br-lan
!
router bgp ' . $dev->id . '
bgp router-id ' . $lan->ipv4 . '
';
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                    foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                        $network = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    }
                    print ' network ' . $network[netid] . '/' . $network[maskbits] . '<br />';
                }
            }
        }
        print 'redistribute ospf
';
        foreach ($wds_links as $key => $wds) {
            $iplocal[] = $wds['interface']['ipv4'];
            if ($wds['routing'] == 'BGP') {
                $wds_network = _ipcalc($iplocal[$key][ipv4], $iplocal[$key][netmask]);
                if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
                    print ' network ' . $wds_network[netid] . '/' . $wds_network[maskbits] . '<br />';
                }
            }
        }
        foreach ($dev->interfaces as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
                    if ($link['routing'] == 'BGP') {
                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                            print ' network ' . $item[netid] . '/' . $item[maskbits] . '<br />';
                        }
                    }
                }
            }
        }
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['routing'] == 'BGP') {
                            if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                                print ' neighbor ' . $link['interface']['ipv4']['ipv4'] . ' remote-as ' . $link['device_id'] . '
';
                            }
                        }
                    }
                }
            }
        }
        foreach ($dev->interfaces as $interface_id => $interface) {
            foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                foreach ($ipv4[links] as $link_id => $link) {
                    if ($link['routing'] == 'BGP') {
                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                            print ' neighbor ' . $link['interface']['ipv4']['ipv4'] . ' remote-as ' . $link['device_id'] . '
';
                        }
                    }
                }
            }
        }
        print '" > /etc/quagga/bgpd.conf</pre>';
    }
    //FILE FIREWALL
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/firewall'));
    print '<pre>
echo "
config defaults
        option \'syn_flood\' \'1\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'ACCEPT\'
';
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    if ($interface[interface_type] == 'wLan/Lan') {
                        $network = 'lan';
                    } else {
                        $network = 'wlan' . ($radio_id + 1);
                    }
                    print '        
config zone
        option \'name\' \'' . $network . '\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'ACCEPT\'

';
                }
            }
        }
    }
    if ($interface[interface_type] == 'wds/p2p') {
        foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
            foreach ($ipv4[links] as $link_id => $link) {
                if ($link['link_type'] == 'wds') {
                    $ifcount = 0;
                }
            }
        }
    }
    foreach ($wds_links as $key => $wds) {
        $hostname = guifi_get_hostname($wds['device_id']);
        if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
            print '        
config zone
        option \'name\' \'wds_' . $hostname . '\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'ACCEPT\'

';
        }
    }
    foreach ($dev->interfaces as $interface_id => $interface) {
        foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
            foreach ($ipv4[links] as $link_id => $link) {
                $hostname = guifi_get_hostname($link['device_id']);
                if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                    print '        
config zone
        option \'name\' \'' . $hostname . '\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'ACCEPT\'

';
                }
            }
        }
    }
    print '" > /etc/config/firewall</pre>';
    //FILE OPKG
    $opkg_conf = '
src/gz snapshots http://downloads.openwrt.org/snapshots/' . $packages . '
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /jffs
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/opkg.conf'));
    _out_file($opkg_conf, '/etc/opkg.conf');
    $dhcp_statics = array();
    $max = explode(".", $dev->ipv4);
    function merge_static($link, &$dhcp_statics, &$max, &$curr)
    {
        if (empty($link['interface'][mac])) {
            $link['interface'][mac] = 'FF:FF:FF:FF:FF:FF';
        }
        $dhcp_statics[] = array($link['interface'][ipv4][ipv4], $link['interface'][mac], guifi_get_hostname($link['interface'][device_id]));
        $curr = explode(".", $link['interface'][ipv4][ipv4]);
        if ($curr[3] > $max[3]) {
            $max[3] = $curr[3];
        }
    }
    $main_ip = guifi_main_ip($dev->id);
    $item = _ipcalc_by_netbits($main_ip[ipv4], $main_ip[maskbits]);
    $max = explode(".", $main_ip[ipv4]);
    // cable links
    foreach ($dev->interfaces as $interface) {
        foreach ($interface[ipv4] as $ipv4) {
            foreach ($ipv4[links] as $link) {
                if ($link['interface'][ipv4][ipv4] != '') {
                    $item2 = _ipcalc($link['interface'][ipv4][ipv4], $link['interface'][ipv4][netmask]);
                    if ($item[netid] == $item2[netid]) {
                        merge_static($link, $dhcp_statics, $max, $cur);
                    }
                }
            }
        }
    }
    // ap/client links
    foreach ($dev->radios as $radio) {
        foreach ($radio[interfaces] as $interface) {
            foreach ($interface[ipv4] as $ipv4) {
                foreach ($ipv4[links] as $link) {
                    if ($link['link_type'] == 'ap/client' and !empty($link['interface'][ipv4][ipv4])) {
                        merge_static($link, $dhcp_statics, $max, $cur);
                    }
                }
            }
        }
    }
    $statics = count($dhcp_statics) - 1;
    $totalstatics = count($dhcp_statics);
    $first = explode(".", $item[netid]);
    $last = explode(".", $item[broadcast]);
    $limit = $last[3] - 1 - ($first[3] + 3) - $totalstatics;
    _outln_comment();
    _outln_comment(t('File /etc/config/luci_ethers'));
    print 'echo "';
    foreach ($dhcp_statics as $static) {
        print '<pre>
## Device: ' . $static[2] . '
config \'static_lease\'
        option \'macaddr\' \'' . $static[1] . '\'
        option \'ipaddr\' \'' . $static[0] . '\'
</pre>';
    }
    print '" > /etc/config/luci_ethers<br />';
    // FILE DHCP
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/dhcp'));
    print '<pre>
echo "
config \'dnsmasq\'
        option \'domainneeded\' \'1\'
        option \'boguspriv\' \'1\'
        option \'filterwin2k\' \'0\'
        option \'localise_queries\' \'1\'
        option \'local\' \'/lan/\'
        option \'domain\' \'lan\'
        option \'expandhosts\' \'1\'
        option \'nonegcache\' \'0\'
        option \'authoritative\' \'1\'
        option \'readethers\' \'1\'
        option \'leasefile\' \'/tmp/dhcp.leases\'
        option \'resolvfile\' \'/tmp/resolv.conf.auto\'
';
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface[interface_type] == 'wLan' || $interface[interface_type] == 'wLan/Lan') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    if ($interface[interface_type] == 'wLan/Lan') {
                        $iface = 'lan';
                        $network = 'lan';
                    } else {
                        $network = 'wlan' . ($radio_id + 1);
                    }
                    $max = explode(".", $ipv4[ipv4]);
                    foreach ($ipv4[links] as $link) {
                        if ($link['link_type'] == 'ap/client' and !empty($link['interface'][ipv4][ipv4])) {
                            $totalstaticss = count($ipv4[links]);
                        }
                    }
                    $first = explode(".", $item[netid]);
                    $last = explode(".", $item[broadcast]);
                    $limit = $last[3] - 1 - ($first[3] + 3) - $totalstaticss;
                    $totalstaticss = '0';
                    print '
 config \'dhcp\' \'' . $network . '\'
        option \'interface\' \'' . $network . '\'
        option \'leasetime\' \'12h\'
        option \'start\' \'' . ($max[3] + 2) . '\'
        option \'limit\' \'' . $limit . '\'
';
                }
            }
        }
    }
    print '" > /etc/config/dhcp<br /></pre>';
}
function guifi_kamikaze_files($dev, $zone)
{
    //SOME VARIABLES
    foreach ($dev->radios[0]['interfaces'] as $interface_id => $interface) {
        foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
            if (isset($ipv4['links'])) {
                foreach ($ipv4['links'] as $key => $link) {
                    if ($link['link_type'] == 'ap/client') {
                        $gateway = $link['interface']['ipv4']['ipv4'];
                    }
                }
            }
        }
    }
    $wan = guifi_unsolclic_if($dev->id, 'Wan');
    $dns = guifi_get_dns($zone, 2);
    list($ntp1, $ntp2) = explode(' ', guifi_get_ntp($zone, 2));
    $ntp[] .= $ntp1;
    $ntp[] .= $ntp2;
    $apssid = 'guifi.net-' . guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']);
    $wireless_model = 0;
    $wireless_iface = 0;
    switch ($dev->variable['model_id']) {
        case "25":
        case "26":
        case "32":
        case "33":
        case "34":
        case "35":
        case "36":
        case "37":
            // NanoStationX, LiteStationX, NanoStation LocoX, Bullet
            $wireless_model = 'atheros';
            $wireless_iface = 'wifi0';
            $vlans = NULL;
            $mode = NULL;
            if ($dev->variable['model_id'] == 25 || $dev->variable['model_id'] == 32 || $dev->variable['model_id'] == 34 || $dev->variable['model_id'] == 36) {
                $mode = 'option \'hwmode\' \'11b\'';
            } else {
                $mode = 'option \'hwmode\' \'11a\'';
            }
            $lan_iface = 'eth0';
            $wan_iface = 'ath0';
            $txant = 'txantenna';
            $rxant = 'rxantenna';
            $packages = 'atheros/packages';
            break;
        default:
            _outln_comment('model id not supported');
            exit;
    }
    if (empty($dev->radios[0][antenna_mode])) {
        $dev->radios[0][antenna_mode] = 'Main';
    }
    if ($dev->radios[0][antenna_mode] != 'Main') {
        $dev->radios[0][antenna_mode] = '1';
    } else {
        $dev->radios[0][antenna_mode] = '0';
    }
    // SECTION FILES
    // FILE NETWORK
    $file_network = '
' . $vlans . '
config interface loopback
        option \'ifname\'  \'lo\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'127.0.0.1\'
        option \'netmask\'  \'255.0.0.0\'

config interface lan
        option \'ifname\'   \'' . $lan_iface . '\'
        option \'type\'     \'bridge\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'192.168.1.1\'
        option \'netmask\'  \'255.255.255.0\'
        option \'dns\'      \'' . $dns . '\'

config interface wan
        option \'ifname\'   \'' . $wan_iface . '\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'' . $wan->ipv4 . '\'
        option \'netmask\'  \'' . $wan->netmask . '\'
        option \'gateway\'  \'' . $gateway . '\'
        option \'dns\'      \'' . $dns . '\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/network'));
    _out_file($file_network, '/etc/config/network');
    // FILE WIRELESS
    $file_wireless = '
config \'wifi-device\' \'' . $wireless_iface . '\'
        option \'type\' \'' . $wireless_model . '\'
        option \'disabled\' \'0\'
        option \'diversity\' \'0\'
        option \'' . $txant . '\' \'' . $dev->radios[0][antenna_mode] . '\'
        option \'' . $rxant . '\' \'' . $dev->radios[0][antenna_mode] . '\'
        ' . $mode . '
        option \'txpower\' \'16\'

config wifi-iface
        option \'device\' \'' . $wireless_iface . '\'
        option \'network\' \'wan\'
        option \'mode\' \'sta\'
        option \'ssid\' \'' . $apssid . '\'
        option \'encryption\' \'none\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/wireless'));
    _out_file($file_wireless, '/etc/config/wireless');
    //FILE FIREWALL
    $firewall = '
config defaults
        option \'syn_flood\' \'1\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'REJECT\'

config zone
        option \'name\' \'lan\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'REJECT\'

config zone
        option \'name\' \'wan\'
        option \'output\' \'ACCEPT\'
        option \'input\' \'ACCEPT\'
        option \'forward\' \'REJECT\'
        option \'masq\' \'1\'

config forwarding
        option \'src\' \'lan\'
        option \'dest\' \'wan\'

config rule
        option \'dst\'              \'wan\'
        option \'src_dport\'        \'22\'
        option \'target\'           \'ACCEPT\'
        option \'protocol\'         \'tcp\'

config rule
        option \'dst\'              \'wan\'
        option \'src_dport\'        \'80\'
        option \'target\'           \'ACCEPT\'
        option \'protocol\'         \'tcp\'

config rule
        option \'dst\'              \'wan\'
        option \'src_dport\'        \'161\'
        option \'target\'           \'ACCEPT\'
        option \'protocol\'         \'udp\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/firewall'));
    _out_file($firewall, '/etc/config/firewall');
    //FILE OPKG
    $opkg_conf = '
src/gz snapshots http://downloads.openwrt.org/snapshots/' . $packages . '
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /jffs
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/opkg.conf'));
    _out_file($opkg_conf, '/etc/opkg.conf');
}
function guifi_kamikaze_files($dev, $zone)
{
    //SOME VARIABLES
    foreach ($dev->radios[0]['interfaces'] as $interface_id => $interface) {
        foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
            if (isset($ipv4['links'])) {
                foreach ($ipv4['links'] as $key => $link) {
                    if ($link['link_type'] == 'ap/client') {
                        $gateway = $link['interface']['ipv4']['ipv4'];
                    }
                }
            }
        }
    }
    $wan = guifi_unsolclic_if($dev->id, 'Wan');
    $dns = guifi_get_dns($zone, 2);
    list($ntp1, $ntp2) = explode(' ', guifi_get_ntp($zone, 2));
    $ntp[] .= $ntp1;
    $ntp[] .= $ntp2;
    $apssid = 'guifi.net-' . guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']);
    $wireless_model = 0;
    $wireless_iface = 0;
    switch ($dev->variable['model_id']) {
        case "1":
        case "15":
        case "16":
        case "17":
        case "18":
            // WRT54Gv1-4, WHR-HP-G54, WHR-G54S (BUFFALO), WRT54GL, WRT54GSv1-2, WRT54GSv4
            $wireless_model = 'broadcom';
            $wireless_iface = 'wl0';
            $vlans = 'config switch eth0
        option vlan0    \\"1 2 3 4 5*\\"
        option vlan1    \\"0 5\\"
      ';
            $mode = NULL;
            $lan_iface = 'eth0.0';
            $wan_iface = 'eth0.1';
            $txant = 'txant';
            $rxant = 'rxant';
            $packages = 'broadcom/packages';
            break;
        default:
            _outln_comment('model id not supported');
            exit;
    }
    if (empty($dev->radios[0][antenna_mode])) {
        $dev->radios[0][antenna_mode] = 'Main';
    }
    if ($dev->radios[0][antenna_mode] != 'Main') {
        $dev->radios[0][antenna_mode] = '1';
    } else {
        $dev->radios[0][antenna_mode] = '0';
    }
    // SECTION FILES
    // FILE NETWORK
    $file_network = '
' . $vlans . '
config interface loopback
        option \'ifname\'  \'lo\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'127.0.0.1\'
        option \'netmask\'  \'255.0.0.0\'

config interface lan
        option \'ifname\'   \'' . $lan_iface . '\'
        option \'type\'     \'bridge\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'192.168.1.1\'
        option \'netmask\'  \'255.255.255.0\'
        option \'dns\'      \'' . $dns . '\'

config interface wan
        option \'ifname\'   \'' . $wan_iface . '\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'' . $wan->ipv4 . '\'
        option \'netmask\'  \'' . $wan->netmask . '\'
        option \'gateway\'  \'' . $gateway . '\'
        option \'dns\'      \'' . $dns . '\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/network'));
    _out_file($file_network, '/etc/config/network');
    // FILE WIRELESS
    $file_wireless = '
config \'wifi-device\' \'' . $wireless_iface . '\'
        option \'type\' \'' . $wireless_model . '\'
        option \'disabled\' \'0\'
        option \'' . $txant . '\' \'' . $dev->radios[0][antenna_mode] . '\'
        option \'' . $rxant . '\' \'' . $dev->radios[0][antenna_mode] . '\'
        ' . $mode . '

config wifi-iface
        option \'device\' \'' . $wireless_iface . '\'
        option \'network\' \'wan\'
        option \'mode\' \'sta\'
        option \'ssid\' \'' . $apssid . '\'
        option \'encryption \'none\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/wireless'));
    _out_file($file_wireless, '/etc/config/wireless');
    //FILE FIREWALL
    $firewall = '
config defaults
        option \'syn_flood\' \'1\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'REJECT\'

config zone
        option \'name\' \'lan\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'REJECT\'

config zone
        option \'name\' \'wan\'
        option \'output\' \'ACCEPT\'
        option \'input\' \'ACCEPT\'
        option \'forward\' \'REJECT\'
        option \'masq\' \'1\'

config forwarding
        option \'src\' \'lan\'
        option \'dest\' \'wan\'

config rule
        option \'dst\'              \'wan\'
        option \'src_dport\'        \'22\'
        option \'target\'           \'ACCEPT\'
        option \'protocol\'         \'tcp\'

config rule
        option \'dst\'              \'wan\'
        option \'src_dport\'        \'80\'
        option \'target\'           \'ACCEPT\'
        option \'protocol\'         \'tcp\'

config rule
        option \'dst\'              \'wan\'
        option \'src_dport\'        \'161\'
        option \'target\'           \'ACCEPT\'
        option \'protocol\'         \'udp\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/firewall'));
    _out_file($firewall, '/etc/config/firewall');
    //FILE OPKG
    $opkg_conf = '
src/gz guifi http://ausa.guifi.net/drupal/files/openwrt/client/' . $packages . '
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/opkg.conf'));
    _out_file($opkg_conf, '/etc/opkg.conf');
}
function guifi_kamikaze_files($dev, $zone)
{
    //SOME VARIABLES
    $lan = guifi_unsolclic_if($dev->id, 'wLan/Lan');
    $wan = guifi_unsolclic_if($dev->id, 'Wan');
    $dns = guifi_get_dns($zone, 2);
    $wireless_model = 0;
    $wireless_iface = 0;
    switch ($dev->variable['model_id']) {
        case "1":
        case "15":
        case "16":
        case "17":
        case "18":
            // WRT54Gv1-4, WHR-HP-G54, WHR-G54S (BUFFALO), WRT54GL, WRT54GSv1-2, WRT54GSv4
            $wireless_model = 'broadcom';
            $wireless_iface = 'wl0';
            $vlans = 'config switch eth0
        option vlan0    \\"1 2 3 4 5*\\"
        option vlan1    \\"0 5\\"
      ';
            $lan_iface = 'eth0.0';
            $wan_iface = 'eth0.1';
            $txant = 'txant';
            $rxant = 'rxant';
            $packages = 'broadcom/packages';
            break;
        default:
            _outln_comment('model id not supported');
            exit;
    }
    if (empty($dev->radios[0][antenna_mode])) {
        $dev->radios[0][antenna_mode] = 'Main';
    }
    if ($dev->radios[0][antenna_mode] != 'Main') {
        $dev->radios[0][antenna_mode] = '1';
    } else {
        $dev->radios[0][antenna_mode] = '0';
    }
    $wds_links = array();
    $wds_str = '';
    foreach ($dev->radios as $radio_id => $radio) {
        foreach ($radio[interfaces] as $interface_id => $interface) {
            if ($interface['interface_type'] == 'wds/p2p') {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $key => $link) {
                        if ($link['link_type'] == 'wds') {
                            $wds_links[] = $link;
                        }
                        $iplocal[] = $ipv4;
                        $iflocal[] = $interface;
                    }
                }
            }
        }
    }
    if (count($wds_links) == 0) {
        return;
    }
    // SECTION FILES
    // FILE NETWORK
    $lan_network = _ipcalc($lan->ipv4, $lan->netmask);
    $file_network = '
' . $vlans . '
config interface loopback
        option \'ifname\'  \'lo\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'127.0.0.1\'
        option \'netmask\'  \'255.0.0.0\'

config interface lan
        option \'ifname\'   \'' . $lan_iface . '\'
        option \'type\'     \'bridge\'
        option \'proto\'    \'static\'
        option \'ipaddr\'   \'' . $lan->ipv4 . '\'
        option \'netmask\'  \'' . $lan->netmask . '\'
        option \'gateway\'  \'0.0.0.0\'
        option \'dns\'      \'' . $dns . '\'

config interface wan
        option \'ifname\'   \'' . $wan_iface . '\'
        option \'proto\'    \'none\'
        
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/network'));
    _out_file($file_network, '/etc/config/network');
    // FILE WIRELESS
    $file_wireless = '
config \'wifi-device\' \'' . $wireless_iface . '\'
        option \'type\' \'' . $wireless_model . '\'
        option \'channel\' \'' . $dev->radios[0][channel] . '\'
        option \'disabled\' \'0\'
        option \'' . $txant . '\' \'' . $dev->radios[0][antenna_mode] . '\'
        option \'' . $rxant . '\' \'' . $dev->radios[0][antenna_mode] . '\'

config wifi-iface
        option \'device\' \'' . $wireless_iface . '\'
        option \'network\' \'lan\'
        option \'mode\' \'ap\'
        option \'ssid\' \'guifi.net-' . guifi_to_7bits($dev->radios[0][ssid]) . '\'
        option \'encryption \'none\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/wireless'));
    _out_file($file_wireless, '/etc/config/wireless');
    // WDS Links
    $ifcount = 1;
    foreach ($wds_links as $key => $wds) {
        $hostname = guifi_get_hostname($wds['device_id']);
        if (preg_match("/(Working|Testing|Building)/", $wds['flag'])) {
            $status = 'active';
            if ($wds['routing'] == 'BGP') {
                $bgpd = '1';
            }
            if ($wds['routing'] == 'OSPF') {
                $ospfd = '1';
            }
            $ifcount++;
            $wds_won = 'option \'bssid\' \'' . $wds['interface']['mac'] . '\'';
            $wds_non = 'option \'proto\' \'static\'
        option \'ifname\' \'wds0.' . ($key + 1) . '\'
        option \'ipaddr\' \'' . $iplocal[$key][ipv4] . '\'
        option \'netmask\' \'' . $iplocal[$key][netmask] . '\'';
        } else {
            $status = 'disabled';
            $wds_won = 'option \'bssid\' \'00:00:00:00:00:00\'';
            $wds_non = 'option \'proto\' \'none\'';
        }
        $wds_network = 'config \'interface\' \'wds_' . $hostname . '\'
        ' . $wds_non . '
        ';
        $wds_wireless = 'config \'wifi-iface\'
        option \'device\' \'wl0\'
        option \'network\' \'wds_' . $hostname . '\'
        option \'mode\' \'wds\'
        option \'encryption\' \'none\'
        ' . $wds_won . '
        ';
        _outln_comment();
        _outln_comment('WDS ' . $hostname . '');
        _outln_comment('Routing: ' . $wds['routing'] . '');
        _outln_comment('Status: ' . $status . '');
        print '<pre>echo "' . $wds_network . '
" >> /etc/config/network
          </pre>';
        print '<pre>echo "' . $wds_wireless . '
" >> /etc/config/wireless
          </pre>';
    }
    if (count($wds_links) >= 5) {
        return;
    }
    // QUAGGA CONFIG FILES
    $file_zebra = '';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/quagga/zebra.conf'));
    _out_file($file_zebra, '/etc/quagga/zebra.conf');
    // FILE OSPFD
    if ($ospfd == '1') {
        _outln_comment();
        _outln_comment();
        _outln_comment(t('File /etc/quagga/ospfd.conf'));
        print '<pre>mv /etc/quagga/ospfd.conf /etc/quagga/ospfd.conf.bak
echo "
!
interface br-lan
!
router ospf
 ospf router-id ' . $lan->ipv4 . '
 redistribute bgp
 network ' . $lan_network[netid] . '/' . $lan_network[maskbits] . ' area 0<br />';
        foreach ($wds_links as $key => $wds) {
            if ($wds['routing'] == 'OSPF') {
                $wds_network = _ipcalc($iplocal[$key][ipv4], $iplocal[$key][netmask]);
                print ' network ' . $wds_network[netid] . '/' . $wds_network[maskbits] . ' area 0<br />';
            }
        }
        print 'default-information originate
!
" > /etc/quagga/ospfd.conf</pre>';
    }
    // FILE BGPD
    if ($bgpd == '1') {
        _outln_comment();
        _outln_comment();
        _outln_comment(t('File /etc/quagga/bgpd.conf'));
        print '<pre>mv /etc/quagga/bgpd.conf /etc/quagga/bgpd.conf.bak
echo "
!
interface br-lan
!
router bgp ' . $dev->id . '
bgp router-id ' . $lan->ipv4 . '
 network ' . $lan_network[netid] . '/' . $lan_network[maskbits] . '<br />';
        foreach ($wds_links as $key => $wds) {
            if ($wds['routing'] == 'BGP') {
                $wds_network = _ipcalc($iplocal[$key][ipv4], $iplocal[$key][netmask]);
                print ' network ' . $wds_network[netid] . '/' . $wds_network[maskbits] . '<br />';
            }
        }
        print 'redistribute ospf
';
        foreach ($dev->radios as $radio_id => $radio) {
            foreach ($radio[interfaces] as $interface_id => $interface) {
                foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                    foreach ($ipv4[links] as $link_id => $link) {
                        if ($link['routing'] == 'BGP') {
                            print 'neighbor ' . $link['interface']['ipv4']['ipv4'] . ' remote-as ' . $link['device_id'] . '
';
                        }
                    }
                }
            }
        }
        print '" > /etc/quagga/bgpd.conf</pre>';
    }
    //FILE FIREWALL
    $firewall = '
config defaults
        option \'syn_flood\' \'1\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'ACCEPT\'
        
config zone
        option \'name\' \'lan\'
        option \'input\' \'ACCEPT\'
        option \'output\' \'ACCEPT\'
        option \'forward\' \'ACCEPT\'

';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/firewall'));
    _out_file($firewall, '/etc/config/firewall');
    //FILE OPKG
    $opkg_conf = '
src/gz guifi http://ausa.guifi.net/drupal/files/openwrt/ap/' . $packages . '
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/opkg.conf'));
    _out_file($opkg_conf, '/etc/opkg.conf');
    $dhcp_statics = array();
    $max = explode(".", $dev->ipv4);
    function merge_static($link, &$dhcp_statics, &$max, &$curr)
    {
        if (empty($link['interface'][mac])) {
            $link['interface'][mac] = 'FF:FF:FF:FF:FF:FF';
        }
        $dhcp_statics[] = array($link['interface'][ipv4][ipv4], $link['interface'][mac], guifi_get_hostname($link['interface'][device_id]));
        $curr = explode(".", $link['interface'][ipv4][ipv4]);
        if ($curr[3] > $max[3]) {
            $max[3] = $curr[3];
        }
    }
    $main_ip = guifi_main_ip($dev->id);
    $item = _ipcalc_by_netbits($main_ip[ipv4], $main_ip[maskbits]);
    $max = explode(".", $main_ip[ipv4]);
    // cable links
    if (!empty($dev->interfaces)) {
        foreach ($dev->interfaces as $interface) {
            if (!empty($interface[ipv4])) {
                foreach ($interface[ipv4] as $ipv4) {
                    if (!empty($ipv4[links])) {
                        foreach ($ipv4[links] as $link) {
                            if ($link['interface'][ipv4][ipv4] != '') {
                                $item2 = _ipcalc($link['interface'][ipv4][ipv4], $link['interface'][ipv4][netmask]);
                                if ($item[netid] == $item2[netid]) {
                                    merge_static($link, $dhcp_statics, $max, $cur);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // ap/client links
    if (!empty($dev->radios)) {
        foreach ($dev->radios as $radio) {
            if (!empty($radio[interfaces])) {
                foreach ($radio[interfaces] as $interface) {
                    if (!empty($interface[ipv4])) {
                        foreach ($interface[ipv4] as $ipv4) {
                            if (!empty($ipv4[links])) {
                                foreach ($ipv4[links] as $link) {
                                    if ($link['link_type'] == 'ap/client' and !empty($link['interface'][ipv4][ipv4])) {
                                        merge_static($link, $dhcp_statics, $max, $cur);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    $statics = count($dhcp_statics) - 1;
    $totalstatics = count($dhcp_statics);
    $first = explode(".", $item[netid]);
    $last = explode(".", $item[broadcast]);
    $limit = $last[3] - 1 - ($first[3] + 3) - $totalstatics;
    if ($statics == -1) {
        _outln_comment();
        _outln_comment(t('File /etc/config/luci_ethers'));
        _outln_nvram('dhcp_start', $max[3] + 2);
    }
    _outln_comment();
    _outln_comment(t('File /etc/config/luci_ethers'));
    print 'echo "';
    foreach ($dhcp_statics as $static) {
        print '<pre>
## Device: ' . $static[2] . '
config \'static_lease\'
        option \'macaddr\' \'' . $static[1] . '\'
        option \'ipaddr\' \'' . $static[0] . '\'
</pre>';
    }
    print '" > /etc/config/luci_ethers<br />';
    // FILE DHCP
    $file_dhcp = '
config \'dnsmasq\'
        option \'domainneeded\' \'1\'
        option \'boguspriv\' \'1\'
        option \'filterwin2k\' \'0\'
        option \'localise_queries\' \'1\'
        option \'local\' \'/lan/\'
        option \'domain\' \'lan\'
        option \'expandhosts\' \'1\'
        option \'nonegcache\' \'0\'
        option \'authoritative\' \'1\'
        option \'readethers\' \'1\'
        option \'leasefile\' \'/tmp/dhcp.leases\'
        option \'resolvfile\' \'/tmp/resolv.conf.auto\'

config \'dhcp\' \'lan\'
        option \'interface\' \'lan\'
        option \'leasetime\' \'12h\'
        option \'start\' \'' . ($max[3] + 2) . '\'
        option \'limit\' \'' . $limit . '\'
';
    _outln_comment();
    _outln_comment();
    _outln_comment(t('File /etc/config/dhcp'));
    _out_file($file_dhcp, '/etc/config/dhcp');
}
 function guifi_unsolclic_network_vars($dev, $zone)
 {
     _outln_comment($dev->nick);
     _outln_comment(t('Global network parameters'));
     _outln_nvram('router_name', $dev->nick);
     _outln_nvram('wan_hostname', $dev->nick);
     $wlan_lan = guifi_unsolclic_if($dev->id, 'wLan/Lan');
     if ($wlan_lan->ipv4 != '') {
         _outln_nvram('lan_ipaddr', $wlan_lan->ipv4);
         _outln_nvram('lan_gateway', '0.0.0.0');
         _outln_nvram('lan_netmask', $wlan_lan->netmask);
     }
     $lan = guifi_unsolclic_if($dev->id, 'Lan');
     if ($lan->ipv4 != '') {
         _outln_nvram('lan_ipaddr', $lan->ipv4);
         $item = _ipcalc($lan->ipv4, $lan->netmask);
         _outln_nvram('lan_gateway', $item['netstart']);
         _outln_nvram('lan_netmask', $lan->netmask);
     }
     $wan = guifi_unsolclic_if($dev->id, 'Wan');
     if ($wan) {
         if (empty($wan->ipv4)) {
             _outln_nvram('wan_proto', 'dhcp');
         } else {
             _outln_nvram('wan_proto', 'static');
             _outln_nvram('wan_ipaddr', $wan->ipv4);
             _outln_nvram('wan_netmask', $wan->netmask);
             if ($dev->variable['firmware'] == 'DD-WRTv23' or $dev->variable['firmware'] == 'DD-guifi') {
                 _outln_nvram('fullswitch', '1');
                 _outln_nvram('wan_dns', guifi_get_dns($zone, 3));
             }
         }
     } else {
         _outln_nvram('wan_proto', 'disabled');
     }
     _outln_nvram('lan_domain', 'guifi.net');
     _outln_nvram('wan_domain', 'guifi.net');
     _outln_nvram('http_passwd', 'guifi');
     _outln_nvram('time_zone', $zone->time_zone);
     _outln_nvram('sv_localdns', guifi_get_dns($zone, 1));
     if ($dev->variable['firmware'] == 'Alchemy') {
         _outln_nvram('wan_dns', guifi_get_dns($zone, 3));
     }
     if ($dev->variable['firmware'] == 'Talisman') {
         foreach (explode(' ', guifi_get_dns($zone, 3)) as $key => $dns) {
             _outln_nvram('wan_dns' . $key, $dns);
         }
     }
     _outln_nvram('wl_net_mode', 'b-only');
     _outln_nvram('wl0_net_mode', 'b-only');
     _outln_nvram('wl_afterburner', 'on');
     _outln_nvram('wl_frameburst', 'on');
     // Setting outpur power (mW)
     _outln_nvram('txpwr', '28');
     if (empty($dev->radios[0][antenna_mode])) {
         $dev->radios[0][antenna_mode] = 'Main';
     }
     if ($dev->radios[0][antenna_mode] != 'Main') {
         $dev->radios[0][antenna_mode] = '1';
     } else {
         $dev->radios[0][antenna_mode] = '0';
     }
     _outln_nvram('txant', $dev->radios[0][antenna_mode]);
     _outln_nvram('wl0_antdiv', '0');
     _outln_nvram('wl_antdiv', '0');
     _outln_nvram('block_wan', '0');
     if ($dev->variable['firmware'] == 'Talisman') {
         _outln_nvram('ident_pass', '0');
         _outln_nvram('multicast_pass', '0');
         _outln_nvram('wl_closed', '0');
         _outln_nvram('block_loopback', '0');
     }
     _outln_comment();
     _outln_comment(t('Management'));
     _outln_nvram('telnetd_enable', '1');
     _outln_nvram('sshd_enable', '1');
     _outln_nvram('sshd_passwd_auth', '1');
     _outln_nvram('remote_management', '1');
     _outln_nvram('remote_mgt_https', '1');
     _outln_nvram('snmpd_enable', '1');
     _outln_nvram('snmpd_sysname', 'guifi.net');
     _outln_nvram('snmpd_syscontact', 'guifi_at_guifi.net');
     _outln_nvram('boot_wait', 'on');
     _outln_comment(t('This is just a fake key. You must install a trusted key if you like to have you router managed externally'));
     _outln_nvram('sshd_authorized_keys', 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAwWNX4942fQExw4Hph2M/sxOAWVE9PB1I4JnNyhoWuF9vid0XcU34kwWqBBlI+LjDErCQyaR4ysFgDX61V4kUuCKwBOMp+UGxhL648VTv5Qji/YwvIzt7nguUOZ5AGPISqsC0717hc0Aja1mvHkQqg9aXKznmszmyKZGhcm2+SU8= root@bandoler.guifi.net');
     // For DD-WRTv23
     _outln_nvram('http_enable', '1');
     _outln_nvram('https_enable', '1');
     _outln_comment();
     _outln_comment('NTP Network time protocol');
     $ntp = guifi_get_ntp($zone, 1);
     if (empty($ntp)) {
         _outln_nvram('ntp_enable', '0');
     } else {
         _outln_nvram('ntp_enable', '1');
         _outln_nvram('ntp_server', $ntp);
     }
     _outln_comment();
     switch ($dev->radios[0][mode]) {
         case "ap":
         case "AP":
             _outln_comment(t('AP mode'));
             _outln_nvram('wl_mode', 'ap');
             _outln_nvram('wl0_mode', 'ap');
             _outln_nvram('wl_channel', $dev->radios[0][channel]);
             _outln_nvram('wl_ssid', 'guifi.net-' . guifi_to_7bits($dev->radios[0][ssid]));
             _outln_nvram('wl_macmode', 'disable');
             _outln_nvram('wl0_macmode', 'disable');
             _outln_nvram('wl_macmode1', 'disable');
             _outln_nvram('wl0_macmode1', 'disable');
             guifi_unsolclic_ospf($dev, $zone);
             guifi_unsolclic_dhcp($dev);
             guifi_unsolclic_wds_vars($dev);
             break;
         case 'client':
             _outln_comment(t('Client mode'));
             $ap_macs = array();
             foreach ($dev->radios[0]['interfaces'] as $interface_id => $interface) {
                 foreach ($interface[ipv4] as $ipv4_id => $ipv4) {
                     if (isset($ipv4[links])) {
                         foreach ($ipv4[links] as $key => $link) {
                             if ($link['link_type'] == 'ap/client') {
                                 $ap_macs[] = $link['interface']['mac'];
                                 $gateway = $link['interface']['ipv4']['ipv4'];
                                 if ($dev->variable['firmware'] == 'Alchemy' or $dev->variable['firmware'] == 'Talisman') {
                                     _outln_nvram('wl_mode', 'wet');
                                     _outln_nvram('wl0_mode', 'wet');
                                     _outln_nvram('wl_ssid', 'guifi.net-' . guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']));
                                 }
                                 if ($dev->variable['firmware'] == 'DD-WRTv23' or $dev->variable['firmware'] == 'DD-guifi') {
                                     _outln_nvram('wl_mode', 'sta');
                                     _outln_nvram('wl0_mode', 'sta');
                                     _outln_nvram('wl_ssid', 'guifi.net-' . guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']));
                                 }
                                 _outln_nvram('wan_gateway', $gateway);
                             }
                         }
                     }
                 }
             }
             if ($dev->variable['firmware'] == 'Alchemy') {
                 $filter = implode(" ", $ap_macs);
                 if ($filter == "") {
                     _outln_comment(t('WARNING: AP MAC not set'));
                     $filter = "FF:FF:FF:FF:FF:FF";
                 }
                 _outln_nvram('wl_macmode', 'allow');
                 _outln_nvram('wl0_macmode', 'allow');
                 _outln_nvram('wl_macmode1', 'other');
                 _outln_nvram('wl0_macmode1', 'other');
                 _outln_nvram('wl_maclist', $filter);
                 _outln_nvram('wl0_maclist', $filter);
                 _outln_nvram('wl_mac_list', $filter);
                 _outln_nvram('wl0_mac_list', $filter);
             } else {
                 _outln_nvram('wl_macmode', 'disabled');
                 _outln_nvram('wl0_macmode', 'disabled');
                 _outln_nvram('wl_macmode1', 'disabled');
                 _outln_nvram('wl0_macmode1', 'disabled');
             }
             $lan = guifi_unsolclic_if($dev->id, 'Lan');
             if ($lan) {
                 guifi_unsolclic_ospf($dev, $zone);
                 break;
             } else {
                 guifi_unsolclic_gateway($dev);
                 break;
             }
     }
     _outln_comment();
 }