hostapd: allow using iapp for any encryption type (fixes #18022)
[openwrt.git] / package / network / services / hostapd / files / netifd.sh
1 hostapd_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_basic_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
53         config_add_string country
54         config_add_boolean country_ie doth
55         config_add_string require_mode
56
57         hostapd_add_log_config
58 }
59
60 hostapd_prepare_device_config() {
61         local config="$1"
62         local driver="$2"
63
64         local base="${config%%.conf}"
65         local base_cfg=
66
67         json_get_vars country country_ie beacon_int doth require_mode
68
69         hostapd_set_log_options base_cfg
70
71         set_default country_ie 1
72         set_default doth 1
73
74         [ -n "$country" ] && {
75                 append base_cfg "country_code=$country" "$N"
76
77                 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
78                 [ "$hwmode" = "a" -a "$doth" -gt 0 ] && append base_cfg "ieee80211h=1" "$N"
79         }
80         [ -n "$hwmode" ] && append base_cfg "hw_mode=$hwmode" "$N"
81
82         local brlist= br
83         json_get_values basic_rate_list basic_rate
84         for br in $basic_rate_list; do
85                 hostapd_add_basic_rate brlist "$br"
86         done
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";;
91         esac
92         [ -n "$brlist" ] && append base_cfg "basic_rates=$brlist" "$N"
93         [ -n "$beacon_int" ] && append base_cfg "beacon_int=$beacon_int" "$N"
94
95         cat > "$config" <<EOF
96 driver=$driver
97 $base_cfg
98 EOF
99 }
100
101 hostapd_common_add_bss_config() {
102         config_add_string 'bssid:macaddr' 'ssid:string'
103         config_add_boolean wds wmm hidden
104
105         config_add_int maxassoc max_inactivity
106         config_add_boolean disassoc_low_ack isolate short_preamble
107
108         config_add_int \
109                 wep_rekey eap_reauth_period \
110                 wpa_group_rekey wpa_pair_rekey wpa_master_rekey
111
112         config_add_boolean rsn_preauth auth_cache
113         config_add_int ieee80211w
114
115         config_add_string 'auth_server:host' 'server:host'
116         config_add_string auth_secret
117         config_add_int 'auth_port:port' 'port:port'
118
119         config_add_string acct_server
120         config_add_string acct_secret
121         config_add_int acct_port
122
123         config_add_string dae_client
124         config_add_string dae_secret
125         config_add_int dae_port
126
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
131
132         config_add_int dynamic_vlan vlan_naming
133         config_add_string vlan_tagged_interface
134
135         config_add_string 'key1:wepkey' 'key2:wepkey' 'key3:wepkey' 'key4:wepkey' 'password:wpakey'
136
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
139
140         config_add_int ieee80211w_max_timeout ieee80211w_retry_timeout
141
142         config_add_string macfilter 'macfile:file'
143         config_add_array 'maclist:list(macaddr)'
144
145         config_add_int mcast_rate
146         config_add_array basic_rate
147 }
148
149 hostapd_set_bss_options() {
150         local var="$1"
151         local phy="$2"
152         local vif="$3"
153
154         wireless_vif_parse_encryption
155
156         local bss_conf
157         local wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey
158
159         json_get_vars \
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 \
165                 iapp_interface
166
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
172         set_default hidden 0
173         set_default wmm 1
174
175         append bss_conf "ctrl_interface=/var/run/hostapd"
176         if [ "$isolate" -gt 0 ]; then
177                 append bss_conf "ap_isolate=$isolate" "$N"
178         fi
179         if [ "$maxassoc" -gt 0 ]; then
180                 append bss_conf "max_num_sta=$maxassoc" "$N"
181         fi
182         if [ "$max_inactivity" -gt 0 ]; then
183                 append bss_conf "ap_max_inactivity=$max_inactivity" "$N"
184         fi
185
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"
190
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"
195         }
196
197         case "$auth_type" in
198                 none)
199                         wps_possible=1
200                         # Here we make the assumption that if we're in open mode
201                         # with WPS enabled, we got to be in unconfigured state.
202                         wps_not_configured=1
203                 ;;
204                 psk)
205                         json_get_vars key
206                         if [ ${#key} -lt 8 ]; then
207                                 wireless_setup_vif_failed INVALID_WPA_PSK
208                                 return 1
209                         elif [ ${#key} -eq 64 ]; then
210                                 append bss_conf "wpa_psk=$key" "$N"
211                         else
212                                 append bss_conf "wpa_passphrase=$key" "$N"
213                         fi
214                         wps_possible=1
215                 ;;
216                 eap)
217                         json_get_vars \
218                                 auth_server auth_secret auth_port \
219                                 acct_server acct_secret acct_port \
220                                 dae_client dae_secret dae_port \
221                                 nasid ownip \
222                                 eap_reauth_period dynamic_vlan \
223                                 vlan_tagged_interface
224
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
229
230                         set_default auth_port 1812
231                         set_default acct_port 1813
232                         set_default dae_port 3799
233
234                         set_default vlan_naming 1
235
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"
239
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"
245                         }
246
247                         [ -n "$eap_reauth_period" ] && append bss_conf "eap_reauth_period=$eap_reauth_period" "$N"
248
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"
252                         }
253
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"
259
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"
265                         }
266                 ;;
267                 wep)
268                         local wep_keyidx=0
269                         json_get_vars key
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"
273                 ;;
274         esac
275
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"
280
281         set_default wps_pushbutton 0
282         set_default wps_label 0
283         set_default wps_pbc_in_m1 0
284
285         config_methods=
286         [ "$wps_pushbutton" -gt 0 ] && append config_methods push_button
287         [ "$wps_label" -gt 0 ] && append config_methods label
288
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"
294
295                 wps_state=2
296                 [ -n "$wps_configured" ] && wps_state=1
297
298                 [ "$ext_registrar" -gt 0 -a -n "$network_bridge" ] && append bss_conf "upnp_iface=$network_bridge" "$N"
299
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"
309         }
310
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"
316         }
317
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"
323                 else
324                         set_default auth_cache 0
325                 fi
326
327                 append bss_conf "okc=$auth_cache" "$N"
328                 [ "$auth_cache" = 0 ] && append bss_conf "disable_pmksa_caching=1" "$N"
329
330                 # RSN -> allow management frame protection
331                 json_get_var ieee80211w ieee80211w
332                 case "$ieee80211w" in
333                         [012])
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"
341                                 }
342                         ;;
343                 esac
344         fi
345
346         _macfile="/var/run/hostapd-$ifname.maclist"
347         case "$macfilter" in
348                 allow)
349                         append bss_conf "macaddr_acl=1" "$N"
350                         append bss_conf "accept_mac_file=$_macfile" "$N"
351                 ;;
352                 deny)
353                         append bss_conf "macaddr_acl=0" "$N"
354                         append bss_conf "deny_mac_file=$_macfile" "$N"
355                 ;;
356                 *)
357                         _macfile=""
358                 ;;
359         esac
360
361         [ -n "$_macfile" ] && {
362                 json_get_vars macfile
363                 json_get_values maclist maclist
364
365                 rm -f "$_macfile"
366                 (
367                         for mac in $maclist; do
368                                 echo "$mac"
369                         done
370                         [ -n "$macfile" -a -f "$macfile" ] && cat "$macfile"
371                 ) > "$_macfile"
372         }
373
374         append "$var" "$bss_conf" "$N"
375         return 0
376 }
377
378 hostapd_set_log_options() {
379         local var="$1"
380
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
383
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
392
393         local log_mask=$(( \
394                 ($log_80211  << 0) | \
395                 ($log_8021x  << 1) | \
396                 ($log_radius << 2) | \
397                 ($log_wpa    << 3) | \
398                 ($log_driver << 4) | \
399                 ($log_iapp   << 5) | \
400                 ($log_mlme   << 6)   \
401         ))
402
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"
407
408         return 0
409 }
410
411 _wpa_supplicant_common() {
412         local ifname="$1"
413
414         _rpath="/var/run/wpa_supplicant"
415         _config="${_rpath}-$ifname.conf"
416 }
417
418 wpa_supplicant_teardown_interface() {
419         _wpa_supplicant_common "$1"
420         rm -rf "$_rpath/$1" "$_config"
421 }
422
423 wpa_supplicant_prepare_interface() {
424         local ifname="$1"
425         _w_driver="$2"
426
427         _wpa_supplicant_common "$1"
428
429         json_get_vars mode wds
430
431         [ -n "$network_bridge" ] && {
432                 fail=
433                 case "$mode" in
434                         adhoc)
435                                 fail=1
436                         ;;
437                         sta)
438                                 [ "$wds" = 1 ] || fail=1
439                         ;;
440                 esac
441
442                 [ -n "$fail" ] && {
443                         wireless_setup_vif_failed BRIDGE_NOT_ALLOWED
444                         return 1
445                 }
446         }
447
448         local ap_scan=
449
450         _w_mode="$mode"
451         _w_modestr=
452
453         [[ "$mode" = adhoc ]] && {
454                 ap_scan="ap_scan=2"
455
456                 _w_modestr="mode=1"
457         }
458
459         wpa_supplicant_teardown_interface "$ifname"
460         cat > "$_config" <<EOF
461 $ap_scan
462 EOF
463         return 0
464 }
465
466 wpa_supplicant_add_network() {
467         local ifname="$1"
468
469         _wpa_supplicant_common "$1"
470         wireless_vif_parse_encryption
471
472         json_get_vars \
473                 ssid bssid key \
474                 basic_rate mcast_rate \
475                 ieee80211w
476
477         local key_mgmt='NONE'
478         local enc_str=
479         local network_data=
480         local T="       "
481
482         local wpa_key_mgmt="WPA-PSK"
483         local scan_ssid="1"
484         local freq
485
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"
492                 }
493
494                 scan_ssid=0
495
496                 [ "$_w_driver" = "nl80211" ] || wpa_key_mgmt="WPA-NONE"
497         }
498
499         [[ "$_w_mode" = adhoc ]] && append network_data "$_w_modestr" "$N$T"
500
501         case "$auth_type" in
502                 none) ;;
503                 wep)
504                         local wep_keyidx=0
505                         hostapd_append_wep_key network_data
506                         append network_data "wep_tx_keyidx=$wep_keyidx" "$N$T"
507                 ;;
508                 psk)
509                         local passphrase
510
511                         key_mgmt="$wpa_key_mgmt"
512                         if [ ${#key} -eq 64 ]; then
513                                 passphrase="psk=${key}"
514                         else
515                                 passphrase="psk=\"${key}\""
516                         fi
517                         append network_data "$passphrase" "$N$T"
518                 ;;
519                 eap)
520                         key_mgmt='WPA-EAP'
521
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"
525                         case "$eap_type" in
526                                 tls)
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"
531                                 ;;
532                                 peap|ttls)
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"
537                                 ;;
538                         esac
539                         append network_data "eap=$(echo $eap_type | tr 'a-z' 'A-Z')" "$N$T"
540                 ;;
541         esac
542
543         case "$wpa" in
544                 1)
545                         append network_data "proto=WPA" "$N$T"
546                 ;;
547                 2)
548                         append network_data "proto=RSN" "$N$T"
549                 ;;
550         esac
551
552         case "$ieee80211w" in
553                 [012])
554                         [ "$wpa" -ge 2 ] && append network_data "ieee80211w=$ieee80211w" "$N$T"
555                 ;;
556         esac
557
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"
561
562
563         [ -n "$basic_rate" ] && {
564                 local br rate_list=
565                 for br in $basic_rate; do
566                         hostapd_add_rate rate_list "$br"
567                 done
568                 [ -n "$rate_list" ] && append network_data "rates=$rate_list" "$N$T"
569         }
570
571         [ -n "$mcast_rate" ] && {
572                 local mc_rate=
573                 hostapd_add_rate mc_rate "$mcast_rate"
574                 append network_data "mcast_rate=$mc_rate" "$N$T"
575         }
576
577         local ht_str
578         [ -n "$ht" ] && append network_data "htmode=$ht" "$N$T"
579
580         cat >> "$_config" <<EOF
581 network={
582         scan_ssid=$scan_ssid
583         ssid="$ssid"
584         key_mgmt=$key_mgmt
585         $network_data
586 }
587 EOF
588         return 0
589 }
590
591 wpa_supplicant_run() {
592         local ifname="$1"; shift
593
594         _wpa_supplicant_common "$ifname"
595
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} \
600                 -i "$ifname" \
601                 -c "$_config" \
602                 -C "$_rpath" \
603                 "$@"
604
605         ret="$?"
606         wireless_add_process "$(cat "/var/run/wpa_supplicant-${ifname}.pid")" /usr/sbin/wpa_supplicant 1
607
608         [ "$ret" != 0 ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
609
610         return $ret
611 }
612
613 hostapd_common_cleanup() {
614         killall hostapd wpa_supplicant meshd-nl80211
615 }