function _ipcalc_by_netmask($ip, $hosts)
{
    return _ipcalc($ip, _netmask_by_hosts($hosts));
}
function guifi_ipcalc_find_ip($base_ip = '0.0.0.0', $mask_range = '0.0.0.0', $ips_allocated = NULL, $verbose = true)
{
    if ($ips_allocated == NULL) {
        $ips_allocated = guifi_ipcalc_get_ips($base_ip, $mask_range);
    }
    $ip_dec = ip2long($base_ip) + 1;
    $item = _ipcalc($base_ip, $mask_range);
    $end_dec = ip2long($item['broadcast']);
    $key = $ip_dec;
    while (isset($ips_allocated[$key]) and $key < $end_dec) {
        $key++;
    }
    if ($key < $end_dec) {
        return long2ip($key);
    }
    $ipc = _ipcalc($base_ip, $mask_range);
    if ($verbose) {
        drupal_set_message(t('Network %net/%mask is full', array('%net' => $base_ip, '%mask' => $ipc[maskbits])), 'warning');
    }
    return FALSE;
}
function guifi_device_links_print($device, $ltype = '%')
{
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_device_links_print(%s)', $ltype), $device);
    $oGC = new GeoCalc();
    $dtotal = 0;
    $ltotal = 0;
    if ($ltype == '%') {
        $title = t('network information');
    } else {
        $title = t('network information') . ' (' . $ltype . ')';
    }
    $rows_wds = array();
    $rows_ap_client = array();
    $rows_cable = array();
    $loc1 = db_fetch_object(db_query('SELECT lat, lon, nick ' . 'FROM {guifi_location} WHERE id=%d', $device['nid']));
    $curr_radio = 0;
    switch ($ltype) {
        case '%':
        case 'wds':
        case 'ap/client':
            $dname_curr = '';
            if ($device['radios']) {
                foreach ($device['radios'] as $radio_id => $radio) {
                    if ($radio['interfaces']) {
                        foreach ($radio['interfaces'] as $interface_id => $interface) {
                            if ($interface['ipv4']) {
                                foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
                                    if ($ipv4['links']) {
                                        foreach ($ipv4['links'] as $link_id => $link) {
                                            guifi_log(GUIFILOG_TRACE, 'going to list link', $link);
                                            $loc2 = db_fetch_object(db_query('SELECT lat, lon, nick FROM {guifi_location} WHERE id=%d', $link['nid']));
                                            $gDist = round($oGC->EllipsoidDistance($loc1->lat, $loc1->lon, $loc2->lat, $loc2->lon), 3);
                                            $dAz = round($oGC->GCAzimuth($loc1->lat, $loc1->lon, $loc2->lat, $loc2->lon));
                                            // Calculo orientacio
                                            if ($dAz < 23) {
                                                $dOr = t("N");
                                            } else {
                                                if ($dAz < 68) {
                                                    $dOr = t("NE");
                                                } else {
                                                    if ($dAz < 113) {
                                                        $dOr = t("E");
                                                    } else {
                                                        if ($dAz < 158) {
                                                            $dOr = t("SE");
                                                        } else {
                                                            if ($dAz < 203) {
                                                                $dOr = t("S");
                                                            } else {
                                                                if ($dAz < 248) {
                                                                    $dOr = t("SW");
                                                                } else {
                                                                    if ($dAz < 293) {
                                                                        $dOr = t("W");
                                                                    } else {
                                                                        if ($dAz < 338) {
                                                                            $dOr = t("NW");
                                                                        } else {
                                                                            $dOr = t("N");
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            $item = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
                                            $ipdest = explode('.', $link['interface']['ipv4']['ipv4']);
                                            $status_url = guifi_cnml_availability(array('device' => $link['device_id'], 'format' => 'short'));
                                            $cr = db_fetch_object(db_query("SELECT count(*) count FROM {guifi_radios} r WHERE id=%d", $link['device_id']));
                                            if ($cr->count > 1) {
                                                $rn = db_fetch_object(db_query("SELECT ssid FROM {guifi_radios} r WHERE r.id=%d AND r.radiodev_counter=%d", $link['device_id'], $link['interface']['radiodev_counter']));
                                                $dname = guifi_get_hostname($link['device_id']) . '<br />' . $rn->ssid;
                                            } else {
                                                $dname = guifi_get_hostname($link['device_id']);
                                            }
                                            $wrow = array($dname_curr != $radio['ssid'] ? array('data' => '<strong>' . $radio['ssid'] . '</strong>', 'header') : '<small>' . $radio['ssid'] . '</small>', array('data' => $link_id, 'align' => 'right'), '<a href="' . base_path() . 'guifi/device/' . $link['device_id'] . '">' . $dname . '</a>', '<a href="' . base_path() . 'node/' . $link['nid'] . '">' . $loc2->nick . '</a>', $ipv4['ipv4'] . '/' . $item['maskbits'], '.' . $ipdest[3], array('data' => t($link['flag']) . $status_url, 'class' => $link['flag']), $link[routing], $gDist, $dAz . '-' . $dOr);
                                            $dname_curr = $radio['ssid'];
                                            if ($interface['interface_type'] == 'wds/p2p' or $interface['interface_class'] == 'wds/p2p') {
                                                $rows_wds[] = $wrow;
                                            }
                                            if ($link['link_type'] == 'ap/client') {
                                                $rows_ap_client[] = $wrow;
                                            }
                                            $dtotal = $dtotal + $gDist;
                                            $ltotal++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if ($ltype != '%') {
                break;
            }
        case 'cable':
            $iname_curr = '';
            if ($device['interfaces']) {
                foreach ($device['interfaces'] as $interface_id => $interface) {
                    if ($interface['ipv4']) {
                        foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
                            if ($ipv4['links']) {
                                foreach ($ipv4['links'] as $link_id => $link) {
                                    $loc2 = db_fetch_object(db_query('SELECT lat, lon, nick FROM {guifi_location} WHERE id=%d', $link['nid']));
                                    $gDist = round($oGC->EllipsoidDistance($loc1->lat, $loc1->lon, $loc2->lat, $loc2->lon), 3);
                                    $item = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
                                    $ipdest = explode('.', $link['interface']['ipv4']['ipv4']);
                                    if ($gs->var['url'] != NULL) {
                                        $img_url = ' <img src=' . $gs->var['url'] . '?device=' . $link['device_id'] . '&type=availability&format=short>';
                                    } else {
                                        $img_url = NULL;
                                    }
                                    $rows_cable[] = array($iname_curr != $interface['interface_type'] ? $interface['interface_type'] : null, array('data' => $link_id, 'align' => 'right'), l(guifi_get_hostname($link['device_id']), 'guifi/device/' . $link['device_id']), $link['interface']['interface_type'], $ipv4['ipv4'] . '/' . $item['maskbits'], '.' . $ipdest[3], array('data' => t($link['flag']) . $img_url, 'class' => $link['flag']), $link[routing]);
                                    $ltotal++;
                                    $iname_curr = $interface['interface_type'];
                                }
                            } else {
                                $rows_cable[] = array($iname_curr != $interface['interface_type'] ? $interface['interface_type'] : null, null, null, null, $ipv4['ipv4'] . '/' . $item['maskbits']);
                                $ltotal++;
                                $iname_curr = $interface['interface_type'];
                            }
                        }
                    }
                    // foreach link
                    $iname_curr = $interface['interface_type'];
                }
            }
            // foreach ipv4
            if ($ltype == 'cable') {
                break;
            }
    }
    $header_cable = array(t('interface'), array('data' => t('id'), 'align' => 'right'), t('device'), t('remote<br>interface'), t('ip address'), '&nbsp;', t('status'), t('routing'));
    $header_wireless = array_merge($header_cable, array(t('kms.'), t('az.')));
    $header_wireless[3] = t('node');
    $output = '';
    $attr = array('class' => 'list-links');
    if ($rows_ap_client) {
        $output .= theme_box('<hr>' . t('ap/client'), theme_table($header_wireless, $rows_ap_client, $attr));
    }
    if ($rows_wds) {
        $output .= theme_box('<hr>' . t('wds/p2p'), theme_table($header_wireless, $rows_wds, $attr));
    }
    if ($rows_cable) {
        $output .= theme_box('<hr>' . t('cable'), theme_table($header_cable, $rows_cable, $attr));
    }
    if ($output) {
        return '<h2>' . $title . '</h2>' . '<h3>' . t('Totals') . ': ' . $ltotal . ' ' . t('links') . ', ' . $dtotal . ' ' . t('kms.') . '</h3><br>' . $output;
    } else {
        return;
    }
}
Example #4
0
function guifi_links_check_overlap($overlap, &$form_state)
{
    if ($form_state['clicked_button']['#value'] == t('Reset')) {
        return;
    }
    $keys = count($overlap['#parents']);
    $radio_id = $overlap['#parents'][$keys - 6];
    $interface_id = $overlap['#parents'][$keys - 4];
    $ipv4_id = $overlap['#parents'][$keys - 2];
    if ($keys == 7) {
        $ipv4 =& $form_state['values']['radios'][$radio_id]['interfaces'][$interface_id]['ipv4'][$ipv4_id];
    } else {
        $ipv4 =& $form_state['values']['interfaces'][$interface_id]['ipv4'][$ipv4_id];
    }
    if ($ipv4['links'][$link_id]['deleted']) {
        return;
    }
    if (ip2long($ipv4['netmask']) >= ip2long($ipv4['overlap'])) {
        return;
    }
    $net = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
    $net_overlap = _ipcalc($ipv4['ipv4'], $ipv4['overlap']);
    $old_netid = $net_overlap['netid'];
    $new_netid = $net['netid'];
    $old_netmask = $ipv4['overlap'];
    $new_netmask = $ipv4['netmask'];
    $new_broadcast = $net['broadcast'];
    $old_broadcast = $net_overlap['broadcast'];
    //guifi_log(GUIFILOG_BASIC,'<br />Old Netid: '.$old_netid.'<br />New Netid: '.$new_netid.'<br />Old Netmask: '.$old_netmask.'<br />New NetMask: '.$new_netmask.'<br />Old broadcast: '.$old_broadcast.'<br />New Broadcast: '.$new_broadcast.'<br /><br /> ');
    $sql = db_query("SELECT INET_ATON(ipv4) as ip FROM guifi_ipv4 WHERE ipv4 BETWEEN '%s' AND '%s' ", $old_broadcast, $new_broadcast);
    while ($item = db_fetch_array($sql)) {
        $ip = long2ip($item['ip']);
        $s = ip2long($old_netid);
        $e = ip2long($new_broadcast);
        for ($i = $s; $i < $e + 1; $i++) {
            $ipnow = long2ip($i);
            if ($ip == $ipnow) {
                drupal_set_message(t('Ip address: %ip is already taken on another device!!', array('%ip' => $ip)), 'error');
                $error = TRUE;
                //guifi_log(GUIFILOG_BASIC,'Tipus de check: Broadcast<br />');
            }
        }
        $count = $e - $s + 1;
    }
    $sql = db_query("SELECT INET_ATON(ipv4) as ip FROM guifi_ipv4 WHERE ipv4 BETWEEN '%s' AND '%s' ", $new_netid, $old_netid);
    while ($item = db_fetch_array($sql)) {
        $ip = long2ip($item['ip']);
        $s = ip2long($new_netid);
        $e = ip2long($old_netid);
        for ($i = $s; $i < $e + 1; $i++) {
            $ipnow = long2ip($i);
            if ($ip == $ipnow) {
                drupal_set_message(t('Ip address: %ip is already taken on another device!!', array('%ip' => $ip)), 'error');
                $error = TRUE;
                //guifi_log(GUIFILOG_BASIC,'Tipus de check: Netid<br>');
            }
        }
        $count = $e - $s + 1;
    }
    if ($error == TRUE) {
        form_error($overlap, t('Error! Your new netmask: /%bit ( %mask ) is overlapping another existing subnet, you can\'t expand it!<br />' . 'Then, We will find a range of network in your area with the size needed, just for information. You can use it if it thinks fit.', array('%mask' => $new_netmask, '%bit' => $net['maskbits'])));
        $nid = $form_state['values']['nid'];
        $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', array(), 2);
        guifi_ipcalc_get_subnet_by_nid($nid, $new_netmask, 'backbone', $ips_allocated, 'Yes', TRUE);
    }
}
Example #5
0
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');
}
Example #6
0
/**
 * Search for an unused subrange of network in the database.
 *
 * You can specify a network_zone where to search a subrange. In case such subrange is too big or doesn't exist,
 * it will try to find it in the parent network zone and so, recursively.
 *
 * @param $params
 *   String with values separated by commas: $mask, $network_type, $zone_id and $allocate.
 *
 * @return
 *   theme()
 */
function guifi_tools_ip_rangesearch($params = NULL)
{
    $output .= drupal_get_form('guifi_tools_ip_rangesearch_form', $params);
    if (empty($params)) {
        return $output;
    }
    // for testing, load a device with quite a few ip's'
    // $device = guifi_device_load(115);
    $tgetipsbegin = microtime(TRUE);
    $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0');
    $tgetipsend = microtime(TRUE);
    $toutput = t('Got & sorted %num ips in %secs seconds', array('%num' => number_format(count($ips_allocated)), '%secs' => number_format($tgetipsend - $tgetipsbegin, 4))) . '<br />';
    list($mask, $network_type, $zone_id, $allocate) = explode(',', $params);
    if (!user_access('administer guifi networks')) {
        $allocate = 'No';
    }
    $net = guifi_ipcalc_get_subnet_by_nid($zone_id, $mask, $network_type, $ips_allocated, $allocate, TRUE);
    // verbose output
    $tgetsubnetbynid = microtime(TRUE);
    $toutput .= t('Got %base/%net in %secs seconds', array('%base' => $net, '%net' => $mask, '%secs' => number_format($tgetsubnetbynid - $tgetipsend, 4))) . '<br />';
    $toutput .= t('Total elapsed was %secs seconds', array('%secs' => number_format($tgetsubnetbynid - $tgetipsbegin, 4))) . '<br />';
    $item = _ipcalc($net, $mask);
    if ($net) {
        foreach ($item as $k => $value) {
            $header[] = t($k);
            $row[] = $value;
        }
        $qoutput .= theme('box', t('Space found at %net', array('%net' => $net)), theme('table', $header, array($row)));
    } else {
        drupal_set_message(t('Was not possible to find %type space for %mask', array('%type' => $network_type, '%mask' => $mask)), 'error');
    }
    return $qoutput . theme('box', t('Find available space for a subnetwork'), $output) . theme('box', t('Performance'), '<small>' . $toutput . '</small>');
}
Example #7
0
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>';
}
Example #8
0
function guifi_ipv4subnet_form($ipv4, $k, $view = false)
{
    define('MAXNIPS', 12);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ipv4subnet_form (id=%d)', $k), $ipv4);
    $ipc = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
    if (!empty($ipv4[subnet])) {
        $subnet = $ipv4[subnet];
    } else {
        $subnet = $ipv4[snet];
    }
    guifi_log(GUIFILOG_FULL, sprintf('guifi_ipv4subnet_form (id=%d)', $k), $subnet);
    $ips = array(ip2long($ipv4[ipv4]));
    foreach ($subnet as $ki => $ip) {
        if (empty($ip[ipv4]) and !empty($ip[ipv4value])) {
            $subnet[$ki][ipv4] = $ip[ipv4value];
        }
        if (ip2long($ip[ipv4])) {
            $ips[] = ip2long($ip[ipv4]);
        }
    }
    sort($ips);
    $form = array('#type' => !$view ? 'hidden' : 'fieldset', '#type' => 'fieldset', '#title' => t('Subnet') . ' ' . $ipc['netid'] . '/' . $ipc['maskbits'] . ' - ' . count($ips) . ' address(es)', '#prefix' => '<div class="ipv4-subnet" id="fieldset-ipv4subnet-' . $k . '">', '#suffix' => '</div>', '#collapsible' => true, '#collapsed' => $view ? false : true);
    //  foreach ($subnet as $ks => $snet) {
    $nIps = 0;
    $newIps = 0;
    for ($ks = 0; $ks < $ipc['hosts']; $ks++) {
        $snet = $subnet[$ks];
        $form[$ks] = array('#type' => !empty($snet[ipv4]) ? 'fieldset' : 'hidden', '#attributes' => array('class' => 'fieldset-interface-port'), '#prefix' => '<div id="fieldset-ipv4subnet-' . $k . '-' . $ks . '">', '#suffix' => '</div>');
        $form[$ks][ipv4value] = array('#type' => 'hidden', '#value' => $snet[ipv4]);
        if (isset($snet['id'])) {
            $form[$ks]['id'] = array('#type' => 'hidden', '#value' => $snet['id']);
        }
        if (isset($snet[interface_id])) {
            $form[$ks][interface_id] = array('#type' => 'hidden', '#value' => $snet[interface_id]);
        }
        $form[$ks][ipv4] = array('#type' => 'textfield', '#default_value' => $snet[ipv4], '#size' => 24, '#maxlength' => 16);
        if ($snet['new']) {
            $form[$ks][ipv4]['#type'] = 'hidden';
            $form[$ks][ipv4]['#value'] = $snet[ipv4];
            $form[$ks][ipv4txt] = array('#type' => 'textfield', '#value' => $snet[ipv4], '#disabled' => true, '#size' => 24);
            $newIps++;
            $form[$ks]['new'] = array('#type' => 'hidden', '#value' => true);
        }
        $form[$ks][did] = array('#type' => ($view and !empty($snet[ipv4])) ? 'textfield' : 'hidden', '#type' => 'textfield', '#default_value' => guifi_get_devicename($snet['did'], 'large'), '#autocomplete_path' => 'guifi/js/select-node-device', '#size' => 60, '#maxlength' => 128, '#element_validate' => array('guifi_devicename_validate'), '#ahah' => array('event' => 'blur', 'path' => 'guifi/js/select-device-interfacename/' . $k . '-' . $ks, 'wrapper' => 'fieldset-ipv4subnet-' . $k . '-' . $ks, 'method' => 'replace', 'effect' => 'fade'));
        //    if (!empty($snet['ipv4'])) {
        $dinterfaces = guifi_get_device_allinterfaces($snet['did']);
        $form[$ks]['iid'] = array('#type' => 'select', '#value' => $snet['iid'], '#required' => true, '#options' => $dinterfaces);
        //    }
        if (!($snet['deleted'] == true)) {
            $form[$ks]['deleted'] = array('#type' => 'image_button', '#src' => drupal_get_path('module', 'guifi') . '/icons/drop.png', '#attributes' => array('title' => t('Delete address')), '#ahah' => array('path' => 'guifi/js/delete-ipv4/' . $k . '-' . $ks, 'wrapper' => 'fieldset-ipv4subnet-' . $k . '-' . $ks, 'method' => 'replace', 'effect' => 'fade'));
        } else {
            $form[$ks][deleted] = array('#type' => 'hidden', '#value' => true);
            $form[$ks][ipv4]['#type'] = 'hidden';
            $form[$ks][ipv4]['#value'] = $snet[ipv4];
            $form[$ks][did]['#type'] = 'hidden';
            $form[$ks][iid]['#type'] = 'hidden';
            $form[$ks][deletedmsg] = array('#type' => 'item', '#value' => t('Address %addr will be DELETED', array('%addr' => $snet[ipv4])));
        }
        // force save if new addresses at the in-memory form is too high
        if (empty($snet['ipv4'])) {
            $nIps++;
        }
        if ($nIps >= MAXNIPS) {
            break;
        }
    }
    if (count($ips) < $ipc['hosts']) {
        if ($newIps < MAXNIPS) {
            $form['add-ipv4'] = array('#type' => 'image_button', '#title' => $first_port ? t('Add') : false, '#src' => drupal_get_path('module', 'guifi') . '/icons/ipv4-new.png', '#attributes' => array('title' => t('Add new ipv4 address to the subnetwork')), '#weight' => 100, '#ahah' => array('path' => 'guifi/js/add-remoteipv4/' . $k, 'wrapper' => 'fieldset-ipv4subnet-' . $k, 'method' => 'replace', 'effect' => 'fade'));
        } else {
            $form['add-ipv4'] = array('#type' => 'item', '#title' => $first_port ? t('Add') : false, '#value' => t('To many new addresses added. Is not safe to add more values, please confirm or discard changes'), '#weight' => 100);
        }
    }
    return $form;
}
Example #9
0
/** _guifi_db_delete(): Delete SQL statements for node, devices, radios, users, services, interfaces, ipv4, links, zones...
***/
function _guifi_db_delete($table, $key, &$to_mail = array(), $depth = 0, $cascade = TRUE)
{
    global $user;
    $log = str_repeat('- ', $depth);
    $depth++;
    $to_mail = array();
    guifi_log(GUIFILOG_TRACE, sprintf('function _guifi_db_delete(%s,%s)', $table, var_export($key, TRUE)));
    if (!in_array($user->mail, $to_mail)) {
        $to_mail[] = $user->mail;
    }
    switch ($table) {
        // Node (location)
        case 'guifi_location':
            // cascade to node devices
            $qc = db_query("SELECT id FROM {guifi_devices} where nid = '%s'", $key['id']);
            while ($device = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_devices', $device, $to_mail, $depth);
            }
            // cascade to node users
            $qc = db_query("SELECT id FROM {guifi_users} where nid = '%s'", $key['id']);
            while ($quser = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_users', $quser, $to_mail, $depth);
            }
            // delete Device
            // cascade to node maintainers
            $qc = db_query("SELECT id FROM {guifi_maintainers} where subject_id = '%s' and subject_type='location'", $key['id']);
            while ($quser = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_maintainers', $quser, $to_mail, $depth);
            }
            break;
        case 'guifi_dns_domains':
            $item = db_fetch_object(db_query('SELECT d.id did, d.name dname, d.notification, d.sid,
        l.nick nname, l.notification ncontact
       FROM {guifi_dns_domains} d LEFT JOIN {guifi_services} l ON d.sid=l.id
       WHERE d.id = %d', $key['id']));
            $log .= t('Domain %id-%name at node %nname deleted.', array('%id' => $key['id'], '%name' => $item->dname, '%nname' => $item->nname));
            // cascade to dns_hosts
            $qc = db_query('SELECT id, counter FROM {guifi_dns_hosts} WHERE id=%d', $key['id']);
            while ($host = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_dns_hosts', $host, $to_mail, $depth);
            }
            break;
        case 'guifi_devices':
            $item = db_fetch_object(db_query('SELECT d.nick dname, d.notification, d.nid, d.type, d.comment,
        l.nick nname, l.notification ncontact
       FROM {guifi_devices} d LEFT JOIN {guifi_location} l ON d.nid=l.id
       WHERE d.id = %d', $key['id']));
            $log .= t('Device (%type) %id-%name at node %nname deleted.', array('%type' => $item->type, '%id' => $key['id'], '%name' => $item->dname, '%nname' => $item->nname));
            // cascade to device radios
            $qc = db_query('SELECT id, radiodev_counter FROM {guifi_radios} WHERE id=%d', $key['id']);
            while ($radio = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_radios', $radio, $to_mail, $depth);
            }
            // cascade to device interfaces
            $qc = db_query('SELECT id FROM {guifi_interfaces} WHERE device_id=%d', $key['id']);
            while ($interface = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_interfaces', $interface, $to_mail, $depth);
            }
            // cascade to node maintainers
            $qc = db_query("SELECT id FROM {guifi_maintainers} where subject_id = '%s' and subject_type='device'", $key['id']);
            while ($quser = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_maintainers', $quser, $to_mail, $depth);
            }
            break;
            // delete Radio
        // delete Radio
        case 'guifi_radios':
            $item = db_fetch_object(db_query('SELECT
          r.protocol, r.ssid sid, r.mode, r.radiodev_counter,
          d.nick dname, d.notification, d.nid, l.nick nname
        FROM {guifi_radios} r, {guifi_devices} d, {guifi_location} l
        WHERE  r.id = %d AND r.radiodev_counter = %d AND
          r.id=d.id AND d.nid=l.id', $key['id']), $key['radiodev_counter']);
            $log .= t('Radio (%mode-%protocol) %id-%rc %ssid at device %dname deleted.', array('%mode' => $item->mode, '%protocol' => $item->protocol, '%id' => $key['id'], '%rc' => $key['radiodev_counter'], '%ssid' => $item->sid, '%dname' => $item->dname));
            // cascade to radio interfaces
            $qc = db_query('SELECT id, radiodev_counter FROM {guifi_interfaces} WHERE device_id=%d AND radiodev_counter=%d', $key['id'], $key['radiodev_counter']);
            while ($interface = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_interfaces', $interface, $to_mail, $depth);
            }
            break;
            // delete Interfaces
        // delete Interfaces
        case 'guifi_interfaces':
            $item = db_fetch_object(db_query('SELECT i.interface_type, i.radiodev_counter, i.connto_did, i.connto_iid,
               d.nick dname,
               d.notification, d.nid, l.nick nname
        FROM {guifi_interfaces} i LEFT JOIN {guifi_devices} d ON i.device_id=d.id
             LEFT JOIN {guifi_location} l ON d.nid=l.id
        WHERE i.id = %d', $key['id']));
            $log .= t('interface (%type) %id - %rc at device %dname deleted.', array('%type' => $item->interface_type, '%id' => $key['id'], '%rc' => $item->radiodev_counter, '%dname' => $item->dname));
            // cascade ipv4
            $qc = db_query('SELECT id, interface_id FROM {guifi_ipv4} WHERE interface_id=%d', $key['id']);
            while ($ipv4 = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_ipv4', $ipv4, $to_mail, $depth);
            }
            // cascade remote interface plug
            if ($item->connto_did and $item->connto_iid) {
                $if_remote = array('device_id' => $item->connto_did, 'id' => $item->connto_iid, 'connto_did' => '', 'connto_iid' => '');
                _guifi_db_sql('guifi_interfaces', array('device_id' => $item->connto_did, 'id' => $item->connto_iid), $if_remote, $log, $to_mail);
            }
            break;
            // delete ipv4
        // delete ipv4
        case 'guifi_ipv4':
            $item = db_fetch_object(db_query('SELECT a.id, a.interface_id, a.ipv4, i.interface_type, d.nick dname, d.notification, d.nid, l.nick nname
        FROM {guifi_ipv4} a LEFT JOIN {guifi_interfaces} i ON a.interface_id=i.id LEFT JOIN {guifi_devices} d ON i.device_id=d.id LEFT JOIN {guifi_location} l ON d.nid=l.id
        WHERE a.id = %d AND a.interface_id=%d', $key['id'], $key['interface_id']));
            $log .= t('address (%addr) at device %dname deleted.', array('%addr' => $item->ipv4, '%dname' => $item->dname));
            if (!$cascade) {
                break;
            }
            // cascade links
            $qc = db_query('SELECT id, device_id FROM {guifi_links} WHERE ipv4_id=%d AND interface_id=%d', $key['id'], $key['interface_id']);
            while ($link = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_links', $link, $to_mail, $depth);
            }
            break;
            // delete links
        // delete links
        case 'guifi_links':
            $item = db_fetch_object(db_query('SELECT l.id, l.link_type, l.ipv4_id, i.id interface_id, ' . '       d.nick dname, d.id device_id, d.notification, d.nid, n.nick nname
        FROM {guifi_links} l ' . '    LEFT JOIN {guifi_interfaces} i ON l.interface_id=i.id ' . '    LEFT JOIN {guifi_devices} d ON l.device_id=d.id ' . '    LEFT JOIN {guifi_location} n ON l.nid=n.id
        WHERE l.id = %d' . '    AND l.device_id=%d', $key['id'], $key['device_id']));
            $log .= t('link %id-%did (%type) at %nname-%dname deleted.', array('%id' => $key['id'], '%did' => $key['device_id'], '%type' => $item->link_type, '%nname' => $item->nname, '%dname' => $item->dname));
            if (!$cascade) {
                break;
            }
            // cascade to remote link
            $qc = db_query('SELECT id, ipv4_id, interface_id, device_id ' . 'FROM {guifi_links} ' . 'WHERE id=%d ' . '  AND device_id !=%d', $key['id'], $key['device_id']);
            while ($link = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('guifi_links', $link, $to_mail, $depth, FALSE);
                // cleanup of remote ipv4 addresses when appropriate
                $qar = db_query('SELECT * ' . 'FROM {guifi_ipv4} ' . 'WHERE id=%d AND interface_id=%d', $link['ipv4_id'], $link['interface_id']);
                while ($ripv4 = db_fetch_array($qar)) {
                    $aitem = _ipcalc($ripv4['ipv4'], $ripv4['netmask']);
                    if ($ripv4['ipv4_type'] == '2') {
                        // type 2: link is backbone
                        // if the addres is a:
                        // /30 (single p2p link)
                        // or /29 and /28 ( Multilink )
                        if ($ripv4['netmask'] == '255.255.255.252' or $ripv4['netmask'] == '255.255.255.248' or $ripv4['netmask'] == '255.255.255.240') {
                            $log .= '<br />' . _guifi_db_delete('guifi_ipv4', array('id' => $link['ipv4_id'], 'interface_id' => $link['interface_id']), $to_mail, $depth, FALSE);
                            // cascade to local ipv4
                            $log .= '<br />' . _guifi_db_delete('guifi_ipv4', array('id' => $item->ipv4_id, 'interface_id' => $item->interface_id), $to_mail, $depth, FALSE);
                        }
                    } else {
                        $mlinks = db_fetch_array(db_query('SELECT count(id) AS links ' . 'FROM {guifi_links} ' . 'WHERE ipv4_id=%d AND interface_id=%d', $link['ipv4_id'], $link['interface_id']));
                        if ($ripv4['ipv4'] != $aitem['netstart'] and $mlinks['links'] < 1) {
                            $log .= '<br />' . _guifi_db_delete('guifi_ipv4', array('id' => $link['ipv4_id'], 'interface_id' => $link['interface_id']), $to_mail, $depth, FALSE);
                        }
                    }
                    // guifi_log(GUIFILOG_BASIC,'function delete cascade remote address()',$link);
                    // cleanup remote interface when appropriate
                    $qir = db_query('SELECT i.id id, i.interface_type, count(a.id) na ' . 'FROM {guifi_interfaces} i ' . '  LEFT OUTER JOIN {guifi_ipv4} a ' . '  ON i.id=a.interface_id ' . 'WHERE i.id=%d ' . 'GROUP BY i.id, i.interface_type', $link['interface_id']);
                    while ($na = db_fetch_array($qir)) {
                        //          guifi_log(GUIFILOG_BASIC,'function delete cascade remote interface()',$na);
                        // delete the interface, if has no other ipv4 address and is not
                        // in the list Wan, wLan/Lan, Lan/Lan, Lan or wds/p2p
                        if (in_array($na['interface_type'], array('Wan', 'wLan/Lan', 'Lan/Lan', 'Lan', 'wds/p2p')) or $na['na'] != 0) {
                            continue;
                        }
                        $log .= '<br />' . _guifi_db_delete('guifi_interfaces', array('id' => $na['id']), $to_mail, $depth, FALSE);
                    }
                }
            }
            // delete services
        // delete services
        case 'guifi_services':
            // cascade interfaces
            break;
            // delete users
        // delete users
        case 'guifi_users':
            $item = db_fetch_object(db_query('SELECT * ' . 'FROM {guifi_users} u ' . 'WHERE id = %d', $key['id']));
            $log .= t('User %id-%name deleted.', array('%id' => $key['id'], '%name' => $item->username));
            break;
        case 'guifi_zone':
            break;
        case 'budgets':
            if (!$cascade) {
                break;
            }
            $qc = db_query('SELECT id, budget_id ' . 'FROM {budget_items} ' . 'WHERE budget_id=%d', $key['id']);
            while ($item = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('budget_items', $item, $to_mail, $depth);
            }
            $qc = db_query('SELECT id, budget_id ' . 'FROM {budget_funds} ' . 'WHERE budget_id=%d', $key['id']);
            while ($fund = db_fetch_array($qc)) {
                $log .= '<br />' . _guifi_db_delete('budget_funds', $fund, $to_mail, $depth);
            }
            break;
    }
    $where_str = '';
    foreach ($key as $k => $value) {
        if ($where_str != '') {
            $where_str .= ' AND ';
        }
        if ($table == 'guifi_types') {
            $where_str .= $k . ' = \'' . $value . '\'';
        } else {
            $where_str .= $k . ' = ' . $value;
        }
    }
    $count = db_fetch_array(db_query("\n    SELECT count(*) c\n    FROM {" . $table . "}\n    WHERE " . $where_str));
    if ($count['c'] != 1) {
        return $log . '<br />' . t('There was nothing to delete at %table with (%where)', array('%table' => $table, '%where' => $where_str));
    }
    if (!in_array($item->notification, $to_mail)) {
        $to_mail[] = $item->notification;
    }
    if (!in_array($item->ncontact, $to_mail)) {
        $to_mail[] = $item->ncontact;
    }
    $where_str = '';
    foreach ($key as $k => $value) {
        if ($where_str != '') {
            $where_str .= ' AND ';
        }
        if ($table == 'guifi_types') {
            $where_str .= $k . ' = \'' . $value . '\'';
        } else {
            $where_str .= $k . ' = ' . $value;
        }
    }
    $delete_str = 'DELETE FROM {' . $table . '} WHERE ' . $where_str;
    $log .= '<br />' . $delete_str;
    guifi_log(GUIFILOG_TRACE, $delete_str);
    db_query($delete_str);
    return $log;
}
Example #10
0
function guifi_api_cloudy_addlink($gapi, $parameters)
{
    if (!guifi_api_check_fields($gapi, array('device_id', 'cloudy_id'), $parameters)) {
        return FALSE;
    }
    /* User allow changes ? */
    $device = guifi_device_load($parameters['device_id']);
    if (!guifi_device_access('update', $device)) {
        $gapi->addError(501);
        return FALSE;
    }
    /* Exist ? */
    if (!$device['id']) {
        $gapi->addError(500, "device not found: {$parameters['device_id']}");
        return FALSE;
    }
    /* User allow changes ? */
    $cloudy = guifi_device_load($parameters['cloudy_id']);
    if (!guifi_device_access('update', $cloudy)) {
        $gapi->addError(501);
        return FALSE;
    }
    /* Exist ? */
    if (!$cloudy['id']) {
        $gapi->addError(500, "device not found: {$parameters['cloudy_id']}");
        return FALSE;
    }
    $ipv4_id = 0;
    /* Si no hi ha interface sel·leccionada busquem una que tingui IPv4 */
    foreach ($device['interfaces'] as $iid => $if) {
        if (is_array($if['ipv4'])) {
            break;
        }
    }
    if (!is_array($if['ipv4'])) {
        $gapi->addError(500, "Device did not assign IPv4.");
    }
    /* Calcular IP */
    /* Coses que no m'agraden a. El 0 posat a ipv4, s'hauria de calcular. b. interface_type, tb sembla que es contabilitza */
    $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $device, 1);
    $base_ip = $device['interfaces'][$iid]['ipv4'][$ipv4_id];
    $item = _ipcalc($base_ip['ipv4'], $base_ip['netmask']);
    $ip = guifi_ipcalc_find_ip($item['netid'], $base_ip['netmask'], $ips_allocated);
    /* Creem l'interficie al Cloudy. És totalment necessari? */
    $newLink = array("new" => TRUE, "interface" => array("new" => TRUE, "device_id" => $cloudy['id'], "interface_type" => "Lan", "ipv4" => array("new" => TRUE, "ipv4_type" => 1, "ipv4" => $ip, "netmask" => $base_ip['netmask'])), "id" => -1, "link_type" => "cable", "flag" => "Planned", "nid" => $cloudy['nid'], "device_id" => $cloudy['id'], "routing" => "Gateway");
    $device['interfaces'][$iid]['ipv4'][$ipv4_id]['links'][] = $newLink;
    $device['interfaces'][$iid]['unfold'] = TRUE;
    guifi_device_save($device);
    $gapi->addResponseField('device', $device);
    return TRUE;
}
function unsolclic_routeros($dev)
{
    //Fixed testing mode
    $ospf_id = '0.0.0.0';
    $ospf_name = 'backbone';
    //
    $ipd = guifi_main_ip($dev->id);
    $ospf_routerid = $ipd['ipv4'];
    $defined_ips = array();
    function bgp_peer($id, $ipv4, $disabled)
    {
        $peername = guifi_get_hostname($id);
        _outln('/ routing bgp peer');
        _outln(sprintf(':foreach i in [find name=%s] do={/routing bgp peer remove $i;}', $peername));
        _outln(sprintf('add name="%s" instance=default remote-address=%s remote-as=%s \\ ', $peername, $ipv4, $id));
        _outln(sprintf('multihop=no route-reflect=no ttl=default in-filter=ebgp-in out-filter=ebgp-out disabled=%s', $disabled));
    }
    function ospf_interface($iname, $netid, $maskbits, $ospf_name, $ospf_zone, $ospf_id, $disabled)
    {
        _outln('/ routing ospf interface');
        _outln(sprintf(':foreach i in [/routing ospf interface find interface=%s] do={/routing ospf interface remove $i;}', $iname));
        _outln(sprintf('add interface=%s', $iname));
        _outln('/ routing ospf network');
        _outln(sprintf(':foreach i in [/routing ospf network find network=%s/%d] do={/routing ospf network remove $i;}', $netid, $maskbits));
        _outln(sprintf('add network=%s/%d area=%s disabled=%s', $netid, $maskbits, $ospf_name, $disabled));
    }
    //  Check if there's any wLan/Lan interface defined on the device
    $wlanlan = false;
    foreach ($dev->radios as $ri) {
        $ii = $ri['interfaces'];
        foreach ($ii as $iii) {
            if ($iii['interface_type'] == 'wLan/Lan') {
                $wlanlan = true;
            }
        }
    }
    $node = node_load(array('nid' => $dev->nid));
    $zone = node_load(array('nid' => $node->zone_id));
    _outln(sprintf(':log info "Unsolclic for %d-%s going to be executed."', $dev->id, $dev->nick));
    _outln_comment();
    if ($dev->variable['firmware'] == 'RouterOSv4.7+') {
        _outln_comment(t('Configuration for RouterOS 4.7 and newer 4.x'));
    } else {
        _outln_comment(t('Configuration for ' . $dev->variable['firmware']));
    }
    _outln_comment(t('Device') . ': ' . $dev->id . '-' . $dev->nick);
    _outln_comment();
    _outln_comment(t('Methods to upload/execute this script:'));
    _outln_comment(t('1.-As a script. Upload this output as a script either with:'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('a.Winbox (with Linux, wine required)'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('b.Terminal (telnet, ssh...)'));
    _outln_comment('&nbsp;&nbsp;&nbsp;' . t('Then execute the script with:'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . t('>&nbsp;/system script run script_name'));
    _outln_comment(t('2.-Imported file:'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('Save this output to a file, then upload it to the router'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('using ftp using a name like "script_name.rsc".'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('(note that extension ".rsc" is required)'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('Run the import file using the command:'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . t('>&nbsp;/import script_name'));
    _outln_comment(t('3.-Telnet cut&paste:'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('Open a terminal session, and cut&paste this output'));
    _outln_comment('&nbsp;&nbsp;&nbsp;&nbsp;' . t('directly on the terminal input.'));
    _outln_comment();
    _outln_comment(t('Notes:'));
    _outln_comment(t('-routing-test package is required if you use RouterOSv2.9 , be sure you have it enabled at system packages'));
    _outln_comment(t('-wlans should be enabled manually, be sure to set the correct antenna (a or b)'));
    _outln_comment(t('&nbsp;&nbsp;according in how did you connect the cable to the miniPCI. Keep the'));
    _outln_comment(t('&nbsp;&nbsp;power at the minimum possible and check the channel.'));
    _outln_comment(t('-The script doesn\'t reset the router, you might have to do it manually'));
    _outln_comment(t('-You must have write access to the router'));
    _outln_comment(t('-MAC access (winbox, MAC telnet...) method is recommended'));
    _outln_comment(t('&nbsp;&nbsp;(the script reconfigures some IP addresses, so communication can be lost)'));
    _outln_comment(t('-No changes are done in user passwords on the device'));
    _outln_comment(t('-A Read Only guest account with no password will be created to allow guest access'));
    _outln_comment(t('&nbsp;&nbsp;to the router with no danger of damage but able to see the config.'));
    _outln_comment(t('-Be sure that all packages are activated.'));
    _outln_comment(t('-Don\'t run the script from telnet and being connected through an IP connection at'));
    _outln_comment(t('&nbsp;&nbsp;the wLan/Lan interface: This interface will be destroyed during the script.'));
    _outln_comment();
    _outln('/ system identity set name=' . $dev->nick);
    // DNS
    _outln_comment();
    _outln_comment('DNS (client &#038; server cache) zone: ' . $node->zone_id);
    list($primary_dns, $secondary_dns) = explode(' ', guifi_get_dns($zone, 2));
    $dns[] .= $primary_dns;
    $dns[] .= $secondary_dns;
    if ($secondary_dns != null) {
        if ($dev->variable['firmware'] == 'RouterOSv4.7+' or $dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
            _outln(sprintf('/ip dns set servers=%s,%s allow-remote-requests=yes', $primary_dns, $secondary_dns));
        } else {
            _outln(sprintf('/ip dns set primary-dns=%s secondary-dns=%s allow-remote-requests=yes', $primary_dns, $secondary_dns));
        }
    } else {
        if ($primary_dns != null) {
            if ($dev->variable['firmware'] == 'RouterOSv4.7+' or $dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
                _outln(sprintf('/ip dns set servers=%s allow-remote-requests=yes', $primary_dns));
            } else {
                _outln(sprintf('/ip dns set primary-dns=%s allow-remote-requests=yes', $primary_dns));
            }
        }
    }
    _outln(':delay 1');
    // NTP
    _outln_comment();
    _outln_comment('NTP (client &#038; server cache) zone: ' . $node->zone_id);
    list($primary_ntp, $secondary_ntp) = explode(' ', guifi_get_ntp($zone));
    if ($secondary_ntp != null) {
        _outln(sprintf('/system ntp client set enabled=yes mode=unicast primary-ntp=%s secondary-ntp=%s', $primary_ntp, $secondary_ntp));
    } else {
        if ($primary_ntp != null) {
            _outln(sprintf('/system ntp client set enabled=yes mode=unicast primary-ntp=%s', $primary_ntp));
        }
    }
    if ($dev->variable['firmware'] == 'RouterOSv2.9') {
        _outln(sprintf('/system ntp server set manycast=no enabled=yes'));
    }
    _outln(':delay 1');
    // Bandwidth-server
    _outln_comment();
    _outln_comment(t('Bandwidth-server'));
    _outln('/ tool bandwidth-server set enabled=yes authenticate=no allocate-udp-ports-from=2000');
    // SNMP
    _outln_comment();
    _outln_comment('SNMP');
    if ($dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
        $community = 'trap-community=public';
        _outln(sprintf('/snmp community set public addresses=0.0.0.0/0'));
    } else {
        $community = '';
    }
    _outln(sprintf('/snmp set contact="*****@*****.**" enabled=yes location="%s" %s', $node->nick, $community));
    // User guest
    _outln_comment();
    _outln_comment('Guest user');
    _outln('/user');
    _outln(':foreach i in [find group=read] do={/user remove $i;}');
    _outln('add name="guest" group=read address=0.0.0.0/0 comment="" disabled=no');
    // Graphing
    _outln_comment();
    _outln_comment(t('Graphing'));
    _outln(sprintf('/tool graphing interface add'));
    // LogServer
    if (!empty($dev->logserver)) {
        $ipd = array();
        $ipd = guifi_main_ip($dev->id);
        _outln_comment(t('Ip for ServerLogs'));
        _outln('/system logging');
        _outln(':foreach i in [/system logging find action=remote]');
        _outln('do={/system logging remove $i }');
        _outln(':foreach i in [/system logging action find name=guifi]');
        _outln('do=[/system logging action remove $i]');
        _outln('/system logging action add name=' . $dev->nick . ' target=remote remote=' . $dev->logserver . ':514 src-address=' . $ipd['ipv4']);
        _outln('/system logging add action=guifi_remot topics=critical');
        _outln('/system logging add action=guifi_remot topics=account');
    }
    if ($radio[mode] != 'client') {
        // Define wLan/Lan bridge (main interface)
        _outln_comment(t('Remove current wLan/Lan bridge if exists'));
        _outln(':foreach i in [/interface bridge find name=wLan/Lan] \\ ');
        _outln('do={:foreach i in [/interface bridge port find bridge=wLan/Lan] \\ ');
        _outln('do={/interface bridge port remove $i; \\ ');
        _outln(':foreach i in [/ip address find interface=wLan/Lan] \\ ');
        _outln('do={/ip address remove $i;};};');
        _outln('/interface bridge remove $i;}');
        // Construct bridge only if exists wlan/lan interface
        if ($wlanlan) {
            _outln_comment(t('Construct main bridge on wlan1 &#038; ether1'));
            _outln('/ interface bridge');
            _outln('add name="wLan/Lan"');
            _outln('/ interface bridge port');
            _outln('add interface=ether1 bridge=wLan/Lan');
            _outln('add interface=wlan1 bridge=wLan/Lan');
        }
        _outln(':delay 1');
    }
    $firewall = false;
    // Going to setup wireless interfaces
    if (isset($dev->radios)) {
        foreach ($dev->radios as $radio_id => $radio) {
            switch ($radio[mode]) {
                case 'ap':
                    $mode = 'ap-bridge';
                    $ssid = $radio['ssid'];
                    $gain = $radio['antenna_gain'];
                    if ($radio[channel] < 5000) {
                        if ($dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
                            $band = '2ghz-b';
                            $chwidth = 'channel-width=20mhz';
                        } else {
                            $band = '2.4ghz-b';
                            $chwidth = '';
                        }
                    } else {
                        if ($dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
                            $band = '5ghz-a';
                            $chwidth = 'channel-width=20mhz';
                        } else {
                            $band = '5ghz';
                            $chwidth = '';
                        }
                    }
                    break;
                case 'client':
                case 'clientrouted':
                    $mode = 'station';
                    $gain = $radio['antenna_gain'];
                    foreach ($radio['interfaces'] as $interface) {
                        foreach ($interface['ipv4'] as $ipv4) {
                            foreach ($ipv4['links'] as $link) {
                                $ssid = guifi_get_ap_ssid($link['interface']['device_id'], $link['interface']['radiodev_counter']);
                                $protocol = guifi_get_ap_protocol($link['interface']['device_id'], $link['interface']['radiodev_counter']);
                                $channel = guifi_get_ap_channel($link['interface']['device_id'], $link['interface']['radiodev_counter']);
                                if ($protocol == '802.11b') {
                                    if ($dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
                                        $band = '2ghz-b';
                                    } else {
                                        $band = '2.4ghz-b';
                                    }
                                }
                                if ($protocol == '802.11a') {
                                    if ($dev->variable['firmware'] == 'RouterOSv5.x' or $dev->variable['firmware'] == 'RouterOSv6.x') {
                                        $band = '5ghz-a';
                                    } else {
                                        $band = '5ghz';
                                    }
                                }
                                if ($protocol == '802.11n' and $channel > 5000) {
                                    $band = '5ghz-a/n';
                                }
                            }
                        }
                    }
                    $firewall = true;
                    break;
            }
            _outln_comment();
            _outln_comment('Radio#: ' . $radio_id . ' ' . $radio[ssid]);
            _outln(sprintf('/interface wireless set wlan%d name="wlan%d" \\ ', $radio_id + 1, $radio_id + 1));
            _outln(sprintf('    radio-name="%s" mode=%s ssid="guifi.net-%s" \\ ', $radio[ssid], $mode, $ssid));
            _outln(sprintf('    band="%s" ' . $chwidth . ' \\ ', $band));
            _outln(sprintf('    frequency-mode=regulatory-domain country=spain antenna-gain=%s \\ ', $gain));
            if ($radio['channel'] != 0 and $radio['channel'] != 5000) {
                // if not auto.. set channel
                if ($radio['channel'] < 20) {
                    $incr = $radio['channel'] * 5;
                    $radio['channel'] = 2407 + $incr;
                }
                _outln(sprintf('    frequency=%d \\ ', $radio['channel']));
            }
            if ($band == '5ghz' || '5ghz-a' and $radio['channel'] == 5000 or $band == '2.4ghz-b' || '2ghz-b' and $radio['channel'] == 0) {
                _outln('    dfs-mode=radar-detect \\ ');
            } else {
                _outln('    dfs-mode=none \\ ');
            }
            if (empty($radio['antenna_mode'])) {
                _outln(sprintf('    wds-mode=static wds-default-bridge=none wds-default-cost=100 \\ '));
            } else {
                if ($radio['antenna_mode'] != 'Main') {
                    $radio['antenna_mode'] = 'ant-b';
                } else {
                    $radio['antenna_mode'] = 'ant-a';
                }
                _outln(sprintf('    antenna-mode=%s wds-mode=static wds-default-bridge=none wds-default-cost=100 \\ ', $radio['antenna_mode']));
            }
            _outln('    wds-cost-range=50-150 wds-ignore-ssid=yes hide-ssid=no');
            if (isset($radio['interfaces'])) {
                foreach ($radio['interfaces'] as $interface_id => $interface) {
                    _outln(':delay 1');
                    _outln_comment('Type: ' . $interface['interface_type']);
                    if ($interface['interface_class'] == 'wds/p2p' || $interface['interface_type'] == 'wds/p2p') {
                        _outln_comment(t('Remove all existing wds interfaces'));
                        _outln(sprintf(':foreach i in [/interface wireless wds find master-interface=wlan%s] \\ ', $radio_id + 1));
                        _outln('do={:foreach n in [/interface wireless wds get $i name] \\ ');
                        _outln('do={:foreach inum in [/ip address find interface=$n] \\ ');
                        _outln('do={/ip address remove $inum;};}; \\ ');
                        _outln('/interface wireless wds remove $i;}');
                        if (isset($interface['ipv4'])) {
                            foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
                                if (isset($ipv4['links'])) {
                                    foreach ($ipv4['links'] as $link_id => $link) {
                                        if (preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                                            $disabled = 'no';
                                        } else {
                                            $disabled = 'yes';
                                        }
                                        $wdsname = 'wds_' . guifi_get_hostname($link['device_id']);
                                        if ($link['interface']['mac'] == null) {
                                            $link['interface']['mac'] = 'FF:FF:FF:FF:FF:FF';
                                        }
                                        _outln('/ interface wireless wds');
                                        _outln(sprintf('add name="%s" master-interface=wlan%d wds-address=%s disabled=%s', $wdsname, $radio_id + 1, $link['interface']['mac'], $disabled));
                                        $item = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
                                        $ospf_zone = guifi_get_ospf_zone($zone);
                                        _outln(sprintf('/ ip address add address=%s/%d network=%s broadcast=%s interface=%s disabled=%s comment="%s"', $ipv4['ipv4'], $item['maskbits'], $item['netid'], $item['broadcast'], $wdsname, $disabled, $wdsname));
                                        if ($link['routing'] == 'OSPF') {
                                            ospf_interface($wdsname, $item['netid'], $item['maskbits'], $ospf_name, $ospf_zone, $ospf_id, 'no');
                                            bgp_peer($link['device_id'], $link['interface']['ipv4']['ipv4'], 'yes');
                                        } else {
                                            ospf_interface($wdsname, $item['netid'], $item['maskbits'], $ospf_name, $ospf_zone, $ospf_id, 'yes');
                                            bgp_peer($link['device_id'], $link['interface']['ipv4']['ipv4'], 'no');
                                        }
                                    }
                                }
                            }
                        }
                        // each wds link (ipv4)
                    } else {
                        // wds
                        // wLan, wLan/Lan, Hotspot or client
                        // Defining all networks and IP addresses at the interface
                        if (isset($interface['ipv4'])) {
                            foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
                                if ($interface['interface_type'] == 'wLan/Lan') {
                                    $iname = $interface['interface_type'];
                                    $ospf_routerid = $ipv4['ipv4'];
                                } else {
                                    $iname = 'wlan' . ($radio_id + 1);
                                }
                                $item = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
                                _outln('/ip address');
                                if ($interface['interface_type'] == 'Wan') {
                                    _outln(sprintf(':foreach i in [find interface=%s] do={remove $i}', $iname));
                                }
                                _outln(sprintf(':foreach i in [find address="%s/%d"] do={remove $i}', $ipv4['ipv4'], $item['maskbits']));
                                _outln(sprintf('/ ip address add address=%s/%d network=%s broadcast=%s interface=%s disabled=no', $ipv4['ipv4'], $item['maskbits'], $item['netid'], $item['broadcast'], $iname));
                                $defined_ips[$ipv4['ipv4']] = $item;
                                $ospf_zone = guifi_get_ospf_zone($zone);
                                _outln('/ routing bgp network');
                                _outln(sprintf(':foreach i in [/routing bgp network find network=%s/%d] do={/routing bgp network remove $i;}', $item['netid'], $item['maskbits']));
                                _outln(sprintf('add network=%s/%d synchronize=no disabled=no', $item['netid'], $item['maskbits']));
                                if ($radio['mode'] != 'client') {
                                    ospf_interface($iname, $item['netid'], $item['maskbits'], $ospf_name, $ospf_zone, $ospf_id, 'no');
                                } else {
                                    ospf_interface($iname, $item['netid'], $item['maskbits'], $ospf_name, $ospf_zone, $ospf_id, 'yes');
                                }
                            }
                        }
                        // HotSpot
                        if ($interface['interface_type'] == 'HotSpot') {
                            _outln_comment();
                            _outln_comment('HotSpot');
                            _outln('/interface wireless');
                            _outln(sprintf(':foreach i in [find name=hotspot%d] do={remove $i}', $radio_id + 1));
                            _outln(sprintf('add name="hotspot%d" arp=enabled master-interface=wlan%d ssid="guifi.net-%s" disabled="no"', $radio_id + 1, $radio_id + 1, variable_get("hotspot_ssid", "HotSpot")));
                            _outln('/ip address');
                            _outln(sprintf(':foreach i in [find address="192.168.%d.1/24"] do={remove $i}', $radio_id + 100));
                            _outln(sprintf('/ip address add address=192.168.%d.1/24 interface=hotspot%d disabled=no', $radio_id + 100, $radio_id + 1));
                            _outln('/ip pool');
                            _outln(sprintf(':foreach i in [find name=hs-pool-%d] do={remove $i}', $radio_id + 100));
                            _outln(sprintf('add name="hs-pool-%d" ranges=192.168.%d.2-192.168.%d.254', $radio_id + 100, $radio_id + 100, $radio_id + 100));
                            _outln('/ip dhcp-server');
                            _outln(sprintf(':foreach i in [find name=hs-dhcp-%d] do={remove $i}', $radio_id + 100));
                            _outln(sprintf('add name="hs-dhcp-%d" interface=hotspot%d lease-time=1h address-pool=hs-pool-%d bootp-support=static authoritative=after-2sec-delay disabled=no', $radio_id + 100, $radio_id + 1, $radio_id + 100));
                            _outln('/ip dhcp-server network');
                            _outln(sprintf(':foreach i in [find address="192.168.%d.0/24"] do={remove $i}', $radio_id + 100));
                            _outln(sprintf('add address=192.168.%d.0/24 gateway=192.168.%d.1 domain=guifi.net comment=dhcp-%s', $radio_id + 100, $radio_id + 100, $radio_id));
                            _outln('/ip hotspot profile');
                            _outln(sprintf(':foreach i in [find name=hsprof%d] do={remove $i}', $radio_id + 1));
                            _outln(sprintf('add name="hsprof%d" hotspot-address=192.168.%d.1 dns-name="guests.guifi.net" html-directory=hotspot smtp-server=0.0.0.0 login-by=http-pap,trial split-user-domain=no trial-uptime=30m/1d trial-user-profile=default use-radius=no', $radio_id + 1, $radio_id + 100));
                            _outln('/ip hotspot user profile');
                            _outln('set default name="default" advertise-url=http://guifi.net/trespassos/');
                            _outln('/ip hotspot');
                            _outln(sprintf(':foreach i in [find name=hotspot%d] do={remove $i}', $radio_id + 1));
                            _outln(sprintf('add name="hotspot%d" interface=hotspot%d address-pool=hs-pool-%d profile=hsprof%d idle-timeout=5m keepalive-timeout=none addresses-per-mac=2 disabled=no', $radio_id + 1, $radio_id + 1, $radio_id + 100, $radio_id + 1));
                            _outln_comment('end of HotSpot');
                        }
                        // HotSpot
                        _outln(':delay 1');
                        if (!preg_match("/(HotSpot|Wan)/", $interface['interface_type'])) {
                            // Not link only (AP), setting DHCP
                            if ($mode == 'ap-bridge') {
                                $maxip = ip2long($item['netstart']) + 1;
                                if ($maxip + 5 > ip2long($item['netend']) - 5) {
                                    $maxip = ip2long($item['netend']);
                                    $dhcp_disabled = 'yes';
                                } else {
                                    $maxip = $maxip + 5;
                                    $dhcp_disabled = 'no';
                                }
                                _outln_comment();
                                _outln_comment('DHCP');
                                _outln(sprintf(':foreach i in [/ip pool find name=dhcp-%s] do={/ip pool remove $i;}', $iname));
                                _outln(sprintf('/ip pool add name=dhcp-%s ranges=%s-%s', $iname, long2ip($maxip), $item['netend']));
                                _outln(sprintf(':foreach i in [/ip dhcp-server find name=dhcp-%s] do={/ip dhcp-server remove $i;}', $iname));
                                _outln(sprintf('/ip dhcp-server add name=dhcp-%s interface=%s address-pool=dhcp-%s disabled=%s', $iname, $iname, $iname, $dhcp_disabled));
                                _outln(sprintf(':foreach i in [/ip dhcp-server network find address="%s/%d"] do={/ip dhcp-server network remove $i;}', $item['netid'], $item['maskbits']));
                                _outln(sprintf('/ip dhcp-server network add address=%s/%d gateway=%s domain=guifi.net comment=dhcp-%s', $item['netid'], $item['maskbits'], $item['netstart'], $iname));
                                $dhcp = array();
                                $dhcp[] = '/ip dhcp-server lease';
                                $dhcp[] = ':foreach i in [find comment=""] do={remove $i;}';
                                $dhcp[] = ':delay 1';
                                if (isset($ipv4['links'])) {
                                    foreach ($ipv4['links'] as $link_id => $link) {
                                        if (isset($link['interface']['ipv4']['ipv4'])) {
                                            if (ip2long($link['interface']['ipv4']['ipv4']) >= $maxip) {
                                                $maxip = ip2long($link['interface']['ipv4']['ipv4']) + 1;
                                            }
                                        }
                                        if ($link['interface']['mac'] == null) {
                                            $rmac = 'ff:ff:ff:ff:ff:ff';
                                        } else {
                                            $rmac = $link['interface']['mac'];
                                        }
                                        $dhcp[] = sprintf('add address=%s mac-address=%s client-id=%s server=dhcp-%s', $link['interface']['ipv4']['ipv4'], $rmac, guifi_get_hostname($link['device_id']), $iname);
                                    }
                                }
                                foreach ($dhcp as $outln) {
                                    _outln($outln);
                                }
                            }
                        }
                    }
                    // wLan, wLan/Lan or client
                    _outln_comment();
                }
            }
            // foreach radio->interface
            _outln(':delay 1');
        }
    }
    // foreach radio
    if ($firewall) {
        _outln_comment();
        _outln_comment('Device has firewall (setting up as CPE)');
        // Setting gateway
        _outln(sprintf('/ip route add gateway=%s', $link['interface']['ipv4']['ipv4']));
        // Setting private network and DHCP
        _outln(':foreach i in [find address="192.168.1.1/24"] do={remove $i}');
        _outln('/ip address add address=192.168.1.1/24 network=192.168.1.0 broadcast=192.168.1.255 interface=ether1 comment="" disabled=no');
        _outln(':delay 1');
        _outln('/ip pool');
        _outln(':foreach i in [find name=private] do={remove $i}');
        _outln('add name="private" ranges=192.168.1.100-192.168.1.200');
        _outln(':delay 1');
        _outln('/ip dhcp-server');
        _outln(':foreach i in [find name=private] do={remove $i}');
        _outln('add name="private" interface=ether1 lease-time=3d address-pool=private bootp-support=static authoritative=after-2sec-delay disabled=no');
        _outln(':delay 1');
        _outln('/ip dhcp-server network');
        _outln(':foreach i in [find] do={remove $i}');
        if ($secondary_dns != null) {
            _outln(sprintf('add address=192.168.1.0/24 gateway=192.168.1.1 netmask=24 dns-server=%s,%s domain="guifi.net" comment=""', $primary_dns, $secondary_dns));
        } else {
            if ($primary_dns != null) {
                _outln(sprintf('add address=192.168.1.0/24 gateway=192.168.1.1 netmask=24 dns-server=%s domain="guifi.net" comment=""', $primary_dns));
            }
        }
        _outln(':delay 1');
        // be sure that there is no dhcp client requests since having a static ip
        _outln('/ip dhcp-client');
        _outln(':foreach i in [find] do={remove $i}');
        _outln(':delay 1');
        // NAT private network
        _outln('/ip firewall nat');
        _outln(':foreach i in [find] do={remove $i}');
        _outln(':delay 1');
        _outln('add chain=srcnat out-interface=wlan1 action=masquerade comment="" disabled=no');
        // Firewall enabled, allowing winbox, ssh and snmp
        _outln('/ip firewall filter');
        _outln(':foreach i in [find] do={remove $i}');
        _outln('add chain=input connection-state=established action=accept comment="Allow Established connections" disabled=no');
        _outln('add chain=input protocol=udp action=accept comment="Allow UDP" disabled=no');
        _outln('add chain=input src-address="192.168.1.0/24" action=accept comment="Allow access to router from known network" disabled=no');
        _outln('add chain=input protocol=tcp dst-port=22 action=accept comment="Allow remote ssh" disabled=no');
        _outln('add chain=input protocol=udp dst-port=161 action=accept comment="Allow snmp" disabled=no');
        _outln('add chain=input protocol=tcp dst-port=8291 action=accept comment="Allow remote winbox" disabled=no');
        _outln('add chain=input protocol=icmp action=accept comment="Allow ping" disabled=no');
        _outln('add chain=forward connection-state=established action=accept comment="Allow already established connections" disabled=no');
        _outln('add chain=forward connection-state=related action=accept comment="Allow related connections" disabled=no');
        _outln('add chain=forward src-address="192.168.1.0/24" action=accept comment="Allow access to router from known network" disabled=no');
        _outln('add chain=input protocol=tcp connection-state=invalid action=drop comment="" disabled=no');
        _outln('add chain=forward protocol=tcp connection-state=invalid action=drop comment="Drop invalid connections" disabled=no');
        _outln('add chain=forward action=drop comment="Drop anything else" disabled=no');
        _outln('add chain=input action=drop comment="Drop anything else" disabled=no');
        _outln(':delay 1');
        // End of Unsolclic
        _outln_comment();
        _outln(sprintf(':log info "Unsolclic for %d-%s executed."', $dev->id, $dev->nick));
        _outln('/');
        return;
    }
    _outln_comment();
    _outln_comment('Routed device');
    // Now, defining other interfaces (if they aren't yet)
    _outln_comment();
    _outln_comment(t('Other cable connections'));
    if (isset($dev->interfaces)) {
        foreach ($dev->interfaces as $interface_id => $interface) {
            switch ($interface[interface_type]) {
                case 'vlan':
                    $iname = 'wLan/Lan';
                    break;
                case 'vlan2':
                    $iname = 'ether2';
                    break;
                case 'vlan3':
                    $iname = 'ether3';
                    break;
                case 'vlan4':
                    $iname = 'wLan/Lan';
                    break;
                case 'Wan':
                    $iname = 'wLan/Lan';
                    break;
                default:
                    $iname = $interface['interface_type'];
                    break;
            }
            $ospf_intrefaces[] = $iname;
            if (isset($interface['ipv4'])) {
                foreach ($interface['ipv4'] as $ipv4_id => $ipv4) {
                    if (!isset($defined_ips[$ipv4['ipv4']])) {
                        $disabled = 'yes';
                        if (isset($ipv4['links'])) {
                            unset($comments);
                            foreach ($ipv4['links'] as $link_id => $link) {
                                if ($disabled = 'yes' and preg_match("/(Working|Testing|Building)/", $link['flag'])) {
                                    $disabled = 'no';
                                }
                                $comments[] = guifi_get_hostname($link['device_id']);
                                $ospf_zone = guifi_get_ospf_zone($zone);
                                $item = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
                                if ($link['routing'] == 'OSPF') {
                                    ospf_interface($iname, $item['netid'], $item['maskbits'], $ospf_name, $ospf_zone, $ospf_id, 'no');
                                    bgp_peer($link['device_id'], $link['interface']['ipv4']['ipv4'], 'yes');
                                } else {
                                    ospf_interface($iname, $item['netid'], $item['maskbits'], $ospf_name, $ospf_zone, $ospf_id, 'yes');
                                    bgp_peer($link['device_id'], $link['interface']['ipv4']['ipv4'], 'no');
                                }
                            }
                        } else {
                            $disabled = 'no';
                        }
                        $item = _ipcalc($ipv4['ipv4'], $ipv4['netmask']);
                        _outln(sprintf(':foreach i in [/ip address find address="%s/%d"] do={/ip address remove $i;}', $ipv4['ipv4'], $item['maskbits']));
                        _outln(':delay 1');
                        _outln(sprintf('/ ip address add address=%s/%d network=%s broadcast=%s interface=%s disabled=%s comment="%s"', $ipv4['ipv4'], $item['maskbits'], $item['netid'], $item['broadcast'], $iname, $disabled, implode(',', $comments)));
                        $defined_ips[$ipv4['ipv4']] = $item;
                    }
                }
            }
        }
    }
    // NAT for internal addresses while being used inside the router
    _outln_comment();
    _outln_comment(t('Internal addresses NAT'));
    _outln(':foreach i in [/ip firewall nat find src-address="172.16.0.0/12"] do={/ip firewall nat remove $i;}');
    _outln(':foreach i in [/ip firewall nat find src-address="192.168.0.0/16"] do={/ip firewall nat remove $i;}');
    _outln('/ip firewall nat');
    switch ($dev->variable['firmware']) {
        case 'RouterOSv2.9':
            _outln(sprintf('add chain=srcnat src-address="192.168.0.0/16" dst-address=!192.168.0.0/16 action=src-nat to-addresses=%s to-ports=0-65535 comment="" disabled=no', $ospf_routerid));
            break;
        case 'RouterOSv3.x':
        case 'RouterOSv4.0+':
        case 'RouterOSv4.7+':
        case 'RouterOSv5.x':
        case 'RouterOSv6.x':
            _outln(sprintf('add chain=srcnat src-address="192.168.0.0/16" dst-address=!192.168.0.0/16 action=src-nat to-addresses=%s comment="" disabled=no', $ospf_routerid));
            break;
    }
    // BGP
    _outln_comment();
    _outln_comment(t('BGP Routing'));
    _outln_comment(t('BGP & OSPF Filters'));
    _outln(':foreach i in [/routing filter find chain=ospf-in] do={/routing filter remove $i;}');
    _outln(':foreach i in [/routing filter find chain=ospf-out] do={/routing filter remove $i;}');
    _outln(':foreach i in [/routing filter find chain=ebgp-in] do={/routing filter remove $i;}');
    _outln(':foreach i in [/routing filter find chain=ebgp-out] do={/routing filter remove $i;}');
    _outln("/ routing filter");
    _outln(sprintf('add chain=ebgp-in comment="0. Set ebgp-in preferred source." set-pref-src="%s" disabled=no', $ospf_routerid));
    _outln('add action=discard chain=ebgp-in comment="1. Discard insert non 10.x routes from BGP peer" disabled=no invert-match=no prefix=!10.0.0.0/8 prefix-length=8-32');
    _outln('add action=discard chain=ebgp-out comment="2. Discard send non 10.x routes to BGP peer" disabled=no invert-match=no prefix=!10.0.0.0/8 prefix-length=8-32');
    _outln(sprintf('add action=accept chain=ospf-in comment="3. Accept insert 10.x routes from OSPF neighbor" disabled=no invert-match=no prefix=10.0.0.0/8 prefix-length=8-32 set-pref-src="%s"', $ospf_routerid));
    _outln('add action=accept chain=ospf-in comment="4. Accept insert 172.x routes from OSPF neighbor" disabled=no invert-match=no prefix=172.16.0.0/12 prefix-length=8-32');
    _outln('add action=discard chain=ospf-in comment="5. Discard insert non 10.x and 172.x from OSPF neighbor" disabled=no invert-match=no');
    _outln('add action=accept chain=ospf-out comment="6. Allow send 10.x routes to OSPF neighbor" disabled=no invert-match=no prefix=10.0.0.0/8 prefix-length=8-32');
    _outln('add action=accept chain=ospf-out comment="7. Allow send 172.x routes to OSPF neighbor" disabled=no invert-match=no prefix=172.16.0.0/12 prefix-length=8-32');
    _outln('add action=discard chain=ospf-out comment="8. Discard send non 10.x and 172.x to OSPF neighbor" disabled=no invert-match=no');
    _outln_comment();
    _outln_comment(t('BGP instance'));
    _outln("/ routing bgp instance");
    _outln(sprintf('set default name="default" as=%d router-id=%s \\ ', $dev->id, $ospf_routerid));
    switch ($dev->variable['firmware']) {
        case 'RouterOSv4.0+':
        case 'RouterOSv4.7+':
        case 'RouterOSv5.x':
        case 'RouterOSv6.x':
            _outln('redistribute-connected=no redistribute-static=no redistribute-rip=no \\ ');
            break;
        default:
            _outln('redistribute-connected=yes redistribute-static=yes redistribute-rip=yes \\ ');
    }
    _outln('redistribute-ospf=yes redistribute-other-bgp=yes out-filter=ebgp-out \\ ');
    _outln('client-to-client-reflection=yes comment="" disabled=no');
    // OSPF
    _outln_comment();
    _outln_comment(t('OSPF Routing'));
    switch ($dev->variable['firmware']) {
        case 'RouterOSv2.9':
        case 'RouterOSv3.x':
            _outln(sprintf('/routing ospf set router-id=%s distribute-default=never redistribute-connected=no \\ ', $ospf_routerid));
            _outln(sprintf('redistribute-static=no redistribute-rip=no redistribute-bgp=as-type-1'));
            break;
        case 'RouterOSv4.0+':
        case 'RouterOSv4.7+':
        case 'RouterOSv5.x':
        case 'RouterOSv6.x':
            _outln(sprintf('/routing ospf instance set default name=default router-id=%s comment="" disabled=no distribute-default=never \\ ', $ospf_routerid));
            _outln(sprintf('redistribute-bgp=as-type-1 redistribute-connected=no redistribute-other-ospf=no redistribute-rip=no redistribute-static=no in-filter=ospf-in out-filter=ospf-out'));
            break;
    }
    // End of Unsolclic
    _outln_comment();
    _outln(sprintf(':log info "Unsolclic for %d-%s executed."', $dev->id, $dev->nick));
    _outln('/');
}
Example #12
0
function guifi_ahah_add_remoteipv4()
{
    $SNet = arg(3);
    $ipv4 = $_POST['ipv4'][$SNet];
    //  $tree = array('ipv4',$SNetId);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 (id=%d):', $SNet), $ipv4[subnet]);
    // find the next free ip address
    $deletedPresent = false;
    $ips = array(ip2long($ipv4[ipv4]));
    foreach ($ipv4[subnet] as $ki => $i) {
        if ($ki != 'add-ipv4') {
            if ($i['deleted'] == true) {
                $deletedPresent = true;
            }
        }
        if (empty($i[ipv4]) and !empty($i[ipv4value])) {
            $ipv4[subnet][$ki][ipv4] = $i[ipv4] = $i[ipv4value];
        }
        if ($ip = ip2long($i[ipv4])) {
            if (!in_array($ip, $ips)) {
                $ips[] = $ip;
            }
        }
    }
    sort($ips);
    guifi_log(GUIFILOG_BASIC, sprintf('guifi_ahah_add_remoteipv4 (id=%d):', $SNet), $ips);
    $ipc = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
    $lstart = ip2long($ipc[netstart]);
    $c = 0;
    while ($ips[$c] == $lstart + $c) {
        $c++;
    }
    $free = long2ip($lstart + $c);
    // Create new element
    $newI = array('new' => true, 'ipv4' => $free, 'netmask' => $ipv4['netmask']);
    if ($deletedPresent == true) {
        drupal_set_message(t('Deleted addresses found. To reuse them you must save first to confirm changes.'), 'warning');
    }
    drupal_set_message(t('Next available address at %base/%maskbits is %addr', array('%addr' => $free, '%base' => $ipc[netid], '%maskbits' => $ipc[maskbits])));
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 (start=%s) new', long2ip($lstart)), $newI);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 ipcalc'), $ipc);
    $k = 0;
    while (!empty($ipv4[subnet][$k][ipv4])) {
        $k++;
    }
    $ipv4[subnet][$k] = $newI;
    guifi_log(GUIFILOG_FULL, sprintf('guifi_ahah_add_remoteipv4 (id=%d):', $SNet), $ipv4[subnet]);
    // Build our new form element.
    $form_element = guifi_ipv4subnet_form($ipv4, $SNet, true);
    // Build the new form.
    $form_state = array('submitted' => FALSE);
    $form_build_id = $_POST['form_build_id'];
    // Add the new element to the stored form. Without adding the element to the
    // form, Drupal is not aware of this new elements existence and will not
    // process it. We retreive the cached form, add the element, and resave.
    $form = form_get_cache($form_build_id, $form_state);
    guifi_log(GUIFILOG_TRACE, sprintf('guifi_ahah_add_remoteipv4 (id=%d) state:', $SNet), $form_state);
    $choice_form = $form[ipv4][$SNet]['subnet'];
    $form['ipv4'][$SNet]['subnet'] = $form_element;
    $choice_form = $form[ipv4][$SNet]['subnet'];
    form_set_cache($form_build_id, $form, $form_state);
    $form += array('#post' => $_POST, '#programmed' => FALSE);
    // Rebuild the old form.
    $form = form_builder('guifi_device_form', $form, $form_state);
    // Render the new output.
    $choice_form = $form[ipv4][$SNet][subnet];
    unset($choice_form['#prefix'], $choice_form['#suffix']);
    // Prevent duplicate wrappers.
    //unset($choice_form[$delta]);
    // build new form
    //  $fs = array();
    //  $form_element['#post'] = array();
    //  $form_element = form_builder($form_element['form_id']['#value'] , $form_element, $fs);
    //  $newfields = drupal_render($form_element);
    //  guifi_log(GUIFILOG_BASIC,sprintf('choice_form %d',$delta),htmlspecialchars($newfield));
    $output = theme('status_messages') . drupal_render($choice_form);
    drupal_json(array('status' => TRUE, 'data' => $output));
    exit;
}
Example #13
0
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_interfaces_add_cable_public_link_submit(&$form, &$form_state)
{
    $values = $form_state['clicked_button']['#parents'];
    $iid = $values[1];
    $ipv4_id = $values[3];
    $to_did = $form_state['values']['interfaces'][$iid]['ipv4'][$ipv4_id]['to_did'];
    $rdevice = guifi_device_load($to_did);
    guifi_log(GUIFILOG_TRACE, sprintf('function guifi_interfaces_add_cable_public_link_submit(%d)', $iid), $form_state['clicked_button']['#parents']);
    $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $form_state['values'], 1);
    // get next available ip address
    $base_ip = $form_state['values']['interfaces'][$iid]['ipv4'][$ipv4_id];
    $item = _ipcalc($base_ip['ipv4'], $base_ip['netmask']);
    $ip = guifi_ipcalc_find_ip($item['netid'], $base_ip['netmask'], $ips_allocated);
    // no IP was given, so raise a message and don't create the link'
    if (empty($ip)) {
        drupal_set_message(t('Unable to assign a free ip, link not created, ' . 'contact the administrator.'));
        return;
    }
    $newlk['new'] = TRUE;
    $newlk['interface'] = array();
    $newlk['link_type'] = 'cable';
    $newlk['flag'] = 'Planned';
    $newlk['nid'] = $form_state['values']['nid'];
    $newlk['device_id'] = $to_did;
    if ($rdevice['type'] == 'radio') {
        $newlk['routing'] = 'BGP';
    } else {
        $newlk['routing'] = 'Gateway';
    }
    $newlk['interface']['new'] = TRUE;
    $newlk['interface']['device_id'] = $to_did;
    $free = guifi_get_free_interfaces($to_did, $rdevice);
    $newlk['interface']['interface_type'] = array_shift($free);
    $newlk['interface']['ipv4']['new'] = TRUE;
    $newlk['interface']['ipv4']['ipv4_type'] = 1;
    $newlk['interface']['ipv4']['ipv4'] = $ip;
    $newlk['interface']['ipv4']['netmask'] = $base_ip['netmask'];
    $form_state['values']['interfaces'][$iid]['ipv4'][$ipv4_id]['links'][] = $newlk;
    $form_state['values']['interfaces'][$iid]['unfold'] = TRUE;
    //  print_r($form_state['values']);
    $form_state['rebuild'] = TRUE;
    return TRUE;
}
Example #15
0
function ospf_net_add_node_networks(&$networks, $nid)
{
    $v = "";
    $result = db_query(sprintf("SELECT t3.ipv4, t3.netmask\n               FROM guifi_devices as t1\n               join guifi_interfaces as t2 on t1.id = t2.device_id\n               join guifi_ipv4 as t3 on t2.id = t3.interface_id\n               where t1.nid = (%s) and t3.ipv4_type=1", $nid));
    while ($record = db_fetch_object($result)) {
        $a = _ipcalc($record->ipv4, $record->netmask);
        $c = ip2long($a["netid"]);
        if (!isset($networks[$c])) {
            $networks[$c] = array("ipv4" => $record->ipv4, "netmask" => $record->netmask, "netid" => $a["netid"], "maskbits" => $a["maskbits"], "nid" => $nid);
        } elseif ($networks[$c]["maskbits"] > $a["maskbits"]) {
            $networks[$c] = array("ipv4" => $record->ipv4, "netmask" => $record->netmask, "netid" => $a["netid"], "maskbits" => $a["maskbits"], "nid" => $nid);
        }
    }
    return 0;
}
Example #16
0
function guifi_radio_add_link2ap_confirm_submit(&$form, &$form_state)
{
    guifi_log(GUIFILOG_TRACE, sprintf("function guifi_radio_add_link2ap_confirm_submit(Radio: %d)", $form_state['values']['filters']['from_radio']), $form_state['values']['filters']);
    $form_state['rebuild'] = TRUE;
    $interface_id = $form_state['values']['link2apInterface'];
    $local_ipv4 =& $form_state['values']['radios'][$form_state['values']['filters']['from_radio']]['interfaces'][$interface_id]['ipv4'];
    list($nid, $device_id, $radiodev_counter) = explode(',', $form_state['values']['linked']);
    // get list of the current used ips
    $ips_allocated = guifi_ipcalc_get_ips('0.0.0.0', '0.0.0.0', $form_state['values'], 1);
    $qAP = db_query('SELECT i.id, i.radiodev_counter, i.mac, a.ipv4, a.netmask, a.id aid ' . 'FROM {guifi_interfaces} i, {guifi_ipv4} a ' . 'WHERE i.device_id = %d ' . '  AND i.interface_type in ("wLan/Lan","wLan") ' . '  AND i.radiodev_counter=%d ' . '  AND a.interface_id=i.id', $device_id, $radiodev_counter);
    $link = array();
    while ($ipAP = db_fetch_array($qAP)) {
        $item = _ipcalc($ipAP['ipv4'], $ipAP['netmask']);
        $link['ipv4'] = guifi_ipcalc_find_ip($item['netid'], $ipAP['netmask'], $ips_allocated);
        if ($link['ipv4'] != NULL) {
            break;
        }
        drupal_set_message(t('Network was full, looking for more networks available...'));
    }
    // if network was full, delete link
    if ($link['ipv4'] == NULL) {
        $form_state['action'] = 'guifi_radio_add_link2ap_form';
        drupal_set_message(t('The Link was not created: ' . 'The selected node have already all possible connections allocated. ' . 'Now you can:' . '<ul><li>select another Access Point</li>' . '<li><b>contribute</b> to create a new Access Point ' . 'to increase the possible network connections..<li>' . '<li>...or check the status and usage of the currently defined connections ' . 'at this device to find an unused but defined connection ' . 'to be reused.</li></ul>' . 'Note that Open Networks are expanding thanks to ' . 'the <b>contributions of their participants</b>.'), 'error');
        return;
    }
    drupal_set_message(t('Got IP address %net/%mask', array('%net' => $link['ipv4'], '%mask' => $ipAP['netmask'])));
    // local ipv4 information
    $lipv4['new'] = TRUE;
    $lipv4['ipv4'] = $link['ipv4'];
    $lipv4['ipv4_type'] = 1;
    $lipv4['netmask'] = $ipAP['netmask'];
    // link
    $lipv4['links'][0]['new'] = TRUE;
    $lipv4['links'][0]['unfold'] = TRUE;
    $lipv4['links'][0]['interface_id'] = $ipAP['id'];
    $lipv4['links'][0]['device_id'] = $device_id;
    $lipv4['links'][0]['nid'] = $nid;
    $lipv4['links'][0]['routing'] = 'Gateway';
    $lipv4['links'][0]['link_type'] = 'ap/client';
    // remote interface
    $lipv4['links'][0]['interface']['id'] = $ipAP['id'];
    $lipv4['links'][0]['interface']['radiodev_counter'] = $ipAP['radiodev_counter'];
    // remote ipv4
    $lipv4['links'][0]['interface']['ipv4']['id'] = $ipAP['aid'];
    $lipv4['links'][0]['interface']['ipv4']['ipv4'] = $ipAP['ipv4'];
    $lipv4['links'][0]['interface']['ipv4']['netmask'] = $ipAP['netmask'];
    // unfold return
    $form_state['values']['radios'][$form_state['values']['filters']['from_radio']]['unfold'] = TRUE;
    $lipv4['unfold'] = TRUE;
    $local_ipv4[] = $lipv4;
    // guifi_log(GUIFILOG_BASIC,'WDS added',$form_state['values']);
    unset($form_state['action']);
    return;
}
 function vout($if, $ipv4, $link)
 {
     global $otype;
     $output = '# ' . $if . ': ' . guifi_get_hostname($link['interface'][device_id]);
     if ($otype == 'html') {
         $output .= "\n<br />";
     } else {
         $output .= "\n";
     }
     $item = _ipcalc($ipv4[ipv4], $ipv4[netmask]);
     if (!preg_match("/(Working|Testing|Building)/", $link[flag])) {
         $output .= '# ';
     }
     $output .= 'ifconfig ' . $if . ' ' . $ipv4[ipv4] . ' netmask ' . $ipv4[netmask] . ' broadcast ' . $item['broadcast'];
     if ($otype == 'html') {
         $output .= "\n<br />";
     } else {
         $output .= "\n";
     }
     return $output;
 }