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