grep -q "${1}$" /proc/net/vlan/config
}
-function device_is_vlan() { # XXX Compat function
- log DEBUG "Deprecated function device_is_vlan() was used."
-
- device_is_virtual $@
-}
-
# Check if the device is a ppp device
function device_is_ppp() {
local device=${1}
device_is_virtual ${device} && \
return ${EXIT_ERROR}
+ [ "$(__device_get_file ${device} type)" != "1" ] && \
+ return ${EXIT_ERROR}
+
return ${EXIT_OK}
}
function device_get_type() {
local device=$(devicify ${1})
- if device_is_vlan ${device}; then
+ if device_is_virtual ${device}; then
echo "vlan"
elif device_is_bonding ${device}; then
}
function device_get() {
- local from_config
-
- while [ $# -gt 0 ]; do
- case "${1}" in
- --from-config)
- from_config=1
- ;;
- --no-config)
- from_config=0
- ;;
- esac
- shift
- done
-
+ local device
local devices
- if [ "${from_config}" != "0" ]; then
- devices="${devices} $(device_config_list)"
- fi
+ for device in ${SYS_CLASS_NET}/*; do
+ device=$(basename ${device})
- if [ "${from_config}" != "1" ]; then
- local device
- for device in ${SYS_CLASS_NET}/*; do
- devices="${devices} $(basename ${device})"
- done
- fi
+ # bonding_masters is no device
+ [ "${device}" = "bonding_masters" ] && continue
+
+ devices="${devices} ${device}"
+ done
echo ${devices}
return ${EXIT_OK}
ip link show ${device} | grep -qE "<.*PROMISC.*>"
}
+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 $@
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}
# 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
local parent
if device_is_virtual ${device}; then
- parent=$(device_virtual_get_parent ${device})
+ parent=$(virtual_get_parent ${device})
device_is_up ${parent} && return ${EXIT_OK}
local parent
if device_is_virtual ${device}; then
- parent=$(device_virtual_get_parent ${device})
+ parent=$(virtual_get_parent ${device})
device_is_up ${parent} || return ${EXIT_OK}
done
}
-function device_create_virtual() {
- log WARN "Called deprecated function device_create_virtual"
- device_virtual_create $@
-}
-
-function device_virtual_create() {
- local port=$(devicify ${1})
- local vid=${2}
- local mac=${3}
- local newport=${port}v${vid}
-
- if [ -z "${mac}" ]; then
- mac=$(mac_generate)
- fi
-
- log INFO "Creating virtual device '${newport}' with address '${mac}'."
-
- local oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
-
- if device_exists ${oldport}; then
- local differences
-
- if [ "${oldport}" != "${newport}" ]; then
- differences="${differences} name"
- fi
- if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
- differences="${differences} address"
- fi
-
- echo "differences: $differences"
-
- if [ -n "${differences}" ]; then
- if device_is_used ${oldport}; then
- error_log "There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on."
- return ${EXIT_ERROR}
- else
- log DEBUG "There is a device '${oldport}' but it not used, so we grab it to ourselves."
- fi
- else
- log DEBUG "Device '${newport}' already exists and reflects our configuration. Go on."
-
- device_set_up ${oldport}
- return ${EXIT_OK}
- fi
-
- else
- log DEBUG "Virtual device '${newport}' does not exist, yet."
-
- vconfig set_name_type DEV_PLUS_VID_NO_PAD >/dev/null
- vconfig add ${port} ${vid} >/dev/null
-
- if [ $? -ne ${EXIT_OK} ]; then
- error_log "Could not create virtual device '${newport}'."
- return ${EXIT_ERROR}
- fi
-
- oldport=$(device_virtual_get_by_parent_and_vid ${port} ${vid})
-
- fi
-
- assert device_exists ${oldport}
-
- if ! device_exists ${oldport}; then
- error "Could not determine the created virtual device '${newport}'."
- return ${EXIT_ERROR}
- fi
-
- # The device is expected to be named like ${port}.${vid}
- # and will be renamed to the virtual schema
- device_set_name ${oldport} ${newport}
-
- if [ $? -ne ${EXIT_OK} ]; then
- error_log "Could not set name of virtual device '${newport}'."
- return ${EXIT_ERROR}
- fi
-
- assert device_exists ${newport}
-
- # Setting new mac address
- device_set_address ${newport} ${mac}
-
- if [ $? -ne ${EXIT_OK} ]; then
- error_log "Could not set address '${mac}' to virtual device '${newport}'."
- return ${EXIT_ERROR}
- fi
-
- # Bring up the new device
- device_set_up ${newport}
-
- return ${EXIT_OK}
-}
-
-function device_virtual_remove() {
- local device=$(devicify ${1})
-
- log INFO "Removing virtual device '${device}' with address '$(macify ${device})'."
-
- device_set_down ${device}
-
- vconfig rem ${device} >/dev/null
-
- if [ $? -ne ${EXIT_OK} ]; then
- error_log "Could not remote virtual device '${newport}'."
- return ${EXIT_ERROR}
- fi
-
- return ${EXIT_OK}
-}
-
-function device_virtual_get_parent() {
- local device=${1}
-
- local parent=$(grep "^${device}" < /proc/net/vlan/config | awk '{ print $NF }')
-
- if device_exists ${parent}; then
- echo "${parent}"
- return ${EXIT_OK}
- fi
-
- return ${EXIT_ERROR}
-}
-
-function device_virtual_get_by_parent_and_vid() {
- local parent=${1}
- local vid=${2}
-
- assert isset parent
- assert isset vid
-
- local v_port
- local v_id
- local v_parent
-
- assert [ -e "/proc/net/vlan/config" ]
-
- fgrep '|' < /proc/net/vlan/config | tr -d '|' | \
- while read v_port v_id v_parent; do
- if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
- echo "${v_port}"
- return ${EXIT_OK}
- fi
- done
-
- return ${EXIT_ERROR}
-}
-
-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() {