pccard_ether revision 120097
1236769Sobrien#!/bin/sh -
2236769Sobrien#
3236769Sobrien# $FreeBSD: head/etc/pccard_ether 120097 2003-09-15 18:27:38Z mbr $
4236769Sobrien#
5236769Sobrien# pccard_ether interfacename [start|stop] [ifconfig option]
6236769Sobrien#
7253883Ssjg# example: pccard_ether fxp0 start link0
8236769Sobrien#
9236769Sobrien
10236769Sobrien. /etc/network.subr
11236769Sobrien
12236769Sobrienstop_dhcp() {
13236769Sobrien	# If dhclient is already running, record
14236769Sobrien	# its interfaces.
15236769Sobrien	if [ -x /usr/bin/grep ]; then
16236769Sobrien		eval _active_list=\"`/bin/ps -axwww | \
17236769Sobrien			/usr/bin/grep dhclient | \
18236769Sobrien			/usr/bin/grep -v grep | \
19236769Sobrien			/usr/bin/sed -e 's|^.*dhclient||' | \
20236769Sobrien			/usr/bin/awk '{for (i=1;i<=NF;i++) \
21236769Sobrien				{ if ($i~/[a-zA-Z].[0-9]$/) \
22236769Sobrien				{ printf(" %s",$i) } }}'` \
23236769Sobrien			\"
24236769Sobrien	fi
25236769Sobrien
26236769Sobrien	# Get the rc.conf list of dhcp configured interfaces
27236769Sobrien	static_dhcp_list="`list_net_interfaces dhcp`"
28236769Sobrien
29236769Sobrien	# Get the current ifconfig list of interfaces
30236769Sobrien	_aprefix=
31236769Sobrien	_nlist=
32236769Sobrien	for _if in ${_active_list} ; do
33236769Sobrien		_test_if=`ifconfig ${_if} 2>&1`
34236769Sobrien		case "$_test_if" in
35236769Sobrien		"ifconfig: interface $_if does not exist")
36236769Sobrien			;;
37236769Sobrien		${interface})
38236769Sobrien			# Don't record the same device twice.
39236769Sobrien			;;
40236769Sobrien		*)
41236769Sobrien			#
42236769Sobrien			# Catch devices which were specified before,
43236769Sobrien			# but have not been part of the rc. We need
44236769Sobrien			# them again for the restart.
45236769Sobrien			#
46236769Sobrien			for _cif in ${static_dhcp_list} ; do
47236769Sobrien				case "$_cif" in
48236769Sobrien				${_if})
49236769Sobrien					# Nothing to add
50236769Sobrien					;;
51236769Sobrien				*)
52236769Sobrien					# Found interface beside rc.conf
53236769Sobrien					_nlist="${_nlist}${_aprefix}${_if}"
54236769Sobrien					;;
55236769Sobrien				esac
56236769Sobrien			done
57236769Sobrien			_dhcplist="${_dhcplist}${_aprefix}${_if}"
58236769Sobrien			[ -z "$_aprefix" ] && _aprefix=' '
59236769Sobrien			;;
60236769Sobrien		esac
61236769Sobrien	done
62236769Sobrien
63236769Sobrien	if [ -s /var/run/dhclient.pid ]; then
64236769Sobrien		pidfile="/var/run/dhclient.pid"
65236769Sobrien	else
66236769Sobrien		return
67236769Sobrien	fi
68236769Sobrien	/sbin/dhclient -r ${interface}
69236769Sobrien	rm -f ${pidfile}
70236769Sobrien	case ${startstop} in
71236769Sobrien	[Ss][Tt][Oo][Pp])
72236769Sobrien		if [ -z "${_nlist}" ]; then
73236769Sobrien			sh `/etc/rc.d/dhclient start`
74236769Sobrien		else
75236769Sobrien			start_dhcp_keep_current
76236769Sobrien		fi
77236769Sobrien		;;
78236769Sobrien	*)
79236769Sobrien		;;
80236769Sobrien	esac
81236769Sobrien}
82236769Sobrien
83236769Sobrienstart_dhcp() {
84236769Sobrien	stop_dhcp
85236769Sobrien	case ${pccard_ether_delay} in
86236769Sobrien	[Nn][Oo])
87236769Sobrien		;;
88236769Sobrien	[0-9])
89236769Sobrien		sleep ${pccard_ether_delay}
90236769Sobrien		;;
91236769Sobrien        esac
92236769Sobrien	[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
93236769Sobrien	[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
94236769Sobrien	if [ -x "${dhclient_program}" ]; then
95236769Sobrien		${dhclient_program} ${dhclient_flags}  $_dhcplist ${interface}
96236769Sobrien	else
97236769Sobrien		echo "${dhclient_program}: DHCP client software not available"
98236769Sobrien	fi
99236769Sobrien}
100236769Sobrien
101236769Sobrien# Called after detaching a card, if dhclient has been
102236769Sobrien# used for more than one interface.
103236769Sobrienstart_dhcp_keep_current() {
104236769Sobrien	[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
105236769Sobrien	[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
106236769Sobrien	if [ -x "${dhclient_program}" ]; then
107236769Sobrien		${dhclient_program} ${dhclient_flags} \
108236769Sobrien			${_dhcplist}
109236769Sobrien	else
110236769Sobrien		echo "${dhclient_program}: DHCP client software not available"
111236769Sobrien	fi
112236769Sobrien}
113236769Sobrien
114236769Sobrien# Suck in the configuration variables
115236769Sobrien#
116236769Sobrienif [ -r /etc/defaults/rc.conf ]; then
117236769Sobrien	. /etc/defaults/rc.conf
118236769Sobrien	source_rc_confs
119236769Sobrienelif [ -r /etc/rc.conf ]; then
120236769Sobrien	. /etc/rc.conf
121240330Smarcelfi
122240330Smarcel
123236769Sobrieninterface=$1
124236769Sobrienshift
125236769Sobrienstartstop=$1
126236769Sobrienshift
127236769Sobrien
128236769Sobriencase ${pccard_ifconfig} in
129236769Sobrien[Nn][Oo] | '')
130236769Sobrien	expr "${removable_interfaces}" : ".*${interface}" > /dev/null || exit 0
131236769Sobrien	;;
132236769Sobrien*)
133236769Sobrien	# Backward compatible
134236769Sobrien	eval ifconfig_${interface}=\${pccard_ifconfig}
135236769Sobrien	;;
136236769Sobrienesac
137236769Sobrien
138236769Sobriencase ${startstop} in
139236769Sobrien[Ss][Tt][Aa][Rr][Tt] | '')
140236769Sobrien	if [ -x /usr/bin/grep ]; then
141236769Sobrien		if ifconfig ${interface} | grep -s UP, > /dev/null 2>&1; then
142236769Sobrien		    # Interface is already up, so ignore it.
143236769Sobrien		    exit 0
144236769Sobrien		fi
145236769Sobrien	fi
146236769Sobrien
147236769Sobrien	if [ -r /etc/start_if.${interface} ]; then
148236769Sobrien		. /etc/start_if.${interface}
149236769Sobrien	fi
150236769Sobrien
151236769Sobrien	eval ifconfig_args=\$ifconfig_${interface}
152236769Sobrien	case ${ifconfig_args} in
153236769Sobrien	[Nn][Oo] | '')
154236769Sobrien		;;
155236769Sobrien	[Dd][Hh][Cc][Pp])
156236769Sobrien		# Start up the DHCP client program
157236769Sobrien		start_dhcp
158236769Sobrien		;;
159236769Sobrien	*)
160236769Sobrien		# Do the primary ifconfig if specified
161236769Sobrien		ifconfig ${interface} ${ifconfig_args} $*
162236769Sobrien
163236769Sobrien		# Check to see if aliases need to be added
164236769Sobrien		alias=0
165236769Sobrien		while :
166236769Sobrien		do
167236769Sobrien			eval ifx_args=\$ifconfig_${interface}_alias${alias}
168236769Sobrien			if [ -n "${ifx_args}" ]; then
169236769Sobrien				ifconfig ${interface} ${ifx_args} alias
170236769Sobrien				alias=`expr ${alias} + 1`
171236769Sobrien			else
172236769Sobrien				break;
173236769Sobrien			fi
174236769Sobrien		done
175236769Sobrien
176236769Sobrien		# Do ipx address if specified
177236769Sobrien		eval ifx_args=\$ifconfig_${interface}_ipx
178236769Sobrien		if [ -n "${ifx_args}" ]; then
179236769Sobrien			ifconfig ${interface} ${ifx_args}
180236769Sobrien		fi
181296637Ssjg
182296637Ssjg		# Add default route into $static_routes
183296637Ssjg		case ${defaultrouter} in
184296637Ssjg		[Nn][Oo] | '')
185296637Ssjg		        ;;
186236769Sobrien		*)
187236769Sobrien			static_routes="default ${static_routes}"
188236769Sobrien			route_default="default ${defaultrouter}"
189236769Sobrien			;;
190236769Sobrien		esac
191236769Sobrien
192236769Sobrien		# Add private route for this interface into $static_routes
193236769Sobrien		eval ifx_routes=\$static_routes_${interface}
194236769Sobrien		if [ -n "${ifx_routes}" ]; then
195236769Sobrien			static_routes="${ifx_routes} ${static_routes}"
196236769Sobrien		fi
197236769Sobrien
198236769Sobrien		# Set up any static routes if specified
199236769Sobrien		if [ -n "${static_routes}" ]; then
200236769Sobrien			for i in ${static_routes}; do
201236769Sobrien				eval route_args=\$route_${i}
202236769Sobrien				route add ${route_args}
203236769Sobrien			done
204236769Sobrien		fi
205236769Sobrien		;;
206236769Sobrien	esac
207236769Sobrien
208236769Sobrien	# IPv6 setup
209236769Sobrien	case ${ipv6_enable} in
210236769Sobrien	[Yy][Ee][Ss])
211236769Sobrien		if [ -r /etc/network.subr ]; then
212236769Sobrien			. /etc/network.subr
213236769Sobrien			network6_interface_setup ${interface}
214236769Sobrien		fi
215236769Sobrien		;;
216236769Sobrien	esac
217236769Sobrien	;;
218236769Sobrien# Stop the interface
219236769Sobrien*)
220236769Sobrien	if [ -r /etc/stop_if.${interface} ]; then
221281812Ssjg		. /etc/stop_if.${interface}
222281812Ssjg	fi
223281812Ssjg
224236769Sobrien	eval ifconfig_args=\$ifconfig_${interface}
225236769Sobrien	case ${ifconfig_args} in
226236769Sobrien	[Nn][Oo] | '')
227236769Sobrien	        ;;
228236769Sobrien	[Dd][Hh][Cc][Pp])
229236769Sobrien		# Stop the DHCP client for this interface
230236769Sobrien		stop_dhcp
231236769Sobrien		;;
232236769Sobrien	*)
233236769Sobrien		# Delete static route if specified
234236769Sobrien		eval ifx_routes=\$static_routes_${interface}
235236769Sobrien		if [ -n "${ifx_routes}" ]; then
236236769Sobrien			for i in ${ifx_routes}; do
237236769Sobrien				eval route_args=\$route_${i}
238236769Sobrien				route delete ${route_args}
239236769Sobrien			done
240236769Sobrien		fi
241236769Sobrien
242236769Sobrien		# Delete aliases if exist
243236769Sobrien		alias=0
244236769Sobrien		while :
245236769Sobrien		do
246236769Sobrien			eval ifx_args=\$ifconfig_${interface}_alias${alias}
247236769Sobrien			if [ -n "${ifx_args}" ]; then
248236769Sobrien				ifconfig ${interface} ${ifx_args} alias delete
249236769Sobrien				alias=`expr ${alias} + 1`
250236769Sobrien			else
251236769Sobrien				break;
252236769Sobrien			fi
253236769Sobrien		done
254236769Sobrien		;;
255236769Sobrien	esac
256236769Sobrien
257236769Sobrien	# Remove the network interface and cleaning ARP table
258236769Sobrien	ifconfig ${interface} delete
259236769Sobrien	arp -d -a
260236769Sobrien
261236769Sobrien	# Clean the routing table
262236769Sobrien	case ${removable_route_flush} in
263236769Sobrien	[Nn][Oo])
264236769Sobrien	        ;;
265236769Sobrien	*)	
266236769Sobrien		# flush beforehand, just in case....
267236769Sobrien		route -n flush -inet
268236769Sobrien		;;
269236769Sobrien	esac
270236769Sobrien	;;
271236769Sobrienesac
272236769Sobrien