pccard_ether revision 118845
1#!/bin/sh -
2#
3# $FreeBSD: head/etc/pccard_ether 118845 2003-08-12 22:27:08Z mbr $
4#
5# pccard_ether interfacename [start|stop] [ifconfig option]
6#
7# example: pccard_ether fxp0 start link0
8#
9
10. /etc/network.subr
11
12stop_dhcp() {
13	# If dhclient is already running, record
14	# its interfaces.
15	if [ -x /usr/bin/grep ]; then
16		eval _active_list=\"`/bin/ps -axwww | \
17			/usr/bin/grep dhclient | \
18			/usr/bin/grep -v grep | \
19			/usr/bin/sed -e 's|^.*dhclient||' | \
20			/usr/bin/awk '{for (i=1;i<=NF;i++) \
21				{ if ($i~/[a-zA-Z].[0-9]$/) \
22				{ printf(" %s",$i) } }}'` \
23			\"
24	fi
25
26	# Get the rc.conf list of dhcp configured interfaces
27	static_dhcp_list="`list_net_interfaces dhcp`"
28
29	# Get the current ifconfig list of interfaces
30	_aprefix=
31	_nlist=
32	for _if in ${_active_list} ; do
33		_test_if=`ifconfig ${_if} 2>&1`
34		case "$_test_if" in
35		"ifconfig: interface $_if does not exist")
36			;;
37		${interface})
38			# Don't record the same device twice.
39			;;
40		*)
41			#
42			# Catch devices which were specified before,
43			# but have not been part of the rc. We need
44			# them again for the restart.
45			#
46			for _cif in ${static_dhcp_list} ; do
47				case "$_cif" in
48				${_if})
49					# Nothing to add
50					;;
51				*)
52					# Found interface beside rc.conf
53					_nlist="${_nlist}${_aprefix}${_if}"
54					;;
55				esac
56			done
57			_dhcplist="${_dhcplist}${_aprefix}${_if}"
58			[ -z "$_aprefix" ] && _aprefix=' '
59			;;
60		esac
61	done
62
63	if [ -s /var/run/dhclient.pid ]; then
64		pidfile="/var/run/dhclient.pid"
65	else
66		return
67	fi
68	kill `cat ${pidfile}`
69	rm -f ${pidfile}
70	case ${startstop} in
71	[Ss][Tt][Oo][Pp])
72		if [ -z "${_nlist}" ]; then
73			sh `/etc/rc.d/dhclient start`
74		else
75			start_dhcp_keep_current
76		fi
77		;;
78	*)
79		;;
80	esac
81}
82
83start_dhcp() {
84	case ${pccard_ether_delay} in
85	[Nn][Oo])
86		;;
87	[0-9])
88		sleep ${pccard_ether_delay}
89		;;
90        esac
91	stop_dhcp
92	[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
93	[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
94	if [ -x "${dhclient_program}" ]; then
95		${dhclient_program} ${dhclient_flags}  $_dhcplist ${interface}
96	else
97		echo "${dhclient_program}: DHCP client software not available"
98	fi
99}
100
101# Called after detaching a card, if dhclient has been
102# used for more than one interface.
103start_dhcp_keep_current() {
104	[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
105	[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
106	if [ -x "${dhclient_program}" ]; then
107		${dhclient_program} ${dhclient_flags} \
108			${_dhcplist}
109	else
110		echo "${dhclient_program}: DHCP client software not available"
111	fi
112}
113
114# Suck in the configuration variables
115#
116if [ -r /etc/defaults/rc.conf ]; then
117	. /etc/defaults/rc.conf
118	source_rc_confs
119elif [ -r /etc/rc.conf ]; then
120	. /etc/rc.conf
121fi
122
123interface=$1
124shift
125startstop=$1
126shift
127
128case ${pccard_ifconfig} in
129[Nn][Oo] | '')
130	expr "${removable_interfaces}" : ".*${interface}" > /dev/null || exit 0
131	;;
132*)
133	# Backward compatible
134	eval ifconfig_${interface}=\${pccard_ifconfig}
135	;;
136esac
137
138case ${startstop} in
139[Ss][Tt][Aa][Rr][Tt] | '')
140	if [ -x /usr/bin/grep ]; then
141		if ifconfig ${interface} | grep -s UP, > /dev/null 2>&1; then
142		    # Interface is already up, so ignore it.
143		    exit 0
144		fi
145	fi
146
147	if [ -r /etc/start_if.${interface} ]; then
148		. /etc/start_if.${interface}
149	fi
150
151	eval ifconfig_args=\$ifconfig_${interface}
152	case ${ifconfig_args} in
153	[Nn][Oo] | '')
154		;;
155	[Dd][Hh][Cc][Pp])
156		# Start up the DHCP client program
157		start_dhcp
158		;;
159	*)
160		# Do the primary ifconfig if specified
161		ifconfig ${interface} ${ifconfig_args} $*
162
163		# Check to see if aliases need to be added
164		alias=0
165		while :
166		do
167			eval ifx_args=\$ifconfig_${interface}_alias${alias}
168			if [ -n "${ifx_args}" ]; then
169				ifconfig ${interface} ${ifx_args} alias
170				alias=`expr ${alias} + 1`
171			else
172				break;
173			fi
174		done
175
176		# Do ipx address if specified
177		eval ifx_args=\$ifconfig_${interface}_ipx
178		if [ -n "${ifx_args}" ]; then
179			ifconfig ${interface} ${ifx_args}
180		fi
181
182		# Add default route into $static_routes
183		case ${defaultrouter} in
184		[Nn][Oo] | '')
185		        ;;
186		*)
187			static_routes="default ${static_routes}"
188			route_default="default ${defaultrouter}"
189			;;
190		esac
191
192		# Add private route for this interface into $static_routes
193		eval ifx_routes=\$static_routes_${interface}
194		if [ -n "${ifx_routes}" ]; then
195			static_routes="${ifx_routes} ${static_routes}"
196		fi
197
198		# Set up any static routes if specified
199		if [ -n "${static_routes}" ]; then
200			for i in ${static_routes}; do
201				eval route_args=\$route_${i}
202				route add ${route_args}
203			done
204		fi
205		;;
206	esac
207
208	# IPv6 setup
209	case ${ipv6_enable} in
210	[Yy][Ee][Ss])
211		if [ -r /etc/network.subr ]; then
212			. /etc/network.subr
213			network6_interface_setup ${interface}
214		fi
215		;;
216	esac
217	;;
218# Stop the interface
219*)
220	if [ -r /etc/stop_if.${interface} ]; then
221		. /etc/stop_if.${interface}
222	fi
223
224	eval ifconfig_args=\$ifconfig_${interface}
225	case ${ifconfig_args} in
226	[Nn][Oo] | '')
227	        ;;
228	[Dd][Hh][Cc][Pp])
229		# Stop the DHCP client for this interface
230		stop_dhcp
231		;;
232	*)
233		# Delete static route if specified
234		eval ifx_routes=\$static_routes_${interface}
235		if [ -n "${ifx_routes}" ]; then
236			for i in ${ifx_routes}; do
237				eval route_args=\$route_${i}
238				route delete ${route_args}
239			done
240		fi
241
242		# Delete aliases if exist
243		alias=0
244		while :
245		do
246			eval ifx_args=\$ifconfig_${interface}_alias${alias}
247			if [ -n "${ifx_args}" ]; then
248				ifconfig ${interface} ${ifx_args} alias delete
249				alias=`expr ${alias} + 1`
250			else
251				break;
252			fi
253		done
254		;;
255	esac
256
257	# Remove the network interface and cleaning ARP table
258	ifconfig ${interface} delete
259	arp -d -a
260
261	# Clean the routing table
262	case ${removable_route_flush} in
263	[Nn][Oo])
264	        ;;
265	*)	
266		# flush beforehand, just in case....
267		route -n flush -inet
268		;;
269	esac
270	;;
271esac
272