2013-12-22 02:21:11 +05:30
|
|
|
#!@SBINDIR@/openrc-run
|
2015-12-05 04:22:19 +05:30
|
|
|
# Copyright (c) 2009-2015 The OpenRC Authors.
|
|
|
|
# See the Authors file at the top-level directory of this distribution and
|
|
|
|
# https://github.com/OpenRC/openrc/blob/master/AUTHORS
|
|
|
|
#
|
|
|
|
# This file is part of OpenRC. It is subject to the license terms in
|
|
|
|
# the LICENSE file found in the top-level directory of this
|
|
|
|
# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
|
|
|
|
# This file may not be copied, modified, propagated, or distributed
|
|
|
|
# except according to the terms contained in the LICENSE file.
|
2009-04-17 05:17:23 +05:30
|
|
|
|
2009-04-19 14:22:00 +05:30
|
|
|
# This script was inspired by the equivalent rc.d network from NetBSD.
|
|
|
|
|
2009-04-17 05:17:23 +05:30
|
|
|
description="Configures network interfaces."
|
|
|
|
__nl="
|
|
|
|
"
|
|
|
|
|
|
|
|
depend()
|
|
|
|
{
|
|
|
|
need localmount
|
2017-03-16 20:46:39 +05:30
|
|
|
after bootmisc clock
|
2012-08-24 21:00:04 +05:30
|
|
|
if [ -n "$(interfaces)" ]; then
|
|
|
|
provide net
|
|
|
|
fi
|
2012-07-03 08:34:22 +05:30
|
|
|
keyword -jail -prefix -vserver
|
2009-04-17 05:17:23 +05:30
|
|
|
}
|
|
|
|
|
2009-04-19 14:22:00 +05:30
|
|
|
uniqify()
|
|
|
|
{
|
|
|
|
local result= i=
|
|
|
|
for i; do
|
|
|
|
case " $result " in
|
|
|
|
*" $i "*);;
|
|
|
|
*) result="$result $i";;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
echo "${result# *}"
|
|
|
|
}
|
|
|
|
|
|
|
|
reverse()
|
|
|
|
{
|
|
|
|
local result= i=
|
|
|
|
for i; do
|
|
|
|
result="$i $result"
|
|
|
|
done
|
|
|
|
echo "${result# *}"
|
|
|
|
}
|
|
|
|
|
|
|
|
sys_interfaces()
|
2009-04-17 05:17:23 +05:30
|
|
|
{
|
2009-04-20 02:32:31 +05:30
|
|
|
case "$RC_UNAME" in
|
2009-04-17 05:17:23 +05:30
|
|
|
Linux)
|
|
|
|
local w= rest= i= cmd=$1
|
|
|
|
while read w rest; do
|
|
|
|
i=${w%%:*}
|
2013-08-19 09:17:04 +05:30
|
|
|
case "$i" in
|
|
|
|
"$w") continue ;;
|
|
|
|
lo|lo0) continue ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
2009-04-17 05:17:23 +05:30
|
|
|
if [ "$cmd" = u ]; then
|
|
|
|
ifconfig "$i" | grep -q "[ ]*UP" || continue
|
|
|
|
fi
|
|
|
|
printf "%s " "$i"
|
|
|
|
done </proc/net/dev
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
ifconfig -l$1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2010-03-23 01:32:12 +05:30
|
|
|
tentative()
|
|
|
|
{
|
|
|
|
local inet= address= rest=
|
|
|
|
|
|
|
|
case "$RC_UNAME" in
|
|
|
|
Linux)
|
2010-08-10 06:31:15 +05:30
|
|
|
[ -x /sbin/ip ] || [ -x /bin/ip ] || return 1
|
2010-03-25 01:55:42 +05:30
|
|
|
[ -n "$(ip -f inet6 addr show tentative)" ]
|
|
|
|
;;
|
2010-03-23 01:32:12 +05:30
|
|
|
*)
|
|
|
|
local inet= address= rest=
|
|
|
|
LC_ALL=C ifconfig -a | while read inet address rest; do
|
|
|
|
case "${inet}" in
|
|
|
|
inet6)
|
|
|
|
case "${rest}" in
|
|
|
|
*" "tentative*) return 2;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
[ $? = 2 ]
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-19 14:22:00 +05:30
|
|
|
auto_interfaces()
|
2009-04-17 05:17:23 +05:30
|
|
|
{
|
2009-04-19 14:22:00 +05:30
|
|
|
local ifs= c= f=
|
|
|
|
|
2009-04-20 02:32:31 +05:30
|
|
|
case "$RC_UNAME" in
|
2009-04-19 14:22:00 +05:30
|
|
|
NetBSD)
|
|
|
|
for c in $(ifconfig -C 2>/dev/null); do
|
|
|
|
for f in /etc/ifconfig.${c}[0-9]*; do
|
|
|
|
[ -f "$f" ] && printf "%s" "$f{##*.} "
|
|
|
|
done
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
for f in /etc/ifconfig.*; do
|
|
|
|
[ -f "$f" ] && printf "%s" "${f##*.} "
|
|
|
|
done
|
|
|
|
for f in /etc/ip.*; do
|
|
|
|
[ -f "$f" ] && printf "%s" "${f##*.} "
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
interfaces()
|
|
|
|
{
|
|
|
|
uniqify $(sys_interfaces "$@") $interfaces $(auto_interfaces)
|
2009-04-17 05:17:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
dumpargs()
|
|
|
|
{
|
|
|
|
local f="$1"
|
|
|
|
|
|
|
|
shift
|
|
|
|
case "$@" in
|
2009-04-19 14:22:00 +05:30
|
|
|
'') [ -f "$f" ] && cat "$f";;
|
2009-04-17 05:17:23 +05:30
|
|
|
*"$__nl"*) echo "$@";;
|
|
|
|
*)
|
|
|
|
(
|
|
|
|
set -o noglob
|
|
|
|
IFS=';'; set -- $@
|
|
|
|
IFS="$__nl"; echo "$*"
|
|
|
|
);;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2009-10-16 00:47:51 +05:30
|
|
|
intup=false
|
2009-04-17 05:17:23 +05:30
|
|
|
runip()
|
|
|
|
{
|
2009-04-20 02:29:46 +05:30
|
|
|
local int="$1" err=
|
2009-04-17 05:17:23 +05:30
|
|
|
shift
|
2009-11-11 06:01:32 +05:30
|
|
|
|
|
|
|
# Ensure we have a valid broadcast address
|
|
|
|
case "$@" in
|
|
|
|
*" broadcast "*|*" brd "*) ;;
|
2009-12-14 15:44:39 +05:30
|
|
|
*:*) ;; # Ignore IPv6
|
2009-11-11 06:01:32 +05:30
|
|
|
*) set -- "$@" brd +;;
|
|
|
|
esac
|
|
|
|
|
2009-04-20 02:29:46 +05:30
|
|
|
err=$(LC_ALL=C ip address add "$@" dev "$int" 2>&1)
|
2009-10-16 00:47:51 +05:30
|
|
|
if [ -z "$err" ]; then
|
|
|
|
# ip does not bring up the interface when adding addresses
|
2009-11-11 02:49:17 +05:30
|
|
|
if ! $intup; then
|
|
|
|
ip link set "$int" up
|
2009-10-16 00:47:51 +05:30
|
|
|
intup=true
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
fi
|
2009-04-17 05:17:23 +05:30
|
|
|
if [ "$err" = "RTNETLINK answers: File exists" ]; then
|
2009-04-20 02:32:31 +05:30
|
|
|
ip address del "$@" dev "$int" 2>/dev/null
|
2009-04-17 05:17:23 +05:30
|
|
|
fi
|
|
|
|
# Localise the error
|
2009-04-20 02:29:46 +05:30
|
|
|
ip address add "$@" dev "$int"
|
2009-04-17 05:17:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
routeflush()
|
|
|
|
{
|
2009-04-20 02:32:31 +05:30
|
|
|
if [ "$RC_UNAME" = Linux ]; then
|
2010-08-10 06:31:15 +05:30
|
|
|
if [ -x /sbin/ip ] || [ -x /bin/ip ]; then
|
2009-04-17 05:17:23 +05:30
|
|
|
ip route flush scope global
|
2009-12-15 01:01:18 +05:30
|
|
|
ip route delete default 2>/dev/null
|
2009-04-17 05:17:23 +05:30
|
|
|
else
|
|
|
|
# Sadly we also delete some link routes, but
|
|
|
|
# this cannot be helped
|
|
|
|
local dest= gate= net= flags= rest=
|
|
|
|
route -n | while read dest gate net flags rest; do
|
|
|
|
[ -z "$net" ] && continue
|
|
|
|
case "$dest" in
|
|
|
|
[0-9]*) ;;
|
|
|
|
*) continue;;
|
|
|
|
esac
|
|
|
|
local xtra= netmask="netmask $net"
|
|
|
|
case "$flags" in
|
|
|
|
U) continue;;
|
|
|
|
*H*) flags=-host; netmask=;;
|
|
|
|
*!*) flags=-net; xtra=reject;;
|
|
|
|
*) flags=-net;;
|
|
|
|
esac
|
|
|
|
route del $flags $dest $netmask $xtra
|
|
|
|
done
|
2009-12-15 01:01:18 +05:30
|
|
|
# Erase any default dev eth0 routes
|
|
|
|
route del default 2>/dev/null
|
2009-04-17 05:17:23 +05:30
|
|
|
fi
|
|
|
|
else
|
|
|
|
route -qn flush
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
runargs()
|
|
|
|
{
|
|
|
|
dumpargs "$@" | while read -r args; do
|
|
|
|
case "$args" in
|
|
|
|
''|"#"*) ;;
|
|
|
|
*)
|
|
|
|
(
|
2009-04-25 17:09:18 +05:30
|
|
|
eval vebegin "${args#*!}"
|
2009-04-17 05:17:23 +05:30
|
|
|
eval "${args#*!}"
|
|
|
|
veend $?
|
|
|
|
);;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
start()
|
|
|
|
{
|
2009-04-20 02:40:06 +05:30
|
|
|
local cr=0 r= int= intv= cmd= args= upcmd=
|
2009-10-16 16:34:20 +05:30
|
|
|
|
2009-10-25 04:33:20 +05:30
|
|
|
if [ -z "$domainname" -a -s /etc/defaultdomain ]; then
|
2009-10-16 16:34:20 +05:30
|
|
|
domainname=$(cat /etc/defaultdomain)
|
|
|
|
fi
|
|
|
|
if [ -n "$domainname" ]; then
|
|
|
|
ebegin "Setting NIS domainname: $domainname"
|
|
|
|
domainname "$domainname"
|
|
|
|
eend $?
|
|
|
|
fi
|
|
|
|
|
2009-04-17 05:17:23 +05:30
|
|
|
einfo "Starting network"
|
|
|
|
routeflush
|
|
|
|
eindent
|
2009-04-20 02:29:46 +05:30
|
|
|
for int in $(interfaces); do
|
2009-04-17 05:17:23 +05:30
|
|
|
local func= cf=
|
2009-04-20 02:40:06 +05:30
|
|
|
intv=$(shell_var "$int")
|
|
|
|
eval upcmd=\$ifup_$intv
|
2009-04-17 05:17:23 +05:30
|
|
|
for func in ip ifconfig; do
|
2009-04-27 13:21:18 +05:30
|
|
|
eval cmd=\$${func}_$intv
|
2009-04-20 02:29:46 +05:30
|
|
|
if [ -n "$cmd" -o -f /etc/"$func.$int" ]; then
|
|
|
|
cf=/etc/"$func.$int"
|
2009-04-17 05:17:23 +05:30
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
[ -n "$cf" -o -n "$upcmd" -o \
|
2009-04-20 02:29:46 +05:30
|
|
|
-f /etc/ifup."$int" -o -f "$cf" ] || continue
|
2009-04-25 23:32:28 +05:30
|
|
|
veinfo "$int"
|
2009-04-17 05:17:23 +05:30
|
|
|
case "$func" in
|
2009-10-16 00:47:51 +05:30
|
|
|
ip) func=runip; intup=false;;
|
2009-04-17 05:17:23 +05:30
|
|
|
esac
|
|
|
|
eindent
|
2009-04-20 02:29:46 +05:30
|
|
|
runargs /etc/ifup."$int" "$upcmd"
|
2009-04-17 05:17:23 +05:30
|
|
|
r=0
|
|
|
|
dumpargs "$cf" "$cmd" | while read -r args; do
|
|
|
|
case "$args" in
|
|
|
|
''|"#"*) ;;
|
|
|
|
"!"*)
|
|
|
|
(
|
2009-04-25 17:09:18 +05:30
|
|
|
eval vebegin "${args#*!}"
|
2009-04-17 05:17:23 +05:30
|
|
|
eval "${args#*!}"
|
|
|
|
veend $?
|
|
|
|
);;
|
|
|
|
*)
|
|
|
|
(
|
|
|
|
set -o noglob
|
|
|
|
eval set -- "$args"
|
|
|
|
vebegin "$@"
|
2009-04-20 02:29:46 +05:30
|
|
|
$func "$int" "$@"
|
2009-04-17 05:17:23 +05:30
|
|
|
veend $?
|
|
|
|
);;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
eoutdent
|
|
|
|
done
|
|
|
|
eoutdent
|
|
|
|
eend $cr
|
|
|
|
|
2010-03-23 01:32:12 +05:30
|
|
|
# Wait for any inet6 tentative addresses
|
|
|
|
r=5
|
|
|
|
while [ $r -gt 0 ]; do
|
|
|
|
tentative || break
|
2010-03-25 01:55:42 +05:30
|
|
|
[ $r = 5 ] && vebegin "Waiting for tentative addresses"
|
2010-03-23 01:32:12 +05:30
|
|
|
sleep 1
|
2011-11-11 08:16:08 +05:30
|
|
|
: $(( r -= 1 ))
|
2010-03-23 01:32:12 +05:30
|
|
|
done
|
2010-03-25 01:55:42 +05:30
|
|
|
if [ $r != 5 ]; then
|
|
|
|
[ $r != 0 ]
|
|
|
|
veend $?
|
|
|
|
fi
|
2011-01-17 15:19:07 +05:30
|
|
|
|
2009-04-17 05:17:23 +05:30
|
|
|
if [ -n "$defaultroute" ]; then
|
|
|
|
ebegin "Setting default route $defaultroute"
|
2009-12-19 13:33:35 +05:30
|
|
|
route add default $defaultroute
|
2009-04-17 05:17:23 +05:30
|
|
|
eend $?
|
2009-12-11 03:38:51 +05:30
|
|
|
elif [ -n "$defaultiproute" ]; then
|
2009-12-11 14:56:43 +05:30
|
|
|
ebegin "Setting default route $defaultiproute"
|
2009-12-19 13:33:35 +05:30
|
|
|
ip route add default $defaultiproute
|
2009-12-11 03:38:51 +05:30
|
|
|
eend $?
|
2009-04-17 05:17:23 +05:30
|
|
|
fi
|
|
|
|
|
2009-12-15 01:01:18 +05:30
|
|
|
if [ -n "$defaultroute6" ]; then
|
|
|
|
ebegin "Setting default route $defaultroute6"
|
|
|
|
if [ "$RC_UNAME" = Linux ]; then
|
|
|
|
routecmd="route -A inet6 add"
|
|
|
|
else
|
|
|
|
routecmd="route -inet6 add"
|
|
|
|
fi
|
|
|
|
$routecmd default $defaultroute6
|
|
|
|
eend $?
|
|
|
|
elif [ -n "$defaultiproute6" ]; then
|
|
|
|
ebegin "Setting default route $defaultiproute6"
|
2009-12-19 13:33:35 +05:30
|
|
|
ip -f inet6 route add default $defaultiproute6
|
2009-12-15 01:01:18 +05:30
|
|
|
eend $?
|
|
|
|
fi
|
|
|
|
|
2009-04-17 05:17:23 +05:30
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
stop()
|
|
|
|
{
|
2009-04-27 19:03:37 +05:30
|
|
|
# Don't stop the network at shutdown.
|
|
|
|
# We don't use the noshutdown keyword so that we are started again
|
|
|
|
# correctly if we go back to multiuser.
|
2013-02-11 21:15:14 +05:30
|
|
|
yesno ${keep_network:-YES} && yesno $RC_GOINGDOWN && return 0
|
2009-04-27 19:03:37 +05:30
|
|
|
|
2009-04-25 23:32:28 +05:30
|
|
|
local int= intv= cmd= downcmd= r=
|
2009-04-17 05:17:23 +05:30
|
|
|
einfo "Stopping network"
|
|
|
|
routeflush
|
|
|
|
eindent
|
2009-04-20 02:29:46 +05:30
|
|
|
for int in $(reverse $(interfaces u)); do
|
2013-04-08 23:16:51 +05:30
|
|
|
case "$int" in
|
|
|
|
lo|lo0) continue ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
2009-04-20 02:40:06 +05:30
|
|
|
intv=$(shell_var "$int")
|
|
|
|
eval downcmd=\$ifdown_$intv
|
|
|
|
eval cmd=\$ip_$intv
|
|
|
|
[ -z "$cmd" ] && eval cmd=\$ifconfig_$intv
|
2009-04-20 02:29:46 +05:30
|
|
|
if [ -n "$cmd" -o -f /etc/ip."$int" -o \
|
|
|
|
-f /etc/ifconfig."$int" -o \
|
|
|
|
-n "$downcmd" -o -f /etc/ifdown."$int" ];
|
2009-04-17 05:17:23 +05:30
|
|
|
then
|
2009-04-25 23:32:28 +05:30
|
|
|
veinfo "$int"
|
2009-04-20 02:29:46 +05:30
|
|
|
runargs /etc/ifdown."$int" "$downcmd"
|
2010-08-10 06:31:15 +05:30
|
|
|
if [ -x /sbin/ip ] || [ -x /bin/ip ]; then
|
2009-12-14 19:51:30 +05:30
|
|
|
# We need to do this, otherwise we may
|
|
|
|
# fail to add things correctly on restart
|
|
|
|
ip address flush dev "$int" 2>/dev/null
|
|
|
|
fi
|
2009-04-20 02:29:46 +05:30
|
|
|
ifconfig "$int" down 2>/dev/null
|
|
|
|
ifconfig "$int" destroy 2>/dev/null
|
2009-04-17 05:17:23 +05:30
|
|
|
fi
|
|
|
|
done
|
|
|
|
eoutdent
|
|
|
|
eend 0
|
|
|
|
}
|