pccard_ether revision 118829
1#!/bin/sh -
2#
3# $FreeBSD: head/etc/pccard_ether 118829 2003-08-12 15:15:34Z mbr $
4#
5# pccard_ether interfacename [start|stop] [ifconfig option]
6#
7# example: pccard_ether fxp0 start link0
8#
9
10stop_dhcp() {
11	if [ -s /var/run/dhclient.${interface}.pid ]; then
12		pidfile="/var/run/dhclient.${interface}.pid"
13	elif [ -s /var/run/dhcpc.${interface}.pid ]; then
14		pidfile="/var/run/dhcpc.${interface}.pid"
15	elif [ -s /var/run/dhclient.pid ]; then
16		# If dhclient is already running, record
17		# it's interfaces.
18		if [ -x /usr/bin/grep ]; then
19			eval _active_list=\"`/bin/ps -axwww | \
20				/usr/bin/grep dhclient | \
21				/usr/bin/grep -v grep | \
22				/usr/bin/sed -e 's|^.*dhclient||' | \
23				/usr/bin/awk '{for (i=1;i<=NF;i++) \
24					{ if ($i~/[a-zA-Z].[0-9]$/) \
25					{ printf(" %s",$i) } }}'` \
26				\"
27		fi
28
29		_aprefix=
30		for _if in ${_active_list} ; do
31			_test_if=`ifconfig ${_if} 2>&1`
32			case "$_test_if" in
33			"ifconfig: interface $_if does not exist")
34				;;
35			*)
36				_dhcplist="${_dhcplist}${_aprefix}${_if}"
37				[ -z "$_aprefix" ] && _aprefix=' '
38				;;
39			esac
40		done
41
42		pidfile="/var/run/dhclient.pid"
43	else
44		return
45	fi
46	kill `cat ${pidfile}`
47	rm -f ${pidfile}
48	sh `/etc/rc.d/dhclient start`
49}
50
51start_dhcp() {
52	stop_dhcp
53	case ${pccard_ether_delay} in
54	[Nn][Oo])
55		;;
56	[0-9])
57		sleep ${pccard_ether_delay}
58		;;
59        esac
60	[ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
61	[ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
62	if [ -x "${dhclient_program}" ]; then
63		if [ `basename ${dhclient_program}` = "dhclient" ]; then
64			pidfile="/var/run/dhclient.${interface}.pid"
65			dhclient_flags="${dhclient_flags} -pf ${pidfile}"
66		fi
67		${dhclient_program} ${dhclient_flags} $_dhcplist ${interface}
68	else
69		echo "${dhclient_program}: DHCP client software not available"
70	fi
71}
72
73# Suck in the configuration variables
74#
75if [ -r /etc/defaults/rc.conf ]; then
76	. /etc/defaults/rc.conf
77	source_rc_confs
78elif [ -r /etc/rc.conf ]; then
79	. /etc/rc.conf
80fi
81
82interface=$1
83shift
84startstop=$1
85shift
86
87case ${pccard_ifconfig} in
88[Nn][Oo] | '')
89	expr "${removable_interfaces}" : ".*${interface}" > /dev/null || exit 0
90	;;
91*)
92	# Backward compatible
93	eval ifconfig_${interface}=\${pccard_ifconfig}
94	;;
95esac
96
97case ${startstop} in
98[Ss][Tt][Aa][Rr][Tt] | '')
99	if ifconfig ${interface} | grep -s UP, > /dev/null 2>&1; then
100	    # Interface is already up, so ignore it.
101	    exit 0
102	fi
103
104	if [ -r /etc/start_if.${interface} ]; then
105		. /etc/start_if.${interface}
106	fi
107
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