2008-01-11 21:01:10 +05:30
|
|
|
# Copyright 2007-2008 Roy Marples <roy@marples.name>
|
|
|
|
# All rights reserved. Released under the 2-clause BSD license.
|
2007-11-14 20:52:04 +05:30
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
iproute2_depend()
|
|
|
|
{
|
2007-08-03 19:23:20 +05:30
|
|
|
program /sbin/ip
|
2007-04-05 16:48:42 +05:30
|
|
|
provide interface
|
|
|
|
after ifconfig
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_up()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
ip link set up dev "${IFACE}"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_down()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
ip link set down dev "${IFACE}"
|
|
|
|
}
|
|
|
|
|
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
|
2007-04-05 16:48:42 +05:30
|
|
|
i=$((${i} + 1))
|
|
|
|
[ ${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
|
|
|
|
i=$((${i} + 1))
|
|
|
|
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
|
|
|
|
[ -d /sys/class/net/"${IFACE}"/wireless ] && return 0
|
|
|
|
|
|
|
|
[ ! -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
|
|
|
local flag=$1 opt="on"
|
|
|
|
if [ "${flag#-}" != "${flag}" ]; then
|
|
|
|
flag=${flag#-}
|
|
|
|
opt="off"
|
|
|
|
fi
|
|
|
|
ip link set "${IFACE}" "${flag}" "${opt}"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_get_mac_address()
|
|
|
|
{
|
2007-04-10 16:03:44 +05:30
|
|
|
local mac=$(LC_ALL=C ip link show "${IFACE}" | sed -n \
|
|
|
|
-e 'y/abcdef/ABCDEF/' \
|
|
|
|
-e '/link\// s/^.*\<\(..:..:..:..:..:..\)\>.*/\1/p')
|
|
|
|
|
|
|
|
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
|
|
|
ip link set address "$1" dev "${IFACE}"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_get_inet_addresses()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
LC_ALL=C ip -family inet addr show "${IFACE}" | \
|
|
|
|
sed -n -e 's/.*inet \([^ ]*\).*/\1/p'
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_get_inet_address()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- $(_get_inet_addresses)
|
|
|
|
[ $# = "0" ] && return 1
|
|
|
|
echo "$1"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
ip addr add "$@" dev "${IFACE}" 2>/dev/null
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Convert an ifconfig line to iproute2
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "$2" = "netmask" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
local one="$1" three="$3"
|
2007-11-28 21:15:03 +05:30
|
|
|
shift; shift; shift
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- "${one}/$(_netmask2cidr "${three}")" "$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
#config=( "${config[@]//pointopoint/peer}" )
|
|
|
|
|
|
|
|
# Always scope lo addresses as host unless specified otherwise
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "${IFACE}" = "lo" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- "$@" "scope" "host"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# IPv4 specifics
|
|
|
|
case "$1" in
|
|
|
|
*.*.*.*)
|
|
|
|
case "$@" in
|
2007-11-28 21:15:03 +05:30
|
|
|
*" brd "*);;
|
|
|
|
*" broadcast "*);;
|
|
|
|
*) set -- "$@" brd +;;
|
2007-04-05 16:48:42 +05:30
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
ip addr add dev "${IFACE}" "$@"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_add_route()
|
|
|
|
{
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ $# -eq 3 ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
set -- "$1" "$2" via "$3"
|
2007-11-28 21:15:03 +05:30
|
|
|
elif [ "$3" = "gw" ]; 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-12-31 14:56:53 +05:30
|
|
|
set -- "${one}" "${two}" via "$@"
|
2007-04-05 16:48:42 +05:30
|
|
|
fi
|
|
|
|
|
|
|
|
local cmd= have_metric=false
|
2007-11-28 21:15:03 +05:30
|
|
|
while [ -n "$1" ]; do
|
2007-04-05 16:48:42 +05:30
|
|
|
case "$1" in
|
2007-11-28 21:15:03 +05:30
|
|
|
metric) cmd="${cmd} $1"; have_metric=true;;
|
|
|
|
netmask) cmd="${cmd}/$(_netmask2cidr "$2")"; shift;;
|
|
|
|
-host|-net);;
|
|
|
|
-A) [ "$2" = "inet6" ] && shift;;
|
|
|
|
*) cmd="${cmd} $1";;
|
2007-04-05 16:48:42 +05:30
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2007-11-28 21:15:03 +05:30
|
|
|
if ! ${have_metric} && [ -n "${metric}" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
cmd="${cmd} metric ${metric}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ip route append ${cmd} dev "${IFACE}"
|
|
|
|
eend $?
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_delete_addresses()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
ip addr flush dev "${IFACE}" scope global 2>/dev/null
|
|
|
|
ip addr flush dev "${IFACE}" scope site 2>/dev/null
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "${IFACE}" != "lo" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
ip addr flush dev "${IFACE}" scope host 2>/dev/null
|
|
|
|
fi
|
|
|
|
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
|
|
|
ip tunnel "$@"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
iproute2_pre_start()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
# MTU support
|
|
|
|
local mtu=
|
|
|
|
eval mtu=\$mtu_${IFVAR}
|
|
|
|
[ -n "${mtu}" ] && ip link set mtu "${mtu}" dev "${IFACE}"
|
|
|
|
|
2007-10-09 15:26:45 +05:30
|
|
|
# TX Queue Length support
|
|
|
|
local len=
|
|
|
|
eval len=\$txqueuelen_${IFVAR}
|
2008-04-16 20:53:57 +05:30
|
|
|
[ -n "${len}" ] && ip link set qlen "${len}" dev "${IFACE}"
|
2007-10-09 15:26:45 +05:30
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
local tunnel=
|
|
|
|
eval tunnel=\$iptunnel_${IFVAR}
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ -n "${tunnel}" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
# Set our base metric to 1000
|
|
|
|
metric=1000
|
|
|
|
|
|
|
|
ebegin "Creating tunnel ${IFVAR}"
|
2007-04-10 16:03:44 +05:30
|
|
|
ip tunnel add ${tunnel} name "${IFACE}"
|
2007-04-05 16:48:42 +05:30
|
|
|
eend $? || return 1
|
2007-04-10 16:03:44 +05:30
|
|
|
_up
|
2007-04-05 16:48:42 +05:30
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
_iproute2_ipv6_tentative()
|
|
|
|
{
|
|
|
|
LC_ALL=C ip addr show dev "${IFACE}" | \
|
2007-12-04 15:25:59 +05:30
|
|
|
grep -q "^[[:space:]]*inet6 .* tentative"
|
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
iproute2_post_start()
|
|
|
|
{
|
2007-11-02 12:56:51 +05:30
|
|
|
# Kernel may not have IP built in
|
|
|
|
if [ -e /proc/net/route ]; then
|
|
|
|
ip route flush table cache dev "${IFACE}"
|
|
|
|
fi
|
2007-12-03 23:32:57 +05:30
|
|
|
|
2007-12-04 15:25:59 +05:30
|
|
|
if _iproute2_ipv6_tentative; then
|
|
|
|
ebegin "Waiting for IPv6 addresses"
|
|
|
|
while true; do
|
|
|
|
_iproute2_ipv6_tentative || break
|
|
|
|
done
|
|
|
|
eend 0
|
|
|
|
fi
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2008-01-11 20:38:57 +05:30
|
|
|
iproute2_post_stop()
|
|
|
|
{
|
2007-04-05 16:48:42 +05:30
|
|
|
# Don't delete sit0 as it's a special tunnel
|
2007-11-28 21:15:03 +05:30
|
|
|
if [ "${IFACE}" != "sit0" ]; then
|
|
|
|
if [ -n "$(ip tunnel show "${IFACE}" 2>/dev/null)" ]; then
|
2007-04-05 16:48:42 +05:30
|
|
|
ebegin "Destroying tunnel ${IFACE}"
|
|
|
|
ip tunnel del "${IFACE}"
|
|
|
|
eend $?
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|