xref: /freebsd/libexec/rc/rc.d/netif (revision 65691b2dafda23691c3989749def755a98e731ec)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
40696600cSBjoern A. Zeeb#
50696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
60696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
70696600cSBjoern A. Zeeb# are met:
80696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
90696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
100696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
110696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
120696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
130696600cSBjoern A. Zeeb#
140696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
150696600cSBjoern A. Zeeb# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160696600cSBjoern A. Zeeb# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170696600cSBjoern A. Zeeb# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
180696600cSBjoern A. Zeeb# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190696600cSBjoern A. Zeeb# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200696600cSBjoern A. Zeeb# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
210696600cSBjoern A. Zeeb# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
220696600cSBjoern A. Zeeb# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
230696600cSBjoern A. Zeeb# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
240696600cSBjoern A. Zeeb#
250696600cSBjoern A. Zeeb#
260696600cSBjoern A. Zeeb
270696600cSBjoern A. Zeeb# PROVIDE: netif
286aae3517SGleb Smirnoff# REQUIRE: FILESYSTEMS iovctl serial sysctl
29425f68e6SGleb Smirnoff# REQUIRE: hostid
30353ce816SCy Schubert# KEYWORD: nojailvnet
310696600cSBjoern A. Zeeb
320696600cSBjoern A. Zeeb. /etc/rc.subr
330696600cSBjoern A. Zeeb. /etc/network.subr
340696600cSBjoern A. Zeeb
350696600cSBjoern A. Zeebname="netif"
360696600cSBjoern A. Zeebdesc="Network interface setup"
370696600cSBjoern A. Zeebrcvar="${name}_enable"
380696600cSBjoern A. Zeebstart_cmd="netif_start"
390696600cSBjoern A. Zeebstop_cmd="netif_stop"
400696600cSBjoern A. Zeebwlanup_cmd="wlan_up"
410696600cSBjoern A. Zeebwlandown_cmd="wlan_down"
420696600cSBjoern A. Zeebcloneup_cmd="clone_up"
430696600cSBjoern A. Zeebclonedown_cmd="clone_down"
440696600cSBjoern A. Zeebclear_cmd="doclear"
450696600cSBjoern A. Zeebvnetup_cmd="vnet_up"
460696600cSBjoern A. Zeebvnetdown_cmd="vnet_down"
470696600cSBjoern A. Zeebextra_commands="cloneup clonedown clear vnetup vnetdown"
480696600cSBjoern A. Zeebcmdifn=
490696600cSBjoern A. Zeeb
500696600cSBjoern A. Zeebset_rcvar_obsolete ipv6_enable ipv6_activate_all_interfaces
510696600cSBjoern A. Zeebset_rcvar_obsolete ipv6_prefer
520696600cSBjoern A. Zeeb
530696600cSBjoern A. Zeebnetif_start()
540696600cSBjoern A. Zeeb{
550696600cSBjoern A. Zeeb	local _if
560696600cSBjoern A. Zeeb
570696600cSBjoern A. Zeeb	# Set the list of interfaces to work on.
580696600cSBjoern A. Zeeb	#
590696600cSBjoern A. Zeeb	cmdifn=$*
600696600cSBjoern A. Zeeb
610696600cSBjoern A. Zeeb	if [ -z "$cmdifn" ]; then
620696600cSBjoern A. Zeeb		#
630696600cSBjoern A. Zeeb		# We're operating as a general network start routine.
640696600cSBjoern A. Zeeb		#
650696600cSBjoern A. Zeeb
660696600cSBjoern A. Zeeb		# disable SIGINT (Ctrl-c) when running at startup
670696600cSBjoern A. Zeeb		trap : 2
680696600cSBjoern A. Zeeb	fi
690696600cSBjoern A. Zeeb
700696600cSBjoern A. Zeeb	# Create IEEE802.11 interface
710696600cSBjoern A. Zeeb	wlan_up $cmdifn
720696600cSBjoern A. Zeeb
730696600cSBjoern A. Zeeb	# Create cloned interfaces
740696600cSBjoern A. Zeeb	clone_up $cmdifn
750696600cSBjoern A. Zeeb
760696600cSBjoern A. Zeeb	# Rename interfaces.
770696600cSBjoern A. Zeeb	ifnet_rename $cmdifn
780696600cSBjoern A. Zeeb
790696600cSBjoern A. Zeeb	# Configure the interface(s).
800696600cSBjoern A. Zeeb	netif_common ifn_start $cmdifn
810696600cSBjoern A. Zeeb
820696600cSBjoern A. Zeeb	if [ -f /etc/rc.d/ipfilter ] ; then
830696600cSBjoern A. Zeeb		# Resync ipfilter
840696600cSBjoern A. Zeeb		/etc/rc.d/ipfilter quietresync
850696600cSBjoern A. Zeeb	fi
860696600cSBjoern A. Zeeb	if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
870696600cSBjoern A. Zeeb		/etc/rc.d/bridge start $cmdifn
880696600cSBjoern A. Zeeb	fi
890696600cSBjoern A. Zeeb	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
900696600cSBjoern A. Zeeb		for _if in $cmdifn; do
910696600cSBjoern A. Zeeb			/etc/rc.d/routing static any $_if
920696600cSBjoern A. Zeeb		done
930696600cSBjoern A. Zeeb	fi
940696600cSBjoern A. Zeeb}
950696600cSBjoern A. Zeeb
960696600cSBjoern A. Zeebnetif_stop()
970696600cSBjoern A. Zeeb{
980696600cSBjoern A. Zeeb	_clone_down=1
990696600cSBjoern A. Zeeb	_wlan_down=1
1000696600cSBjoern A. Zeeb	netif_stop0 $*
1010696600cSBjoern A. Zeeb}
1020696600cSBjoern A. Zeeb
1030696600cSBjoern A. Zeebdoclear()
1040696600cSBjoern A. Zeeb{
1050696600cSBjoern A. Zeeb	_clone_down=
1060696600cSBjoern A. Zeeb	_wlan_down=
1070696600cSBjoern A. Zeeb	netif_stop0 $*
1080696600cSBjoern A. Zeeb}
1090696600cSBjoern A. Zeeb
1100696600cSBjoern A. Zeebnetif_stop0()
1110696600cSBjoern A. Zeeb{
1120696600cSBjoern A. Zeeb	local _if
1130696600cSBjoern A. Zeeb
1140696600cSBjoern A. Zeeb	# Set the list of interfaces to work on.
1150696600cSBjoern A. Zeeb	#
1160696600cSBjoern A. Zeeb	cmdifn=$*
1170696600cSBjoern A. Zeeb
1180696600cSBjoern A. Zeeb	# Deconfigure the interface(s)
1190696600cSBjoern A. Zeeb	netif_common ifn_stop $cmdifn
1200696600cSBjoern A. Zeeb
1210696600cSBjoern A. Zeeb	# Destroy wlan interfaces
1220696600cSBjoern A. Zeeb	if [ -n "$_wlan_down" ]; then
1230696600cSBjoern A. Zeeb		wlan_down $cmdifn
1240696600cSBjoern A. Zeeb	fi
1250696600cSBjoern A. Zeeb
1260696600cSBjoern A. Zeeb	# Destroy cloned interfaces
1270696600cSBjoern A. Zeeb	if [ -n "$_clone_down" ]; then
1280696600cSBjoern A. Zeeb		clone_down $cmdifn
1290696600cSBjoern A. Zeeb	fi
1300696600cSBjoern A. Zeeb
1310696600cSBjoern A. Zeeb	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
1320696600cSBjoern A. Zeeb		for _if in $cmdifn; do
1330696600cSBjoern A. Zeeb			/etc/rc.d/routing stop any $_if
1340696600cSBjoern A. Zeeb		done
1350696600cSBjoern A. Zeeb	fi
1360696600cSBjoern A. Zeeb}
1370696600cSBjoern A. Zeeb
1380696600cSBjoern A. Zeebvnet_up()
1390696600cSBjoern A. Zeeb{
1400696600cSBjoern A. Zeeb	cmdifn=$*
1410696600cSBjoern A. Zeeb
1420696600cSBjoern A. Zeeb	netif_common ifn_vnetup $cmdifn
1430696600cSBjoern A. Zeeb}
1440696600cSBjoern A. Zeeb
1450696600cSBjoern A. Zeebvnet_down()
1460696600cSBjoern A. Zeeb{
1470696600cSBjoern A. Zeeb	cmdifn=$*
1480696600cSBjoern A. Zeeb
1490696600cSBjoern A. Zeeb	netif_common ifn_vnetdown $cmdifn
1500696600cSBjoern A. Zeeb}
1510696600cSBjoern A. Zeeb
1520696600cSBjoern A. Zeeb# netif_common routine
1530696600cSBjoern A. Zeeb#	Common configuration subroutine for network interfaces. This
154*65691b2dSRobert William Vesterman#	routine takes all the preparatory steps needed for configuring
1550696600cSBjoern A. Zeeb#	an interface and then calls $routine.
1560696600cSBjoern A. Zeebnetif_common()
1570696600cSBjoern A. Zeeb{
1580696600cSBjoern A. Zeeb	local _cooked_list _tmp_list _fail _func _ok _str _cmdifn
1590696600cSBjoern A. Zeeb
1600696600cSBjoern A. Zeeb	_func=
1610696600cSBjoern A. Zeeb
1620696600cSBjoern A. Zeeb	if [ -z "$1" ]; then
1630696600cSBjoern A. Zeeb		err 1 "netif_common(): No function name specified."
1640696600cSBjoern A. Zeeb	else
1650696600cSBjoern A. Zeeb		_func="$1"
1660696600cSBjoern A. Zeeb		shift
1670696600cSBjoern A. Zeeb	fi
1680696600cSBjoern A. Zeeb
1690696600cSBjoern A. Zeeb	# Set the scope of the command (all interfaces or just one).
1700696600cSBjoern A. Zeeb	#
1710696600cSBjoern A. Zeeb	_cooked_list=
1720696600cSBjoern A. Zeeb	_tmp_list=
1730696600cSBjoern A. Zeeb	_cmdifn=$*
1740696600cSBjoern A. Zeeb	if [ -n "$_cmdifn" ]; then
1750696600cSBjoern A. Zeeb		# Don't check that the interface(s) exist.  We need to run
1760696600cSBjoern A. Zeeb		# the down code even when the interface doesn't exist to
1770696600cSBjoern A. Zeeb		# kill off wpa_supplicant.
1780696600cSBjoern A. Zeeb		# XXXBED: is this really true or does wpa_supplicant die?
1790696600cSBjoern A. Zeeb		# if so, we should get rid of the devd entry
1800696600cSBjoern A. Zeeb		_cooked_list="$_cmdifn"
1810696600cSBjoern A. Zeeb	else
1820696600cSBjoern A. Zeeb		_cooked_list="`list_net_interfaces`"
1830696600cSBjoern A. Zeeb	fi
1840696600cSBjoern A. Zeeb
1850696600cSBjoern A. Zeeb	# Expand epair[0-9] to epair[0-9][ab].
1860696600cSBjoern A. Zeeb	for ifn in $_cooked_list; do
1870696600cSBjoern A. Zeeb	case ${ifn#epair} in
1880696600cSBjoern A. Zeeb	[0-9]*[ab])	;;	# Skip epair[0-9]*[ab].
1890696600cSBjoern A. Zeeb	[0-9]*)
1900696600cSBjoern A. Zeeb		for _str in $_cooked_list; do
1910696600cSBjoern A. Zeeb		case $_str in
1920696600cSBjoern A. Zeeb		$ifn)	_tmp_list="$_tmp_list ${ifn}a ${ifn}b" ;;
1930696600cSBjoern A. Zeeb		*)	_tmp_list="$_tmp_list ${ifn}" ;;
1940696600cSBjoern A. Zeeb		esac
1950696600cSBjoern A. Zeeb		done
1960696600cSBjoern A. Zeeb		_cooked_list=${_tmp_list# }
1970696600cSBjoern A. Zeeb	;;
1980696600cSBjoern A. Zeeb	esac
1990696600cSBjoern A. Zeeb	done
2000696600cSBjoern A. Zeeb
2010696600cSBjoern A. Zeeb	_dadwait=
2020696600cSBjoern A. Zeeb	_fail=
2030696600cSBjoern A. Zeeb	_ok=
2040696600cSBjoern A. Zeeb	for ifn in ${_cooked_list# }; do
2050696600cSBjoern A. Zeeb		# Skip if ifn does not exist.
2060696600cSBjoern A. Zeeb		case $_func in
2070696600cSBjoern A. Zeeb		ifn_stop)
2080696600cSBjoern A. Zeeb			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
2090696600cSBjoern A. Zeeb				warn "$ifn does not exist.  Skipped."
2100696600cSBjoern A. Zeeb				_fail="${_fail} ${ifn}"
2110696600cSBjoern A. Zeeb				continue
2120696600cSBjoern A. Zeeb			fi
2130696600cSBjoern A. Zeeb		;;
2140696600cSBjoern A. Zeeb		esac
2150696600cSBjoern A. Zeeb		if ${_func} ${ifn} $2; then
2160696600cSBjoern A. Zeeb			_ok="${_ok} ${ifn}"
217dc5361eaSColin Percival			if ipv6if ${ifn} && [ "${ifn}" != "lo0" ]; then
2180696600cSBjoern A. Zeeb				_dadwait=1
2190696600cSBjoern A. Zeeb			fi
2200696600cSBjoern A. Zeeb		else
2210696600cSBjoern A. Zeeb			_fail="${_fail} ${ifn}"
2220696600cSBjoern A. Zeeb		fi
2230696600cSBjoern A. Zeeb	done
2240696600cSBjoern A. Zeeb
2250696600cSBjoern A. Zeeb	# inet6 address configuration needs sleep for DAD.
2260696600cSBjoern A. Zeeb	case ${_func}:${_dadwait} in
2270696600cSBjoern A. Zeeb	ifn_start:1|ifn_vnetup:1|ifn_vnetdown:1)
2280696600cSBjoern A. Zeeb		sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
2290696600cSBjoern A. Zeeb		sleep 1
2300696600cSBjoern A. Zeeb	;;
2310696600cSBjoern A. Zeeb	esac
2320696600cSBjoern A. Zeeb
2330696600cSBjoern A. Zeeb	_str=
2340696600cSBjoern A. Zeeb	if [ -n "${_ok}" ]; then
2350696600cSBjoern A. Zeeb		case ${_func} in
2360696600cSBjoern A. Zeeb		ifn_start)
2370696600cSBjoern A. Zeeb			_str='Starting'
2380696600cSBjoern A. Zeeb		;;
2390696600cSBjoern A. Zeeb		ifn_stop)
2400696600cSBjoern A. Zeeb			_str='Stopping'
2410696600cSBjoern A. Zeeb		;;
2420696600cSBjoern A. Zeeb		ifn_vnetup)
2430696600cSBjoern A. Zeeb			_str='Moving'
2440696600cSBjoern A. Zeeb		;;
2450696600cSBjoern A. Zeeb		ifn_vnetdown)
2460696600cSBjoern A. Zeeb			_str='Reclaiming'
2470696600cSBjoern A. Zeeb		;;
2480696600cSBjoern A. Zeeb		esac
24919a6267dSJose Luis Duran		startmsg "${_str} Network:${_ok}."
2500696600cSBjoern A. Zeeb		case ${_func} in
2510696600cSBjoern A. Zeeb		ifn_vnetup)
2520696600cSBjoern A. Zeeb			# Clear _ok not to do "ifconfig $ifn"
2530696600cSBjoern A. Zeeb			# because $ifn is no longer in the current vnet.
2540696600cSBjoern A. Zeeb			_ok=
2550696600cSBjoern A. Zeeb		;;
2560696600cSBjoern A. Zeeb		esac
2570696600cSBjoern A. Zeeb		if check_startmsgs; then
2580696600cSBjoern A. Zeeb			for ifn in ${_ok}; do
2590696600cSBjoern A. Zeeb				/sbin/ifconfig ${ifn}
2600696600cSBjoern A. Zeeb			done
2610696600cSBjoern A. Zeeb		fi
2620696600cSBjoern A. Zeeb	fi
2630696600cSBjoern A. Zeeb
2640696600cSBjoern A. Zeeb	debug "The following interfaces were not configured: $_fail"
2650696600cSBjoern A. Zeeb}
2660696600cSBjoern A. Zeeb
2670696600cSBjoern A. Zeeb# Load the old "network" config file also for compatibility.
2680696600cSBjoern A. Zeeb# This is needed for mfsBSD at least.
2690696600cSBjoern A. Zeebload_rc_config network
2700696600cSBjoern A. Zeebload_rc_config $name
271f99f0ee1SAlexander Leidinger
272f99f0ee1SAlexander Leidinger# doesn't make sense to run in a svcj: config setting
273f99f0ee1SAlexander Leidingernetif_svcj="NO"
274f99f0ee1SAlexander Leidinger
2750696600cSBjoern A. Zeebrun_rc_command $*
276