netif revision 159138
117683Spst#!/bin/sh
217683Spst#
317683Spst# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
417683Spst#
517683Spst# Redistribution and use in source and binary forms, with or without
617683Spst# modification, are permitted provided that the following conditions
717683Spst# are met:
817683Spst# 1. Redistributions of source code must retain the above copyright
917683Spst#    notice, this list of conditions and the following disclaimer.
1017683Spst# 2. Redistributions in binary form must reproduce the above copyright
1117683Spst#    notice, this list of conditions and the following disclaimer in the
1217683Spst#    documentation and/or other materials provided with the distribution.
1317683Spst#
1417683Spst# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
1517683Spst# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1617683Spst# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1717683Spst# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
1817683Spst# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1917683Spst# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2026175Sfenner# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2117683Spst# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2217683Spst# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2317683Spst# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2417683Spst#
2517683Spst# $FreeBSD: head/etc/rc.d/netif 159138 2006-06-01 11:01:54Z thompsa $
2626175Sfenner#
2726175Sfenner
2898530Sfenner# PROVIDE: netif
2926175Sfenner# REQUIRE: atm1 ipfilter mountcritlocal serial sppp sysctl
3026175Sfenner# KEYWORD: nojail
3175107Sfenner
3275107Sfenner. /etc/rc.subr
3375107Sfenner. /etc/network.subr
3475107Sfenner
3517683Spstname="network"
3617683Spststart_cmd="network_start"
3717683Spststop_cmd="network_stop"
3817683Spstcloneup_cmd="clone_up"
3917683Spstclonedown_cmd="clone_down"
4017683Spstextra_commands="cloneup clonedown"
4117683Spst_cmdifn=
4217683Spst
4317683Spstnetwork_start()
4417683Spst{
4517683Spst	# Set the list of interfaces to work on.
4617683Spst	#
4717683Spst	_cmdifn=$*
4817683Spst
4917683Spst	if [ -z "$_cmdifn" ]; then
5017683Spst		#
5117683Spst		# We're operating as a general network start routine.
5217683Spst		#
5317683Spst
5417683Spst		# disable SIGINT (Ctrl-c) when running at startup
5517683Spst		trap : 2
5617683Spst
5717683Spst		# Create cloned interfaces
5817683Spst		clone_up
5917683Spst
6017683Spst		# Create IPv6<-->IPv4 tunnels
6117683Spst		gif_up
6217683Spst
6317683Spst		# Rename interfaces.
6417683Spst		ifnet_rename
6517683Spst	fi
6617683Spst
6717683Spst	# Configure the interface(s).
6817683Spst	network_common ifn_start verbose
6917683Spst
7017683Spst	if [ -f /etc/rc.d/ipfilter ] ; then
7117683Spst		# Resync ipfilter
7217683Spst		/etc/rc.d/ipfilter resync
7317683Spst	fi
7417683Spst	if [ -f /etc/rc.d/bridge -a -n "$_cmdifn" ] ; then
7517683Spst		/etc/rc.d/bridge start $_cmdifn
7617683Spst	fi
7717683Spst}
7817683Spst
7917683Spstnetwork_stop()
8017683Spst{
8117683Spst	# Set the list of interfaces to work on.
8217683Spst	#
8317683Spst	_cmdifn=$*
8417683Spst
8517683Spst	echo -n "Stopping network:"
8617683Spst
8717683Spst	# Deconfigure the interface(s)
8817683Spst	network_common ifn_stop
8917683Spst	echo '.'
9017683Spst}
9198530Sfenner
9298530Sfenner# network_common routine verbose
9398530Sfenner#	Common configuration subroutine for network interfaces. This
9498530Sfenner#	routine takes all the preparatory steps needed for configuriing
9598530Sfenner#	an interface and then calls $routine. If $verbose is specified,
9698530Sfenner#	it will call ifconfig(8) to show, in long format, the configured
9798530Sfenner#	interfaces. If $verbose is not given, it will simply output the
9898530Sfenner#	configured interface(s).
9998530Sfennernetwork_common()
10098530Sfenner{
10198530Sfenner	_func=
10298530Sfenner	_verbose=
10398530Sfenner
10498530Sfenner	if [ -z "$1" ]; then
10598530Sfenner		err 1 "network_common(): No function name specified."
10698530Sfenner	else
10798530Sfenner		_func="$1"
10817683Spst	fi
10917683Spst	[ -n "$2" ] && _verbose=yes
11017683Spst
11117683Spst	# Set the scope of the command (all interfaces or just one).
11217683Spst	#
11317683Spst	_cooked_list=
11417683Spst	if [ -n "$_cmdifn" ]; then
11517683Spst		# Don't check that the interfaces exist.  We need to run
11617683Spst		# the down code even when the interface doesn't exist to
11717683Spst		# kill off wpa_supplicant.
11817683Spst		_cooked_list="$_cmdifn"
11917683Spst	else
12017683Spst		_cooked_list="`list_net_interfaces`"
12117683Spst	fi
12217683Spst
12317683Spst	for ifn in ${_cooked_list}; do
12417683Spst		if ${_func} ${ifn} ; then
12517683Spst			eval showstat_$ifn=1
12617683Spst		else
12717683Spst			_fail="$_fail $ifn"
12817683Spst		fi
12917683Spst	done
13075107Sfenner
13117683Spst	# Display interfaces configured by this script
13217683Spst	#
13317683Spst	for ifn in ${_cooked_list}; do
13417683Spst		eval showstat=\$showstat_${ifn}
13517683Spst		if [ ! -z ${showstat} ]; then
13617683Spst			if [ -n "$_verbose" ]; then
13717683Spst				ifconfig ${ifn}
13817683Spst			else
13917683Spst				echo -n " ${ifn}"
14017683Spst			fi
14117683Spst		fi
14217683Spst	done
14317683Spst	debug "The following interfaces were not configured: $_fail"
14417683Spst}
14517683Spst
14617683Spstifn_start()
14717683Spst{
14817683Spst	local ifn cfg
14917683Spst	ifn="$1"
15017683Spst	cfg=1
15117683Spst
15217683Spst	[ -z "$ifn" ] && return 1
15317683Spst
15417683Spst	ifscript_up ${ifn} && cfg=0
15517683Spst	ifconfig_up ${ifn} && cfg=0
15617683Spst	ipv4_up ${ifn} && cfg=0
15717683Spst	ipx_up ${ifn} && cfg=0
15817683Spst
15917683Spst	return $cfg
16017683Spst}
16117683Spst
16217683Spstifn_stop()
16317683Spst{
16417683Spst	local ifn cfg
16517683Spst	ifn="$1"
16617683Spst	cfg=1
16717683Spst
16817683Spst	[ -z "$ifn" ] && return 1
16917683Spst
17017683Spst	ipx_down ${ifn} && cfg=0
17117683Spst	ipv4_down ${ifn} && cfg=0
17217683Spst	ifconfig_down ${ifn} && cfg=0
17317683Spst	ifscript_down ${ifn} && cfg=0
17417683Spst
17517683Spst	return $cfg
17617683Spst}
17717683Spst
17817683Spstload_rc_config $name
17917683Spstrun_rc_command $*
18017683Spst