]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.device
hostapd: Enable WMM by default.
[people/ms/network.git] / functions.device
index 47187f58a3b663c935ff7053d3d9e00ea79021a9..b15e15ec96d190df16074a89393b0d146f07eb2b 100644 (file)
@@ -65,7 +65,44 @@ function device_exists() {
        # If device name was not found, exit.
        [ -n "${device}" ] || return ${EXIT_ERROR}
 
-       [ -d "${SYS_CLASS_NET}/${device}" ]
+       # Check for a normal network device.
+       [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
+
+       # If the check above, did not find a result,
+       # we check for serial devices.
+       serial_exists ${device}
+}
+
+function device_delete() {
+       local device=${1}
+       assert isset device
+
+       # Nothing to do, it device does not exist.
+       device_exists ${device} || return ${EXIT_OK}
+
+       # Delete the device.
+       cmd_quiet ip link delete ${device}
+       local ret=$?
+
+       if [ ${ret} -ne ${EXIT_OK} ]; then
+               log ERROR "device: Could not delete device '${device}': ${ret}"
+               return ${EXIT_ERROR}
+       fi
+
+       return ${ret}
+}
+
+function device_has_flag() {
+       local device=${1}
+       local flag=${2}
+
+       local flags=$(__device_get_file ${device} flags)
+
+       if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
+               return ${EXIT_FALSE}
+       else
+               return ${EXIT_TRUE}
+       fi
 }
 
 # Check if the device is up
@@ -74,7 +111,49 @@ function device_is_up() {
 
        device_exists ${device} || return ${EXIT_ERROR}
 
-       ip link show ${device} 2>/dev/null | grep -qE "<.*UP.*>"
+       device_has_flag ${device} 0x1
+}
+
+function device_ifindex_to_name() {
+       local idx=${1}
+       assert isset idx
+
+       local device device_idx
+       for device in ${SYS_CLASS_NET}/*; do
+               device=$(basename ${device})
+               device_exists ${device} || continue
+
+               device_idx=$(device_get_ifindex ${device})
+
+               if [ "${device_idx}" = "${idx}" ]; then
+                       print "${device}"
+                       return ${EXIT_OK}
+               fi
+       done
+
+       return ${EXIT_ERROR}
+}
+
+function device_get_ifindex() {
+       local device=${1}
+       assert isset device
+
+       local path="${SYS_CLASS_NET}/${1}/ifindex"
+
+       # Check if file can be read.
+       [ -r "${path}" ] || return ${EXIT_ERROR}
+
+       print "$(<${path})"
+}
+
+# Check if the device is a batman-adv bridge
+function device_is_batman_adv() {
+       [ -d "${SYS_CLASS_NET}/${1}/mesh" ]
+}
+
+# Check if the device is a batman-adv bridge port
+function device_is_batman_adv_port() {
+       [ -d "${SYS_CLASS_NET}/${1}/batman_adv" ]
 }
 
 # Check if the device is a bonding device
@@ -96,29 +175,60 @@ function device_is_bridge() {
 
 function device_is_bridge_attached() {
        local device=${1}
-
        [ -d "${SYS_CLASS_NET}/${device}/brport" ]
 }
 
-# Check if the device is a virtual device
-function device_is_virtual() {
+function device_get_bridge() {
        local device=${1}
+       assert isset device
 
-       [ -e "/proc/net/vlan/${device}" ]
+       # Check if device is attached to a bridge.
+       device_is_bridge_attached ${device} || return ${EXIT_ERROR}
+
+       local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
+       [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
+
+       local ifindex=$(<${ifindex_path})
+       assert isset ifindex
+
+       device_ifindex_to_name ${ifindex}
 }
 
-# Check if the device has virtual devices
-function device_has_virtuals() {
+# Check if the device is a vlan device
+function device_is_vlan() {
        local device=${1}
+       assert isset device
 
-       if device_is_virtual ${device}; then
-               return 1
-       fi
+       [ -e "${PROC_NET_VLAN}/${device}" ]
+}
 
-       if [ ! -e "/proc/net/vlan/config" ]; then
-               return 1
+# Check if the device has vlan devices
+function device_has_vlans() {
+       local device=${1}
+       assert isset device
+
+       if device_is_vlan ${device}; then
+               return ${EXIT_FALSE}
        fi
-       grep -q "${1}$" /proc/net/vlan/config
+
+       local vlans=$(device_get_vlans ${device})
+       [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+}
+
+function device_get_vlans() {
+       local device=${1}
+       assert isset device
+
+       # If no 8021q module has been loaded into the kernel,
+       # we cannot do anything.
+       [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
+
+       local dev spacer1 id spacer2 parent
+       while read dev spacer1 id spacer2 parent; do
+               [ "${parent}" = "${device}" ] || continue
+
+               print "${dev}"
+       done < ${PROC_NET_VLAN_CONFIG}
 }
 
 # Check if the device is a ppp device
@@ -126,11 +236,15 @@ function device_is_ppp() {
        local device=${1}
 
        local type=$(__device_get_file ${device} type)
-       if [ "${type}" = "512" ]; then
-               return ${EXIT_OK}
-       fi
 
-       return ${EXIT_ERROR}
+       [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
+}
+
+# Check if the device is a pointopoint device.
+function device_is_ptp() {
+       local device=${1}
+
+       device_has_flag ${device} 0x10
 }
 
 # Check if the device is a loopback device
@@ -140,8 +254,19 @@ function device_is_loopback() {
        [ "${device}" = "lo" ]
 }
 
+# Check if the device is a wireless device
+function device_is_wireless() {
+       local device=${1}
+
+       [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
+}
+
+function device_is_serial() {
+       serial_exists $@
+}
+
 # Check if the device is a physical network interface
-function device_is_real() {
+function device_is_ethernet() {
        local device=${1}
 
        device_is_loopback ${device} && \
@@ -156,7 +281,7 @@ function device_is_real() {
        device_is_ppp ${device} && \
                return ${EXIT_ERROR}
 
-       device_is_virtual ${device} && \
+       device_is_vlan ${device} && \
                return ${EXIT_ERROR}
 
        [ "$(__device_get_file ${device} type)" != "1" ] && \
@@ -169,7 +294,7 @@ function device_is_real() {
 function device_get_type() {
        local device=${1}
 
-       if device_is_virtual ${device}; then
+       if device_is_vlan ${device}; then
                echo "vlan"
 
        elif device_is_bonding ${device}; then
@@ -181,11 +306,23 @@ function device_get_type() {
        elif device_is_ppp ${device}; then
                echo "ppp"
 
+       elif device_is_batman_adv ${device}; then
+               echo "batman-adv"
+
+       elif device_is_batman_adv_port ${device}; then
+               echo "batman-adv-port"
+
        elif device_is_loopback ${device}; then
                echo "loopback"
 
-       elif device_is_real ${device}; then
-               echo "real"
+       elif device_is_wireless ${device}; then
+               echo "wireless"
+
+       elif device_is_ethernet ${device}; then
+               echo "ethernet"
+
+       elif device_is_serial ${device}; then
+               echo "serial"
 
        else
                echo "unknown"
@@ -194,20 +331,17 @@ function device_get_type() {
 
 function device_get_status() {
        local device=${1}
-
        assert isset device
 
-       local status=${STATUS_UNKNOWN}
+       local status=${STATUS_DOWN}
 
-       if ! device_has_carrier ${device}; then
-               status=${STATUS_NOCARRIER}
-       elif device_is_up ${device}; then
+       if device_is_up ${device}; then
                status=${STATUS_UP}
-       elif device_is_down ${device}; then
-               status=${STATUS_DOWN}
-       fi
 
-       assert isset status
+               if ! device_has_carrier ${device}; then
+                       status=${STATUS_NOCARRIER}
+               fi
+       fi
 
        echo "${status}"
 }
@@ -275,13 +409,14 @@ function device_has_carrier() {
        local device=${1}
        assert isset device
 
-       [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
+       local carrier=$(__device_get_file ${device} carrier)
+       [ "${carrier}" = "1" ]
 }
 
 function device_is_promisc() {
        local device=${1}
 
-       ip link show ${device} | grep -qE "<.*PROMISC.*>"
+       device_has_flag ${device} 0x200
 }
 
 function device_set_promisc() {
@@ -304,7 +439,7 @@ function device_is_free() {
 function device_is_used() {
        local device=${1}
 
-       device_has_virtuals ${device} && \
+       device_has_vlans ${device} && \
                return ${EXIT_OK}
        device_is_bonded ${device} && \
                return ${EXIT_OK}
@@ -368,8 +503,8 @@ function device_set_parent_up() {
        local device=${1}
        local parent
 
-       if device_is_virtual ${device}; then
-               parent=$(virtual_get_parent ${device})
+       if device_is_vlan ${device}; then
+               parent=$(vlan_get_parent ${device})
 
                device_is_up ${parent} && return ${EXIT_OK}
 
@@ -405,8 +540,8 @@ function device_set_parent_down() {
        local device=${1}
        local parent
 
-       if device_is_virtual ${device}; then
-               parent=$(virtual_get_parent ${device})
+       if device_is_vlan ${device}; then
+               parent=$(vlan_get_parent ${device})
 
                device_is_up ${parent} || return ${EXIT_OK}
 
@@ -520,7 +655,10 @@ function __device_get_file() {
        assert isset device
        assert isset file
 
-       cat ${SYS_CLASS_NET}/${device}/${file}
+       local path="${SYS_CLASS_NET}/${device}/${file}"
+       [ -r "${path}" ] || return ${EXIT_ERROR}
+
+       echo "$(<${path})"
 }
 
 function device_get_rx_bytes() {
@@ -559,40 +697,14 @@ function device_get_tx_errors() {
        __device_get_file ${device} statistics/tx_errors
 }
 
-function device_hotplug() {
+function device_get_speed() {
        local device=${1}
-       shift
-
-       assert isset device
 
-       # Just check if the device has already vanished.
-       device_exists ${device} || return ${EXIT_ERROR}
-
-       if ! device_is_free ${device}; then
-               log ERROR "The device '${device}' is in use."
-               return ${EXIT_ERROR}
-       fi
-
-       if ! device_is_real ${device}; then
-               log DEBUG "Don't rename any virtual devices."
-               return ${EXIT_OK}
-       fi
-
-       for port in $(ports_get_all); do
-               port_cmd hotplug ${port} ${device}
-               if [ $? -eq ${EXIT_OK} ]; then
-                       echo "${port}"
-                       return ${EXIT_OK}
-               fi
-       done
-
-       # If no port configuration could be found, we search for the next
-       # unused name and return that.
-       local port=$(port_find_free ${PORT_PATTERN})
+       __device_get_file ${device} speed
+}
 
-       log DEBUG "Could not find an existing port configuration for '${device}'."
-       log DEBUG "${device} --> ${port}"
+function device_get_duplex() {
+       local device=${1}
 
-       echo "${port}"
-       return ${EXIT_OK}
+       __device_get_file ${device} duplex
 }