network.subr revision 113674
1#
2# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25# $FreeBSD: head/etc/network.subr 113674 2003-04-18 17:51:54Z mtm $
26#
27
28#
29# Subroutines commonly used from network startup scripts.
30# Requires that rc.conf be loaded first.
31#
32
33# ifconfig_up if
34#	Evaluate ifconfig(8) arguments for interface $if and
35#	run ifconfig(8) with those arguments. It returns 0 if
36#	arguments were found and executed or 1 if the interface
37#	had no arguments.
38#
39ifconfig_up()
40{
41	eval ifconfig_args=\$ifconfig_$1
42	if [ -n "${ifconfig_args}" ]; then
43		ifconfig $1 ${ifconfig_args}
44		return 0
45	fi
46	return 1
47}
48
49# ifalias_up if
50#	Configure aliases for network interface $if.
51#	It returns 0 if at least one alias was configured or
52#	1 if there were none.
53#
54ifalias_up()
55{
56	_ret=1
57	alias=0
58	while : ; do
59		eval ifconfig_args=\$ifconfig_$1_alias${alias}
60		if [ -n "${ifconfig_args}" ]; then
61			ifconfig $1 ${ifconfig_args} alias
62			alias=$((${alias} + 1))
63			_ret=0
64		else
65			break
66		fi
67	done
68	return $_ret
69}
70
71# ifscript_up if
72#	Evaluate a startup script for the $if interface.
73#	It returns 0 if a script was found and processed or
74#	1 if no script was found.
75#
76ifscript_up()
77{
78	if [ -r /etc/start_if.$1 ]; then
79		. /etc/start_if.$1
80		return 0
81	fi
82	return 1
83}
84
85# Create cloneable interfaces.
86#
87clone_up()
88{
89	_prefix=
90	_list=
91	for ifn in ${cloned_interfaces}; do
92		ifconfig ${ifn} create
93		if $? ; then
94			_list="${_list}${_prefix}${ifn}"
95			[ -z "$_prefix" ] && _prefix=' '
96		fi
97	done
98	debug "Cloned: ${_list}"
99}
100
101# Destroy cloned interfaces. Destroyed interfaces are echoed
102# to standard output.
103#
104clone_down()
105{
106	_prefix=
107	_list=
108	for ifn in ${cloned_interfaces}; do
109		ifconfig ${ifn} destroy
110		if $? ; then
111			_list="${_list}${_prefix}${ifn}"
112			[ -z "$_prefix" ] && _prefix=' '
113		fi
114	done
115	debug "Destroyed clones: ${_list}"
116}
117
118gif_up() {
119	case ${gif_interfaces} in
120	[Nn][Oo] | '')
121		;;
122	*)
123		for i in ${gif_interfaces}; do
124			eval peers=\$gifconfig_$i
125			case ${peers} in
126			'')
127				continue
128				;;
129			*)
130				ifconfig $i create >/dev/null 2>&1
131				ifconfig $i tunnel ${peers}
132				ifconfig $i up
133				;;
134			esac
135		done
136		;;
137	esac
138}
139
140#
141# ipx_up ifn
142# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
143# arguments were found and configured; returns 1 otherwise.
144#
145ipx_up()
146{
147	ifn="$1"
148	eval ifconfig_args=\$ifconfig_${ifn}_ipx
149	if [ -n "${ifconfig_args}" ]; then
150		ifconfig ${ifn} ${ifconfig_args}
151		return 0
152	fi
153	return 1
154}
155
156#
157# list_net_interfaces type
158#	List all network interfaces. The type of interface returned
159#	can be controlled by the type argument. The type
160#	argument can be any of the following:
161#		nodhcp - all interfaces, excluding DHCP configured interfaces
162#		dhcp   - list only DHCP configured interfaces
163#	If no argument is specified all network interfaces are output.
164#	Note that the list always includes cloned interfaces.
165#
166list_net_interfaces()
167{
168	type=$1
169
170	# Get a list of ALL the interfaces
171	#
172	case ${network_interfaces} in
173	[Aa][Uu][Tt][Oo])
174		_tmplist="`ifconfig -l`"
175		;;
176	*)
177		_tmplist="${network_interfaces}"
178		;;
179	esac
180	_tmplist="${_tmplist} ${cloned_interfaces}"
181
182	if [ -z "$type" ]; then
183		echo $_tmplist
184		return 0
185	fi
186
187	# Separate out dhcp and non-dhcp intefraces
188	#
189	_aprefix=
190	_brefix=
191	for _if in ${_tmplist} ; do
192		eval _ifarg="\$ifconfig_${_if}"
193		case "$_ifarg" in
194		[Dd][Hh][Cc][Pp])
195			_dhcplist="${_dhcplist}${_aprefix}${_if}"
196			[ -z "$_aprefix" ] && _aprefix=' '
197			;;
198		''|*)
199			_nodhcplist="${_nodhcplist}${_bprefix}${_if}"
200			[ -z "$_bprefix" ] && _bprefix=' '
201			;;
202		esac
203	done
204
205	case "$type" in
206	nodhcp)
207		echo $_nodhcplist
208		;;
209	dhcp)
210		echo $_dhcplist
211		;;
212	esac
213	return 0			
214}
215