hostapd: fix hostapd RSN preauthentication PMKSA caching
[openwrt/openwrt.git] / package / network / services / hostapd / files / hostapd.sh
1 hostapd_set_bss_options() {
2 local var="$1"
3 local vif="$2"
4 local enc wep_rekey wpa_group_rekey wpa_pair_rekey wpa_master_rekey wps_possible
5
6 config_get enc "$vif" encryption "none"
7 config_get wep_rekey "$vif" wep_rekey # 300
8 config_get wpa_group_rekey "$vif" wpa_group_rekey # 300
9 config_get wpa_pair_rekey "$vif" wpa_pair_rekey # 300
10 config_get wpa_master_rekey "$vif" wpa_master_rekey # 640
11 config_get_bool ap_isolate "$vif" isolate 0
12 config_get_bool disassoc_low_ack "$vif" disassoc_low_ack 1
13 config_get max_num_sta "$vif" max_num_sta 0
14 config_get max_inactivity "$vif" max_inactivity 0
15 config_get_bool preamble "$vif" short_preamble 1
16
17 config_get device "$vif" device
18 config_get hwmode "$device" hwmode
19 config_get phy "$device" phy
20
21 append "$var" "ctrl_interface=/var/run/hostapd-$phy" "$N"
22
23 if [ "$ap_isolate" -gt 0 ]; then
24 append "$var" "ap_isolate=$ap_isolate" "$N"
25 fi
26 if [ "$max_num_sta" -gt 0 ]; then
27 append "$var" "max_num_sta=$max_num_sta" "$N"
28 fi
29 if [ "$max_inactivity" -gt 0 ]; then
30 append "$var" "ap_max_inactivity=$max_inactivity" "$N"
31 fi
32 append "$var" "disassoc_low_ack=$disassoc_low_ack" "$N"
33 if [ "$preamble" -gt 0 ]; then
34 append "$var" "preamble=$preamble" "$N"
35 fi
36
37 # Examples:
38 # psk-mixed/tkip => WPA1+2 PSK, TKIP
39 # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
40 # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
41 # ...
42
43 # TODO: move this parsing function somewhere generic, so that
44 # later it can be reused by drivers that don't use hostapd
45
46 # crypto defaults: WPA2 vs WPA1
47 case "$enc" in
48 wpa2*|*psk2*)
49 wpa=2
50 crypto="CCMP"
51 ;;
52 *mixed*)
53 wpa=3
54 crypto="CCMP TKIP"
55 ;;
56 *)
57 wpa=1
58 crypto="TKIP"
59 ;;
60 esac
61
62 # explicit override for crypto setting
63 case "$enc" in
64 *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) crypto="CCMP TKIP";;
65 *aes|*ccmp) crypto="CCMP";;
66 *tkip) crypto="TKIP";;
67 esac
68
69 # enforce CCMP for 11ng and 11na
70 case "$hwmode:$crypto" in
71 *ng:TKIP|*na:TKIP) crypto="CCMP TKIP";;
72 esac
73
74 # use crypto/auth settings for building the hostapd config
75 case "$enc" in
76 none)
77 wps_possible=1
78 wpa=0
79 crypto=
80 # Here we make the assumption that if we're in open mode
81 # with WPS enabled, we got to be in unconfigured state.
82 wps_not_configured=1
83 ;;
84 *psk*)
85 config_get psk "$vif" key
86 if [ ${#psk} -eq 64 ]; then
87 append "$var" "wpa_psk=$psk" "$N"
88 else
89 append "$var" "wpa_passphrase=$psk" "$N"
90 fi
91 wps_possible=1
92 [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
93 [ -n "$wpa_pair_rekey" ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
94 [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey" "$N"
95 ;;
96 *wpa*)
97 # required fields? formats?
98 # hostapd is particular, maybe a default configuration for failures
99 config_get auth_server "$vif" auth_server
100 [ -z "$auth_server" ] && config_get auth_server "$vif" server
101 append "$var" "auth_server_addr=$auth_server" "$N"
102 config_get auth_port "$vif" auth_port
103 [ -z "$auth_port" ] && config_get auth_port "$vif" port
104 auth_port=${auth_port:-1812}
105 append "$var" "auth_server_port=$auth_port" "$N"
106 config_get auth_secret "$vif" auth_secret
107 [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
108 append "$var" "auth_server_shared_secret=$auth_secret" "$N"
109 # You don't really want to enable this unless you are doing
110 # some corner case testing or are using OpenWrt as a work around
111 # for some systematic issues.
112 config_get_bool auth_cache "$vif" auth_cache 0
113 config_get rsn_preauth "$vif" rsn_preauth
114 [ "$auth_cache" -gt 0 ] || [[ "$rsn_preauth" = 1 ]] || append "$var" "disable_pmksa_caching=1" "$N"
115 [ "$auth_cache" -gt 0 ] || [[ "$rsn_preauth" = 1 ]] || append "$var" "okc=0" "$N"
116 config_get acct_server "$vif" acct_server
117 [ -n "$acct_server" ] && append "$var" "acct_server_addr=$acct_server" "$N"
118 config_get acct_port "$vif" acct_port
119 [ -n "$acct_port" ] && acct_port=${acct_port:-1813}
120 [ -n "$acct_port" ] && append "$var" "acct_server_port=$acct_port" "$N"
121 config_get acct_secret "$vif" acct_secret
122 [ -n "$acct_secret" ] && append "$var" "acct_server_shared_secret=$acct_secret" "$N"
123 config_get eap_reauth_period "$vif" eap_reauth_period
124 [ -n "$eap_reauth_period" ] && append "$var" "eap_reauth_period=$eap_reauth_period" "$N"
125 config_get dae_client "$vif" dae_client
126 config_get dae_secret "$vif" dae_secret
127 [ -n "$dae_client" -a -n "$dae_secret" ] && {
128 config_get dae_port "$vif" dae_port
129 append "$var" "radius_das_port=${dae_port:-3799}" "$N"
130 append "$var" "radius_das_client=$dae_client $dae_secret" "$N"
131 }
132 config_get nasid "$vif" nasid
133 append "$var" "nas_identifier=$nasid" "$N"
134 append "$var" "eapol_key_index_workaround=1" "$N"
135 append "$var" "ieee8021x=1" "$N"
136 append "$var" "wpa_key_mgmt=WPA-EAP" "$N"
137 [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
138 [ -n "$wpa_pair_rekey" ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey" "$N"
139 [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey" "$N"
140 ;;
141 *wep*)
142 config_get key "$vif" key
143 key="${key:-1}"
144 case "$key" in
145 [1234])
146 for idx in 1 2 3 4; do
147 local zidx
148 zidx=$(($idx - 1))
149 config_get ckey "$vif" "key${idx}"
150 [ -n "$ckey" ] && \
151 append "$var" "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N"
152 done
153 append "$var" "wep_default_key=$((key - 1))" "$N"
154 ;;
155 *)
156 append "$var" "wep_key0=$(prepare_key_wep "$key")" "$N"
157 append "$var" "wep_default_key=0" "$N"
158 [ -n "$wep_rekey" ] && append "$var" "wep_rekey_period=$wep_rekey" "$N"
159 ;;
160 esac
161 case "$enc" in
162 *shared*)
163 auth_algs=2
164 ;;
165 *mixed*)
166 auth_algs=3
167 ;;
168 esac
169 wpa=0
170 crypto=
171 ;;
172 *)
173 wpa=0
174 crypto=
175 ;;
176 esac
177 append "$var" "auth_algs=${auth_algs:-1}" "$N"
178 append "$var" "wpa=$wpa" "$N"
179 [ -n "$crypto" ] && append "$var" "wpa_pairwise=$crypto" "$N"
180 [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
181
182 config_get ssid "$vif" ssid
183 config_get bridge "$vif" bridge
184 config_get ieee80211d "$vif" ieee80211d
185 config_get iapp_interface "$vif" iapp_interface
186
187 config_get_bool wps_pbc "$vif" wps_pushbutton 0
188 config_get_bool wps_label "$vif" wps_label 0
189
190 config_get config_methods "$vif" wps_config
191 [ "$wps_pbc" -gt 0 ] && append config_methods push_button
192
193 [ -n "$wps_possible" -a -n "$config_methods" ] && {
194 config_get device_type "$vif" wps_device_type "6-0050F204-1"
195 config_get device_name "$vif" wps_device_name "OpenWrt AP"
196 config_get manufacturer "$vif" wps_manufacturer "openwrt.org"
197 config_get wps_pin "$vif" wps_pin "12345670"
198
199 append "$var" "eap_server=1" "$N"
200 append "$var" "ap_pin=$wps_pin" "$N"
201 append "$var" "wps_state=${wps_not_configured:-2}" "$N"
202 append "$var" "ap_setup_locked=0" "$N"
203 append "$var" "device_type=$device_type" "$N"
204 append "$var" "device_name=$device_name" "$N"
205 append "$var" "manufacturer=$manufacturer" "$N"
206 append "$var" "config_methods=$config_methods" "$N"
207 }
208
209 append "$var" "ssid=$ssid" "$N"
210 [ -n "$bridge" ] && append "$var" "bridge=$bridge" "$N"
211 [ -n "$ieee80211d" ] && append "$var" "ieee80211d=$ieee80211d" "$N"
212 [ -n "$iapp_interface" ] && append "$var" iapp_interface=$(uci_get_state network "$iapp_interface" ifname "$iapp_interface") "$N"
213
214 if [ "$wpa" -ge "2" ]
215 then
216 # RSN -> allow preauthentication. You have two
217 # options, rsn_preauth for production or rsn_preauth_testing
218 # for validation / testing.
219 if [ -n "$bridge" -a "$rsn_preauth" = 1 ]
220 then
221 append "$var" "rsn_preauth=1" "$N"
222 append "$var" "rsn_preauth_interfaces=$bridge" "$N"
223 append "$var" "okc=1" "$N"
224 else
225 # RSN preauthentication testings hould disable
226 # Opportunistic Key Caching (okc) as otherwise the PMKSA
227 # entry for a test could come from the Opportunistic Key Caching
228 config_get rsn_preauth_testing "$vif" rsn_preauth_testing
229 if [ -n "$bridge" -a "$rsn_preauth_testing" = 1 ]
230 then
231 append "$var" "rsn_preauth=1" "$N"
232 append "$var" "rsn_preauth_interfaces=$bridge" "$N"
233 append "$var" "okc=0" "$N"
234 fi
235 fi
236
237 # RSN -> allow management frame protection
238 config_get ieee80211w "$vif" ieee80211w
239 case "$ieee80211w" in
240 [012])
241 append "$var" "ieee80211w=$ieee80211w" "$N"
242 [ "$ieee80211w" -gt "0" ] && {
243 config_get ieee80211w_max_timeout "$vif" ieee80211w_max_timeout
244 config_get ieee80211w_retry_timeout "$vif" ieee80211w_retry_timeout
245 [ -n "$ieee80211w_max_timeout" ] && \
246 append "$var" "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
247 [ -n "$ieee80211w_retry_timeout" ] && \
248 append "$var" "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
249 }
250 ;;
251 esac
252 fi
253
254 config_get macfile "$vif" macfile
255 config_get maclist "$vif" maclist
256 if [ -z "$macfile" ]
257 then
258 # if no macfile has been specified, fallback to the default name
259 # and truncate file to avoid aggregating entries over time
260 macfile="/var/run/hostapd-$ifname.maclist"
261 echo "" > "$macfile"
262 else
263 if [ -n "$maclist" ]
264 then
265 # to avoid to overwrite the original file, make a copy
266 # before appending the entries specified by the maclist
267 # option
268 cp $macfile $macfile.maclist
269 macfile=$macfile.maclist
270 fi
271 fi
272
273 if [ -n "$maclist" ]
274 then
275 for mac in $maclist; do
276 echo "$mac" >> $macfile
277 done
278 fi
279
280 config_get macfilter "$vif" macfilter
281 case "$macfilter" in
282 allow)
283 append "$var" "macaddr_acl=1" "$N"
284 append "$var" "accept_mac_file=$macfile" "$N"
285 ;;
286 deny)
287 append "$var" "macaddr_acl=0" "$N"
288 append "$var" "deny_mac_file=$macfile" "$N"
289 ;;
290 esac
291 }
292
293 hostapd_set_log_options() {
294 local var="$1"
295 local cfg="$2"
296 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
297
298 config_get log_level "$cfg" log_level 2
299
300 config_get_bool log_80211 "$cfg" log_80211 1
301 config_get_bool log_8021x "$cfg" log_8021x 1
302 config_get_bool log_radius "$cfg" log_radius 1
303 config_get_bool log_wpa "$cfg" log_wpa 1
304 config_get_bool log_driver "$cfg" log_driver 1
305 config_get_bool log_iapp "$cfg" log_iapp 1
306 config_get_bool log_mlme "$cfg" log_mlme 1
307
308 local log_mask=$(( \
309 ($log_80211 << 0) | \
310 ($log_8021x << 1) | \
311 ($log_radius << 2) | \
312 ($log_wpa << 3) | \
313 ($log_driver << 4) | \
314 ($log_iapp << 5) | \
315 ($log_mlme << 6) \
316 ))
317
318 append "$var" "logger_syslog=$log_mask" "$N"
319 append "$var" "logger_syslog_level=$log_level" "$N"
320 append "$var" "logger_stdout=$log_mask" "$N"
321 append "$var" "logger_stdout_level=$log_level" "$N"
322 }
323
324 hostapd_setup_vif() {
325 local vif="$1"
326 local driver="$2"
327 local ifname device channel hwmode
328
329 hostapd_cfg=
330
331 config_get ifname "$vif" ifname
332 config_get device "$vif" device
333 config_get channel "$device" channel
334 config_get hwmode "$device" hwmode
335
336 hostapd_set_log_options hostapd_cfg "$device"
337 hostapd_set_bss_options hostapd_cfg "$vif"
338
339 case "$hwmode" in
340 *bg|*gdt|*gst|*fh) hwmode=g;;
341 *adt|*ast) hwmode=a;;
342 esac
343 [ "$channel" = auto ] && channel=
344 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
345 cat > /var/run/hostapd-$ifname.conf <<EOF
346 driver=$driver
347 interface=$ifname
348 ${hwmode:+hw_mode=${hwmode#11}}
349 ${channel:+channel=$channel}
350 $hostapd_cfg
351 EOF
352 hostapd -P /var/run/wifi-$ifname.pid -B /var/run/hostapd-$ifname.conf
353 }
354