xref: /dragonfly/etc/rc.d/netif (revision cf89a63b)
1#!/bin/sh
2#
3# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
18# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24#
25# $FreeBSD: src/etc/rc.d/netif,v 1.2 2003/06/29 05:34:41 mtm Exp $
26#
27
28# PROVIDE: netif
29# REQUIRE: atm1 cleanvar mountcritlocal serial sppp sysctl tty
30
31. /etc/rc.subr
32. /etc/network.subr
33
34name="network"
35start_cmd="network_start"
36stop_cmd="network_stop"
37cloneup_cmd="clone_up"
38clonedown_cmd="clone_down"
39extra_commands="cloneup clonedown"
40_cmdifn=
41
42network_start()
43{
44	# Set the list of interfaces to work on.
45	#
46	_cmdifn=$*
47
48	if [ -z "$_cmdifn" ]; then
49		#
50		# We're operating as a general network start routine.
51		#
52
53		# Create cloned interfaces
54		clone_up
55
56		# Create IPv6<-->IPv4 tunnels
57		gif_up
58
59		# Rename interfaces.
60		ifnet_rename
61	fi
62
63	# Configure the interface(s).
64	network_common ifn_start verbose
65
66	# Give our interfaces a little time to come up
67	# before we start pounding them.  In particular, dhclient
68	# and named can get mightily confused.
69	sleep 2
70}
71
72network_stop()
73{
74	# Set the list of interfaces to work on.
75	#
76	_cmdifn=$*
77
78	echo -n "Stopping network:"
79
80	# Deconfigure the interface(s)
81	network_common ifn_stop
82	echo '.'
83}
84
85# network_common routine verbose
86#	Common configuration subroutine for network interfaces. This
87#	routine takes all the preparatory steps needed for configuring
88#	an interface and then calls $routine. If $verbose is specified,
89#	it will call ifconfig(8) to show, in long format, the configured
90#	interfaces. If $verbose is not given, it will simply output the
91#	configured interface(s).
92network_common()
93{
94	_func=
95	_verbose=
96
97	if [ -z "$1" ]; then
98		err 1 "network_common(): No function name specified."
99	else
100		_func="$1"
101	fi
102	[ -n "$2" ] && _verbose=yes
103
104	# Get a list of network interfaces.
105	_ifn_list="`list_net_interfaces`"
106
107	# Set the scope of the command (all interfaces or just one).
108	#
109	_cooked_list=
110	if [ -n "$_cmdifn" ]; then
111		for i in $_cmdifn ; do
112			eval _if=\"`expr "$_ifn_list" : ".*\(${i}\).*"`\"
113			if [ -z "$_if" ]; then
114				err 1 "No such network interface: $i"
115			fi
116			_cooked_list="$_cooked_list $_if"
117		done
118	else
119		_cooked_list="$_ifn_list"
120	fi
121
122	for ifn in ${_cooked_list}; do
123		if ${_func} ${ifn} ; then
124			eval showstat_$ifn=1
125		else
126			_fail="$_fail $ifn"
127		fi
128	done
129
130	# Display interfaces configured by this script
131	#
132	for ifn in ${_cooked_list}; do
133		eval showstat=\$showstat_${ifn}
134		if [ ! -z ${showstat} ]; then
135			if [ -n "$_verbose" ]; then
136				ifconfig ${ifn}
137			else
138				echo -n " ${ifn}"
139			fi
140		fi
141	done
142	debug "The following interfaces were not configured: $_fail"
143}
144
145ifn_start()
146{
147	local ifn cfg
148	ifn="$1"
149	cfg=1
150
151	[ -z "$ifn" ] && return 1
152
153	ifscript_up ${ifn} && cfg=0
154	ifconfig_up ${ifn} && cfg=0
155	ifalias_up ${ifn} && cfg=0
156	ipx_up ${ifn} && cfg=0
157	childif_create ${ifn} && cfg=0
158
159	return $cfg
160}
161
162ifn_stop()
163{
164	local ifn cfg
165	ifn="$1"
166	cfg=1
167
168	[ -z "$ifn" ] && return 1
169
170	ipx_down ${ifn} && cfg=0
171	ifalias_down ${ifn} && cfg=0
172	ifconfig_down ${ifn} && cfg=0
173	ifscript_down ${ifn} && cfg=0
174	childif_destroy ${ifn} && cfg=0
175
176	return $cfg
177}
178
179# get_if_var if var [default]
180#	Return the value of the pseudo-hash corresponding to $if where
181#	$var is a string containg the sub-string "IF" which will be
182#	replaced with $if after the characters defined in _punct are
183#	replaced with '_'. If the variable is unset, replace it with
184#	$default if given.
185get_if_var()
186{
187	local _if _punct _var _default prefix suffix
188
189	if [ $# -ne 2 -a $# -ne 3 ]; then
190		err 3 'USAGE: get_if_var name var [default]'
191	fi
192
193	_if=$1
194	_punct=". - / +"
195	for _punct_c in $_punct; do
196		_if=`ltr ${_if} ${_punct_c} '_'`
197	done
198	_var=$2
199	_default=$3
200
201	prefix=${_var%%IF*}
202	suffix=${_var##*IF}
203	eval echo \${${prefix}${_if}${suffix}-${_default}}
204}
205
206# childif_create
207#	Create and configure child interfaces.  Return 0 if child
208#	interfaces are created.
209#
210childif_create()
211{
212	local cfg child child_vlans child_wlans create_args debug_flags ifn i
213	cfg=1
214	ifn=$1
215
216	# Create wireless interfaces
217	child_wlans=`get_if_var $ifn wlans_IF`
218
219	for child in ${child_wlans}; do
220		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
221		debug_flags="`get_if_var $child wlandebug_IF`"
222
223		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
224			ifconfig $child create ${create_args} && cfg=0
225			if [ -n "${debug_flags}" ]; then
226				wlandebug -i $child ${debug_flags}
227			fi
228		else
229			i=`ifconfig wlan create ${create_args}`
230			if [ -n "${debug_flags}" ]; then
231				wlandebug -i $i ${debug_flags}
232			fi
233			ifconfig $i name $child && cfg=0
234		fi
235		ifn_start $child
236	done
237
238	return ${cfg}
239}
240
241# childif_destroy
242#	Destroy child interfaces.
243#
244childif_destroy()
245{
246	local cfg child child_vlans child_wlans ifn
247	cfg=1
248
249	child_wlans=`get_if_var $ifn wlans_IF`
250	for child in ${child_wlans}; do
251		if ! `ifconfig -n $child > /dev/null 2>&1`; then
252			continue
253		fi
254		ifn_stop $child
255		ifconfig $child destroy && cfg=0
256	done
257
258	return ${cfg}
259}
260
261load_rc_config $name
262run_rc_command $*
263