blob: 8c233a4e60bf1f7dc9d4b9e8e7b2bab2ebc3a5a8 [file] [log] [blame]
developer8556ff22022-11-10 11:58:51 +08001. /lib/functions/network.sh
2. /lib/functions.sh
3
4wpa_supplicant_add_rate() {
5 local var="$1"
6 local val="$(($2 / 1000))"
7 local sub="$((($2 / 100) % 10))"
8 append $var "$val" ","
9 [ $sub -gt 0 ] && append $var "."
10}
11
12hostapd_add_rate() {
13 local var="$1"
14 local val="$(($2 / 100))"
15 append $var "$val" " "
16}
17
18hostapd_append_wep_key() {
19 local var="$1"
20
21 wep_keyidx=0
22 set_default key 1
23 case "$key" in
24 [1234])
25 for idx in 1 2 3 4; do
26 local zidx
developer2c78ce72023-02-24 11:26:12 +080027 zidx="$(($idx - 1))"
developer8556ff22022-11-10 11:58:51 +080028 json_get_var ckey "key${idx}"
29 [ -n "$ckey" ] && \
30 append $var "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N$T"
31 done
developer2c78ce72023-02-24 11:26:12 +080032 wep_keyidx="$((key - 1))"
developer8556ff22022-11-10 11:58:51 +080033 ;;
34 *)
35 append $var "wep_key0=$(prepare_key_wep "$key")" "$N$T"
36 ;;
37 esac
38}
39
40hostapd_append_wpa_key_mgmt() {
41 local auth_type_l="$(echo $auth_type | tr 'a-z' 'A-Z')"
42
43 case "$auth_type" in
44 psk|eap)
45 append wpa_key_mgmt "WPA-$auth_type_l"
46 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-${auth_type_l}"
47 [ "${ieee80211w:-0}" -gt 0 ] && append wpa_key_mgmt "WPA-${auth_type_l}-SHA256"
48 ;;
49 eap192)
50 append wpa_key_mgmt "WPA-EAP-SUITE-B-192"
51 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-EAP-SHA384"
52 ;;
53 eap-eap192)
54 append wpa_key_mgmt "WPA-EAP-SUITE-B-192"
55 append wpa_key_mgmt "WPA-EAP"
56 [ "${ieee80211r:-0}" -gt 0 ] && {
57 append wpa_key_mgmt "FT-EAP-SHA384"
58 append wpa_key_mgmt "FT-EAP"
59 }
60 [ "${ieee80211w:-0}" -gt 0 ] && append wpa_key_mgmt "WPA-EAP-SHA256"
61 ;;
62 sae)
63 append wpa_key_mgmt "SAE"
64 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-SAE"
65 ;;
66 psk-sae)
67 append wpa_key_mgmt "WPA-PSK"
68 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-PSK"
69 [ "${ieee80211w:-0}" -gt 0 ] && append wpa_key_mgmt "WPA-PSK-SHA256"
70 append wpa_key_mgmt "SAE"
71 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt "FT-SAE"
72 ;;
73 owe)
74 append wpa_key_mgmt "OWE"
75 ;;
76 esac
77
78 [ "$fils" -gt 0 ] && {
79 case "$auth_type" in
80 eap*)
81 append wpa_key_mgmt FILS-SHA256
82 [ "${ieee80211r:-0}" -gt 0 ] && append wpa_key_mgmt FT-FILS-SHA256
83 ;;
84 esac
85 }
86
87 [ "$auth_osen" = "1" ] && append wpa_key_mgmt "OSEN"
88}
89
90hostapd_add_log_config() {
91 config_add_boolean \
92 log_80211 \
93 log_8021x \
94 log_radius \
95 log_wpa \
96 log_driver \
97 log_iapp \
98 log_mlme
99
100 config_add_int log_level
101}
102
103hostapd_common_add_device_config() {
104 config_add_array basic_rate
105 config_add_array supported_rates
106 config_add_string beacon_rate
107
108 config_add_string country country3
109 config_add_boolean country_ie doth
110 config_add_boolean spectrum_mgmt_required
111 config_add_int local_pwr_constraint
112 config_add_string require_mode
113 config_add_boolean legacy_rates
114 config_add_int cell_density
115 config_add_int rts_threshold
116 config_add_int rssi_reject_assoc_rssi
117 config_add_int rssi_ignore_probe_request
118 config_add_int maxassoc
119
120 config_add_string acs_chan_bias
121 config_add_array hostapd_options
122
123 config_add_int airtime_mode
124
125 hostapd_add_log_config
126}
127
128hostapd_prepare_device_config() {
129 local config="$1"
130 local driver="$2"
131
132 local base_cfg=
133
developer047bc182022-11-16 12:20:48 +0800134 json_get_vars country country3 country_ie beacon_int:100 doth require_mode legacy_rates \
developer8556ff22022-11-10 11:58:51 +0800135 acs_chan_bias local_pwr_constraint spectrum_mgmt_required airtime_mode cell_density \
136 rts_threshold beacon_rate rssi_reject_assoc_rssi rssi_ignore_probe_request maxassoc
137
138 hostapd_set_log_options base_cfg
139
140 set_default country_ie 1
141 set_default spectrum_mgmt_required 0
142 set_default doth 1
143 set_default legacy_rates 0
144 set_default airtime_mode 0
145 set_default cell_density 0
146
147 [ -n "$country" ] && {
148 append base_cfg "country_code=$country" "$N"
149 [ -n "$country3" ] && append base_cfg "country3=$country3" "$N"
150
151 [ "$country_ie" -gt 0 ] && {
152 append base_cfg "ieee80211d=1" "$N"
153 [ -n "$local_pwr_constraint" ] && append base_cfg "local_pwr_constraint=$local_pwr_constraint" "$N"
154 [ "$spectrum_mgmt_required" -gt 0 ] && append base_cfg "spectrum_mgmt_required=$spectrum_mgmt_required" "$N"
155 }
156 [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
157 }
158
159 [ -n "$acs_chan_bias" ] && append base_cfg "acs_chan_bias=$acs_chan_bias" "$N"
160
161 local brlist= br
162 json_get_values basic_rate_list basic_rate
163 local rlist= r
164 json_get_values rate_list supported_rates
165
166 [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
167 if [ "$hwmode" = "g" ] || [ "$hwmode" = "a" ]; then
168 [ -n "$require_mode" ] && legacy_rates=0
169 case "$require_mode" in
170 n) append base_cfg "require_ht=1" "$N";;
171 ac) append base_cfg "require_vht=1" "$N";;
172 esac
173 fi
174 case "$hwmode" in
175 b)
176 if [ "$cell_density" -eq 1 ]; then
177 set_default rate_list "5500 11000"
178 set_default basic_rate_list "5500 11000"
179 elif [ "$cell_density" -ge 2 ]; then
180 set_default rate_list "11000"
181 set_default basic_rate_list "11000"
182 fi
183 ;;
184 g)
185 if [ "$cell_density" -eq 0 ] || [ "$cell_density" -eq 1 ]; then
186 if [ "$legacy_rates" -eq 0 ]; then
187 set_default rate_list "6000 9000 12000 18000 24000 36000 48000 54000"
188 set_default basic_rate_list "6000 12000 24000"
189 elif [ "$cell_density" -eq 1 ]; then
190 set_default rate_list "5500 6000 9000 11000 12000 18000 24000 36000 48000 54000"
191 set_default basic_rate_list "5500 11000"
192 fi
193 elif [ "$cell_density" -ge 3 ] && [ "$legacy_rates" -ne 0 ] || [ "$cell_density" -eq 2 ]; then
194 if [ "$legacy_rates" -eq 0 ]; then
195 set_default rate_list "12000 18000 24000 36000 48000 54000"
196 set_default basic_rate_list "12000 24000"
197 else
198 set_default rate_list "11000 12000 18000 24000 36000 48000 54000"
199 set_default basic_rate_list "11000"
200 fi
201 elif [ "$cell_density" -ge 3 ]; then
202 set_default rate_list "24000 36000 48000 54000"
203 set_default basic_rate_list "24000"
204 fi
205 ;;
206 a)
207 if [ "$cell_density" -eq 1 ]; then
208 set_default rate_list "6000 9000 12000 18000 24000 36000 48000 54000"
209 set_default basic_rate_list "6000 12000 24000"
210 elif [ "$cell_density" -eq 2 ]; then
211 set_default rate_list "12000 18000 24000 36000 48000 54000"
212 set_default basic_rate_list "12000 24000"
213 elif [ "$cell_density" -ge 3 ]; then
214 set_default rate_list "24000 36000 48000 54000"
215 set_default basic_rate_list "24000"
216 fi
217 ;;
218 esac
219
220 for r in $rate_list; do
221 hostapd_add_rate rlist "$r"
222 done
223
224 for br in $basic_rate_list; do
225 hostapd_add_rate brlist "$br"
226 done
227
228 [ -n "$rssi_reject_assoc_rssi" ] && append base_cfg "rssi_reject_assoc_rssi=$rssi_reject_assoc_rssi" "$N"
229 [ -n "$rssi_ignore_probe_request" ] && append base_cfg "rssi_ignore_probe_request=$rssi_ignore_probe_request" "$N"
230 [ -n "$beacon_rate" ] && append base_cfg "beacon_rate=$beacon_rate" "$N"
231 [ -n "$rlist" ] && append base_cfg "supported_rates=$rlist" "$N"
232 [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
233 append base_cfg "beacon_int=$beacon_int" "$N"
234 [ -n "$rts_threshold" ] && append base_cfg "rts_threshold=$rts_threshold" "$N"
developer8556ff22022-11-10 11:58:51 +0800235 [ "$airtime_mode" -gt 0 ] && append base_cfg "airtime_mode=$airtime_mode" "$N"
236 [ -n "$maxassoc" ] && append base_cfg "iface_max_num_sta=$maxassoc" "$N"
237
238 json_get_values opts hostapd_options
239 for val in $opts; do
240 append base_cfg "$val" "$N"
241 done
242
243 cat > "$config" <<EOF
244driver=$driver
245$base_cfg
246EOF
247}
248
249hostapd_common_add_bss_config() {
250 config_add_string 'bssid:macaddr' 'ssid:string'
251 config_add_boolean wds wmm uapsd hidden utf8_ssid ppsk
252
253 config_add_int maxassoc max_inactivity
254 config_add_boolean disassoc_low_ack isolate short_preamble skip_inactivity_poll
255
256 config_add_int \
257 wep_rekey eap_reauth_period \
258 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
259 config_add_boolean wpa_strict_rekey
260 config_add_boolean wpa_disable_eapol_key_retries
261
262 config_add_boolean tdls_prohibit
263
264 config_add_boolean rsn_preauth auth_cache
265 config_add_int ieee80211w
266 config_add_int eapol_version
267
268 config_add_string 'auth_server:host' 'server:host'
269 config_add_string auth_secret key
270 config_add_int 'auth_port:port' 'port:port'
271
272 config_add_string acct_server
273 config_add_string acct_secret
274 config_add_int acct_port
275 config_add_int acct_interval
276
277 config_add_int bss_load_update_period chan_util_avg_period
278
279 config_add_string dae_client
280 config_add_string dae_secret
281 config_add_int dae_port
282
283 config_add_string nasid
284 config_add_string ownip
285 config_add_string radius_client_addr
286 config_add_string iapp_interface
287 config_add_string eap_type ca_cert client_cert identity anonymous_identity auth priv_key priv_key_pwd
288 config_add_boolean ca_cert_usesystem ca_cert2_usesystem
289 config_add_string subject_match subject_match2
290 config_add_array altsubject_match altsubject_match2
291 config_add_array domain_match domain_match2 domain_suffix_match domain_suffix_match2
292 config_add_string ieee80211w_mgmt_cipher
293
294 config_add_int dynamic_vlan vlan_naming vlan_no_bridge
295 config_add_string vlan_tagged_interface vlan_bridge
296 config_add_string vlan_file
297
298 config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
299
300 config_add_string wpa_psk_file
301
302 config_add_int multi_ap
303
304 config_add_boolean wps_pushbutton wps_label ext_registrar wps_pbc_in_m1
305 config_add_int wps_ap_setup_locked wps_independent
306 config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
307 config_add_string multi_ap_backhaul_ssid multi_ap_backhaul_key
308
309 config_add_boolean wnm_sleep_mode wnm_sleep_mode_no_keys bss_transition mbo
310 config_add_int time_advertisement
311 config_add_string time_zone
312 config_add_string vendor_elements
313
314 config_add_boolean ieee80211k rrm_neighbor_report rrm_beacon_report
315
316 config_add_boolean ftm_responder stationary_ap
317 config_add_string lci civic
318
319 config_add_boolean ieee80211r pmk_r1_push ft_psk_generate_local ft_over_ds
320 config_add_int r0_key_lifetime reassociation_deadline
321 config_add_string mobility_domain r1_key_holder
322 config_add_array r0kh r1kh
323
324 config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
325
326 config_add_string macfilter 'macfile:file'
327 config_add_array 'maclist:list(macaddr)'
328
329 config_add_array bssid_blacklist
330 config_add_array bssid_whitelist
331
332 config_add_int mcast_rate
333 config_add_array basic_rate
334 config_add_array supported_rates
335
336 config_add_boolean sae_require_mfp
337 config_add_int sae_pwe
338
339 config_add_string 'owe_transition_bssid:macaddr' 'owe_transition_ssid:string'
340 config_add_string owe_transition_ifname
341
342 config_add_boolean iw_enabled iw_internet iw_asra iw_esr iw_uesa
343 config_add_int iw_access_network_type iw_venue_group iw_venue_type
344 config_add_int iw_ipaddr_type_availability iw_gas_address3
345 config_add_string iw_hessid iw_network_auth_type iw_qos_map_set
346 config_add_array iw_roaming_consortium iw_domain_name iw_anqp_3gpp_cell_net iw_nai_realm
347 config_add_array iw_anqp_elem iw_venue_name iw_venue_url
348
349 config_add_boolean hs20 disable_dgaf osen
350 config_add_int anqp_domain_id
351 config_add_int hs20_deauth_req_timeout
352 config_add_array hs20_oper_friendly_name
353 config_add_array osu_provider
354 config_add_array operator_icon
355 config_add_array hs20_conn_capab
356 config_add_string osu_ssid hs20_wan_metrics hs20_operating_class hs20_t_c_filename hs20_t_c_timestamp
357
358 config_add_string hs20_t_c_server_url
359
360 config_add_array airtime_sta_weight
361 config_add_int airtime_bss_weight airtime_bss_limit
362
363 config_add_boolean multicast_to_unicast multicast_to_unicast_all proxy_arp per_sta_vif
364
365 config_add_array hostapd_bss_options
366 config_add_boolean default_disabled
367
368 config_add_boolean request_cui
369 config_add_array radius_auth_req_attr
370 config_add_array radius_acct_req_attr
371
372 config_add_int eap_server
373 config_add_string eap_user_file ca_cert server_cert private_key private_key_passwd server_id
374
375 config_add_boolean fils
376 config_add_string fils_dhcp
377
378 config_add_int ocv
379}
380
381hostapd_set_vlan_file() {
382 local ifname="$1"
383 local vlan="$2"
384 json_get_vars name vid
385 echo "${vid} ${ifname}-${name}" >> /var/run/hostapd-${ifname}.vlan
386 wireless_add_vlan "${vlan}" "${ifname}-${name}"
387}
388
389hostapd_set_vlan() {
390 local ifname="$1"
391
392 rm -f /var/run/hostapd-${ifname}.vlan
393 for_each_vlan hostapd_set_vlan_file ${ifname}
394}
395
396hostapd_set_psk_file() {
397 local ifname="$1"
398 local vlan="$2"
399 local vlan_id=""
400
401 json_get_vars mac vid key
402 set_default mac "00:00:00:00:00:00"
403 [ -n "$vid" ] && vlan_id="vlanid=$vid "
404 echo "${vlan_id} ${mac} ${key}" >> /var/run/hostapd-${ifname}.psk
405}
406
407hostapd_set_psk() {
408 local ifname="$1"
409
410 rm -f /var/run/hostapd-${ifname}.psk
411 for_each_station hostapd_set_psk_file ${ifname}
412}
413
414append_iw_roaming_consortium() {
415 [ -n "$1" ] && append bss_conf "roaming_consortium=$1" "$N"
416}
417
418append_iw_domain_name() {
419 if [ -z "$iw_domain_name_conf" ]; then
420 iw_domain_name_conf="$1"
421 else
422 iw_domain_name_conf="$iw_domain_name_conf,$1"
423 fi
424}
425
426append_iw_anqp_3gpp_cell_net() {
427 if [ -z "$iw_anqp_3gpp_cell_net_conf" ]; then
428 iw_anqp_3gpp_cell_net_conf="$1"
429 else
430 iw_anqp_3gpp_cell_net_conf="$iw_anqp_3gpp_cell_net_conf:$1"
431 fi
432}
433
434append_iw_anqp_elem() {
435 [ -n "$1" ] && append bss_conf "anqp_elem=$1" "$N"
436}
437
438append_iw_nai_realm() {
439 [ -n "$1" ] && append bss_conf "nai_realm=$1" "$N"
440}
441
442append_iw_venue_name() {
443 append bss_conf "venue_name=$1" "$N"
444}
445
446append_iw_venue_url() {
447 append bss_conf "venue_url=$1" "$N"
448}
449
450append_hs20_oper_friendly_name() {
451 append bss_conf "hs20_oper_friendly_name=$1" "$N"
452}
453
454append_osu_provider_friendly_name() {
455 append bss_conf "osu_friendly_name=$1" "$N"
456}
457
458append_osu_provider_service_desc() {
459 append bss_conf "osu_service_desc=$1" "$N"
460}
461
462append_hs20_icon() {
463 local width height lang type path
464 config_get width "$1" width
465 config_get height "$1" height
466 config_get lang "$1" lang
467 config_get type "$1" type
468 config_get path "$1" path
469
470 append bss_conf "hs20_icon=$width:$height:$lang:$type:$1:$path" "$N"
471}
472
473append_hs20_icons() {
474 config_load wireless
475 config_foreach append_hs20_icon hs20-icon
476}
477
478append_operator_icon() {
479 append bss_conf "operator_icon=$1" "$N"
480}
481
482append_osu_icon() {
483 append bss_conf "osu_icon=$1" "$N"
484}
485
486append_osu_provider() {
487 local cfgtype osu_server_uri osu_friendly_name osu_nai osu_nai2 osu_method_list
488
489 config_load wireless
490 config_get cfgtype "$1" TYPE
491 [ "$cfgtype" != "osu-provider" ] && return
492
493 append bss_conf "# provider $1" "$N"
494 config_get osu_server_uri "$1" osu_server_uri
495 config_get osu_nai "$1" osu_nai
496 config_get osu_nai2 "$1" osu_nai2
497 config_get osu_method_list "$1" osu_method
498
499 append bss_conf "osu_server_uri=$osu_server_uri" "$N"
500 append bss_conf "osu_nai=$osu_nai" "$N"
501 append bss_conf "osu_nai2=$osu_nai2" "$N"
502 append bss_conf "osu_method_list=$osu_method_list" "$N"
503
504 config_list_foreach "$1" osu_service_desc append_osu_provider_service_desc
505 config_list_foreach "$1" osu_friendly_name append_osu_friendly_name
506 config_list_foreach "$1" osu_icon append_osu_icon
507
508 append bss_conf "$N"
509}
510
511append_hs20_conn_capab() {
512 [ -n "$1" ] && append bss_conf "hs20_conn_capab=$1" "$N"
513}
514
515append_radius_acct_req_attr() {
516 [ -n "$1" ] && append bss_conf "radius_acct_req_attr=$1" "$N"
517}
518
519append_radius_auth_req_attr() {
520 [ -n "$1" ] && append bss_conf "radius_auth_req_attr=$1" "$N"
521}
522
523append_airtime_sta_weight() {
524 [ -n "$1" ] && append bss_conf "airtime_sta_weight=$1" "$N"
525}
526
527hostapd_set_bss_options() {
528 local var="$1"
529 local phy="$2"
530 local vif="$3"
531
532 wireless_vif_parse_encryption
533
534 local bss_conf bss_md5sum ft_key
535 local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wpa_key_mgmt
536
537 json_get_vars \
538 wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wpa_strict_rekey \
539 wpa_disable_eapol_key_retries tdls_prohibit \
540 maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
541 wps_pushbutton wps_label ext_registrar wps_pbc_in_m1 wps_ap_setup_locked \
542 wps_independent wps_device_type wps_device_name wps_manufacturer wps_pin \
543 macfilter ssid utf8_ssid wmm uapsd hidden short_preamble rsn_preauth \
544 iapp_interface eapol_version dynamic_vlan ieee80211w nasid \
545 acct_server acct_secret acct_port acct_interval \
546 bss_load_update_period chan_util_avg_period sae_require_mfp sae_pwe \
547 multi_ap multi_ap_backhaul_ssid multi_ap_backhaul_key skip_inactivity_poll \
548 ppsk airtime_bss_weight airtime_bss_limit airtime_sta_weight \
549 multicast_to_unicast_all proxy_arp per_sta_vif \
550 eap_server eap_user_file ca_cert server_cert private_key private_key_passwd server_id \
551 vendor_elements fils ocv
552
553 set_default fils 0
554 set_default isolate 0
555 set_default maxassoc 0
556 set_default max_inactivity 0
557 set_default short_preamble 1
558 set_default disassoc_low_ack 1
559 set_default skip_inactivity_poll 0
560 set_default hidden 0
561 set_default wmm 1
562 set_default uapsd 1
563 set_default wpa_disable_eapol_key_retries 0
564 set_default tdls_prohibit 0
565 set_default eapol_version $((wpa & 1))
566 set_default acct_port 1813
567 set_default bss_load_update_period 60
568 set_default chan_util_avg_period 600
569 set_default utf8_ssid 1
570 set_default multi_ap 0
571 set_default ppsk 0
572 set_default airtime_bss_weight 0
573 set_default airtime_bss_limit 0
574 set_default eap_server 0
575
576 /usr/sbin/hostapd -vfils || fils=0
577
578 append bss_conf "ctrl_interface=/var/run/hostapd"
579 if [ "$isolate" -gt 0 ]; then
580 append bss_conf "ap_isolate=$isolate" "$N"
581 fi
582 if [ "$maxassoc" -gt 0 ]; then
583 append bss_conf "max_num_sta=$maxassoc" "$N"
584 fi
585 if [ "$max_inactivity" -gt 0 ]; then
586 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
587 fi
588
589 [ "$airtime_bss_weight" -gt 0 ] && append bss_conf "airtime_bss_weight=$airtime_bss_weight" "$N"
590 [ "$airtime_bss_limit" -gt 0 ] && append bss_conf "airtime_bss_limit=$airtime_bss_limit" "$N"
591 json_for_each_item append_airtime_sta_weight airtime_sta_weight
592
593 append bss_conf "bss_load_update_period=$bss_load_update_period" "$N"
594 append bss_conf "chan_util_avg_period=$chan_util_avg_period" "$N"
595 append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
596 append bss_conf "skip_inactivity_poll=$skip_inactivity_poll" "$N"
597 append bss_conf "preamble=$short_preamble" "$N"
598 append bss_conf "wmm_enabled=$wmm" "$N"
599 append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
600 append bss_conf "uapsd_advertisement_enabled=$uapsd" "$N"
601 append bss_conf "utf8_ssid=$utf8_ssid" "$N"
602 append bss_conf "multi_ap=$multi_ap" "$N"
603 [ -n "$vendor_elements" ] && append bss_conf "vendor_elements=$vendor_elements" "$N"
604
605 [ "$tdls_prohibit" -gt 0 ] && append bss_conf "tdls_prohibit=$tdls_prohibit" "$N"
606
607 [ "$wpa" -gt 0 ] && {
608 [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
609 [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
610 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
611 [ -n "$wpa_strict_rekey" ] && append bss_conf "wpa_strict_rekey=$wpa_strict_rekey" "$N"
612 }
613
developerc5ce7502022-12-19 11:33:22 +0800614 set_default nasid "${macaddr//\:}"
615 append bss_conf "nas_identifier=$nasid" "$N"
616
developer8556ff22022-11-10 11:58:51 +0800617 [ -n "$acct_server" ] && {
618 append bss_conf "acct_server_addr=$acct_server" "$N"
619 append bss_conf "acct_server_port=$acct_port" "$N"
620 [ -n "$acct_secret" ] && \
621 append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
622 [ -n "$acct_interval" ] && \
623 append bss_conf "radius_acct_interim_interval=$acct_interval" "$N"
624 json_for_each_item append_radius_acct_req_attr radius_acct_req_attr
625 }
626
627 [ -n "$ocv" ] && append bss_conf "ocv=$ocv" "$N"
628
629 case "$auth_type" in
630 sae|owe|eap192|eap-eap192)
631 set_default ieee80211w 2
632 set_default sae_require_mfp 1
633 set_default sae_pwe 2
634 ;;
635 psk-sae)
636 set_default ieee80211w 1
637 set_default sae_require_mfp 1
638 set_default sae_pwe 2
639 ;;
640 esac
641 [ -n "$sae_require_mfp" ] && append bss_conf "sae_require_mfp=$sae_require_mfp" "$N"
642 [ -n "$sae_pwe" ] && append bss_conf "sae_pwe=$sae_pwe" "$N"
643
644 local vlan_possible=""
645
646 case "$auth_type" in
647 none|owe)
648 json_get_vars owe_transition_bssid owe_transition_ssid owe_transition_ifname
649
650 [ -n "$owe_transition_ssid" ] && append bss_conf "owe_transition_ssid=\"$owe_transition_ssid\"" "$N"
651 [ -n "$owe_transition_bssid" ] && append bss_conf "owe_transition_bssid=$owe_transition_bssid" "$N"
652 [ -n "$owe_transition_ifname" ] && append bss_conf "owe_transition_ifname=$owe_transition_ifname" "$N"
653
654 wps_possible=1
655 # Here we make the assumption that if we're in open mode
656 # with WPS enabled, we got to be in unconfigured state.
657 wps_not_configured=1
658 ;;
659 psk|sae|psk-sae)
660 json_get_vars key wpa_psk_file
661 if [ "$auth_type" = "psk" ] && [ "$ppsk" -ne 0 ] ; then
662 json_get_vars auth_server auth_secret auth_port
663 set_default auth_port 1812
664 append bss_conf "auth_server_addr=$auth_server" "$N"
665 append bss_conf "auth_server_port=$auth_port" "$N"
666 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
667 append bss_conf "macaddr_acl=2" "$N"
668 append bss_conf "wpa_psk_radius=2" "$N"
669 elif [ ${#key} -eq 64 ]; then
670 append bss_conf "wpa_psk=$key" "$N"
671 elif [ ${#key} -ge 8 ] && [ ${#key} -le 63 ]; then
672 append bss_conf "wpa_passphrase=$key" "$N"
673 elif [ -n "$key" ] || [ -z "$wpa_psk_file" ]; then
674 wireless_setup_vif_failed INVALID_WPA_PSK
675 return 1
676 fi
677 [ -z "$wpa_psk_file" ] && set_default wpa_psk_file /var/run/hostapd-$ifname.psk
678 [ -n "$wpa_psk_file" ] && {
679 [ -e "$wpa_psk_file" ] || touch "$wpa_psk_file"
680 append bss_conf "wpa_psk_file=$wpa_psk_file" "$N"
681 }
682 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
683
684 set_default dynamic_vlan 0
685 vlan_possible=1
686 wps_possible=1
687 ;;
688 eap|eap192|eap-eap192)
689 json_get_vars \
690 auth_server auth_secret auth_port \
691 dae_client dae_secret dae_port \
developerc5ce7502022-12-19 11:33:22 +0800692 dynamic_ownip ownip radius_client_addr \
developer8556ff22022-11-10 11:58:51 +0800693 eap_reauth_period request_cui \
694 erp_domain mobility_domain \
695 fils_realm fils_dhcp
696
697 # radius can provide VLAN ID for clients
698 vlan_possible=1
699
developerc5ce7502022-12-19 11:33:22 +0800700 set_default dynamic_ownip 1
701
developer8556ff22022-11-10 11:58:51 +0800702 # legacy compatibility
703 [ -n "$auth_server" ] || json_get_var auth_server server
704 [ -n "$auth_port" ] || json_get_var auth_port port
705 [ -n "$auth_secret" ] || json_get_var auth_secret key
706
707 [ "$fils" -gt 0 ] && {
708 set_default erp_domain "$mobility_domain"
709 set_default erp_domain "$(echo "$ssid" | md5sum | head -c 8)"
710 set_default fils_realm "$erp_domain"
711
712 append bss_conf "erp_send_reauth_start=1" "$N"
713 append bss_conf "erp_domain=$erp_domain" "$N"
714 append bss_conf "fils_realm=$fils_realm" "$N"
715 append bss_conf "fils_cache_id=$(echo "$fils_realm" | md5sum | head -c 4)" "$N"
716
717 [ "$fils_dhcp" = "*" ] && {
718 json_get_values network network
719 fils_dhcp=
720 for net in $network; do
721 fils_dhcp="$(ifstatus "$net" | jsonfilter -e '@.data.dhcpserver')"
722 [ -n "$fils_dhcp" ] && break
723 done
724
725 [ -z "$fils_dhcp" -a -n "$network_bridge" -a -n "$network_ifname" ] && \
726 fils_dhcp="$(udhcpc -B -n -q -s /lib/netifd/dhcp-get-server.sh -t 1 -i "$network_ifname" 2>/dev/null)"
727 }
728 [ -n "$fils_dhcp" ] && append bss_conf "dhcp_server=$fils_dhcp" "$N"
729 }
730
731 set_default auth_port 1812
732 set_default dae_port 3799
733 set_default request_cui 0
734
735 [ "$eap_server" -eq 0 ] && {
736 append bss_conf "auth_server_addr=$auth_server" "$N"
737 append bss_conf "auth_server_port=$auth_port" "$N"
738 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
739 }
740
741 [ "$request_cui" -gt 0 ] && append bss_conf "radius_request_cui=$request_cui" "$N"
742 [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
743
744 [ -n "$dae_client" -a -n "$dae_secret" ] && {
745 append bss_conf "radius_das_port=$dae_port" "$N"
746 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
747 }
748 json_for_each_item append_radius_auth_req_attr radius_auth_req_attr
749
developerc5ce7502022-12-19 11:33:22 +0800750 if [ -n "$ownip" ]; then
751 append bss_conf "own_ip_addr=$ownip" "$N"
752 elif [ "$dynamic_ownip" -gt 0 ]; then
753 append bss_conf "dynamic_own_ip_addr=$dynamic_ownip" "$N"
754 fi
755
developer8556ff22022-11-10 11:58:51 +0800756 [ -n "$radius_client_addr" ] && append bss_conf "radius_client_addr=$radius_client_addr" "$N"
757 append bss_conf "eapol_key_index_workaround=1" "$N"
758 append bss_conf "ieee8021x=1" "$N"
759
760 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
761 ;;
762 wep)
763 local wep_keyidx=0
764 json_get_vars key
765 hostapd_append_wep_key bss_conf
766 append bss_conf "wep_default_key=$wep_keyidx" "$N"
767 [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
768 ;;
769 esac
770
771 case "$auth_type" in
772 none|owe|psk|sae|psk-sae|wep)
773 json_get_vars \
774 auth_server auth_port auth_secret \
775 ownip radius_client_addr
776
777 [ -n "$auth_server" ] && {
778 set_default auth_port 1812
779
780 append bss_conf "auth_server_addr=$auth_server" "$N"
781 append bss_conf "auth_server_port=$auth_port" "$N"
782 [ -n "$auth_secret" ] && append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
783 [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
784 [ -n "$radius_client_addr" ] && append bss_conf "radius_client_addr=$radius_client_addr" "$N"
785 append bss_conf "macaddr_acl=2" "$N"
786 }
787 ;;
788 esac
789
developer2c78ce72023-02-24 11:26:12 +0800790 local auth_algs="$((($auth_mode_shared << 1) | $auth_mode_open))"
developer8556ff22022-11-10 11:58:51 +0800791 append bss_conf "auth_algs=${auth_algs:-1}" "$N"
792 append bss_conf "wpa=$wpa" "$N"
793 [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
794
795 set_default wps_pushbutton 0
796 set_default wps_label 0
797 set_default wps_pbc_in_m1 0
798
799 config_methods=
800 [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
801 [ "$wps_label" -gt 0 ] && append config_methods label
802
803 # WPS not possible on Multi-AP backhaul-only SSID
804 [ "$multi_ap" = 1 ] && wps_possible=
805
806 [ -n "$wps_possible" -a -n "$config_methods" ] && {
807 set_default ext_registrar 0
808 set_default wps_device_type "6-0050F204-1"
809 set_default wps_device_name "OpenWrt AP"
810 set_default wps_manufacturer "www.openwrt.org"
811 set_default wps_independent 1
812
813 wps_state=2
814 [ -n "$wps_not_configured" ] && wps_state=1
815
816 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
817
818 append bss_conf "eap_server=1" "$N"
819 [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
820 append bss_conf "wps_state=$wps_state" "$N"
821 append bss_conf "device_type=$wps_device_type" "$N"
822 append bss_conf "device_name=$wps_device_name" "$N"
823 append bss_conf "manufacturer=$wps_manufacturer" "$N"
824 append bss_conf "config_methods=$config_methods" "$N"
825 append bss_conf "wps_independent=$wps_independent" "$N"
826 [ -n "$wps_ap_setup_locked" ] && append bss_conf "ap_setup_locked=$wps_ap_setup_locked" "$N"
827 [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
828 [ "$multi_ap" -gt 0 ] && [ -n "$multi_ap_backhaul_ssid" ] && {
829 append bss_conf "multi_ap_backhaul_ssid=\"$multi_ap_backhaul_ssid\"" "$N"
830 if [ -z "$multi_ap_backhaul_key" ]; then
831 :
832 elif [ ${#multi_ap_backhaul_key} -lt 8 ]; then
833 wireless_setup_vif_failed INVALID_WPA_PSK
834 return 1
835 elif [ ${#multi_ap_backhaul_key} -eq 64 ]; then
836 append bss_conf "multi_ap_backhaul_wpa_psk=$multi_ap_backhaul_key" "$N"
837 else
838 append bss_conf "multi_ap_backhaul_wpa_passphrase=$multi_ap_backhaul_key" "$N"
839 fi
840 }
841 }
842
843 append bss_conf "ssid=$ssid" "$N"
844 [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge${N}wds_bridge=" "$N"
845 [ -n "$network_ifname" ] && append bss_conf "snoop_iface=$network_ifname" "$N"
846 [ -n "$iapp_interface" ] && {
847 local ifname
848 network_get_device ifname "$iapp_interface" || ifname="$iapp_interface"
849 append bss_conf "iapp_interface=$ifname" "$N"
850 }
851
852 json_get_vars time_advertisement time_zone wnm_sleep_mode wnm_sleep_mode_no_keys bss_transition mbo
853 set_default bss_transition 0
854 set_default wnm_sleep_mode 0
855 set_default wnm_sleep_mode_no_keys 0
856 set_default mbo 0
857
858 [ -n "$time_advertisement" ] && append bss_conf "time_advertisement=$time_advertisement" "$N"
859 [ -n "$time_zone" ] && append bss_conf "time_zone=$time_zone" "$N"
860 if [ "$wnm_sleep_mode" -eq "1" ]; then
861 append bss_conf "wnm_sleep_mode=1" "$N"
862 [ "$wnm_sleep_mode_no_keys" -eq "1" ] && append bss_conf "wnm_sleep_mode_no_keys=1" "$N"
863 fi
864 [ "$bss_transition" -eq "1" ] && append bss_conf "bss_transition=1" "$N"
865 [ "$mbo" -eq 1 ] && append bss_conf "mbo=1" "$N"
866
867 json_get_vars ieee80211k rrm_neighbor_report rrm_beacon_report
868 set_default ieee80211k 0
869 if [ "$ieee80211k" -eq "1" ]; then
870 set_default rrm_neighbor_report 1
871 set_default rrm_beacon_report 1
872 else
873 set_default rrm_neighbor_report 0
874 set_default rrm_beacon_report 0
875 fi
876
877 [ "$rrm_neighbor_report" -eq "1" ] && append bss_conf "rrm_neighbor_report=1" "$N"
878 [ "$rrm_beacon_report" -eq "1" ] && append bss_conf "rrm_beacon_report=1" "$N"
879
880 json_get_vars ftm_responder stationary_ap lci civic
881 set_default ftm_responder 0
882 if [ "$ftm_responder" -eq "1" ]; then
883 set_default stationary_ap 0
884 iw phy "$phy" info | grep -q "ENABLE_FTM_RESPONDER" && {
885 append bss_conf "ftm_responder=1" "$N"
886 [ "$stationary_ap" -eq "1" ] && append bss_conf "stationary_ap=1" "$N"
887 [ -n "$lci" ] && append bss_conf "lci=$lci" "$N"
888 [ -n "$civic" ] && append bss_conf "civic=$civic" "$N"
889 }
890 fi
891
892 if [ "$wpa" -ge "1" ]; then
893 json_get_vars ieee80211r
894 set_default ieee80211r 0
895
896 if [ "$ieee80211r" -gt "0" ]; then
897 json_get_vars mobility_domain ft_psk_generate_local ft_over_ds reassociation_deadline
898
899 set_default mobility_domain "$(echo "$ssid" | md5sum | head -c 4)"
900 set_default ft_over_ds 0
901 set_default reassociation_deadline 1000
902
903 case "$auth_type" in
904 psk|sae|psk-sae)
905 set_default ft_psk_generate_local 1
906 ;;
907 *)
908 set_default ft_psk_generate_local 0
909 ;;
910 esac
911
912 [ -n "$network_ifname" ] && append bss_conf "ft_iface=$network_ifname" "$N"
913 append bss_conf "mobility_domain=$mobility_domain" "$N"
914 append bss_conf "ft_psk_generate_local=$ft_psk_generate_local" "$N"
915 append bss_conf "ft_over_ds=$ft_over_ds" "$N"
916 append bss_conf "reassociation_deadline=$reassociation_deadline" "$N"
developer8556ff22022-11-10 11:58:51 +0800917
918 if [ "$ft_psk_generate_local" -eq "0" ]; then
919 json_get_vars r0_key_lifetime r1_key_holder pmk_r1_push
920 json_get_values r0kh r0kh
921 json_get_values r1kh r1kh
922
923 set_default r0_key_lifetime 10000
924 set_default pmk_r1_push 0
925
926 [ -n "$r0kh" -a -n "$r1kh" ] || {
927 ft_key=`echo -n "$mobility_domain/${auth_secret:-${key}}" | md5sum | awk '{print $1}'`
928
929 set_default r0kh "ff:ff:ff:ff:ff:ff,*,$ft_key"
930 set_default r1kh "00:00:00:00:00:00,00:00:00:00:00:00,$ft_key"
931 }
932
933 [ -n "$r1_key_holder" ] && append bss_conf "r1_key_holder=$r1_key_holder" "$N"
934 append bss_conf "r0_key_lifetime=$r0_key_lifetime" "$N"
935 append bss_conf "pmk_r1_push=$pmk_r1_push" "$N"
936
937 for kh in $r0kh; do
938 append bss_conf "r0kh=${kh//,/ }" "$N"
939 done
940 for kh in $r1kh; do
941 append bss_conf "r1kh=${kh//,/ }" "$N"
942 done
943 fi
944 fi
945 if [ "$fils" -gt 0 ]; then
946 json_get_vars fils_realm
947 set_default fils_realm "$(echo "$ssid" | md5sum | head -c 8)"
948 fi
949
950 append bss_conf "wpa_disable_eapol_key_retries=$wpa_disable_eapol_key_retries" "$N"
951
952 hostapd_append_wpa_key_mgmt
953 [ -n "$wpa_key_mgmt" ] && append bss_conf "wpa_key_mgmt=$wpa_key_mgmt" "$N"
954 fi
955
956 if [ "$wpa" -ge "2" ]; then
957 if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
958 set_default auth_cache 1
959 append bss_conf "rsn_preauth=1" "$N"
960 append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
961 else
962 case "$auth_type" in
963 sae|psk-sae|owe)
964 set_default auth_cache 1
965 ;;
966 *)
967 set_default auth_cache 0
968 ;;
969 esac
970 fi
971
972 append bss_conf "okc=$auth_cache" "$N"
973 [ "$auth_cache" = 0 -a "$fils" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
974
975 # RSN -> allow management frame protection
976 case "$ieee80211w" in
977 [012])
978 json_get_vars ieee80211w_mgmt_cipher ieee80211w_max_timeout ieee80211w_retry_timeout
979 append bss_conf "ieee80211w=$ieee80211w" "$N"
980 [ "$ieee80211w" -gt "0" ] && {
981 if [ "$auth_type" = "eap192" ]; then
982 append bss_conf "group_mgmt_cipher=BIP-GMAC-256" "$N"
983 else
984 append bss_conf "group_mgmt_cipher=${ieee80211w_mgmt_cipher:-AES-128-CMAC}" "$N"
985 fi
986 [ -n "$ieee80211w_max_timeout" ] && \
987 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
988 [ -n "$ieee80211w_retry_timeout" ] && \
989 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
990 }
991 ;;
992 esac
993 fi
994
995 _macfile="/var/run/hostapd-$ifname.maclist"
996 case "$macfilter" in
997 allow)
998 append bss_conf "macaddr_acl=1" "$N"
999 append bss_conf "accept_mac_file=$_macfile" "$N"
1000 # accept_mac_file can be used to set MAC to VLAN ID mapping
1001 vlan_possible=1
1002 ;;
1003 deny)
1004 append bss_conf "macaddr_acl=0" "$N"
1005 append bss_conf "deny_mac_file=$_macfile" "$N"
1006 ;;
1007 *)
1008 _macfile=""
1009 ;;
1010 esac
1011
1012 [ -n "$_macfile" ] && {
1013 json_get_vars macfile
1014 json_get_values maclist maclist
1015
1016 rm -f "$_macfile"
1017 (
1018 for mac in $maclist; do
1019 echo "$mac"
1020 done
1021 [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
1022 ) > "$_macfile"
1023 }
1024
1025 [ -n "$vlan_possible" -a -n "$dynamic_vlan" ] && {
1026 json_get_vars vlan_naming vlan_tagged_interface vlan_bridge vlan_file vlan_no_bridge
1027 set_default vlan_naming 1
1028 [ -z "$vlan_file" ] && set_default vlan_file /var/run/hostapd-$ifname.vlan
1029 append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
1030 append bss_conf "vlan_naming=$vlan_naming" "$N"
1031 if [ -n "$vlan_bridge" ]; then
1032 append bss_conf "vlan_bridge=$vlan_bridge" "$N"
1033 else
1034 set_default vlan_no_bridge 1
1035 fi
1036 append bss_conf "vlan_no_bridge=$vlan_no_bridge" "$N"
1037 [ -n "$vlan_tagged_interface" ] && \
1038 append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
1039 [ -n "$vlan_file" ] && {
1040 [ -e "$vlan_file" ] || touch "$vlan_file"
1041 append bss_conf "vlan_file=$vlan_file" "$N"
1042 }
1043 }
1044
1045 json_get_vars iw_enabled iw_internet iw_asra iw_esr iw_uesa iw_access_network_type
1046 json_get_vars iw_hessid iw_venue_group iw_venue_type iw_network_auth_type
1047 json_get_vars iw_roaming_consortium iw_domain_name iw_anqp_3gpp_cell_net iw_nai_realm
1048 json_get_vars iw_anqp_elem iw_qos_map_set iw_ipaddr_type_availability iw_gas_address3
1049 json_get_vars iw_venue_name iw_venue_url
1050
1051 set_default iw_enabled 0
1052 if [ "$iw_enabled" = "1" ]; then
1053 append bss_conf "interworking=1" "$N"
1054 set_default iw_internet 1
1055 set_default iw_asra 0
1056 set_default iw_esr 0
1057 set_default iw_uesa 0
1058
1059 append bss_conf "internet=$iw_internet" "$N"
1060 append bss_conf "asra=$iw_asra" "$N"
1061 append bss_conf "esr=$iw_esr" "$N"
1062 append bss_conf "uesa=$iw_uesa" "$N"
1063
1064 [ -n "$iw_access_network_type" ] && \
1065 append bss_conf "access_network_type=$iw_access_network_type" "$N"
1066 [ -n "$iw_hessid" ] && append bss_conf "hessid=$iw_hessid" "$N"
1067 [ -n "$iw_venue_group" ] && \
1068 append bss_conf "venue_group=$iw_venue_group" "$N"
1069 [ -n "$iw_venue_type" ] && append bss_conf "venue_type=$iw_venue_type" "$N"
1070 [ -n "$iw_network_auth_type" ] && \
1071 append bss_conf "network_auth_type=$iw_network_auth_type" "$N"
1072 [ -n "$iw_gas_address3" ] && append bss_conf "gas_address3=$iw_gas_address3" "$N"
1073
1074 json_for_each_item append_iw_roaming_consortium iw_roaming_consortium
1075 json_for_each_item append_iw_anqp_elem iw_anqp_elem
1076 json_for_each_item append_iw_nai_realm iw_nai_realm
1077 json_for_each_item append_iw_venue_name iw_venue_name
1078 json_for_each_item append_iw_venue_url iw_venue_url
1079
1080 iw_domain_name_conf=
1081 json_for_each_item append_iw_domain_name iw_domain_name
1082 [ -n "$iw_domain_name_conf" ] && \
1083 append bss_conf "domain_name=$iw_domain_name_conf" "$N"
1084
1085 iw_anqp_3gpp_cell_net_conf=
1086 json_for_each_item append_iw_anqp_3gpp_cell_net iw_anqp_3gpp_cell_net
1087 [ -n "$iw_anqp_3gpp_cell_net_conf" ] && \
1088 append bss_conf "anqp_3gpp_cell_net=$iw_anqp_3gpp_cell_net_conf" "$N"
1089 fi
1090
1091 set_default iw_qos_map_set 0,0,2,16,1,1,255,255,18,22,24,38,40,40,44,46,48,56
1092 case "$iw_qos_map_set" in
1093 *,*);;
1094 *) iw_qos_map_set="";;
1095 esac
1096 [ -n "$iw_qos_map_set" ] && append bss_conf "qos_map_set=$iw_qos_map_set" "$N"
1097
1098 local hs20 disable_dgaf osen anqp_domain_id hs20_deauth_req_timeout \
1099 osu_ssid hs20_wan_metrics hs20_operating_class hs20_t_c_filename hs20_t_c_timestamp \
1100 hs20_t_c_server_url
1101 json_get_vars hs20 disable_dgaf osen anqp_domain_id hs20_deauth_req_timeout \
1102 osu_ssid hs20_wan_metrics hs20_operating_class hs20_t_c_filename hs20_t_c_timestamp \
1103 hs20_t_c_server_url
1104
1105 set_default hs20 0
1106 set_default disable_dgaf $hs20
1107 set_default osen 0
1108 set_default anqp_domain_id 0
1109 set_default hs20_deauth_req_timeout 60
1110 if [ "$hs20" = "1" ]; then
1111 append bss_conf "hs20=1" "$N"
1112 append_hs20_icons
1113 append bss_conf "disable_dgaf=$disable_dgaf" "$N"
1114 append bss_conf "osen=$osen" "$N"
1115 append bss_conf "anqp_domain_id=$anqp_domain_id" "$N"
1116 append bss_conf "hs20_deauth_req_timeout=$hs20_deauth_req_timeout" "$N"
1117 [ -n "$osu_ssid" ] && append bss_conf "osu_ssid=$osu_ssid" "$N"
1118 [ -n "$hs20_wan_metrics" ] && append bss_conf "hs20_wan_metrics=$hs20_wan_metrics" "$N"
1119 [ -n "$hs20_operating_class" ] && append bss_conf "hs20_operating_class=$hs20_operating_class" "$N"
1120 [ -n "$hs20_t_c_filename" ] && append bss_conf "hs20_t_c_filename=$hs20_t_c_filename" "$N"
1121 [ -n "$hs20_t_c_timestamp" ] && append bss_conf "hs20_t_c_timestamp=$hs20_t_c_timestamp" "$N"
1122 [ -n "$hs20_t_c_server_url" ] && append bss_conf "hs20_t_c_server_url=$hs20_t_c_server_url" "$N"
1123 json_for_each_item append_hs20_oper_friendly_name hs20_oper_friendly_name
1124 json_for_each_item append_hs20_conn_capab hs20_conn_capab
1125 json_for_each_item append_osu_provider osu_provider
1126 json_for_each_item append_operator_icon operator_icon
1127 fi
1128
1129 if [ "$eap_server" = "1" ]; then
1130 append bss_conf "eap_server=1" "$N"
1131 append bss_conf "eap_server_erp=1" "$N"
1132 [ -n "$eap_user_file" ] && append bss_conf "eap_user_file=$eap_user_file" "$N"
1133 [ -n "$ca_cert" ] && append bss_conf "ca_cert=$ca_cert" "$N"
1134 [ -n "$server_cert" ] && append bss_conf "server_cert=$server_cert" "$N"
1135 [ -n "$private_key" ] && append bss_conf "private_key=$private_key" "$N"
1136 [ -n "$private_key_passwd" ] && append bss_conf "private_key_passwd=$private_key_passwd" "$N"
1137 [ -n "$server_id" ] && append bss_conf "server_id=$server_id" "$N"
1138 fi
1139
1140 set_default multicast_to_unicast_all 0
1141 if [ "$multicast_to_unicast_all" -gt 0 ]; then
1142 append bss_conf "multicast_to_unicast=$multicast_to_unicast_all" "$N"
1143 fi
1144 set_default proxy_arp 0
1145 if [ "$proxy_arp" -gt 0 ]; then
1146 append bss_conf "proxy_arp=$proxy_arp" "$N"
1147 fi
1148
1149 set_default per_sta_vif 0
1150 if [ "$per_sta_vif" -gt 0 ]; then
1151 append bss_conf "per_sta_vif=$per_sta_vif" "$N"
1152 fi
1153
1154 json_get_values opts hostapd_bss_options
1155 for val in $opts; do
1156 append bss_conf "$val" "$N"
1157 done
1158
developer2c78ce72023-02-24 11:26:12 +08001159 bss_md5sum="$(echo $bss_conf | md5sum | cut -d" " -f1)"
developer8556ff22022-11-10 11:58:51 +08001160 append bss_conf "config_id=$bss_md5sum" "$N"
1161
1162 append "$var" "$bss_conf" "$N"
1163 return 0
1164}
1165
1166hostapd_set_log_options() {
1167 local var="$1"
1168
1169 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
1170 json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
1171
1172 set_default log_level 2
1173 set_default log_80211 1
1174 set_default log_8021x 1
1175 set_default log_radius 1
1176 set_default log_wpa 1
1177 set_default log_driver 1
1178 set_default log_iapp 1
1179 set_default log_mlme 1
1180
developer2c78ce72023-02-24 11:26:12 +08001181 local log_mask="$(( \
developer8556ff22022-11-10 11:58:51 +08001182 ($log_80211 << 0) | \
1183 ($log_8021x << 1) | \
1184 ($log_radius << 2) | \
1185 ($log_wpa << 3) | \
1186 ($log_driver << 4) | \
1187 ($log_iapp << 5) | \
1188 ($log_mlme << 6) \
developer2c78ce72023-02-24 11:26:12 +08001189 ))"
developer8556ff22022-11-10 11:58:51 +08001190
1191 append "$var" "logger_syslog=$log_mask" "$N"
1192 append "$var" "logger_syslog_level=$log_level" "$N"
1193 append "$var" "logger_stdout=$log_mask" "$N"
1194 append "$var" "logger_stdout_level=$log_level" "$N"
1195
1196 return 0
1197}
1198
1199_wpa_supplicant_common() {
1200 local ifname="$1"
1201
1202 _rpath="/var/run/wpa_supplicant"
1203 _config="${_rpath}-$ifname.conf"
1204}
1205
1206wpa_supplicant_teardown_interface() {
1207 _wpa_supplicant_common "$1"
1208 rm -rf "$_rpath/$1" "$_config"
1209}
1210
1211wpa_supplicant_prepare_interface() {
1212 local ifname="$1"
1213 _w_driver="$2"
1214
1215 _wpa_supplicant_common "$1"
1216
1217 json_get_vars mode wds multi_ap
1218
1219 [ -n "$network_bridge" ] && {
1220 fail=
1221 case "$mode" in
1222 adhoc)
1223 fail=1
1224 ;;
1225 sta)
1226 [ "$wds" = 1 -o "$multi_ap" = 1 ] || fail=1
1227 ;;
1228 esac
1229
1230 [ -n "$fail" ] && {
1231 wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
1232 return 1
1233 }
1234 }
1235
1236 local ap_scan=
1237
1238 _w_mode="$mode"
1239
1240 [ "$mode" = adhoc ] && {
1241 ap_scan="ap_scan=2"
1242 }
1243
1244 local country_str=
1245 [ -n "$country" ] && {
1246 country_str="country=$country"
1247 }
1248
1249 multiap_flag_file="${_config}.is_multiap"
1250 if [ "$multi_ap" = "1" ]; then
1251 touch "$multiap_flag_file"
1252 else
1253 [ -e "$multiap_flag_file" ] && rm "$multiap_flag_file"
1254 fi
1255 wpa_supplicant_teardown_interface "$ifname"
1256 cat > "$_config" <<EOF
1257${scan_list:+freq_list=$scan_list}
1258$ap_scan
1259$country_str
1260EOF
1261 return 0
1262}
1263
1264wpa_supplicant_set_fixed_freq() {
1265 local freq="$1"
1266 local htmode="$2"
1267
1268 append network_data "fixed_freq=1" "$N$T"
1269 append network_data "frequency=$freq" "$N$T"
1270 case "$htmode" in
1271 NOHT) append network_data "disable_ht=1" "$N$T";;
1272 HE20|HT20|VHT20) append network_data "disable_ht40=1" "$N$T";;
1273 HT40*|VHT40|VHT80|VHT160|HE40|HE80|HE160) append network_data "ht40=1" "$N$T";;
1274 esac
1275 case "$htmode" in
1276 VHT*) append network_data "vht=1" "$N$T";;
1277 esac
1278 case "$htmode" in
1279 HE80|VHT80) append network_data "max_oper_chwidth=1" "$N$T";;
1280 HE160|VHT160) append network_data "max_oper_chwidth=2" "$N$T";;
1281 HE20|HE40|VHT20|VHT40) append network_data "max_oper_chwidth=0" "$N$T";;
1282 *) append network_data "disable_vht=1" "$N$T";;
1283 esac
1284}
1285
1286wpa_supplicant_add_network() {
1287 local ifname="$1"
1288 local freq="$2"
1289 local htmode="$3"
1290 local noscan="$4"
1291
1292 _wpa_supplicant_common "$1"
1293 wireless_vif_parse_encryption
1294
1295 json_get_vars \
1296 ssid bssid key \
1297 basic_rate mcast_rate \
1298 ieee80211w ieee80211r fils ocv \
1299 multi_ap \
1300 default_disabled
1301
1302 case "$auth_type" in
1303 sae|owe|eap192|eap-eap192)
1304 set_default ieee80211w 2
1305 ;;
1306 psk-sae)
1307 set_default ieee80211w 1
1308 ;;
1309 esac
1310
1311 set_default ieee80211r 0
1312 set_default multi_ap 0
1313 set_default default_disabled 0
1314
1315 local key_mgmt='NONE'
1316 local network_data=
1317 local T=" "
1318
1319 local scan_ssid="scan_ssid=1"
1320 local freq wpa_key_mgmt
1321
1322 [ "$_w_mode" = "adhoc" ] && {
1323 append network_data "mode=1" "$N$T"
1324 [ -n "$freq" ] && wpa_supplicant_set_fixed_freq "$freq" "$htmode"
1325 [ "$noscan" = "1" ] && append network_data "noscan=1" "$N$T"
1326
1327 scan_ssid="scan_ssid=0"
1328
1329 [ "$_w_driver" = "nl80211" ] || append wpa_key_mgmt "WPA-NONE"
1330 }
1331
1332 [ "$_w_mode" = "mesh" ] && {
developer17bb0a82022-12-13 15:52:04 +08001333 json_get_vars mesh_id mesh_fwding mesh_rssi_threshold encryption
developer8556ff22022-11-10 11:58:51 +08001334 [ -n "$mesh_id" ] && ssid="${mesh_id}"
1335
1336 append network_data "mode=5" "$N$T"
1337 [ -n "$mesh_fwding" ] && append network_data "mesh_fwding=${mesh_fwding}" "$N$T"
1338 [ -n "$mesh_rssi_threshold" ] && append network_data "mesh_rssi_threshold=${mesh_rssi_threshold}" "$N$T"
1339 [ -n "$freq" ] && wpa_supplicant_set_fixed_freq "$freq" "$htmode"
1340 [ "$noscan" = "1" ] && append network_data "noscan=1" "$N$T"
developer17bb0a82022-12-13 15:52:04 +08001341 [ "$encryption" = "none" -o -z "$encryption" ] || append wpa_key_mgmt "SAE"
developer8556ff22022-11-10 11:58:51 +08001342 scan_ssid=""
1343 }
1344
1345 [ "$_w_mode" = "sta" ] && {
1346 [ "$multi_ap" = 1 ] && append network_data "multi_ap_backhaul_sta=1" "$N$T"
1347 [ "$default_disabled" = 1 ] && append network_data "disabled=1" "$N$T"
1348 }
1349
1350 [ -n "$ocv" ] && append network_data "ocv=$ocv" "$N$T"
1351
1352 case "$auth_type" in
1353 none) ;;
1354 owe)
1355 hostapd_append_wpa_key_mgmt
1356 key_mgmt="$wpa_key_mgmt"
1357 ;;
1358 wep)
1359 local wep_keyidx=0
1360 hostapd_append_wep_key network_data
1361 append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
1362 ;;
1363 wps)
1364 key_mgmt='WPS'
1365 ;;
1366 psk|sae|psk-sae)
1367 local passphrase
1368
1369 if [ "$_w_mode" != "mesh" ]; then
1370 hostapd_append_wpa_key_mgmt
1371 fi
1372
1373 key_mgmt="$wpa_key_mgmt"
1374
developer2c78ce72023-02-24 11:26:12 +08001375 if [ "$_w_mode" = "mesh" ] || [ "$auth_type" = "sae" ]; then
1376 passphrase="sae_password=\"${key}\""
developer8556ff22022-11-10 11:58:51 +08001377 else
developer2c78ce72023-02-24 11:26:12 +08001378 if [ ${#key} -eq 64 ]; then
1379 passphrase="psk=${key}"
developer8556ff22022-11-10 11:58:51 +08001380 else
1381 passphrase="psk=\"${key}\""
1382 fi
1383 fi
1384 append network_data "$passphrase" "$N$T"
1385 ;;
1386 eap|eap192|eap-eap192)
1387 hostapd_append_wpa_key_mgmt
1388 key_mgmt="$wpa_key_mgmt"
1389
1390 json_get_vars eap_type identity anonymous_identity ca_cert ca_cert_usesystem
1391
1392 [ "$fils" -gt 0 ] && append network_data "erp=1" "$N$T"
1393 if [ "$ca_cert_usesystem" -eq "1" -a -f "/etc/ssl/certs/ca-certificates.crt" ]; then
1394 append network_data "ca_cert=\"/etc/ssl/certs/ca-certificates.crt\"" "$N$T"
1395 else
1396 [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
1397 fi
1398 [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
1399 [ -n "$anonymous_identity" ] && append network_data "anonymous_identity=\"$anonymous_identity\"" "$N$T"
1400 case "$eap_type" in
1401 tls)
1402 json_get_vars client_cert priv_key priv_key_pwd
1403 append network_data "client_cert=\"$client_cert\"" "$N$T"
1404 append network_data "private_key=\"$priv_key\"" "$N$T"
1405 append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
1406
1407 json_get_vars subject_match
1408 [ -n "$subject_match" ] && append network_data "subject_match=\"$subject_match\"" "$N$T"
1409
1410 json_get_values altsubject_match altsubject_match
1411 if [ -n "$altsubject_match" ]; then
1412 local list=
1413 for x in $altsubject_match; do
1414 append list "$x" ";"
1415 done
1416 append network_data "altsubject_match=\"$list\"" "$N$T"
1417 fi
1418
1419 json_get_values domain_match domain_match
1420 if [ -n "$domain_match" ]; then
1421 local list=
1422 for x in $domain_match; do
1423 append list "$x" ";"
1424 done
1425 append network_data "domain_match=\"$list\"" "$N$T"
1426 fi
1427
1428 json_get_values domain_suffix_match domain_suffix_match
1429 if [ -n "$domain_suffix_match" ]; then
1430 local list=
1431 for x in $domain_suffix_match; do
1432 append list "$x" ";"
1433 done
1434 append network_data "domain_suffix_match=\"$list\"" "$N$T"
1435 fi
1436 ;;
1437 fast|peap|ttls)
1438 json_get_vars auth password ca_cert2 ca_cert2_usesystem client_cert2 priv_key2 priv_key2_pwd
1439 set_default auth MSCHAPV2
1440
1441 if [ "$auth" = "EAP-TLS" ]; then
1442 if [ "$ca_cert2_usesystem" -eq "1" -a -f "/etc/ssl/certs/ca-certificates.crt" ]; then
1443 append network_data "ca_cert2=\"/etc/ssl/certs/ca-certificates.crt\"" "$N$T"
1444 else
1445 [ -n "$ca_cert2" ] && append network_data "ca_cert2=\"$ca_cert2\"" "$N$T"
1446 fi
1447 append network_data "client_cert2=\"$client_cert2\"" "$N$T"
1448 append network_data "private_key2=\"$priv_key2\"" "$N$T"
1449 append network_data "private_key2_passwd=\"$priv_key2_pwd\"" "$N$T"
1450 else
1451 append network_data "password=\"$password\"" "$N$T"
1452 fi
1453
1454 json_get_vars subject_match
1455 [ -n "$subject_match" ] && append network_data "subject_match=\"$subject_match\"" "$N$T"
1456
1457 json_get_values altsubject_match altsubject_match
1458 if [ -n "$altsubject_match" ]; then
1459 local list=
1460 for x in $altsubject_match; do
1461 append list "$x" ";"
1462 done
1463 append network_data "altsubject_match=\"$list\"" "$N$T"
1464 fi
1465
1466 json_get_values domain_match domain_match
1467 if [ -n "$domain_match" ]; then
1468 local list=
1469 for x in $domain_match; do
1470 append list "$x" ";"
1471 done
1472 append network_data "domain_match=\"$list\"" "$N$T"
1473 fi
1474
1475 json_get_values domain_suffix_match domain_suffix_match
1476 if [ -n "$domain_suffix_match" ]; then
1477 local list=
1478 for x in $domain_suffix_match; do
1479 append list "$x" ";"
1480 done
1481 append network_data "domain_suffix_match=\"$list\"" "$N$T"
1482 fi
1483
1484 phase2proto="auth="
1485 case "$auth" in
1486 "auth"*)
1487 phase2proto=""
1488 ;;
1489 "EAP-"*)
1490 auth="$(echo $auth | cut -b 5- )"
1491 [ "$eap_type" = "ttls" ] &&
1492 phase2proto="autheap="
1493 json_get_vars subject_match2
1494 [ -n "$subject_match2" ] && append network_data "subject_match2=\"$subject_match2\"" "$N$T"
1495
1496 json_get_values altsubject_match2 altsubject_match2
1497 if [ -n "$altsubject_match2" ]; then
1498 local list=
1499 for x in $altsubject_match2; do
1500 append list "$x" ";"
1501 done
1502 append network_data "altsubject_match2=\"$list\"" "$N$T"
1503 fi
1504
1505 json_get_values domain_match2 domain_match2
1506 if [ -n "$domain_match2" ]; then
1507 local list=
1508 for x in $domain_match2; do
1509 append list "$x" ";"
1510 done
1511 append network_data "domain_match2=\"$list\"" "$N$T"
1512 fi
1513
1514 json_get_values domain_suffix_match2 domain_suffix_match2
1515 if [ -n "$domain_suffix_match2" ]; then
1516 local list=
1517 for x in $domain_suffix_match2; do
1518 append list "$x" ";"
1519 done
1520 append network_data "domain_suffix_match2=\"$list\"" "$N$T"
1521 fi
1522 ;;
1523 esac
1524 append network_data "phase2=\"$phase2proto$auth\"" "$N$T"
1525 ;;
1526 esac
1527 append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
1528 ;;
1529 esac
1530
1531 [ "$wpa_cipher" = GCMP ] && {
1532 append network_data "pairwise=GCMP" "$N$T"
1533 append network_data "group=GCMP" "$N$T"
1534 }
1535
1536 [ "$mode" = mesh ] || {
1537 case "$wpa" in
1538 1)
1539 append network_data "proto=WPA" "$N$T"
1540 ;;
1541 2)
1542 append network_data "proto=RSN" "$N$T"
1543 ;;
1544 esac
1545
1546 case "$ieee80211w" in
1547 [012])
1548 [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
1549 ;;
1550 esac
1551 }
1552 [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
1553 [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
1554
1555 local bssid_blacklist bssid_whitelist
1556 json_get_values bssid_blacklist bssid_blacklist
1557 json_get_values bssid_whitelist bssid_whitelist
1558
1559 [ -n "$bssid_blacklist" ] && append network_data "bssid_blacklist=$bssid_blacklist" "$N$T"
1560 [ -n "$bssid_whitelist" ] && append network_data "bssid_whitelist=$bssid_whitelist" "$N$T"
1561
1562 [ -n "$basic_rate" ] && {
1563 local br rate_list=
1564 for br in $basic_rate; do
1565 wpa_supplicant_add_rate rate_list "$br"
1566 done
1567 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
1568 }
1569
1570 [ -n "$mcast_rate" ] && {
1571 local mc_rate=
1572 wpa_supplicant_add_rate mc_rate "$mcast_rate"
1573 append network_data "mcast_rate=$mc_rate" "$N$T"
1574 }
1575
1576 if [ "$key_mgmt" = "WPS" ]; then
1577 echo "wps_cred_processing=1" >> "$_config"
1578 else
1579 cat >> "$_config" <<EOF
1580network={
1581 $scan_ssid
1582 ssid="$ssid"
1583 key_mgmt=$key_mgmt
1584 $network_data
1585}
1586EOF
1587 fi
1588 return 0
1589}
1590
1591wpa_supplicant_run() {
1592 local ifname="$1"
1593 local hostapd_ctrl="$2"
1594
1595 _wpa_supplicant_common "$ifname"
1596
1597 ubus wait_for wpa_supplicant
1598 local supplicant_res="$(ubus call wpa_supplicant config_add "{ \
1599 \"driver\": \"${_w_driver:-wext}\", \"ctrl\": \"$_rpath\", \
1600 \"iface\": \"$ifname\", \"config\": \"$_config\" \
1601 ${network_bridge:+, \"bridge\": \"$network_bridge\"} \
1602 ${hostapd_ctrl:+, \"hostapd_ctrl\": \"$hostapd_ctrl\"} \
1603 }")"
1604
1605 ret="$?"
1606
1607 [ "$ret" != 0 -o -z "$supplicant_res" ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
1608
1609 wireless_add_process "$(jsonfilter -s "$supplicant_res" -l 1 -e @.pid)" "/usr/sbin/wpa_supplicant" 1 1
1610
1611 return $ret
1612}
1613
1614hostapd_common_cleanup() {
1615 killall meshd-nl80211
1616}