netif revision 175676
1113674Smtm#!/bin/sh
2113674Smtm#
3113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
4113674Smtm#
5113674Smtm# Redistribution and use in source and binary forms, with or without
6113674Smtm# modification, are permitted provided that the following conditions
7113674Smtm# are met:
8113674Smtm# 1. Redistributions of source code must retain the above copyright
9113674Smtm#    notice, this list of conditions and the following disclaimer.
10113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
11113674Smtm#    notice, this list of conditions and the following disclaimer in the
12113674Smtm#    documentation and/or other materials provided with the distribution.
13113674Smtm#
14113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT ``AS IS'' AND ANY EXPRESS OR
15113674Smtm# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16113674Smtm# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17113674Smtm# IN NO EVENT SHALL THE PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT,
18113674Smtm# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19113674Smtm# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20113674Smtm# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21113674Smtm# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22113674Smtm# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23113674Smtm# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24113674Smtm#
25113674Smtm# $FreeBSD: head/etc/rc.d/netif 175676 2008-01-26 11:22:12Z mtm $
26113674Smtm#
27113674Smtm
28113674Smtm# PROVIDE: netif
29171590Sjhb# REQUIRE: atm1 cleanvar ipfilter FILESYSTEMS serial sppp sysctl
30136224Smtm# KEYWORD: nojail
31113674Smtm
32113674Smtm. /etc/rc.subr
33113674Smtm. /etc/network.subr
34113674Smtm
35113674Smtmname="network"
36113674Smtmstart_cmd="network_start"
37113674Smtmstop_cmd="network_stop"
38113674Smtmcloneup_cmd="clone_up"
39113674Smtmclonedown_cmd="clone_down"
40113674Smtmextra_commands="cloneup clonedown"
41165664Syarcmdifn=
42113674Smtm
43113674Smtmnetwork_start()
44113674Smtm{
45132892Smtm	# Set the list of interfaces to work on.
46132892Smtm	#
47165664Syar	cmdifn=$*
48132892Smtm
49165664Syar	if [ -z "$cmdifn" ]; then
50117021Smtm		#
51117021Smtm		# We're operating as a general network start routine.
52117021Smtm		#
53113674Smtm
54149789Skeramida		# disable SIGINT (Ctrl-c) when running at startup
55149730Sbrooks		trap : 2
56149725Sbrooks
57117021Smtm		# Create cloned interfaces
58117021Smtm		clone_up
59113674Smtm
60166583Sflz		# Create Fast EtherChannel interfaces
61166583Sflz		fec_up
62166583Sflz
63117021Smtm		# Create IPv6<-->IPv4 tunnels
64117021Smtm		gif_up
65137070Spjd
66137070Spjd		# Rename interfaces.
67137070Spjd		ifnet_rename
68117021Smtm	fi
69117021Smtm
70117021Smtm	# Configure the interface(s).
71117021Smtm	network_common ifn_start verbose
72117021Smtm
73128714Sphk	if [ -f /etc/rc.d/ipfilter ] ; then
74128714Sphk		# Resync ipfilter
75175676Smtm		/etc/rc.d/ipfilter quietresync
76128714Sphk	fi
77165664Syar	if [ -f /etc/rc.d/bridge -a -n "$cmdifn" ] ; then
78165664Syar		/etc/rc.d/bridge start $cmdifn
79159138Sthompsa	fi
80117021Smtm}
81117021Smtm
82117021Smtmnetwork_stop()
83117021Smtm{
84132892Smtm	# Set the list of interfaces to work on.
85132892Smtm	#
86165664Syar	cmdifn=$*
87132892Smtm
88117021Smtm	echo -n "Stopping network:"
89117021Smtm
90117021Smtm	# Deconfigure the interface(s)
91117021Smtm	network_common ifn_stop
92117021Smtm	echo '.'
93117021Smtm}
94117021Smtm
95117021Smtm# network_common routine verbose
96117021Smtm#	Common configuration subroutine for network interfaces. This
97117021Smtm#	routine takes all the preparatory steps needed for configuriing
98117021Smtm#	an interface and then calls $routine. If $verbose is specified,
99117021Smtm#	it will call ifconfig(8) to show, in long format, the configured
100117021Smtm#	interfaces. If $verbose is not given, it will simply output the
101117021Smtm#	configured interface(s).
102117021Smtmnetwork_common()
103117021Smtm{
104165664Syar	local _cooked_list _fail _func _verbose
105165664Syar
106117021Smtm	_func=
107117021Smtm	_verbose=
108117021Smtm
109117021Smtm	if [ -z "$1" ]; then
110129492Smtm		err 1 "network_common(): No function name specified."
111117021Smtm	else
112117021Smtm		_func="$1"
113117021Smtm	fi
114117021Smtm	[ -n "$2" ] && _verbose=yes
115117021Smtm
116117021Smtm	# Set the scope of the command (all interfaces or just one).
117113674Smtm	#
118132892Smtm	_cooked_list=
119165664Syar	if [ -n "$cmdifn" ]; then
120147681Sbrooks		# Don't check that the interfaces exist.  We need to run
121147681Sbrooks		# the down code even when the interface doesn't exist to
122147681Sbrooks		# kill off wpa_supplicant.
123165664Syar		_cooked_list="$cmdifn"
124132892Smtm	else
125149725Sbrooks		_cooked_list="`list_net_interfaces`"
126117021Smtm	fi
127113674Smtm
128165664Syar	_fail=
129117021Smtm	for ifn in ${_cooked_list}; do
130117021Smtm		if ${_func} ${ifn} ; then
131117021Smtm			eval showstat_$ifn=1
132117021Smtm		else
133117021Smtm			_fail="$_fail $ifn"
134117021Smtm		fi
135113674Smtm	done
136113674Smtm
137113674Smtm	# Display interfaces configured by this script
138113674Smtm	#
139117021Smtm	for ifn in ${_cooked_list}; do
140113674Smtm		eval showstat=\$showstat_${ifn}
141113674Smtm		if [ ! -z ${showstat} ]; then
142117021Smtm			if [ -n "$_verbose" ]; then
143117021Smtm				ifconfig ${ifn}
144117021Smtm			else
145117021Smtm				echo -n " ${ifn}"
146117021Smtm			fi
147113674Smtm		fi
148113674Smtm	done
149117021Smtm	debug "The following interfaces were not configured: $_fail"
150117021Smtm}
151113674Smtm
152117021Smtmifn_start()
153117021Smtm{
154117021Smtm	local ifn cfg
155117021Smtm	ifn="$1"
156117021Smtm	cfg=1
157117021Smtm
158117021Smtm	[ -z "$ifn" ] && return 1
159117021Smtm
160117021Smtm	ifscript_up ${ifn} && cfg=0
161117021Smtm	ifconfig_up ${ifn} && cfg=0
162152441Sbrooks	ipv4_up ${ifn} && cfg=0
163117021Smtm	ipx_up ${ifn} && cfg=0
164117021Smtm
165117021Smtm	return $cfg
166113674Smtm}
167113674Smtm
168117021Smtmifn_stop()
169113674Smtm{
170117021Smtm	local ifn cfg
171117021Smtm	ifn="$1"
172117021Smtm	cfg=1
173113674Smtm
174117021Smtm	[ -z "$ifn" ] && return 1
175117021Smtm
176117021Smtm	ipx_down ${ifn} && cfg=0
177152441Sbrooks	ipv4_down ${ifn} && cfg=0
178117021Smtm	ifconfig_down ${ifn} && cfg=0
179117021Smtm	ifscript_down ${ifn} && cfg=0
180117021Smtm
181117021Smtm	return $cfg
182113674Smtm}
183113674Smtm
184113674Smtmload_rc_config $name
185132892Smtmrun_rc_command $*
186