xref: /dragonfly/etc/rc.d/netif (revision a4da4a90)
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"
40wlanup_cmd="wlan_up"
41wlandown_cmd="wlan_down"
42extra_commands="cloneup clonedown wlanup wlandown"
43_cmdifn=
44
45netif_start()
46{
47	# Set the list of interfaces to work on.
48	#
49	_cmdifn=$*
50
51	if [ -z "$_cmdifn" ]; then
52		#
53		# We're operating as a general network start routine.
54		#
55
56		# Create IEEE 802.11 interfaces
57		wlan_up
58
59		# Create cloned interfaces
60		clone_up
61
62		# Create IPv6<-->IPv4 tunnels
63		gif_up
64
65		# Rename interfaces.
66		ifnet_rename
67	fi
68
69	# Configure the interface(s).
70	netif_common ifn_start verbose
71}
72
73netif_stop()
74{
75	# Set the list of interfaces to work on.
76	#
77	_cmdifn=$*
78
79	echo -n "Stopping network:"
80
81	# Deconfigure the interface(s)
82	netif_common ifn_stop
83	echo '.'
84}
85
86# netif_common routine verbose
87#	Common configuration subroutine for network interfaces. This
88#	routine takes all the preparatory steps needed for configuring
89#	an interface and then calls $routine. If $verbose is specified,
90#	it will call ifconfig(8) to show, in long format, the configured
91#	interfaces. If $verbose is not given, it will simply output the
92#	configured interface(s).
93#
94netif_common()
95{
96	_func=
97	_verbose=
98
99	if [ -z "$1" ]; then
100		err 1 "netif_common(): No function name specified."
101	else
102		_func="$1"
103	fi
104	[ -n "$2" ] && _verbose=yes
105
106	# Get a list of network interfaces.
107	_ifn_list="`list_net_interfaces`"
108
109	# Set the scope of the command (all interfaces or just one).
110	#
111	_cooked_list=
112	if [ -n "$_cmdifn" ]; then
113		for i in $_cmdifn ; do
114			eval _if=\"`expr "$_ifn_list" : ".*\(${i}\).*"`\"
115			if [ -z "$_if" ]; then
116				err 1 "No such network interface: $i"
117			fi
118			_cooked_list="$_cooked_list $_if"
119		done
120	else
121		_cooked_list="$_ifn_list"
122	fi
123
124	for ifn in ${_cooked_list}; do
125		if ${_func} ${ifn} ; then
126			eval showstat_$ifn=1
127		else
128			_fail="$_fail $ifn"
129		fi
130	done
131
132	# Display interfaces configured by this script
133	#
134	for ifn in ${_cooked_list}; do
135		eval showstat=\$showstat_${ifn}
136		if [ ! -z ${showstat} ]; then
137			if [ -n "$_verbose" ]; then
138				ifconfig ${ifn}
139			else
140				echo -n " ${ifn}"
141			fi
142		fi
143	done
144	debug "The following interfaces were not configured: $_fail"
145}
146
147# ifn_start ifn
148#	Bring up and configure an interface.
149#
150ifn_start()
151{
152	local ifn cfg
153	ifn="$1"
154	cfg=1
155
156	[ -z "$ifn" ] && err 1 "ifn_start called without an interface"
157
158	ifscript_up ${ifn} && cfg=0
159	ifconfig_up ${ifn} && cfg=0
160	ifalias_up ${ifn} && cfg=0
161	childif_create ${ifn} && cfg=0
162
163	return $cfg
164}
165
166# ifn_stop ifn
167#	Shutdown and de-configure an interface.
168#
169ifn_stop()
170{
171	local ifn cfg
172	ifn="$1"
173	cfg=1
174
175	[ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
176
177	childif_destroy ${ifn} && cfg=0
178	ifalias_down ${ifn} && cfg=0
179	ifconfig_down ${ifn} && cfg=0
180	ifscript_down ${ifn} && cfg=0
181
182	return $cfg
183}
184
185# childif_create ifn
186#	Create and configure child interfaces.  Return 0 if child
187#	interfaces are created.
188#
189childif_create()
190{
191	local cfg child child_vlans create_args ifn i
192	cfg=1
193	ifn=$1
194
195	# Create vlan interfaces
196	child_vlans=`get_if_var $ifn vlans_IF`
197
198	for child in ${child_vlans}; do
199		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
200			child="${ifn}.${child}"
201			create_args=`get_if_var $child create_args_IF`
202			ifconfig $child create ${create_args} && cfg=0
203		else
204			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
205			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
206				ifconfig $child create ${create_args} && cfg=0
207			else
208				i=`ifconfig vlan create ${create_args}`
209				ifconfig $i name $child && cfg=0
210			fi
211		fi
212		ifn_start $child
213	done
214
215	return ${cfg}
216}
217
218# childif_destroy ifn
219#	Destroy child interfaces.
220#
221childif_destroy()
222{
223	local cfg child child_vlans ifn
224	cfg=1
225	ifn=$1
226
227	child_vlans=`get_if_var $ifn vlans_IF`
228	for child in ${child_vlans}; do
229		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
230			child="${ifn}.${child}"
231		fi
232		if ! ifexists $child; then
233			continue
234		fi
235		ifn_stop $child
236		ifconfig $child destroy && cfg=0
237	done
238
239	return ${cfg}
240}
241
242# Load the old "network" config file also for compatibility
243load_rc_config network
244load_rc_config $name
245run_rc_command $*
246