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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2566 - (show annotations) (download) (as text)
Mon Apr 9 18:57:57 2007 UTC (7 years, 3 months ago) by uberlord
File MIME type: text/x-sh
File size: 12556 byte(s)
duh
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 -o -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}" authmode open
457 ifconfig "${IFACE}" -mediaopt adhoc
458 ifconfig "${IFACE}" -mediaopt hostap
459 }
460
461 iwconfig_configure() {
462 local x APS
463 eval SSID=\$ssid_${IFVAR}
464
465 # Setup ad-hoc mode?
466 eval x=\$mode_${IFVAR}
467 x=${x:-managed}
468 case "${x}" in
469 ad-hoc|adhoc|hostap|master) iwconfig_setup_specific "${x}" ;;
470 esac
471
472 if [ "${x}" != "managed" -a "${x}" != "auto" ] ; then
473 eerror "Only managed, ad-hoc, master and auto modes are supported"
474 return 1
475 fi
476
477 # Has an ESSID been forced?
478 if [ -n "${SSID}" ]; then
479 iwconfig_set_mode "${x}"
480 iwconfig_associate && return 0
481 [ "${SSID}" = "any" ] && iwconfig_force_preferred && return 0
482
483 eval SSID=\$adhoc_ssid_${IFVAR}
484 if [ -n "${SSID}" ]; then
485 iwconfig_setup_specific adhoc
486 return $?
487 fi
488 return 1
489 fi
490
491 # Are we forcing preferred only?
492 eval x=\$associate_order_${IFVAR}
493 [ -n "${x}" ] && associate_order=${x}
494 associate_order=${associate_order:-any}
495 if [ "${associate_order}" = "forcepreferredonly" ]; then
496 iwconfig_force_preferred && return 0
497 else
498 iwconfig_scan || return 1
499 iwconfig_connect_preferred && return 0
500 [ "${associate_order}" = "forcepreferred" ] || \
501 [ "${associate_order}" = "forceany" ] && \
502 iwconfig_force_preferred && return 0
503 [ "${associate_order}" = "any" ] || \
504 [ "${associate_order}" = "forceany" ] && \
505 iwconfig_connect_not_preferred && return 0
506 fi
507
508 e="associate with"
509 [ -z "${MAC_0}" ] && e="find"
510 [ "${preferred_aps}" = "force" ] || \
511 [ "${preferred_aps}" = "forceonly" ] && \
512 e="force"
513 e="Couldn't ${e} any access points on ${IFACE}"
514
515 eval SSID=\$adhoc_ssid_${IFVAR}
516 if [ -n "${SSID}" ]; then
517 ewarn "${e}"
518 iwconfig_setup_specific adhoc
519 return $?
520 fi
521
522 eerror "${e}"
523 return 1
524 }
525
526 iwconfig_pre_start() {
527 # We don't configure wireless if we're being called from
528 # the background
529 ${IN_BACKGROUND} && return 0
530
531 save_options "SSID" ""
532 _exists || return 0
533
534 if ! _is_wireless ; then
535 veinfo "${IFACE} is not wireless"
536 return 0
537 fi
538
539 iwconfig_defaults
540 iwconfig_user_config
541
542 # Set the base metric to be 2000
543 metric=2000
544
545 einfo "Configuring wireless network for ${IFACE}"
546
547 if iwconfig_configure ; then
548 save_options "ESSID" "${ESSID}"
549 return 0
550 fi
551
552 eerror "Failed to configure wireless for ${IFACE}"
553 iwconfig_defaults
554 #iwconfig "${IFACE}" txpower 0 2>/dev/null
555 unset SSID SSIDVAR
556 _down
557 return 1
558 }
559
560 iwconfig_post_stop() {
561 ${IN_BACKGROUND} && return 0
562 _is_wireless || return 0
563 iwconfig_defaults
564 #iwconfig "${IFACE}" txpower 0 2>/dev/null
565 }
566
567 # vim: set ts=4

  ViewVC Help
Powered by ViewVC 1.1.20