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