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