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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20