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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 252 - (show annotations) (download)
Thu Feb 23 13:39:22 2006 UTC (12 years, 4 months ago) by phreak
File size: 10836 byte(s)
Merging baselayout r1924
1 #!/bin/bash
2 # Copyright (c) 2004-2006 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
87 eval status=( $( wpa_cli -i"$1" status | sed -n -e 's/^\(key_mgmt\|wpa_state\|EAP state\)=\([^=]\+\).*/\U\"\2\"/p' ) )
88
89 case "${status[0]}" in
90 "NONE") [[ ${status[1]} == "ASSOCIATED" || ${status[1]} == "COMPLETED" ]] ;;
91 "IEEE 802.1X (no WPA)") [[ ${status[2]} == "SUCCESS" ]] ;;
92 *) [[ ${status[1]} == "COMPLETED" ]] ;;
93 esac
94
95 return $?
96 }
97
98 # void wpa_supplicant_kill(char *interface, bool report)
99 #
100 # Kills any existing wpa_supplicant process on the interface
101 wpa_supplicant_kill() {
102 local iface="$1" report="${2:-false}" pidfile
103
104 # Shutdown wpa_cli first, if it's running
105 # This is important as future versions of wpa_supplicant
106 # may send a disconnect message to wpa_cli when it shutsdown
107 pidfile="/var/run/wpa_cli-${iface}.pid"
108 if ! clean_pidfile "${pidfile}" ; then
109 ${report} && ebegin "Stopping wpa_cli on ${iface}"
110 start-stop-daemon --stop --exec /bin/wpa_cli --pidfile "${pidfile}"
111 ${report} && eend "$?"
112 fi
113
114 # Now shutdown wpa_supplicant
115 pidfile="/var/run/wpa_supplicant-${iface}.pid"
116 if ! clean_pidfile "${pidfile}" ; then
117 ${report} && ebegin "Stopping wpa_supplicant on ${iface}"
118 start-stop-daemon --stop --exec /sbin/wpa_supplicant \
119 --pidfile "${pidfile}"
120 ${report} && eend "$?"
121 else
122 # Support wpa_supplicant-0.3.x
123 local pid="$( pgrep -f "^/sbin/wpa_supplicant .* -i${iface}[ ]*$" )"
124 if [[ -n ${pid} ]]; then
125 ${report} && ebegin "Stopping wpa_supplicant on ${iface}"
126 kill -s TERM "${pid}"
127 ${report} && eend 0
128 fi
129 fi
130
131 # If wpa_supplicant exits uncleanly, we need to remove the stale dir
132 [[ -S "/var/run/wpa_supplicant/${iface}" ]] \
133 && rm -f "/var/run/wpa_supplicant/${iface}"
134 }
135
136 # bool wpa_supplicant_associate(char *interface)
137 #
138 # Returns 0 if wpa_supplicant associates and authenticates to an AP
139 # otherwise, 1
140 wpa_supplicant_associate() {
141 local iface="$1" ifvar="$( bash_variable "$1" )" timeout
142 timeout="associate_timeout_${ifvar}"
143 [[ -z ${!timeout} ]] && timeout="wpa_timeout_${ifvar}"
144 timeout="${!timeout:--1}"
145
146 if [[ ${timeout} == "0" ]]; then
147 ewarn "WARNING: infinite timeout set for association on ${iface}"
148 elif [[ ${timeout} -lt 0 ]]; then
149 einfo "Backgrounding ..."
150 exit 0
151 fi
152
153 local i=0
154 while true ; do
155 if [[ -n ${actfile} ]] ; then
156 service_started "net.${iface}" && return 0
157 else
158 if ! wpa_cli -i"${iface}" status &>/dev/null ; then
159 eend 1 "wpa_supplicant has exited unexpectedly"
160 return 1
161 fi
162 wpa_supplicant_associated "${iface}" && return 0
163 fi
164 sleep 1
165 [[ ${timeout} == "0" ]] && continue
166 (( i++ ))
167 [[ ${i} == "${timeout}" || ${i} -gt "${timeout}" ]] && break
168 done
169
170 # Spit out an appropriate error
171 if [[ -n ${actfile} ]] ; then
172 eend 1 "Failed to configure ${iface} in the background"
173 else
174 eend 1 "Timed out"
175 fi
176
177 # exit without error with wpa_supplicant-0.4.x as we may get kickstarted
178 # when an AP comes in range
179 [[ -n ${actfile} ]] && exit 0
180
181 # Kill wpa_supplicant for 0.3.x
182 wpa_supplicant_kill "${iface}"
183 return 1
184 }
185
186 # bool wpa_supplicant_pre_start(char *interface)
187 #
188 # Start wpa_supplicant on an interface and wait for association
189 # Returns 0 (true) when successful, non-zero otherwise
190 wpa_supplicant_pre_start() {
191 local iface="$1" opts="" timeout="" actfile="" cfgfile=""
192
193 # We don't configure wireless if we're being called from
194 # the background unless we're not currently running
195 if ${IN_BACKGROUND} ; then
196 if service_started_daemon "net.${iface}" /sbin/wpa_supplicant ; then
197 if wpa_supplicant_exists "${iface}" ; then
198 ESSID="$( wpa_supplicant_get_essid "${iface}" )"
199 ESSIDVAR="$( bash_variable "${ESSID}" )"
200 save_options "ESSID" "${ESSID}"
201 fi
202 return 0
203 fi
204 fi
205
206 save_options "ESSID" ""
207
208 local ifvar="$( bash_variable "${iface}" )"
209 opts="wpa_supplicant_${ifvar}"
210 opts=" ${!opts} "
211 [[ ${opts} != *" -D"* ]] \
212 && vewarn "wpa_supplicant_${ifvar} does not define a driver"
213
214 # We only work on wirelesss interfaces unless a driver for wired
215 # has been defined
216 if [[ ${opts} != *" -Dwired "* && ${opts} != *" -D wired "* ]]; then
217 if ! wpa_supplicant_exists "${iface}" ; then
218 veinfo "wpa_supplicant only works on wireless interfaces"
219 veinfo "unless the -D wired option is specified"
220 return 0
221 fi
222 fi
223
224 # Check for rf_kill - only ipw supports this at present, but other
225 # cards may in the future
226 if [[ -e "/sys/class/net/${iface}/device/rf_kill" ]]; then
227 if [[ $( < "/sys/class/net/${iface}/device/rf_kill" ) != 0 ]]; then
228 eerror "Wireless radio has been killed for interface ${iface}"
229 return 1
230 fi
231 fi
232
233 # If wireless-tools is installed, try and apply our user config
234 # This is needed for some drivers - such as hostap because they start
235 # the card in Master mode which causes problems with wpa_supplicant.
236 if is_function iwconfig_defaults ; then
237 if wpa_supplicant_exists "${iface}" ; then
238 iwconfig_defaults "${iface}"
239 iwconfig_user_config "${iface}"
240 fi
241 fi
242
243 ebegin "Starting wpa_supplicant on ${iface}"
244
245 cfgfile="${opts##* -c}"
246 if [[ -n ${cfgfile} && ${cfgfile} != "${opts}" ]]; then
247 [[ ${cfgfile:0:1} == " " ]] && cfgfile="${cfgfile# *}"
248 cfgfile="${cfgfile%% *}"
249 else
250 # Support new and old style locations
251 cfgfile="/etc/wpa_supplicant/wpa_supplicant-${iface}.conf"
252 [[ ! -e ${cfgfile} ]] \
253 && cfgfile="/etc/wpa_supplicant/wpa_supplicant.conf"
254 [[ ! -e ${cfgfile} ]] \
255 && cfgfile="/etc/wpa_supplicant.conf"
256 opts="${opts} -c${cfgfile}"
257 fi
258
259 if [[ ! -f ${cfgfile} ]]; then
260 eend 1 "configuration file ${cfgfile} not found!"
261 return 1
262 fi
263
264 local ctrl_dir="$( sed -n -e 's/[ \t]*#.*//g;s/[ \t]*$//g;s/^ctrl_interface=//p' "${cfgfile}" )"
265 if [[ ${ctrl_dir} != "/var/run/wpa_supplicant" ]]; then
266 eerror "${cfgfile} must set"
267 eerror " ctrl_interface=/var/run/wpa_supplicant"
268 eend 1
269 return 1
270 fi
271
272 # Some drivers require the interface to be up
273 interface_up "${iface}"
274
275 version="$( wpa_cli -v | sed -n -e 's/wpa_cli v//p' )"
276 version=( ${version//./ } )
277 (( version = version[0] * 1000 + version[1] * 100 + version[2] ))
278
279 # wpa_supplicant 0.4.0 and greater supports wpa_cli actions
280 # This is very handy as if and when different association mechanisms are
281 # introduced to wpa_supplicant we don't have to recode for them as
282 # wpa_cli is now responsible for informing us of success/failure.
283 # The downside of this is that we don't see the interface being configured
284 # for DHCP/static.
285 if [[ ${version} -gt 399 ]]; then
286 opts="${opts} -W -P/var/run/wpa_supplicant-${iface}.pid"
287 actfile="/etc/wpa_supplicant/wpa_cli.sh"
288 # Support old file location
289 [[ ! -x ${actfile} ]] && actfile="/sbin/wpa_cli.action"
290 [[ ! -x ${actfile} ]] && unset actfile
291 fi
292
293 eval start-stop-daemon --start --exec /sbin/wpa_supplicant \
294 --pidfile "/var/run/wpa_supplicant-${iface}.pid" \
295 -- "${opts}" -B -i"${iface}"
296 eend "$?" || return 1
297
298 # Starting wpa_supplication-0.4.0, we can get wpa_cli to
299 # start/stop our scripts from wpa_supplicant messages
300 if [[ -n ${actfile} ]] ; then
301 mark_service_inactive "net.${iface}"
302 ebegin "Starting wpa_cli on ${iface}"
303 start-stop-daemon --start --exec /bin/wpa_cli \
304 --pidfile "/var/run/wpa_cli-${iface}.pid" \
305 -- -a"${actfile}" -i"${iface}" \
306 -P"/var/run/wpa_cli-${iface}.pid" -B
307 eend "$?" || return 1
308 fi
309
310 eindent
311 veinfo "Waiting for association"
312 eend 0
313
314 wpa_supplicant_associate "${iface}" || return 1
315
316 # Only report wireless info for wireless interfaces
317 if wpa_supplicant_exists "${iface}" ; then
318 # Set ESSID for essidnet and report
319 ESSID="$( wpa_supplicant_get_essid "${iface}" )"
320 ESSIDVAR="$( bash_variable "${ESSID}" )"
321 save_options "ESSID" "${ESSID}"
322
323 local -a status
324 eval status=( $( wpa_cli -i"${iface}" status | sed -n -e 's/^\(bssid\|pairwise_cipher\|key_mgmt\)=\([^=]\+\).*/\"\U\2\"/p' | tr '[:lower:]' '[:upper:]' ) )
325 einfo "${iface} connected to \"${ESSID//\\\\/\\\\}\" at ${status[0]}"
326
327 if [[ ${status[2]} == "NONE" ]]; then
328 if [[ ${status[1]} == "NONE" ]]; then
329 ewarn "not using any encryption"
330 else
331 veinfo "using ${status[1]}"
332 fi
333 else
334 veinfo "using ${status[2]}/${status[1]}"
335 fi
336 eoutdent
337 else
338 einfo "${iface} connected"
339 fi
340
341 if [[ -n ${actfile} ]] ; then
342 local addr="$( interface_get_address "${iface}" )"
343 einfo "${iface} configured with address ${addr}"
344 exit 0
345 fi
346
347 return 0
348 }
349
350 # bool wpa_supplicant_post_stop(char *iface)
351 #
352 # Stops wpa_supplicant on an interface
353 # Returns 0 (true) when successful, non-zero otherwise
354 wpa_supplicant_post_stop() {
355 if ${IN_BACKGROUND} ; then
356 # Only stop wpa_supplicant if it's not the controlling daemon
357 ! service_started_daemon "net.$1" /sbin/wpa_supplicant 0
358 fi
359 [[ $? == 0 ]] && wpa_supplicant_kill "$1" true
360 return 0
361 }
362
363 # vim:ts=4

  ViewVC Help
Powered by ViewVC 1.1.20