hostapd.sh 21 KB

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