xref: /dragonfly/etc/rc.d/netif (revision ef976ecc)
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: cleanvar mountcritlocal serial sppp sysctl tty
30
31. /etc/rc.subr
32. /etc/network.subr
33
34name="netif"
35rcvar=`set_rcvar`
36start_cmd="netif_start"
37stop_cmd="netif_stop"
38cloneup_cmd="clone_up"
39clonedown_cmd="clone_down"
40extra_commands="cloneup clonedown"
41_cmdifn=
42
43netif_start()
44{
45	# Set the list of interfaces to work on.
46	#
47	_cmdifn=$*
48
49	if [ -z "$_cmdifn" ]; then
50		#
51		# We're operating as a general network start routine.
52		#
53
54		# Create cloned interfaces
55		clone_up
56
57		# Create IPv6<-->IPv4 tunnels
58		gif_up
59
60		# Rename interfaces.
61		ifnet_rename
62	fi
63
64	# Configure the interface(s).
65	netif_common ifn_start verbose
66
67	# Give our interfaces a little time to come up
68	# before we start pounding them, e.g., DHCP client.
69	sleep 2
70}
71
72netif_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	netif_common ifn_stop
82	echo '.'
83}
84
85# netif_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).
92#
93netif_common()
94{
95	_func=
96	_verbose=
97
98	if [ -z "$1" ]; then
99		err 1 "netif_common(): No function name specified."
100	else
101		_func="$1"
102	fi
103	[ -n "$2" ] && _verbose=yes
104
105	# Get a list of network interfaces.
106	_ifn_list="`list_net_interfaces`"
107
108	# Set the scope of the command (all interfaces or just one).
109	#
110	_cooked_list=
111	if [ -n "$_cmdifn" ]; then
112		for i in $_cmdifn ; do
113			eval _if=\"`expr "$_ifn_list" : ".*\(${i}\).*"`\"
114			if [ -z "$_if" ]; then
115				err 1 "No such network interface: $i"
116			fi
117			_cooked_list="$_cooked_list $_if"
118		done
119	else
120		_cooked_list="$_ifn_list"
121	fi
122
123	for ifn in ${_cooked_list}; do
124		if ${_func} ${ifn} ; then
125			eval showstat_$ifn=1
126		else
127			_fail="$_fail $ifn"
128		fi
129	done
130
131	# Display interfaces configured by this script
132	#
133	for ifn in ${_cooked_list}; do
134		eval showstat=\$showstat_${ifn}
135		if [ ! -z ${showstat} ]; then
136			if [ -n "$_verbose" ]; then
137				ifconfig ${ifn} 2>/dev/null
138			else
139				echo -n " ${ifn}"
140			fi
141		fi
142	done
143	debug "The following interfaces were not configured: $_fail"
144}
145
146# ifn_start ifn
147#	Bring up and configure an interface.
148#
149ifn_start()
150{
151	local ifn cfg
152	ifn="$1"
153	cfg=1
154
155	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
156
157	ifscript_up ${ifn} && cfg=0
158	ifconfig_up ${ifn} && cfg=0
159	ifalias_up ${ifn} && cfg=0
160	childif_create ${ifn} && cfg=0
161
162	return $cfg
163}
164
165# ifn_stop ifn
166#	Shutdown and de-configure an interface.
167#
168ifn_stop()
169{
170	local ifn cfg
171	ifn="$1"
172	cfg=1
173
174	[ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
175
176	ifalias_down ${ifn} && cfg=0
177	ifconfig_down ${ifn} && cfg=0
178	ifscript_down ${ifn} && cfg=0
179	childif_destroy ${ifn} && cfg=0
180
181	return $cfg
182}
183
184# childif_create
185#	Create and configure child interfaces.  Return 0 if child
186#	interfaces are created.
187#
188childif_create()
189{
190	local cfg child child_vlans child_wlans create_args debug_flags ifn i
191	cfg=1
192	ifn=$1
193
194	# Create wireless interfaces
195	child_wlans=`get_if_var $ifn wlans_IF`
196
197	for child in ${child_wlans}; do
198		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
199		debug_flags="`get_if_var $child wlandebug_IF`"
200
201		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
202			ifconfig $child create ${create_args} && cfg=0
203			if [ -n "${debug_flags}" ]; then
204				wlandebug -i $child ${debug_flags}
205			fi
206		else
207			i=`ifconfig wlan create ${create_args}`
208			if [ -n "${debug_flags}" ]; then
209				wlandebug -i $i ${debug_flags}
210			fi
211			ifconfig $i name $child && cfg=0
212		fi
213		ifn_start $child
214	done
215
216	# Create vlan interfaces
217	child_vlans=`get_if_var $ifn vlans_IF`
218
219	for child in ${child_vlans}; do
220		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
221			child="${ifn}.${child}"
222			create_args=`get_if_var $child create_args_IF`
223			ifconfig $child create ${create_args} && cfg=0
224		else
225			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
226			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
227				ifconfig $child create ${create_args} && cfg=0
228			else
229				i=`ifconfig vlan create ${create_args}`
230				ifconfig $i name $child && cfg=0
231			fi
232		fi
233		ifn_start $child
234	done
235
236	return ${cfg}
237}
238
239# childif_destroy
240#	Destroy child interfaces.
241#
242childif_destroy()
243{
244	local cfg child child_vlans child_wlans ifn
245	cfg=1
246
247	child_wlans=`get_if_var $ifn wlans_IF`
248	for child in ${child_wlans}; do
249		if ! `ifconfig -n $child > /dev/null 2>&1`; then
250			continue
251		fi
252		ifn_stop $child
253		ifconfig $child destroy && cfg=0
254	done
255
256	return ${cfg}
257}
258
259# Load the old "network" config file also for compatibility
260load_rc_config network
261load_rc_config $name
262run_rc_command $*
263