]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/pccard_ether
This commit was generated by cvs2svn to compensate for changes in r135601,
[FreeBSD/FreeBSD.git] / etc / pccard_ether
1 #!/bin/sh -
2 #
3 # $FreeBSD$
4 #
5 # pccard_ether interfacename [start|stop] [ifconfig option]
6 #
7 # example: pccard_ether fxp0 start link0
8 #
9
10 . /etc/network.subr
11
12 stop_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         /sbin/dhclient -r ${interface}
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
83 start_dhcp() {
84         stop_dhcp
85         case ${pccard_ether_delay} in
86         [Nn][Oo])
87                 ;;
88         [0-9]*)
89                 sleep ${pccard_ether_delay}
90                 ;;
91         esac
92         [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
93         [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
94         if [ -x "${dhclient_program}" ]; then
95                 interfaces=`echo $_dhcplist ${interface} | xargs -n 1 echo | sort -u`
96                 ${dhclient_program} ${dhclient_flags}  ${interfaces}
97         else
98                 echo "${dhclient_program}: DHCP client software not available"
99         fi
100 }
101
102 # Called after detaching a card, if dhclient has been
103 # used for more than one interface.
104 start_dhcp_keep_current() {
105         [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
106         [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
107         if [ -x "${dhclient_program}" ]; then
108                 ${dhclient_program} ${dhclient_flags} \
109                         ${_dhcplist}
110         else
111                 echo "${dhclient_program}: DHCP client software not available"
112         fi
113 }
114
115 # Suck in the configuration variables
116 #
117 if [ -r /etc/defaults/rc.conf ]; then
118         . /etc/defaults/rc.conf
119         source_rc_confs
120 elif [ -r /etc/rc.conf ]; then
121         . /etc/rc.conf
122 fi
123
124 interface=$1
125 shift
126 startstop=$1
127 shift
128
129 case ${pccard_ifconfig} in
130 [Nn][Oo] | '')
131         expr "${removable_interfaces}" : ".*${interface}" > /dev/null || exit 0
132         ;;
133 *)
134         # Backward compatible
135         eval ifconfig_${interface}=\${pccard_ifconfig}
136         ;;
137 esac
138
139 case ${startstop} in
140 [Ss][Tt][Aa][Rr][Tt] | '')
141         if [ -x /usr/bin/grep ]; then
142                 if ifconfig ${interface} | grep -s netmask > /dev/null 2>&1; then
143                     # Interface is already up, so ignore it.
144                     exit 0
145                 fi
146         fi
147
148         if [ -r /etc/start_if.${interface} ]; then
149                 . /etc/start_if.${interface}
150         fi
151
152         eval ifconfig_args=\$ifconfig_${interface}
153         case ${ifconfig_args} in
154         [Nn][Oo] | '')
155                 ;;
156         [Dd][Hh][Cc][Pp])
157                 # Start up the DHCP client program
158                 start_dhcp
159                 ;;
160         *)
161                 # Do the primary ifconfig if specified
162                 ifconfig ${interface} ${ifconfig_args} $*
163
164                 # Check to see if aliases need to be added
165                 alias=0
166                 while :
167                 do
168                         eval ifx_args=\$ifconfig_${interface}_alias${alias}
169                         if [ -n "${ifx_args}" ]; then
170                                 ifconfig ${interface} ${ifx_args} alias
171                                 alias=`expr ${alias} + 1`
172                         else
173                                 break;
174                         fi
175                 done
176
177                 # Do ipx address if specified
178                 eval ifx_args=\$ifconfig_${interface}_ipx
179                 if [ -n "${ifx_args}" ]; then
180                         ifconfig ${interface} ${ifx_args}
181                 fi
182
183                 # Add default route into $static_routes
184                 case ${defaultrouter} in
185                 [Nn][Oo] | '')
186                         ;;
187                 *)
188                         static_routes="default ${static_routes}"
189                         route_default="default ${defaultrouter}"
190                         ;;
191                 esac
192
193                 # Add private route for this interface into $static_routes
194                 eval ifx_routes=\$static_routes_${interface}
195                 if [ -n "${ifx_routes}" ]; then
196                         static_routes="${ifx_routes} ${static_routes}"
197                 fi
198
199                 # Set up any static routes if specified
200                 if [ -n "${static_routes}" ]; then
201                         for i in ${static_routes}; do
202                                 eval route_args=\$route_${i}
203                                 route add ${route_args}
204                         done
205                 fi
206                 ;;
207         esac
208
209         # IPv6 setup
210         case ${ipv6_enable} in
211         [Yy][Ee][Ss])
212                 if [ -r /etc/network.subr ]; then
213                         . /etc/network.subr
214                         network6_interface_setup ${interface}
215                 fi
216                 ;;
217         esac
218         ;;
219 # Stop the interface
220 *)
221         if [ -r /etc/stop_if.${interface} ]; then
222                 . /etc/stop_if.${interface}
223         fi
224
225         eval ifconfig_args=\$ifconfig_${interface}
226         case ${ifconfig_args} in
227         [Nn][Oo] | '')
228                 ;;
229         [Dd][Hh][Cc][Pp])
230                 # Stop the DHCP client for this interface
231                 stop_dhcp
232                 ;;
233         *)
234                 # Delete static route if specified
235                 eval ifx_routes=\$static_routes_${interface}
236                 if [ -n "${ifx_routes}" ]; then
237                         for i in ${ifx_routes}; do
238                                 eval route_args=\$route_${i}
239                                 route delete ${route_args}
240                         done
241                 fi
242
243                 # Delete aliases if exist
244                 alias=0
245                 while :
246                 do
247                         eval ifx_args=\$ifconfig_${interface}_alias${alias}
248                         if [ -n "${ifx_args}" ]; then
249                                 ifconfig ${interface} ${ifx_args} alias delete
250                                 alias=`expr ${alias} + 1`
251                         else
252                                 break;
253                         fi
254                 done
255                 ;;
256         esac
257
258         # Remove the network interface and cleaning ARP table
259         ifconfig ${interface} delete
260         arp -d -a
261
262         # Clean the routing table
263         case ${removable_route_flush} in
264         [Nn][Oo])
265                 ;;
266         *)
267                 # flush beforehand, just in case....
268                 route -n flush -inet
269                 ;;
270         esac
271         ;;
272 esac