netif revision 175676
1218847Sed#!/bin/sh
2231530Sed#
3218847Sed# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4218847Sed#
5218847Sed# Redistribution and use in source and binary forms, with or without
6218847Sed# modification, are permitted provided that the following conditions
7218847Sed# are met:
8218847Sed# 1. Redistributions of source code must retain the above copyright
9218847Sed#    notice, this list of conditions and the following disclaimer.
10218847Sed# 2. Redistributions in binary form must reproduce the above copyright
11218847Sed#    notice, this list of conditions and the following disclaimer in the
12218847Sed#    documentation and/or other materials provided with the distribution.
13218847Sed#
14218847Sed# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
15218847Sed# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16218847Sed# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17218847Sed# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
18218847Sed# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19218847Sed# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20218847Sed# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21218847Sed# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22218847Sed# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23218847Sed# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24218847Sed#
25218847Sed# $FreeBSD: head/etc/rc.d/netif 175676 2008-01-26 11:22:12Z mtm $
26218847Sed#
27218847Sed
28218847Sed# PROVIDE: netif
29218847Sed# REQUIRE: atm1 cleanvar ipfilter FILESYSTEMS serial sppp sysctl
30218847Sed# KEYWORD: nojail
31218847Sed
32218847Sed. /etc/rc.subr
33218847Sed. /etc/network.subr
34231530Sed
35218847Sedname="network"
36218847Sedstart_cmd="network_start"
37218847Sedstop_cmd="network_stop"
38218847Sedcloneup_cmd="clone_up"
39218847Sedclonedown_cmd="clone_down"
40218847Sedextra_commands="cloneup clonedown"
41218847Sedcmdifn=
42218847Sed
43218847Sednetwork_start()
44218847Sed{
45218847Sed	# Set the list of interfaces to work on.
46218847Sed	#
47218847Sed	cmdifn=$*
48218847Sed
49218847Sed	if [ -z "$cmdifn" ]; then
50218847Sed		#
51231530Sed		# We're operating as a general network start routine.
52231530Sed		#
53218847Sed
54218847Sed		# disable SIGINT (Ctrl-c) when running at startup
55218847Sed		trap : 2
56231530Sed
57218847Sed		# Create cloned interfaces
58231530Sed		clone_up
59231530Sed
60231530Sed		# Create Fast EtherChannel interfaces
61218847Sed		fec_up
62218847Sed
63231530Sed		# Create IPv6<-->IPv4 tunnels
64218847Sed		gif_up
65231530Sed
66218847Sed		# Rename interfaces.
67231530Sed		ifnet_rename
68218847Sed	fi
69218847Sed
70218847Sed	# Configure the interface(s).
71218847Sed	network_common ifn_start verbose
72218847Sed
73218847Sed	if [ -f /etc/rc.d/ipfilter ] ; then
74231530Sed		# Resync ipfilter
75218847Sed		/etc/rc.d/ipfilter quietresync
76218847Sed	fi
77218847Sed	if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
78218847Sed		/etc/rc.d/bridge start $cmdifn
79218847Sed	fi
80231530Sed}
81231530Sed
82231530Sednetwork_stop()
83231530Sed{
84231530Sed	# Set the list of interfaces to work on.
85231530Sed	#
86231530Sed	cmdifn=$*
87231530Sed
88231530Sed	echo -n "Stopping network:"
89231530Sed
90231530Sed	# Deconfigure the interface(s)
91231530Sed	network_common ifn_stop
92231530Sed	echo '.'
93231530Sed}
94231530Sed
95231530Sed# network_common routine verbose
96231530Sed#	Common configuration subroutine for network interfaces. This
97231530Sed#	routine takes all the preparatory steps needed for configuriing
98231530Sed#	an interface and then calls $routine. If $verbose is specified,
99231530Sed#	it will call ifconfig(8) to show, in long format, the configured
100231530Sed#	interfaces. If $verbose is not given, it will simply output the
101231530Sed#	configured interface(s).
102231530Sednetwork_common()
103231530Sed{
104231530Sed	local _cooked_list _fail _func _verbose
105231530Sed
106231530Sed	_func=
107231530Sed	_verbose=
108231530Sed
109231530Sed	if [ -z "$1" ]; then
110231530Sed		err 1 "network_common(): No function name specified."
111231530Sed	else
112231530Sed		_func="$1"
113231530Sed	fi
114231530Sed	[ -n "$2" ] && _verbose=yes
115
116	# Set the scope of the command (all interfaces or just one).
117	#
118	_cooked_list=
119	if [ -n "$cmdifn" ]; then
120		# Don't check that the interfaces exist.  We need to run
121		# the down code even when the interface doesn't exist to
122		# kill off wpa_supplicant.
123		_cooked_list="$cmdifn"
124	else
125		_cooked_list="`list_net_interfaces`"
126	fi
127
128	_fail=
129	for ifn in ${_cooked_list}; do
130		if ${_func} ${ifn} ; then
131			eval showstat_$ifn=1
132		else
133			_fail="$_fail $ifn"
134		fi
135	done
136
137	# Display interfaces configured by this script
138	#
139	for ifn in ${_cooked_list}; do
140		eval showstat=\$showstat_${ifn}
141		if [ ! -z ${showstat} ]; then
142			if [ -n "$_verbose" ]; then
143				ifconfig ${ifn}
144			else
145				echo -n " ${ifn}"
146			fi
147		fi
148	done
149	debug "The following interfaces were not configured: $_fail"
150}
151
152ifn_start()
153{
154	local ifn cfg
155	ifn="$1"
156	cfg=1
157
158	[ -z "$ifn" ] && return 1
159
160	ifscript_up ${ifn} && cfg=0
161	ifconfig_up ${ifn} && cfg=0
162	ipv4_up ${ifn} && cfg=0
163	ipx_up ${ifn} && cfg=0
164
165	return $cfg
166}
167
168ifn_stop()
169{
170	local ifn cfg
171	ifn="$1"
172	cfg=1
173
174	[ -z "$ifn" ] && return 1
175
176	ipx_down ${ifn} && cfg=0
177	ipv4_down ${ifn} && cfg=0
178	ifconfig_down ${ifn} && cfg=0
179	ifscript_down ${ifn} && cfg=0
180
181	return $cfg
182}
183
184load_rc_config $name
185run_rc_command $*
186