# Copyright (c) 2007-2008 Roy Marples # Released under the 2-clause BSD license. # shellcheck shell=sh disable=SC1008 ifconfig_depend() { program ifconfig provide interface } _up() { ifconfig "${IFACE}" up } _down() { ifconfig "${IFACE}" down } _exists() { [ -e /sys/class/net/"$IFACE" ] } _ifindex() { local index=-1 local f v if [ -e /sys/class/net/"${IFACE}"/ifindex ]; then index=$(cat /sys/class/net/"${IFACE}"/ifindex) else for f in /sys/class/net/*/ifindex ; do v=$(cat $f) [ $v -gt $index ] && index=$v done : $(( index += 1 )) fi echo "${index}" return 0 } _is_wireless() { # Support new sysfs layout [ -d /sys/class/net/"${IFACE}"/wireless -o \ -d /sys/class/net/"${IFACE}"/phy80211 ] && return 0 [ ! -e /proc/net/wireless ] && return 1 grep -q "^[[:space:]]*${IFACE}:" /proc/net/wireless } _set_flag() { ifconfig "${IFACE}" "$1" } _get_mac_address() { local mac=$(LC_ALL=C ifconfig "${IFACE}" | \ sed -n -e 's/.* \(HWaddr\|ether\) \(..:..:..:..:..:..\).*/\2/p') case "${mac}" in 00:00:00:00:00:00);; 44:44:44:44:44:44);; FF:FF:FF:FF:FF:FF);; "");; *) echo "${mac}"; return 0;; esac return 1 } _set_mac_address() { ifconfig "${IFACE}" hw ether "$1" } _get_inet_address() { set -- $(LC_ALL=C ifconfig "${IFACE}" | sed -n -e 's/.*\(inet addr:\|inet \)\([^ ]*\).*\(Mask:\|netmask \)\([^ ]*\).*/\2 \4/p') [ -z "$1" ] && return 1 printf '%s' "$1" shift echo "/$(_netmask2cidr "$1")" } _get_inet6_address() { set -- $(LC_ALL=C ifconfig "${IFACE}" | sed -n -e 's/.*\(inet6 addr:\|inet6\) \([^ /]*\)\(\/\| *prefixlen \)\([^ ]*\).*/\2\/\4/p') [ -z "$1" ] && return 1 echo -n "$1" } _get_addresses_fn() { local fn="$1" local iface=${IFACE} i=0 local addrs="$($fn)" while true; do local IFACE="${iface}:${i}" _exists || break local addr="$($fn)" [ -n "${addr}" ] && addrs="${addrs}${addrs:+ }${addr}" : $(( i += 1 )) done echo "${addrs}" } _get_inet_addresses() { _get_addresses_fn _get_inet_address } _get_inet6_addresses() { _get_addresses_fn _get_inet6_address } _cidr2netmask() { local cidr="$1" netmask="" done=0 i=0 sum=0 cur=128 local octets= frac= local octets=$(( cidr / 8 )) local frac=$(( cidr % 8 )) while [ ${octets} -gt 0 ]; do netmask="${netmask}.255" : $(( octets -= 1 )) : $(( done += 1 )) done if [ ${done} -lt 4 ]; then while [ ${i} -lt ${frac} ]; do : $(( sum += cur )) : $(( cur /= 2 )) : $(( i += 1 )) done netmask="${netmask}.${sum}" : $(( done += 1 )) while [ ${done} -lt 4 ]; do netmask="${netmask}.0" : $(( done += 1 )) done fi echo "${netmask#.*}" } _add_address() { if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ]; then ifconfig "${IFACE}" "$@" 2>/dev/null return 0 fi case "$1" in *:*) ifconfig "${IFACE}" inet6 add "$@"; return $?;; esac # IPv4 is tricky - ifconfig requires an aliased device # for multiple addresses local iface="${IFACE}" if LC_ALL=C ifconfig "${iface}" | grep -Eq '\/dev/null)" ] && return 0 ebegin "Destroying tunnel ${IFACE}" iptunnel del "${IFACE}" eend $? } # Is the interface administratively/operationally up? # The 'UP' status in ifconfig/iproute2 is the administrative status # Operational state is available in iproute2 output as 'state UP', or the # operstate sysfs variable. # 0: up # 1: down # 2: invalid arguments is_admin_up() { local iface="$1" [ -z "$iface" ] && iface="$IFACE" ifconfig "${iface}" | \ sed -n '1,1{ /flags=.*[<,]UP[,>]/{ q 0 }}; q 1; ' } is_oper_up() { local iface="$1" [ -z "$iface" ] && iface="$IFACE" read state