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