/[baselayout]/branches/baselayout-1_12/net-scripts/net.modules.d/iwconfig
Gentoo

Contents of /branches/baselayout-1_12/net-scripts/net.modules.d/iwconfig

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1798 - (show annotations) (download)
Tue Jan 10 12:43:34 2006 UTC (8 years, 9 months ago) by uberlord
File size: 24669 byte(s)
Fixed bonding module, improved wireless with rt2500 and WPA
1 #!/bin/bash
2 # Copyright (c) 2004-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # Contributed by Roy Marples (uberlord@gentoo.org)
6 # Many thanks to all the people in the Gentoo forums for their ideas and
7 # motivation for me to make this and keep on improving it
8
9 # Fix any potential localisation problems
10 # Note that LC_ALL trumps LC_anything_else according to locale(7)
11 iwconfig() {
12 LC_ALL=C /sbin/iwconfig "$@"
13 }
14 iwgetid() {
15 LC_ALL=C /sbin/iwgetid "$@"
16 }
17 iwlist() {
18 LC_ALL=C /sbin/iwlist "$@"
19 }
20 iwpriv() {
21 LC_ALL=C /sbin/iwpriv "$@"
22 }
23
24 # void iwconfig_depend(void)
25 #
26 # Sets up the dependancies for the module
27 iwconfig_depend() {
28 after plug
29 before interface
30 provide wireless
31 functions interface_up interface_down interface_exists
32 }
33
34 # bool iwconfig_check_installed(void)
35 #
36 # Returns 1 if wireless-tools is installed, otherwise 0
37 iwconfig_check_installed() {
38 local report=${1:-false}
39 [[ -x /sbin/iwconfig ]] && return 0
40 ${report} && eerror "For Wireless (802.11) support, emerge net-wireless/wireless-tools"
41
42 if [[ ! -e /proc/net/wireless ]]; then
43 installed="1"
44 if ${report} ; then
45 eerror "iwconfig requires wireless support"
46 eerror "(CONFIG_NET_WIRELESS=y) enabled in the kernel"
47 fi
48 fi
49
50 return 1
51 }
52
53 # bool iwconfig_exists(char *interface)
54 #
55 # Checks to see if wireless extensions are enabled on the interface
56 iwconfig_exists() {
57 [[ ! -e /proc/net/wireless ]] && return 1
58 grep -q "^[ \t]*$1:[ \t]" /proc/net/wireless
59 }
60
61 # char* iwconfig_get_wep_status(char *interface)
62 #
63 # Echos a string showing whether WEP is enabled or disabled
64 # for the given interface
65 iwconfig_get_wep_status() {
66 local key=$( iwconfig "$1" | grep -i -o "Encryption key:[0-9,A-F]" )
67 local mode status="disabled"
68
69 if [[ -n ${key} ]]; then
70 status="enabled"
71 mode="$( iwconfig $1 | sed -n -e 's/^.*Security mode:\(.*[^ ]\).*/\1/p' )"
72 [[ -n ${mode} ]] && mode=" - ${mode}"
73 fi
74
75 echo "(WEP ${status}${mode})"
76 }
77
78 # char* iwconfig_get_essid(char *iface)
79 #
80 # Gets the current ESSID of the iface
81 iwconfig_get_essid() {
82 local i essid
83
84 for (( i=0; i<5; i++ )); do
85 essid="$( iwgetid $1 2>/dev/null | sed -n -e 's/^.*ESSID:"\(.*\)"$/\1/p' )"
86 if [[ -n ${essid} ]]; then
87 echo "${essid}"
88 return 0
89 fi
90 sleep 1
91 done
92
93 return 1
94 }
95
96 # char* iwconfig_get_ap_mac_address(char *interface)
97 #
98 # Returns the MAC address of the Access Point
99 # the interface is connected to
100 iwconfig_get_ap_mac_address() {
101 iwgetid --ap "$1" | sed -n -e 's/^.*Cell: .*\<\(..:..:..:..:..:..\)\>.*/\U\1/p'
102 }
103
104 # char* iwconfig_get_mode(char *interface)
105 #
106 # Returns the wireless mode in lower case
107 iwconfig_get_mode() {
108 iwgetid --mode "$1" | sed -n -e 's/^.*Mode:\(.*\)/\L\1/p'
109 }
110
111 # char* iwconfig_get_type(char *interface)
112 #
113 # Returns the type of interface - the IEEE part
114 iwconfig_get_type() {
115 iwconfig "$1" | sed -n -e 's/^'"$1"' *\([^ ]* [^ ]*\).*/\1/p'
116 }
117
118 # void iwconfig_report(char *interface)
119 #
120 # Output how our wireless interface has been configured
121 iwconfig_report() {
122 local iface="$1" essid mac m="to"
123
124 essid=$( iwconfig_get_essid "${iface}" )
125
126 local wep_status=$( iwconfig_get_wep_status "${iface}" )
127 local channel=$( iwgetid --channel "${iface}" 2>/dev/null | cut -d: -f2 )
128 [[ -n ${channel} ]] && channel="on channel ${channel} "
129
130 essid="${essid//\\\\/\\\\}"
131 local mode=$( iwconfig_get_mode "${iface}" )
132 if [[ ${mode} == "master" ]]; then
133 m="as"
134 elif [[ ${mode} != "ad-hoc" ]]; then
135 mac=$( iwconfig_get_ap_mac_address "${iface}" )
136 [[ -n ${mac} ]] && mac=" at ${mac}"
137 fi
138
139 eindent
140 einfo "${iface} connected ${m} \"${essid}\"${mac}"
141 einfo "in ${mode} mode ${channel}${wep_status}"
142 eoutdent
143 }
144
145 # char* iwconfig_get_wep_key(char *mac_address)
146 #
147 # Returns the configured WEP key for the given mac address
148 # or the given ESSID. The mac address setting takes precendence
149 iwconfig_get_wep_key() {
150 local mac="$1" key
151 key="mac_key_${mac//:/}"
152 [[ -z ${!key} ]] && key="key_${ESSIDVAR}"
153 echo "${!key:-off}"
154 }
155
156 # void iwconfig_user_config(char *iface, char *ifvar)
157 #
158 # Applies the user configuration to the interface
159 iwconfig_user_config() {
160 local iface="$1" conf aconf ifvar="$2"
161 [[ -z ${ifvar} ]] && ifvar=$( bash_variable "$1" )
162
163 # Apply the user configuration
164 conf="iwconfig_${ifvar}"
165 if [[ -n ${!conf} ]]; then
166 aconf=( "${!conf} ")
167 for conf in "${aconf[@]}" ; do
168 if ! eval iwconfig "${iface}" "${conf}" ; then
169 ewarn "${iface} does not support the following configuration commands"
170 ewarn " ${conf}"
171 fi
172 done
173 fi
174
175 conf="iwpriv_${ifvar}[@]"
176 if [[ -n ${!conf} ]]; then
177 aconf=( "${!conf}" )
178 for conf in "${aconf[@]}" ; do
179 if ! eval iwpriv "${iface}" "${conf}" ; then
180 ewarn "${iface} does not support the following private ioctls"
181 ewarn " ${conf}"
182 fi
183 done
184 fi
185 }
186
187 # bool iwconfig_setup_specific(char *iface)
188 #
189 # Sets up our wireless interface to operate in ad-hoc or master mode
190 iwconfig_setup_specific() {
191 local iface="$1" mode="$2" channel key dessid
192 local ifvar=$( bash_variable "$1" )
193
194 if [[ -z ${ESSID} ]]; then
195 eerror "${iface} requires an ESSID to be set to operate in ${mode} mode"
196 eerror "adjust the essid_${iface} setting in /etc/conf.d/wireless"
197 return 1
198 fi
199 dessid="${ESSID//\\\\/\\\\}"
200 ESSIDVAR=$( bash_variable "${ESSID}" )
201 key=$( iwconfig_get_wep_key )
202
203 # We only change the mode if it's not the same
204 local cur_mode=$( iwconfig_get_mode "${iface}" )
205 if [[ ${cur_mode} != "${mode}" ]]; then
206 if ! iwconfig "${iface}" mode "${mode}" ; then
207 eerror "${iface} does not support setting the mode to \"${mode}\""
208 return 1
209 fi
210 fi
211
212 channel="channel_${ifvar}"
213 # We default the channel to 3
214 channel="${!channel:-3}"
215 if ! iwconfig "${iface}" channel "${channel}" ; then
216 ewarn "${iface} does not support setting the channel to \"${channel}\""
217 return 1
218 fi
219
220 # Now set the key
221 if ! eval iwconfig "${iface}" key "${key}" ; then
222 if [[ ${key} != "off" ]]; then
223 ewarn "${iface} does not support setting keys"
224 ewarn "or the parameter \"mac_key_${ESSIDVAR}\" or \"key_${ESSIDVAR}\" is incorrect"
225 fi
226 fi
227
228 iwconfig_user_config "${iface}" "${ESSIDVAR}"
229
230 # Finally set the ESSID
231 if ! iwconfig "${iface}" essid "${ESSID}" ; then
232 eerror "${iface} does not support setting ESSID to \"${dessid}\""
233 return 1
234 fi
235 iwconfig "${iface}" nick "${ESSID}" 2>/dev/null
236
237 iwconfig_report "${iface}"
238
239 return 0
240 }
241
242 # bool iwconfig_associate_mac(char *iface)
243 #
244 # Returns true if the AP MAC address is valid or not
245 iwconfig_associate_mac() {
246 # Checks if a MAC address has been assigned
247 local mac="$( iwconfig_get_ap_mac_address $1 )" i
248 local -a invalid_macs=(
249 "00:00:00:00:00:00"
250 "44:44:44:44:44:44"
251 "FF:00:00:00:00:00"
252 "FF:FF:FF:FF:FF:FF"
253 )
254
255 [[ -z ${mac} ]] && return 1
256 for i in "${invalid_macs[@]}"; do
257 [[ ${mac} == "${i}" ]] && return 1
258 done
259 return 0
260 }
261
262 # bool iwconfig_associate_quality(char *iface)
263 #
264 # Returns true if the link quality is not 0 or 0.
265 iwconfig_associate_quality() {
266 local quality=$( \
267 sed -n -e 's/^.*'"$1"': *[0-9]* *\([0-9]*\).*/\1/p' \
268 /proc/net/wireless
269 )
270 [[ ${quality} != "0" ]]
271 return "$?"
272 }
273
274 # bool iwconfig_test_associated(char *iface)
275 #
276 # Returns true if the interface has associated with an Access Point
277 iwconfig_test_associated() {
278 local iface="$1" ttype ifvar=$( bash_variable "$1" ) x
279 # Some drivers don't set MAC to a bogus value when assocation is lost/fails
280 # whereas they do set link quality to 0
281
282 x="associate_test_${ifvar}"
283 ttype=$( echo "${!x:-mac}" | tr '[:upper:]' '[:lower:]' )
284 if [[ ${ttype} != "mac" && ${ttype} != "quality" && ${ttype} != "all" ]]; then
285 ewarn " associate_test_${iface} is not set to mac, quality or all"
286 ewarn " defaulting to \"mac\""
287 test="mac"
288 fi
289
290 case "${ttype}" in
291 mac) iwconfig_associate_mac "${iface}" && return 0 ;;
292 quality) iwconfig_associate_quality "${iface}" && return 0 ;;
293 all) iwconfig_associate_mac "${iface}" \
294 && iwconfig_associate_quality "${iface}" && return 0 ;;
295 esac
296
297 return 1
298 }
299
300 # bool iwconfig_wait_for_association(char *iface)
301 #
302 # Waits for a configured ammount of time until
303 # we are assocaited with an Access Point
304 iwconfig_wait_for_association() {
305 local iface="$1" i=0 timeout ifvar=$( bash_variable "$1" )
306 timeout="associate_timeout_${ifvar}"
307 [[ -z ${!timeout} ]] && timeout="sleep_associate_${ifvar}"
308 timeout="${!timeout:-10}"
309
310 [[ ${timeout} == "0" ]] \
311 && vewarn "WARNING: infinite timeout set for association on ${iface}"
312
313 while true; do
314 iwconfig_test_associated "${iface}" && return 0
315 sleep 1
316 [[ ${timeout} == "0" ]] && continue
317 (( i++ ))
318 [[ ${i} == "${timeout}" || ${i} -gt ${timeout} ]] && break
319 done
320 return 1
321 }
322
323 # bool iwconfig_associate(char *interface, char *mac_address, char *wep_required)
324 #
325 # Tries to associate the interface with an Access Point
326 # If we scanned the Access Point we know if we need WEP to associate or not
327 # and if we have a WEP key for the ESSID or not
328 # so we can fail gracefully without even trying to connect
329 iwconfig_associate() {
330 local iface="$1" mode="${2:-managed}"
331 local mac="$3" wep_required="$4" w="(WEP Disabled)"
332 local dessid="${ESSID//\\\\/\\\\}" key
333
334 iwconfig "${iface}" mode "${mode}" 2>/dev/null
335 if [[ ${ESSID} == "any" ]]; then
336 iwconfig "${iface}" ap any 2>/dev/null
337 dessid="any"
338 unset ESSIDVAR
339 else
340 ESSIDVAR=$( bash_variable "${ESSID}" )
341 key=$( iwconfig_get_wep_key "${mac}" )
342 if [[ ${wep_required} == "on" && ${key} == "off" ]]; then
343 eerror "WEP key is not set for \"${dessid}\" - not connecting"
344 return 1
345 fi
346 if [[ ${wep_required} == "off" && ${key} != "off" ]]; then
347 key="off"
348 ewarn "\"${dessid}\" is not WEP enabled - ignoring setting"
349 fi
350
351 if ! eval iwconfig "${iface}" key "${key}" ; then
352 if [[ ${key} != "off" ]]; then
353 ewarn "${iface} does not support setting keys"
354 ewarn "or the parameter \"mac_key_${ESSIDVAR}\" or \"key_${ESSIDVAR}\" is incorrect"
355 return 1
356 fi
357 fi
358 [[ ${key} != "off" ]] && w=$( iwconfig_get_wep_status "${iface}" )
359 fi
360
361 iwconfig_user_config "${iface}" "${ESSIDVAR}"
362
363 if ! iwconfig "${iface}" essid "${ESSID}" ; then
364 if [[ ${ESSID} != "any" ]]; then
365 ewarn "${iface} does not support setting ESSID to \"${dessid}\""
366 fi
367 fi
368 iwconfig "${iface}" nick "${ESSID}" 2>/dev/null
369
370 vebegin "Connecting to \"${dessid}\" ${w}"
371
372 if [[ ${ESSID} != "any" ]] && is_function preassociate ; then
373 veinfo "Running preassociate function"
374 eindent
375 ( preassociate "${iface}" )
376 e="$?"
377 eoutdent
378 if [[ ${e} != 0 ]]; then
379 veend 1 "preassociate \"${dessid}\" on ${iface} failed"
380 return 1
381 fi
382 fi
383
384 if ! iwconfig_wait_for_association "${iface}" ; then
385 veend 1
386 return 1
387 fi
388 veend 0
389
390 if [[ ${ESSID} == "any" ]]; then
391 ESSID=$( iwconfig_get_essid "${iface}" )
392 iwconfig_associate "${iface}"
393 return $?
394 fi
395
396 iwconfig_report "${iface}"
397
398 if is_function postassociate ; then
399 veinfo "Running postassociate function"
400 eindent
401 ( postassociate "${iface}" )
402 eoutdent
403 fi
404
405 return 0
406 }
407
408 # bool iwconfig_scan(char *iface)
409 #
410 # Fills 3 arrays with information from a wireless scan
411 iwconfig_scan() {
412 local iface="$1" mode x ifvar=$( bash_variable "$1" )
413
414 # First, we may need to change mode to scan in
415 x="scan_mode_${ifvar}"
416 mode=$( echo "${!x}" | tr '[:upper:]' '[:lower:]' )
417 if [[ -n ${mode} ]]; then
418 if ! iwconfig "${iface}" mode "${mode}" ; then
419 ewarn "${iface} does not support setting the mode to \"${mode}\""
420 fi
421 fi
422
423 # Next we set any private driver ioctls needed
424 x="iwpriv_scan_pre_${ifvar}"
425 if [[ -n ${!x} ]]; then
426 if ! eval iwpriv "${iface}" "${!x}" ; then
427 ewarn "${iface} does not support the following private ioctls" \
428 ewarn " ${!x}"
429 fi
430 fi
431
432 # Set the essid to any. This is required for scanning
433 #iwconfig "${iface}" essid any
434
435 veinfo "Scanning for access points"
436
437 # Sleep if required
438 x="sleep_scan_${ifvar}"
439 [[ -z ${!x} || ${!x} -gt 0 ]] && sleep "${!x:-1}"
440
441 local error=true i=-1 line
442 local -a mac essid enc qual mode
443
444 while read line; do
445 error=false
446 case "${line}" in
447 *Address:*)
448 (( i++ ))
449 mac[i]=$( echo "${line#*: }" | tr '[:lower:]' '[:upper:]' )
450 ;;
451 *ESSID:*)
452 essid[i]="${line#*\"}"
453 essid[i]="${essid[i]%*\"}"
454 ;;
455 *Mode:*)
456 mode[i]=$(echo "${line#*:}" | tr '[:upper:]' '[:lower:]' )
457 [[ ${mode[i]} == "master" ]] && mode[i]="managed"
458 ;;
459 *'Encryption key:'*)
460 enc[i]="${line#*:}"
461 ;;
462 *Quality*)
463 qual[i]="${line#*:}"
464 qual[i]="${qual[i]%/*}"
465 qual[i]="${qual[i]//[![:digit:]]/}"
466 qual[i]="${qual[i]:-0}"
467 ;;
468 esac
469 done < <( iwlist "${iface}" scan 2>/dev/null )
470
471 if ${error}; then
472 ewarn "${iface} does not support scanning"
473 x="adhoc_essid_${ifvar}"
474 [[ -n ${!x} ]] && return 0
475 if [[ -n ${preferred_aps} ]]; then
476 [[ ${associate_order} == "forcepreferred" \
477 || ${associate_order} == "forcepreferredonly" ]] && return 0
478 fi
479 eerror "You either need to set a preferred_aps list in /etc/conf.d/wireless"
480 eerror " preferred_aps=( \"ESSID1\" \"ESSID2\" )"
481 eerror " and set associate_order_${iface}=\"forcepreferred\""
482 eerror " or set associate_order_${iface}=\"forcepreferredonly\""
483 eerror "or hardcode the ESSID to \"any\" and let the driver find an Access Point"
484 eerror " essid_${iface}=\"any\""
485 eerror "or configure defaulting to Ad-Hoc when Managed fails"
486 eerror " adhoc_essid_${iface}=\"WLAN\""
487 eerror "or hardcode the ESSID against the interface (not recommended)"
488 eerror " essid_${iface}=\"ESSID\""
489 return 1
490 fi
491
492 # We may need to unset the previous private driver ioctls
493 x="iwpriv_scan_post_${ifvar}"
494 if [[ -n ${!x} ]]; then
495 if ! eval iwpriv "${iface}" "${!x}" ; then
496 ewarn "${iface} does not support the following private ioctls" \
497 ewarn " ${!x}"
498 fi
499 fi
500
501 # Change back mode if needed
502 x="mode_${ifvar}"
503 x=$( echo "${!x:-managed}" | tr '[:upper:]' '[:lower:]' )
504 [[ ${mode} != "${x}" ]] && iwconfig "${iface}" mode "${x}"
505
506 for (( i=0; i<${#mac[@]}; i++ )); do
507 # Don't like ad-hoc nodes by default
508 [[ ${mode[i]} == "ad-hoc" ]] && (( qual[i]-=10000 ))
509 sortline="${sortline}${qual[i]} ${i}\n"
510 done
511
512 sortline=( $( echo -e "${sortline}" | sort -nr ) )
513
514 for (( i=0; i<${#mac[@]}; i++ )); do
515 (( x=(i * 2) + 1 ))
516 mac_APs[i]="${mac[${sortline[x]}]}"
517 essid_APs[i]="${essid[${sortline[x]}]}"
518 mode_APs[i]="${mode[${sortline[x]}]}"
519 enc_APs[i]="${enc[${sortline[x]}]}"
520 done
521
522 return 0
523 }
524
525 # void iwconfig_scan_report(void)
526 #
527 # Report the results of the scan and re-map any ESSIDs if they
528 # have been configured for the MAC address found
529 iwconfig_scan_report() {
530 local i k m remove
531 local -a u
532
533 [[ -z ${mac_APs} ]] && ewarn " no access points found"
534
535 # We need to do the for loop like this so we can
536 # dynamically remove from the array
537 eindent
538 for ((i=0; i<${#mac_APs[@]}; i++)); do
539 k=""
540 [[ ${enc_APs[i]} == "yes" ]] && k="(WEP required)"
541
542 if [[ -z ${essid_APs[i]} ]]; then
543 veinfo "Found ${mac_APs[i]} ${k}"
544 else
545 veinfo "Found \"${essid_APs[i]//\\\\/\\\\}\" at ${mac_APs[i]} ${k}"
546 fi
547
548 eindent
549
550 m="mac_essid_${mac_APs[i]//:/}"
551 if [[ -n ${!m} ]]; then
552 essid_APs[i]="${!m}"
553 veinfo "mapping to \"${!m//\\\\/\\\\}\""
554 fi
555
556 remove=false
557 # If we don't know the essid then we cannot connect to them
558 # so we remove them from our array
559 if [[ -z ${essid_APs[i]} ]]; then
560 remove=true
561 else
562 for k in "${blacklist_aps[@]}"; do
563 if [[ ${k} == "${essid_APs[i]}" ]]; then
564 vewarn "\"${k//\\\\/\\\\}\" has been blacklisted - not connecting"
565 remove=true
566 break
567 fi
568 done
569 fi
570
571 eoutdent
572
573 ${remove} && u=( "${u[@]}" "${i}" )
574 done
575
576 eoutdent
577
578 # Now we remove any duplicates
579 for ((i=0; i < ${#essid_APs[@]} - 1; i++)); do
580 for ((j=${i} + 1; j <${#essid_APs[@]}; j++)); do
581 [[ ${essid_APs[i]} == "${essid_APs[j]}" ]] && u=( "${u[@]}" "${j}" )
582 done
583 done
584
585 for i in ${u[@]}; do
586 unset essid_APs[i]
587 unset mode_APs[i]
588 unset mac_APs[i]
589 unset enc_APs[i]
590 done
591
592 # We need to squash our arrays so indexes work again
593 essid_APs=( "${essid_APs[@]}" )
594 mode_APs=( "${mode_APs[@]}" )
595 mac_APs=( "${mac_APs[@]}" )
596 enc_APs=( "${enc_APs[@]}" )
597 }
598
599 # bool iwconfig_force_preferred(char *iface)
600 #
601 # Forces the preferred_aps list to associate in order
602 # but only if they were not picked up by our scan
603 iwconfig_force_preferred() {
604 local iface=$1 essid i
605
606 [[ -z ${preferred_aps} ]] && return 1
607
608 ewarn "Trying to force preferred in case they are hidden"
609 for essid in "${preferred_aps[@]}"; do
610 local found_AP=false
611 for ((i = 0; i < ${#mac_APs[@]}; i++)); do
612 if [[ ${essid} == "${essid_APs[i]}" ]]; then
613 found_AP=true
614 break
615 fi
616 done
617 if ! ${found_AP} ; then
618 ESSID="${essid}"
619 iwconfig_associate "${iface}" && return 0
620 fi
621 done
622
623 ewarn "Failed to associate with any preferred access points on ${iface}"
624 return 1
625 }
626
627 # bool iwconfig_connect_preferred(char *iface)
628 #
629 # Connects to preferred_aps in order if they were picked up
630 # by our scan
631 iwconfig_connect_preferred() {
632 local iface="$1" essid i
633
634 for essid in "${preferred_aps[@]}"; do
635 for ((i=0; i<${#essid_APs[@]}; i++)); do
636 if [[ ${essid} == "${essid_APs[i]}" ]]; then
637 ESSID="${essid}"
638 iwconfig_associate "${iface}" "${mode_APs[i]}" "${mac_APs[i]}" \
639 "${enc_APs[i]}" && return 0
640 break
641 fi
642 done
643 done
644
645 return 1
646 }
647
648 # bool iwconfig_connect_not_preferred(char *iface)
649 #
650 # Connects to any AP's found that are not in
651 # our preferred list
652 iwconfig_connect_not_preferred() {
653 local iface=$1 i ap has_preferred
654
655 for ((i=0; i<${#mac_APs[@]}; i++)); do
656 has_preferred=false
657 for ap in "${preferred_aps[@]}"; do
658 if [[ ${ap} == "${essid_APs[i]}" ]]; then
659 has_preferred=true
660 break
661 fi
662 done
663 if ! ${has_preferred} ; then
664 ESSID="${essid_APs[i]}"
665 iwconfig_associate "${iface}" "${mode_APs[i]}" "${mac_APs[i]}" \
666 "${enc_APs[i]}" && return 0
667 fi
668 done
669
670 return 1
671 }
672
673 # void iwconfig_defaults(char *iface)
674 #
675 # Apply some sane defaults to the wireless interface
676 # incase the user already applied some changes
677 iwconfig_defaults() {
678 local iface="$1"
679
680 # Set some defaults
681 iwconfig "${iface}" rate auto &>/dev/null
682 iwconfig "${iface}" rts off &>/dev/null
683 iwconfig "${iface}" frag off &>/dev/null
684 iwconfig "${iface}" power off &>/dev/null
685 iwconfig "${iface}" txpower auto &>/dev/null
686 iwconfig "${iface}" key [1] off &>/dev/null
687 iwconfig "${iface}" mode managed &>/dev/null
688 }
689
690 # void iwconfig_strip_associated(char *iface)
691 #
692 # We check to see which ifaces have associated AP's except for the iface
693 # given and remove those AP's from the scan list
694 # We also remove from the preferred list
695 iwconfig_strip_associated() {
696 local iface="$1" e a j
697 local essid=$( iwconfig_get_essid "${iface}" )
698 local -a ifaces=( $( iwconfig 2>/dev/null | grep -o "^\w*" ) )
699
700 for i in "${ifaces[@]}"; do
701 [[ ${i} == ${iface} ]] && continue
702 interface_is_up "${i}" || continue
703 iwconfig_test_associated "${i}" || continue
704 e=$( iwconfig_get_essid "${i}" )
705 u=()
706 for ((j=0; j<${#mac_APs[@]}; j++)); do
707 if [[ ${essid_APs[j]} == "${e}" ]]; then
708 ewarn "${e} has already been associated with ${i}"
709 unset essid_APs[j]
710 unset mode_Aps[j]
711 unset mac_APs[j]
712 unset enc_APs[j]
713 # We need to squash our arrays so that indexes work
714 essid_APs=( "${essid_APs[@]}" )
715 mode_APs=( "${mode_APs[@]}" )
716 mac_APs=( "${mac_APs[@]}" )
717 enc_APs=( "${enc_APs[@]}" )
718 break
719 fi
720 done
721 for ((j=0; j<${#preferred_aps[@]}; j++)); do
722 if [[ ${preferred_aps[j]} == "${e}" ]]; then
723 unset preferred_aps[j]
724 preferred_aps=( "${preferred_aps[@]}" )
725 break
726 fi
727 done
728 done
729 }
730
731 # bool iwconfig_configure(char *iface)
732 #
733 # The main startup code
734 # First we bring the interface up, apply defaults, apply user configuration
735 # Then we test to see if ad-hoc mode has been requested and branch if needed
736 # Then we scan for access points and try to connect to them in a predetermined order
737 # Once we're connected we show a report and then configure any interface
738 # variables for the ESSID
739 iwconfig_configure() {
740 local iface="$1" test x e ifvar=$( bash_variable "$1" )
741 local -a essid_APs mac_APs mode_APs enc_APs
742
743 iwconfig_defaults "${iface}"
744 iwconfig_user_config "${iface}"
745
746 ESSID="essid_${ifvar}"
747 ESSID="${!ESSID}"
748
749 # Setup ad-hoc mode?
750 x="mode_${ifvar}"
751 x=$( echo "${!x:-managed}" | tr '[:upper:]' '[:lower:]' )
752 if [[ ${x} == "ad-hoc" || ${x} == "master" ]]; then
753 iwconfig_setup_specific "${iface}" "${x}"
754 return $?
755 fi
756
757 if [[ ${x} != "managed" && ${x} != "auto" ]]; then
758 eerror "Only managed, ad-hoc, master and auto modes are supported"
759 return 1
760 fi
761
762 # We only change the mode if it's not the same as some drivers
763 # only do managed and throw an error changing to managed
764 local cur_mode=$( iwconfig_get_mode "${iface}" )
765 if [[ ${cur_mode} != "${x}" ]]; then
766 if ! iwconfig "${iface}" mode "${x}" ; then
767 eerror "${iface} does not support setting the mode to \"${x}\""
768 return 1
769 fi
770 fi
771
772 # These arrays hold the results of our scan
773 local -a mac_APs essid_APs enc_APs
774
775 # Has an ESSID been forced?
776 if [[ -n ${ESSID} ]]; then
777 iwconfig_associate "${iface}" && return 0
778 [[ ${ESSID} == "any" ]] && iwconfig_force_preferred "${iface}" && return 0
779
780 ESSID="adhoc_essid_${ifvar}"
781 ESSID="${!ESSID}"
782 if [[ -n ${ESSID} ]]; then
783 iwconfig_setup_specific "${iface}" ad-hoc
784 return $?
785 fi
786 return 1
787 fi
788
789 # Do we have a preferred Access Point list specific to the interface?
790 x="preferred_aps_${ifvar}[@]"
791 [[ -n ${!x} ]] && preferred_aps=( "${!x}" )
792
793 # Do we have a blacklist Access Point list specific to the interface?
794 x="blacklist_aps_${ifvar}[@]"
795 [[ -n ${!x} ]] && blacklist_aps=( "${!x}" )
796
797 # Are we forcing preferred only?
798 x="associate_order_${ifvar}"
799 [[ -n ${!x} ]] && associate_order="${!x}"
800 associate_order=$(
801 echo "${associate_order:-any}" \
802 | tr '[:upper:]' '[:lower:]'
803 )
804
805 if [[ ${associate_order} == "forcepreferredonly" ]]; then
806 iwconfig_force_preferred "${iface}" && return 0
807 else
808 iwconfig_scan "${iface}" || return 1
809 iwconfig_scan_report
810
811 # Strip AP's from the list that have already been associated with
812 # other wireless cards in the system if requested
813 x="unique_ap_${ifvar}"
814 [[ -n ${!x} ]] && unique_ap="${!x}"
815 unique_ap=$( echo "${unique_ap:-no}" | tr '[:upper:]' '[:lower:]' )
816 [[ ${unique_ap} != "no" ]] && iwconfig_strip_associated "${iface}"
817
818 iwconfig_connect_preferred "${iface}" && return 0
819 [[ ${associate_order} == "forcepreferred" \
820 || ${associate_order} == "forceany" ]] \
821 && iwconfig_force_preferred "${iface}" && return 0
822 [[ ${associate_order} == "any" || ${associate_order} == "forceany" ]] \
823 && iwconfig_connect_not_preferred "${iface}" && return 0
824 fi
825
826 e="associate with"
827 [[ -z ${mac_APs} ]] && e="find"
828 [[ ${preferred_only} == "force" || ${preferred_aps} == "forceonly" ]] \
829 && e="force"
830 e="Couldn't ${e} any access points on ${iface}"
831
832 ESSID="adhoc_essid_${ifvar}"
833 ESSID="${!ESSID}"
834 if [[ -n ${ESSID} ]]; then
835 ewarn "${e}"
836 iwconfig_setup_specific "${iface}" ad-hoc
837 return $?
838 fi
839
840 eerror "${e}"
841 return 1
842 }
843
844 # bool iwconfig_pre_start(char *iface)
845 #
846 # Start entry point
847 # First we check if wireless extensions exist on the interface
848 # If they are then we configue wireless
849 iwconfig_pre_start() {
850 local iface="$1" r=0
851
852 # We don't configure wireless if we're being called from
853 # the background
854 ${IN_BACKGROUND} && return 0
855
856 save_options "ESSID" ""
857 interface_exists "${iface}" || return 0
858
859 # We need to bring the interface up, as some cards do not register
860 # in /proc/wireless until they are brought up.
861 interface_up "${iface}"
862
863 if ! iwconfig_exists "${iface}" ; then
864 veinfo "Wireless extensions not found for ${iface}"
865 return 0
866 fi
867
868 # Check for rf_kill - only ipw supports this at present, but other
869 # cards may in the future.
870 if [[ -e "/sys/class/net/${iface}/device/rf_kill" ]]; then
871 if [[ $( < "/sys/class/net/${iface}/device/rf_kill" ) != 0 ]]; then
872 eerror "Wireless radio has been killed for interface ${iface}"
873 return 1
874 fi
875 fi
876
877 einfo "Configuring wireless network for ${iface}"
878
879 # Are we a proper IEEE device?
880 # Most devices reutrn IEEE 802.11b/g - but intel cards return IEEE in lower case
881 # and RA cards return RAPCI or similar which really sucks :(
882 # For the time being, we will test prism54 not loading firmware which reports
883 # NOT READY!
884 x=$( iwconfig_get_type "${iface}" )
885 if [[ ${x} == "NOT READY!" ]]; then
886 eerror "Looks like there was a probem loading the firmware for ${iface}"
887 return 1
888 fi
889
890 # Setup IFS incase parent script has modified it
891 local IFS=$' '$'\n'$'\t'
892
893 if iwconfig_configure "${iface}" ; then
894 save_options "ESSID" "${ESSID}"
895 return 0
896 fi
897
898 eerror "Failed to configure wireless for ${iface}"
899 iwconfig_defaults "${iface}"
900 unset ESSID ESSIDVAR
901 interface_down "${iface}"
902 return 1
903 }
904
905 # vim:ts=4

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20