2009-05-01 19:41:40 +05:30
|
|
|
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
|
2011-06-30 05:16:31 +05:30
|
|
|
# Released under the 2-clause BSD license.
|
2007-11-14 20:52:04 +05:30
|
|
|
|
2012-01-05 03:07:33 +05:30
|
|
|
_ifconfig()
|
|
|
|
{
|
|
|
|
if [ -x /bin/ifconfig ]; then
|
|
|
|
echo /bin/ifconfig
|
|
|
|
else
|
|
|
|
which ifconfig 2>/dev/null
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
ifconfig_depend()
|
|
|
|
{
|
2012-01-05 03:07:33 +05:30
|
|
|
program $(_ifconfig)
|
2007-04-05 16:48:42 +05:30
|
|
|
provide interface
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_up()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
ifconfig "${IFACE}" up
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_down()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
ifconfig "${IFACE}" down
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_exists()
|
|
|
|
{
|
2007-04-16 18:26:43 +05:30
|
|
|
grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/dev
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_ifindex()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
local line= i=-2
|
2007-11-28 21:15:03 +05:30
|
|
|
while read line; do
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( i += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
[ ${i} -lt 1 ] && continue
|
|
|
|
case "${line}" in
|
2007-04-16 18:26:43 +05:30
|
|
|
"${IFACE}:"*) echo "${i}"; return 0;;
|
2007-04-05 16:48:42 +05:30
|
|
|
esac
|
|
|
|
done < /proc/net/dev
|
2007-08-08 02:31:16 +05:30
|
|
|
|
|
|
|
# Return the next available index
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( i += 1 ))
|
2007-08-08 02:31:16 +05:30
|
|
|
echo "${i}"
|
2007-04-05 16:48:42 +05:30
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_is_wireless()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
# Support new sysfs layout
|
2009-12-06 00:55:39 +05:30
|
|
|
[ -d /sys/class/net/"${IFACE}"/wireless -o \
|
|
|
|
-d /sys/class/net/"${IFACE}"/phy80211 ] && return 0
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
[ ! -e /proc/net/wireless ] && return 1
|
2007-04-16 18:26:43 +05:30
|
|
|
grep -Eq "^[[:space:]]*${IFACE}:" /proc/net/wireless
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_set_flag()
|
|
|
|
{
|
2007-11-02 03:55:53 +05:30
|
|
|
ifconfig "${IFACE}" "$1"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_get_mac_address()
|
|
|
|
{
|
2007-04-10 16:03:44 +05:30
|
|
|
local mac=$(LC_ALL=C ifconfig "${IFACE}" | \
|
2008-04-19 12:52:30 +05:30
|
|
|
sed -n -e 's/.* \(HWaddr\|ether\) \(..:..:..:..:..:..\).*/\2/p')
|
2007-04-10 16:03:44 +05:30
|
|
|
|
|
|
|
case "${mac}" in
|
2007-11-28 21:15:03 +05:30
|
|
|
00:00:00:00:00:00);;
|
|
|
|
44:44:44:44:44:44);;
|
|
|
|
FF:FF:FF:FF:FF:FF);;
|
|
|
|
"");;
|
|
|
|
*) echo "${mac}"; return 0;;
|
2007-04-10 16:03:44 +05:30
|
|
|
esac
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_set_mac_address()
|
|
|
|
{
|
2007-04-10 16:03:44 +05:30
|
|
|
ifconfig "${IFACE}" hw ether "$1"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_get_inet_address()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- $(LC_ALL=C ifconfig "${IFACE}" |
|
2008-04-19 12:52:30 +05:30
|
|
|
sed -n -e 's/.*\(inet addr:\|inet \)\([^ ]*\).*\(Mask:\|netmask \)\([^ ]*\).*/\2 \4/p')
|
2007-05-08 22:22:26 +05:30
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
echo -n "$1"
|
|
|
|
shift
|
|
|
|
echo "/$(_netmask2cidr "$1")"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_get_inet_addresses()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
local iface=${IFACE} i=0
|
|
|
|
local addrs="$(_get_inet_address)"
|
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
while true; do
|
2007-04-05 16:48:42 +05:30
|
|
|
local IFACE="${iface}:${i}"
|
|
|
|
_exists || break
|
|
|
|
local addr="$(_get_inet_address)"
|
|
|
|
[ -n "${addr}" ] && addrs="${addrs}${addrs:+ }${addr}"
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( i += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
done
|
|
|
|
echo "${addrs}"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_cidr2netmask()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
local cidr="$1" netmask="" done=0 i=0 sum=0 cur=128
|
|
|
|
local octets= frac=
|
|
|
|
|
2011-11-11 08:16:08 +05:30
|
|
|
local octets=$(( cidr / 8 ))
|
|
|
|
local frac=$(( cidr % 8 ))
|
2007-11-28 21:15:03 +05:30
|
|
|
while [ ${octets} -gt 0 ]; do
|
2007-04-05 16:48:42 +05:30
|
|
|
netmask="${netmask}.255"
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( octets -= 1 ))
|
|
|
|
: $(( done += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
done
|
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ ${done} -lt 4 ]; then
|
|
|
|
while [ ${i} -lt ${frac} ]; do
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( sum += cur ))
|
|
|
|
: $(( cur /= 2 ))
|
|
|
|
: $(( i += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
done
|
|
|
|
netmask="${netmask}.${sum}"
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( done += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
while [ ${done} -lt 4 ]; do
|
2007-04-05 16:48:42 +05:30
|
|
|
netmask="${netmask}.0"
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( done += 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "${netmask#.*}"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_add_address()
|
|
|
|
{
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
ifconfig "${IFACE}" "$@" 2>/dev/null
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2011-01-17 15:19:07 +05:30
|
|
|
case "$1" in
|
2007-04-05 16:48:42 +05:30
|
|
|
*:*) ifconfig "${IFACE}" inet6 add "$@"; return $?;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# IPv4 is tricky - ifconfig requires an aliased device
|
|
|
|
# for multiple addresses
|
|
|
|
local iface="${IFACE}"
|
2008-04-19 12:52:30 +05:30
|
|
|
if LC_ALL=C ifconfig "${iface}" | grep -Eq '\<inet (addr:)?.*'; then
|
2007-04-05 16:48:42 +05:30
|
|
|
# Get the last alias made for the interface and add 1 to it
|
|
|
|
i=$(ifconfig | sed '1!G;h;$!d' | grep -m 1 -o "^${iface}:[0-9]*" \
|
|
|
|
| sed -n -e 's/'"${iface}"'://p')
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( i = ${i:-0} + 1 ))
|
2007-04-05 16:48:42 +05:30
|
|
|
iface="${iface}:${i}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# ifconfig doesn't like CIDR addresses
|
|
|
|
local ip="${1%%/*}" cidr="${1##*/}" netmask=
|
|
|
|
if [ -n "${cidr}" -a "${cidr}" != "${ip}" ]; then
|
|
|
|
netmask="$(_cidr2netmask "${cidr}")"
|
|
|
|
shift
|
|
|
|
set -- "${ip}" netmask "${netmask}" "$@"
|
|
|
|
fi
|
|
|
|
|
2007-07-10 20:37:18 +05:30
|
|
|
local arg= cmd=
|
2007-11-28 21:15:03 +05:30
|
|
|
while [ -n "$1" ]; do
|
2007-07-10 20:37:18 +05:30
|
|
|
case "$1" in
|
|
|
|
brd)
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "$2" = "+" ]; then
|
2007-07-10 20:37:18 +05:30
|
|
|
shift
|
|
|
|
else
|
|
|
|
cmd="${cmd} broadcast"
|
|
|
|
fi
|
|
|
|
;;
|
2010-12-16 02:07:50 +05:30
|
|
|
peer) cmd="${cmd} pointopoint";;
|
2007-11-28 21:15:03 +05:30
|
|
|
*) cmd="${cmd} $1";;
|
2007-07-10 20:37:18 +05:30
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-10 20:37:18 +05:30
|
|
|
ifconfig "${iface}" ${cmd}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_add_route()
|
|
|
|
{
|
2009-04-20 01:35:40 +05:30
|
|
|
local inet6= family=
|
|
|
|
|
|
|
|
if [ "$1" = "-A" -o "$1" = "-f" -o "$1" = "-family" ]; then
|
|
|
|
family="-A $2"
|
|
|
|
shift; shift
|
2011-12-13 14:13:18 +05:30
|
|
|
elif [ "$1" = "-4" ]; then
|
|
|
|
family="-A inet"
|
|
|
|
shift
|
|
|
|
elif [ "$1" = "-6" ]; then
|
|
|
|
family="-A inet6"
|
|
|
|
shift
|
2009-04-20 01:35:40 +05:30
|
|
|
fi
|
2007-08-03 19:23:20 +05:30
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ -n "${metric}" ]; then
|
2007-08-03 19:23:20 +05:30
|
|
|
set -- "$@" metric ${metric}
|
|
|
|
fi
|
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ $# -eq 3 ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- "$1" "$2" gw "$3"
|
2007-11-28 21:15:03 +05:30
|
|
|
elif [ "$3" = "via" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
local one=$1 two=$2
|
2007-11-28 21:15:03 +05:30
|
|
|
shift; shift; shift
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- "${one}" "${two}" gw "$@"
|
|
|
|
fi
|
|
|
|
|
2007-08-03 19:23:20 +05:30
|
|
|
case "$@" in
|
2009-04-20 01:35:40 +05:30
|
|
|
*:*) [ "$1" = "-net" ] && shift;;
|
2007-08-03 19:23:20 +05:30
|
|
|
esac
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2009-04-20 01:35:40 +05:30
|
|
|
route ${family} add "$@" dev "${IFACE}"
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_delete_addresses()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
# We don't remove addresses from aliases
|
|
|
|
case "${IFACE}" in
|
2007-11-28 21:15:03 +05:30
|
|
|
*:*) return 0;;
|
2007-04-05 16:48:42 +05:30
|
|
|
esac
|
|
|
|
|
|
|
|
einfo "Removing addresses"
|
|
|
|
eindent
|
|
|
|
# iproute2 can add many addresses to an iface unlike ifconfig ...
|
|
|
|
# iproute2 added addresses cause problems for ifconfig
|
|
|
|
# as we delete an address, a new one appears, so we have to
|
|
|
|
# keep polling
|
2007-11-28 21:15:03 +05:30
|
|
|
while true; do
|
2007-07-10 20:37:18 +05:30
|
|
|
local addr=$(_get_inet_address)
|
2007-04-05 16:48:42 +05:30
|
|
|
[ -z "${addr}" ] && break
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "${addr}" = "127.0.0.1/8" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
# Don't delete the loopback address
|
|
|
|
[ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] && break
|
|
|
|
fi
|
2007-07-10 20:37:18 +05:30
|
|
|
einfo "${addr}"
|
2007-04-05 16:48:42 +05:30
|
|
|
ifconfig "${IFACE}" 0.0.0.0 || break
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove IPv6 addresses
|
|
|
|
local addr=
|
|
|
|
for addr in $(LC_ALL=C ifconfig "${IFACE}" | \
|
2008-04-19 12:52:30 +05:30
|
|
|
sed -n -e 's/^.*\(inet6 addr:\|inet6\) \([^ ]*\) .*\(Scope:[^L]\|scopeid [^<]*<[^l]\).*/\2/p'); do
|
2008-05-28 19:08:58 +05:30
|
|
|
if [ "${IFACE}" = "lo" ]; then
|
|
|
|
case "${addr}" in
|
|
|
|
"::1/128"|"/128") continue;;
|
|
|
|
esac
|
|
|
|
fi
|
2007-04-05 16:48:42 +05:30
|
|
|
einfo "${addr}"
|
|
|
|
ifconfig "${IFACE}" inet6 del "${addr}"
|
|
|
|
done
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_has_carrier()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_tunnel()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
iptunnel "$@"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
ifconfig_pre_start()
|
|
|
|
{
|
2009-07-09 03:08:48 +05:30
|
|
|
local tunnel=
|
|
|
|
eval tunnel=\$iptunnel_${IFVAR}
|
|
|
|
if [ -n "${tunnel}" ]; then
|
|
|
|
# Set our base metric to 1000
|
|
|
|
metric=1000
|
|
|
|
ebegin "Creating tunnel ${IFVAR}"
|
|
|
|
iptunnel add ${tunnel}
|
|
|
|
eend $? || return 1
|
|
|
|
_up
|
|
|
|
fi
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
# MTU support
|
|
|
|
local mtu=
|
|
|
|
eval mtu=\$mtu_${IFVAR}
|
|
|
|
[ -n "${mtu}" ] && ifconfig "${IFACE}" mtu "${mtu}"
|
|
|
|
|
2007-10-09 15:26:45 +05:30
|
|
|
# TX Queue Length support
|
|
|
|
local len=
|
|
|
|
eval len=\$txqueuelen_${IFVAR}
|
|
|
|
[ -n "${len}" ] && ifconfig "${IFACE}" txqueuelen "${len}"
|
|
|
|
|
2009-07-09 03:08:48 +05:30
|
|
|
return 0
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
ifconfig_post_stop()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
# Don't delete sit0 as it's a special tunnel
|
|
|
|
[ "${IFACE}" = "sit0" ] && return 0
|
|
|
|
|
|
|
|
[ -z "$(iptunnel show "${IFACE}" 2>/dev/null)" ] && return 0
|
|
|
|
|
|
|
|
ebegin "Destroying tunnel ${IFACE}"
|
|
|
|
iptunnel del "${IFACE}"
|
|
|
|
eend $?
|
|
|
|
}
|
2011-12-13 08:18:11 +05:30
|
|
|
|
|
|
|
# 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 </sys/class/net/"${iface}"/operstate
|
|
|
|
[ "x$state" = "up" ]
|
|
|
|
}
|