hostapd.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. hostapd_set_bss_options() {
  2. local var="$1"
  3. local vif="$2"
  4. local enc wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wps_possible wpa_key_mgmt
  5. config_get enc "$vif" encryption "none"
  6. config_get wep_rekey "$vif" wep_rekey # 300
  7. config_get wpa_group_rekey "$vif" wpa_group_rekey # 300
  8. config_get wpa_pair_rekey "$vif" wpa_pair_rekey # 300
  9. config_get wpa_master_rekey "$vif" wpa_master_rekey # 640
  10. config_get_bool ap_isolate "$vif" isolate 0
  11. config_get_bool disassoc_low_ack "$vif" disassoc_low_ack 1
  12. config_get max_num_sta "$vif" max_num_sta 0
  13. config_get max_inactivity "$vif" max_inactivity 0
  14. config_get_bool preamble "$vif" short_preamble 1
  15. config_get device "$vif" device
  16. config_get hwmode "$device" hwmode
  17. config_get phy "$device" phy
  18. append "$var" "ctrl_interface=/var/run/hostapd-$phy" "$N"
  19. if [ "$ap_isolate" -gt 0 ]; then
  20. append "$var" "ap_isolate=$ap_isolate" "$N"
  21. fi
  22. if [ "$max_num_sta" -gt 0 ]; then
  23. append "$var" "max_num_sta=$max_num_sta" "$N"
  24. fi
  25. if [ "$max_inactivity" -gt 0 ]; then
  26. append "$var" "ap_max_inactivity=$max_inactivity" "$N"
  27. fi
  28. append "$var" "disassoc_low_ack=$disassoc_low_ack" "$N"
  29. if [ "$preamble" -gt 0 ]; then
  30. append "$var" "preamble=$preamble" "$N"
  31. fi
  32. # Examples:
  33. # psk-mixed/tkip => WPA1+2 PSK, TKIP
  34. # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
  35. # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
  36. # ...
  37. # TODO: move this parsing function somewhere generic, so that
  38. # later it can be reused by drivers that don't use hostapd
  39. # crypto defaults: WPA2 vs WPA1
  40. case "$enc" in
  41. wpa2*|*psk2*)
  42. wpa=2
  43. crypto="CCMP"
  44. ;;
  45. *mixed*)
  46. wpa=3
  47. crypto="CCMP TKIP"
  48. ;;
  49. *)
  50. wpa=1
  51. crypto="TKIP"
  52. ;;
  53. esac
  54. # explicit override for crypto setting
  55. case "$enc" in
  56. *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) crypto="CCMP TKIP";;
  57. *aes|*ccmp) crypto="CCMP";;
  58. *tkip) crypto="TKIP";;
  59. esac
  60. # enforce CCMP for 11ng and 11na
  61. case "$hwmode:$crypto" in
  62. *ng:TKIP|*na:TKIP) crypto="CCMP TKIP";;
  63. esac
  64. # use crypto/auth settings for building the hostapd config
  65. case "$enc" in
  66. none)
  67. wps_possible=1
  68. wpa=0
  69. crypto=
  70. # Here we make the assumption that if we're in open mode
  71. # with WPS enabled, we got to be in unconfigured state.
  72. wps_not_configured=1
  73. ;;
  74. *psk*)
  75. config_get psk "$vif" key
  76. if [ ${#psk} -eq 64 ]; then
  77. append "$var" "wpa_psk=$psk" "$N"
  78. else
  79. append "$var" "wpa_passphrase=$psk" "$N"
  80. fi
  81. wps_possible=1
  82. [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
  83. [ -n "$wpa_pair_rekey" ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
  84. [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey" "$N"
  85. append wpa_key_mgmt "WPA-PSK"
  86. ;;
  87. *wpa*|*8021x*)
  88. # required fields? formats?
  89. # hostapd is particular, maybe a default configuration for failures
  90. config_get auth_server "$vif" auth_server
  91. [ -z "$auth_server" ] && config_get auth_server "$vif" server
  92. append "$var" "auth_server_addr=$auth_server" "$N"
  93. config_get auth_port "$vif" auth_port
  94. [ -z "$auth_port" ] && config_get auth_port "$vif" port
  95. auth_port=${auth_port:-1812}
  96. append "$var" "auth_server_port=$auth_port" "$N"
  97. config_get auth_secret "$vif" auth_secret
  98. [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
  99. append "$var" "auth_server_shared_secret=$auth_secret" "$N"
  100. # You don't really want to enable this unless you are doing
  101. # some corner case testing or are using OpenWrt as a work around
  102. # for some systematic issues.
  103. config_get_bool auth_cache "$vif" auth_cache 0
  104. config_get rsn_preauth "$vif" rsn_preauth
  105. [ "$auth_cache" -gt 0 ] || [[ "$rsn_preauth" = 1 ]] || append "$var" "disable_pmksa_caching=1" "$N"
  106. [ "$auth_cache" -gt 0 ] || [[ "$rsn_preauth" = 1 ]] || append "$var" "okc=0" "$N"
  107. config_get acct_server "$vif" acct_server
  108. [ -n "$acct_server" ] && append "$var" "acct_server_addr=$acct_server" "$N"
  109. config_get acct_port "$vif" acct_port
  110. [ -n "$acct_port" ] && acct_port=${acct_port:-1813}
  111. [ -n "$acct_port" ] && append "$var" "acct_server_port=$acct_port" "$N"
  112. config_get acct_secret "$vif" acct_secret
  113. [ -n "$acct_secret" ] && append "$var" "acct_server_shared_secret=$acct_secret" "$N"
  114. config_get eap_reauth_period "$vif" eap_reauth_period
  115. [ -n "$eap_reauth_period" ] && append "$var" "eap_reauth_period=$eap_reauth_period" "$N"
  116. config_get dae_client "$vif" dae_client
  117. config_get dae_secret "$vif" dae_secret
  118. [ -n "$dae_client" -a -n "$dae_secret" ] && {
  119. config_get dae_port "$vif" dae_port
  120. append "$var" "radius_das_port=${dae_port:-3799}" "$N"
  121. append "$var" "radius_das_client=$dae_client $dae_secret" "$N"
  122. }
  123. config_get ownip "$vif" ownip
  124. append "$var" "own_ip_addr=$ownip" "$N"
  125. append "$var" "eapol_key_index_workaround=1" "$N"
  126. append "$var" "ieee8021x=1" "$N"
  127. append wpa_key_mgmt "WPA-EAP"
  128. [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
  129. [ -n "$wpa_pair_rekey" ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
  130. [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey" "$N"
  131. ;;
  132. *wep*)
  133. config_get key "$vif" key
  134. key="${key:-1}"
  135. case "$key" in
  136. [1234])
  137. for idx in 1 2 3 4; do
  138. local zidx
  139. zidx=$(($idx - 1))
  140. config_get ckey "$vif" "key${idx}"
  141. [ -n "$ckey" ] && \
  142. append "$var" "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N"
  143. done
  144. append "$var" "wep_default_key=$((key - 1))" "$N"
  145. ;;
  146. *)
  147. append "$var" "wep_key0=$(prepare_key_wep "$key")" "$N"
  148. append "$var" "wep_default_key=0" "$N"
  149. [ -n "$wep_rekey" ] && append "$var" "wep_rekey_period=$wep_rekey" "$N"
  150. ;;
  151. esac
  152. case "$enc" in
  153. *shared*)
  154. auth_algs=2
  155. ;;
  156. *mixed*)
  157. auth_algs=3
  158. ;;
  159. esac
  160. wpa=0
  161. crypto=
  162. ;;
  163. *)
  164. wpa=0
  165. crypto=
  166. ;;
  167. esac
  168. append "$var" "auth_algs=${auth_algs:-1}" "$N"
  169. append "$var" "wpa=$wpa" "$N"
  170. [ -n "$crypto" ] && append "$var" "wpa_pairwise=$crypto" "$N"
  171. [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
  172. config_get ssid "$vif" ssid
  173. config_get bridge "$vif" bridge
  174. config_get ieee80211d "$vif" ieee80211d
  175. config_get iapp_interface "$vif" iapp_interface
  176. config_get_bool wps_pbc "$vif" wps_pushbutton 0
  177. config_get_bool wps_label "$vif" wps_label 0
  178. config_get config_methods "$vif" wps_config
  179. [ "$wps_pbc" -gt 0 ] && append config_methods push_button
  180. [ -n "$wps_possible" -a -n "$config_methods" ] && {
  181. config_get device_type "$vif" wps_device_type "6-0050F204-1"
  182. config_get device_name "$vif" wps_device_name "OpenWrt AP"
  183. config_get manufacturer "$vif" wps_manufacturer "openwrt.org"
  184. config_get wps_pin "$vif" wps_pin
  185. config_get_bool ext_registrar "$vif" ext_registrar 0
  186. [ "$ext_registrar" -gt 0 -a -n "$bridge" ] && append "$var" "upnp_iface=$bridge" "$N"
  187. append "$var" "eap_server=1" "$N"
  188. [ -n "$wps_pin" ] && append "$var" "ap_pin=$wps_pin" "$N"
  189. append "$var" "wps_state=${wps_not_configured:-2}" "$N"
  190. append "$var" "ap_setup_locked=0" "$N"
  191. append "$var" "device_type=$device_type" "$N"
  192. append "$var" "device_name=$device_name" "$N"
  193. append "$var" "manufacturer=$manufacturer" "$N"
  194. append "$var" "config_methods=$config_methods" "$N"
  195. }
  196. append "$var" "ssid=$ssid" "$N"
  197. [ -n "$bridge" ] && append "$var" "bridge=$bridge" "$N"
  198. [ -n "$ieee80211d" ] && append "$var" "ieee80211d=$ieee80211d" "$N"
  199. [ -n "$iapp_interface" ] && append "$var" iapp_interface=$(uci_get_state network "$iapp_interface" ifname "$iapp_interface") "$N"
  200. if [ "$wpa" -ge "1" ]
  201. then
  202. config_get nasid "$vif" nasid
  203. [ -n "$nasid" ] && append "$var" "nas_identifier=$nasid" "$N"
  204. config_get_bool ieee80211r "$vif" ieee80211r 0
  205. if [ "$ieee80211r" -gt 0 ]
  206. then
  207. config_get mobility_domain "$vif" mobility_domain "4f57"
  208. config_get r0_key_lifetime "$vif" r0_key_lifetime "10000"
  209. config_get r1_key_holder "$vif" r1_key_holder "00004f577274"
  210. config_get reassociation_deadline "$vif" reassociation_deadline "1000"
  211. config_get r0kh "$vif" r0kh
  212. config_get r1kh "$vif" r1kh
  213. config_get_bool pmk_r1_push "$vif" pmk_r1_push 0
  214. append "$var" "mobility_domain=$mobility_domain" "$N"
  215. append "$var" "r0_key_lifetime=$r0_key_lifetime" "$N"
  216. append "$var" "r1_key_holder=$r1_key_holder" "$N"
  217. append "$var" "reassociation_deadline=$reassociation_deadline" "$N"
  218. append "$var" "pmk_r1_push=$pmk_r1_push" "$N"
  219. for kh in $r0kh; do
  220. "$var" "r0kh=${kh//,/ }" "$N"
  221. done
  222. for kh in $r1kh; do
  223. "$var" "r1kh=${kh//,/ }" "$N"
  224. done
  225. [ "$wpa_key_mgmt" != "${wpa_key_mgmt/EAP/}" ] && append wpa_key_mgmt "FT-EAP"
  226. [ "$wpa_key_mgmt" != "${wpa_key_mgmt/PSK/}" ] && append wpa_key_mgmt "FT-PSK"
  227. fi
  228. [ -n "wpa_key_mgmt" ] && append "$var" "wpa_key_mgmt=$wpa_key_mgmt"
  229. fi
  230. if [ "$wpa" -ge "2" ]
  231. then
  232. # RSN -> allow preauthentication. You have two
  233. # options, rsn_preauth for production or rsn_preauth_testing
  234. # for validation / testing.
  235. if [ -n "$bridge" -a "$rsn_preauth" = 1 ]
  236. then
  237. append "$var" "rsn_preauth=1" "$N"
  238. append "$var" "rsn_preauth_interfaces=$bridge" "$N"
  239. append "$var" "okc=1" "$N"
  240. else
  241. # RSN preauthentication testings hould disable
  242. # Opportunistic Key Caching (okc) as otherwise the PMKSA
  243. # entry for a test could come from the Opportunistic Key Caching
  244. config_get rsn_preauth_testing "$vif" rsn_preauth_testing
  245. if [ -n "$bridge" -a "$rsn_preauth_testing" = 1 ]
  246. then
  247. append "$var" "rsn_preauth=1" "$N"
  248. append "$var" "rsn_preauth_interfaces=$bridge" "$N"
  249. append "$var" "okc=0" "$N"
  250. fi
  251. fi
  252. # RSN -> allow management frame protection
  253. config_get ieee80211w "$vif" ieee80211w
  254. case "$ieee80211w" in
  255. [012])
  256. append "$var" "ieee80211w=$ieee80211w" "$N"
  257. [ "$ieee80211w" -gt "0" ] && {
  258. config_get ieee80211w_max_timeout "$vif" ieee80211w_max_timeout
  259. config_get ieee80211w_retry_timeout "$vif" ieee80211w_retry_timeout
  260. [ -n "$ieee80211w_max_timeout" ] && \
  261. append "$var" "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
  262. [ -n "$ieee80211w_retry_timeout" ] && \
  263. append "$var" "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
  264. }
  265. ;;
  266. esac
  267. fi
  268. config_get macfile "$vif" macfile
  269. config_get maclist "$vif" maclist
  270. if [ -z "$macfile" ]
  271. then
  272. # if no macfile has been specified, fallback to the default name
  273. # and truncate file to avoid aggregating entries over time
  274. macfile="/var/run/hostapd-$ifname.maclist"
  275. echo "" > "$macfile"
  276. else
  277. if [ -n "$maclist" ]
  278. then
  279. # to avoid to overwrite the original file, make a copy
  280. # before appending the entries specified by the maclist
  281. # option
  282. cp $macfile $macfile.maclist
  283. macfile=$macfile.maclist
  284. fi
  285. fi
  286. if [ -n "$maclist" ]
  287. then
  288. for mac in $maclist; do
  289. echo "$mac" >> $macfile
  290. done
  291. fi
  292. config_get macfilter "$vif" macfilter
  293. case "$macfilter" in
  294. allow)
  295. append "$var" "macaddr_acl=1" "$N"
  296. append "$var" "accept_mac_file=$macfile" "$N"
  297. ;;
  298. deny)
  299. append "$var" "macaddr_acl=0" "$N"
  300. append "$var" "deny_mac_file=$macfile" "$N"
  301. ;;
  302. esac
  303. }
  304. hostapd_set_log_options() {
  305. local var="$1"
  306. local cfg="$2"
  307. local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
  308. config_get log_level "$cfg" log_level 2
  309. config_get_bool log_80211 "$cfg" log_80211 1
  310. config_get_bool log_8021x "$cfg" log_8021x 1
  311. config_get_bool log_radius "$cfg" log_radius 1
  312. config_get_bool log_wpa "$cfg" log_wpa 1
  313. config_get_bool log_driver "$cfg" log_driver 1
  314. config_get_bool log_iapp "$cfg" log_iapp 1
  315. config_get_bool log_mlme "$cfg" log_mlme 1
  316. local log_mask=$(( \
  317. ($log_80211 << 0) | \
  318. ($log_8021x << 1) | \
  319. ($log_radius << 2) | \
  320. ($log_wpa << 3) | \
  321. ($log_driver << 4) | \
  322. ($log_iapp << 5) | \
  323. ($log_mlme << 6) \
  324. ))
  325. append "$var" "logger_syslog=$log_mask" "$N"
  326. append "$var" "logger_syslog_level=$log_level" "$N"
  327. append "$var" "logger_stdout=$log_mask" "$N"
  328. append "$var" "logger_stdout_level=$log_level" "$N"
  329. }
  330. hostapd_setup_vif() {
  331. local vif="$1"
  332. local driver="$2"
  333. local ifname device channel hwmode
  334. hostapd_cfg=
  335. config_get ifname "$vif" ifname
  336. config_get device "$vif" device
  337. config_get channel "$device" channel
  338. config_get hwmode "$device" hwmode
  339. hostapd_set_log_options hostapd_cfg "$device"
  340. hostapd_set_bss_options hostapd_cfg "$vif"
  341. case "$hwmode" in
  342. *bg|*gdt|*gst|*fh) hwmode=g;;
  343. *adt|*ast) hwmode=a;;
  344. esac
  345. [ "$channel" = auto ] && channel=
  346. [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
  347. cat > /var/run/hostapd-$ifname.conf <<EOF
  348. driver=$driver
  349. interface=$ifname
  350. ${hwmode:+hw_mode=${hwmode#11}}
  351. ${channel:+channel=$channel}
  352. $hostapd_cfg
  353. EOF
  354. hostapd -P /var/run/wifi-$ifname.pid -B /var/run/hostapd-$ifname.conf
  355. }