netif revision 132892
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 132892 2004-07-30 17:19:35Z mtm $
26113674Smtm#
27113674Smtm
28113674Smtm# PROVIDE: netif
29126392Sgreen# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl
30126744Spjd# KEYWORD: FreeBSD 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"
41117021Smtm_cmdifn=
42113674Smtm
43113674Smtmnetwork_start()
44113674Smtm{
45132892Smtm	# Set the list of interfaces to work on.
46132892Smtm	#
47132892Smtm	_cmdifn=$*
48132892Smtm
49117021Smtm	if [ -z "$_cmdifn" ]; then
50117021Smtm		#
51117021Smtm		# We're operating as a general network start routine.
52117021Smtm		#
53113674Smtm
54117021Smtm		# Create cloned interfaces
55117021Smtm		clone_up
56113674Smtm
57117021Smtm		# Create IPv6<-->IPv4 tunnels
58117021Smtm		gif_up
59117021Smtm	fi
60117021Smtm
61117021Smtm	# Configure the interface(s).
62117021Smtm	network_common ifn_start verbose
63117021Smtm
64128714Sphk	if [ -f /etc/rc.d/ipfilter ] ; then
65128714Sphk		# Resync ipfilter
66128714Sphk		/etc/rc.d/ipfilter resync
67128714Sphk	fi
68117021Smtm}
69117021Smtm
70117021Smtmnetwork_stop()
71117021Smtm{
72132892Smtm	# Set the list of interfaces to work on.
73132892Smtm	#
74132892Smtm	_cmdifn=$*
75132892Smtm
76117021Smtm	echo -n "Stopping network:"
77117021Smtm
78117021Smtm	# Deconfigure the interface(s)
79117021Smtm	network_common ifn_stop
80117021Smtm	echo '.'
81117021Smtm}
82117021Smtm
83117021Smtm# network_common routine verbose
84117021Smtm#	Common configuration subroutine for network interfaces. This
85117021Smtm#	routine takes all the preparatory steps needed for configuriing
86117021Smtm#	an interface and then calls $routine. If $verbose is specified,
87117021Smtm#	it will call ifconfig(8) to show, in long format, the configured
88117021Smtm#	interfaces. If $verbose is not given, it will simply output the
89117021Smtm#	configured interface(s).
90117021Smtmnetwork_common()
91117021Smtm{
92117021Smtm	_func=
93117021Smtm	_verbose=
94117021Smtm
95117021Smtm	if [ -z "$1" ]; then
96129492Smtm		err 1 "network_common(): No function name specified."
97117021Smtm	else
98117021Smtm		_func="$1"
99117021Smtm	fi
100117021Smtm	[ -n "$2" ] && _verbose=yes
101117021Smtm
102113674Smtm	# Get a list of network interfaces. Do not include dhcp interfaces.
103113674Smtm	_ifn_list="`list_net_interfaces nodhcp`"
104113674Smtm
105117021Smtm	# Set the scope of the command (all interfaces or just one).
106113674Smtm	#
107132892Smtm	_cooked_list=
108117021Smtm	if [ -n "$_cmdifn" ]; then
109132892Smtm		for i in $_cmdifn ; do
110132892Smtm			eval _if=\"`expr "$_ifn_list" : ".*\(${i}\).*"`\"
111132892Smtm			if [ -z "$_if" ]; then
112132892Smtm				err 1 "No such network interface: $i"
113132892Smtm			fi
114132892Smtm			_cooked_list="$_cooked_list $_if"
115132892Smtm		done
116132892Smtm	else
117132892Smtm		_cooked_list="$_ifn_list"
118117021Smtm	fi
119113674Smtm
120117021Smtm	for ifn in ${_cooked_list}; do
121117021Smtm		if ${_func} ${ifn} ; then
122117021Smtm			eval showstat_$ifn=1
123117021Smtm		else
124117021Smtm			_fail="$_fail $ifn"
125117021Smtm		fi
126113674Smtm	done
127113674Smtm
128113674Smtm	# Display interfaces configured by this script
129113674Smtm	#
130117021Smtm	for ifn in ${_cooked_list}; do
131113674Smtm		eval showstat=\$showstat_${ifn}
132113674Smtm		if [ ! -z ${showstat} ]; then
133117021Smtm			if [ -n "$_verbose" ]; then
134117021Smtm				ifconfig ${ifn}
135117021Smtm			else
136117021Smtm				echo -n " ${ifn}"
137117021Smtm			fi
138113674Smtm		fi
139113674Smtm	done
140117021Smtm	debug "The following interfaces were not configured: $_fail"
141117021Smtm}
142113674Smtm
143117021Smtmifn_start()
144117021Smtm{
145117021Smtm	local ifn cfg
146117021Smtm	ifn="$1"
147117021Smtm	cfg=1
148117021Smtm
149117021Smtm	[ -z "$ifn" ] && return 1
150117021Smtm
151117021Smtm	ifscript_up ${ifn} && cfg=0
152117021Smtm	ifconfig_up ${ifn} && cfg=0
153117021Smtm	ifalias_up ${ifn} && cfg=0
154117021Smtm	ipx_up ${ifn} && cfg=0
155117021Smtm
156117021Smtm	return $cfg
157113674Smtm}
158113674Smtm
159117021Smtmifn_stop()
160113674Smtm{
161117021Smtm	local ifn cfg
162117021Smtm	ifn="$1"
163117021Smtm	cfg=1
164113674Smtm
165117021Smtm	[ -z "$ifn" ] && return 1
166117021Smtm
167117021Smtm	ipx_down ${ifn} && cfg=0
168117021Smtm	ifalias_down ${ifn} && cfg=0
169117021Smtm	ifconfig_down ${ifn} && cfg=0
170117021Smtm	ifscript_down ${ifn} && cfg=0
171117021Smtm
172117021Smtm	return $cfg
173113674Smtm}
174113674Smtm
175113674Smtmload_rc_config $name
176132892Smtmrun_rc_command $*
177