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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20