]> git.ipfire.org Git - people/ms/network.git/blobdiff - functions.device
hostapd: Enable WMM by default.
[people/ms/network.git] / functions.device
index 8ee7ca53d3c5f112a8ff90175ae0b32c58feddf3..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,52 +175,98 @@ 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
+
+       # 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 is a vlan device
+function device_is_vlan() {
        local device=${1}
+       assert isset device
 
-       [ -e "/proc/net/vlan/${device}" ]
+       [ -e "${PROC_NET_VLAN}/${device}" ]
 }
 
-# Check if the device has virtual devices
-function device_has_virtuals() {
+# Check if the device has vlan devices
+function device_has_vlans() {
        local device=${1}
+       assert isset device
 
-       if device_is_virtual ${device}; then
-               return 1
+       if device_is_vlan ${device}; then
+               return ${EXIT_FALSE}
        fi
 
-       if [ ! -e "/proc/net/vlan/config" ]; then
-               return 1
-       fi
-       grep -q "${1}$" /proc/net/vlan/config
+       local vlans=$(device_get_vlans ${device})
+       [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
 }
 
-function device_is_vlan() { # XXX Compat function
-       log DEBUG "Deprecated function device_is_vlan() was used."
+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}
 
-       device_is_virtual $@
+       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
 function device_is_ppp() {
        local device=${1}
 
-       ip link show ${device} 2>/dev/null | grep -qE "<.*POINTOPOINT.*>"
+       local type=$(__device_get_file ${device} type)
+
+       [ "${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
 function device_is_loopback() {
-       local device=$(devicify ${1})
+       local device=${1}
+
        [ "${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,10 @@ 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" ] && \
                return ${EXIT_ERROR}
 
        return ${EXIT_OK}
@@ -164,7 +292,7 @@ function device_is_real() {
 
 # Get the device type
 function device_get_type() {
-       local device=$(devicify ${1})
+       local device=${1}
 
        if device_is_vlan ${device}; then
                echo "vlan"
@@ -178,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"
@@ -191,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}"
 }
@@ -269,14 +406,28 @@ function devices_get_all() {
 
 # Check if a device has a cable plugged in
 function device_has_carrier() {
-       local device=$(devicify ${1})
-       [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
+       local device=${1}
+       assert isset device
+
+       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() {
+       local device=${1}
+       local state=${2}
+
+       assert device_exists ${device}
+       assert isset state
+       assert isoneof state on off
+
+       ip link set ${device} promisc ${state}
 }
 
 # Check if the device is free
@@ -286,9 +437,9 @@ function device_is_free() {
 
 # Check if the device is used
 function device_is_used() {
-       local device=$(devicify ${1})
+       local device=${1}
 
-       device_has_virtuals ${device} && \
+       device_has_vlans ${device} && \
                return ${EXIT_OK}
        device_is_bonded ${device} && \
                return ${EXIT_OK}
@@ -298,31 +449,6 @@ function device_is_used() {
        return ${EXIT_ERROR}
 }
 
-# XXX to be removed I think
-function device_get_free() {
-       local destination=${1}
-
-       # Replace + by a valid number
-       if grep -q "+$" <<<${destination}; then
-               local number=0
-               destination=$(sed -e "s/+//" <<<$destination)
-               while [ "${number}" -le "100" ]; do
-                       if ! device_exists "${destination}${number}"; then
-                               destination="${destination}${number}"
-                               break
-                       fi
-                       number=$(($number + 1))
-               done
-       fi
-       echo "${destination}"
-}
-
-function device_rename() {
-       warning_log "Called deprecated function 'device_rename'"
-
-       device_set_name $@
-}
-
 function device_hash() {
        local device=${1}
 
@@ -336,7 +462,7 @@ function device_hash() {
 # Give the device a new name
 function device_set_name() {
        local source=$1
-       local destination=$(device_get_free ${2})
+       local destination=${2}
 
        # Check if devices exists
        if ! device_exists ${source} || device_exists ${destination}; then
@@ -358,7 +484,7 @@ function device_set_name() {
 
 # Set device up
 function device_set_up() {
-       local device=$(devicify ${1})
+       local device=${1}
 
        # Silently fail if device was not found
        [ -z "${device}" ] && return ${EXIT_ERROR}
@@ -377,8 +503,8 @@ function device_set_parent_up() {
        local device=${1}
        local parent
 
-       if device_is_virtual ${device}; then
-               parent=$(device_virtual_get_parent ${device})
+       if device_is_vlan ${device}; then
+               parent=$(vlan_get_parent ${device})
 
                device_is_up ${parent} && return ${EXIT_OK}
 
@@ -393,7 +519,8 @@ function device_set_parent_up() {
 
 # Set device down
 function device_set_down() {
-       local device=$(devicify ${1})
+       local device=${1}
+       assert isset device
 
        local ret=${EXIT_OK}
 
@@ -413,8 +540,8 @@ function device_set_parent_down() {
        local device=${1}
        local parent
 
-       if device_is_virtual ${device}; then
-               parent=$(device_virtual_get_parent ${device})
+       if device_is_vlan ${device}; then
+               parent=$(vlan_get_parent ${device})
 
                device_is_up ${parent} || return ${EXIT_OK}
 
@@ -489,35 +616,36 @@ function device_discover() {
        done
 }
 
-function device_has_ipv4() {
+function device_has_ip() {
        local device=${1}
        local addr=${2}
 
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
+       assert isset addr
+       assert device_exists ${device}
+
+       # IPv6 addresses must be fully imploded
+       local protocol=$(ip_detect_protocol ${addr})
+       case "${protocol}" in
+               ipv6)
+                       addr=$(ipv6_implode ${addr})
+                       ;;
+       esac
 
-       ip addr show ${device} | grep -q -e "inet " -e "${addr}"
+       listmatch ${addr} $(device_get_addresses ${device})
 }
 
-function device_has_ipv6() {
+function device_get_addresses() {
        local device=${1}
-       local addr=${2}
-
-       if ! device_exists ${device}; then
-               error "Device '${device}' does not exist."
-               return ${EXIT_ERROR}
-       fi
 
-       local prefix=${addr##*/}
-       addr=$(ipv6_implode ${addr%%/*})
+       assert device_exists ${device}
 
-       if [ -n "${prefix}" ]; then
-               addr="${addr}/${prefix}"
-       fi
-
-       ip addr show ${device} | grep -q "inet6 ${addr}"
+       local prot
+       local addr
+       local line
+       ip addr show ${device} | \
+               while read prot addr line; do
+                       [ "${prot:0:4}" = "inet" ] && echo "${addr}"
+               done
 }
 
 function __device_get_file() {
@@ -527,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() {
@@ -565,3 +696,15 @@ function device_get_tx_errors() {
 
        __device_get_file ${device} statistics/tx_errors
 }
+
+function device_get_speed() {
+       local device=${1}
+
+       __device_get_file ${device} speed
+}
+
+function device_get_duplex() {
+       local device=${1}
+
+       __device_get_file ${device} duplex
+}