2009-10-08 03:40:19 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
. /etc/rc.conf
|
|
|
|
. /etc/rc.d/functions
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
for s in wireless bonding bridges dhcpcd; do
|
|
|
|
[[ -f /etc/conf.d/$s ]] && . "/etc/conf.d/$s"
|
|
|
|
done
|
|
|
|
|
2011-06-18 04:44:09 +00:00
|
|
|
# helper function to determine if legacy network support is needed
|
|
|
|
need_legacy() {
|
2011-08-08 04:11:13 +00:00
|
|
|
# complain when `interface' is unset and `INTERFACES' has profiles enabled
|
|
|
|
if [[ -z $interface && ${INTERFACES[@]##!*} ]]; then
|
2011-06-18 04:44:09 +00:00
|
|
|
return 0 # need legacy
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1 # enough present for iproute2 support
|
|
|
|
}
|
|
|
|
|
|
|
|
deprecated() {
|
2011-08-08 04:11:13 +00:00
|
|
|
printf "${C_FAIL}Warning:${C_CLEAR} Your network settings are deprecated.\n"
|
2011-06-18 04:44:09 +00:00
|
|
|
printf " Please refer to /etc/rc.conf on how to define a single wired\n"
|
|
|
|
printf " connection, or use a utility such as netcfg.\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
network_up() {
|
2011-08-08 04:11:13 +00:00
|
|
|
ip link set dev $interface up || return 1
|
2011-06-18 04:44:09 +00:00
|
|
|
|
|
|
|
if [[ $address ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
ip addr add $address/${netmask:-24} broadcast ${broadcast:-+} dev $interface || return 1
|
|
|
|
[[ $gateway ]] && { ip route add default via $gateway || return 1; }
|
2011-06-18 04:44:09 +00:00
|
|
|
else
|
2011-08-08 04:11:13 +00:00
|
|
|
dhcpcd $DHCPCD_ARGS $interface || return 1
|
2011-06-18 04:44:09 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
network_down() {
|
|
|
|
if [[ -f /var/run/dhcpcd-$interface.pid ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
dhcpcd -k $interface || return 1
|
2011-06-18 04:44:09 +00:00
|
|
|
else
|
2011-08-08 04:11:13 +00:00
|
|
|
ip addr flush dev $interface || return 1
|
2011-06-18 04:44:09 +00:00
|
|
|
fi
|
|
|
|
|
2011-08-08 04:11:13 +00:00
|
|
|
ip link set dev $interface down || return 1
|
2011-06-18 04:44:09 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
ifup() {
|
|
|
|
local ifcfg=${!1}
|
|
|
|
|
|
|
|
if [[ ! $1 ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
echo "usage: $0 ifup <interface_name>"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2010-04-05 01:16:29 +00:00
|
|
|
# Get the name of the interface from the first token in the string
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $ifcfg = dhcp ]]; then
|
|
|
|
ifname=$1
|
2010-04-05 01:16:29 +00:00
|
|
|
else
|
|
|
|
ifname=${ifcfg%% *}
|
|
|
|
fi
|
|
|
|
|
2011-08-08 04:11:13 +00:00
|
|
|
ifconfig $ifname up
|
2009-10-08 03:40:19 +00:00
|
|
|
|
|
|
|
wi_up $1 || return 1
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $ifcfg = dhcp ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
# remove the .pid file if it exists
|
2011-08-08 04:11:13 +00:00
|
|
|
rm -f /var/run/dhcpcd-${1}.pid >/dev/null 2>&1
|
|
|
|
rm -f /var/run/dhcpcd-${1}.cache >/dev/null 2>&1
|
|
|
|
dhcpcd $DHCPCD_ARGS ${1}
|
2009-10-08 03:40:19 +00:00
|
|
|
else
|
2011-08-08 04:11:13 +00:00
|
|
|
ifconfig $ifcfg
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
wi_up() {
|
|
|
|
local iwcfg=wlan_$1
|
2009-10-08 03:40:19 +00:00
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ ${!iwcfg} ]] || return 0
|
|
|
|
|
2011-08-08 04:11:13 +00:00
|
|
|
iwconfig ${!iwcfg}
|
2011-05-26 04:45:00 +00:00
|
|
|
[[ $WIRELESS_TIMEOUT ]] || WIRELESS_TIMEOUT=2
|
2009-10-08 03:40:19 +00:00
|
|
|
sleep $WIRELESS_TIMEOUT
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
bssid=$(iwgetid $1 -ra)
|
|
|
|
if [[ $bssid = 00:00:00:00:00:00 ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
printhl "Could not associate $1 - try increasing WIRELESS_TIMEOUT and check network is WEP or has no security"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
ifdown() {
|
|
|
|
local ifcfg=${!1}
|
|
|
|
|
|
|
|
if [[ ! $1 ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
echo "usage: $0 ifdown <interface_name>"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
|
|
|
|
if [[ $ifcfg = dhcp && -f /var/run/dhcpcd-${1}.pid ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
dhcpcd -k ${1} >/dev/null 2>&1
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
# Always bring the interface itself down
|
2011-08-08 04:11:13 +00:00
|
|
|
ifconfig ${1} down >/dev/null 2>&1
|
2009-10-08 03:40:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
iflist() {
|
2009-10-08 03:40:19 +00:00
|
|
|
for ifline in ${INTERFACES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $ifline = ${ifline#!} ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
printf " $ifline:\t"
|
|
|
|
else
|
|
|
|
printf "$ifline:\t"
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
echo ${!ifline#!}
|
2009-10-08 03:40:19 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
rtup() {
|
|
|
|
local routecfg=${!1}
|
|
|
|
|
|
|
|
if [[ ! $1 ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
echo "usage: $0 rtup <route_name>"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
|
|
|
|
if [[ $routecfg =~ :: ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
route -A inet6 add $routecfg
|
2009-10-08 03:40:19 +00:00
|
|
|
else
|
2011-08-08 04:11:13 +00:00
|
|
|
route add $routecfg
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
rtdown() {
|
|
|
|
local routecfg=${!1}
|
|
|
|
|
|
|
|
if [[ ! $1 ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
echo "usage: $0 rtdown <route_name>"
|
|
|
|
return 1
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
|
|
|
|
if [[ $routecfg =~ :: ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
route -A inet6 del $routecfg
|
2009-10-08 03:40:19 +00:00
|
|
|
else
|
2011-08-08 04:11:13 +00:00
|
|
|
route del $routecfg
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
rtlist() {
|
2009-10-08 03:40:19 +00:00
|
|
|
for rtline in ${ROUTES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $rtline = ${rtline#!} ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
printf " $rtline:\t"
|
|
|
|
else
|
|
|
|
printf "$rtline:\t"
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
echo ${!rtline#!}
|
2009-10-08 03:40:19 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
bond_up() {
|
2009-10-08 03:40:19 +00:00
|
|
|
for ifline in ${BOND_INTERFACES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $ifline = ${ifline#!} ]]; then
|
|
|
|
bondcfg="bond_$ifline"
|
|
|
|
if [[ ${!bondcfg} ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
ifenslave $ifline ${!bondcfg} || error=1
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
bond_down() {
|
2009-10-08 03:40:19 +00:00
|
|
|
for ifline in ${BOND_INTERFACES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $ifline = ${ifline#!} ]]; then
|
|
|
|
bondcfg="bond_$ifline"
|
2011-08-08 04:11:13 +00:00
|
|
|
ifenslave -d $ifline ${!bondcfg} || error=1
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
bridge_up() {
|
2009-10-08 03:40:19 +00:00
|
|
|
for br in ${BRIDGE_INTERFACES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $br = ${br#!} ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
# if the bridge already exists, remove it
|
2011-08-08 04:11:13 +00:00
|
|
|
if [[ $(ifconfig $br 2>/dev/null) ]]; then
|
|
|
|
ifconfig $br down
|
|
|
|
brctl delbr $br
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
2011-08-08 04:11:13 +00:00
|
|
|
brctl addbr $br
|
2011-05-26 04:45:00 +00:00
|
|
|
brifs="bridge_$br"
|
|
|
|
for brif in ${!brifs}; do
|
|
|
|
if [[ $brif = ${brif#!} ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
for ifline in ${BOND_INTERFACES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $brif = $ifline && $ifline = ${ifline#!} ]]; then
|
2009-10-08 03:40:19 +00:00
|
|
|
ifup $ifline
|
2011-05-26 04:45:00 +00:00
|
|
|
bondcfg="bond_$ifline"
|
2011-08-08 04:11:13 +00:00
|
|
|
ifenslave $ifline ${!bondcfg} || error=1
|
2011-05-26 04:45:00 +00:00
|
|
|
unset bond_$ifline
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2011-08-08 04:11:13 +00:00
|
|
|
brctl addif $br $brif || error=1
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
bridge_down() {
|
2009-10-08 03:40:19 +00:00
|
|
|
for br in ${BRIDGE_INTERFACES[@]}; do
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $br = ${br#!} ]]; then
|
2011-08-08 04:11:13 +00:00
|
|
|
brctl delbr $br
|
2009-10-08 03:40:19 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
start)
|
2011-06-18 04:44:09 +00:00
|
|
|
# deprecation check
|
|
|
|
need_legacy && deprecated
|
2009-10-08 03:40:19 +00:00
|
|
|
if ! ck_daemon network; then
|
|
|
|
echo "Network is already running. Try 'network restart'"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
stat_busy "Starting Network"
|
|
|
|
error=0
|
2011-06-18 04:44:09 +00:00
|
|
|
if need_legacy; then
|
|
|
|
# bring up bridge interfaces
|
|
|
|
bridge_up
|
|
|
|
# bring up ethernet interfaces
|
|
|
|
for ifline in ${INTERFACES[@]}; do
|
|
|
|
if [[ $ifline = ${ifline#!} ]]; then
|
|
|
|
ifup $ifline || error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# bring up bond interfaces
|
|
|
|
bond_up
|
|
|
|
# bring up routes
|
|
|
|
for rtline in "${ROUTES[@]}"; do
|
|
|
|
if [ "$rtline" = "${rtline#!}" ]; then
|
|
|
|
rtup $rtline || error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
network_up
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
if ((error == 0)); then
|
2009-10-08 03:40:19 +00:00
|
|
|
add_daemon network
|
|
|
|
stat_done
|
|
|
|
else
|
|
|
|
stat_fail
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
stop)
|
2011-06-18 04:44:09 +00:00
|
|
|
# deprecation check
|
|
|
|
need_legacy && deprecated
|
2011-05-26 04:45:00 +00:00
|
|
|
if [[ $NETWORK_PERSIST =~ yes|YES && $RUNLEVEL == [06] ]]; then
|
2011-01-02 05:56:57 +00:00
|
|
|
status "Skipping Network Shutdown" true
|
|
|
|
exit 0
|
|
|
|
fi
|
2010-07-28 19:27:38 +00:00
|
|
|
|
2009-10-08 03:40:19 +00:00
|
|
|
stat_busy "Stopping Network"
|
|
|
|
rm_daemon network
|
|
|
|
error=0
|
2011-06-18 04:44:09 +00:00
|
|
|
if need_legacy; then
|
|
|
|
for rtline in "${ROUTES[@]}"; do
|
|
|
|
if [[ $rtline = ${rtline#!} ]]; then
|
|
|
|
rtdown $rtline || error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# bring down bond interfaces
|
|
|
|
bond_down
|
|
|
|
for ifline in ${INTERFACES[@]}; do
|
|
|
|
if [[ $ifline = ${ifline#!} ]]; then
|
|
|
|
ifdown $ifline || error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# bring down bridge interfaces
|
|
|
|
bridge_down
|
|
|
|
else
|
|
|
|
network_down
|
|
|
|
fi
|
2011-05-26 04:45:00 +00:00
|
|
|
if ((error == 0)); then
|
2009-10-08 03:40:19 +00:00
|
|
|
stat_done
|
|
|
|
else
|
|
|
|
stat_fail
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
restart)
|
|
|
|
$0 stop
|
2011-08-08 04:11:13 +00:00
|
|
|
sleep 2
|
2009-10-08 03:40:19 +00:00
|
|
|
$0 start
|
|
|
|
;;
|
|
|
|
ifup|ifdown|iflist|rtup|rtdown|rtlist)
|
2011-06-18 04:44:09 +00:00
|
|
|
# deprecation check
|
2011-08-08 04:11:13 +00:00
|
|
|
deprecated
|
2009-10-08 03:40:19 +00:00
|
|
|
$1 $2
|
|
|
|
;;
|
|
|
|
*)
|
2011-05-26 04:45:00 +00:00
|
|
|
echo "usage: $0 {start|stop|restart}"
|
|
|
|
echo " $0 {ifup|ifdown|iflist|rtup|rtdown|rtlist}";;
|
2009-10-08 03:40:19 +00:00
|
|
|
esac
|
|
|
|
|
2011-05-26 04:45:00 +00:00
|
|
|
# vim: set ts=2 sw=2 noet:
|