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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.20