wpa_supplicant.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. wpa_supplicant_setup_vif() {
  2. local vif="$1"
  3. local driver="$2"
  4. local key="$key"
  5. local options="$3"
  6. local freq=""
  7. local ht="$5"
  8. local ap_scan=""
  9. local scan_ssid="1"
  10. [ -n "$4" ] && freq="frequency=$4"
  11. config_get enc "$vif" encryption
  12. config_get key "$vif" key
  13. local net_cfg bridge
  14. config_get bridge "$vif" bridge
  15. [ -z "$bridge" ] && {
  16. net_cfg="$(find_net_config "$vif")"
  17. [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
  18. config_set "$vif" bridge "$bridge"
  19. }
  20. local mode ifname wds modestr=""
  21. config_get mode "$vif" mode
  22. config_get ifname "$vif" ifname
  23. config_get_bool wds "$vif" wds 0
  24. [ -z "$bridge" ] || [ "$mode" = ap ] || [ "$mode" = sta -a $wds -eq 1 ] || {
  25. echo "wpa_supplicant_setup_vif($ifname): Refusing to bridge $mode mode interface"
  26. return 1
  27. }
  28. [ "$mode" = "adhoc" ] && {
  29. modestr="mode=1"
  30. scan_ssid="0"
  31. ap_scan="ap_scan=2"
  32. }
  33. key_mgmt='NONE'
  34. case "$enc" in
  35. *none*) ;;
  36. *wep*)
  37. config_get key "$vif" key
  38. key="${key:-1}"
  39. case "$key" in
  40. [1234])
  41. for idx in 1 2 3 4; do
  42. local zidx
  43. zidx=$(($idx - 1))
  44. config_get ckey "$vif" "key${idx}"
  45. [ -n "$ckey" ] && \
  46. append "wep_key${zidx}" "wep_key${zidx}=$(prepare_key_wep "$ckey")"
  47. done
  48. wep_tx_keyidx="wep_tx_keyidx=$((key - 1))"
  49. ;;
  50. *)
  51. wep_key0="wep_key0=$(prepare_key_wep "$key")"
  52. wep_tx_keyidx="wep_tx_keyidx=0"
  53. ;;
  54. esac
  55. ;;
  56. *psk*)
  57. key_mgmt='WPA-PSK'
  58. # if you want to use PSK with a non-nl80211 driver you
  59. # have to use WPA-NONE and wext driver for wpa_s
  60. [ "$mode" = "adhoc" -a "$driver" != "nl80211" ] && {
  61. key_mgmt='WPA-NONE'
  62. driver='wext'
  63. }
  64. if [ ${#key} -eq 64 ]; then
  65. passphrase="psk=${key}"
  66. else
  67. passphrase="psk=\"${key}\""
  68. fi
  69. case "$enc" in
  70. *psk2*)
  71. proto='proto=RSN'
  72. config_get ieee80211w "$vif" ieee80211w
  73. ;;
  74. *psk*)
  75. proto='proto=WPA'
  76. ;;
  77. esac
  78. ;;
  79. *wpa*|*8021x*)
  80. proto='proto=WPA2'
  81. key_mgmt='WPA-EAP'
  82. config_get ieee80211w "$vif" ieee80211w
  83. config_get ca_cert "$vif" ca_cert
  84. config_get eap_type "$vif" eap_type
  85. ca_cert=${ca_cert:+"ca_cert=\"$ca_cert\""}
  86. case "$eap_type" in
  87. tls)
  88. pairwise='pairwise=CCMP'
  89. group='group=CCMP'
  90. config_get identity "$vif" identity
  91. config_get client_cert "$vif" client_cert
  92. config_get priv_key "$vif" priv_key
  93. config_get priv_key_pwd "$vif" priv_key_pwd
  94. identity="identity=\"$identity\""
  95. client_cert="client_cert=\"$client_cert\""
  96. priv_key="private_key=\"$priv_key\""
  97. priv_key_pwd="private_key_passwd=\"$priv_key_pwd\""
  98. ;;
  99. peap|ttls)
  100. config_get auth "$vif" auth
  101. config_get identity "$vif" identity
  102. config_get password "$vif" password
  103. phase2="phase2=\"auth=${auth:-MSCHAPV2}\""
  104. identity="identity=\"$identity\""
  105. password="${password:+password=\"$password\"}"
  106. ;;
  107. esac
  108. eap_type="eap=$(echo $eap_type | tr 'a-z' 'A-Z')"
  109. ;;
  110. esac
  111. case "$ieee80211w" in
  112. [012])
  113. ieee80211w="ieee80211w=$ieee80211w"
  114. ;;
  115. esac
  116. local fixed_freq bssid1 beacon_int brates mrate
  117. config_get ifname "$vif" ifname
  118. config_get bridge "$vif" bridge
  119. config_get ssid "$vif" ssid
  120. config_get bssid "$vif" bssid
  121. bssid1=${bssid:+"bssid=$bssid"}
  122. beacon_int=${beacon_int:+"beacon_int=$beacon_int"}
  123. local br brval brsub brstr
  124. [ -n "$basic_rate_list" ] && {
  125. for br in $basic_rate_list; do
  126. brval="$(($br / 1000))"
  127. brsub="$((($br / 100) % 10))"
  128. [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
  129. [ -n "$brstr" ] && brstr="$brstr,"
  130. brstr="$brstr$brval"
  131. done
  132. brates=${basic_rate_list:+"rates=$brstr"}
  133. }
  134. local mcval=""
  135. [ -n "$mcast_rate" ] && {
  136. mcval="$(($mcast_rate / 1000))"
  137. mcsub="$(( ($mcast_rate / 100) % 10 ))"
  138. [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
  139. mrate=${mcast_rate:+"mcast_rate=$mcval"}
  140. }
  141. local ht_str
  142. [ -n "$ht" ] && ht_str="htmode=$ht"
  143. rm -rf /var/run/wpa_supplicant-$ifname
  144. cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
  145. ctrl_interface=/var/run/wpa_supplicant-$ifname
  146. $ap_scan
  147. network={
  148. $modestr
  149. scan_ssid=$scan_ssid
  150. ssid="$ssid"
  151. $bssid1
  152. key_mgmt=$key_mgmt
  153. $proto
  154. $freq
  155. ${fixed:+"fixed_freq=1"}
  156. $beacon_int
  157. $brates
  158. $mrate
  159. $ht_str
  160. $ieee80211w
  161. $passphrase
  162. $pairwise
  163. $group
  164. $eap_type
  165. $ca_cert
  166. $client_cert
  167. $priv_key
  168. $priv_key_pwd
  169. $phase2
  170. $identity
  171. $password
  172. $wep_key0
  173. $wep_key1
  174. $wep_key2
  175. $wep_key3
  176. $wep_tx_keyidx
  177. }
  178. EOF
  179. if [ -n "$proto" -o "$key_mgmt" = "NONE" ]; then
  180. wpa_supplicant ${bridge:+ -b $bridge} -B -P "/var/run/wifi-${ifname}.pid" -D ${driver:-wext} -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf $options
  181. else
  182. return 0
  183. fi
  184. }