hostapd: backport BSSID black/whitelists
[openwrt/svn-archive/archive.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
124 config_add_string 'auth_server:host' 'server:host'
125 config_add_string auth_secret
126 config_add_int 'auth_port:port' 'port:port'
127
128 config_add_string acct_server
129 config_add_string acct_secret
130 config_add_int acct_port
131
132 config_add_string dae_client
133 config_add_string dae_secret
134 config_add_int dae_port
135
136 config_add_string nasid
137 config_add_string ownip
138 config_add_string iapp_interface
139 config_add_string eap_type ca_cert client_cert identity auth priv_key priv_key_pwd
140
141 config_add_int dynamic_vlan vlan_naming
142 config_add_string vlan_tagged_interface vlan_bridge
143
144 config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
145
146 config_add_string wpa_psk_file
147
148 config_add_boolean wps_pushbutton wps_label ext_registrar wps_pbc_in_m1
149 config_add_string wps_device_type wps_device_name wps_manufacturer wps_pin
150
151 config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
152
153 config_add_string macfilter 'macfile:file'
154 config_add_array 'maclist:list(macaddr)'
155
156 config_add_array bssid_blacklist
157 config_add_array bssid_whitelist
158
159 config_add_int mcast_rate
160 config_add_array basic_rate
161 config_add_array supported_rates
162 }
163
164 hostapd_set_bss_options() {
165 local var="$1"
166 local phy="$2"
167 local vif="$3"
168
169 wireless_vif_parse_encryption
170
171 local bss_conf
172 local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey
173
174 json_get_vars \
175 wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey \
176 maxassoc max_inactivity disassoc_low_ack isolate auth_cache \
177 wps_pushbutton wps_label ext_registrar wps_pbc_in_m1 \
178 wps_device_type wps_device_name wps_manufacturer wps_pin \
179 macfilter ssid wmm uapsd hidden short_preamble rsn_preauth \
180 iapp_interface
181
182 set_default isolate 0
183 set_default maxassoc 0
184 set_default max_inactivity 0
185 set_default short_preamble 1
186 set_default disassoc_low_ack 1
187 set_default hidden 0
188 set_default wmm 1
189 set_default uapsd 1
190
191 append bss_conf "ctrl_interface=/var/run/hostapd"
192 if [ "$isolate" -gt 0 ]; then
193 append bss_conf "ap_isolate=$isolate" "$N"
194 fi
195 if [ "$maxassoc" -gt 0 ]; then
196 append bss_conf "max_num_sta=$maxassoc" "$N"
197 fi
198 if [ "$max_inactivity" -gt 0 ]; then
199 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
200 fi
201
202 append bss_conf "disassoc_low_ack=$disassoc_low_ack" "$N"
203 append bss_conf "preamble=$short_preamble" "$N"
204 append bss_conf "wmm_enabled=$wmm" "$N"
205 append bss_conf "ignore_broadcast_ssid=$hidden" "$N"
206 append bss_conf "uapsd_advertisement_enabled=$uapsd" "$N"
207
208 [ "$wpa" -gt 0 ] && {
209 [ -n "$wpa_group_rekey" ] && append bss_conf "wpa_group_rekey=$wpa_group_rekey" "$N"
210 [ -n "$wpa_pair_rekey" ] && append bss_conf "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
211 [ -n "$wpa_master_rekey" ] && append bss_conf "wpa_gmk_rekey=$wpa_master_rekey" "$N"
212 }
213
214 case "$auth_type" in
215 none)
216 wps_possible=1
217 # Here we make the assumption that if we're in open mode
218 # with WPS enabled, we got to be in unconfigured state.
219 wps_not_configured=1
220 ;;
221 psk)
222 json_get_vars key wpa_psk_file
223 if [ ${#key} -lt 8 ]; then
224 wireless_setup_vif_failed INVALID_WPA_PSK
225 return 1
226 elif [ ${#key} -eq 64 ]; then
227 append bss_conf "wpa_psk=$key" "$N"
228 else
229 append bss_conf "wpa_passphrase=$key" "$N"
230 fi
231 [ -n "$wpa_psk_file" ] && {
232 [ -e "$wpa_psk_file" ] || touch "$wpa_psk_file"
233 append bss_conf "wpa_psk_file=$wpa_psk_file" "$N"
234 }
235 wps_possible=1
236 ;;
237 eap)
238 json_get_vars \
239 auth_server auth_secret auth_port \
240 acct_server acct_secret acct_port \
241 dae_client dae_secret dae_port \
242 nasid ownip \
243 eap_reauth_period dynamic_vlan \
244 vlan_naming vlan_tagged_interface \
245 vlan_bridge
246
247 # legacy compatibility
248 [ -n "$auth_server" ] || json_get_var auth_server server
249 [ -n "$auth_port" ] || json_get_var auth_port port
250 [ -n "$auth_secret" ] || json_get_var auth_secret key
251
252 set_default auth_port 1812
253 set_default acct_port 1813
254 set_default dae_port 3799
255
256 set_default vlan_naming 1
257
258 append bss_conf "auth_server_addr=$auth_server" "$N"
259 append bss_conf "auth_server_port=$auth_port" "$N"
260 append bss_conf "auth_server_shared_secret=$auth_secret" "$N"
261
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 }
268
269 [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
270
271 [ -n "$dae_client" -a -n "$dae_secret" ] && {
272 append bss_conf "radius_das_port=$dae_port" "$N"
273 append bss_conf "radius_das_client=$dae_client $dae_secret" "$N"
274 }
275
276 append bss_conf "nas_identifier=$nasid" "$N"
277 [ -n "$ownip" ] && append bss_conf "own_ip_addr=$ownip" "$N"
278 append bss_conf "eapol_key_index_workaround=1" "$N"
279 append bss_conf "ieee8021x=1" "$N"
280 append bss_conf "wpa_key_mgmt=WPA-EAP" "$N"
281
282 [ -n "$dynamic_vlan" ] && {
283 append bss_conf "dynamic_vlan=$dynamic_vlan" "$N"
284 append bss_conf "vlan_naming=$vlan_naming" "$N"
285 [ -n "$vlan_bridge" ] && \
286 append bss_conf "vlan_bridge=$vlan_bridge" "$N"
287 [ -n "$vlan_tagged_interface" ] && \
288 append bss_conf "vlan_tagged_interface=$vlan_tagged_interface" "$N"
289 }
290 ;;
291 wep)
292 local wep_keyidx=0
293 json_get_vars key
294 hostapd_append_wep_key bss_conf
295 append bss_conf "wep_default_key=$wep_keyidx" "$N"
296 [ -n "$wep_rekey" ] && append bss_conf "wep_rekey_period=$wep_rekey" "$N"
297 ;;
298 esac
299
300 local auth_algs=$((($auth_mode_shared << 1) | $auth_mode_open))
301 append bss_conf "auth_algs=${auth_algs:-1}" "$N"
302 append bss_conf "wpa=$wpa" "$N"
303 [ -n "$wpa_pairwise" ] && append bss_conf "wpa_pairwise=$wpa_pairwise" "$N"
304
305 set_default wps_pushbutton 0
306 set_default wps_label 0
307 set_default wps_pbc_in_m1 0
308
309 config_methods=
310 [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
311 [ "$wps_label" -gt 0 ] && append config_methods label
312
313 [ -n "$wps_possible" -a -n "$config_methods" ] && {
314 set_default ext_registrar 0
315 set_default wps_device_type "6-0050F204-1"
316 set_default wps_device_name "OpenWrt AP"
317 set_default wps_manufacturer "openwrt.org"
318
319 wps_state=2
320 [ -n "$wps_configured" ] && wps_state=1
321
322 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
323
324 append bss_conf "eap_server=1" "$N"
325 [ -n "$wps_pin" ] && append bss_conf "ap_pin=$wps_pin" "$N"
326 append bss_conf "wps_state=$wps_state" "$N"
327 append bss_conf "ap_setup_locked=0" "$N"
328 append bss_conf "device_type=$wps_device_type" "$N"
329 append bss_conf "device_name=$wps_device_name" "$N"
330 append bss_conf "manufacturer=$wps_manufacturer" "$N"
331 append bss_conf "config_methods=$config_methods" "$N"
332 [ "$wps_pbc_in_m1" -gt 0 ] && append bss_conf "pbc_in_m1=$wps_pbc_in_m1" "$N"
333 }
334
335 append bss_conf "ssid=$ssid" "$N"
336 [ -n "$network_bridge" ] && append bss_conf "bridge=$network_bridge" "$N"
337 [ -n "$iapp_interface" ] && {
338 iapp_interface="$(uci_get_state network "$iapp_interface" ifname "$iapp_interface")"
339 [ -n "$iapp_interface" ] && append bss_conf "iapp_interface=$iapp_interface" "$N"
340 }
341
342 if [ "$wpa" -ge "2" ]; then
343 if [ -n "$network_bridge" -a "$rsn_preauth" = 1 ]; then
344 set_default auth_cache 1
345 append bss_conf "rsn_preauth=1" "$N"
346 append bss_conf "rsn_preauth_interfaces=$network_bridge" "$N"
347 else
348 set_default auth_cache 0
349 fi
350
351 append bss_conf "okc=$auth_cache" "$N"
352 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
353
354 # RSN -> allow management frame protection
355 json_get_var ieee80211w ieee80211w
356 case "$ieee80211w" in
357 [012])
358 json_get_vars ieee80211w_max_timeout ieee80211w_retry_timeout
359 append bss_conf "ieee80211w=$ieee80211w" "$N"
360 [ "$ieee80211w" -gt "0" ] && {
361 [ -n "$ieee80211w_max_timeout" ] && \
362 append bss_conf "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
363 [ -n "$ieee80211w_retry_timeout" ] && \
364 append bss_conf "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
365 }
366 ;;
367 esac
368 fi
369
370 _macfile="/var/run/hostapd-$ifname.maclist"
371 case "$macfilter" in
372 allow)
373 append bss_conf "macaddr_acl=1" "$N"
374 append bss_conf "accept_mac_file=$_macfile" "$N"
375 ;;
376 deny)
377 append bss_conf "macaddr_acl=0" "$N"
378 append bss_conf "deny_mac_file=$_macfile" "$N"
379 ;;
380 *)
381 _macfile=""
382 ;;
383 esac
384
385 [ -n "$_macfile" ] && {
386 json_get_vars macfile
387 json_get_values maclist maclist
388
389 rm -f "$_macfile"
390 (
391 for mac in $maclist; do
392 echo "$mac"
393 done
394 [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
395 ) > "$_macfile"
396 }
397
398 append "$var" "$bss_conf" "$N"
399 return 0
400 }
401
402 hostapd_set_log_options() {
403 local var="$1"
404
405 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
406 json_get_vars log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
407
408 set_default log_level 2
409 set_default log_80211 1
410 set_default log_8021x 1
411 set_default log_radius 1
412 set_default log_wpa 1
413 set_default log_driver 1
414 set_default log_iapp 1
415 set_default log_mlme 1
416
417 local log_mask=$(( \
418 ($log_80211 << 0) | \
419 ($log_8021x << 1) | \
420 ($log_radius << 2) | \
421 ($log_wpa << 3) | \
422 ($log_driver << 4) | \
423 ($log_iapp << 5) | \
424 ($log_mlme << 6) \
425 ))
426
427 append "$var" "logger_syslog=$log_mask" "$N"
428 append "$var" "logger_syslog_level=$log_level" "$N"
429 append "$var" "logger_stdout=$log_mask" "$N"
430 append "$var" "logger_stdout_level=$log_level" "$N"
431
432 return 0
433 }
434
435 _wpa_supplicant_common() {
436 local ifname="$1"
437
438 _rpath="/var/run/wpa_supplicant"
439 _config="${_rpath}-$ifname.conf"
440 }
441
442 wpa_supplicant_teardown_interface() {
443 _wpa_supplicant_common "$1"
444 rm -rf "$_rpath/$1" "$_config"
445 }
446
447 wpa_supplicant_prepare_interface() {
448 local ifname="$1"
449 _w_driver="$2"
450
451 _wpa_supplicant_common "$1"
452
453 json_get_vars mode wds
454
455 [ -n "$network_bridge" ] && {
456 fail=
457 case "$mode" in
458 adhoc)
459 fail=1
460 ;;
461 sta)
462 [ "$wds" = 1 ] || fail=1
463 ;;
464 esac
465
466 [ -n "$fail" ] && {
467 wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
468 return 1
469 }
470 }
471
472 local ap_scan=
473
474 _w_mode="$mode"
475 _w_modestr=
476
477 [[ "$mode" = adhoc ]] && {
478 ap_scan="ap_scan=2"
479
480 _w_modestr="mode=1"
481 }
482
483 wpa_supplicant_teardown_interface "$ifname"
484 cat > "$_config" <<EOF
485 $ap_scan
486 EOF
487 return 0
488 }
489
490 wpa_supplicant_add_network() {
491 local ifname="$1"
492
493 _wpa_supplicant_common "$1"
494 wireless_vif_parse_encryption
495
496 json_get_vars \
497 ssid bssid key \
498 basic_rate mcast_rate \
499 ieee80211w
500
501 local key_mgmt='NONE'
502 local enc_str=
503 local network_data=
504 local T=" "
505
506 local wpa_key_mgmt="WPA-PSK"
507 local scan_ssid="1"
508 local freq
509
510 [[ "$_w_mode" = "adhoc" ]] && {
511 append network_data "mode=1" "$N$T"
512 [ -n "$channel" ] && {
513 freq="$(get_freq "$phy" "$channel")"
514 append network_data "fixed_freq=1" "$N$T"
515 append network_data "frequency=$freq" "$N$T"
516 }
517
518 scan_ssid=0
519
520 [ "$_w_driver" = "nl80211" ] || wpa_key_mgmt="WPA-NONE"
521 }
522
523 [[ "$_w_mode" = adhoc ]] && append network_data "$_w_modestr" "$N$T"
524
525 case "$auth_type" in
526 none) ;;
527 wep)
528 local wep_keyidx=0
529 hostapd_append_wep_key network_data
530 append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
531 ;;
532 psk)
533 local passphrase
534
535 key_mgmt="$wpa_key_mgmt"
536 if [ ${#key} -eq 64 ]; then
537 passphrase="psk=${key}"
538 else
539 passphrase="psk=\"${key}\""
540 fi
541 append network_data "$passphrase" "$N$T"
542 ;;
543 eap)
544 key_mgmt='WPA-EAP'
545
546 json_get_vars eap_type identity ca_cert
547 [ -n "$ca_cert" ] && append network_data "ca_cert=\"$ca_cert\"" "$N$T"
548 [ -n "$identity" ] && append network_data "identity=\"$identity\"" "$N$T"
549 case "$eap_type" in
550 tls)
551 json_get_vars client_cert priv_key priv_key_pwd
552 append network_data "client_cert=\"$client_cert\"" "$N$T"
553 append network_data "private_key=\"$priv_key\"" "$N$T"
554 append network_data "private_key_passwd=\"$priv_key_pwd\"" "$N$T"
555 ;;
556 peap|ttls)
557 json_get_vars auth password
558 set_default auth MSCHAPV2
559 append network_data "phase2=\"$auth\"" "$N$T"
560 append network_data "password=\"$password\"" "$N$T"
561 ;;
562 esac
563 append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
564 ;;
565 esac
566
567 case "$wpa" in
568 1)
569 append network_data "proto=WPA" "$N$T"
570 ;;
571 2)
572 append network_data "proto=RSN" "$N$T"
573 ;;
574 esac
575
576 case "$ieee80211w" in
577 [012])
578 [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
579 ;;
580 esac
581
582 local beacon_int brates mrate
583 [ -n "$bssid" ] && append network_data "bssid=$bssid" "$N$T"
584 [ -n "$beacon_int" ] && append network_data "beacon_int=$beacon_int" "$N$T"
585
586 local bssid_blacklist bssid_whitelist
587 json_get_values bssid_blacklist bssid_blacklist
588 json_get_values bssid_whitelist bssid_whitelist
589
590 [ -n "$bssid_blacklist" ] && append network_data "bssid_blacklist=$bssid_blacklist" "$N$T"
591 [ -n "$bssid_whitelist" ] && append network_data "bssid_whitelist=$bssid_whitelist" "$N$T"
592
593 [ -n "$basic_rate" ] && {
594 local br rate_list=
595 for br in $basic_rate; do
596 wpa_supplicant_add_rate rate_list "$br"
597 done
598 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
599 }
600
601 [ -n "$mcast_rate" ] && {
602 local mc_rate=
603 wpa_supplicant_add_rate mc_rate "$mcast_rate"
604 append network_data "mcast_rate=$mc_rate" "$N$T"
605 }
606
607 local ht_str
608 [[ "$_w_mode" = adhoc ]] || ibss_htmode=
609 [ -n "$ibss_htmode" ] && append network_data "htmode=$ibss_htmode" "$N$T"
610
611 cat >> "$_config" <<EOF
612 network={
613 scan_ssid=$scan_ssid
614 ssid="$ssid"
615 key_mgmt=$key_mgmt
616 $network_data
617 }
618 EOF
619 return 0
620 }
621
622 wpa_supplicant_run() {
623 local ifname="$1"; shift
624
625 _wpa_supplicant_common "$ifname"
626
627 /usr/sbin/wpa_supplicant -B \
628 ${network_bridge:+-b $network_bridge} \
629 -P "/var/run/wpa_supplicant-${ifname}.pid" \
630 -D ${_w_driver:-wext} \
631 -i "$ifname" \
632 -c "$_config" \
633 -C "$_rpath" \
634 "$@"
635
636 ret="$?"
637 wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
638
639 [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
640
641 return $ret
642 }
643
644 hostapd_common_cleanup() {
645 killall hostapd wpa_supplicant meshd-nl80211
646 }