#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2010 Michael Tremer & Christian Schmidt # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ############################################################################### function devicify() { local device=${1} assert isset device if device_exists ${device}; then echo "${device}" return ${EXIT_OK} fi local d for d in $(devices_get_all); do if [ "$(device_get_address ${d})" = "${device}" ]; then echo "${d}" return ${EXIT_OK} fi done return ${EXIT_ERROR} } function macify() { local device=${1} assert isset device if mac_is_valid ${device}; then echo "${device}" return ${EXIT_OK} fi if device_exists ${device}; then device_get_address ${device} return ${EXIT_OK} fi return ${EXIT_ERROR} } # Check if the device exists function device_exists() { local device=${1} # If device name was not found, exit. [ -n "${device}" ] || return ${EXIT_ERROR} # 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_matches_pattern() { local device="${1}" assert isset device local pattern="${2}" assert isset pattern pattern="^${pattern//N/[[:digit:]]+}$" [[ ${device} =~ ${pattern} ]] \ && return ${EXIT_TRUE} || return ${EXIT_FALSE} } 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 function device_is_up() { local device=${1} device_exists ${device} || return ${EXIT_ERROR} 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 slave port function device_is_batman_adv_slave() { local device="${1}" if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)" case "${status}" in "active") return ${EXIT_TRUE} ;; *) return ${EXIT_FALSE} ;; esac fi return ${EXIT_FALSE} } # Check if the device is a bonding device function device_is_bonding() { [ -d "/sys/class/net/${1}/bonding" ] } # Check if the device bonded in a bonding device function device_is_bonded() { local device=${1} [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ] } # Check if the device is a bridge function device_is_bridge() { [ -d "/sys/class/net/${1}/bridge" ] } function device_is_bridge_attached() { local device=${1} [ -d "${SYS_CLASS_NET}/${device}/brport" ] } function device_is_wireless_monitor() { local device="${1}" assert isset device device_is_wireless "${device}" && \ device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}" } function device_is_wireless_adhoc() { local device="${1}" assert isset device device_is_wireless "${device}" && \ device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}" } 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}" ] } # 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 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 function device_is_ppp() { local device=${1} 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=${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_get_phy() { local device="${1}" if device_is_wireless "${device}"; then print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" return ${EXIT_OK} fi return ${EXIT_ERROR} } function device_is_serial() { serial_exists $@ } # Check if the device is a physical network interface function device_is_ethernet() { local device=${1} device_is_loopback ${device} && \ return ${EXIT_ERROR} device_is_bonding ${device} && \ return ${EXIT_ERROR} device_is_bridge ${device} && \ return ${EXIT_ERROR} device_is_ppp ${device} && \ return ${EXIT_ERROR} device_is_vlan ${device} && \ return ${EXIT_ERROR} [ "$(__device_get_file ${device} type)" != "1" ] && \ return ${EXIT_ERROR} return ${EXIT_OK} } # Get the device type function device_get_type() { local device=${1} # If the device does not exist (happens on udev remove events), # we do not bother to run all checks. if ! device_exists "${device}"; then echo "unknown" elif device_is_vlan ${device}; then echo "vlan" elif device_is_bonding ${device}; then echo "bonding" elif device_is_bridge ${device}; then echo "bridge" elif device_is_ppp ${device}; then echo "ppp" elif device_is_batman_adv ${device}; then echo "batman-adv" elif device_is_loopback ${device}; then echo "loopback" elif device_is_wireless_adhoc ${device}; then echo "wireless-adhoc" 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" fi } function device_is_ethernet_compatible() { local device="${1}" # /sys/class/net/*/type must equal 1 for ethernet compatible devices local type="$(__device_get_file "${device}" "type")" [[ "${type}" = "1" ]] } function device_get_status() { local device=${1} assert isset device local status=${STATUS_DOWN} if device_is_up ${device}; then status=${STATUS_UP} if ! device_has_carrier ${device}; then status=${STATUS_NOCARRIER} fi fi echo "${status}" } function device_get_address() { local device=${1} cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null } function device_set_address() { assert [ $# -eq 2 ] local device="${1}" local addr="${2}" if ! device_exists "${device}"; then error "Device '${device}' does not exist." return ${EXIT_ERROR} fi # Do nothing if the address has not changed local old_addr="$(device_get_address "${device}")" if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then return ${EXIT_OK} fi log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" local up if device_is_up "${device}"; then device_set_down "${device}" up=1 fi ip link set "${device}" address "${addr}" local ret=$? if [ "${up}" = "1" ]; then device_set_up "${device}" fi if [ "${ret}" != "0" ]; then error_log "Could not set address '${addr}' on device '${device}'" fi return ${ret} } function device_get() { local device local devices for device in ${SYS_CLASS_NET}/*; do device=$(basename ${device}) # bonding_masters is no device [ "${device}" = "bonding_masters" ] && continue devices="${devices} ${device}" done echo ${devices} return ${EXIT_OK} } function devices_get_all() { device_get } # Check if a device has a cable plugged in function device_has_carrier() { local device=${1} assert isset device local carrier=$(__device_get_file ${device} carrier) [ "${carrier}" = "1" ] } function device_is_promisc() { local device=${1} 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 function device_is_free() { ! device_is_used $@ } # Check if the device is used function device_is_used() { local device=${1} device_has_vlans ${device} && \ return ${EXIT_OK} device_is_bonded ${device} && \ return ${EXIT_OK} device_is_bridge_attached ${device} && \ return ${EXIT_OK} return ${EXIT_ERROR} } function device_hash() { local device=${1} # Get mac address of device and remove all colons (:) # that will result in a hash. device=$(macify ${device}) echo "${device//:/}" } # Give the device a new name function device_set_name() { local source=$1 local destination=${2} # Check if devices exists if ! device_exists ${source} || device_exists ${destination}; then return 4 fi local up if device_is_up ${source}; then ip link set ${source} down up=1 fi ip link set ${source} name ${destination} if [ "${up}" = "1" ]; then ip link set ${destination} up fi } # Set device up function device_set_up() { local device=${1} # Silently fail if device was not found [ -z "${device}" ] && return ${EXIT_ERROR} # Do nothing if device is already up device_is_up ${device} && return ${EXIT_OK} device_set_parent_up ${device} log DEBUG "Setting up device '${device}'" ip link set ${device} up } function device_set_parent_up() { local device=${1} local parent if device_is_vlan ${device}; then parent=$(vlan_get_parent ${device}) device_is_up ${parent} && return ${EXIT_OK} log DEBUG "Setting up parent device '${parent}' of '${device}'" device_set_up ${parent} return $? fi return ${EXIT_OK} } # Set device down function device_set_down() { local device=${1} assert isset device local ret=${EXIT_OK} if device_is_up ${device}; then log DEBUG "Tearing down device '${device}'" ip link set ${device} down ret=$? fi device_set_parent_down ${device} return ${ret} } function device_set_parent_down() { local device=${1} local parent if device_is_vlan ${device}; then parent=$(vlan_get_parent ${device}) device_is_up ${parent} || return ${EXIT_OK} if device_is_free ${parent}; then log DEBUG "Tearing down parent device '${parent}' of '${device}'" device_set_down ${parent} fi fi return ${EXIT_OK} } function device_get_mtu() { local device=${1} if ! device_exists ${device}; then error "Device '${device}' does not exist." return ${EXIT_ERROR} fi echo $(<${SYS_CLASS_NET}/${device}/mtu) } # Set mtu to a device function device_set_mtu() { local device=${1} local mtu=${2} if ! device_exists ${device}; then error "Device '${device}' does not exist." return ${EXIT_ERROR} fi local oldmtu=$(device_get_mtu ${device}) if [ "${oldmtu}" = "${mtu}" ]; then # No need to set mtu. return ${EXIT_OK} fi log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}." local up if device_is_up ${device}; then device_set_down ${device} up=1 fi ip link set ${device} mtu ${mtu} local ret=$? if [ "${up}" = "1" ]; then device_set_up ${device} fi if [ "${ret}" != "0" ]; then error_log "Could not set mtu '${mtu}' on device '${device}'." fi return ${ret} } function device_adjust_mtu() { assert [ $# -eq 2 ] local device="${1}" local other_device="${2}" local mtu="$(device_get_mtu "${other_device}")" device_set_mtu "${device}" "${mtu}" } function device_discover() { local device=${1} log INFO "Running discovery process on device '${device}'." local hook for hook in $(hook_zone_get_all); do hook_zone_exec ${hook} discover ${device} done } function device_has_ip() { local device=${1} local addr=${2} 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 listmatch ${addr} $(device_get_addresses ${device}) } function device_get_addresses() { local device=${1} assert device_exists ${device} 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() { local device=${1} local file=${2} assert isset device assert isset file local path="${SYS_CLASS_NET}/${device}/${file}" [ -r "${path}" ] || return ${EXIT_ERROR} echo "$(<${path})" } function __device_set_file() { assert [ $# -eq 3 ] local device="${1}" local file="${2}" local value="${3}" local path="${SYS_CLASS_NET}/${device}/${file}" if [ ! -w "${path}" ]; then log DEBUG "Cannot write to file '${file}' (${value})" return ${EXIT_ERROR} fi echo "${value}" > "${path}" } function device_get_rx_bytes() { local device=${1} __device_get_file ${device} statistics/rx_bytes } function device_get_tx_bytes() { local device=${1} __device_get_file ${device} statistics/tx_bytes } function device_get_rx_packets() { local device=${1} __device_get_file ${device} statistics/rx_packets } function device_get_tx_packets() { local device=${1} __device_get_file ${device} statistics/tx_packets } function device_get_rx_errors() { local device=${1} __device_get_file ${device} statistics/rx_errors } function device_get_tx_errors() { local device=${1} __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 } function device_get_link_string() { local device="${1}" assert isset device local s local speed="$(device_get_speed "${device}")" if isset speed; then list_append s "${speed} MBit/s" fi local duplex="$(device_get_duplex "${device}")" if isset duplex; then list_append s "${duplex} duplex" fi print "${s}" }