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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2707 - (show annotations) (download) (as text)
Wed May 16 06:33:25 2007 UTC (7 years, 3 months ago) by uberlord
File MIME type: text/x-sh
File size: 12807 byte(s)
Allow more modes to work.
1 # Copyright 2004-2007 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 _config_vars="$_config_vars ssid 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 "$1" 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_${IFVAR}")"
373 [ $# = 0 ] && eval "$(_get_array "preferred_aps")"
374
375 local ssid=
376 for ssid in "$@"; do
377 local found_AP=false i=0 e=
378 while [ ${i} -le ${APS:--1} ] ; do
379 eval e=\$SSID_${i}
380 if [ "${e}" = "${ssid}" ] ; then
381 found_AP=true
382 break
383 fi
384 i=$((${i} + 1))
385 done
386 if ! ${found_AP} ; then
387 SSID=${ssid}
388 iwconfig_associate && return 0
389 fi
390 done
391
392 ewarn "Failed to associate with any preferred access points on ${IFACE}"
393 return 1
394 }
395
396 iwconfig_connect_preferred() {
397 local ssid= i=0 mode= mac= caps= freq= chan=
398
399 eval "$(_get_array "preferred_aps_${IFVAR}")"
400 [ $# = 0 ] && eval "$(_get_array "preferred_aps")"
401
402 for ssid in "$@"; do
403 while [ ${i} -le ${APS} ] ; do
404 eval e=\$SSID_${i}
405 if [ "${e}" = "${ssid}" ] ; then
406 SSID=${e}
407 eval mac=\$MAC_${i}
408 eval caps=\$CAPS_${i}
409 eval freq=\$FREQ_${i}
410 eval chan=\$CHAN_${i}
411 iwconfig_associate "${mac}" \
412 "${chan}" "${caps}" && return 0
413 fi
414 i=$((${i} + 1))
415 done
416 done
417
418 return 1
419 }
420
421 iwconfig_connect_not_preferred() {
422 local ssid= i=0 mode= mac= caps= freq= chan= pref=
423
424 while [ ${i} -le ${APS} ] ; do
425 eval e=\$SSID_${i}
426 if [ -z "${e}" ] ; then
427 i=$((${i} + 1))
428 continue
429 fi
430
431 eval "$(_get_array preferred_aps)"
432 pref=false
433 for ssid in "$@" ; do
434 if [ "${e}" = "${ssid}" ] ; then
435 pref=true
436 break
437 fi
438 done
439
440 if ! ${pref} ; then
441 SSID=${e}
442 eval mac=\$MAC_${i}
443 eval caps=\$CAPS_${i}
444 eval freq=\$FREQ_${i}
445 eval chan=\$CHAN_${i}
446 iwconfig_associate "${mac}" \
447 "${chan}" "${caps}" && return 0
448 fi
449 i=$((${i} + 1))
450 done
451
452 return 1
453 }
454
455 iwconfig_defaults() {
456 # Set some defaults
457 #ifconfig "${iface}" txpower 100 2>/dev/null
458 ifconfig "${IFACE}" bssid -
459 ifconfig "${IFACE}" ssid -
460 ifconfig "${IFACE}" wepkey 1:- wepkey 2:- wepkey 3:- wepkey 4:-
461 ifconfig "${IFACE}" authmode open
462 ifconfig "${IFACE}" -mediaopt adhoc
463 ifconfig "${IFACE}" -mediaopt hostap
464 }
465
466 iwconfig_configure() {
467 local x APS
468 eval SSID=\$ssid_${IFVAR}
469
470 # Setup ad-hoc mode?
471 eval x=\$mode_${IFVAR}
472 x=${x:-managed}
473 case "${x}" in
474 ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;;
475 esac
476
477 if [ "${x}" != "managed" -a "${x}" != "auto" -a "${x}" != "ad-hoc" -a "${x}" != "adhoc" -a ${x} != "master" ] ; then
478 eerror "Only managed, ad-hoc, master and auto modes are supported"
479 return 1
480 fi
481
482 # Has an ESSID been forced?
483 if [ -n "${SSID}" ]; then
484 iwconfig_set_mode "${x}"
485 iwconfig_associate && return 0
486 [ "${SSID}" = "any" ] && iwconfig_force_preferred && return 0
487
488 eval SSID=\$adhoc_ssid_${IFVAR}
489 if [ -n "${SSID}" ]; then
490 iwconfig_setup_specific adhoc
491 return $?
492 fi
493 return 1
494 fi
495
496 # Are we forcing preferred only?
497 eval x=\$associate_order_${IFVAR}
498 [ -n "${x}" ] && associate_order=${x}
499 associate_order=${associate_order:-any}
500 if [ "${associate_order}" = "forcepreferredonly" ]; then
501 iwconfig_force_preferred && return 0
502 else
503 iwconfig_scan || return 1
504 iwconfig_connect_preferred && return 0
505 [ "${associate_order}" = "forcepreferred" ] || \
506 [ "${associate_order}" = "forceany" ] && \
507 iwconfig_force_preferred && return 0
508 [ "${associate_order}" = "any" ] || \
509 [ "${associate_order}" = "forceany" ] && \
510 iwconfig_connect_not_preferred && return 0
511 fi
512
513 e="associate with"
514 [ -z "${MAC_0}" ] && e="find"
515 [ "${preferred_aps}" = "force" ] || \
516 [ "${preferred_aps}" = "forceonly" ] && \
517 e="force"
518 e="Couldn't ${e} any access points on ${IFACE}"
519
520 eval SSID=\$adhoc_ssid_${IFVAR}
521 if [ -n "${SSID}" ]; then
522 ewarn "${e}"
523 iwconfig_setup_specific adhoc
524 return $?
525 fi
526
527 eerror "${e}"
528 return 1
529 }
530
531 iwconfig_pre_start() {
532 # We don't configure wireless if we're being called from
533 # the background
534 ${IN_BACKGROUND} && return 0
535
536 save_options "SSID" ""
537 _exists || return 0
538
539 if ! _is_wireless ; then
540 veinfo "${IFACE} is not wireless"
541 return 0
542 fi
543
544 iwconfig_defaults
545 iwconfig_user_config
546
547 # Set the base metric to be 2000
548 metric=2000
549
550 einfo "Configuring wireless network for ${IFACE}"
551
552 if iwconfig_configure ; then
553 save_options "ESSID" "${ESSID}"
554 return 0
555 fi
556
557 eerror "Failed to configure wireless for ${IFACE}"
558 iwconfig_defaults
559 #iwconfig "${IFACE}" txpower 0 2>/dev/null
560 unset SSID SSIDVAR
561 _down
562 return 1
563 }
564
565 iwconfig_post_stop() {
566 ${IN_BACKGROUND} && return 0
567 _is_wireless || return 0
568 iwconfig_defaults
569 #iwconfig "${IFACE}" txpower 0 2>/dev/null
570 }
571
572 # vim: set ts=4

  ViewVC Help
Powered by ViewVC 1.1.20