/[baselayout]/branches/baselayout-1_12/net-scripts/net.modules.d/wpa_supplicant
Gentoo

Contents of /branches/baselayout-1_12/net-scripts/net.modules.d/wpa_supplicant

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1753 - (show annotations) (download)
Fri Dec 9 15:08:06 2005 UTC (8 years, 7 months ago) by uberlord
File size: 10351 byte(s)
Remove nasty undocumented backgrounding as we now support
ifplugd, netplug and wpa_supplicant-0.4.x which do much better jobs.
1 #!/bin/bash
2 # Copyright (c) 2004-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6
7 # Fix any potential localisation problems
8 # Note that LC_ALL trumps LC_anything_else according to locale(7)
9 wpa_supplicant() {
10 LC_ALL=C /sbin/wpa_supplicant "$@"
11 }
12
13 wpa_cli() {
14 LC_ALL=C /bin/wpa_cli "$@"
15 }
16
17 # void wpa_supplicant_depend(void)
18 #
19 # Sets up the dependancies for the module
20 wpa_supplicant_depend() {
21 after macnet plug
22 before interface
23 provide wireless
24 functions interface_exists
25 }
26
27 # bool wpa_supplicant_check_installed(void)
28 #
29 # Returns 0 if wpa_supplicant is installed, otherwise 1
30 wpa_supplicant_check_installed() {
31 local report="${1:-false}" installed="0"
32 if [[ ! -x /sbin/wpa_supplicant ]]; then
33 installed="1"
34 ${report} && eerror "For WPA support (wpa_supplicant) support, emerge net-wireless/wpa_supplicant"
35 fi
36 if [[ ! -e /proc/net/packet ]]; then
37 installed="1"
38 if ${report} ; then
39 eerror "wpa_supplicant requires Packet Socket"
40 eerror "(CONFIG_PACKET=y) enabled in the kernel"
41 fi
42 fi
43 return "${installed}"
44 }
45
46 # bool wpa_supplicant_exists(char *interface)
47 #
48 # Checks to see if wireless extensions are enabled on the interface
49 wpa_supplicant_exists() {
50 [[ ! -e /proc/net/wireless ]] && return 1
51 grep -q "^[ \t]*$1:[ \t]" /proc/net/wireless
52 }
53
54 # char* wpa_supplicant_get_essid(char *interface)
55 #
56 # Gets the current ESSID of iface
57 wpa_supplicant_get_essid() {
58 local i essid
59
60 for (( i=0; i<5; i++ )); do
61 essid=$( wpa_cli -i"$1" status | sed -n -e 's/^ssid=//p' )
62 if [[ -n ${essid} ]]; then
63 echo "${essid}"
64 return 0
65 fi
66 sleep 1
67 done
68
69 return 1
70 }
71
72 # char* wpa_supplicant_get_ap_mac_address(char *interface)
73 #
74 # Returns the MAC address of the Access Point
75 # the interface is connected to
76 wpa_supplicant_get_ap_mac_address() {
77 wpa_cli -i"$1" status | sed -n -e 's/^bssid=\([^=]\+\).*/\U\1/p'
78 }
79
80 # bool wpa_supplicant_associated(char *interface)
81 #
82 # Returns 0 if we're associated correctly or 1 if not
83 # Note that just because we are associated does not mean we are using the
84 # correct encryption keys
85 wpa_supplicant_associated() {
86 local -a status=( "$( wpa_cli -i"$1" status | sed -n -e 's/^\(key_mgmt\|wpa_state\|EAP state\)=\([^=]\+\).*/\U\2/p' )" )
87
88 case "${status[0]}" in
89 "NONE") [[ ${status[1]} == "ASSOCIATED" ]] ;;
90 "IEEE 802.1X (no WPA)") [[ ${status[2]} == "SUCCESS" ]] ;;
91 *) [[ ${status[1]} == "COMPLETED" ]] ;;
92 esac
93
94 return $?
95 }
96
97 # void wpa_supplicant_kill(char *interface, bool report)
98 #
99 # Kills any existing wpa_supplicant process on the interface
100 wpa_supplicant_kill() {
101 local iface="$1" report="${2:-false}" pidfile
102
103 # Shutdown wpa_cli first, if it's running
104 # This is important as future versions of wpa_supplicant
105 # may send a disconnect message to wpa_cli when it shutsdown
106 pidfile="/var/run/wpa_cli-${iface}.pid"
107 if ! clean_pidfile "${pidfile}" ; then
108 ${report} && ebegin "Stopping wpa_cli on ${iface}"
109 start-stop-daemon --stop --exec /bin/wpa_cli --pidfile "${pidfile}"
110 ${report} && eend "$?"
111 fi
112
113 # Now shutdown wpa_supplicant
114 pidfile="/var/run/wpa_supplicant-${iface}.pid"
115 if ! clean_pidfile "${pidfile}" ; then
116 ${report} && ebegin "Stopping wpa_supplicant on ${iface}"
117 start-stop-daemon --stop --exec /sbin/wpa_supplicant \
118 --pidfile "${pidfile}"
119 ${report} && eend "$?"
120 else
121 # Support wpa_supplicant-0.3.x
122 local pid=$( pgrep -f '^/sbin/wpa_supplicant .* -i'"${iface}"'[ ]*$' )
123 if [[ -n ${pid} ]]; then
124 ${report} && ebegin "Stopping wpa_supplicant on ${iface}"
125 kill -s TERM "${pid}"
126 ${report} && eend 0
127 fi
128 fi
129
130 # If wpa_supplicant exits uncleanly, we need to remove the stale dir
131 [[ -S "/var/run/wpa_supplicant/${iface}" ]] \
132 && rm -f "/var/run/wpa_supplicant/${iface}"
133 }
134
135 # bool wpa_supplicant_associate(char *interface)
136 #
137 # Returns 0 if wpa_supplicant associates and authenticates to an AP
138 # otherwise, 1
139 wpa_supplicant_associate() {
140 local iface="$1" ifvar=$( bash_variable "$1" ) timeout i
141 timeout="associate_timeout_${ifvar}"
142 [[ -z ${!timeout} ]] && timeout="wpa_timeout_${ifvar}"
143 timeout="${!timeout:--1}"
144
145 if [[ ${timeout} == "0" ]]; then
146 ewarn "WARNING: infinite timeout set for association on ${iface}"
147 elif [[ ${timeout} -lt 0 ]]; then
148 einfo "Backgrounding ..."
149 exit 0
150 fi
151
152 while true ; do
153 if ${action} ; then
154 service_started "net.${iface}" && return 0
155 else
156 if ! wpa_cli -i"${iface}" status &>/dev/null ; then
157 eend 1 "wpa_supplicant has exited unexpectedly"
158 return 1
159 fi
160 wpa_supplicant_associated "${iface}" && return 0
161 fi
162 sleep 1
163 (( i++ ))
164 [[ ${i} == "${timeout}" || ${i} -gt "${timeout}" ]] && break
165 done
166
167 # Spit out an appropriate error
168 if ${action} ; then
169 eend 1 "Failed to configure ${iface} in the background"
170 else
171 eend 1 "Timed out"
172 fi
173
174 # exit without error with wpa_supplicant-0.4.x as we may get kickstarted
175 # when an AP comes in range
176 ${action} && exit 0
177
178 # Kill wpa_supplicant for 0.3.x
179 wpa_supplicant_kill "${iface}"
180 return 1
181 }
182
183 # bool wpa_supplicant_pre_start(char *interface)
184 #
185 # Start wpa_supplicant on an interface and wait for association
186 # Returns 0 (true) when successful, non-zero otherwise
187 wpa_supplicant_pre_start() {
188 local iface="$1" opts timeout action=false cfgfile
189 local actfile="/sbin/wpa_cli.action"
190
191 # We don't configure wireless if we're being called from
192 # the background unless we're not currently running
193 if ${IN_BACKGROUND} ; then
194 if service_started_daemon "net.${iface}" /sbin/wpa_supplicant ; then
195 if wpa_supplicant_exists "${iface}" ; then
196 ESSID=$( wpa_supplicant_get_essid "${iface}" )
197 ESSIDVAR=$( bash_variable "${ESSID}" )
198 save_options "ESSID" "${ESSID}"
199 fi
200 return 0
201 fi
202 fi
203
204 save_options "ESSID" ""
205
206 local ifvar=$( bash_variable "${iface}" )
207 opts="wpa_supplicant_${ifvar}"
208 opts=" ${!opts} "
209 [[ ${opts} != *" -D"* ]] \
210 && ewarn "wpa_supplicant_${ifvar} does not define a driver"
211
212 # We only work on wirelesss interfaces unless a driver for wired
213 # has been defined
214 if [[ ${opts} != *" -Dwired "* && ${opts} != *" -D wired "* ]]; then
215 if ! wpa_supplicant_exists "${iface}" ; then
216 veinfo "wpa_supplicant only works on wireless interfaces"
217 veinfo "unless the -D wired option is specified"
218 return 0
219 fi
220 fi
221
222 # Check for rf_kill - only ipw supports this at present, but other
223 # cards may in the future
224 if [[ -e "/sys/class/net/${iface}/device/rf_kill" ]]; then
225 if [[ $( < "/sys/class/net/${iface}/device/rf_kill" ) != 0 ]]; then
226 eerror "Wireless radio has been killed for interface ${iface}"
227 return 1
228 fi
229 fi
230
231 # If wireless-tools is installed, try and apply our user config
232 # This is needed for some drivers - such as hostap because they start
233 # the card in Master mode which causes problems with wpa_supplicant.
234 if is_function iwconfig_defaults ; then
235 if wpa_supplicant_exists "${iface}" ; then
236 iwconfig_defaults "${iface}"
237 iwconfig_user_config "${iface}"
238 fi
239 fi
240
241 ebegin "Starting wpa_supplicant on ${iface}"
242
243 cfgfile="${opts##* -c}"
244 if [[ -n ${cfgfile} && ${cfgfile} != "${opts}" ]]; then
245 [[ ${cfgfile:0:1} == " " ]] && cfgfile="${cfgfile# *}"
246 cfgfile="${cfgfile%% *}"
247 else
248 cfgfile="/etc/wpa_supplicant.conf"
249 opts="${opts} -c/etc/wpa_supplicant.conf"
250 fi
251
252 if [[ ! -f ${cfgfile} ]]; then
253 eend 1 "configuration file ${cfgfile} not found!"
254 return 1
255 fi
256
257 local ctrl_dir=$( sed -n -e 's/[ \t]*#.*//g;s/[ \t]*$//g;s/^ctrl_interface=//p' "${cfgfile}" )
258 if [[ ${ctrl_dir} != "/var/run/wpa_supplicant" ]]; then
259 eerror "${cfgfile} must set"
260 eerror " ctrl_interface=/var/run/wpa_supplicant"
261 eend 1
262 return 1
263 fi
264
265 # Some drivers require the interface to be up
266 interface_up "${iface}"
267
268 version=$( wpa_cli -v | sed -n -e 's/wpa_cli v//p' )
269 version=( ${version//./ } )
270 (( version = version[0] * 1000 + version[1] * 100 + version[2] ))
271
272 # wpa_supplicant 0.4.0 and greater supports wpa_cli actions
273 # This is very handy as if and when different association mechanisms are
274 # introduced to wpa_supplicant we don't have to recode for them as
275 # wpa_cli is now responsible for informing us of success/failure.
276 # The downside of this is that we don't see the interface being configured
277 # for DHCP/static.
278 if [[ ${version} -gt 399 && -x ${actfile} ]]; then
279 opts="${opts} -W -P/var/run/wpa_supplicant-${iface}.pid"
280 action=true
281 fi
282
283 eval start-stop-daemon --start --exec /sbin/wpa_supplicant \
284 --pidfile "/var/run/wpa_supplicant-${iface}.pid" \
285 -- "${opts}" -B -i"${iface}"
286 eend "$?" || return 1
287
288 # Starting wpa_supplication-0.4.0, we can get wpa_cli to
289 # start/stop our scripts from wpa_supplicant messages
290 if ${action} ; then
291 mark_service_inactive "net.${iface}"
292 ebegin "Starting wpa_cli on ${iface}"
293 start-stop-daemon --start --exec /bin/wpa_cli \
294 --pidfile "/var/run/wpa_cli-${iface}.pid" \
295 -- -a"${actfile}" -i"${iface}" \
296 -P"/var/run/wpa_cli-${iface}.pid" -B
297 eend "$?" || return 1
298 fi
299
300 eindent
301 veinfo "Waiting for association"
302 eend 0
303
304 wpa_supplicant_associate "${iface}" || return 1
305
306 # Only report wireless info for wireless interfaces
307 if wpa_supplicant_exists "${iface}" ; then
308 # Set ESSID for essidnet and report
309 ESSID=$( wpa_supplicant_get_essid "${iface}" )
310 ESSIDVAR=$( bash_variable "${ESSID}" )
311 save_options "ESSID" "${ESSID}"
312
313 local -a status=( "$( wpa_cli -i${iface} status | sed -n -e 's/^\(bssid\|pairwise_cipher\|key_mgmt\)=\([^=]\+\).*/\"\U\2\"/p' | tr '[:lower:]' '[:upper:]' )" )
314 einfo "${iface} connected to \"${ESSID//\\\\/\\\\}\" at ${status[0]}"
315
316 if [[ ${status[2]} == "NONE" ]]; then
317 if [[ ${status[1]} == "NONE" ]]; then
318 ewarn "not using any encryption"
319 else
320 veinfo "using ${status[1]}"
321 fi
322 else
323 veinfo "using ${status[2]}/${status[1]}"
324 fi
325 eoutdent
326 else
327 einfo "${iface} connected"
328 fi
329
330 if ${action} ; then
331 local addr=$( interface_get_address "${iface}" )
332 einfo "${iface} configured with address ${addr}"
333 exit 0
334 fi
335
336 return 0
337 }
338
339 # bool wpa_supplicant_post_stop(char *iface)
340 #
341 # Stops wpa_supplicant on an interface
342 # Returns 0 (true) when successful, non-zero otherwise
343 wpa_supplicant_post_stop() {
344 if ${IN_BACKGROUND} ; then
345 # Only stop wpa_supplicant if it's not the controlling daemon
346 ! service_started_daemon "net.$1" /sbin/wpa_supplicant 0
347 fi
348 [[ $? == 0 ]] && wpa_supplicant_kill "$1" true
349 return 0
350 }
351
352 # vim:ts=4

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20