pccard_ether revision 118829
138774Snsouch#!/bin/sh -
238774Snsouch#
338774Snsouch# $FreeBSD: head/etc/pccard_ether 118829 2003-08-12 15:15:34Z mbr $
438774Snsouch#
538774Snsouch# pccard_ether interfacename [start|stop] [ifconfig option]
638774Snsouch#
738774Snsouch# example: pccard_ether fxp0 start link0
838774Snsouch#
938774Snsouch
1038774Snsouchstop_dhcp() {
1138774Snsouch	if [ -s /var/run/dhclient.${interface}.pid ]; then
1238774Snsouch		pidfile="/var/run/dhclient.${interface}.pid"
1338774Snsouch	elif [ -s /var/run/dhcpc.${interface}.pid ]; then
1438774Snsouch		pidfile="/var/run/dhcpc.${interface}.pid"
1538774Snsouch	elif [ -s /var/run/dhclient.pid ]; then
1638774Snsouch		# If dhclient is already running, record
1738774Snsouch		# it's interfaces.
1838774Snsouch		if [ -x /usr/bin/grep ]; then
1938774Snsouch			eval _active_list=\"`/bin/ps -axwww | \
2038774Snsouch				/usr/bin/grep dhclient | \
2138774Snsouch				/usr/bin/grep -v grep | \
2238774Snsouch				/usr/bin/sed -e 's|^.*dhclient||' | \
2338774Snsouch				/usr/bin/awk '{for (i=1;i<=NF;i++) \
2438774Snsouch					{ if ($i~/[a-zA-Z].[0-9]$/) \
2538774Snsouch					{ printf(" %s",$i) } }}'` \
2650477Speter				\"
2738774Snsouch		fi
2838774Snsouch
2959093Sdfr		_aprefix=
3059093Sdfr		for _if in ${_active_list} ; do
3141012Snsouch			_test_if=`ifconfig ${_if} 2>&1`
3238774Snsouch			case "$_test_if" in
3338774Snsouch			"ifconfig: interface $_if does not exist")
3440782Snsouch				;;
3538774Snsouch			*)
3638774Snsouch				_dhcplist="${_dhcplist}${_aprefix}${_if}"
3738774Snsouch				[ -z "$_aprefix" ] && _aprefix=' '
3838774Snsouch				;;
3938774Snsouch			esac
4038774Snsouch		done
4138774Snsouch
4238774Snsouch		pidfile="/var/run/dhclient.pid"
4340782Snsouch	else
4440782Snsouch		return
4540782Snsouch	fi
4640782Snsouch	kill `cat ${pidfile}`
4740782Snsouch	rm -f ${pidfile}
4840782Snsouch	sh `/etc/rc.d/dhclient start`
4940782Snsouch}
5040782Snsouch
5140782Snsouchstart_dhcp() {
5238774Snsouch	stop_dhcp
5338774Snsouch	case ${pccard_ether_delay} in
5438774Snsouch	[Nn][Oo])
5538774Snsouch		;;
5638774Snsouch	[0-9])
5740782Snsouch		sleep ${pccard_ether_delay}
5838774Snsouch		;;
5938774Snsouch        esac
6038774Snsouch	[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
6138774Snsouch	[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
6238774Snsouch	if [ -x "${dhclient_program}" ]; then
6338774Snsouch		if [ `basename ${dhclient_program}` = "dhclient" ]; then
6438774Snsouch			pidfile="/var/run/dhclient.${interface}.pid"
6538774Snsouch			dhclient_flags="${dhclient_flags} -pf ${pidfile}"
6640782Snsouch		fi
6738774Snsouch		${dhclient_program} ${dhclient_flags} $_dhcplist ${interface}
6838774Snsouch	else
6938774Snsouch		echo "${dhclient_program}: DHCP client software not available"
7038774Snsouch	fi
7138774Snsouch}
7238774Snsouch
7338774Snsouch# Suck in the configuration variables
7438774Snsouch#
7538774Snsouchif [ -r /etc/defaults/rc.conf ]; then
7638774Snsouch	. /etc/defaults/rc.conf
7738774Snsouch	source_rc_confs
7838774Snsouchelif [ -r /etc/rc.conf ]; then
7938774Snsouch	. /etc/rc.conf
8038774Snsouchfi
8138774Snsouch
8238774Snsouchinterface=$1
8338774Snsouchshift
8440782Snsouchstartstop=$1
8540782Snsouchshift
8638774Snsouch
8738774Snsouchcase ${pccard_ifconfig} in
8838774Snsouch[Nn][Oo] | '')
8938774Snsouch	expr "${removable_interfaces}" : ".*${interface}" > /dev/null || exit 0
9038774Snsouch	;;
9138774Snsouch*)
9238774Snsouch	# Backward compatible
9338774Snsouch	eval ifconfig_${interface}=\${pccard_ifconfig}
9438774Snsouch	;;
9538774Snsouchesac
9640782Snsouch
9738774Snsouchcase ${startstop} in
9838774Snsouch[Ss][Tt][Aa][Rr][Tt] | '')
9938774Snsouch	if ifconfig ${interface} | grep -s UP, > /dev/null 2>&1; then
10038774Snsouch	    # Interface is already up, so ignore it.
10138774Snsouch	    exit 0
10238774Snsouch	fi
10338774Snsouch
10438774Snsouch	if [ -r /etc/start_if.${interface} ]; then
10540782Snsouch		. /etc/start_if.${interface}
10640782Snsouch	fi
10738774Snsouch
108	eval ifconfig_args=\$ifconfig_${interface}
109	case ${ifconfig_args} in
110	[Nn][Oo] | '')
111		;;
112	[Dd][Hh][Cc][Pp])
113		# Start up the DHCP client program
114		start_dhcp
115		;;
116	*)
117		# Do the primary ifconfig if specified
118		ifconfig ${interface} ${ifconfig_args} $*
119
120		# Check to see if aliases need to be added
121		alias=0
122		while :
123		do
124			eval ifx_args=\$ifconfig_${interface}_alias${alias}
125			if [ -n "${ifx_args}" ]; then
126				ifconfig ${interface} ${ifx_args} alias
127				alias=`expr ${alias} + 1`
128			else
129				break;
130			fi
131		done
132
133		# Do ipx address if specified
134		eval ifx_args=\$ifconfig_${interface}_ipx
135		if [ -n "${ifx_args}" ]; then
136			ifconfig ${interface} ${ifx_args}
137		fi
138
139		# Add default route into $static_routes
140		case ${defaultrouter} in
141		[Nn][Oo] | '')
142		        ;;
143		*)
144			static_routes="default ${static_routes}"
145			route_default="default ${defaultrouter}"
146			;;
147		esac
148
149		# Add private route for this interface into $static_routes
150		eval ifx_routes=\$static_routes_${interface}
151		if [ -n "${ifx_routes}" ]; then
152			static_routes="${ifx_routes} ${static_routes}"
153		fi
154
155		# Set up any static routes if specified
156		if [ -n "${static_routes}" ]; then
157			for i in ${static_routes}; do
158				eval route_args=\$route_${i}
159				route add ${route_args}
160			done
161		fi
162		;;
163	esac
164
165	# IPv6 setup
166	case ${ipv6_enable} in
167	[Yy][Ee][Ss])
168		if [ -r /etc/network.subr ]; then
169			. /etc/network.subr
170			network6_interface_setup ${interface}
171		fi
172		;;
173	esac
174	;;
175# Stop the interface
176*)
177	if [ -r /etc/stop_if.${interface} ]; then
178		. /etc/stop_if.${interface}
179	fi
180
181	eval ifconfig_args=\$ifconfig_${interface}
182	case ${ifconfig_args} in
183	[Nn][Oo] | '')
184	        ;;
185	[Dd][Hh][Cc][Pp])
186		# Stop the DHCP client for this interface
187		stop_dhcp
188		;;
189	*)
190		# Delete static route if specified
191		eval ifx_routes=\$static_routes_${interface}
192		if [ -n "${ifx_routes}" ]; then
193			for i in ${ifx_routes}; do
194				eval route_args=\$route_${i}
195				route delete ${route_args}
196			done
197		fi
198
199		# Delete aliases if exist
200		alias=0
201		while :
202		do
203			eval ifx_args=\$ifconfig_${interface}_alias${alias}
204			if [ -n "${ifx_args}" ]; then
205				ifconfig ${interface} ${ifx_args} alias delete
206				alias=`expr ${alias} + 1`
207			else
208				break;
209			fi
210		done
211		;;
212	esac
213
214	# Remove the network interface and cleaning ARP table
215	ifconfig ${interface} delete
216	arp -d -a
217
218	# Clean the routing table
219	case ${removable_route_flush} in
220	[Nn][Oo])
221	        ;;
222	*)	
223		# flush beforehand, just in case....
224		route -n flush -inet
225		;;
226	esac
227	;;
228esac
229