1 wpa_supplicant_add_rate() {
3 local val="$(($2 / 1000))"
4 local sub="$((($2 / 100) % 10))"
6 [ $sub -gt 0 ] && append $var "."
11 local val="$(($2 / 100))"
12 append $var "$val" " "
15 hostapd_append_wep_key() {
22 for idx in 1 2 3 4; do
25 json_get_var ckey "key${idx}"
27 append $var "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N$T"
29 wep_keyidx=$((key - 1))
32 append $var "wep_key0=$(prepare_key_wep "$key")" "$N$T"
37 hostapd_add_log_config() {
47 config_add_int log_level
50 hostapd_common_add_device_config() {
51 config_add_array basic_rate
52 config_add_array supported_rates
54 config_add_string country
55 config_add_boolean country_ie doth
56 config_add_string require_mode
58 hostapd_add_log_config
61 hostapd_prepare_device_config() {
65 local base="${config%%.conf}"
68 json_get_vars country country_ie beacon_int doth require_mode
70 hostapd_set_log_options base_cfg
72 set_default country_ie 1
75 [ -n "$country" ] && {
76 append base_cfg "country_code=$country" "$N"
78 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
79 [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
81 [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
84 json_get_values basic_rate_list basic_rate
85 for br in $basic_rate_list; do
86 hostapd_add_rate brlist "$br"
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";;
95 json_get_values rate_list supported_rates
96 for r in $rate_list; do
97 hostapd_add_rate rlist "$r"
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"
104 cat > "$config" <<EOF
110 hostapd_common_add_bss_config() {
111 config_add_string 'bssid:macaddr' 'ssid:string'
112 config_add_boolean wds wmm uapsd hidden
114 config_add_int maxassoc max_inactivity
115 config_add_boolean disassoc_low_ack isolate short_preamble
118 wep_rekey eap_reauth_period \
119 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
121 config_add_boolean rsn_preauth auth_cache
122 config_add_int ieee80211w
123 config_add_int eapol_version
125 config_add_string 'auth_server:host' 'server:host'
126 config_add_string auth_secret
127 config_add_int 'auth_port:port' 'port:port'
129 config_add_string acct_server
130 config_add_string acct_secret
131 config_add_int acct_port
133 config_add_string dae_client
134 config_add_string dae_secret
135 config_add_int dae_port
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
142 config_add_int dynamic_vlan vlan_naming
143 config_add_string vlan_tagged_interface vlan_bridge
144 config_add_string vlan_file
146 config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
148 config_add_string wpa_psk_file
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
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
158 config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
160 config_add_string macfilter 'macfile:file'
161 config_add_array 'maclist:list(macaddr)'
163 config_add_array bssid_blacklist
164 config_add_array bssid_whitelist
166 config_add_int mcast_rate
167 config_add_array basic_rate
168 config_add_array supported_rates
171 hostapd_set_bss_options() {
176 wireless_vif_parse_encryption
179 local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wpa_key_mgmt
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
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
197 set_default eapol_version 0
199 append bss_conf "ctrl_interface=/var/run/hostapd"
200 if [ "$isolate" -gt 0 ]; then
201 append bss_conf "ap_isolate=$isolate" "$N"
203 if [ "$maxassoc" -gt 0 ]; then
204 append bss_conf "max_num_sta=$maxassoc" "$N"
206 if [ "$max_inactivity" -gt 0 ]; then
207 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
210 append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
211 append bss_conf "preamble=$short_preamble" "$N"
212 append bss_conf "wmm_enabled=$wmm" "$N"
213 append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
214 append bss_conf "uapsd_advertisement_enabled=$uapsd" "$N"
216 [ "$wpa" -gt 0 ] && {
217 [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
218 [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
219 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
225 # Here we make the assumption that if we're in open mode
226 # with WPS enabled, we got to be in unconfigured state.
230 json_get_vars key wpa_psk_file
231 if [ ${#key} -lt 8 ]; then
232 wireless_setup_vif_failed INVALID_WPA_PSK
234 elif [ ${#key} -eq 64 ]; then
235 append bss_conf "wpa_psk=$key" "$N"
237 append bss_conf "wpa_passphrase=$key" "$N"
239 [ -n "$wpa_psk_file" ] && {
240 [ -e "$wpa_psk_file" ] || touch "$wpa_psk_file"
241 append bss_conf "wpa_psk_file=$wpa_psk_file" "$N"
243 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
246 append wpa_key_mgmt "WPA-PSK"
250 auth_server auth_secret auth_port \
251 acct_server acct_secret acct_port \
252 dae_client dae_secret dae_port \
254 eap_reauth_period dynamic_vlan \
255 vlan_naming vlan_tagged_interface \
256 vlan_bridge vlan_file
258 # legacy compatibility
259 [ -n "$auth_server" ] || json_get_var auth_server server
260 [ -n "$auth_port" ] || json_get_var auth_port port
261 [ -n "$auth_secret" ] || json_get_var auth_secret key
263 set_default auth_port 1812
264 set_default acct_port 1813
265 set_default dae_port 3799
267 set_default vlan_naming 1
269 append bss_conf "auth_server_addr=$auth_server" "$N"
270 append bss_conf "auth_server_port=$auth_port" "$N"
271 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
273 [ -n "$acct_server" ] && {
274 append bss_conf "acct_server_addr=$acct_server" "$N"
275 append bss_conf "acct_server_port=$acct_port" "$N"
276 [ -n "$acct_secret" ] && \
277 append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
280 [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
282 [ -n "$dae_client" -a -n "$dae_secret" ] && {
283 append bss_conf "radius_das_port=$dae_port" "$N"
284 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
287 [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
288 append bss_conf "eapol_key_index_workaround=1" "$N"
289 append bss_conf "ieee8021x=1" "$N"
290 append wpa_key_mgmt "WPA-EAP"
292 [ -n "$dynamic_vlan" ] && {
293 append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
294 append bss_conf "vlan_naming=$vlan_naming" "$N"
295 [ -n "$vlan_bridge" ] && \
296 append bss_conf "vlan_bridge=$vlan_bridge" "$N"
297 [ -n "$vlan_tagged_interface" ] && \
298 append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
299 [ -n "$vlan_file" ] && {
300 [ -e "$vlan_file" ] || touch "$vlan_file"
301 append bss_conf "vlan_file=$vlan_file" "$N"
305 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
310 hostapd_append_wep_key bss_conf
311 append bss_conf "wep_default_key=$wep_keyidx" "$N"
312 [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
316 local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
317 append bss_conf "auth_algs=${auth_algs:-1}" "$N"
318 append bss_conf "wpa=$wpa" "$N"
319 [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
321 set_default wps_pushbutton 0
322 set_default wps_label 0
323 set_default wps_pbc_in_m1 0
326 [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
327 [ "$wps_label" -gt 0 ] && append config_methods label
329 [ -n "$wps_possible" -a -n "$config_methods" ] && {
330 set_default ext_registrar 0
331 set_default wps_device_type "6-0050F204-1"
332 set_default wps_device_name "OpenWrt AP"
333 set_default wps_manufacturer "openwrt.org"
336 [ -n "$wps_configured" ] && wps_state=1
338 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
340 append bss_conf "eap_server=1" "$N"
341 [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
342 append bss_conf "wps_state=$wps_state" "$N"
343 append bss_conf "ap_setup_locked=0" "$N"
344 append bss_conf "device_type=$wps_device_type" "$N"
345 append bss_conf "device_name=$wps_device_name" "$N"
346 append bss_conf "manufacturer=$wps_manufacturer" "$N"
347 append bss_conf "config_methods=$config_methods" "$N"
348 [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
351 append bss_conf "ssid=$ssid" "$N"
352 [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
353 [ -n "$iapp_interface" ] && {
354 iapp_interface="$(uci_get_state network "$iapp_interface" ifname "$iapp_interface")"
355 [ -n "$iapp_interface" ] && append bss_conf "iapp_interface=$iapp_interface" "$N"
358 if [ "$wpa" -ge "1" ]; then
359 json_get_vars nasid ieee80211r
360 set_default ieee80211r 0
361 [ -n "$nasid" ] && append bss_conf "nas_identifier=$nasid" "$N"
363 if [ "$ieee80211r" -gt "0" ]; then
364 json_get_vars mobility_domain r0_key_lifetime r1_key_holder \
365 reassociation_deadline pmk_r1_push
366 json_get_values r0kh r0kh
367 json_get_values r1kh r1kh
369 set_default mobility_domain "4f57"
370 set_default r0_key_lifetime 10000
371 set_default r1_key_holder "00004f577274"
372 set_default reassociation_deadline 1000
373 set_default pmk_r1_push 0
375 append bss_conf "mobility_domain=$mobility_domain" "$N"
376 append bss_conf "r0_key_lifetime=$r0_key_lifetime" "$N"
377 append bss_conf "r1_key_holder=$r1_key_holder" "$N"
378 append bss_conf "reassociation_deadline=$reassociation_deadline" "$N"
379 append bss_conf "pmk_r1_push=$pmk_r1_push" "$N"
382 append bss_conf "r0kh=${kh//,/ }" "$N"
385 append bss_conf "r1kh=${kh//,/ }" "$N"
388 [ "$wpa_key_mgmt" != "${wpa_key_mgmt/EAP/}" ] && append wpa_key_mgmt "FT-EAP"
389 [ "$wpa_key_mgmt" != "${wpa_key_mgmt/PSK/}" ] && append wpa_key_mgmt "FT-PSK"
392 [ -n "$wpa_key_mgmt" ] && append bss_conf "wpa_key_mgmt=$wpa_key_mgmt" "$N"
395 if [ "$wpa" -ge "2" ]; then
396 if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
397 set_default auth_cache 1
398 append bss_conf "rsn_preauth=1" "$N"
399 append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
401 set_default auth_cache 0
404 append bss_conf "okc=$auth_cache" "$N"
405 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
407 # RSN -> allow management frame protection
408 json_get_var ieee80211w ieee80211w
409 case "$ieee80211w" in
411 json_get_vars ieee80211w_max_timeout ieee80211w_retry_timeout
412 append bss_conf "ieee80211w=$ieee80211w" "$N"
413 [ "$ieee80211w" -gt "0" ] && {
414 [ -n "$ieee80211w_max_timeout" ] && \
415 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
416 [ -n "$ieee80211w_retry_timeout" ] && \
417 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
423 _macfile="/var/run/hostapd-$ifname.maclist"
426 append bss_conf "macaddr_acl=1" "$N"
427 append bss_conf "accept_mac_file=$_macfile" "$N"
430 append bss_conf "macaddr_acl=0" "$N"
431 append bss_conf "deny_mac_file=$_macfile" "$N"
438 [ -n "$_macfile" ] && {
439 json_get_vars macfile
440 json_get_values maclist maclist
444 for mac in $maclist; do
447 [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
451 append "$var" "$bss_conf" "$N"
455 hostapd_set_log_options() {
458 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
459 json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
461 set_default log_level 2
462 set_default log_80211 1
463 set_default log_8021x 1
464 set_default log_radius 1
465 set_default log_wpa 1
466 set_default log_driver 1
467 set_default log_iapp 1
468 set_default log_mlme 1
471 ($log_80211 << 0) | \
472 ($log_8021x << 1) | \
473 ($log_radius << 2) | \
475 ($log_driver << 4) | \
480 append "$var" "logger_syslog=$log_mask" "$N"
481 append "$var" "logger_syslog_level=$log_level" "$N"
482 append "$var" "logger_stdout=$log_mask" "$N"
483 append "$var" "logger_stdout_level=$log_level" "$N"
488 _wpa_supplicant_common() {
491 _rpath="/var/run/wpa_supplicant"
492 _config="${_rpath}-$ifname.conf"
495 wpa_supplicant_teardown_interface() {
496 _wpa_supplicant_common "$1"
497 rm -rf "$_rpath/$1" "$_config"
500 wpa_supplicant_prepare_interface() {
504 _wpa_supplicant_common "$1"
506 json_get_vars mode wds
508 [ -n "$network_bridge" ] && {
515 [ "$wds" = 1 ] || fail=1
520 wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
530 [[ "$mode" = adhoc ]] && {
536 wpa_supplicant_teardown_interface "$ifname"
537 cat > "$_config" <<EOF
543 wpa_supplicant_add_network() {
546 _wpa_supplicant_common "$1"
547 wireless_vif_parse_encryption
551 basic_rate mcast_rate \
552 ieee80211w ieee80211r
554 set_default ieee80211r 0
556 local key_mgmt='NONE'
561 local wpa_key_mgmt="WPA-PSK"
562 local scan_ssid="scan_ssid=1"
565 [ "$ieee80211r" -gt 0 ] && wpa_key_mgmt="FT-PSK $wpa_key_mgmt"
567 [[ "$_w_mode" = "adhoc" ]] && {
568 append network_data "mode=1" "$N$T"
569 [ -n "$channel" ] && {
570 freq="$(get_freq "$phy" "$channel")"
571 append network_data "fixed_freq=1" "$N$T"
572 append network_data "frequency=$freq" "$N$T"
575 scan_ssid="scan_ssid=0"
577 [ "$_w_driver" = "nl80211" ] || wpa_key_mgmt="WPA-NONE"
580 [[ "$_w_mode" = "mesh" ]] && {
581 append network_data "mode=5" "$N$T"
582 [ -n "$channel" ] && {
583 freq="$(get_freq "$phy" "$channel")"
584 append network_data "frequency=$freq" "$N$T"
590 [[ "$_w_mode" = "adhoc" -o "$_w_mode" = "mesh" ]] && append network_data "$_w_modestr" "$N$T"
596 hostapd_append_wep_key network_data
597 append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
602 key_mgmt="$wpa_key_mgmt"
603 if [ ${#key} -eq 64 ]; then
604 passphrase="psk=${key}"
606 passphrase="psk=\"${key}\""
608 append network_data "$passphrase" "$N$T"
612 [ "$ieee80211r" -gt 0 ] && key_mgmt="FT-EAP $key_mgmt"
614 json_get_vars eap_type identity ca_cert
615 [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
616 [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
619 json_get_vars client_cert priv_key priv_key_pwd
620 append network_data "client_cert=\"$client_cert\"" "$N$T"
621 append network_data "private_key=\"$priv_key\"" "$N$T"
622 append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
625 json_get_vars auth password
626 set_default auth MSCHAPV2
627 append network_data "phase2=\"$auth\"" "$N$T"
628 append network_data "password=\"$password\"" "$N$T"
631 append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
635 [ "$mode" = mesh ] || {
638 append network_data "proto=WPA" "$N$T"
641 append network_data "proto=RSN" "$N$T"
645 case "$ieee80211w" in
647 [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
651 local beacon_int brates mrate
652 [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
653 [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
655 local bssid_blacklist bssid_whitelist
656 json_get_values bssid_blacklist bssid_blacklist
657 json_get_values bssid_whitelist bssid_whitelist
659 [ -n "$bssid_blacklist" ] && append network_data "bssid_blacklist=$bssid_blacklist" "$N$T"
660 [ -n "$bssid_whitelist" ] && append network_data "bssid_whitelist=$bssid_whitelist" "$N$T"
662 [ -n "$basic_rate" ] && {
664 for br in $basic_rate; do
665 wpa_supplicant_add_rate rate_list "$br"
667 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
670 [ -n "$mcast_rate" ] && {
672 wpa_supplicant_add_rate mc_rate "$mcast_rate"
673 append network_data "mcast_rate=$mc_rate" "$N$T"
677 [[ "$_w_mode" = adhoc ]] || ibss_htmode=
678 [ -n "$ibss_htmode" ] && append network_data "htmode=$ibss_htmode" "$N$T"
680 cat >> "$_config" <<EOF
691 wpa_supplicant_run() {
692 local ifname="$1"; shift
694 _wpa_supplicant_common "$ifname"
696 /usr/sbin/wpa_supplicant -B \
697 ${network_bridge:+-b $network_bridge} \
698 -P "/var/run/wpa_supplicant-${ifname}.pid" \
699 -D ${_w_driver:-wext} \
706 wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
708 [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
713 hostapd_common_cleanup() {
714 killall hostapd wpa_supplicant meshd-nl80211