1
0

hostapd.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. . /lib/functions/network.sh
  2. wpa_supplicant_add_rate() {
  3. local var="$1"
  4. local val="$(($2 / 1000))"
  5. local sub="$((($2 / 100) % 10))"
  6. append $var "$val" ","
  7. [ $sub -gt 0 ] && append $var "."
  8. }
  9. hostapd_add_rate() {
  10. local var="$1"
  11. local val="$(($2 / 100))"
  12. append $var "$val" " "
  13. }
  14. hostapd_append_wep_key() {
  15. local var="$1"
  16. wep_keyidx=0
  17. set_default key 1
  18. case "$key" in
  19. [1234])
  20. for idx in 1 2 3 4; do
  21. local zidx
  22. zidx=$(($idx - 1))
  23. json_get_var ckey "key${idx}"
  24. [ -n "$ckey" ] && \
  25. append $var "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N$T"
  26. done
  27. wep_keyidx=$((key - 1))
  28. ;;
  29. *)
  30. append $var "wep_key0=$(prepare_key_wep "$key")" "$N$T"
  31. ;;
  32. esac
  33. }
  34. hostapd_append_wpa_key_mgmt() {
  35. local auth_type="$(echo $auth_type | tr 'a-z' 'A-Z')"
  36. append wpa_key_mgmt "WPA-$auth_type"
  37. [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-${auth_type}"
  38. [ "${ieee80211w:-0}" -gt 0 ] && append wpa_key_mgmt "WPA-${auth_type}-SHA256"
  39. }
  40. hostapd_add_log_config() {
  41. config_add_boolean \
  42. log_80211 \
  43. log_8021x \
  44. log_radius \
  45. log_wpa \
  46. log_driver \
  47. log_iapp \
  48. log_mlme
  49. config_add_int log_level
  50. }
  51. hostapd_common_add_device_config() {
  52. config_add_array basic_rate
  53. config_add_array supported_rates
  54. config_add_string country
  55. config_add_boolean country_ie doth
  56. config_add_string require_mode
  57. hostapd_add_log_config
  58. }
  59. hostapd_prepare_device_config() {
  60. local config="$1"
  61. local driver="$2"
  62. local base="${config%%.conf}"
  63. local base_cfg=
  64. json_get_vars country country_ie beacon_int doth require_mode
  65. hostapd_set_log_options base_cfg
  66. set_default country_ie 1
  67. set_default doth 1
  68. [ -n "$country" ] && {
  69. append base_cfg "country_code=$country" "$N"
  70. [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
  71. [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
  72. }
  73. [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
  74. local brlist= br
  75. json_get_values basic_rate_list basic_rate
  76. for br in $basic_rate_list; do
  77. hostapd_add_rate brlist "$br"
  78. done
  79. case "$require_mode" in
  80. g) brlist="60 120 240" ;;
  81. n) append base_cfg "require_ht=1" "$N";;
  82. ac) append base_cfg "require_vht=1" "$N";;
  83. esac
  84. local rlist= r
  85. json_get_values rate_list supported_rates
  86. for r in $rate_list; do
  87. hostapd_add_rate rlist "$r"
  88. done
  89. [ -n "$rlist" ] && append base_cfg "supported_rates=$rlist" "$N"
  90. [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
  91. [ -n "$beacon_int" ] && append base_cfg "beacon_int=$beacon_int" "$N"
  92. cat > "$config" <<EOF
  93. driver=$driver
  94. $base_cfg
  95. EOF
  96. }
  97. hostapd_common_add_bss_config() {
  98. config_add_string 'bssid:macaddr' 'ssid:string'
  99. config_add_boolean wds wmm uapsd hidden
  100. config_add_int maxassoc max_inactivity
  101. config_add_boolean disassoc_low_ack isolate short_preamble
  102. config_add_int \
  103. wep_rekey eap_reauth_period \
  104. wpa_group_rekey wpa_pair_rekey wpa_master_rekey
  105. config_add_boolean rsn_preauth auth_cache
  106. config_add_int ieee80211w
  107. config_add_int eapol_version
  108. config_add_string 'auth_server:host' 'server:host'
  109. config_add_string auth_secret
  110. config_add_int 'auth_port:port' 'port:port'
  111. config_add_string acct_server
  112. config_add_string acct_secret
  113. config_add_int acct_port
  114. config_add_string dae_client
  115. config_add_string dae_secret
  116. config_add_int dae_port
  117. config_add_string nasid
  118. config_add_string ownip
  119. config_add_string iapp_interface
  120. config_add_string eap_type ca_cert client_cert identity anonymous_identity auth priv_key priv_key_pwd
  121. config_add_int dynamic_vlan vlan_naming
  122. config_add_string vlan_tagged_interface vlan_bridge
  123. config_add_string vlan_file
  124. config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
  125. config_add_string wpa_psk_file
  126. config_add_boolean wps_pushbutton wps_label ext_registrar wps_pbc_in_m1
  127. config_add_int wps_ap_setup_locked wps_independent
  128. config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
  129. config_add_boolean ieee80211r pmk_r1_push
  130. config_add_int r0_key_lifetime reassociation_deadline
  131. config_add_string mobility_domain r1_key_holder
  132. config_add_array r0kh r1kh
  133. config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
  134. config_add_string macfilter 'macfile:file'
  135. config_add_array 'maclist:list(macaddr)'
  136. config_add_array bssid_blacklist
  137. config_add_array bssid_whitelist
  138. config_add_int mcast_rate
  139. config_add_array basic_rate
  140. config_add_array supported_rates
  141. }
  142. hostapd_set_bss_options() {
  143. local var="$1"
  144. local phy="$2"
  145. local vif="$3"
  146. wireless_vif_parse_encryption
  147. local bss_conf
  148. local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wpa_key_mgmt
  149. json_get_vars \
  150. wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey \
  151. maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
  152. wps_pushbutton wps_label ext_registrar wps_pbc_in_m1 wps_ap_setup_locked \
  153. wps_independent wps_device_type wps_device_name wps_manufacturer wps_pin \
  154. macfilter ssid wmm uapsd hidden short_preamble rsn_preauth \
  155. iapp_interface eapol_version acct_server acct_secret acct_port \
  156. dynamic_vlan ieee80211w
  157. set_default isolate 0
  158. set_default maxassoc 0
  159. set_default max_inactivity 0
  160. set_default short_preamble 1
  161. set_default disassoc_low_ack 1
  162. set_default hidden 0
  163. set_default wmm 1
  164. set_default uapsd 1
  165. set_default eapol_version 0
  166. set_default acct_port 1813
  167. append bss_conf "ctrl_interface=/var/run/hostapd"
  168. if [ "$isolate" -gt 0 ]; then
  169. append bss_conf "ap_isolate=$isolate" "$N"
  170. fi
  171. if [ "$maxassoc" -gt 0 ]; then
  172. append bss_conf "max_num_sta=$maxassoc" "$N"
  173. fi
  174. if [ "$max_inactivity" -gt 0 ]; then
  175. append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
  176. fi
  177. append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
  178. append bss_conf "preamble=$short_preamble" "$N"
  179. append bss_conf "wmm_enabled=$wmm" "$N"
  180. append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
  181. append bss_conf "uapsd_advertisement_enabled=$uapsd" "$N"
  182. [ "$wpa" -gt 0 ] && {
  183. [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
  184. [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
  185. [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
  186. }
  187. [ -n "$acct_server" ] && {
  188. append bss_conf "acct_server_addr=$acct_server" "$N"
  189. append bss_conf "acct_server_port=$acct_port" "$N"
  190. [ -n "$acct_secret" ] && \
  191. append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
  192. }
  193. local vlan_possible=""
  194. case "$auth_type" in
  195. none)
  196. wps_possible=1
  197. # Here we make the assumption that if we're in open mode
  198. # with WPS enabled, we got to be in unconfigured state.
  199. wps_not_configured=1
  200. ;;
  201. psk)
  202. json_get_vars key wpa_psk_file
  203. if [ ${#key} -lt 8 ]; then
  204. wireless_setup_vif_failed INVALID_WPA_PSK
  205. return 1
  206. elif [ ${#key} -eq 64 ]; then
  207. append bss_conf "wpa_psk=$key" "$N"
  208. else
  209. append bss_conf "wpa_passphrase=$key" "$N"
  210. fi
  211. [ -n "$wpa_psk_file" ] && {
  212. [ -e "$wpa_psk_file" ] || touch "$wpa_psk_file"
  213. append bss_conf "wpa_psk_file=$wpa_psk_file" "$N"
  214. }
  215. [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
  216. wps_possible=1
  217. ;;
  218. eap)
  219. json_get_vars \
  220. auth_server auth_secret auth_port \
  221. dae_client dae_secret dae_port \
  222. ownip \
  223. eap_reauth_period
  224. # radius can provide VLAN ID for clients
  225. vlan_possible=1
  226. # legacy compatibility
  227. [ -n "$auth_server" ] || json_get_var auth_server server
  228. [ -n "$auth_port" ] || json_get_var auth_port port
  229. [ -n "$auth_secret" ] || json_get_var auth_secret key
  230. set_default auth_port 1812
  231. set_default dae_port 3799
  232. append bss_conf "auth_server_addr=$auth_server" "$N"
  233. append bss_conf "auth_server_port=$auth_port" "$N"
  234. append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
  235. [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
  236. [ -n "$dae_client" -a -n "$dae_secret" ] && {
  237. append bss_conf "radius_das_port=$dae_port" "$N"
  238. append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
  239. }
  240. [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
  241. append bss_conf "eapol_key_index_workaround=1" "$N"
  242. append bss_conf "ieee8021x=1" "$N"
  243. [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
  244. ;;
  245. wep)
  246. local wep_keyidx=0
  247. json_get_vars key
  248. hostapd_append_wep_key bss_conf
  249. append bss_conf "wep_default_key=$wep_keyidx" "$N"
  250. [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
  251. ;;
  252. esac
  253. local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
  254. append bss_conf "auth_algs=${auth_algs:-1}" "$N"
  255. append bss_conf "wpa=$wpa" "$N"
  256. [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
  257. set_default wps_pushbutton 0
  258. set_default wps_label 0
  259. set_default wps_pbc_in_m1 0
  260. config_methods=
  261. [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
  262. [ "$wps_label" -gt 0 ] && append config_methods label
  263. [ -n "$wps_possible" -a -n "$config_methods" ] && {
  264. set_default ext_registrar 0
  265. set_default wps_device_type "6-0050F204-1"
  266. set_default wps_device_name "Lede AP"
  267. set_default wps_manufacturer "www.lede-project.org"
  268. set_default wps_independent 1
  269. wps_state=2
  270. [ -n "$wps_configured" ] && wps_state=1
  271. [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
  272. append bss_conf "eap_server=1" "$N"
  273. [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
  274. append bss_conf "wps_state=$wps_state" "$N"
  275. append bss_conf "device_type=$wps_device_type" "$N"
  276. append bss_conf "device_name=$wps_device_name" "$N"
  277. append bss_conf "manufacturer=$wps_manufacturer" "$N"
  278. append bss_conf "config_methods=$config_methods" "$N"
  279. append bss_conf "wps_independent=$wps_independent" "$N"
  280. [ -n "$wps_ap_setup_locked" ] && append bss_conf "ap_setup_locked=$wps_ap_setup_locked" "$N"
  281. [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
  282. }
  283. append bss_conf "ssid=$ssid" "$N"
  284. [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
  285. [ -n "$iapp_interface" ] && {
  286. local ifname
  287. network_get_device ifname "$iapp_interface" || ifname = "$iapp_interface"
  288. append bss_conf "iapp_interface=$ifname" "$N"
  289. }
  290. if [ "$wpa" -ge "1" ]; then
  291. json_get_vars nasid ieee80211r
  292. set_default ieee80211r 0
  293. [ -n "$nasid" ] && append bss_conf "nas_identifier=$nasid" "$N"
  294. if [ "$ieee80211r" -gt "0" ]; then
  295. json_get_vars mobility_domain r0_key_lifetime r1_key_holder \
  296. reassociation_deadline pmk_r1_push
  297. json_get_values r0kh r0kh
  298. json_get_values r1kh r1kh
  299. set_default mobility_domain "4f57"
  300. set_default r0_key_lifetime 10000
  301. set_default r1_key_holder "00004f577274"
  302. set_default reassociation_deadline 1000
  303. set_default pmk_r1_push 0
  304. append bss_conf "mobility_domain=$mobility_domain" "$N"
  305. append bss_conf "r0_key_lifetime=$r0_key_lifetime" "$N"
  306. append bss_conf "r1_key_holder=$r1_key_holder" "$N"
  307. append bss_conf "reassociation_deadline=$reassociation_deadline" "$N"
  308. append bss_conf "pmk_r1_push=$pmk_r1_push" "$N"
  309. for kh in $r0kh; do
  310. append bss_conf "r0kh=${kh//,/ }" "$N"
  311. done
  312. for kh in $r1kh; do
  313. append bss_conf "r1kh=${kh//,/ }" "$N"
  314. done
  315. fi
  316. hostapd_append_wpa_key_mgmt
  317. [ -n "$wpa_key_mgmt" ] && append bss_conf "wpa_key_mgmt=$wpa_key_mgmt" "$N"
  318. fi
  319. if [ "$wpa" -ge "2" ]; then
  320. if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
  321. set_default auth_cache 1
  322. append bss_conf "rsn_preauth=1" "$N"
  323. append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
  324. else
  325. set_default auth_cache 0
  326. fi
  327. append bss_conf "okc=$auth_cache" "$N"
  328. [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
  329. # RSN -> allow management frame protection
  330. case "$ieee80211w" in
  331. [012])
  332. json_get_vars ieee80211w_max_timeout ieee80211w_retry_timeout
  333. append bss_conf "ieee80211w=$ieee80211w" "$N"
  334. [ "$ieee80211w" -gt "0" ] && {
  335. [ -n "$ieee80211w_max_timeout" ] && \
  336. append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
  337. [ -n "$ieee80211w_retry_timeout" ] && \
  338. append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
  339. }
  340. ;;
  341. esac
  342. fi
  343. _macfile="/var/run/hostapd-$ifname.maclist"
  344. case "$macfilter" in
  345. allow)
  346. append bss_conf "macaddr_acl=1" "$N"
  347. append bss_conf "accept_mac_file=$_macfile" "$N"
  348. # accept_mac_file can be used to set MAC to VLAN ID mapping
  349. vlan_possible=1
  350. ;;
  351. deny)
  352. append bss_conf "macaddr_acl=0" "$N"
  353. append bss_conf "deny_mac_file=$_macfile" "$N"
  354. ;;
  355. *)
  356. _macfile=""
  357. ;;
  358. esac
  359. [ -n "$_macfile" ] && {
  360. json_get_vars macfile
  361. json_get_values maclist maclist
  362. rm -f "$_macfile"
  363. (
  364. for mac in $maclist; do
  365. echo "$mac"
  366. done
  367. [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
  368. ) > "$_macfile"
  369. }
  370. [ -n "$vlan_possible" -a -n "$dynamic_vlan" ] && {
  371. json_get_vars vlan_naming vlan_tagged_interface vlan_bridge vlan_file
  372. set_default vlan_naming 1
  373. append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
  374. append bss_conf "vlan_naming=$vlan_naming" "$N"
  375. [ -n "$vlan_bridge" ] && \
  376. append bss_conf "vlan_bridge=$vlan_bridge" "$N"
  377. [ -n "$vlan_tagged_interface" ] && \
  378. append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
  379. [ -n "$vlan_file" ] && {
  380. [ -e "$vlan_file" ] || touch "$vlan_file"
  381. append bss_conf "vlan_file=$vlan_file" "$N"
  382. }
  383. }
  384. append "$var" "$bss_conf" "$N"
  385. return 0
  386. }
  387. hostapd_set_log_options() {
  388. local var="$1"
  389. local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
  390. json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
  391. set_default log_level 2
  392. set_default log_80211 1
  393. set_default log_8021x 1
  394. set_default log_radius 1
  395. set_default log_wpa 1
  396. set_default log_driver 1
  397. set_default log_iapp 1
  398. set_default log_mlme 1
  399. local log_mask=$(( \
  400. ($log_80211 << 0) | \
  401. ($log_8021x << 1) | \
  402. ($log_radius << 2) | \
  403. ($log_wpa << 3) | \
  404. ($log_driver << 4) | \
  405. ($log_iapp << 5) | \
  406. ($log_mlme << 6) \
  407. ))
  408. append "$var" "logger_syslog=$log_mask" "$N"
  409. append "$var" "logger_syslog_level=$log_level" "$N"
  410. append "$var" "logger_stdout=$log_mask" "$N"
  411. append "$var" "logger_stdout_level=$log_level" "$N"
  412. return 0
  413. }
  414. _wpa_supplicant_common() {
  415. local ifname="$1"
  416. _rpath="/var/run/wpa_supplicant"
  417. _config="${_rpath}-$ifname.conf"
  418. }
  419. wpa_supplicant_teardown_interface() {
  420. _wpa_supplicant_common "$1"
  421. rm -rf "$_rpath/$1" "$_config"
  422. }
  423. wpa_supplicant_prepare_interface() {
  424. local ifname="$1"
  425. _w_driver="$2"
  426. _wpa_supplicant_common "$1"
  427. json_get_vars mode wds
  428. [ -n "$network_bridge" ] && {
  429. fail=
  430. case "$mode" in
  431. adhoc)
  432. fail=1
  433. ;;
  434. sta)
  435. [ "$wds" = 1 ] || fail=1
  436. ;;
  437. esac
  438. [ -n "$fail" ] && {
  439. wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
  440. return 1
  441. }
  442. }
  443. local ap_scan=
  444. _w_mode="$mode"
  445. _w_modestr=
  446. [[ "$mode" = adhoc ]] && {
  447. ap_scan="ap_scan=2"
  448. _w_modestr="mode=1"
  449. }
  450. local country_str=
  451. [ -n "$country" ] && {
  452. country_str="country=$country"
  453. }
  454. wpa_supplicant_teardown_interface "$ifname"
  455. cat > "$_config" <<EOF
  456. $ap_scan
  457. $country_str
  458. EOF
  459. return 0
  460. }
  461. wpa_supplicant_add_network() {
  462. local ifname="$1"
  463. _wpa_supplicant_common "$1"
  464. wireless_vif_parse_encryption
  465. json_get_vars \
  466. ssid bssid key \
  467. basic_rate mcast_rate \
  468. ieee80211w ieee80211r
  469. set_default ieee80211r 0
  470. local key_mgmt='NONE'
  471. local enc_str=
  472. local network_data=
  473. local T=" "
  474. local scan_ssid="scan_ssid=1"
  475. local freq wpa_key_mgmt
  476. [[ "$_w_mode" = "adhoc" ]] && {
  477. append network_data "mode=1" "$N$T"
  478. [ -n "$channel" ] && {
  479. freq="$(get_freq "$phy" "$channel")"
  480. append network_data "fixed_freq=1" "$N$T"
  481. append network_data "frequency=$freq" "$N$T"
  482. }
  483. scan_ssid="scan_ssid=0"
  484. [ "$_w_driver" = "nl80211" ] || append wpa_key_mgmt "WPA-NONE"
  485. }
  486. [[ "$_w_mode" = "mesh" ]] && {
  487. json_get_vars mesh_id
  488. ssid="${mesh_id}"
  489. append network_data "mode=5" "$N$T"
  490. [ -n "$channel" ] && {
  491. freq="$(get_freq "$phy" "$channel")"
  492. append network_data "frequency=$freq" "$N$T"
  493. }
  494. append wpa_key_mgmt "SAE"
  495. scan_ssid=""
  496. }
  497. [[ "$_w_mode" = "adhoc" -o "$_w_mode" = "mesh" ]] && append network_data "$_w_modestr" "$N$T"
  498. case "$auth_type" in
  499. none) ;;
  500. wep)
  501. local wep_keyidx=0
  502. hostapd_append_wep_key network_data
  503. append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
  504. ;;
  505. psk)
  506. local passphrase
  507. if [ "$_w_mode" != "mesh" ]; then
  508. hostapd_append_wpa_key_mgmt
  509. fi
  510. key_mgmt="$wpa_key_mgmt"
  511. if [ ${#key} -eq 64 ]; then
  512. passphrase="psk=${key}"
  513. else
  514. passphrase="psk=\"${key}\""
  515. fi
  516. append network_data "$passphrase" "$N$T"
  517. ;;
  518. eap)
  519. hostapd_append_wpa_key_mgmt
  520. key_mgmt="$wpa_key_mgmt"
  521. json_get_vars eap_type identity anonymous_identity ca_cert
  522. [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
  523. [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
  524. [ -n "$anonymous_identity" ] && append network_data "anonymous_identity=\"$anonymous_identity\"" "$N$T"
  525. case "$eap_type" in
  526. tls)
  527. json_get_vars client_cert priv_key priv_key_pwd
  528. append network_data "client_cert=\"$client_cert\"" "$N$T"
  529. append network_data "private_key=\"$priv_key\"" "$N$T"
  530. append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
  531. ;;
  532. fast|peap|ttls)
  533. json_get_vars auth password ca_cert2 client_cert2 priv_key2 priv_key2_pwd
  534. set_default auth MSCHAPV2
  535. if [ "$auth" = "EAP-TLS" ]; then
  536. [ -n "$ca_cert2" ] &&
  537. append network_data "ca_cert2=\"$ca_cert2\"" "$N$T"
  538. append network_data "client_cert2=\"$client_cert2\"" "$N$T"
  539. append network_data "private_key2=\"$priv_key2\"" "$N$T"
  540. append network_data "private_key2_passwd=\"$priv_key2_pwd\"" "$N$T"
  541. else
  542. append network_data "password=\"$password\"" "$N$T"
  543. fi
  544. phase2proto="auth="
  545. case "$auth" in
  546. "auth"*)
  547. phase2proto=""
  548. ;;
  549. "EAP-"*)
  550. auth="$(echo $auth | cut -b 5- )"
  551. [ "$eap_type" = "ttls" ] &&
  552. phase2proto="autheap="
  553. ;;
  554. esac
  555. append network_data "phase2=\"$phase2proto$auth\"" "$N$T"
  556. ;;
  557. esac
  558. append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
  559. ;;
  560. esac
  561. [ "$mode" = mesh ] || {
  562. case "$wpa" in
  563. 1)
  564. append network_data "proto=WPA" "$N$T"
  565. ;;
  566. 2)
  567. append network_data "proto=RSN" "$N$T"
  568. ;;
  569. esac
  570. case "$ieee80211w" in
  571. [012])
  572. [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
  573. ;;
  574. esac
  575. }
  576. local beacon_int brates mrate
  577. [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
  578. [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
  579. local bssid_blacklist bssid_whitelist
  580. json_get_values bssid_blacklist bssid_blacklist
  581. json_get_values bssid_whitelist bssid_whitelist
  582. [ -n "$bssid_blacklist" ] && append network_data "bssid_blacklist=$bssid_blacklist" "$N$T"
  583. [ -n "$bssid_whitelist" ] && append network_data "bssid_whitelist=$bssid_whitelist" "$N$T"
  584. [ -n "$basic_rate" ] && {
  585. local br rate_list=
  586. for br in $basic_rate; do
  587. wpa_supplicant_add_rate rate_list "$br"
  588. done
  589. [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
  590. }
  591. [ -n "$mcast_rate" ] && {
  592. local mc_rate=
  593. wpa_supplicant_add_rate mc_rate "$mcast_rate"
  594. append network_data "mcast_rate=$mc_rate" "$N$T"
  595. }
  596. local ht_str
  597. [[ "$_w_mode" = adhoc ]] || ibss_htmode=
  598. [ -n "$ibss_htmode" ] && append network_data "htmode=$ibss_htmode" "$N$T"
  599. cat >> "$_config" <<EOF
  600. network={
  601. $scan_ssid
  602. ssid="$ssid"
  603. key_mgmt=$key_mgmt
  604. $network_data
  605. }
  606. EOF
  607. return 0
  608. }
  609. wpa_supplicant_run() {
  610. local ifname="$1"; shift
  611. _wpa_supplicant_common "$ifname"
  612. /usr/sbin/wpa_supplicant -B \
  613. ${network_bridge:+-b $network_bridge} \
  614. -P "/var/run/wpa_supplicant-${ifname}.pid" \
  615. -D ${_w_driver:-wext} \
  616. -i "$ifname" \
  617. -c "$_config" \
  618. -C "$_rpath" \
  619. "$@"
  620. ret="$?"
  621. wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
  622. [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
  623. return $ret
  624. }
  625. hostapd_common_cleanup() {
  626. killall hostapd wpa_supplicant meshd-nl80211
  627. }