interfaces.lib.inc 8.48 KB
Newer Older
1 2 3
<?php

/*
4
    Copyright (c) 2015-2016 Franco Fichtner <franco@opnsense.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
27 28
*/

29 30
function legacy_interface_listget($flag = '')
{
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    $cmd = '/sbin/ifconfig -l';
    $ifs = null;

    if ($flag === 'up') {
        $cmd .= 'u';
    } else if ($flag === 'down') {
        $cmd .= 'd';
    }

    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
        return ($ifs);
    }

    if (isset($out[0])) {
        $ifs = explode(' ', $out[0]);
    }

    return ($ifs);
51 52
}

53
function legacy_interface_flags($ifs, $flag, $report_errors=true)
54
{
55
    /* $flags isn't escaped because it can be an argument list */
56
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' ' . $flag;
57

58 59 60 61
    exec($cmd . ' 2>&1', $out, $ret);
    if (!empty($ret) && $report_errors) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
62 63
}

64 65
function legacy_interface_create($ifs)
{
66 67
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' create';
    $new = null;
68

69 70 71 72 73
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
        return ($new);
    }
74

75 76 77
    if (isset($out[0])) {
        $new = $out[0];
    }
78

79
    return ($new);
80 81 82 83
}

function legacy_interface_destroy($ifs)
{
84
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' destroy';
85

86 87 88 89
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
90 91 92 93
}

function legacy_interface_setaddress($ifs, $addr)
{
94
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' alias ' . escapeshellarg($addr);
95

96 97 98 99
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
100 101 102 103
}

function legacy_interface_deladdress($ifs, $addr)
{
104
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' -alias ' . escapeshellarg($addr);
105

106 107 108 109
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
110 111 112 113
}

function legacy_interface_rename($ifs, $name)
{
114
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' name ' . escapeshellarg($name);
115

116 117 118 119
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
120
}
121 122 123

function legacy_interface_mtu($ifs, $mtu)
{
124
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' mtu ' . escapeshellarg($mtu);
125

126 127 128 129
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
130 131 132 133
}

function legacy_bridge_member($ifs, $member)
{
134
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' addm ' . escapeshellarg($member);
135

136 137 138 139
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
140 141 142 143
}

function legacy_vlan_tag($ifs, $member, $tag)
{
144
    $cmd = '/sbin/ifconfig ' . escapeshellarg($ifs) . ' vlandev ' . escapeshellarg($member) . ' vlan ' . escapeshellarg($tag);
145

146 147 148 149
    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
    }
150
}
151 152 153

function legacy_interface_stats($ifs)
{
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    $cmd = '/usr/local/sbin/ifinfo ' . escapeshellarg($ifs);
    $stats = array();

    exec($cmd . ' 2>&1', $out, $ret);
    if ($ret) {
        log_error('The command `' . $cmd . '\' failed to execute');
        return $stats;
    }

    if (count($out)) {
        /* first one is header */
        array_shift($out);

        foreach ($out as $line) {
            $stat = explode(':', $line);
            $stats[trim($stat[0])] = trim($stat[1]);
        }
    }

    return $stats;
174
}
175 176 177 178 179 180 181 182 183 184

/**
 * detect interface capabilities using ifconfig -m
 * @param $intf interface name
 * @return array list of interface capabilities (in lowercase)
 */
function legacy_interface_details($intf)
{
    $result = array();
    $result["capabilities"] = array();
185
    $result["options"] = array();
186 187 188 189 190 191 192 193 194 195
    $process = proc_open('/sbin/ifconfig -m ' . escapeshellarg($intf), array(array("pipe", "r"), array("pipe", "w")), $pipes);
    if (is_resource($process)) {
        $ifconfig_data = stream_get_contents($pipes[1]);
        foreach (explode("\n", $ifconfig_data) as $line) {
            if (strpos(trim($line), 'capabilities=') !== false) {
                // parse capabilities
                $capabilities = substr($line, strpos($line, '<') + 1, -1);
                foreach (explode(',', $capabilities) as $capability) {
                    $result["capabilities"][] = strtolower(trim($capability));
                }
196 197 198 199 200 201
            } elseif (strpos(trim($line), 'options=') !== false) {
                // parse options
                $capabilities = substr($line, strpos($line, '<') + 1, -1);
                foreach (explode(',', $capabilities) as $capability) {
                    $result["options"][] = strtolower(trim($capability));
                }
202
            }
203

204 205 206 207 208 209
        }
        fclose($pipes[1]);
        proc_close($process);
    }
    return $result;
}
210 211 212 213 214 215 216 217

function legacy_netgraph_attach($ifs)
{
	mwexecf('/usr/local/sbin/ngattach %s', array($ifs));
}

function legacy_netgraph_detach($ifs)
{
218
        mwexecf('/usr/sbin/ngctl msg %s: detach', array($ifs), true);
219 220 221 222 223 224
}

function legacy_netgraph_rename($tmpifs, $ifs)
{
        mwexecf('/usr/sbin/ngctl name %s: %s', array($tmpifs, $ifs));
}
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

/**
 * configure interface hardware settings
 * @param string $ifs interface name
 */
function configure_interface_hardware($ifs)
{
    global $config;
    $intf_details = legacy_interface_details($ifs);
    /* skip vlans for checksumming and polling */
    if (!stristr($ifs, "_vlan") && is_array($intf_details)) {
        // get current settings
        $csum_set = in_array('rxcsum', $intf_details['options']) || in_array('txcsum', $intf_details['options']);
        $tso_set = in_array('tso4', $intf_details['options']) || in_array('tso6', $intf_details['options']);
        $lro_set = in_array('lro', $intf_details['options']);
        $polling_set = in_array('polling', $intf_details['options']);

        // hardware checksum offloading offloading
        if (isset($config['system']['disablechecksumoffloading']) && $csum_set) {
            legacy_interface_flags($ifs, '-txcsum -rxcsum', false);
        } elseif (!isset($config['system']['disablechecksumoffloading']) && !$csum_set) {
            legacy_interface_flags($ifs, 'txcsum rxcsum', false);
        }
        // TCP segmentation offloading
        if (isset($config['system']['disablesegmentationoffloading']) && $tso_set) {
            legacy_interface_flags($ifs, '-tso', false);
        } elseif (!isset($config['system']['disablesegmentationoffloading']) && !$tso_set) {
            legacy_interface_flags($ifs, 'tso', false);
        }

        // large receive offload
        if (isset($config['system']['disablelargereceiveoffloading']) && $lro_set) {
            legacy_interface_flags($ifs, '-lro', false);
        } elseif (!isset($config['system']['disablelargereceiveoffloading']) && !$lro_set) {
            legacy_interface_flags($ifs, 'lro', false);
        }

        // polling
        if (isset($config['system']['polling']) && !$polling_set) {
            legacy_interface_flags($ifs, 'polling', false);
        } elseif (!isset($config['system']['polling']) && $polling_set) {
            legacy_interface_flags($ifs, '-polling', false);
        }
    }
}