xref: /freebsd/libexec/rc/rc.d/netif (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
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#
26
27# PROVIDE: netif
28# REQUIRE: FILESYSTEMS iovctl serial sysctl
29# REQUIRE: hostid
30# KEYWORD: nojailvnet
31
32. /etc/rc.subr
33. /etc/network.subr
34
35name="netif"
36desc="Network interface setup"
37rcvar="${name}_enable"
38start_cmd="netif_start"
39stop_cmd="netif_stop"
40wlanup_cmd="wlan_up"
41wlandown_cmd="wlan_down"
42cloneup_cmd="clone_up"
43clonedown_cmd="clone_down"
44clear_cmd="doclear"
45vnetup_cmd="vnet_up"
46vnetdown_cmd="vnet_down"
47extra_commands="cloneup clonedown clear vnetup vnetdown"
48cmdifn=
49
50set_rcvar_obsolete ipv6_enable ipv6_activate_all_interfaces
51set_rcvar_obsolete ipv6_prefer
52
53netif_start()
54{
55	local _if
56
57	# Set the list of interfaces to work on.
58	#
59	cmdifn=$*
60
61	if [ -z "$cmdifn" ]; then
62		#
63		# We're operating as a general network start routine.
64		#
65
66		# disable SIGINT (Ctrl-c) when running at startup
67		trap : 2
68	fi
69
70	# Create IEEE802.11 interface
71	wlan_up $cmdifn
72
73	# Create cloned interfaces
74	clone_up $cmdifn
75
76	# Rename interfaces.
77	ifnet_rename $cmdifn
78
79	# Configure the interface(s).
80	netif_common ifn_start $cmdifn
81
82	if [ -f /etc/rc.d/ipfilter ] ; then
83		# Resync ipfilter
84		/etc/rc.d/ipfilter quietresync
85	fi
86	if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
87		/etc/rc.d/bridge start $cmdifn
88	fi
89	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
90		for _if in $cmdifn; do
91			/etc/rc.d/routing static any $_if
92		done
93	fi
94}
95
96netif_stop()
97{
98	_clone_down=1
99	_wlan_down=1
100	netif_stop0 $*
101}
102
103doclear()
104{
105	_clone_down=
106	_wlan_down=
107	netif_stop0 $*
108}
109
110netif_stop0()
111{
112	local _if
113
114	# Set the list of interfaces to work on.
115	#
116	cmdifn=$*
117
118	# Deconfigure the interface(s)
119	netif_common ifn_stop $cmdifn
120
121	# Destroy wlan interfaces
122	if [ -n "$_wlan_down" ]; then
123		wlan_down $cmdifn
124	fi
125
126	# Destroy cloned interfaces
127	if [ -n "$_clone_down" ]; then
128		clone_down $cmdifn
129	fi
130
131	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
132		for _if in $cmdifn; do
133			/etc/rc.d/routing stop any $_if
134		done
135	fi
136}
137
138vnet_up()
139{
140	cmdifn=$*
141
142	netif_common ifn_vnetup $cmdifn
143}
144
145vnet_down()
146{
147	cmdifn=$*
148
149	netif_common ifn_vnetdown $cmdifn
150}
151
152# netif_common routine
153#	Common configuration subroutine for network interfaces. This
154#	routine takes all the preparatory steps needed for configuriing
155#	an interface and then calls $routine.
156netif_common()
157{
158	local _cooked_list _tmp_list _fail _func _ok _str _cmdifn
159
160	_func=
161
162	if [ -z "$1" ]; then
163		err 1 "netif_common(): No function name specified."
164	else
165		_func="$1"
166		shift
167	fi
168
169	# Set the scope of the command (all interfaces or just one).
170	#
171	_cooked_list=
172	_tmp_list=
173	_cmdifn=$*
174	if [ -n "$_cmdifn" ]; then
175		# Don't check that the interface(s) exist.  We need to run
176		# the down code even when the interface doesn't exist to
177		# kill off wpa_supplicant.
178		# XXXBED: is this really true or does wpa_supplicant die?
179		# if so, we should get rid of the devd entry
180		_cooked_list="$_cmdifn"
181	else
182		_cooked_list="`list_net_interfaces`"
183	fi
184
185	# Expand epair[0-9] to epair[0-9][ab].
186	for ifn in $_cooked_list; do
187	case ${ifn#epair} in
188	[0-9]*[ab])	;;	# Skip epair[0-9]*[ab].
189	[0-9]*)
190		for _str in $_cooked_list; do
191		case $_str in
192		$ifn)	_tmp_list="$_tmp_list ${ifn}a ${ifn}b" ;;
193		*)	_tmp_list="$_tmp_list ${ifn}" ;;
194		esac
195		done
196		_cooked_list=${_tmp_list# }
197	;;
198	esac
199	done
200
201	_dadwait=
202	_fail=
203	_ok=
204	for ifn in ${_cooked_list# }; do
205		# Skip if ifn does not exist.
206		case $_func in
207		ifn_stop)
208			if ! ${IFCONFIG_CMD} $ifn > /dev/null 2>&1; then
209				warn "$ifn does not exist.  Skipped."
210				_fail="${_fail} ${ifn}"
211				continue
212			fi
213		;;
214		esac
215		if ${_func} ${ifn} $2; then
216			_ok="${_ok} ${ifn}"
217			if ipv6if ${ifn} && [ "${ifn}" != "lo0" ]; then
218				_dadwait=1
219			fi
220		else
221			_fail="${_fail} ${ifn}"
222		fi
223	done
224
225	# inet6 address configuration needs sleep for DAD.
226	case ${_func}:${_dadwait} in
227	ifn_start:1|ifn_vnetup:1|ifn_vnetdown:1)
228		sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
229		sleep 1
230	;;
231	esac
232
233	_str=
234	if [ -n "${_ok}" ]; then
235		case ${_func} in
236		ifn_start)
237			_str='Starting'
238		;;
239		ifn_stop)
240			_str='Stopping'
241		;;
242		ifn_vnetup)
243			_str='Moving'
244		;;
245		ifn_vnetdown)
246			_str='Reclaiming'
247		;;
248		esac
249		startmsg "${_str} Network:${_ok}."
250		case ${_func} in
251		ifn_vnetup)
252			# Clear _ok not to do "ifconfig $ifn"
253			# because $ifn is no longer in the current vnet.
254			_ok=
255		;;
256		esac
257		if check_startmsgs; then
258			for ifn in ${_ok}; do
259				/sbin/ifconfig ${ifn}
260			done
261		fi
262	fi
263
264	debug "The following interfaces were not configured: $_fail"
265}
266
267# Load the old "network" config file also for compatibility.
268# This is needed for mfsBSD at least.
269load_rc_config network
270load_rc_config $name
271run_rc_command $*
272