home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / boot / i386 / rescue / etc / sysconfig / network / scripts / ifup-wireless < prev    next >
Text File  |  2006-11-29  |  25KB  |  726 lines

  1. #! /bin/bash
  2. #
  3. # Copyright (c) 2005 SUSE LINUX Products GmbH Nuernberg, Germany.
  4. # All rights reserved.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it under
  7. # the terms of the GNU General Public License as published by the Free Software
  8. # Foundation; either version 2 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU General Public License along with
  17. # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. # Place, Suite 330, Boston, MA 02111-1307 USA
  19. #
  20. # Author: Joachim Gleissner <jg@suse.de>, 2005
  21. #
  22. # $Id: ifup-wireless 1510 2006-10-27 13:32:53Z jg $
  23. #
  24.  
  25. usage () {
  26.         echo $@
  27.         echo "Usage: if{up,down,status}-wireless [<config>] <hwdesc> [-o <options>]"
  28.         echo "  hwdesc may be the interface name or any valid description"
  29.         echo "  of the corresponding device, for details see ifup(8)."
  30.         echo "Options are: [on]boot : we are currently booting (or shutting down)"
  31.         echo "             hotplug  : we are handling a hotplug event"
  32.         echo "All other or wrong options are silently ignored."
  33.         exit $R_USAGE
  34. }
  35.  
  36. ######################################################################
  37. # change the working direcory and source some common files
  38. #
  39. R_INTERNAL=1      # internal error, e.g. no config or missing scripts
  40. cd /etc/sysconfig/network || exit $R_INTERNAL
  41. test -f ./config && . ./config
  42. test -f scripts/functions && . scripts/functions || exit $R_INTERNAL
  43.  
  44. ######################################################################
  45. # check arguments and how we are called (in case of links)
  46. #
  47. SCRIPTNAME=${0##*/}
  48. debug $*
  49. ACTION=${SCRIPTNAME#if}
  50. ACTION=${ACTION%%-wireless}
  51. case "${ACTION}" in
  52.     up|down|status) ;;
  53.     check) exit $R_SUCCESS ;;
  54.     *) usage
  55. esac
  56. INTERFACE=$1
  57. case "$INTERFACE" in ""|-h|*help*) usage; esac
  58. shift
  59. if [ -n "$1" -a "$1" != "-o" ] ; then
  60.         CONFIG=$INTERFACE
  61.         INTERFACE=$1
  62. fi
  63. shift
  64. test "$1" = "-o" && shift
  65. OPTIONS=$@
  66. MODE=manual
  67. while [ $# -gt 0 ]; do
  68.         case $1 in
  69.                 boot|onboot) MODE=onboot ;;
  70.                 hotplug)     MODE=hotplug ;;
  71.                 quiet)       be_quiet_has_gone ;;
  72.                 debug)       DEBUG=yes ;;
  73.                 *)           debug "unknown option $1 ignored" ;;
  74.         esac
  75.         shift
  76. done
  77.  
  78.  
  79. ######################################################################
  80. # check presence of global configuration file and source it
  81. #
  82. test -f ./wireless && . ./wireless
  83. # ifcfg-* is more important and fragile then wireless
  84. if [ -f ./ifcfg-$CONFIG ] ; then
  85.    . ./ifcfg-$CONFIG
  86. elif [ "$ACTION" != down ] ; then
  87.    err_mesg "could not find configuration file ifcfg-$CONFIG"
  88. fi
  89.  
  90.  
  91. ######################################################################
  92. # get the interface and check if it is available and wireless
  93. # skip check on action "down", possibly running wpa_supplicant needs
  94. # to be shut down even when the interface is already gone
  95. #
  96. if [ "${ACTION}" != "down" ]; then
  97.     if ! is_iface_available  ${INTERFACE} ; then
  98.         test "$ACTION" != status && logerror "interface ${INTERFACE} is not available"
  99.         exit $R_NODEV
  100.     fi
  101. # $WIRELESS=yes/no can be set in a config file if there is no other way to
  102. # find out. If it is empty we use the type we got from getcfg (HWD_INTERFACETYPE_0)
  103.     if [ "$WIRELESS" != yes -a "$HWD_INTERFACETYPE_0" != wlan ]; then
  104.         info_mesg "$HWD_HWDESC is not wireless, exiting"
  105.         exit 0
  106.     fi
  107. fi
  108.  
  109. ######################################################################
  110. # helper functions
  111. #
  112. get_network_count()
  113. {
  114.     count=0
  115.     for i in `seq 1 9` ; do
  116.         W="`eval echo \\$WIRELESS_AUTH_MODE_$i`"
  117.         if [ -n "`eval echo $\"$W\"`" ]; then
  118.             count=$((count+1))
  119.         fi
  120.     done
  121.     return $count
  122. }
  123.  
  124. get_first_index()
  125. {
  126.     test -n "$WIRELESS_AUTH_MODE" && return 0
  127.     for i in 0 1 2 3 4 5 6 7 8 9 ; do
  128.         eval W="\$WIRELESS_AUTH_MODE_$i"
  129.         test -n "$W" && return $i
  130.     done
  131.     return 255
  132. }
  133.  
  134. get_next_index()
  135. {
  136.     local i=$1
  137.     while [ $i -lt 9 ]; do
  138.         i=$((i+1))
  139.         eval W="\$WIRELESS_AUTH_MODE_$i"
  140.         test -n "$W" && return $i
  141.     done
  142.     return 255
  143. }
  144.  
  145. need_wpa_supplicant()
  146. {
  147.     case "$WIRELESS_AUTH_MODE" in
  148.     wpa-*|WPA-*|psk|PSK|eap|EAP)
  149.         return 0
  150.     esac
  151.     # check whether we have more than one network configured
  152.     get_network_count
  153.     if [ $? -gt 1 ]; then
  154.         return 0
  155.     fi
  156.     return 1
  157. }
  158.  
  159. applicable_wpa_supplicant()
  160. {
  161.     test "$WPA_DRIVER" = "unsupported" && return 1
  162.     case "$WIRELESS_ESSID" in
  163.         any|"") return 1;;
  164.     esac
  165.     case "$WIRELESS_MODE" in
  166.         Managed|managed|"") return 0;;
  167.     esac
  168.     return 1
  169. }
  170.  
  171.  
  172.  
  173. ######################################################################
  174. # using wpa_supplicant for non-WPA connections often makes trouble
  175. # so we do not prefer it if not explicitly set
  176. #
  177. test -z "$PREFER_WPA_SUPPLICANT" && PREFER_WPA_SUPPLICANT=no
  178. info_mesg "prefer wpa_supplicant: $PREFER_WPA_SUPPLICANT"
  179.  
  180. ######################################################################
  181. # check for needed tools
  182. #
  183. for i in iwconfig wlanctl-ng hexdump sed awk ; do
  184. if [ -z "`type -p $i`" ] ; then
  185.     err_mesg "cannot find '$i', please install"
  186.     exit $R_ERROR
  187. fi
  188. done
  189.  
  190. if [ -z "`type -p wpa_supplicant`" ]; then
  191.     if need_wpa_supplicant ; then
  192.         err_mesg "cannot find 'wpa_supplicant', please install"
  193.         exit $R_ERROR
  194.     fi
  195.     PREFER_WPA_SUPPLICANT=no
  196. fi
  197.  
  198. run_iw_tool() {
  199.         local COMMAND MESSAGE
  200.         RETVAL=$R_SUCCESS
  201.         test -z "$3" && return
  202.         info_mesg "run_iw_tool()" "$@"
  203.         case $1 in 
  204.                 config|spy|priv) IWTOOL=iw${1} ;;
  205.                 *) exit $R_INTERNAL ;;
  206.         esac
  207.         shift
  208.         MESSAGE=`$IWTOOL $INTERFACE "$@" 2>&1` || RETVAL=$?
  209.         test -z "$MESSAGE" && return
  210.         err_mesg "command '$IWTOOL $INTERFACE $*' returned\n $MESSAGE"
  211. }
  212.  
  213. RETVAL=$R_SUCCESS
  214.  
  215. wlanctl()
  216. {
  217.         local MESSAGE
  218.         test -z "$1" && return
  219.         info_mesg "running wlanctl-ng $INTERFACE $@"
  220.         MESSAGE=`wlanctl-ng $INTERFACE "$@" 2>&1` || RETVAL=$?
  221.         info_mesg $MESSAGE
  222. }
  223.  
  224. ascii_to_hex()
  225. {
  226.         if [ -z "${1:7:1}" ]; then
  227.                 echo -n ${1#s:*} | hexdump -e '5/1 "%2x"' | sed -e 's/ /0/g'
  228.         elif [ -z "${1:15:1}" ]; then
  229.                 echo -n ${1#s:*} | hexdump -e '13/1 "%2x"' | sed -e 's/ /0/g'
  230.         elif [ -z "${1:18:1}" ]; then
  231.                 echo -n ${1#s:*} | hexdump -e '16/1 "%2x"' | sed -e 's/ /0/g'
  232.         elif [ -z "${1:21:1}" ]; then
  233.                 echo -n ${1#s:*} | hexdump -e '19/1 "%2x"' | sed -e 's/ /0/g'
  234.         elif [ -z "${1:31:1}" ]; then
  235.                 echo -n ${1#s:*} | hexdump -e '29/1 "%2x"' | sed -e 's/ /0/g'
  236.         else
  237.                 err_mesg "Unsupported WEP key length"
  238.                 exit $R_ERROR
  239.         fi
  240. }
  241.  
  242. generate_keys()
  243. {
  244.         test -z "$WIRELESS_KEY_0" && WIRELESS_KEY_0="$WIRELESS_KEY"
  245.         if [ -z "$WIRELESS_KEY_0" -a  \
  246.                  -z "$WIRELESS_KEY_1" -a \
  247.                  -z "$WIRELESS_KEY_2" -a \
  248.                  -z "$WIRELESS_KEY_3" -a \
  249.                  "$WIRELESS_AUTH_MODE" != "psk" -a \
  250.                  "$WIRELESS_AUTH_MODE" != "wpa-psk" ]; then
  251.                 message "`printf "    %-9s warning: using NO encryption" $INTERFACE`"
  252.                 # message "warning: using NO encryption"
  253.                 ENCRYPTION=no
  254.                 return
  255.         fi
  256.         for i in 0 1 2 3 ; do
  257.                 eval K=\$WIRELESS_KEY_$i
  258.                 eval WIRELESS_KEY_$i="$(print_key "$K" $WIRELESS_KEY_LENGTH)"
  259.         done
  260.         ENCRYPTION=yes
  261. }
  262.  
  263. format_key()
  264. {
  265.         # key has usually format XXXX-XXXX-XX [...]
  266.         echo $1 | tr -d '-' | awk '{ KEY=$0 ;
  267.                 if (length()>10) for (i=0;i<26-length();i++) KEY=KEY"0"
  268.                 for (i=1;i<length(KEY)-1;i+=2) FKEY=FKEY substr(KEY, i, 2)":"
  269.                 FKEY=FKEY substr(KEY, i, 2)
  270.                 print FKEY }' 
  271. }
  272.  
  273. setup_iwdev()
  274. {
  275.         # Mode need to be first : some settings apply only in a specific mode !
  276.         run_iw_tool config mode $WIRELESS_MODE
  277.         # This is a bit hackish, but should do the job right...
  278.         if [ -n "$WIRELESS_ESSID" -o -n "$WIRELESS_MODE" ] ; then
  279.                 test -z "$WIRELESS_NICK" && WIRELESS_NICK=`/bin/hostname`
  280.         fi
  281.         # Regular stuff...
  282.         while read OPT ARG; do
  283.                 run_iw_tool config $OPT "$ARG"
  284.         done <<-EOL
  285. nick $WIRELESS_NICK
  286. nwid $WIRELESS_NWID
  287. freq $WIRELESS_FREQ
  288. channel $WIRELESS_CHANNEL
  289. sens $WIRELESS_SENS
  290. rate $WIRELESS_RATE
  291. rts $WIRELESS_RTS
  292. frag $WIRELESS_FRAG
  293. $WIRELESS_IWCONFIG_OPTIONS
  294. EOL
  295.         # set encryption key(s)
  296.         run_iw_tool config key off
  297.         # special hack for madwifi
  298.         test "$HWD_DRIVER" = "ath_pci" && run_iw_tool priv authmode 1
  299.         if [ -z "$WIRELESS_SEC_MODE" ]; then
  300.                 case "$WIRELESS_AUTH_MODE" in
  301.                 shared|sharedkey|restricted)
  302.                         WIRELESS_SEC_MODE="restricted"
  303.                         # special hack for madwifi
  304.                         test "$HWD_DRIVER" = "ath_pci" && run_iw_tool priv authmode 2
  305.                         ;;
  306.                 open|opensystem|"") WIRELESS_SEC_MODE="open";;
  307.                 esac
  308.         fi
  309.         if [ -n "$WIRELESS_KEY_0" -a \
  310.              -z "$WIRELESS_KEY_1" -a \
  311.              -z "$WIRELESS_KEY_2" -a \
  312.              -z "$WIRELESS_KEY_3" ]; then
  313.                 # some drivers (at least madwifi) do not like multiple keys
  314.                 # so we do not use that setting method when we have only one
  315.                 run_iw_tool config key $WIRELESS_SEC_MODE $WIRELESS_KEY_0
  316.         elif [ -n "$WIRELESS_KEY_0" ]; then
  317.                 ARG="key $WIRELESS_SEC_MODE $WIRELESS_KEY_0 [1]"
  318.                 for i in 1 2 3 ; do 
  319.                         eval K=\$WIRELESS_KEY_$i
  320.                         test -n "$K" && ARG="$ARG key $K [$((i+1))]"
  321.                 done
  322.                 run_iw_tool config $ARG
  323.                 if [ $RETVAL -ne 0 ]; then
  324.                         err_mesg "setting encryption key FAILED, aborting interface setup"
  325.                         exit $R_ERROR
  326.                 fi
  327.                 test -z "$WIRELESS_DEFAULT_KEY" && WIRELESS_DEFAULT_KEY=0
  328.                 run_iw_tool config key [$((WIRELESS_DEFAULT_KEY+1))]
  329.         fi
  330.         run_iw_tool spy $WIRELESS_IWSPY_OPTIONS
  331.         run_iw_tool priv $WIRELESS_IWPRIV_OPTIONS
  332.         # ESSID need to be last : most device re-perform the scanning/discovery
  333.         # when this is set, and things like encryption keys are better be
  334.         # defined if we want to discover the right set of APs/nodes.
  335.         test -z "$WIRELESS_ESSID" && WIRELESS_ESSID="any"
  336.         run_iw_tool config essid "$WIRELESS_ESSID"
  337. }
  338.  
  339. setup_wlanngdev()
  340. {
  341.         wlanctl dot11req_reset setdefaultmib=false
  342.         test "$WIRELESS_ESSID" = "any"  && WIRELESS_ESSID=""
  343.         if [ "$ENCRYPTION" = "no" ]; then
  344.                 wlanctl dot11req_mibset mibattribute=dot11PrivacyInvoked=false
  345.                 wlanctl dot11req_mibset mibattribute=dot11ExcludeUnencrypted=false
  346.                 AUTHTYPE=opensystem
  347.         else
  348.                 result=`wlanctl-ng $INTERFACE dot11req_mibget mibattribute=dot11PrivacyOptionImplemented`
  349.                 if [ $? = 0 ] ; then
  350.                         eval $result
  351.                         eval $mibattribute
  352.                 else
  353.                         err_mesg "Could not query device: $result"
  354.                         exit $R_ERROR
  355.                 fi
  356.                 if [ "$dot11PrivacyOptionImplemented" = "false" ]; then
  357.                         err_mesg "Could not set encryption, device does not support it"
  358.                         exit $R_ERROR
  359.                 fi
  360.                 wlanctl dot11req_mibset mibattribute=dot11PrivacyInvoked=true
  361.                 test -z "$WIRELESS_DEFAULT_KEY" && WIRELESS_DEFAULT_KEY=0
  362.                 wlanctl dot11req_mibset mibattribute=dot11WEPDefaultKeyID=$WIRELESS_DEFAULT_KEY
  363.                 for i in 0 1 2 3 ; do
  364.                         eval K=\$WIRELESS_KEY_$i
  365.                         if [ -n "$K" ]; then
  366.                                 wlanctl dot11req_mibset mibattribute=dot11WEPDefaultKey$i=$( format_key $K )
  367.                                 test $RETVAL -ne 0 && 
  368.                                         { err_mesg "Setting encryption key failed, aborting interface setup" ;
  369.                                           exit $R_ERROR ; }
  370.                         fi
  371.                 done
  372.                 case "$WIRELESS_AUTH_MODE" in
  373.                         shared|sharedkey|restricted) 
  374.                         wlanctl dot11req_mibset mibattribute=dot11ExcludeUnencrypted=true
  375.                         AUTHTYPE=sharedkey
  376.                         ;;
  377.                         open|opensystem|"")
  378.                         wlanctl dot11req_mibset mibattribute=dot11ExcludeUnencrypted=false
  379.                         AUTHTYPE=opensystem
  380.                         ;;
  381.                 esac
  382.         fi
  383.         case "$WIRELESS_MODE" in
  384.                 [M,m]anaged)
  385.                 wlanctl lnxreq_autojoin ssid="$WIRELESS_ESSID" authtype=$AUTHTYPE
  386.                 ;;
  387.                 [A,a]d-hoc)
  388.                 test -z "$WIRELESS_ESSID" && WIRELESS_ESSID="linux"
  389.                 test -z "$WIRLEESS_CHANNEL" && WIRELESS_CHANNEL=7
  390.                 wlanctl dot11req_start \
  391.                                 ssid=$WIRELESS_ESSID \
  392.                                 bsstype=independent \
  393.                                 beaconperiod=100 \
  394.                                 dtimperiod=3 \
  395.                                 cfpollable=false \
  396.                                 cfpollreq=false \
  397.                                 cfpperiod=3 \
  398.                                 cfpmaxduration=100 \
  399.                                 probedelay=100 \
  400.                                 dschannel=$WIRELESS_CHANNEL \
  401.                                 basicrate1=2 \
  402.                                 basicrate2=4 \
  403.                                 operationalrate1=2 \
  404.                                 operationalrate2=4 \
  405.                                 operationalrate3=11 \
  406.                                 operationalrate4=22
  407.                 ;;
  408.                 [M,m]aster|[R,r]epeater|[S,s]econdary)
  409.                 err_mesg "Mode $WIRELESS_MODE is not supported yet for wlanng devices"
  410.                 exit $R_ERROR
  411.                 ;;
  412.         esac
  413. }
  414.  
  415. old_setup()
  416. {
  417.     info_mesg "warning: debug mode logs your encryption keys!"
  418.     generate_keys
  419.     if [ -z "$WIRELESS_MODE" ]; then
  420.         info_mesg "WIRELESS_MODE is unset, using Managed"
  421.         WIRELESS_MODE="Managed"
  422.     fi
  423.     # some drivers seem to have extensions like wlan-ng,
  424.     # although they do not
  425.     case "$HWD_DRIVER" in rtl8180|ipw3945|zd12*)
  426.         setup_iwdev
  427.         return
  428.         ;;
  429.     esac
  430.     # probe for (and enable) wlan-ng devices
  431.     wlanctl-ng $INTERFACE lnxreq_ifstate ifstate=enable >/dev/null 2>&1
  432.     if [ $? -eq 0 ]; then
  433.         setup_wlanngdev
  434.     else
  435.         setup_iwdev
  436.     fi
  437. }
  438.  
  439. print_key()
  440. {
  441.     test -z "$1" && return
  442.     K="$1"
  443.     L="$2"
  444.     test -z "$2" && L=104
  445.     if [ -n "$K" ]; then
  446.         if [ ${K:0:1} = "s" ]; then
  447.         # we are using ascii key representation (iwconfig method)
  448.         echo "$(ascii_to_hex "$K")"
  449.         elif [ ${K:0:1} = "h" ]; then
  450.             case $L in
  451.             40|64)
  452.                 echo "$(lwepgen "${K:2}" | head -n 1 | tr -d ':')"
  453.                 ;;
  454.             104|128)
  455.                 echo "$(lwepgen -s "${K:2}" | head -n 1 | tr -d ':')"
  456.                 ;;
  457.             *)
  458.                 err_mesg "Unsupported key length $L in hash mode"
  459.                 exit $R_ERROR
  460.                 ;;
  461.             esac
  462.         else
  463.             echo "${K//[:-]/}"
  464.         fi
  465.     fi
  466. }
  467.  
  468.  
  469. ######################################################################
  470. # helper to print a line of the wpa_supplicant.conf network block
  471. # format print_network_line tag value suffix [default_value]
  472. print_network_line()
  473. {
  474.     TAG="$1"
  475.     eval VALUE="\$$2$3"
  476.     test -z "$VALUE" && VALUE="$4"
  477.     test -n "$VALUE" && echo "  $TAG=\"$VALUE\""
  478. }
  479.  
  480. print_network_line_unquoted()
  481. {
  482.     TAG="$1"
  483.     eval VALUE="\$$2$3"
  484.     test -z "$VALUE" && VALUE="$4"
  485.     test -n "$VALUE" && echo "  $TAG=$VALUE"
  486. }
  487.  
  488. print_network_block()
  489. {
  490.     SUFFIX="_$1"
  491.     # special case for index 0 (may go away in the future)
  492.     test "$1" = "0"  && SUFFIX=""
  493.     echo "network={"
  494.     if [ "`eval echo \\$WIRELESS_HIDDEN_SSID$SUFFIX`" = "no" ]; then
  495.         echo "  scan_ssid=0"
  496.     else
  497.         echo "  scan_ssid=1"
  498.     fi
  499.     print_network_line ssid WIRELESS_ESSID "$SUFFIX"
  500.     print_network_line_unquoted priority WIRELESS_PRIORITY "$SUFFIX"
  501.     case "`eval echo \\$WIRELESS_AUTH_MODE$SUFFIX`" in
  502.     open)
  503.         echo "  key_mgmt=NONE"
  504.         for i in 0 1 2 3 ; do
  505.             KEY="`eval print_key \\"\\$WIRELESS_KEY_$i$SUFFIX\\" \\$WIRELESS_KEY_LENGTH$SUFFIX`"
  506.             test -n "$KEY" && echo "  wep_key$i=$KEY"
  507.         done
  508.         print_network_line_unquoted wep_tx_keyidx WIRELESS_DEFAULT_KEY "$SUFFIX"
  509.         ;;
  510.     shared|sharedkey)
  511.         echo "  key_mgmt=NONE"
  512.         for i in 0 1 2 3 ; do
  513.             KEY="`eval print_key \\"\\$WIRELESS_KEY_$i$SUFFIX\\" \\$WIRELESS_KEY_LENGTH$SUFFIX`"
  514.             test -n "$KEY" && echo "  wep_key$i=$KEY"
  515.         done
  516.         print_network_line_unquoted wep_tx_keyidx WIRELESS_DEFAULT_KEY "$SUFFIX"
  517.         echo "  auth_alg=SHARED"
  518.         ;;
  519.     *psk|*PSK)
  520.         echo "  key_mgmt=WPA-PSK"
  521.         eval L=\$WIRELESS_WPA_PSK$SUFFIX
  522.         if [ ${#L} = 64 ]; then
  523.             echo "  psk=$L"
  524.         else
  525.             echo "  psk=\"$L\""
  526.         fi
  527.         if [ "$WIRELESS_AP_SCANMODE" = "2" ]; then
  528.             print_network_line_unquoted proto WIRELESS_WPA_PROTO "$SUFFIX" WPA
  529.             print_network_line_unquoted pairwise WIRELESS_CIPHER_PAIRWISE "$SUFFIX" TKIP
  530.             print_network_line_unquoted group WIRELESS_CIPHER_GROUP "$SUFFIX" TKIP
  531.         else
  532.             print_network_line_unquoted proto WIRELESS_WPA_PROTO "$SUFFIX"
  533.             print_network_line_unquoted pairwise WIRELESS_CIPHER_PAIRWISE "$SUFFIX"
  534.             print_network_line_unquoted group WIRELESS_CIPHER_GROUP "$SUFFIX"
  535.         fi
  536.         ;;
  537.      eap|EAP|wpa-eap|WPA-EAP)
  538.         echo "  key_mgmt=WPA-EAP"
  539.         eval EAP_MODE=\$WIRELESS_EAP_MODE$SUFFIX
  540.         if [ -n "$EAP_MODE" ]; then
  541.         print_network_line_unquoted eap WIRELESS_EAP_MODE "$SUFFIX"
  542.         else
  543.         echo "  eap=TTLS PEAP TLS"
  544.     fi
  545.         if [ "$WIRELESS_AP_SCANMODE" = "2" ]; then
  546.             print_network_line_unquoted proto WIRELESS_WPA_PROTO "$SUFFIX" WPA
  547.             print_network_line_unquoted pairwise WIRELESS_CIPHER_PAIRWISE "$SUFFIX" TKIP
  548.             print_network_line_unquoted group WIRELESS_CIPHER_GROUP "$SUFFIX" TKIP
  549.         else
  550.             print_network_line_unquoted proto WIRELESS_WPA_PROTO "$SUFFIX"
  551.             print_network_line_unquoted pairwise WIRELESS_CIPHER_PAIRWISE "$SUFFIX"
  552.             print_network_line_unquoted group WIRELESS_CIPHER_GROUP "$SUFFIX"
  553.         fi
  554.         print_network_line identity WIRELESS_WPA_IDENTITY "$SUFFIX"
  555.         print_network_line password WIRELESS_WPA_PASSWORD "$SUFFIX"
  556.         print_network_line anonymous_identity WIRELESS_WPA_ANONID "$SUFFIX"
  557.         print_network_line ca_cert WIRELESS_CA_CERT "$SUFFIX"
  558.         print_network_line client_cert WIRELESS_CLIENT_CERT "$SUFFIX"
  559.         print_network_line private_key WIRELESS_CLIENT_KEY "$SUFFIX"
  560.         print_network_line private_key_passwd WIRELESS_CLIENT_KEY_PASSWORD "$SUFFIX"
  561.         eval PEAPVER=\$WIRELESS_PEAP_VERSION$SUFFIX
  562.         if [ -n "$PEAPVER" ]; then
  563.         echo "  phase1=\"peaplabel=0 peapver=$PEAPVER\""
  564.         else
  565.         echo "  phase1=\"peaplabel=0\""
  566.     fi
  567.     
  568.         eval EAP_AUTH=\$WIRELESS_EAP_AUTH$SUFFIX
  569.         if [ -n "$EAP_AUTH" ]; then
  570.         echo "  phase2=\"auth=$EAP_AUTH\""
  571.     fi
  572.         ;;
  573.     esac        
  574.     echo "}"
  575. }
  576.     
  577. print_wpa_conf()
  578. {
  579.     # header
  580.     echo "ctrl_interface=/var/run/wpa_supplicant"
  581.     test -n "$WIRELESS_AP_SCANMODE" && echo "ap_scan=$WIRELESS_AP_SCANMODE"
  582.  
  583.     get_first_index
  584.     local index=$?
  585.  
  586.     while [ $index -ne 255 ]; do
  587.         print_network_block $index
  588.         get_next_index $index
  589.         index=$?
  590.     done
  591. }
  592.  
  593. start_wpa_supplicant()
  594. {
  595.     if [ -e /var/run/wpa_supplicant-$INTERFACE.conf ]; then
  596.         if [ -f /var/run/wpa_supplicant/${INTERFACE}.pid ]; then
  597.             pid=$( cat /var/run/wpa_supplicant/${INTERFACE}.pid)
  598.         else
  599.             pid=$(pgrep -f ".*wpa_supplicant-$INTERFACE.conf.*")
  600.         fi
  601.         wpa_dead=y
  602.         if [ -n "$pid" ]; then
  603.             kill -0 $pid && wpa_dead=n
  604.         fi
  605.         case "$wpa_dead" in
  606.             y)
  607.                 info_mesg "Stale wpa_supplicant-$INTERFACE.conf found, removing"
  608.                 rm -f /var/run/wpa_supplicant-$INTERFACE.conf
  609.                 ;;
  610.             n)
  611.                 message "`printf "    %-9s warning: wpa_supplicant already running on interface" $INTERFACE`"
  612.                 return
  613.                 ;;
  614.         esac
  615.     fi
  616.     if [ -n "$WIRELESS_WPA_CONF" ]; then
  617.         if [ -e "$WIRELESS_WPA_CONF" ]; then
  618.             WPA_SUPP_CONF="$WIRELESS_WPA_CONF"
  619.         else
  620.             err_mesg "Unable to setup wpa (could not open $WIRELESS_WPA_CONF)"
  621.             exit $R_ERROR
  622.         fi
  623.     else
  624.         WPA_SUPP_CONF="/var/run/wpa_supplicant-$INTERFACE.conf"
  625.         umask 0077
  626.         print_wpa_conf > $WPA_SUPP_CONF
  627.     fi
  628.     if [ "$DEBUG" = "yes" ]; then
  629.         SUPPARGS="-P/var/run/wpa_supplicant/${INTERFACE}.pid -d 2>&1 | logger &"
  630.     else
  631.         SUPPARGS="-P/var/run/wpa_supplicant/${INTERFACE}.pid -B"
  632.     fi
  633.     mkdir -p /var/run/wpa_supplicant
  634.     message "`printf "    %-9s starting wpa_supplicant" $INTERFACE`"
  635.     eval wpa_supplicant -i$INTERFACE -c$WPA_SUPP_CONF -D$WPA_DRIVER $SUPPARGS
  636. }
  637.  
  638. kill_wpa_supplicant()
  639. {
  640.     if [ -f /var/run/wpa_supplicant/${INTERFACE}.pid ]; then
  641.         pid=$( cat /var/run/wpa_supplicant/${INTERFACE}.pid)
  642.     else
  643.         pid=$(pgrep -f ".*wpa_supplicant-$INTERFACE.conf.*")
  644.     fi
  645.     if [ -z "$pid" ]; then
  646.         info_mesg "No wpa_supplicant running on interface $INTERFACE"
  647.         return
  648.     fi
  649.     kill $pid
  650.     rm -f /var/run/wpa_supplicant-$INTERFACE.conf
  651. }
  652.  
  653. show_status()
  654. {
  655.     if [ -f /var/run/wpa_supplicant/${INTERFACE}.pid ]; then
  656.         wpa_cli -i$INTERFACE status
  657.     else
  658.         iwconfig $INTERFACE
  659.     fi
  660. }
  661.  
  662. case $ACTION in
  663.     up)
  664.         case "${HWD_DRIVER}" in
  665.             ath_pci)
  666.                 WPA_DRIVER=wext
  667.                 ;;
  668.             at76c5*)
  669.                 WPA_DRIVER=atmel
  670.                 PREFER_WPA_SUPPLICANT=no
  671.                 ;;
  672.             prism54)
  673.                 WPA_DRIVER=prism54
  674.                 PREFER_WPA_SUPPLICANT=no
  675.                 ;;
  676.             ipw2200|ipw3945|hostap_*)
  677.                 WPA_DRIVER=wext
  678.                 ;;
  679.             ipw2100)
  680.                 WPA_DRIVER=wext
  681.                 ;;
  682.             ndiswrapper|*.sys)
  683.                 WPA_DRIVER=wext
  684.                 ;;
  685.             *)
  686.                 WPA_DRIVER=unsupported
  687.                 PREFER_WPA_SUPPLICANT=no
  688.                 ;;
  689.         esac
  690.         if need_wpa_supplicant ; then
  691.             info_mesg "configuration requires wpa_supplicant"
  692.             if [ "$WPA_DRIVER" = "unsupported" ]; then
  693.                 message "`printf "    %-9s warning: WPA configured but may be unsupported" $INTERFACE`"
  694.                 message "`printf "    %-9s warning: by this device" $INTERFACE`"
  695.                 info_mesg "using WPA driver 'wext' for interface $INTERFACE"
  696.                 WPA_DRIVER=wext
  697.             fi
  698.             start_wpa_supplicant
  699.         elif [ "$PREFER_WPA_SUPPLICANT" = "yes" ]; then
  700.             if applicable_wpa_supplicant ; then
  701.                 start_wpa_supplicant
  702.             else
  703.                 info_mesg "setting up $INTERFACE w/o wpa_supplicant"
  704.                 old_setup
  705.             fi
  706.         else
  707.             old_setup
  708.         fi
  709.         ;;
  710.     down)
  711.         kill_wpa_supplicant
  712.         ;;
  713.     status)
  714.         if is_iface_up $INTERFACE ; then
  715.             # do not show status output on boot, this may log
  716.             # the WEP key in the boot log
  717.             test "$MODE" = "onboot" || show_status
  718.             RETVAL=$R_SUCCESS
  719.         else
  720.             RETVAL=$R_NOTRUNNING
  721.         fi
  722.         ;;
  723. esac
  724.  
  725. exit $RETVAL
  726.