| 1 | # wpa_supplicant (net-analyzer/wpa_supplicant) module for net-scripts |
1 | # wpa_supplicant (net-wireless/wpa_supplicant) module for net-scripts |
| 2 | # Version 1.0.0 |
2 | # Version 1.0.0 |
| 3 | # Copyright (c) 2004 Gentoo Foundation |
3 | # Copyright (c) 2004 Gentoo Foundation |
| 4 | # Distributed under the terms of the GNU General Public License V2 |
4 | # Distributed under the terms of the GNU General Public License V2 |
| 5 | # Contributed by Roy Marples (uberlord@gentoo.org) |
5 | # Contributed by Roy Marples (uberlord@gentoo.org) |
| 6 | |
6 | |
| … | |
… | |
| 90 | [[ ${status[1]} == COMPLETED ]] |
90 | [[ ${status[1]} == COMPLETED ]] |
| 91 | fi |
91 | fi |
| 92 | return $? |
92 | return $? |
| 93 | } |
93 | } |
| 94 | |
94 | |
| 95 | # bool wpa_supplicant_pre_start(char *iface) |
95 | # void wpa_supplicant_kill(char *interface, bool report) |
| 96 | # |
96 | # |
| 97 | # Start wpa_supplicant on an interface and wait for association |
|
|
| 98 | # Returns 0 (true) when successful, non-zero otherwise |
|
|
| 99 | wpa_supplicant_pre_start() { |
|
|
| 100 | local iface=${1} opts ifvar i cfgfile=/etc/wpa_supplicant.conf timeout |
|
|
| 101 | |
|
|
| 102 | # We only work on wirelesss interfaces |
|
|
| 103 | wpa_supplicant_check_extensions ${iface} || return 0 |
|
|
| 104 | |
|
|
| 105 | ebegin "Starting wpa_supplicant on ${iface}" |
|
|
| 106 | |
|
|
| 107 | if [[ ! -f ${cfgfile} ]]; then |
|
|
| 108 | eend 1 "configuration file ${cfgfile} not found!" |
|
|
| 109 | return 1 |
|
|
| 110 | fi |
|
|
| 111 | |
|
|
| 112 | ifvar=$( interface_variable ${1} ) |
|
|
| 113 | eval opts=\" \$\{wpa_supplicant_${ifvar}\}\" |
|
|
| 114 | [[ ${opts} != *' -D'* ]] && ewarn "wpa_supplicant_${ifvar} does not define a driver" |
|
|
| 115 | |
|
|
| 116 | # Kill off any existing wpa_supplicant on this interface |
97 | # Kills any existing wpa_supplicant process on the interface |
|
|
98 | wpa_supplicant_kill() { |
|
|
99 | local iface=$1 report=${2:-false} |
| 117 | local pid=$( ps --no-headers -fC wpa_supplicant 2>/dev/null | awk '/-i'${iface}'/ { print $2 }' ) |
100 | local pid=$( ps --no-headers -fC wpa_supplicant 2>/dev/null | awk '/-i'${iface}'/ { print $2 }' ) |
| 118 | [[ -n ${pid} ]] && kill -s TERM ${pid} |
|
|
| 119 | |
|
|
| 120 | # Some drivers require the interface to be up |
|
|
| 121 | interface_up ${iface} |
|
|
| 122 | |
101 | |
| 123 | if ! wpa_supplicant ${opts} -B -c/etc/wpa_supplicant.conf -i${iface} ; then |
102 | if [[ -n ${pid} ]]; then |
| 124 | eend 1 |
103 | ${report} && ebegin "Stopping wpa_supplicant on ${iface}" |
| 125 | return 1 |
104 | kill -s TERM ${pid} |
|
|
105 | ${report} && eend 0 |
| 126 | fi |
106 | fi |
| 127 | |
107 | |
| 128 | eindent |
108 | # If wpa_supplicant exits uncleanly, we need to remove the stale dir |
| 129 | veinfo "Waiting for association" |
109 | [[ -d /var/run/wpa_supplicant/${iface} ]] && rm -f /var/run/wpa_supplicant/${iface} |
|
|
110 | } |
|
|
111 | |
|
|
112 | # bool wpa_supplicant_associate(char *interface) |
|
|
113 | # |
|
|
114 | # Returns 0 if wpa_supplicant associates and authenticates to an AP |
|
|
115 | # otherwise, 1 |
|
|
116 | wpa_supplicant_associate() { |
|
|
117 | local iface=${1} ifvar=$( interface_variable ${1} ) timeout i |
|
|
118 | |
| 130 | eval timeout=\"\$\{wpa_timeout_${ifvar}:-60\}\" |
119 | eval timeout=\"\$\{wpa_timeout_${ifvar}:-60\}\" |
| 131 | for (( i=0; i<${timeout}; i++ )); do |
120 | for (( i=0; i<${timeout}; i++ )); do |
| 132 | wpa_supplicant_associated ${iface} && break |
121 | wpa_supplicant_associated ${iface} && break |
| 133 | sleep 1 |
122 | sleep 1 |
| 134 | done |
123 | done |
| 135 | if [[ ${i} == ${timeout} ]]; then |
124 | if [[ ${i} == ${timeout} ]]; then |
| 136 | if ! wpa_supplicant_associated ${iface}; then |
125 | if ! wpa_supplicant_associated ${iface}; then |
| 137 | eend 1 "timed out" |
126 | [[ ${background} != yes ]] && eend 1 "timed out" |
| 138 | |
127 | |
| 139 | # We now need to kill the process |
128 | # We now need to kill the process |
| 140 | kill -s TERM $( ps --no-headers -fC wpa_supplicant 2>/dev/null | awk '/-i'${iface}'/ { print $2 }' ) 2>/dev/null |
129 | wpa_supplicant_kill ${iface} |
| 141 | |
130 | |
| 142 | return 1 |
131 | return 1 |
| 143 | fi |
132 | fi |
| 144 | fi |
133 | fi |
|
|
134 | } |
|
|
135 | |
|
|
136 | # bool wpa_supplicant_pre_start(char *interface) |
|
|
137 | # |
|
|
138 | # Start wpa_supplicant on an interface and wait for association |
|
|
139 | # Returns 0 (true) when successful, non-zero otherwise |
|
|
140 | wpa_supplicant_pre_start() { |
|
|
141 | local iface=$1 opts ifvar cfgfile=/etc/wpa_supplicant.conf timeout |
|
|
142 | |
|
|
143 | # We only work on wirelesss interfaces |
|
|
144 | wpa_supplicant_check_extensions ${iface} || return 0 |
|
|
145 | |
|
|
146 | # Kill off any existing wpa_supplicant on this interface |
|
|
147 | # This is so we can re-read the configuration file and clean any stale |
|
|
148 | # directories |
|
|
149 | wpa_supplicant_kill ${iface} true |
|
|
150 | |
|
|
151 | ebegin "Starting wpa_supplicant on ${iface}" |
|
|
152 | |
|
|
153 | if [[ ! -f ${cfgfile} ]]; then |
|
|
154 | eend 1 "configuration file ${cfgfile} not found!" |
|
|
155 | return 1 |
|
|
156 | fi |
|
|
157 | |
|
|
158 | local ctrl_dir=$( awk -F# '/^ctrl_interface=/ { print $1 }' ${cfgfile} ) |
|
|
159 | ctrl_dir=${ctrl_dir/ctrl_interface=/} |
|
|
160 | if [[ ${ctrl_dir} != "/var/run/wpa_supplicant" ]]; then |
|
|
161 | eerror "${cfgfile} must set" |
|
|
162 | eerror " ctrl_interface=/var/run/wpa_supplicant" |
|
|
163 | eend 1 |
|
|
164 | return 1 |
|
|
165 | fi |
|
|
166 | |
|
|
167 | ifvar=$( interface_variable ${iface} ) |
|
|
168 | eval opts=\" \$\{wpa_supplicant_${ifvar}\}\" |
|
|
169 | [[ ${opts} != *' -D'* ]] && ewarn "wpa_supplicant_${ifvar} does not define a driver" |
|
|
170 | |
|
|
171 | # Some drivers require the interface to be up |
|
|
172 | interface_up ${iface} |
|
|
173 | |
|
|
174 | if ! wpa_supplicant ${opts} -B -c/etc/wpa_supplicant.conf -i${iface} ; then |
|
|
175 | eend 1 |
|
|
176 | return 1 |
|
|
177 | fi |
|
|
178 | |
|
|
179 | if ! wpa_cli -i${iface} status &>/dev/null ; then |
|
|
180 | eend 1 "wpa_supplicant has exited unexpectedly" |
|
|
181 | return 1 |
|
|
182 | fi |
|
|
183 | |
|
|
184 | eindent |
|
|
185 | veinfo "Waiting for association" |
| 145 | eend 0 |
186 | eend 0 |
|
|
187 | |
|
|
188 | wpa_supplicant_associate ${iface} || return 1 |
| 146 | |
189 | |
| 147 | # Set ESSID for essidnet and report |
190 | # Set ESSID for essidnet and report |
| 148 | ESSID=$( wpa_supplicant_get_essid ${iface} ) |
191 | ESSID=$( wpa_supplicant_get_essid ${iface} ) |
| 149 | local -a status=( "$( wpa_cli -i${iface} status | awk -F= '/^bssid|^pairwise_cipher|^key_mgmt/ { print $2 }' )" ) |
192 | local -a status=( "$( wpa_cli -i${iface} status | awk -F= '/^bssid|^pairwise_cipher|^key_mgmt/ { print $2 }' )" ) |
| 150 | local mac=$( echo ${status[0]} | tr '[:lower:]' '[:upper:]' ) |
193 | local mac=$( echo ${status[0]} | tr '[:lower:]' '[:upper:]' ) |
| … | |
… | |
| 167 | # bool wpa_supplicant_post_stop(char *iface) |
210 | # bool wpa_supplicant_post_stop(char *iface) |
| 168 | # |
211 | # |
| 169 | # Stops wpa_supplicant on an interface |
212 | # Stops wpa_supplicant on an interface |
| 170 | # Returns 0 (true) when successful, non-zero otherwise |
213 | # Returns 0 (true) when successful, non-zero otherwise |
| 171 | wpa_supplicant_post_stop() { |
214 | wpa_supplicant_post_stop() { |
| 172 | local iface=${1} |
215 | wpa_supplicant_kill $1 true |
| 173 | |
|
|
| 174 | # wpa_supplicant does not write to a pid file ... |
|
|
| 175 | local pid=$( ps --no-headers -fC wpa_supplicant 2>/dev/null | awk '/-i'${iface}'/ { print $2 }' ) |
|
|
| 176 | [[ -z ${pid} ]] && return 0 |
|
|
| 177 | |
|
|
| 178 | ebegin "Stopping wpa_supplicant on ${iface}" |
|
|
| 179 | kill -s TERM ${pid} |
|
|
| 180 | eend 0 |
|
|
| 181 | |
|
|
| 182 | return 0 |
216 | return 0 |
| 183 | } |
217 | } |