/[baselayout]/trunk/net.BSD/iwconfig.sh
Gentoo

Contents of /trunk/net.BSD/iwconfig.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2570 - (show annotations) (download) (as text)
Tue Apr 10 12:41:38 2007 UTC (7 years ago) by uberlord
File MIME type: text/x-sh
File size: 12622 byte(s)
Fix scanning and clear wepkeys when we start
1 # Copyright 2004-2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 _config_vars="$_config_vars essid mode associate_timeout preferred_aps blacklist_aps"
5
6 iwconfig_depend() {
7 program /sbin/ifconfig
8 after plug
9 before interface
10 provide wireless
11 }
12
13 iwconfig_get_wep_status() {
14 local status="disabled"
15 local mode=$(LC_ALL=C ifconfig "${IFACE}" \
16 | sed -n -e 's/^[[:space:]]*authmode \([^ ]*\) privacy ON .*/\1/p')
17 if [ -n "${mode}" ] ; then
18 status="enabled - ${mode}"
19 fi
20
21 echo "(WEP ${status})"
22 }
23
24 _iwconfig_get() {
25 LC_ALL=C ifconfig "${IFACE}" | \
26 sed -n -e 's/^[[:space:]]*ssid \(.*\) channel \([0-9]*\) bssid \(..:..:..:..:..:..\)$/\'"$1"'/p'
27 }
28
29 _get_ssid() {
30 _iwconfig_get 1
31 }
32
33 _get_ap_mac_address() {
34 _iwconfig_get 3
35 }
36
37 _get_channel() {
38 _iwconfig_get 2
39 }
40
41 iwconfig_report() {
42 local m="connected to"
43 local ssid=$(_get_ssid)
44 local mac=$(_get_ap_mac_address "${iface}")
45 [ -n "${mac}" ] && mac=" at ${mac}"
46 local wep_status="$(iwconfig_get_wep_status "${iface}")"
47 local channel=$(_get_channel)
48 [ -n "${channel}" ] && channel="on channel ${channel} "
49
50 eindent
51 einfo "${IFACE} ${m} \"${ssid}\"${mac}"
52 einfo "${channel}${wep_status}"
53 eoutdent
54 }
55
56 iwconfig_get_wep_key() {
57 local mac="$1" key=
58 [ -n "${mac}" ] && mac="$(echo "${mac}" | sed -e 's/://g')"
59 eval key=\$mac_key_${mac}
60 [ -z "${key}" ] && eval key=\$key_${SSIDVAR}
61 echo "${key:--}"
62 }
63
64 iwconfig_user_config() {
65 local conf=
66 eval set -- \$ifconfig_${SSIDVAR}
67 for conf in "$@" ; do
68 ifconfig "${IFACE}" ${conf}
69 done
70 }
71
72 iwconfig_set_mode() {
73 local x= opt= unopt="hostap adhoc"
74 case "${mode}" in
75 master|hostap) unopt="adhoc" opt="hostap" ;;
76 ad-hoc|adhoc) unopt="hostap" opt="adhoc" ;;
77 esac
78 for x in ${unopt} ; do
79 ifconfig "${IFACE}" -mediaopt ${x}
80 done
81 for x in ${opt} ; do
82 ifconfig "${IFACE}" mediaopt ${x}
83 done
84 }
85
86 iwconfig_setup_specific() {
87 local mode="${1:-master}" channel=
88 if [ -z "${SSID}" ]; then
89 eerror "${IFACE} requires an SSID to be set to operate in ${mode} mode"
90 eerror "adjust the ssid_${IFVAR} setting in /etc/conf.d/net"
91 return 1
92 fi
93
94 iwconfig_set_mode "${mode}" || return 1
95
96 SSIDVAR=$(_shell_var "${SSID}")
97 local key=$(iwconfig_get_wep_key)
98
99 # Now set the key
100 eval ifconfig "${IFACE}" wepkey "${key}"
101
102 ifconfig "${IFACE}" ssid "${ESSID}" || return 1
103
104 eval channel=\$channel_${IFVAR}
105 # We default the channel to 3
106 ifconfig "${IFACE}" channel "${channel:-3}" || return 1
107
108 iwconfig_user_config
109 iwconfig_report "${iface}"
110 return 0
111 }
112
113 iwconfig_associate() {
114 local mac="$1" channel="$2" caps="$3"
115 local mode= w="(WEP Disabled)" key=
116
117 SSIDVAR=$(_shell_var "${SSID}")
118 key=$(iwconfig_get_wep_key "${mac}")
119 case "${caps}" in
120 [EI]P*)
121 if [ "${key}" = "-" ] ; then
122 ewarn "WEP key is not set for \"${SSID}\"; not connecting"
123 return 1
124 fi
125 ;;
126 "") ;;
127 *)
128 if [ "${key}" != "-" ] ; then
129 key="-"
130 ewarn "\"${ESSID}\" is not WEP enabled; ignoring setting"
131 fi
132 ;;
133 esac
134
135 # Set mode accordingly
136 case "${caps}" in
137 *E*)
138 mode="managed"
139 if LC_ALL=C ifconfig "${IFACE}" | \
140 grep -q "^[[:space:]]*media: .*adhoc" ; then
141 ifconfig "${IFACE}" down -mediaopt adhoc up
142 fi
143 ;;
144 *I*)
145 mode="adhoc"
146 if ! LC_ALL=C ifconfig "${IFACE}" | \
147 grep -q "^[[:space:]]*media: .*adhoc" ; then
148 ifconfig "${IFACE}" down mediaopt adhoc up
149 fi
150 ;;
151 *)
152 if LC_ALL=C ifconfig "${IFACE}" \
153 | grep -q "^[[:space:]]*media: .*adhoc" ; then
154 mode="adhoc"
155 else
156 mode="managed"
157 fi
158 ;;
159 esac
160
161 if [ "${key}" = "-" ] ; then
162 ifconfig "${IFACE}" wepmode off
163 else
164 ifconfig "${IFACE}" wepmode on
165 ifconfig "${IFACE}" deftxkey 1
166 w=$(iwconfig_get_wep_status)
167 fi
168
169 ebegin "Connecting to \"${SSID}\" in ${mode} mode ${w}"
170
171 if ! eval ifconfig "${IFACE}" wepkey "${key}" ; then
172 eerror "Invalid WEP key ${key}"
173 return 1
174 fi
175
176 ifconfig "${IFACE}" ssid "${SSID}" || return 1
177 iwconfig_user_config
178
179 if [ "${SSID}" != "any" ] && type preassociate >/dev/null 2>/dev/null ; then
180 veinfo "Running preassociate function"
181 veindent
182 ( preassociate )
183 local e=$?
184 veoutdent
185 if [ ${e} -eq 0 ] ; then
186 veend 1 "preassociate \"${SSID}\" on ${IFACE} failed"
187 return 1
188 fi
189 fi
190
191 local timeout= i=0
192 eval timeout=\$associate_timeout_${IFVAR}
193 timeout=${timeout:-10}
194
195 [ ${timeout} -eq 0 ] \
196 && vewarn "WARNING: infinite timeout set for association on ${IFACE}"
197
198 while true; do
199 _has_carrier && break
200 sleep 1
201 [ ${timeout} -eq 0 ] && continue
202 i=$((${i} + 1))
203 [ ${i} -ge ${timeout} ] && return 1
204 done
205
206 if ! _has_carrier ; then
207 eend 1
208 return 1
209 fi
210 eend 0
211
212 if [ "${SSID}" = "any" ]; then
213 SSID="$(_get_ssid)"
214 iwconfig_associate
215 return $?
216 fi
217
218 iwconfig_report
219
220 if type postassociate >/dev/null 2>/dev/null ; then
221 veinfo "Running postassociate function"
222 veindent
223 ( postassociate )
224 veoutdent
225 fi
226
227 return 0
228 }
229
230 iwconfig_scan() {
231 local x= i=0 scan= quality=
232 einfo "Scanning for access points"
233 eindent
234
235 scan="$(LC_ALL=C ifconfig -v "${IFACE}" list scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")"
236 while [ ${i} -lt 3 -a -z "${scan}" ] ; do
237 scan="${scan}${scan:+ }$(LC_ALL=C ifconfig -v "${IFACE}" scan 2>/dev/null | sed -e "1 d" -e "s/$/'/g" -e "s/^/'/g")"
238 i=$((${i} + 1))
239 done
240
241 local OIFS=$IFS
242 APS=-1
243 eval set -- ${scan}
244 for line in "$@" ; do
245 APS=$((${APS} + 1))
246 set -- ${line}
247 while true ; do
248 case "$1" in
249 *:*:*:*:*:*) break ;;
250 esac
251 eval SSID_${APS}="\"\${SSID_${APS}}\${SSID_${APS}:+ }$1\""
252 shift
253 done
254 eval MAC_${APS}="$(echo "$1" | tr '[:lower:]' '[:upper:]')"
255 eval CHAN_${APS}="$2"
256 quality=${4%:*}
257 shift ; shift ; shift ; shift ; shift
258 eval CAPS_${APS}="\"$*\""
259
260 # Add 1000 for managed nodes as we prefer them to adhoc
261 set -- $*
262 case "$1" in
263 *E*) eval QUAL_${APS}=$((${quality} + 1000)) ;;
264 *) eval QUAL_${APS}=\$quality ;;
265 esac
266 done
267
268 if [ -z "${MAC_0}" ]; then
269 ewarn "no access points found"
270 eoutdent
271 return 1
272 fi
273
274 # Sort based on quality
275 local i=0 k=1 a= b= x= t=
276 while [ ${i} -lt ${APS} ] ; do
277 k=$((${i} + 1))
278 while [ ${k} -le ${APS} ] ; do
279 eval a=\$QUALITY_${i}
280 [ -z "${a}" ] && break
281 eval b=\$QUALITY_${k}
282 if [ -n "${b}" -a "${a}" -lt "${b}" ] ; then
283 for x in MAC SSID CHAN QUALITY CAPS ; do
284 eval t=\$${x}_${i}
285 eval ${x}_${i}=\$${x}_${k}
286 eval ${x}_${k}=\$t
287 done
288 fi
289 k=$((${k} + 1))
290 done
291 i=$((${i} + 1))
292 done
293
294 # Strip any duplicates
295 local i=0 k=1 a= b=
296 while [ ${i} -lt ${APS} ] ; do
297 k=$((${i} + 1))
298 while [ ${k} -le ${APS} ] ; do
299 eval a=\$MAC_${i}
300 eval b=\$MAC_${k}
301 if [ "${a}" = "${b}" ] ; then
302 eval a=\$QUALITY_${i}
303 eval b=\$QUALITY_${k}
304 if [ -n "${a}" -a -n "${b}" ] ; then
305 if [ ${a} -ge ${b} ] ; then
306 unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k}
307 else
308 unset MAC_${i} SSID_${i} CHAN_${i} QUALITY_${i} CAPS_${i}
309 fi
310 else
311 unset MAC_${k} SSID_${k} CHAN_${k} QUALITY_${k} CAPS_${k}
312 fi
313 fi
314 k=$((${k} + 1))
315 done
316 i=$((${i} + 1))
317 done
318
319 local i=0 e= m= black= s=
320 eval "$(_get_array "blacklist_aps")"
321 black="$@"
322
323 while [ ${i} -le ${APS} ] ; do
324 eval x=\$MAC_${i}
325 if [ -z "${x}" ] ; then
326 i=$((${i} + 1))
327 continue
328 fi
329
330 eval m=\$MODE_${i}
331 [ -n "${m}" ] && m=", ${m}"
332 eval s=\$SSID_${i}
333 eval q=\$QUALITY_${i}
334 eval e=\$CAPS_${i}
335 case "${e}" in
336 [EI]P*) e=", encrypted" ;;
337 *) e="" ;;
338 esac
339 if [ -z "${s}" ] ; then
340 einfo "Found ${x}${m}${e}"
341 else
342 einfo "Found \"${s}\" at ${x}${m}${e}"
343 fi
344
345 x="$(echo "${x}" | sed -e 's/://g')"
346 eval x=\$mac_ssid_${x}
347 if [ -n "${x}" ] ; then
348 eval SSID_${i}=\$x
349 s=${x}
350 eindent
351 einfo "mapping to \"${x}\""
352 eoutdent
353 fi
354
355 eval "$(_get_array "blacklist_aps")"
356 for x in "$@" ; do
357 if [ "${x}" = "${s}" ] ; then
358 ewarn "${s} has been blacklisted - not connecting"
359 unset SSID_${i} MAC_${i} CHAN_${i} QUALITY_${i} CAPS_${i}
360 fi
361 done
362 i=$((${i} + 1))
363 done
364 eoutdent
365 return 0
366 }
367
368 iwconfig_force_preferred() {
369 [ -z "${preferred_aps}" ] && return 1
370
371 ewarn "Trying to force preferred in case they are hidden"
372 eval "(_get_array "preferred_aps")"
373 local ssid=
374 for ssid in "$@"; do
375 local found_AP=false i=0 e=
376 while [ ${i} -le ${APS} ] ; do
377 eval e=\$SSID_${i}
378 if [ "${e}" = "${ssid}" ] ; then
379 found_AP=true
380 break
381 fi
382 i=$((${i} + 1))
383 done
384 if ! ${found_AP} ; then
385 SSID=${e}
386 iwconfig_associate && return 0
387 fi
388 done
389
390 ewarn "Failed to associate with any preferred access points on ${IFACE}"
391 return 1
392 }
393
394 iwconfig_connect_preferred() {
395 local essid= i=0 mode= mac= caps= freq= chan=
396
397 eval "$(_get_array preferred_aps)"
398 for essid in "$@"; do
399 while [ ${i} -le ${APS} ] ; do
400 eval e=\$SSID_${i}
401 if [ "${e}" = "${essid}" ] ; then
402 SSID=${e}
403 eval mac=\$MAC_${i}
404 eval caps=\$CAPS_${i}
405 eval freq=\$FREQ_${i}
406 eval chan=\$CHAN_${i}
407 iwconfig_associate "${mac}" \
408 "${chan}" "${caps}" && return 0
409 fi
410 i=$((${i} + 1))
411 done
412 done
413
414 return 1
415 }
416
417 iwconfig_connect_not_preferred() {
418 local essid= i=0 mode= mac= caps= freq= chan= pref=
419
420 while [ ${i} -le ${APS} ] ; do
421 eval e=\$SSID_${i}
422 if [ -z "${e}" ] ; then
423 i=$((${i} + 1))
424 continue
425 fi
426
427 eval "$(_get_array preferred_aps)"
428 pref=false
429 for essid in "$@" ; do
430 if [ "${e}" = "${essid}" ] ; then
431 pref=true
432 break
433 fi
434 done
435
436 if ! ${pref} ; then
437 SSID=${e}
438 eval mac=\$MAC_${i}
439 eval caps=\$CAPS_${i}
440 eval freq=\$FREQ_${i}
441 eval chan=\$CHAN_${i}
442 iwconfig_associate "${mac}" \
443 "${chan}" "${caps}" && return 0
444 fi
445 i=$((${i} + 1))
446 done
447
448 return 1
449 }
450
451 iwconfig_defaults() {
452 # Set some defaults
453 #ifconfig "${iface}" txpower 100 2>/dev/null
454 ifconfig "${IFACE}" bssid -
455 ifconfig "${IFACE}" ssid -
456 ifconfig "${IFACE}" wepkey 1:- wepkey 2:- wepkey 3:- wepkey 4:-
457 ifconfig "${IFACE}" authmode open
458 ifconfig "${IFACE}" -mediaopt adhoc
459 ifconfig "${IFACE}" -mediaopt hostap
460 }
461
462 iwconfig_configure() {
463 local x APS
464 eval SSID=\$ssid_${IFVAR}
465
466 # Setup ad-hoc mode?
467 eval x=\$mode_${IFVAR}
468 x=${x:-managed}
469 case "${x}" in
470 ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;;
471 esac
472
473 if [ "${x}" != "managed" -a "${x}" != "auto" ] ; then
474 eerror "Only managed, ad-hoc, master and auto modes are supported"
475 return 1
476 fi
477
478 # Has an ESSID been forced?
479 if [ -n "${SSID}" ]; then
480 iwconfig_set_mode "${x}"
481 iwconfig_associate && return 0
482 [ "${SSID}" = "any" ] && iwconfig_force_preferred && return 0
483
484 eval SSID=\$adhoc_ssid_${IFVAR}
485 if [ -n "${SSID}" ]; then
486 iwconfig_setup_specific adhoc
487 return $?
488 fi
489 return 1
490 fi
491
492 # Are we forcing preferred only?
493 eval x=\$associate_order_${IFVAR}
494 [ -n "${x}" ] && associate_order=${x}
495 associate_order=${associate_order:-any}
496 if [ "${associate_order}" = "forcepreferredonly" ]; then
497 iwconfig_force_preferred && return 0
498 else
499 iwconfig_scan || return 1
500 iwconfig_connect_preferred && return 0
501 [ "${associate_order}" = "forcepreferred" ] || \
502 [ "${associate_order}" = "forceany" ] && \
503 iwconfig_force_preferred && return 0
504 [ "${associate_order}" = "any" ] || \
505 [ "${associate_order}" = "forceany" ] && \
506 iwconfig_connect_not_preferred && return 0
507 fi
508
509 e="associate with"
510 [ -z "${MAC_0}" ] && e="find"
511 [ "${preferred_aps}" = "force" ] || \
512 [ "${preferred_aps}" = "forceonly" ] && \
513 e="force"
514 e="Couldn't ${e} any access points on ${IFACE}"
515
516 eval SSID=\$adhoc_ssid_${IFVAR}
517 if [ -n "${SSID}" ]; then
518 ewarn "${e}"
519 iwconfig_setup_specific adhoc
520 return $?
521 fi
522
523 eerror "${e}"
524 return 1
525 }
526
527 iwconfig_pre_start() {
528 # We don't configure wireless if we're being called from
529 # the background
530 ${IN_BACKGROUND} && return 0
531
532 save_options "SSID" ""
533 _exists || return 0
534
535 if ! _is_wireless ; then
536 veinfo "${IFACE} is not wireless"
537 return 0
538 fi
539
540 iwconfig_defaults
541 iwconfig_user_config
542
543 # Set the base metric to be 2000
544 metric=2000
545
546 einfo "Configuring wireless network for ${IFACE}"
547
548 if iwconfig_configure ; then
549 save_options "ESSID" "${ESSID}"
550 return 0
551 fi
552
553 eerror "Failed to configure wireless for ${IFACE}"
554 iwconfig_defaults
555 #iwconfig "${IFACE}" txpower 0 2>/dev/null
556 unset SSID SSIDVAR
557 _down
558 return 1
559 }
560
561 iwconfig_post_stop() {
562 ${IN_BACKGROUND} && return 0
563 _is_wireless || return 0
564 iwconfig_defaults
565 #iwconfig "${IFACE}" txpower 0 2>/dev/null
566 }
567
568 # vim: set ts=4

  ViewVC Help
Powered by ViewVC 1.1.20