3 local val="$(($2 / 1000))"
4 local sub="$((($2 / 100) % 10))"
6 [ $sub -gt 0 ] && append $var "."
9 hostapd_add_basic_rate() {
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
53 config_add_string country
54 config_add_boolean country_ie doth
55 config_add_string require_mode
57 hostapd_add_log_config
60 hostapd_prepare_device_config() {
64 local base="${config%%.conf}"
67 json_get_vars country country_ie beacon_int doth require_mode
69 hostapd_set_log_options base_cfg
71 set_default country_ie 1
74 [ -n "$country" ] && {
75 append base_cfg "country_code=$country" "$N"
77 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
78 [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
80 [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
83 json_get_values basic_rate_list basic_rate
84 for br in $basic_rate_list; do
85 hostapd_add_basic_rate brlist "$br"
87 case "$require_mode" in
88 g) brlist="60 120 240" ;;
89 n) append base_cfg "require_ht=1" "$N";;
90 ac) append base_cfg "require_vht=1" "$N";;
92 [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
93 [ -n "$beacon_int" ] && append base_cfg "beacon_int=$beacon_int" "$N"
101 hostapd_common_add_bss_config() {
102 config_add_string 'bssid:macaddr' 'ssid:string'
103 config_add_boolean wds wmm hidden
105 config_add_int maxassoc max_inactivity
106 config_add_boolean disassoc_low_ack isolate short_preamble
109 wep_rekey eap_reauth_period \
110 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
112 config_add_boolean rsn_preauth auth_cache
113 config_add_int ieee80211w
115 config_add_string 'auth_server:host' 'server:host'
116 config_add_string auth_secret
117 config_add_int 'auth_port:port' 'port:port'
119 config_add_string acct_server
120 config_add_string acct_secret
121 config_add_int acct_port
123 config_add_string dae_client
124 config_add_string dae_secret
125 config_add_int dae_port
127 config_add_string nasid
128 config_add_string ownip
129 config_add_string iapp_interface
130 config_add_string eap_type ca_cert client_cert identity auth priv_key priv_key_pwd
132 config_add_int dynamic_vlan vlan_naming
133 config_add_string vlan_tagged_interface
135 config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
137 config_add_boolean wps_pushbutton wps_label ext_registrar wps_pbc_in_m1
138 config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
140 config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
142 config_add_string macfilter 'macfile:file'
143 config_add_array 'maclist:list(macaddr)'
145 config_add_int mcast_rate
146 config_add_array basic_rate
149 hostapd_set_bss_options() {
154 wireless_vif_parse_encryption
157 local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey
160 wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey \
161 maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
162 wps_pushbutton wps_label ext_registrar wps_pbc_in_m1 \
163 wps_device_type wps_device_name wps_manufacturer wps_pin \
164 macfilter ssid wmm hidden short_preamble rsn_preauth \
167 set_default isolate 0
168 set_default maxassoc 0
169 set_default max_inactivity 0
170 set_default short_preamble 1
171 set_default disassoc_low_ack 1
175 append bss_conf "ctrl_interface=/var/run/hostapd"
176 if [ "$isolate" -gt 0 ]; then
177 append bss_conf "ap_isolate=$isolate" "$N"
179 if [ "$maxassoc" -gt 0 ]; then
180 append bss_conf "max_num_sta=$maxassoc" "$N"
182 if [ "$max_inactivity" -gt 0 ]; then
183 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
186 append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
187 append bss_conf "preamble=$short_preamble" "$N"
188 append bss_conf "wmm_enabled=$wmm" "$N"
189 append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
191 [ "$wpa" -gt 0 ] && {
192 [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
193 [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
194 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
200 # Here we make the assumption that if we're in open mode
201 # with WPS enabled, we got to be in unconfigured state.
206 if [ ${#key} -lt 8 ]; then
207 wireless_setup_vif_failed INVALID_WPA_PSK
209 elif [ ${#key} -eq 64 ]; then
210 append bss_conf "wpa_psk=$key" "$N"
212 append bss_conf "wpa_passphrase=$key" "$N"
218 auth_server auth_secret auth_port \
219 acct_server acct_secret acct_port \
220 dae_client dae_secret dae_port \
222 eap_reauth_period dynamic_vlan \
223 vlan_tagged_interface
225 # legacy compatibility
226 [ -n "$auth_server" ] || json_get_var auth_server server
227 [ -n "$auth_port" ] || json_get_var auth_port port
228 [ -n "$auth_secret" ] || json_get_var auth_secret key
230 set_default auth_port 1812
231 set_default acct_port 1813
232 set_default dae_port 3799
234 set_default vlan_naming 1
236 append bss_conf "auth_server_addr=$auth_server" "$N"
237 append bss_conf "auth_server_port=$auth_port" "$N"
238 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
240 [ -n "$acct_server" ] && {
241 append bss_conf "acct_server_addr=$acct_server" "$N"
242 append bss_conf "acct_server_port=$acct_port" "$N"
243 [ -n "$acct_secret" ] && \
244 append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
247 [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
249 [ -n "$dae_client" -a -n "$dae_secret" ] && {
250 append bss_conf "radius_das_port=$dae_port" "$N"
251 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
254 append bss_conf "nas_identifier=$nasid" "$N"
255 [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
256 append bss_conf "eapol_key_index_workaround=1" "$N"
257 append bss_conf "ieee8021x=1" "$N"
258 append bss_conf "wpa_key_mgmt=WPA-EAP" "$N"
260 [ -n "$dynamic_vlan" ] && {
261 append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
262 append bss_conf "vlan_naming=$vlan_naming" "$N"
263 [ -n "$vlan_tagged_interface" ] && \
264 append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
270 hostapd_append_wep_key bss_conf
271 append bss_conf "wep_default_key=$wep_keyidx" "$N"
272 [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
276 local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
277 append bss_conf "auth_algs=${auth_algs:-1}" "$N"
278 append bss_conf "wpa=$wpa" "$N"
279 [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
281 set_default wps_pushbutton 0
282 set_default wps_label 0
283 set_default wps_pbc_in_m1 0
286 [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
287 [ "$wps_label" -gt 0 ] && append config_methods label
289 [ -n "$wps_possible" -a -n "$config_methods" ] && {
290 set_default ext_registrar 0
291 set_default wps_device_type "6-0050F204-1"
292 set_default wps_device_name "OpenWrt AP"
293 set_default wps_manufacturer "openwrt.org"
296 [ -n "$wps_configured" ] && wps_state=1
298 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
300 append bss_conf "eap_server=1" "$N"
301 [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
302 append bss_conf "wps_state=$wps_state" "$N"
303 append bss_conf "ap_setup_locked=0" "$N"
304 append bss_conf "device_type=$wps_device_type" "$N"
305 append bss_conf "device_name=$wps_device_name" "$N"
306 append bss_conf "manufacturer=$wps_manufacturer" "$N"
307 append bss_conf "config_methods=$config_methods" "$N"
308 [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
311 append bss_conf "ssid=$ssid" "$N"
312 [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
313 [ -n "$iapp_interface" ] && {
314 iapp_interface="$(uci_get_state network "$iapp_interface" ifname "$iapp_interface")"
315 [ -n "$iapp_interface" ] && append bss_conf "iapp_interface=$iapp_interface" "$N"
318 if [ "$wpa" -ge "2" ]; then
319 if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
320 set_default auth_cache 1
321 append bss_conf "rsn_preauth=1" "$N"
322 append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
324 set_default auth_cache 0
327 append bss_conf "okc=$auth_cache" "$N"
328 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
330 # RSN -> allow management frame protection
331 json_get_var ieee80211w ieee80211w
332 case "$ieee80211w" in
334 json_get_vars ieee80211w_max_timeout ieee80211w_retry_timeout
335 append bss_conf "ieee80211w=$ieee80211w" "$N"
336 [ "$ieee80211w" -gt "0" ] && {
337 [ -n "$ieee80211w_max_timeout" ] && \
338 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
339 [ -n "$ieee80211w_retry_timeout" ] && \
340 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
346 _macfile="/var/run/hostapd-$ifname.maclist"
349 append bss_conf "macaddr_acl=1" "$N"
350 append bss_conf "accept_mac_file=$_macfile" "$N"
353 append bss_conf "macaddr_acl=0" "$N"
354 append bss_conf "deny_mac_file=$_macfile" "$N"
361 [ -n "$_macfile" ] && {
362 json_get_vars macfile
363 json_get_values maclist maclist
367 for mac in $maclist; do
370 [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
374 append "$var" "$bss_conf" "$N"
378 hostapd_set_log_options() {
381 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
382 json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
384 set_default log_level 2
385 set_default log_80211 1
386 set_default log_8021x 1
387 set_default log_radius 1
388 set_default log_wpa 1
389 set_default log_driver 1
390 set_default log_iapp 1
391 set_default log_mlme 1
394 ($log_80211 << 0) | \
395 ($log_8021x << 1) | \
396 ($log_radius << 2) | \
398 ($log_driver << 4) | \
403 append "$var" "logger_syslog=$log_mask" "$N"
404 append "$var" "logger_syslog_level=$log_level" "$N"
405 append "$var" "logger_stdout=$log_mask" "$N"
406 append "$var" "logger_stdout_level=$log_level" "$N"
411 _wpa_supplicant_common() {
414 _rpath="/var/run/wpa_supplicant"
415 _config="${_rpath}-$ifname.conf"
418 wpa_supplicant_teardown_interface() {
419 _wpa_supplicant_common "$1"
420 rm -rf "$_rpath/$1" "$_config"
423 wpa_supplicant_prepare_interface() {
427 _wpa_supplicant_common "$1"
429 json_get_vars mode wds
431 [ -n "$network_bridge" ] && {
438 [ "$wds" = 1 ] || fail=1
443 wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
453 [[ "$mode" = adhoc ]] && {
459 wpa_supplicant_teardown_interface "$ifname"
460 cat > "$_config" <<EOF
466 wpa_supplicant_add_network() {
469 _wpa_supplicant_common "$1"
470 wireless_vif_parse_encryption
474 basic_rate mcast_rate \
477 local key_mgmt='NONE'
482 local wpa_key_mgmt="WPA-PSK"
486 [[ "$_w_mode" = "adhoc" ]] && {
487 append network_data "mode=1" "$N$T"
488 [ -n "$channel" ] && {
489 freq="$(get_freq "$phy" "$channel")"
490 append network_data "fixed_freq=1" "$N$T"
491 append network_data "frequency=$freq" "$N$T"
496 [ "$_w_driver" = "nl80211" ] || wpa_key_mgmt="WPA-NONE"
499 [[ "$_w_mode" = adhoc ]] && append network_data "$_w_modestr" "$N$T"
505 hostapd_append_wep_key network_data
506 append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
511 key_mgmt="$wpa_key_mgmt"
512 if [ ${#key} -eq 64 ]; then
513 passphrase="psk=${key}"
515 passphrase="psk=\"${key}\""
517 append network_data "$passphrase" "$N$T"
522 json_get_vars eap_type identity ca_cert
523 [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
524 [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
527 json_get_vars client_cert priv_key priv_key_pwd
528 append network_data "client_cert=\"$client_cert\"" "$N$T"
529 append network_data "private_key=\"$priv_key\"" "$N$T"
530 append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
533 json_get_vars auth password
534 set_default auth MSCHAPV2
535 append network_data "phase2=\"$auth\"" "$N$T"
536 append network_data "password=\"$password\"" "$N$T"
539 append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
545 append network_data "proto=WPA" "$N$T"
548 append network_data "proto=RSN" "$N$T"
552 case "$ieee80211w" in
554 [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
558 local beacon_int brates mrate
559 [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
560 [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
563 [ -n "$basic_rate" ] && {
565 for br in $basic_rate; do
566 hostapd_add_rate rate_list "$br"
568 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
571 [ -n "$mcast_rate" ] && {
573 hostapd_add_rate mc_rate "$mcast_rate"
574 append network_data "mcast_rate=$mc_rate" "$N$T"
578 [ -n "$ht" ] && append network_data "htmode=$ht" "$N$T"
580 cat >> "$_config" <<EOF
591 wpa_supplicant_run() {
592 local ifname="$1"; shift
594 _wpa_supplicant_common "$ifname"
596 /usr/sbin/wpa_supplicant -B \
597 ${network_bridge:+-b $network_bridge} \
598 -P "/var/run/wpa_supplicant-${ifname}.pid" \
599 -D ${_w_driver:-wext} \
606 wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
608 [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
613 hostapd_common_cleanup() {
614 killall hostapd wpa_supplicant meshd-nl80211