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