/[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 1798 - (show annotations) (download)
Tue Jan 10 12:43:34 2006 UTC (8 years, 8 months ago) by uberlord
File size: 10412 byte(s)
Fixed bonding module, improved wireless with rt2500 and WPA
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 i
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 while true ; do
154 if ${action} ; then
155 service_started "net.${iface}" && return 0
156 else
157 if ! wpa_cli -i"${iface}" status &>/dev/null ; then
158 eend 1 "wpa_supplicant has exited unexpectedly"
159 return 1
160 fi
161 wpa_supplicant_associated "${iface}" && return 0
162 fi
163 sleep 1
164 (( i++ ))
165 [[ ${i} == "${timeout}" || ${i} -gt "${timeout}" ]] && break
166 done
167
168 # Spit out an appropriate error
169 if ${action} ; then
170 eend 1 "Failed to configure ${iface} in the background"
171 else
172 eend 1 "Timed out"
173 fi
174
175 # exit without error with wpa_supplicant-0.4.x as we may get kickstarted
176 # when an AP comes in range
177 ${action} && exit 0
178
179 # Kill wpa_supplicant for 0.3.x
180 wpa_supplicant_kill "${iface}"
181 return 1
182 }
183
184 # bool wpa_supplicant_pre_start(char *interface)
185 #
186 # Start wpa_supplicant on an interface and wait for association
187 # Returns 0 (true) when successful, non-zero otherwise
188 wpa_supplicant_pre_start() {
189 local iface="$1" opts timeout action=false cfgfile
190 local actfile="/sbin/wpa_cli.action"
191
192 # We don't configure wireless if we're being called from
193 # the background unless we're not currently running
194 if ${IN_BACKGROUND} ; then
195 if service_started_daemon "net.${iface}" /sbin/wpa_supplicant ; then
196 if wpa_supplicant_exists "${iface}" ; then
197 ESSID=$( wpa_supplicant_get_essid "${iface}" )
198 ESSIDVAR=$( bash_variable "${ESSID}" )
199 save_options "ESSID" "${ESSID}"
200 fi
201 return 0
202 fi
203 fi
204
205 save_options "ESSID" ""
206
207 local ifvar=$( bash_variable "${iface}" )
208 opts="wpa_supplicant_${ifvar}"
209 opts=" ${!opts} "
210 [[ ${opts} != *" -D"* ]] \
211 && vewarn "wpa_supplicant_${ifvar} does not define a driver"
212
213 # We only work on wirelesss interfaces unless a driver for wired
214 # has been defined
215 if [[ ${opts} != *" -Dwired "* && ${opts} != *" -D wired "* ]]; then
216 if ! wpa_supplicant_exists "${iface}" ; then
217 veinfo "wpa_supplicant only works on wireless interfaces"
218 veinfo "unless the -D wired option is specified"
219 return 0
220 fi
221 fi
222
223 # Check for rf_kill - only ipw supports this at present, but other
224 # cards may in the future
225 if [[ -e "/sys/class/net/${iface}/device/rf_kill" ]]; then
226 if [[ $( < "/sys/class/net/${iface}/device/rf_kill" ) != 0 ]]; then
227 eerror "Wireless radio has been killed for interface ${iface}"
228 return 1
229 fi
230 fi
231
232 # If wireless-tools is installed, try and apply our user config
233 # This is needed for some drivers - such as hostap because they start
234 # the card in Master mode which causes problems with wpa_supplicant.
235 if is_function iwconfig_defaults ; then
236 if wpa_supplicant_exists "${iface}" ; then
237 iwconfig_defaults "${iface}"
238 iwconfig_user_config "${iface}"
239 fi
240 fi
241
242 ebegin "Starting wpa_supplicant on ${iface}"
243
244 cfgfile="${opts##* -c}"
245 if [[ -n ${cfgfile} && ${cfgfile} != "${opts}" ]]; then
246 [[ ${cfgfile:0:1} == " " ]] && cfgfile="${cfgfile# *}"
247 cfgfile="${cfgfile%% *}"
248 else
249 cfgfile="/etc/wpa_supplicant.conf"
250 opts="${opts} -c/etc/wpa_supplicant.conf"
251 fi
252
253 if [[ ! -f ${cfgfile} ]]; then
254 eend 1 "configuration file ${cfgfile} not found!"
255 return 1
256 fi
257
258 local ctrl_dir=$( sed -n -e 's/[ \t]*#.*//g;s/[ \t]*$//g;s/^ctrl_interface=//p' "${cfgfile}" )
259 if [[ ${ctrl_dir} != "/var/run/wpa_supplicant" ]]; then
260 eerror "${cfgfile} must set"
261 eerror " ctrl_interface=/var/run/wpa_supplicant"
262 eend 1
263 return 1
264 fi
265
266 # Some drivers require the interface to be up
267 interface_up "${iface}"
268
269 version=$( wpa_cli -v | sed -n -e 's/wpa_cli v//p' )
270 version=( ${version//./ } )
271 (( version = version[0] * 1000 + version[1] * 100 + version[2] ))
272
273 # wpa_supplicant 0.4.0 and greater supports wpa_cli actions
274 # This is very handy as if and when different association mechanisms are
275 # introduced to wpa_supplicant we don't have to recode for them as
276 # wpa_cli is now responsible for informing us of success/failure.
277 # The downside of this is that we don't see the interface being configured
278 # for DHCP/static.
279 if [[ ${version} -gt 399 && -x ${actfile} ]]; then
280 opts="${opts} -W -P/var/run/wpa_supplicant-${iface}.pid"
281 action=true
282 fi
283
284 eval start-stop-daemon --start --exec /sbin/wpa_supplicant \
285 --pidfile "/var/run/wpa_supplicant-${iface}.pid" \
286 -- "${opts}" -B -i"${iface}"
287 eend "$?" || return 1
288
289 # Starting wpa_supplication-0.4.0, we can get wpa_cli to
290 # start/stop our scripts from wpa_supplicant messages
291 if ${action} ; then
292 mark_service_inactive "net.${iface}"
293 ebegin "Starting wpa_cli on ${iface}"
294 start-stop-daemon --start --exec /bin/wpa_cli \
295 --pidfile "/var/run/wpa_cli-${iface}.pid" \
296 -- -a"${actfile}" -i"${iface}" \
297 -P"/var/run/wpa_cli-${iface}.pid" -B
298 eend "$?" || return 1
299 fi
300
301 eindent
302 veinfo "Waiting for association"
303 eend 0
304
305 wpa_supplicant_associate "${iface}" || return 1
306
307 # Only report wireless info for wireless interfaces
308 if wpa_supplicant_exists "${iface}" ; then
309 # Set ESSID for essidnet and report
310 ESSID=$( wpa_supplicant_get_essid "${iface}" )
311 ESSIDVAR=$( bash_variable "${ESSID}" )
312 save_options "ESSID" "${ESSID}"
313
314 local -a status
315 eval status=( $( wpa_cli -i"${iface}" status | sed -n -e 's/^\(bssid\|pairwise_cipher\|key_mgmt\)=\([^=]\+\).*/\"\U\2\"/p' | tr '[:lower:]' '[:upper:]' ) )
316 einfo "${iface} connected to \"${ESSID//\\\\/\\\\}\" at ${status[0]}"
317
318 if [[ ${status[2]} == "NONE" ]]; then
319 if [[ ${status[1]} == "NONE" ]]; then
320 ewarn "not using any encryption"
321 else
322 veinfo "using ${status[1]}"
323 fi
324 else
325 veinfo "using ${status[2]}/${status[1]}"
326 fi
327 eoutdent
328 else
329 einfo "${iface} connected"
330 fi
331
332 if ${action} ; then
333 local addr=$( interface_get_address "${iface}" )
334 einfo "${iface} configured with address ${addr}"
335 exit 0
336 fi
337
338 return 0
339 }
340
341 # bool wpa_supplicant_post_stop(char *iface)
342 #
343 # Stops wpa_supplicant on an interface
344 # Returns 0 (true) when successful, non-zero otherwise
345 wpa_supplicant_post_stop() {
346 if ${IN_BACKGROUND} ; then
347 # Only stop wpa_supplicant if it's not the controlling daemon
348 ! service_started_daemon "net.$1" /sbin/wpa_supplicant 0
349 fi
350 [[ $? == 0 ]] && wpa_supplicant_kill "$1" true
351 return 0
352 }
353
354 # vim:ts=4

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20