netif revision 129492
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 129492 2004-05-20 14:16:05Z 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{
45117021Smtm	if [ -z "$_cmdifn" ]; then
46117021Smtm		#
47117021Smtm		# We're operating as a general network start routine.
48117021Smtm		#
49113674Smtm
50117021Smtm		# Create cloned interfaces
51117021Smtm		clone_up
52113674Smtm
53117021Smtm		# Create IPv6<-->IPv4 tunnels
54117021Smtm		gif_up
55117021Smtm	fi
56117021Smtm
57117021Smtm	# Configure the interface(s).
58117021Smtm	network_common ifn_start verbose
59117021Smtm
60128714Sphk	if [ -f /etc/rc.d/ipfilter ] ; then
61128714Sphk		# Resync ipfilter
62128714Sphk		/etc/rc.d/ipfilter resync
63128714Sphk	fi
64117021Smtm}
65117021Smtm
66117021Smtmnetwork_stop()
67117021Smtm{
68117021Smtm	echo -n "Stopping network:"
69117021Smtm
70117021Smtm	# Deconfigure the interface(s)
71117021Smtm	network_common ifn_stop
72117021Smtm	echo '.'
73117021Smtm}
74117021Smtm
75117021Smtm# network_common routine verbose
76117021Smtm#	Common configuration subroutine for network interfaces. This
77117021Smtm#	routine takes all the preparatory steps needed for configuriing
78117021Smtm#	an interface and then calls $routine. If $verbose is specified,
79117021Smtm#	it will call ifconfig(8) to show, in long format, the configured
80117021Smtm#	interfaces. If $verbose is not given, it will simply output the
81117021Smtm#	configured interface(s).
82117021Smtmnetwork_common()
83117021Smtm{
84117021Smtm	_func=
85117021Smtm	_verbose=
86117021Smtm
87117021Smtm	if [ -z "$1" ]; then
88129492Smtm		err 1 "network_common(): No function name specified."
89117021Smtm	else
90117021Smtm		_func="$1"
91117021Smtm	fi
92117021Smtm	[ -n "$2" ] && _verbose=yes
93117021Smtm
94113674Smtm	# Get a list of network interfaces. Do not include dhcp interfaces.
95113674Smtm	_ifn_list="`list_net_interfaces nodhcp`"
96113674Smtm
97117021Smtm	# Set the scope of the command (all interfaces or just one).
98113674Smtm	#
99117021Smtm	_cooked_list="$_ifn_list"
100117021Smtm	if [ -n "$_cmdifn" ]; then
101117021Smtm		eval _cooked_list=\"`expr "$_ifn_list" : ".*\($_cmdifn\).*"`\"
102117021Smtm		if [ -z "$_cooked_list" ]; then
103129492Smtm			err 1 "No such network interface: $_cmdifn"
104117021Smtm			return 1
105117021Smtm		fi
106117021Smtm	fi
107113674Smtm
108117021Smtm	for ifn in ${_cooked_list}; do
109117021Smtm		if ${_func} ${ifn} ; then
110117021Smtm			eval showstat_$ifn=1
111117021Smtm		else
112117021Smtm			_fail="$_fail $ifn"
113117021Smtm		fi
114113674Smtm	done
115113674Smtm
116113674Smtm	# Display interfaces configured by this script
117113674Smtm	#
118117021Smtm	for ifn in ${_cooked_list}; do
119113674Smtm		eval showstat=\$showstat_${ifn}
120113674Smtm		if [ ! -z ${showstat} ]; then
121117021Smtm			if [ -n "$_verbose" ]; then
122117021Smtm				ifconfig ${ifn}
123117021Smtm			else
124117021Smtm				echo -n " ${ifn}"
125117021Smtm			fi
126113674Smtm		fi
127113674Smtm	done
128117021Smtm	debug "The following interfaces were not configured: $_fail"
129117021Smtm}
130113674Smtm
131117021Smtmifn_start()
132117021Smtm{
133117021Smtm	local ifn cfg
134117021Smtm	ifn="$1"
135117021Smtm	cfg=1
136117021Smtm
137117021Smtm	[ -z "$ifn" ] && return 1
138117021Smtm
139117021Smtm	ifscript_up ${ifn} && cfg=0
140117021Smtm	ifconfig_up ${ifn} && cfg=0
141117021Smtm	ifalias_up ${ifn} && cfg=0
142117021Smtm	ipx_up ${ifn} && cfg=0
143117021Smtm
144117021Smtm	return $cfg
145113674Smtm}
146113674Smtm
147117021Smtmifn_stop()
148113674Smtm{
149117021Smtm	local ifn cfg
150117021Smtm	ifn="$1"
151117021Smtm	cfg=1
152113674Smtm
153117021Smtm	[ -z "$ifn" ] && return 1
154117021Smtm
155117021Smtm	ipx_down ${ifn} && cfg=0
156117021Smtm	ifalias_down ${ifn} && cfg=0
157117021Smtm	ifconfig_down ${ifn} && cfg=0
158117021Smtm	ifscript_down ${ifn} && cfg=0
159117021Smtm
160117021Smtm	return $cfg
161113674Smtm}
162113674Smtm
163117021Smtmif [ -n "$2" ]; then
164117021Smtm	_cmdifn="$2"
165117021Smtmfi
166117021Smtm
167113674Smtmload_rc_config $name
168113674Smtmrun_rc_command "$1"
169