1113674Smtm#!/bin/sh
2113674Smtm#
3113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4113674Smtm#
5113674Smtm# Redistribution and use in source and binary forms, with or without
6113674Smtm# modification, are permitted provided that the following conditions
7113674Smtm# are met:
8113674Smtm# 1. Redistributions of source code must retain the above copyright
9113674Smtm#    notice, this list of conditions and the following disclaimer.
10113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
11113674Smtm#    notice, this list of conditions and the following disclaimer in the
12113674Smtm#    documentation and/or other materials provided with the distribution.
13113674Smtm#
14113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
15113674Smtm# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16113674Smtm# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17113674Smtm# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
18113674Smtm# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19113674Smtm# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20113674Smtm# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21113674Smtm# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22113674Smtm# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23113674Smtm# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24113674Smtm#
25113674Smtm# $FreeBSD$
26113674Smtm#
27113674Smtm
28113674Smtm# PROVIDE: netif
29240336Sobrien# REQUIRE: atm1 FILESYSTEMS serial sppp sysctl
30195026Sdougb# REQUIRE: ipfilter ipfs
31250804Sjamie# KEYWORD: nojailvnet
32113674Smtm
33113674Smtm. /etc/rc.subr
34113674Smtm. /etc/network.subr
35113674Smtm
36113674Smtmname="network"
37113674Smtmstart_cmd="network_start"
38113674Smtmstop_cmd="network_stop"
39113674Smtmcloneup_cmd="clone_up"
40113674Smtmclonedown_cmd="clone_down"
41253924Shrsclear_cmd="doclear"
42256255Shrsvnetup_cmd="vnet_up"
43256255Shrsvnetdown_cmd="vnet_down"
44256255Shrsextra_commands="cloneup clonedown clear vnetup vnetdown"
45165664Syarcmdifn=
46113674Smtm
47212579Shrsset_rcvar_obsolete ipv6_enable ipv6_activate_all_interfaces
48212579Shrsset_rcvar_obsolete ipv6_prefer
49197139Shrs
50113674Smtmnetwork_start()
51113674Smtm{
52251584Shrs	local _if
53251584Shrs
54132892Smtm	# Set the list of interfaces to work on.
55132892Smtm	#
56165664Syar	cmdifn=$*
57132892Smtm
58165664Syar	if [ -z "$cmdifn" ]; then
59117021Smtm		#
60117021Smtm		# We're operating as a general network start routine.
61117021Smtm		#
62113674Smtm
63149789Skeramida		# disable SIGINT (Ctrl-c) when running at startup
64149730Sbrooks		trap : 2
65149725Sbrooks
66166583Sflz		# Create Fast EtherChannel interfaces
67166583Sflz		fec_up
68253924Shrs	fi
69166583Sflz
70253924Shrs	# Create cloned interfaces
71253924Shrs	clone_up $cmdifn
72137070Spjd
73253924Shrs	# Rename interfaces.
74253924Shrs	ifnet_rename $cmdifn
75117021Smtm
76117021Smtm	# Configure the interface(s).
77256039Shrs	network_common ifn_start $cmdifn
78117021Smtm
79128714Sphk	if [ -f /etc/rc.d/ipfilter ] ; then
80128714Sphk		# Resync ipfilter
81175676Smtm		/etc/rc.d/ipfilter quietresync
82128714Sphk	fi
83165664Syar	if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
84165664Syar		/etc/rc.d/bridge start $cmdifn
85159138Sthompsa	fi
86251584Shrs	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
87251584Shrs		for _if in $cmdifn; do
88251584Shrs			/etc/rc.d/routing start any $_if
89251584Shrs		done
90251584Shrs	fi
91117021Smtm}
92117021Smtm
93117021Smtmnetwork_stop()
94117021Smtm{
95253924Shrs	_clone_down=1
96253924Shrs	network_stop0 $*
97253924Shrs}
98253924Shrs
99253924Shrsdoclear()
100253924Shrs{
101253924Shrs	_clone_down=
102253924Shrs	network_stop0 $*
103253924Shrs}
104253924Shrs
105253924Shrsnetwork_stop0()
106253924Shrs{
107251584Shrs	local _if
108251584Shrs
109132892Smtm	# Set the list of interfaces to work on.
110132892Smtm	#
111165664Syar	cmdifn=$*
112132892Smtm
113117021Smtm	# Deconfigure the interface(s)
114256039Shrs	network_common ifn_stop $cmdifn
115251584Shrs
116253924Shrs	# Destroy cloned interfaces
117253924Shrs	if [ -n "$_clone_down" ]; then
118253924Shrs		clone_down $cmdifn
119253924Shrs	fi
120253924Shrs
121251584Shrs	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
122251584Shrs		for _if in $cmdifn; do
123251584Shrs			/etc/rc.d/routing stop any $_if
124251584Shrs		done
125251584Shrs	fi
126117021Smtm}
127117021Smtm
128256255Shrsvnet_up()
129256255Shrs{
130256255Shrs	cmdifn=$*
131256255Shrs
132256255Shrs	network_common ifn_vnetup $cmdifn
133256255Shrs}
134256255Shrs
135256255Shrsvnet_down()
136256255Shrs{
137256255Shrs	cmdifn=$*
138256255Shrs
139256255Shrs	network_common ifn_vnetdown $cmdifn
140256255Shrs}
141256255Shrs
142178356Ssam# network_common routine
143117021Smtm#	Common configuration subroutine for network interfaces. This
144117021Smtm#	routine takes all the preparatory steps needed for configuriing
145178356Ssam#	an interface and then calls $routine.
146117021Smtmnetwork_common()
147117021Smtm{
148256039Shrs	local _cooked_list _tmp_list _fail _func _ok _str _cmdifn
149165664Syar
150117021Smtm	_func=
151117021Smtm
152117021Smtm	if [ -z "$1" ]; then
153129492Smtm		err 1 "network_common(): No function name specified."
154117021Smtm	else
155117021Smtm		_func="$1"
156256039Shrs		shift
157117021Smtm	fi
158117021Smtm
159117021Smtm	# Set the scope of the command (all interfaces or just one).
160113674Smtm	#
161132892Smtm	_cooked_list=
162256039Shrs	_tmp_list=
163256039Shrs	_cmdifn=$*
164256039Shrs	if [ -n "$_cmdifn" ]; then
165178356Ssam		# Don't check that the interface(s) exist.  We need to run
166147681Sbrooks		# the down code even when the interface doesn't exist to
167147681Sbrooks		# kill off wpa_supplicant.
168178356Ssam		# XXXBED: is this really true or does wpa_supplicant die?
169178356Ssam		# if so, we should get rid of the devd entry
170256039Shrs		_cooked_list="$_cmdifn"
171132892Smtm	else
172149725Sbrooks		_cooked_list="`list_net_interfaces`"
173117021Smtm	fi
174113674Smtm
175256039Shrs	# Expand epair[0-9] to epair[0-9][ab].
176256039Shrs	for ifn in $_cooked_list; do
177256039Shrs	case ${ifn#epair} in
178256039Shrs	[0-9]*[ab])	;;	# Skip epair[0-9]*[ab].
179256039Shrs	[0-9]*)
180256039Shrs		for _str in $_cooked_list; do
181256039Shrs		case $_str in
182256039Shrs		$ifn)	_tmp_list="$_tmp_list ${ifn}a ${ifn}b" ;;
183256039Shrs		*)	_tmp_list="$_tmp_list ${ifn}" ;;
184256039Shrs		esac
185256039Shrs		done
186256039Shrs		_cooked_list=${_tmp_list# }
187256039Shrs	;;
188256039Shrs	esac
189256039Shrs	done
190256039Shrs
191230453Shrs	_dadwait=
192165664Syar	_fail=
193179961Smtm	_ok=
194256039Shrs	for ifn in ${_cooked_list# }; do
195253924Shrs		# Skip if ifn does not exist.
196253924Shrs		case $_func in
197253924Shrs		ifn_stop)
198253924Shrs			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
199253924Shrs				warn "$ifn does not exist.  Skipped."
200253924Shrs				_fail="${_fail} ${ifn}"
201253924Shrs				continue
202253924Shrs			fi
203253924Shrs		;;
204253924Shrs		esac
205179961Smtm		if ${_func} ${ifn} $2; then
206179961Smtm			_ok="${_ok} ${ifn}"
207230453Shrs			if ipv6if ${ifn}; then
208230453Shrs				_dadwait=1
209230453Shrs			fi
210179961Smtm		else
211178356Ssam			_fail="${_fail} ${ifn}"
212117021Smtm		fi
213113674Smtm	done
214113674Smtm
215230453Shrs	# inet6 address configuration needs sleep for DAD.
216230726Shrs	case ${_func}:${_dadwait} in
217256255Shrs	ifn_start:1|ifn_vnetup:1|ifn_vnetdown:1)
218230453Shrs		sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
219230453Shrs		sleep 1
220230726Shrs	;;
221230726Shrs	esac
222230453Shrs
223179961Smtm	_str=
224179961Smtm	if [ -n "${_ok}" ]; then
225179961Smtm		case ${_func} in
226179961Smtm		ifn_start)
227179961Smtm			_str='Starting'
228256255Shrs		;;
229179961Smtm		ifn_stop)
230179961Smtm			_str='Stopping'
231256255Shrs		;;
232256255Shrs		ifn_vnetup)
233256255Shrs			_str='Moving'
234256255Shrs		;;
235256255Shrs		ifn_vnetdown)
236256255Shrs			_str='Reclaiming'
237256255Shrs		;;
238179961Smtm		esac
239179961Smtm		echo "${_str} Network:${_ok}."
240256255Shrs		case ${_func} in
241256255Shrs		ifn_vnetup)
242256255Shrs			# Clear _ok not to do "ifconfig $ifn"
243256255Shrs			# because $ifn is no longer in the current vnet.
244256255Shrs			_ok=
245256255Shrs		;;
246256255Shrs		esac
247197947Sdougb		if check_startmsgs; then
248184485Spjd			for ifn in ${_ok}; do
249184485Spjd				/sbin/ifconfig ${ifn}
250184485Spjd			done
251179961Smtm		fi
252179961Smtm	fi
253179961Smtm
254117021Smtm	debug "The following interfaces were not configured: $_fail"
255117021Smtm}
256113674Smtm
257113674Smtmload_rc_config $name
258132892Smtmrun_rc_command $*
259