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$
26#
27
28# PROVIDE: netif
29# REQUIRE: atm1 FILESYSTEMS serial sppp sysctl
30# REQUIRE: ipfilter ipfs
31# KEYWORD: nojailvnet
32
33. /etc/rc.subr
34. /etc/network.subr
35
36name="network"
37start_cmd="network_start"
38stop_cmd="network_stop"
39cloneup_cmd="clone_up"
40clonedown_cmd="clone_down"
41extra_commands="cloneup clonedown"
42cmdifn=
43
44set_rcvar_obsolete ipv6_enable ipv6_activate_all_interfaces
45set_rcvar_obsolete ipv6_prefer
46
47network_start()
48{
49	local _if
50
51	# Set the list of interfaces to work on.
52	#
53	cmdifn=$*
54
55	if [ -z "$cmdifn" ]; then
56		#
57		# We're operating as a general network start routine.
58		#
59
60		# disable SIGINT (Ctrl-c) when running at startup
61		trap : 2
62
63		# Create cloned interfaces
64		clone_up
65
66		# Create Fast EtherChannel interfaces
67		fec_up
68
69		# Create IPv6<-->IPv4 tunnels
70		gif_up
71
72		# Rename interfaces.
73		ifnet_rename
74	fi
75
76	# Configure the interface(s).
77	network_common ifn_start
78
79	if [ -f /etc/rc.d/ipfilter ] ; then
80		# Resync ipfilter
81		/etc/rc.d/ipfilter quietresync
82	fi
83	if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
84		/etc/rc.d/bridge start $cmdifn
85	fi
86	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
87		for _if in $cmdifn; do
88			/etc/rc.d/routing start any $_if
89		done
90	fi
91}
92
93network_stop()
94{
95	local _if
96
97	# Set the list of interfaces to work on.
98	#
99	cmdifn=$*
100
101	# Deconfigure the interface(s)
102	network_common ifn_stop
103
104	if [ -f /etc/rc.d/routing -a -n "$cmdifn" ] ; then
105		for _if in $cmdifn; do
106			/etc/rc.d/routing stop any $_if
107		done
108	fi
109}
110
111# network_common routine
112#	Common configuration subroutine for network interfaces. This
113#	routine takes all the preparatory steps needed for configuriing
114#	an interface and then calls $routine.
115network_common()
116{
117	local _cooked_list _fail _func _ok _str
118
119	_func=
120
121	if [ -z "$1" ]; then
122		err 1 "network_common(): No function name specified."
123	else
124		_func="$1"
125	fi
126
127	# Set the scope of the command (all interfaces or just one).
128	#
129	_cooked_list=
130	if [ -n "$cmdifn" ]; then
131		# Don't check that the interface(s) exist.  We need to run
132		# the down code even when the interface doesn't exist to
133		# kill off wpa_supplicant.
134		# XXXBED: is this really true or does wpa_supplicant die?
135		# if so, we should get rid of the devd entry
136		_cooked_list="$cmdifn"
137	else
138		_cooked_list="`list_net_interfaces`"
139	fi
140
141	_dadwait=
142	_fail=
143	_ok=
144	for ifn in ${_cooked_list}; do
145		if ${_func} ${ifn} $2; then
146			_ok="${_ok} ${ifn}"
147			if ipv6if ${ifn}; then
148				_dadwait=1
149			fi
150		else
151			_fail="${_fail} ${ifn}"
152		fi
153	done
154
155	# inet6 address configuration needs sleep for DAD.
156	case ${_func}:${_dadwait} in
157	ifn_start:1)
158		sleep `${SYSCTL_N} net.inet6.ip6.dad_count`
159		sleep 1
160	;;
161	esac
162
163	_str=
164	if [ -n "${_ok}" ]; then
165		case ${_func} in
166		ifn_start)
167			_str='Starting'
168			;;
169		ifn_stop)
170			_str='Stopping'
171			;;
172		esac
173		echo "${_str} Network:${_ok}."
174		if check_startmsgs; then
175			for ifn in ${_ok}; do
176				/sbin/ifconfig ${ifn}
177			done
178		fi
179	fi
180
181	debug "The following interfaces were not configured: $_fail"
182}
183
184load_rc_config $name
185run_rc_command $*
186