# Copyright 2004-2007 Gentoo Foundation # Copyright 2007 Roy Marples # All rights reserved # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. ifconfig_depend() { program /sbin/ifconfig provide interface } _up() { ifconfig "${IFACE}" up } _down() { ifconfig "${IFACE}" down } _exists() { grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/dev } _ifindex() { local line= i=-2 while read line; do i=$((${i} + 1)) [ ${i} -lt 1 ] && continue case "${line}" in "${IFACE}:"*) echo "${i}"; return 0;; esac done < /proc/net/dev # Return the next available index i=$((${i} + 1)) echo "${i}" return 1 } _is_wireless() { # Support new sysfs layout [ -d /sys/class/net/"${IFACE}"/wireless ] && return 0 [ ! -e /proc/net/wireless ] && return 1 grep -Eq "^[[: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 \(..:..:..:..:..:..\).*/\1/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:\([^ ]*\).*Mask:\([^ ]*\).*/\1 \2/p') [ -z "$1" ] && return 1 echo -n "$1" shift echo "/$(_netmask2cidr "$1")" } _get_inet_addresses() { local iface=${IFACE} i=0 local addrs="$(_get_inet_address)" while true; do local IFACE="${iface}:${i}" _exists || break local addr="$(_get_inet_address)" [ -n "${addr}" ] && addrs="${addrs}${addrs:+ }${addr}" i=$((${i} + 1)) done echo "${addrs}" } _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=$((${octets} - 1)) done=$((${done} + 1)) done if [ ${done} -lt 4 ]; then while [ ${i} -lt ${frac} ]; do sum=$((${sum} + ${cur})) cur=$((${cur} / 2)) i=$((${i} + 1)) done netmask="${netmask}.${sum}" done=$((${done} + 1)) while [ ${done} -lt 4 ]; do netmask="${netmask}.0" done=$((${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 $? } # vim: set ts=4 :