hostapd.sh 13 KB

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