/[vps]/baselayout-vserver/trunk/net-scripts/net.modules.d/iwconfig
Gentoo

Contents of /baselayout-vserver/trunk/net-scripts/net.modules.d/iwconfig

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20