xref: /dragonfly/etc/rc.d/netif (revision 106728aa)
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="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, e.g., DHCP client.
68	sleep 2
69}
70
71network_stop()
72{
73	# Set the list of interfaces to work on.
74	#
75	_cmdifn=$*
76
77	echo -n "Stopping network:"
78
79	# Deconfigure the interface(s)
80	network_common ifn_stop
81	echo '.'
82}
83
84# network_common routine verbose
85#	Common configuration subroutine for network interfaces. This
86#	routine takes all the preparatory steps needed for configuring
87#	an interface and then calls $routine. If $verbose is specified,
88#	it will call ifconfig(8) to show, in long format, the configured
89#	interfaces. If $verbose is not given, it will simply output the
90#	configured interface(s).
91network_common()
92{
93	_func=
94	_verbose=
95
96	if [ -z "$1" ]; then
97		err 1 "network_common(): No function name specified."
98	else
99		_func="$1"
100	fi
101	[ -n "$2" ] && _verbose=yes
102
103	# Get a list of network interfaces.
104	_ifn_list="`list_net_interfaces`"
105
106	# Set the scope of the command (all interfaces or just one).
107	#
108	_cooked_list=
109	if [ -n "$_cmdifn" ]; then
110		for i in $_cmdifn ; do
111			eval _if=\"`expr "$_ifn_list" : ".*\(${i}\).*"`\"
112			if [ -z "$_if" ]; then
113				err 1 "No such network interface: $i"
114			fi
115			_cooked_list="$_cooked_list $_if"
116		done
117	else
118		_cooked_list="$_ifn_list"
119	fi
120
121	for ifn in ${_cooked_list}; do
122		if ${_func} ${ifn} ; then
123			eval showstat_$ifn=1
124		else
125			_fail="$_fail $ifn"
126		fi
127	done
128
129	# Display interfaces configured by this script
130	#
131	for ifn in ${_cooked_list}; do
132		eval showstat=\$showstat_${ifn}
133		if [ ! -z ${showstat} ]; then
134			if [ -n "$_verbose" ]; then
135				ifconfig ${ifn} 2>/dev/null
136			else
137				echo -n " ${ifn}"
138			fi
139		fi
140	done
141	debug "The following interfaces were not configured: $_fail"
142}
143
144ifn_start()
145{
146	local ifn cfg
147	ifn="$1"
148	cfg=1
149
150	[ -z "$ifn" ] && return 1
151
152	ifscript_up ${ifn} && cfg=0
153	ifconfig_up ${ifn} && cfg=0
154	ifalias_up ${ifn} && cfg=0
155	childif_create ${ifn} && cfg=0
156
157	return $cfg
158}
159
160ifn_stop()
161{
162	local ifn cfg
163	ifn="$1"
164	cfg=1
165
166	[ -z "$ifn" ] && return 1
167
168	ifalias_down ${ifn} && cfg=0
169	ifconfig_down ${ifn} && cfg=0
170	ifscript_down ${ifn} && cfg=0
171	childif_destroy ${ifn} && cfg=0
172
173	return $cfg
174}
175
176# childif_create
177#	Create and configure child interfaces.  Return 0 if child
178#	interfaces are created.
179#
180childif_create()
181{
182	local cfg child child_vlans child_wlans create_args debug_flags ifn i
183	cfg=1
184	ifn=$1
185
186	# Create wireless interfaces
187	child_wlans=`get_if_var $ifn wlans_IF`
188
189	for child in ${child_wlans}; do
190		create_args="wlandev $ifn `get_if_var $child create_args_IF`"
191		debug_flags="`get_if_var $child wlandebug_IF`"
192
193		if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
194			ifconfig $child create ${create_args} && cfg=0
195			if [ -n "${debug_flags}" ]; then
196				wlandebug -i $child ${debug_flags}
197			fi
198		else
199			i=`ifconfig wlan create ${create_args}`
200			if [ -n "${debug_flags}" ]; then
201				wlandebug -i $i ${debug_flags}
202			fi
203			ifconfig $i name $child && cfg=0
204		fi
205		ifn_start $child
206	done
207
208	# Create vlan interfaces
209	child_vlans=`get_if_var $ifn vlans_IF`
210
211	for child in ${child_vlans}; do
212		if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
213			child="${ifn}.${child}"
214			create_args=`get_if_var $child create_args_IF`
215			ifconfig $child create ${create_args} && cfg=0
216		else
217			create_args="vlandev $ifn `get_if_var $child create_args_IF`"
218			if expr $child : 'vlan[0-9][0-9]*$' >/dev/null 2>&1; then
219				ifconfig $child create ${create_args} && cfg=0
220			else
221				i=`ifconfig vlan create ${create_args}`
222				ifconfig $i name $child && cfg=0
223			fi
224		fi
225		ifn_start $child
226	done
227
228	return ${cfg}
229}
230
231# childif_destroy
232#	Destroy child interfaces.
233#
234childif_destroy()
235{
236	local cfg child child_vlans child_wlans ifn
237	cfg=1
238
239	child_wlans=`get_if_var $ifn wlans_IF`
240	for child in ${child_wlans}; do
241		if ! `ifconfig -n $child > /dev/null 2>&1`; then
242			continue
243		fi
244		ifn_stop $child
245		ifconfig $child destroy && cfg=0
246	done
247
248	return ${cfg}
249}
250
251load_rc_config $name
252run_rc_command $*
253