1 . /lib/functions/network.sh
3 wpa_supplicant_add_rate() {
5 local val="$(($2 / 1000))"
6 local sub="$((($2 / 100) % 10))"
8 [ $sub -gt 0 ] && append $var "."
13 local val="$(($2 / 100))"
14 append $var "$val" " "
17 hostapd_append_wep_key() {
24 for idx in 1 2 3 4; do
27 json_get_var ckey "key${idx}"
29 append $var "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N$T"
31 wep_keyidx=$((key - 1))
34 append $var "wep_key0=$(prepare_key_wep "$key")" "$N$T"
39 hostapd_add_log_config() {
49 config_add_int log_level
52 hostapd_common_add_device_config() {
53 config_add_array basic_rate
54 config_add_array supported_rates
56 config_add_string country
57 config_add_boolean country_ie doth
58 config_add_string require_mode
60 hostapd_add_log_config
63 hostapd_prepare_device_config() {
67 local base="${config%%.conf}"
70 json_get_vars country country_ie beacon_int doth require_mode
72 hostapd_set_log_options base_cfg
74 set_default country_ie 1
77 [ -n "$country" ] && {
78 append base_cfg "country_code=$country" "$N"
80 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
81 [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
83 [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
86 json_get_values basic_rate_list basic_rate
87 for br in $basic_rate_list; do
88 hostapd_add_rate brlist "$br"
90 case "$require_mode" in
91 g) brlist="60 120 240" ;;
92 n) append base_cfg "require_ht=1" "$N";;
93 ac) append base_cfg "require_vht=1" "$N";;
97 json_get_values rate_list supported_rates
98 for r in $rate_list; do
99 hostapd_add_rate rlist "$r"
102 [ -n "$rlist" ] && append base_cfg "supported_rates=$rlist" "$N"
103 [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
104 [ -n "$beacon_int" ] && append base_cfg "beacon_int=$beacon_int" "$N"
106 cat > "$config" <<EOF
112 hostapd_common_add_bss_config() {
113 config_add_string 'bssid:macaddr' 'ssid:string'
114 config_add_boolean wds wmm uapsd hidden
116 config_add_int maxassoc max_inactivity
117 config_add_boolean disassoc_low_ack isolate short_preamble
120 wep_rekey eap_reauth_period \
121 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
123 config_add_boolean rsn_preauth auth_cache
124 config_add_int ieee80211w
125 config_add_int eapol_version
127 config_add_string 'auth_server:host' 'server:host'
128 config_add_string auth_secret
129 config_add_int 'auth_port:port' 'port:port'
131 config_add_string acct_server
132 config_add_string acct_secret
133 config_add_int acct_port
135 config_add_string dae_client
136 config_add_string dae_secret
137 config_add_int dae_port
139 config_add_string nasid
140 config_add_string ownip
141 config_add_string iapp_interface
142 config_add_string eap_type ca_cert client_cert identity anonymous_identity auth priv_key priv_key_pwd
144 config_add_int dynamic_vlan vlan_naming
145 config_add_string vlan_tagged_interface vlan_bridge
146 config_add_string vlan_file
148 config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
150 config_add_string wpa_psk_file
152 config_add_boolean wps_pushbutton wps_label ext_registrar wps_pbc_in_m1
153 config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
155 config_add_boolean ieee80211r pmk_r1_push
156 config_add_int r0_key_lifetime reassociation_deadline
157 config_add_string mobility_domain r1_key_holder
158 config_add_array r0kh r1kh
160 config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
162 config_add_string macfilter 'macfile:file'
163 config_add_array 'maclist:list(macaddr)'
165 config_add_array bssid_blacklist
166 config_add_array bssid_whitelist
168 config_add_int mcast_rate
169 config_add_array basic_rate
170 config_add_array supported_rates
173 hostapd_set_bss_options() {
178 wireless_vif_parse_encryption
181 local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wpa_key_mgmt
184 wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey \
185 maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
186 wps_pushbutton wps_label ext_registrar wps_pbc_in_m1 \
187 wps_device_type wps_device_name wps_manufacturer wps_pin \
188 macfilter ssid wmm uapsd hidden short_preamble rsn_preauth \
189 iapp_interface eapol_version acct_server acct_secret acct_port
191 set_default isolate 0
192 set_default maxassoc 0
193 set_default max_inactivity 0
194 set_default short_preamble 1
195 set_default disassoc_low_ack 1
199 set_default eapol_version 0
200 set_default acct_port 1813
202 append bss_conf "ctrl_interface=/var/run/hostapd"
203 if [ "$isolate" -gt 0 ]; then
204 append bss_conf "ap_isolate=$isolate" "$N"
206 if [ "$maxassoc" -gt 0 ]; then
207 append bss_conf "max_num_sta=$maxassoc" "$N"
209 if [ "$max_inactivity" -gt 0 ]; then
210 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
213 append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
214 append bss_conf "preamble=$short_preamble" "$N"
215 append bss_conf "wmm_enabled=$wmm" "$N"
216 append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
217 append bss_conf "uapsd_advertisement_enabled=$uapsd" "$N"
219 [ "$wpa" -gt 0 ] && {
220 [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
221 [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
222 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
225 [ -n "$acct_server" ] && {
226 append bss_conf "acct_server_addr=$acct_server" "$N"
227 append bss_conf "acct_server_port=$acct_port" "$N"
228 [ -n "$acct_secret" ] && \
229 append bss_conf "acct_server_shared_secret=$acct_secret" "$N"
235 # Here we make the assumption that if we're in open mode
236 # with WPS enabled, we got to be in unconfigured state.
240 json_get_vars key wpa_psk_file
241 if [ ${#key} -lt 8 ]; then
242 wireless_setup_vif_failed INVALID_WPA_PSK
244 elif [ ${#key} -eq 64 ]; then
245 append bss_conf "wpa_psk=$key" "$N"
247 append bss_conf "wpa_passphrase=$key" "$N"
249 [ -n "$wpa_psk_file" ] && {
250 [ -e "$wpa_psk_file" ] || touch "$wpa_psk_file"
251 append bss_conf "wpa_psk_file=$wpa_psk_file" "$N"
253 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
256 append wpa_key_mgmt "WPA-PSK"
260 auth_server auth_secret auth_port \
261 dae_client dae_secret dae_port \
263 eap_reauth_period dynamic_vlan \
264 vlan_naming vlan_tagged_interface \
265 vlan_bridge vlan_file
267 # legacy compatibility
268 [ -n "$auth_server" ] || json_get_var auth_server server
269 [ -n "$auth_port" ] || json_get_var auth_port port
270 [ -n "$auth_secret" ] || json_get_var auth_secret key
272 set_default auth_port 1812
273 set_default dae_port 3799
275 set_default vlan_naming 1
277 append bss_conf "auth_server_addr=$auth_server" "$N"
278 append bss_conf "auth_server_port=$auth_port" "$N"
279 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
281 [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
283 [ -n "$dae_client" -a -n "$dae_secret" ] && {
284 append bss_conf "radius_das_port=$dae_port" "$N"
285 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
288 [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
289 append bss_conf "eapol_key_index_workaround=1" "$N"
290 append bss_conf "ieee8021x=1" "$N"
291 append wpa_key_mgmt "WPA-EAP"
293 [ -n "$dynamic_vlan" ] && {
294 append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
295 append bss_conf "vlan_naming=$vlan_naming" "$N"
296 [ -n "$vlan_bridge" ] && \
297 append bss_conf "vlan_bridge=$vlan_bridge" "$N"
298 [ -n "$vlan_tagged_interface" ] && \
299 append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
300 [ -n "$vlan_file" ] && {
301 [ -e "$vlan_file" ] || touch "$vlan_file"
302 append bss_conf "vlan_file=$vlan_file" "$N"
306 [ "$eapol_version" -ge "1" -a "$eapol_version" -le "2" ] && append bss_conf "eapol_version=$eapol_version" "$N"
311 hostapd_append_wep_key bss_conf
312 append bss_conf "wep_default_key=$wep_keyidx" "$N"
313 [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
317 local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
318 append bss_conf "auth_algs=${auth_algs:-1}" "$N"
319 append bss_conf "wpa=$wpa" "$N"
320 [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
322 set_default wps_pushbutton 0
323 set_default wps_label 0
324 set_default wps_pbc_in_m1 0
327 [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
328 [ "$wps_label" -gt 0 ] && append config_methods label
330 [ -n "$wps_possible" -a -n "$config_methods" ] && {
331 set_default ext_registrar 0
332 set_default wps_device_type "6-0050F204-1"
333 set_default wps_device_name "Lede AP"
334 set_default wps_manufacturer "www.lede-project.org"
337 [ -n "$wps_configured" ] && wps_state=1
339 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
341 append bss_conf "eap_server=1" "$N"
342 [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
343 append bss_conf "wps_state=$wps_state" "$N"
344 append bss_conf "ap_setup_locked=0" "$N"
345 append bss_conf "device_type=$wps_device_type" "$N"
346 append bss_conf "device_name=$wps_device_name" "$N"
347 append bss_conf "manufacturer=$wps_manufacturer" "$N"
348 append bss_conf "config_methods=$config_methods" "$N"
349 [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
352 append bss_conf "ssid=$ssid" "$N"
353 [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
354 [ -n "$iapp_interface" ] && {
356 network_get_device ifname "$iapp_interface" || ifname = "$iapp_interface"
357 append bss_conf "iapp_interface=$ifname" "$N"
360 if [ "$wpa" -ge "1" ]; then
361 json_get_vars nasid ieee80211r
362 set_default ieee80211r 0
363 [ -n "$nasid" ] && append bss_conf "nas_identifier=$nasid" "$N"
365 if [ "$ieee80211r" -gt "0" ]; then
366 json_get_vars mobility_domain r0_key_lifetime r1_key_holder \
367 reassociation_deadline pmk_r1_push
368 json_get_values r0kh r0kh
369 json_get_values r1kh r1kh
371 set_default mobility_domain "4f57"
372 set_default r0_key_lifetime 10000
373 set_default r1_key_holder "00004f577274"
374 set_default reassociation_deadline 1000
375 set_default pmk_r1_push 0
377 append bss_conf "mobility_domain=$mobility_domain" "$N"
378 append bss_conf "r0_key_lifetime=$r0_key_lifetime" "$N"
379 append bss_conf "r1_key_holder=$r1_key_holder" "$N"
380 append bss_conf "reassociation_deadline=$reassociation_deadline" "$N"
381 append bss_conf "pmk_r1_push=$pmk_r1_push" "$N"
384 append bss_conf "r0kh=${kh//,/ }" "$N"
387 append bss_conf "r1kh=${kh//,/ }" "$N"
390 [ "$wpa_key_mgmt" != "${wpa_key_mgmt/EAP/}" ] && append wpa_key_mgmt "FT-EAP"
391 [ "$wpa_key_mgmt" != "${wpa_key_mgmt/PSK/}" ] && append wpa_key_mgmt "FT-PSK"
394 [ -n "$wpa_key_mgmt" ] && append bss_conf "wpa_key_mgmt=$wpa_key_mgmt" "$N"
397 if [ "$wpa" -ge "2" ]; then
398 if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
399 set_default auth_cache 1
400 append bss_conf "rsn_preauth=1" "$N"
401 append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
403 set_default auth_cache 0
406 append bss_conf "okc=$auth_cache" "$N"
407 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
409 # RSN -> allow management frame protection
410 json_get_var ieee80211w ieee80211w
411 case "$ieee80211w" in
413 json_get_vars ieee80211w_max_timeout ieee80211w_retry_timeout
414 append bss_conf "ieee80211w=$ieee80211w" "$N"
415 [ "$ieee80211w" -gt "0" ] && {
416 [ -n "$ieee80211w_max_timeout" ] && \
417 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
418 [ -n "$ieee80211w_retry_timeout" ] && \
419 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
425 _macfile="/var/run/hostapd-$ifname.maclist"
428 append bss_conf "macaddr_acl=1" "$N"
429 append bss_conf "accept_mac_file=$_macfile" "$N"
432 append bss_conf "macaddr_acl=0" "$N"
433 append bss_conf "deny_mac_file=$_macfile" "$N"
440 [ -n "$_macfile" ] && {
441 json_get_vars macfile
442 json_get_values maclist maclist
446 for mac in $maclist; do
449 [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
453 append "$var" "$bss_conf" "$N"
457 hostapd_set_log_options() {
460 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
461 json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
463 set_default log_level 2
464 set_default log_80211 1
465 set_default log_8021x 1
466 set_default log_radius 1
467 set_default log_wpa 1
468 set_default log_driver 1
469 set_default log_iapp 1
470 set_default log_mlme 1
473 ($log_80211 << 0) | \
474 ($log_8021x << 1) | \
475 ($log_radius << 2) | \
477 ($log_driver << 4) | \
482 append "$var" "logger_syslog=$log_mask" "$N"
483 append "$var" "logger_syslog_level=$log_level" "$N"
484 append "$var" "logger_stdout=$log_mask" "$N"
485 append "$var" "logger_stdout_level=$log_level" "$N"
490 _wpa_supplicant_common() {
493 _rpath="/var/run/wpa_supplicant"
494 _config="${_rpath}-$ifname.conf"
497 wpa_supplicant_teardown_interface() {
498 _wpa_supplicant_common "$1"
499 rm -rf "$_rpath/$1" "$_config"
502 wpa_supplicant_prepare_interface() {
506 _wpa_supplicant_common "$1"
508 json_get_vars mode wds
510 [ -n "$network_bridge" ] && {
517 [ "$wds" = 1 ] || fail=1
522 wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
532 [[ "$mode" = adhoc ]] && {
539 [ -n "$country" ] && {
540 country_str="country=$country"
543 wpa_supplicant_teardown_interface "$ifname"
544 cat > "$_config" <<EOF
551 wpa_supplicant_add_network() {
554 _wpa_supplicant_common "$1"
555 wireless_vif_parse_encryption
559 basic_rate mcast_rate \
560 ieee80211w ieee80211r
562 set_default ieee80211r 0
564 local key_mgmt='NONE'
569 local wpa_key_mgmt="WPA-PSK"
570 local scan_ssid="scan_ssid=1"
573 [ "$ieee80211r" -gt 0 ] && wpa_key_mgmt="FT-PSK $wpa_key_mgmt"
575 [[ "$_w_mode" = "adhoc" ]] && {
576 append network_data "mode=1" "$N$T"
577 [ -n "$channel" ] && {
578 freq="$(get_freq "$phy" "$channel")"
579 append network_data "fixed_freq=1" "$N$T"
580 append network_data "frequency=$freq" "$N$T"
583 scan_ssid="scan_ssid=0"
585 [ "$_w_driver" = "nl80211" ] || wpa_key_mgmt="WPA-NONE"
588 [[ "$_w_mode" = "mesh" ]] && {
589 json_get_vars mesh_id
592 append network_data "mode=5" "$N$T"
593 [ -n "$channel" ] && {
594 freq="$(get_freq "$phy" "$channel")"
595 append network_data "frequency=$freq" "$N$T"
601 [[ "$_w_mode" = "adhoc" -o "$_w_mode" = "mesh" ]] && append network_data "$_w_modestr" "$N$T"
607 hostapd_append_wep_key network_data
608 append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
613 key_mgmt="$wpa_key_mgmt"
614 if [ ${#key} -eq 64 ]; then
615 passphrase="psk=${key}"
617 passphrase="psk=\"${key}\""
619 append network_data "$passphrase" "$N$T"
623 [ "$ieee80211r" -gt 0 ] && key_mgmt="FT-EAP $key_mgmt"
625 json_get_vars eap_type identity anonymous_identity ca_cert
626 [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
627 [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
628 [ -n "$anonymous_identity" ] && append network_data "anonymous_identity=\"$anonymous_identity\"" "$N$T"
631 json_get_vars client_cert priv_key priv_key_pwd
632 append network_data "client_cert=\"$client_cert\"" "$N$T"
633 append network_data "private_key=\"$priv_key\"" "$N$T"
634 append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
637 json_get_vars auth password ca_cert2 client_cert2 priv_key2 priv_key2_pwd
638 set_default auth MSCHAPV2
640 if [ "$auth" = "EAP-TLS" ]; then
641 [ -n "$ca_cert2" ] &&
642 append network_data "ca_cert2=\"$ca_cert2\"" "$N$T"
643 append network_data "client_cert2=\"$client_cert2\"" "$N$T"
644 append network_data "private_key2=\"$priv_key2\"" "$N$T"
645 append network_data "private_key2_passwd=\"$priv_key2_pwd\"" "$N$T"
647 append network_data "password=\"$password\"" "$N$T"
656 auth="$(echo $auth | cut -b 5- )"
657 [ "$eap_type" = "ttls" ] &&
658 phase2proto="autheap="
661 append network_data "phase2=\"$phase2proto$auth\"" "$N$T"
664 append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
668 [ "$mode" = mesh ] || {
671 append network_data "proto=WPA" "$N$T"
674 append network_data "proto=RSN" "$N$T"
678 case "$ieee80211w" in
680 [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
684 local beacon_int brates mrate
685 [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
686 [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
688 local bssid_blacklist bssid_whitelist
689 json_get_values bssid_blacklist bssid_blacklist
690 json_get_values bssid_whitelist bssid_whitelist
692 [ -n "$bssid_blacklist" ] && append network_data "bssid_blacklist=$bssid_blacklist" "$N$T"
693 [ -n "$bssid_whitelist" ] && append network_data "bssid_whitelist=$bssid_whitelist" "$N$T"
695 [ -n "$basic_rate" ] && {
697 for br in $basic_rate; do
698 wpa_supplicant_add_rate rate_list "$br"
700 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
703 [ -n "$mcast_rate" ] && {
705 wpa_supplicant_add_rate mc_rate "$mcast_rate"
706 append network_data "mcast_rate=$mc_rate" "$N$T"
710 [[ "$_w_mode" = adhoc ]] || ibss_htmode=
711 [ -n "$ibss_htmode" ] && append network_data "htmode=$ibss_htmode" "$N$T"
713 cat >> "$_config" <<EOF
724 wpa_supplicant_run() {
725 local ifname="$1"; shift
727 _wpa_supplicant_common "$ifname"
729 /usr/sbin/wpa_supplicant -B \
730 ${network_bridge:+-b $network_bridge} \
731 -P "/var/run/wpa_supplicant-${ifname}.pid" \
732 -D ${_w_driver:-wext} \
739 wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
741 [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
746 hostapd_common_cleanup() {
747 killall hostapd wpa_supplicant meshd-nl80211