netif revision 117021
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 117021 2003-06-29 05:34:41Z mtm $
26113674Smtm#
27113674Smtm
28113674Smtm# PROVIDE: netif
29113674Smtm# REQUIRE: atm1 ipfilter mountcritlocal pccard serial sppp sysctl tty
30113674Smtm# KEYWORD: FreeBSD
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
60117021Smtm	# Resync ipfilter
61117021Smtm	/etc/rc.d/ipfilter resync
62117021Smtm}
63117021Smtm
64117021Smtmnetwork_stop()
65117021Smtm{
66117021Smtm	echo -n "Stopping network:"
67117021Smtm
68117021Smtm	# Deconfigure the interface(s)
69117021Smtm	network_common ifn_stop
70117021Smtm	echo '.'
71117021Smtm}
72117021Smtm
73117021Smtm# network_common routine verbose
74117021Smtm#	Common configuration subroutine for network interfaces. This
75117021Smtm#	routine takes all the preparatory steps needed for configuriing
76117021Smtm#	an interface and then calls $routine. If $verbose is specified,
77117021Smtm#	it will call ifconfig(8) to show, in long format, the configured
78117021Smtm#	interfaces. If $verbose is not given, it will simply output the
79117021Smtm#	configured interface(s).
80117021Smtmnetwork_common()
81117021Smtm{
82117021Smtm	_func=
83117021Smtm	_verbose=
84117021Smtm
85117021Smtm	if [ -z "$1" ]; then
86117021Smtm		err "network_common(): No function name specified."
87117021Smtm	else
88117021Smtm		_func="$1"
89117021Smtm	fi
90117021Smtm	[ -n "$2" ] && _verbose=yes
91117021Smtm
92113674Smtm	# Get a list of network interfaces. Do not include dhcp interfaces.
93113674Smtm	_ifn_list="`list_net_interfaces nodhcp`"
94113674Smtm
95117021Smtm	# Set the scope of the command (all interfaces or just one).
96113674Smtm	#
97117021Smtm	_cooked_list="$_ifn_list"
98117021Smtm	if [ -n "$_cmdifn" ]; then
99117021Smtm		eval _cooked_list=\"`expr "$_ifn_list" : ".*\($_cmdifn\).*"`\"
100117021Smtm		if [ -z "$_cooked_list" ]; then
101117021Smtm			err "No such network interface: $_cmdifn"
102117021Smtm			return 1
103117021Smtm		fi
104117021Smtm	fi
105113674Smtm
106117021Smtm	for ifn in ${_cooked_list}; do
107117021Smtm		if ${_func} ${ifn} ; then
108117021Smtm			eval showstat_$ifn=1
109117021Smtm		else
110117021Smtm			_fail="$_fail $ifn"
111117021Smtm		fi
112113674Smtm	done
113113674Smtm
114113674Smtm	# Display interfaces configured by this script
115113674Smtm	#
116117021Smtm	for ifn in ${_cooked_list}; do
117113674Smtm		eval showstat=\$showstat_${ifn}
118113674Smtm		if [ ! -z ${showstat} ]; then
119117021Smtm			if [ -n "$_verbose" ]; then
120117021Smtm				ifconfig ${ifn}
121117021Smtm			else
122117021Smtm				echo -n " ${ifn}"
123117021Smtm			fi
124113674Smtm		fi
125113674Smtm	done
126117021Smtm	debug "The following interfaces were not configured: $_fail"
127117021Smtm}
128113674Smtm
129117021Smtmifn_start()
130117021Smtm{
131117021Smtm	local ifn cfg
132117021Smtm	ifn="$1"
133117021Smtm	cfg=1
134117021Smtm
135117021Smtm	[ -z "$ifn" ] && return 1
136117021Smtm
137117021Smtm	ifscript_up ${ifn} && cfg=0
138117021Smtm	ifconfig_up ${ifn} && cfg=0
139117021Smtm	ifalias_up ${ifn} && cfg=0
140117021Smtm	ipx_up ${ifn} && cfg=0
141117021Smtm
142117021Smtm	return $cfg
143113674Smtm}
144113674Smtm
145117021Smtmifn_stop()
146113674Smtm{
147117021Smtm	local ifn cfg
148117021Smtm	ifn="$1"
149117021Smtm	cfg=1
150113674Smtm
151117021Smtm	[ -z "$ifn" ] && return 1
152117021Smtm
153117021Smtm	ipx_down ${ifn} && cfg=0
154117021Smtm	ifalias_down ${ifn} && cfg=0
155117021Smtm	ifconfig_down ${ifn} && cfg=0
156117021Smtm	ifscript_down ${ifn} && cfg=0
157117021Smtm
158117021Smtm	return $cfg
159113674Smtm}
160113674Smtm
161117021Smtmif [ -n "$2" ]; then
162117021Smtm	_cmdifn="$2"
163117021Smtmfi
164117021Smtm
165113674Smtmload_rc_config $name
166113674Smtmrun_rc_command "$1"
167