network.subr revision 113674
125184Sjkh#
2113674Smtm# Copyright (c) 2003 The FreeBSD Project. All rights reserved.
3113674Smtm#
4113674Smtm# Redistribution and use in source and binary forms, with or without
5113674Smtm# modification, are permitted provided that the following conditions
6113674Smtm# are met:
7113674Smtm# 1. Redistributions of source code must retain the above copyright
8113674Smtm#    notice, this list of conditions and the following disclaimer.
9113674Smtm# 2. Redistributions in binary form must reproduce the above copyright
10113674Smtm#    notice, this list of conditions and the following disclaimer in the
11113674Smtm#    documentation and/or other materials provided with the distribution.
12113674Smtm#
13113674Smtm# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
14113674Smtm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15113674Smtm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16113674Smtm# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
17113674Smtm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18113674Smtm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19113674Smtm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20113674Smtm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21113674Smtm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22113674Smtm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23113674Smtm# SUCH DAMAGE.
24113674Smtm#
2550472Speter# $FreeBSD: head/etc/network.subr 113674 2003-04-18 17:51:54Z mtm $
2666830Sobrien#
2725184Sjkh
28113674Smtm#
29113674Smtm# Subroutines commonly used from network startup scripts.
30113674Smtm# Requires that rc.conf be loaded first.
31113674Smtm#
3225184Sjkh
33113674Smtm# ifconfig_up if
34113674Smtm#	Evaluate ifconfig(8) arguments for interface $if and
35113674Smtm#	run ifconfig(8) with those arguments. It returns 0 if
36113674Smtm#	arguments were found and executed or 1 if the interface
37113674Smtm#	had no arguments.
38113674Smtm#
39113674Smtmifconfig_up()
40113674Smtm{
41113674Smtm	eval ifconfig_args=\$ifconfig_$1
42113674Smtm	if [ -n "${ifconfig_args}" ]; then
43113674Smtm		ifconfig $1 ${ifconfig_args}
44113674Smtm		return 0
45113674Smtm	fi
46113674Smtm	return 1
47113674Smtm}
4825184Sjkh
49113674Smtm# ifalias_up if
50113674Smtm#	Configure aliases for network interface $if.
51113674Smtm#	It returns 0 if at least one alias was configured or
52113674Smtm#	1 if there were none.
53113674Smtm#
54113674Smtmifalias_up()
55113674Smtm{
56113674Smtm	_ret=1
57113674Smtm	alias=0
58113674Smtm	while : ; do
59113674Smtm		eval ifconfig_args=\$ifconfig_$1_alias${alias}
60113674Smtm		if [ -n "${ifconfig_args}" ]; then
61113674Smtm			ifconfig $1 ${ifconfig_args} alias
62113674Smtm			alias=$((${alias} + 1))
63113674Smtm			_ret=0
64113674Smtm		else
65113674Smtm			break
66113674Smtm		fi
67113674Smtm	done
68113674Smtm	return $_ret
69113674Smtm}
70100280Sgordon
71113674Smtm# ifscript_up if
72113674Smtm#	Evaluate a startup script for the $if interface.
73113674Smtm#	It returns 0 if a script was found and processed or
74113674Smtm#	1 if no script was found.
75113674Smtm#
76113674Smtmifscript_up()
77100280Sgordon{
78113674Smtm	if [ -r /etc/start_if.$1 ]; then
79113674Smtm		. /etc/start_if.$1
80113674Smtm		return 0
81113674Smtm	fi
82113674Smtm	return 1
83100280Sgordon}
84100280Sgordon
85113674Smtm# Create cloneable interfaces.
86113674Smtm#
87113674Smtmclone_up()
88100280Sgordon{
89113674Smtm	_prefix=
90113674Smtm	_list=
91113674Smtm	for ifn in ${cloned_interfaces}; do
92113674Smtm		ifconfig ${ifn} create
93113674Smtm		if $? ; then
94113674Smtm			_list="${_list}${_prefix}${ifn}"
95113674Smtm			[ -z "$_prefix" ] && _prefix=' '
96113674Smtm		fi
97113674Smtm	done
98113674Smtm	debug "Cloned: ${_list}"
99113674Smtm}
100100280Sgordon
101113674Smtm# Destroy cloned interfaces. Destroyed interfaces are echoed
102113674Smtm# to standard output.
103113674Smtm#
104113674Smtmclone_down()
105113674Smtm{
106113674Smtm	_prefix=
107113674Smtm	_list=
108113674Smtm	for ifn in ${cloned_interfaces}; do
109113674Smtm		ifconfig ${ifn} destroy
110113674Smtm		if $? ; then
111113674Smtm			_list="${_list}${_prefix}${ifn}"
112113674Smtm			[ -z "$_prefix" ] && _prefix=' '
113113674Smtm		fi
114113674Smtm	done
115113674Smtm	debug "Destroyed clones: ${_list}"
116100280Sgordon}
117100280Sgordon
118113674Smtmgif_up() {
119100282Sdougb	case ${gif_interfaces} in
120100282Sdougb	[Nn][Oo] | '')
121100282Sdougb		;;
122100282Sdougb	*)
123100282Sdougb		for i in ${gif_interfaces}; do
124100282Sdougb			eval peers=\$gifconfig_$i
125100282Sdougb			case ${peers} in
126100282Sdougb			'')
127100282Sdougb				continue
128100282Sdougb				;;
129100282Sdougb			*)
130100282Sdougb				ifconfig $i create >/dev/null 2>&1
131100282Sdougb				ifconfig $i tunnel ${peers}
132103710Sume				ifconfig $i up
133100282Sdougb				;;
134100282Sdougb			esac
135100282Sdougb		done
136100282Sdougb		;;
137100282Sdougb	esac
138100282Sdougb}
139100282Sdougb
140113674Smtm#
141113674Smtm# ipx_up ifn
142113674Smtm# Configure any IPX addresses for interface $ifn. Returns 0 if IPX
143113674Smtm# arguments were found and configured; returns 1 otherwise.
144113674Smtm#
145113674Smtmipx_up()
146100280Sgordon{
147113674Smtm	ifn="$1"
148113674Smtm	eval ifconfig_args=\$ifconfig_${ifn}_ipx
149113674Smtm	if [ -n "${ifconfig_args}" ]; then
150113674Smtm		ifconfig ${ifn} ${ifconfig_args}
151113674Smtm		return 0
15285831Sdes	fi
153113674Smtm	return 1
154113674Smtm}
15585831Sdes
156113674Smtm#
157113674Smtm# list_net_interfaces type
158113674Smtm#	List all network interfaces. The type of interface returned
159113674Smtm#	can be controlled by the type argument. The type
160113674Smtm#	argument can be any of the following:
161113674Smtm#		nodhcp - all interfaces, excluding DHCP configured interfaces
162113674Smtm#		dhcp   - list only DHCP configured interfaces
163113674Smtm#	If no argument is specified all network interfaces are output.
164113674Smtm#	Note that the list always includes cloned interfaces.
165113674Smtm#
166113674Smtmlist_net_interfaces()
167113674Smtm{
168113674Smtm	type=$1
16965532Snectar
170113674Smtm	# Get a list of ALL the interfaces
17151231Ssheldonh	#
17251231Ssheldonh	case ${network_interfaces} in
17351231Ssheldonh	[Aa][Uu][Tt][Oo])
174113674Smtm		_tmplist="`ifconfig -l`"
17551231Ssheldonh		;;
17683677Sbrooks	*)
177113674Smtm		_tmplist="${network_interfaces}"
17883677Sbrooks		;;
17951231Ssheldonh	esac
180113674Smtm	_tmplist="${_tmplist} ${cloned_interfaces}"
18149122Sbrian
182113674Smtm	if [ -z "$type" ]; then
183113674Smtm		echo $_tmplist
184113674Smtm		return 0
185113674Smtm	fi
18649122Sbrian
187113674Smtm	# Separate out dhcp and non-dhcp intefraces
188113674Smtm	#
189113674Smtm	_aprefix=
190113674Smtm	_brefix=
191113674Smtm	for _if in ${_tmplist} ; do
192113674Smtm		eval _ifarg="\$ifconfig_${_if}"
193113674Smtm		case "$_ifarg" in
19451231Ssheldonh		[Dd][Hh][Cc][Pp])
195113674Smtm			_dhcplist="${_dhcplist}${_aprefix}${_if}"
196113674Smtm			[ -z "$_aprefix" ] && _aprefix=' '
19751231Ssheldonh			;;
198113674Smtm		''|*)
199113674Smtm			_nodhcplist="${_nodhcplist}${_bprefix}${_if}"
200113674Smtm			[ -z "$_bprefix" ] && _bprefix=' '
20151231Ssheldonh			;;
20251231Ssheldonh		esac
20354458Sobrien	done
20451231Ssheldonh
205113674Smtm	case "$type" in
206113674Smtm	nodhcp)
207113674Smtm		echo $_nodhcplist
208113674Smtm		;;
209113674Smtm	dhcp)
210113674Smtm		echo $_dhcplist
211113674Smtm		;;
212113674Smtm	esac
213113674Smtm	return 0			
21425184Sjkh}
215