hostapd.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. hostapd_set_bss_options() {
  2. local var="$1"
  3. local vif="$2"
  4. local enc wpa_group_rekey wps_possible
  5. config_get enc "$vif" encryption
  6. config_get wpa_group_rekey "$vif" wpa_group_rekey
  7. config_get_bool ap_isolate "$vif" isolate 0
  8. config_get device "$vif" device
  9. config_get hwmode "$device" hwmode
  10. config_get phy "$device" phy
  11. append "$var" "ctrl_interface=/var/run/hostapd-$phy" "$N"
  12. if [ "$ap_isolate" -gt 0 ]; then
  13. append "$var" "ap_isolate=$ap_isolate" "$N"
  14. fi
  15. # Examples:
  16. # psk-mixed/tkip => WPA1+2 PSK, TKIP
  17. # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
  18. # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
  19. # ...
  20. # TODO: move this parsing function somewhere generic, so that
  21. # later it can be reused by drivers that don't use hostapd
  22. # crypto defaults: WPA2 vs WPA1
  23. case "$enc" in
  24. wpa2*|*psk2*)
  25. wpa=2
  26. crypto="CCMP"
  27. ;;
  28. *mixed*)
  29. wpa=3
  30. crypto="CCMP TKIP"
  31. ;;
  32. *)
  33. wpa=1
  34. crypto="TKIP"
  35. ;;
  36. esac
  37. # explicit override for crypto setting
  38. case "$enc" in
  39. *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) crypto="CCMP TKIP";;
  40. *aes|*ccmp) crypto="CCMP";;
  41. *tkip) crypto="TKIP";;
  42. esac
  43. # enforce CCMP for 11ng and 11na
  44. case "$hwmode" in
  45. *ng|*na) crypto="CCMP";;
  46. esac
  47. # use crypto/auth settings for building the hostapd config
  48. case "$enc" in
  49. *psk*)
  50. config_get psk "$vif" key
  51. if [ ${#psk} -eq 64 ]; then
  52. append "$var" "wpa_psk=$psk" "$N"
  53. else
  54. append "$var" "wpa_passphrase=$psk" "$N"
  55. fi
  56. wps_possible=1
  57. ;;
  58. *wpa*)
  59. # required fields? formats?
  60. # hostapd is particular, maybe a default configuration for failures
  61. config_get server "$vif" server
  62. append "$var" "auth_server_addr=$server" "$N"
  63. config_get port "$vif" port
  64. port=${port:-1812}
  65. append "$var" "auth_server_port=$port" "$N"
  66. config_get secret "$vif" key
  67. append "$var" "auth_server_shared_secret=$secret" "$N"
  68. config_get nasid "$vif" nasid
  69. append "$var" "nas_identifier=$nasid" "$N"
  70. append "$var" "eapol_key_index_workaround=1" "$N"
  71. append "$var" "radius_acct_interim_interval=300" "$N"
  72. append "$var" "ieee8021x=1" "$N"
  73. append "$var" "wpa_key_mgmt=WPA-EAP" "$N"
  74. append "$var" "wpa_group_rekey=300" "$N"
  75. append "$var" "wpa_gmk_rekey=640" "$N"
  76. ;;
  77. *wep*)
  78. config_get key "$vif" key
  79. key="${key:-1}"
  80. case "$key" in
  81. [1234])
  82. for idx in 1 2 3 4; do
  83. local zidx
  84. zidx=$(($idx - 1))
  85. config_get ckey "$vif" "key${idx}"
  86. [ -n "$ckey" ] && \
  87. append "$var" "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N"
  88. done
  89. append "$var" "wep_default_key=$((key - 1))" "$N"
  90. ;;
  91. *)
  92. append "$var" "wep_key0=$(prepare_key_wep "$key")" "$N"
  93. append "$var" "wep_default_key=0" "$N"
  94. ;;
  95. esac
  96. case "$enc" in
  97. *shared*)
  98. auth_algs=2
  99. ;;
  100. *mixed*)
  101. auth_algs=3
  102. ;;
  103. esac
  104. wpa=0
  105. crypto=
  106. ;;
  107. *)
  108. wpa=0
  109. crypto=
  110. ;;
  111. esac
  112. append "$var" "auth_algs=${auth_algs:-1}" "$N"
  113. append "$var" "wpa=$wpa" "$N"
  114. [ -n "$crypto" ] && append "$var" "wpa_pairwise=$crypto" "$N"
  115. [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
  116. config_get ssid "$vif" ssid
  117. config_get bridge "$vif" bridge
  118. config_get ieee80211d "$vif" ieee80211d
  119. config_get iapp_interface "$vif" iapp_interface
  120. config_get_bool wps_pbc "$vif" wps_pushbutton 0
  121. [ -n "$wps_possible" -a "$wps_pbc" -gt 0 ] && {
  122. append "$var" "eap_server=1" "$N"
  123. append "$var" "wps_state=2" "$N"
  124. append "$var" "ap_setup_locked=1" "$N"
  125. append "$var" "config_methods=push_button" "$N"
  126. }
  127. append "$var" "ssid=$ssid" "$N"
  128. [ -n "$bridge" ] && append "$var" "bridge=$bridge" "$N"
  129. [ -n "$ieee80211d" ] && append "$var" "ieee80211d=$ieee80211d" "$N"
  130. [ -n "$iapp_interface" ] && append "$var" iapp_interface=$(uci_get_state network "$iapp_interface" ifname "$iapp_interface") "$N"
  131. if [ "$wpa" -ge "2" ]
  132. then
  133. # RSN -> allow preauthentication
  134. config_get rsn_preauth "$vif" rsn_preauth
  135. if [ -n "$bridge" -a "$rsn_preauth" = 1 ]
  136. then
  137. append "$var" "rsn_preauth=1" "$N"
  138. append "$var" "rsn_preauth_interfaces=$bridge" "$N"
  139. fi
  140. # RSN -> allow management frame protection
  141. config_get ieee80211w "$vif" ieee80211w
  142. case "$ieee80211w" in
  143. [012])
  144. append "$var" "ieee80211w=$ieee80211w" "$N"
  145. [ "$ieee80211w" -gt "0" ] && {
  146. config_get ieee80211w_max_timeout "$vif" ieee80211w_max_timeout
  147. config_get ieee80211w_retry_timeout "$vif" ieee80211w_retry_timeout
  148. [ -n "$ieee80211w_max_timeout" ] && \
  149. append "$var" "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
  150. [ -n "$ieee80211w_retry_timeout" ] && \
  151. append "$var" "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
  152. }
  153. ;;
  154. esac
  155. fi
  156. }
  157. hostapd_setup_vif() {
  158. local vif="$1"
  159. local driver="$2"
  160. hostapd_cfg=
  161. hostapd_set_bss_options hostapd_cfg "$vif"
  162. config_get ifname "$vif" ifname
  163. config_get device "$vif" device
  164. config_get channel "$device" channel
  165. config_get hwmode "$device" hwmode
  166. case "$hwmode" in
  167. *bg|*gdt|*gst|*fh) hwmode=g;;
  168. *adt|*ast) hwmode=a;;
  169. esac
  170. [ "$channel" = auto ] && channel=
  171. [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
  172. cat > /var/run/hostapd-$ifname.conf <<EOF
  173. driver=$driver
  174. interface=$ifname
  175. ${hwmode:+hw_mode=${hwmode#11}}
  176. ${channel:+channel=$channel}
  177. $hostapd_cfg
  178. EOF
  179. hostapd -P /var/run/wifi-$ifname.pid -B /var/run/hostapd-$ifname.conf
  180. }