hostapd: Activate Simultaneous Authentication of Equals (SAE)
[openwrt/openwrt.git] / package / network / services / hostapd / files / hostapd.sh
1 . /lib/functions/network.sh
2
3 wpa_supplicant_add_rate() {
4 local var="$1"
5 local val="$(($2 / 1000))"
6 local sub="$((($2 / 100) % 10))"
7 append $var "$val" ","
8 [ $sub -gt 0 ] && append $var "."
9 }
10
11 hostapd_add_rate() {
12 local var="$1"
13 local val="$(($2 / 100))"
14 append $var "$val" " "
15 }
16
17 hostapd_append_wep_key() {
18 local var="$1"
19
20 wep_keyidx=0
21 set_default key 1
22 case "$key" in
23 [1234])
24 for idx in 1 2 3 4; do
25 local zidx
26 zidx=$(($idx - 1))
27 json_get_var ckey "key${idx}"
28 [ -n "$ckey" ] && \
29 append $var "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N$T"
30 done
31 wep_keyidx=$((key - 1))
32 ;;
33 *)
34 append $var "wep_key0=$(prepare_key_wep "$key")" "$N$T"
35 ;;
36 esac
37 }
38
39 hostapd_append_wpa_key_mgmt() {
40 local auth_type_l="$(echo $auth_type | tr 'a-z' 'A-Z')"
41
42 case "$auth_type" in
43 psk|eap)
44 append wpa_key_mgmt "WPA-$auth_type_l"
45 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-${auth_type_l}"
46 [ "${ieee80211w:-0}" -gt 0 ] && append wpa_key_mgmt "WPA-${auth_type_l}-SHA256"
47 ;;
48 sae)
49 append wpa_key_mgmt "SAE"
50 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-SAE"
51 ;;
52 psk-sae)
53 append wpa_key_mgmt "WPA-PSK"
54 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-PSK"
55 [ "${ieee80211w:-0}" -gt 0 ] && append wpa_key_mgmt "WPA-PSK-SHA256"
56 append wpa_key_mgmt "SAE"
57 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-SAE"
58 ;;
59 esac
60 }
61
62 hostapd_add_log_config() {
63 config_add_boolean \
64 log_80211 \
65 log_8021x \
66 log_radius \
67 log_wpa \
68 log_driver \
69 log_iapp \
70 log_mlme
71
72 config_add_int log_level
73 }
74
75 hostapd_common_add_device_config() {
76 config_add_array basic_rate
77 config_add_array supported_rates
78
79 config_add_string country
80 config_add_boolean country_ie doth
81 config_add_string require_mode
82 config_add_boolean legacy_rates
83
84 config_add_string acs_chan_bias
85 config_add_array hostapd_options
86
87 hostapd_add_log_config
88 }
89
90 hostapd_prepare_device_config() {
91 local config="$1"
92 local driver="$2"
93
94 local base="${config%%.conf}"
95 local base_cfg=
96
97 json_get_vars country country_ie beacon_int:100 doth require_mode legacy_rates acs_chan_bias
98
99 hostapd_set_log_options base_cfg
100
101 set_default country_ie 1
102 set_default doth 1
103 set_default legacy_rates 1
104
105 [ "$hwmode" = "b" ] && legacy_rates=1
106
107 [ -n "$country" ] && {
108 append base_cfg "country_code=$country" "$N"
109
110 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
111 [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
112 }
113
114 [ -n "$acs_chan_bias" ] && append base_cfg "acs_chan_bias=$acs_chan_bias" "$N"
115
116 local brlist= br
117 json_get_values basic_rate_list basic_rate
118 local rlist= r
119 json_get_values rate_list supported_rates
120
121 [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
122 [ "$legacy_rates" -eq 0 ] && set_default require_mode g
123
124 [ "$hwmode" = "g" ] && {
125 [ "$legacy_rates" -eq 0 ] && set_default rate_list "6000 9000 12000 18000 24000 36000 48000 54000"
126 [ -n "$require_mode" ] && set_default basic_rate_list "6000 12000 24000"
127 }
128
129 case "$require_mode" in
130 n) append base_cfg "require_ht=1" "$N";;
131 ac) append base_cfg "require_vht=1" "$N";;
132 esac
133
134 for r in $rate_list; do
135 hostapd_add_rate rlist "$r"
136 done
137
138 for br in $basic_rate_list; do
139 hostapd_add_rate brlist "$br"
140 done
141
142 [ -n "$rlist" ] && append base_cfg "supported_rates=$rlist" "$N"
143 [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
144 append base_cfg "beacon_int=$beacon_int" "$N"
145
146 json_get_values opts hostapd_options
147 for val in $opts; do
148 append base_cfg "$val" "$N"
149 done
150
151 cat > "$config" <<EOF
152 driver=$driver
153 $base_cfg
154 EOF
155 }
156
157 hostapd_common_add_bss_config() {
158 config_add_string 'bssid:macaddr' 'ssid:string'
159 config_add_boolean wds wmm uapsd hidden
160
161 config_add_int maxassoc max_inactivity
162 config_add_boolean disassoc_low_ack isolate short_preamble
163
164 config_add_int \
165 wep_rekey eap_reauth_period \
166 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
167 config_add_boolean wpa_disable_eapol_key_retries
168
169 config_add_boolean tdls_prohibit
170
171 config_add_boolean rsn_preauth auth_cache
172 config_add_int ieee80211w
173 config_add_int eapol_version
174
175 config_add_string 'auth_server:host' 'server:host'
176 config_add_string auth_secret
177 config_add_int 'auth_port:port' 'port:port'
178
179 config_add_string acct_server
180 config_add_string acct_secret
181 config_add_int acct_port
182 config_add_int acct_interval
183
184 config_add_string dae_client
185 config_add_string dae_secret
186 config_add_int dae_port
187
188 config_add_string nasid
189 config_add_string ownip
190 config_add_string radius_client_addr
191 config_add_string iapp_interface
192 config_add_string eap_type ca_cert client_cert identity anonymous_identity auth priv_key priv_key_pwd
193 config_add_string ieee80211w_mgmt_cipher
194
195 config_add_int dynamic_vlan vlan_naming
196 config_add_string vlan_tagged_interface vlan_bridge
197 config_add_string vlan_file
198
199 config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
200
201 config_add_string wpa_psk_file
202
203 config_add_boolean wps_pushbutton wps_label ext_registrar wps_pbc_in_m1
204 config_add_int wps_ap_setup_locked wps_independent
205 config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
206
207 config_add_boolean ieee80211v wnm_sleep_mode bss_transition
208 config_add_int time_advertisement
209 config_add_string time_zone
210
211 config_add_boolean ieee80211r pmk_r1_push ft_psk_generate_local ft_over_ds
212 config_add_int r0_key_lifetime reassociation_deadline
213 config_add_string mobility_domain r1_key_holder
214 config_add_array r0kh r1kh
215
216 config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
217
218 config_add_string macfilter 'macfile:file'
219 config_add_array 'maclist:list(macaddr)'
220
221 config_add_array bssid_blacklist
222 config_add_array bssid_whitelist
223
224 config_add_int mcast_rate
225 config_add_array basic_rate
226 config_add_array supported_rates
227
228 config_add_boolean sae_require_mfp
229 }
230
231 hostapd_set_bss_options() {
232 local var="$1"
233 local phy="$2"
234 local vif="$3"
235
236 wireless_vif_parse_encryption
237
238 local bss_conf
239 local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wpa_key_mgmt
240
241 json_get_vars \
242 wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey \
243 wpa_disable_eapol_key_retries tdls_prohibit \
244 maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
245 wps_pushbutton wps_label ext_registrar wps_pbc_in_m1 wps_ap_setup_locked \
246 wps_independent wps_device_type wps_device_name wps_manufacturer wps_pin \
247 macfilter ssid wmm uapsd hidden short_preamble rsn_preauth \
248 iapp_interface eapol_version dynamic_vlan ieee80211w nasid \
249 acct_server acct_secret acct_port acct_interval \
250 bss_load_update_period chan_util_avg_period sae_require_mfp
251
252 set_default isolate 0
253 set_default maxassoc 0
254 set_default max_inactivity 0
255 set_default short_preamble 1
256 set_default disassoc_low_ack 1
257 set_default hidden 0
258 set_default wmm 1
259 set_default uapsd 1
260 set_default wpa_disable_eapol_key_retries 0
261 set_default tdls_prohibit 0
262 set_default eapol_version 0
263 set_default acct_port 1813
264 set_default bss_load_update_period 60
265 set_default chan_util_avg_period 600
266
267 append bss_conf "ctrl_interface=/var/run/hostapd"
268 if [ "$isolate" -gt 0 ]; then
269 append bss_conf "ap_isolate=$isolate" "$N"
270 fi
271 if [ "$maxassoc" -gt 0 ]; then
272 append bss_conf "max_num_sta=$maxassoc" "$N"
273 fi
274 if [ "$max_inactivity" -gt 0 ]; then
275 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
276 fi
277
278 append bss_conf "bss_load_update_period=$bss_load_update_period" "$N"
279 append bss_conf "chan_util_avg_period=$chan_util_avg_period" "$N"
280 append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
281 append bss_conf "preamble=$short_preamble" "$N"
282 append bss_conf "wmm_enabled=$wmm" "$N"
283 append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
284 append bss_conf "uapsd_advertisement_enabled=$uapsd" "$N"
285
286 [ "$tdls_prohibit" -gt 0 ] && append bss_conf "tdls_prohibit=$tdls_prohibit" "$N"
287
288 [ "$wpa" -gt 0 ] && {
289 [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
290 [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
291 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
292 }
293
294 [ -n "$nasid" ] && append bss_conf "nas_identifier=$nasid" "$N"
295 [ -n "$acct_server" ] && {
296 append bss_conf "acct_server_addr=$acct_server" "$N"
297 append bss_conf "acct_server_port=$acct_port" "$N"
298 [ -n "$acct_secret" ] && \
299 append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
300 [ -n "$acct_interval" ] && \
301 append bss_conf "radius_acct_interim_interval=$acct_interval" "$N"
302 }
303
304 case "$auth_type" in
305 sae)
306 set_default ieee80211w 2
307 set_default sae_require_mfp 1
308 ;;
309 psk-sae)
310 set_default ieee80211w 1
311 set_default sae_require_mfp 1
312 ;;
313 esac
314 [ -n "$sae_require_mfp" ] && append bss_conf "sae_require_mfp=$sae_require_mfp" "$N"
315
316 local vlan_possible=""
317
318 case "$auth_type" in
319 none)
320 wps_possible=1
321 # Here we make the assumption that if we're in open mode
322 # with WPS enabled, we got to be in unconfigured state.
323 wps_not_configured=1
324 ;;
325 psk|sae|psk-sae)
326 json_get_vars key wpa_psk_file
327 if [ ${#key} -lt 8 ]; then
328 wireless_setup_vif_failed INVALID_WPA_PSK
329 return 1
330 elif [ ${#key} -eq 64 ]; then
331 append bss_conf "wpa_psk=$key" "$N"
332 else
333 append bss_conf "wpa_passphrase=$key" "$N"
334 fi
335 [ -n "$wpa_psk_file" ] && {
336 [ -e "$wpa_psk_file" ] || touch "$wpa_psk_file"
337 append bss_conf "wpa_psk_file=$wpa_psk_file" "$N"
338 }
339 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
340
341 wps_possible=1
342 ;;
343 eap)
344 json_get_vars \
345 auth_server auth_secret auth_port \
346 dae_client dae_secret dae_port \
347 ownip radius_client_addr \
348 eap_reauth_period
349
350 # radius can provide VLAN ID for clients
351 vlan_possible=1
352
353 # legacy compatibility
354 [ -n "$auth_server" ] || json_get_var auth_server server
355 [ -n "$auth_port" ] || json_get_var auth_port port
356 [ -n "$auth_secret" ] || json_get_var auth_secret key
357
358 set_default auth_port 1812
359 set_default dae_port 3799
360
361
362 append bss_conf "auth_server_addr=$auth_server" "$N"
363 append bss_conf "auth_server_port=$auth_port" "$N"
364 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
365
366 [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
367
368 [ -n "$dae_client" -a -n "$dae_secret" ] && {
369 append bss_conf "radius_das_port=$dae_port" "$N"
370 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
371 }
372
373 [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
374 [ -n "$radius_client_addr" ] && append bss_conf "radius_client_addr=$radius_client_addr" "$N"
375 append bss_conf "eapol_key_index_workaround=1" "$N"
376 append bss_conf "ieee8021x=1" "$N"
377
378 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
379 ;;
380 wep)
381 local wep_keyidx=0
382 json_get_vars key
383 hostapd_append_wep_key bss_conf
384 append bss_conf "wep_default_key=$wep_keyidx" "$N"
385 [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
386 ;;
387 esac
388
389 local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
390 append bss_conf "auth_algs=${auth_algs:-1}" "$N"
391 append bss_conf "wpa=$wpa" "$N"
392 [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
393
394 set_default wps_pushbutton 0
395 set_default wps_label 0
396 set_default wps_pbc_in_m1 0
397
398 config_methods=
399 [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
400 [ "$wps_label" -gt 0 ] && append config_methods label
401
402 [ -n "$wps_possible" -a -n "$config_methods" ] && {
403 set_default ext_registrar 0
404 set_default wps_device_type "6-0050F204-1"
405 set_default wps_device_name "OpenWrt AP"
406 set_default wps_manufacturer "www.openwrt.org"
407 set_default wps_independent 1
408
409 wps_state=2
410 [ -n "$wps_configured" ] && wps_state=1
411
412 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
413
414 append bss_conf "eap_server=1" "$N"
415 [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
416 append bss_conf "wps_state=$wps_state" "$N"
417 append bss_conf "device_type=$wps_device_type" "$N"
418 append bss_conf "device_name=$wps_device_name" "$N"
419 append bss_conf "manufacturer=$wps_manufacturer" "$N"
420 append bss_conf "config_methods=$config_methods" "$N"
421 append bss_conf "wps_independent=$wps_independent" "$N"
422 [ -n "$wps_ap_setup_locked" ] && append bss_conf "ap_setup_locked=$wps_ap_setup_locked" "$N"
423 [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
424 }
425
426 append bss_conf "ssid=$ssid" "$N"
427 [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
428 [ -n "$iapp_interface" ] && {
429 local ifname
430 network_get_device ifname "$iapp_interface" || ifname="$iapp_interface"
431 append bss_conf "iapp_interface=$ifname" "$N"
432 }
433
434 json_get_vars ieee80211v
435 set_default ieee80211v 0
436 if [ "$ieee80211v" -eq "1" ]; then
437 json_get_vars time_advertisement time_zone wnm_sleep_mode bss_transition
438
439 set_default time_advertisement 0
440 set_default wnm_sleep_mode 0
441 set_default bss_transition 0
442
443 append bss_conf "time_advertisement=$time_advertisement" "$N"
444 [ -n "$time_zone" ] && append bss_conf "time_zone=$time_zone" "$N"
445 append bss_conf "wnm_sleep_mode=$wnm_sleep_mode" "$N"
446 append bss_conf "bss_transition=$bss_transition" "$N"
447 fi
448
449 if [ "$wpa" -ge "1" ]; then
450 json_get_vars ieee80211r
451 set_default ieee80211r 0
452
453 if [ "$ieee80211r" -gt "0" ]; then
454 json_get_vars mobility_domain ft_psk_generate_local ft_over_ds reassociation_deadline
455
456 set_default mobility_domain "$(echo "$ssid" | md5sum | head -c 4)"
457 set_default ft_psk_generate_local 1
458 set_default ft_over_ds 1
459 set_default reassociation_deadline 1000
460
461 append bss_conf "mobility_domain=$mobility_domain" "$N"
462 append bss_conf "ft_psk_generate_local=$ft_psk_generate_local" "$N"
463 append bss_conf "ft_over_ds=$ft_over_ds" "$N"
464 append bss_conf "reassociation_deadline=$reassociation_deadline" "$N"
465 [ -n "$nasid" ] || append bss_conf "nas_identifier=${macaddr//\:}" "$N"
466
467 if [ "$ft_psk_generate_local" -eq "0" ]; then
468 json_get_vars r0_key_lifetime r1_key_holder pmk_r1_push
469 json_get_values r0kh r0kh
470 json_get_values r1kh r1kh
471
472 set_default r0_key_lifetime 10000
473 set_default pmk_r1_push 0
474
475 [ -n "$r1_key_holder" ] && append bss_conf "r1_key_holder=$r1_key_holder" "$N"
476 append bss_conf "r0_key_lifetime=$r0_key_lifetime" "$N"
477 append bss_conf "pmk_r1_push=$pmk_r1_push" "$N"
478
479 for kh in $r0kh; do
480 append bss_conf "r0kh=${kh//,/ }" "$N"
481 done
482 for kh in $r1kh; do
483 append bss_conf "r1kh=${kh//,/ }" "$N"
484 done
485 fi
486 fi
487
488 append bss_conf "wpa_disable_eapol_key_retries=$wpa_disable_eapol_key_retries" "$N"
489
490 hostapd_append_wpa_key_mgmt
491 [ -n "$wpa_key_mgmt" ] && append bss_conf "wpa_key_mgmt=$wpa_key_mgmt" "$N"
492 fi
493
494 if [ "$wpa" -ge "2" ]; then
495 if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
496 set_default auth_cache 1
497 append bss_conf "rsn_preauth=1" "$N"
498 append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
499 else
500 set_default auth_cache 0
501 fi
502
503 append bss_conf "okc=$auth_cache" "$N"
504 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
505
506 # RSN -> allow management frame protection
507 case "$ieee80211w" in
508 [012])
509 json_get_vars ieee80211w_mgmt_cipher ieee80211w_max_timeout ieee80211w_retry_timeout
510 append bss_conf "ieee80211w=$ieee80211w" "$N"
511 [ "$ieee80211w" -gt "0" ] && {
512 append bss_conf "group_mgmt_cipher=${ieee80211w_mgmt_cipher:-AES-128-CMAC}" "$N"
513 [ -n "$ieee80211w_max_timeout" ] && \
514 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
515 [ -n "$ieee80211w_retry_timeout" ] && \
516 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
517 }
518 ;;
519 esac
520 fi
521
522 _macfile="/var/run/hostapd-$ifname.maclist"
523 case "$macfilter" in
524 allow)
525 append bss_conf "macaddr_acl=1" "$N"
526 append bss_conf "accept_mac_file=$_macfile" "$N"
527 # accept_mac_file can be used to set MAC to VLAN ID mapping
528 vlan_possible=1
529 ;;
530 deny)
531 append bss_conf "macaddr_acl=0" "$N"
532 append bss_conf "deny_mac_file=$_macfile" "$N"
533 ;;
534 *)
535 _macfile=""
536 ;;
537 esac
538
539 [ -n "$_macfile" ] && {
540 json_get_vars macfile
541 json_get_values maclist maclist
542
543 rm -f "$_macfile"
544 (
545 for mac in $maclist; do
546 echo "$mac"
547 done
548 [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
549 ) > "$_macfile"
550 }
551
552 [ -n "$vlan_possible" -a -n "$dynamic_vlan" ] && {
553 json_get_vars vlan_naming vlan_tagged_interface vlan_bridge vlan_file
554 set_default vlan_naming 1
555 append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
556 append bss_conf "vlan_naming=$vlan_naming" "$N"
557 [ -n "$vlan_bridge" ] && \
558 append bss_conf "vlan_bridge=$vlan_bridge" "$N"
559 [ -n "$vlan_tagged_interface" ] && \
560 append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
561 [ -n "$vlan_file" ] && {
562 [ -e "$vlan_file" ] || touch "$vlan_file"
563 append bss_conf "vlan_file=$vlan_file" "$N"
564 }
565 }
566
567 append "$var" "$bss_conf" "$N"
568 return 0
569 }
570
571 hostapd_set_log_options() {
572 local var="$1"
573
574 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
575 json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
576
577 set_default log_level 2
578 set_default log_80211 1
579 set_default log_8021x 1
580 set_default log_radius 1
581 set_default log_wpa 1
582 set_default log_driver 1
583 set_default log_iapp 1
584 set_default log_mlme 1
585
586 local log_mask=$(( \
587 ($log_80211 << 0) | \
588 ($log_8021x << 1) | \
589 ($log_radius << 2) | \
590 ($log_wpa << 3) | \
591 ($log_driver << 4) | \
592 ($log_iapp << 5) | \
593 ($log_mlme << 6) \
594 ))
595
596 append "$var" "logger_syslog=$log_mask" "$N"
597 append "$var" "logger_syslog_level=$log_level" "$N"
598 append "$var" "logger_stdout=$log_mask" "$N"
599 append "$var" "logger_stdout_level=$log_level" "$N"
600
601 return 0
602 }
603
604 _wpa_supplicant_common() {
605 local ifname="$1"
606
607 _rpath="/var/run/wpa_supplicant"
608 _config="${_rpath}-$ifname.conf"
609 }
610
611 wpa_supplicant_teardown_interface() {
612 _wpa_supplicant_common "$1"
613 rm -rf "$_rpath/$1" "$_config"
614 }
615
616 wpa_supplicant_prepare_interface() {
617 local ifname="$1"
618 _w_driver="$2"
619
620 _wpa_supplicant_common "$1"
621
622 json_get_vars mode wds
623
624 [ -n "$network_bridge" ] && {
625 fail=
626 case "$mode" in
627 adhoc)
628 fail=1
629 ;;
630 sta)
631 [ "$wds" = 1 ] || fail=1
632 ;;
633 esac
634
635 [ -n "$fail" ] && {
636 wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
637 return 1
638 }
639 }
640
641 local ap_scan=
642
643 _w_mode="$mode"
644 _w_modestr=
645
646 [[ "$mode" = adhoc ]] && {
647 ap_scan="ap_scan=2"
648
649 _w_modestr="mode=1"
650 }
651
652 local country_str=
653 [ -n "$country" ] && {
654 country_str="country=$country"
655 }
656
657 wpa_supplicant_teardown_interface "$ifname"
658 cat > "$_config" <<EOF
659 $ap_scan
660 $country_str
661 EOF
662 return 0
663 }
664
665 wpa_supplicant_set_fixed_freq() {
666 local freq="$1"
667 local htmode="$2"
668
669 append network_data "fixed_freq=1" "$N$T"
670 append network_data "frequency=$freq" "$N$T"
671 case "$htmode" in
672 NOHT) append network_data "disable_ht=1" "$N$T";;
673 HT20|VHT20) append network_data "disable_ht40=1" "$N$T";;
674 HT40*|VHT40*|VHT80*|VHT160*) append network_data "ht40=1" "$N$T";;
675 esac
676 case "$htmode" in
677 VHT*) append network_data "vht=1" "$N$T";;
678 esac
679 case "$htmode" in
680 VHT80) append network_data "max_oper_chwidth=1" "$N$T";;
681 VHT160) append network_data "max_oper_chwidth=2" "$N$T";;
682 *) append network_data "max_oper_chwidth=0" "$N$T";;
683 esac
684 }
685
686 wpa_supplicant_add_network() {
687 local ifname="$1"
688 local freq="$2"
689 local htmode="$3"
690 local noscan="$4"
691
692 _wpa_supplicant_common "$1"
693 wireless_vif_parse_encryption
694
695 json_get_vars \
696 ssid bssid key \
697 basic_rate mcast_rate \
698 ieee80211w ieee80211r
699
700 set_default ieee80211r 0
701
702 local key_mgmt='NONE'
703 local enc_str=
704 local network_data=
705 local T=" "
706
707 local scan_ssid="scan_ssid=1"
708 local freq wpa_key_mgmt
709
710 [[ "$_w_mode" = "adhoc" ]] && {
711 append network_data "mode=1" "$N$T"
712 [ -n "$freq" ] && wpa_supplicant_set_fixed_freq "$freq" "$htmode"
713
714 scan_ssid="scan_ssid=0"
715
716 [ "$_w_driver" = "nl80211" ] || append wpa_key_mgmt "WPA-NONE"
717 }
718
719 [[ "$_w_mode" = "mesh" ]] && {
720 json_get_vars mesh_id mesh_fwding mesh_rssi_threshold
721 [ -n "$mesh_id" ] && ssid="${mesh_id}"
722
723 append network_data "mode=5" "$N$T"
724 [ -n "$mesh_fwding" ] && append network_data "mesh_fwding=${mesh_fwding}" "$N$T"
725 [ -n "$mesh_rssi_threshold" ] && append network_data "mesh_rssi_threshold=${mesh_rssi_threshold}" "$N$T"
726 [ -n "$freq" ] && wpa_supplicant_set_fixed_freq "$freq" "$htmode"
727 [ "$noscan" = "1" ] && append network_data "noscan=1" "$N$T"
728 append wpa_key_mgmt "SAE"
729 scan_ssid=""
730 }
731
732 [ "$_w_mode" = "adhoc" -o "$_w_mode" = "mesh" ] && append network_data "$_w_modestr" "$N$T"
733
734 case "$auth_type" in
735 none) ;;
736 wep)
737 local wep_keyidx=0
738 hostapd_append_wep_key network_data
739 append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
740 ;;
741 psk|sae|psk-sae)
742 local passphrase
743
744 if [ "$_w_mode" != "mesh" ]; then
745 hostapd_append_wpa_key_mgmt
746 fi
747
748 key_mgmt="$wpa_key_mgmt"
749
750 if [ ${#key} -eq 64 ]; then
751 passphrase="psk=${key}"
752 else
753 if [ "$_w_mode" = "mesh" ]; then
754 passphrase="sae_password=\"${key}\""
755 else
756 passphrase="psk=\"${key}\""
757 fi
758 fi
759 append network_data "$passphrase" "$N$T"
760 ;;
761 eap)
762 hostapd_append_wpa_key_mgmt
763 key_mgmt="$wpa_key_mgmt"
764
765 json_get_vars eap_type identity anonymous_identity ca_cert
766 [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
767 [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
768 [ -n "$anonymous_identity" ] && append network_data "anonymous_identity=\"$anonymous_identity\"" "$N$T"
769 case "$eap_type" in
770 tls)
771 json_get_vars client_cert priv_key priv_key_pwd
772 append network_data "client_cert=\"$client_cert\"" "$N$T"
773 append network_data "private_key=\"$priv_key\"" "$N$T"
774 append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
775 ;;
776 fast|peap|ttls)
777 json_get_vars auth password ca_cert2 client_cert2 priv_key2 priv_key2_pwd
778 set_default auth MSCHAPV2
779
780 if [ "$auth" = "EAP-TLS" ]; then
781 [ -n "$ca_cert2" ] &&
782 append network_data "ca_cert2=\"$ca_cert2\"" "$N$T"
783 append network_data "client_cert2=\"$client_cert2\"" "$N$T"
784 append network_data "private_key2=\"$priv_key2\"" "$N$T"
785 append network_data "private_key2_passwd=\"$priv_key2_pwd\"" "$N$T"
786 else
787 append network_data "password=\"$password\"" "$N$T"
788 fi
789
790 phase2proto="auth="
791 case "$auth" in
792 "auth"*)
793 phase2proto=""
794 ;;
795 "EAP-"*)
796 auth="$(echo $auth | cut -b 5- )"
797 [ "$eap_type" = "ttls" ] &&
798 phase2proto="autheap="
799 ;;
800 esac
801 append network_data "phase2=\"$phase2proto$auth\"" "$N$T"
802 ;;
803 esac
804 append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
805 ;;
806 esac
807
808 [ "$mode" = mesh ] || {
809 case "$wpa" in
810 1)
811 append network_data "proto=WPA" "$N$T"
812 ;;
813 2)
814 append network_data "proto=RSN" "$N$T"
815 ;;
816 esac
817
818 case "$ieee80211w" in
819 [012])
820 [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
821 ;;
822 esac
823 }
824 [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
825 [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
826
827 local bssid_blacklist bssid_whitelist
828 json_get_values bssid_blacklist bssid_blacklist
829 json_get_values bssid_whitelist bssid_whitelist
830
831 [ -n "$bssid_blacklist" ] && append network_data "bssid_blacklist=$bssid_blacklist" "$N$T"
832 [ -n "$bssid_whitelist" ] && append network_data "bssid_whitelist=$bssid_whitelist" "$N$T"
833
834 [ -n "$basic_rate" ] && {
835 local br rate_list=
836 for br in $basic_rate; do
837 wpa_supplicant_add_rate rate_list "$br"
838 done
839 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
840 }
841
842 [ -n "$mcast_rate" ] && {
843 local mc_rate=
844 wpa_supplicant_add_rate mc_rate "$mcast_rate"
845 append network_data "mcast_rate=$mc_rate" "$N$T"
846 }
847
848 cat >> "$_config" <<EOF
849 network={
850 $scan_ssid
851 ssid="$ssid"
852 key_mgmt=$key_mgmt
853 $network_data
854 }
855 EOF
856 return 0
857 }
858
859 wpa_supplicant_run() {
860 local ifname="$1"; shift
861
862 _wpa_supplicant_common "$ifname"
863
864 /usr/sbin/wpa_supplicant -B \
865 ${network_bridge:+-b $network_bridge} \
866 -P "/var/run/wpa_supplicant-${ifname}.pid" \
867 -D ${_w_driver:-wext} \
868 -i "$ifname" \
869 -c "$_config" \
870 -C "$_rpath" \
871 "$@"
872
873 ret="$?"
874 wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
875
876 [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
877
878 return $ret
879 }
880
881 hostapd_common_cleanup() {
882 killall hostapd wpa_supplicant meshd-nl80211
883 }