hostapd: add 802.11r support
[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 wpa_key_mgmt
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                         append wpa_key_mgmt "WPA-PSK"
96                 ;;
97                 *wpa*|*8021x*)
98                         # required fields? formats?
99                         # hostapd is particular, maybe a default configuration for failures
100                         config_get auth_server "$vif" auth_server
101                         [ -z "$auth_server" ] && config_get auth_server "$vif" server
102                         append "$var" "auth_server_addr=$auth_server" "$N"
103                         config_get auth_port "$vif" auth_port
104                         [ -z "$auth_port" ] && config_get auth_port "$vif" port
105                         auth_port=${auth_port:-1812}
106                         append "$var" "auth_server_port=$auth_port" "$N"
107                         config_get auth_secret "$vif" auth_secret
108                         [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
109                         append "$var" "auth_server_shared_secret=$auth_secret" "$N"
110                         # You don't really want to enable this unless you are doing
111                         # some corner case testing or are using OpenWrt as a work around
112                         # for some systematic issues.
113                         config_get_bool auth_cache "$vif" auth_cache 0
114                         config_get rsn_preauth "$vif" rsn_preauth
115                         [ "$auth_cache" -gt 0 ] || [[ "$rsn_preauth" = 1 ]] || append "$var" "disable_pmksa_caching=1" "$N"
116                         [ "$auth_cache" -gt 0 ] || [[ "$rsn_preauth" = 1 ]] || append "$var" "okc=0" "$N"
117                         config_get acct_server "$vif" acct_server
118                         [ -n "$acct_server" ] && append "$var" "acct_server_addr=$acct_server" "$N"
119                         config_get acct_port "$vif" acct_port
120                         [ -n "$acct_port" ] && acct_port=${acct_port:-1813}
121                         [ -n "$acct_port" ] && append "$var" "acct_server_port=$acct_port" "$N"
122                         config_get acct_secret "$vif" acct_secret
123                         [ -n "$acct_secret" ] && append "$var" "acct_server_shared_secret=$acct_secret" "$N"
124                         config_get eap_reauth_period "$vif" eap_reauth_period
125                         [ -n "$eap_reauth_period" ] && append "$var" "eap_reauth_period=$eap_reauth_period" "$N"
126                         config_get dae_client "$vif" dae_client
127                         config_get dae_secret "$vif" dae_secret
128                         [ -n "$dae_client" -a -n "$dae_secret" ] && {
129                                 config_get dae_port  "$vif" dae_port
130                                 append "$var" "radius_das_port=${dae_port:-3799}" "$N"
131                                 append "$var" "radius_das_client=$dae_client $dae_secret" "$N"
132                         }
133                         config_get ownip "$vif" ownip
134                         append "$var" "own_ip_addr=$ownip" "$N"
135                         append "$var" "eapol_key_index_workaround=1" "$N"
136                         append "$var" "ieee8021x=1" "$N"
137                         append wpa_key_mgmt "WPA-EAP"
138                         [ -n "$wpa_group_rekey"  ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
139                         [ -n "$wpa_pair_rekey"   ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey"    "$N"
140                         [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey"  "$N"
141                 ;;
142                 *wep*)
143                         config_get key "$vif" key
144                         key="${key:-1}"
145                         case "$key" in
146                                 [1234])
147                                         for idx in 1 2 3 4; do
148                                                 local zidx
149                                                 zidx=$(($idx - 1))
150                                                 config_get ckey "$vif" "key${idx}"
151                                                 [ -n "$ckey" ] && \
152                                                         append "$var" "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N"
153                                         done
154                                         append "$var" "wep_default_key=$((key - 1))"  "$N"
155                                 ;;
156                                 *)
157                                         append "$var" "wep_key0=$(prepare_key_wep "$key")" "$N"
158                                         append "$var" "wep_default_key=0" "$N"
159                                         [ -n "$wep_rekey" ] && append "$var" "wep_rekey_period=$wep_rekey" "$N"
160                                 ;;
161                         esac
162                         case "$enc" in
163                                 *shared*)
164                                         auth_algs=2
165                                 ;;
166                                 *mixed*)
167                                         auth_algs=3
168                                 ;;
169                         esac
170                         wpa=0
171                         crypto=
172                 ;;
173                 *)
174                         wpa=0
175                         crypto=
176                 ;;
177         esac
178         append "$var" "auth_algs=${auth_algs:-1}" "$N"
179         append "$var" "wpa=$wpa" "$N"
180         [ -n "$crypto" ] && append "$var" "wpa_pairwise=$crypto" "$N"
181         [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
182
183         config_get ssid "$vif" ssid
184         config_get bridge "$vif" bridge
185         config_get ieee80211d "$vif" ieee80211d
186         config_get iapp_interface "$vif" iapp_interface
187
188         config_get_bool wps_pbc "$vif" wps_pushbutton 0
189         config_get_bool wps_label "$vif" wps_label 0
190
191         config_get config_methods "$vif" wps_config
192         [ "$wps_pbc" -gt 0 ] && append config_methods push_button
193
194         [ -n "$wps_possible" -a -n "$config_methods" ] && {
195                 config_get device_type "$vif" wps_device_type "6-0050F204-1"
196                 config_get device_name "$vif" wps_device_name "OpenWrt AP"
197                 config_get manufacturer "$vif" wps_manufacturer "openwrt.org"
198                 config_get wps_pin "$vif" wps_pin
199
200                 config_get_bool ext_registrar "$vif" ext_registrar 0
201                 [ "$ext_registrar" -gt 0 -a -n "$bridge" ] && append "$var" "upnp_iface=$bridge" "$N"
202
203                 append "$var" "eap_server=1" "$N"
204                 [ -n "$wps_pin" ] && append "$var" "ap_pin=$wps_pin" "$N"
205                 append "$var" "wps_state=${wps_not_configured:-2}" "$N"
206                 append "$var" "ap_setup_locked=0" "$N"
207                 append "$var" "device_type=$device_type" "$N"
208                 append "$var" "device_name=$device_name" "$N"
209                 append "$var" "manufacturer=$manufacturer" "$N"
210                 append "$var" "config_methods=$config_methods" "$N"
211         }
212
213         append "$var" "ssid=$ssid" "$N"
214         [ -n "$bridge" ] && append "$var" "bridge=$bridge" "$N"
215         [ -n "$ieee80211d" ] && append "$var" "ieee80211d=$ieee80211d" "$N"
216         [ -n "$iapp_interface" ] && append "$var" iapp_interface=$(uci_get_state network "$iapp_interface" ifname "$iapp_interface") "$N"
217
218         if [ "$wpa" -ge "1" ]
219         then
220                 config_get nasid "$vif" nasid
221                 [ -n "$nasid" ] && append "$var" "nas_identifier=$nasid" "$N"
222
223                 config_get_bool ieee80211r "$vif" ieee80211r 0
224                 if [ "$ieee80211r" -gt 0 ]
225                 then
226                         config_get mobility_domain "$vif" mobility_domain "4f57"
227                         config_get r0_key_lifetime "$vif" r0_key_lifetime "10000"
228                         config_get r1_key_holder "$vif" r1_key_holder "00004f577274"
229                         config_get reassociation_deadline "$vif" reassociation_deadline "1000"
230                         config_get r0kh "$vif" r0kh
231                         config_get r1kh "$vif" r1kh
232                         config_get_bool pmk_r1_push "$vif" pmk_r1_push 0
233
234                         append "$var" "mobility_domain=$mobility_domain" "$N"
235                         append "$var" "r0_key_lifetime=$r0_key_lifetime" "$N"
236                         append "$var" "r1_key_holder=$r1_key_holder" "$N"
237                         append "$var" "reassociation_deadline=$reassociation_deadline" "$N"
238                         append "$var" "pmk_r1_push=$pmk_r1_push" "$N"
239
240                         for kh in $r0kh; do
241                                 "$var" "r0kh=${kh//,/ }" "$N"
242                         done
243                         for kh in $r1kh; do
244                                 "$var" "r1kh=${kh//,/ }" "$N"
245                         done
246
247                         [ "$wpa_key_mgmt" != "${wpa_key_mgmt/EAP/}" ] && append wpa_key_mgmt "FT-EAP"
248                         [ "$wpa_key_mgmt" != "${wpa_key_mgmt/PSK/}" ] && append wpa_key_mgmt "FT-PSK"
249                 fi
250
251                 [ -n "wpa_key_mgmt" ] && append "$var" "wpa_key_mgmt=$wpa_key_mgmt"
252         fi
253
254         if [ "$wpa" -ge "2" ]
255         then
256                 # RSN -> allow preauthentication. You have two
257                 # options, rsn_preauth for production or rsn_preauth_testing
258                 # for validation / testing.
259                 if [ -n "$bridge" -a "$rsn_preauth" = 1 ]
260                 then
261                         append "$var" "rsn_preauth=1" "$N"
262                         append "$var" "rsn_preauth_interfaces=$bridge" "$N"
263                         append "$var" "okc=1" "$N"
264                 else
265                         # RSN preauthentication testings hould disable
266                         # Opportunistic Key Caching (okc) as otherwise the PMKSA
267                         # entry for a test could come from the Opportunistic Key Caching
268                         config_get rsn_preauth_testing "$vif" rsn_preauth_testing
269                         if [ -n "$bridge" -a "$rsn_preauth_testing" = 1 ]
270                         then
271                                 append "$var" "rsn_preauth=1" "$N"
272                                 append "$var" "rsn_preauth_interfaces=$bridge" "$N"
273                                 append "$var" "okc=0" "$N"
274                         fi
275                 fi
276
277                 # RSN -> allow management frame protection
278                 config_get ieee80211w "$vif" ieee80211w
279                 case "$ieee80211w" in
280                         [012])
281                                 append "$var" "ieee80211w=$ieee80211w" "$N"
282                                 [ "$ieee80211w" -gt "0" ] && {
283                                         config_get ieee80211w_max_timeout "$vif" ieee80211w_max_timeout
284                                         config_get ieee80211w_retry_timeout "$vif" ieee80211w_retry_timeout
285                                         [ -n "$ieee80211w_max_timeout" ] && \
286                                                 append "$var" "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
287                                         [ -n "$ieee80211w_retry_timeout" ] && \
288                                                 append "$var" "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
289                                 }
290                         ;;
291                 esac
292         fi
293
294         config_get macfile "$vif" macfile
295         config_get maclist "$vif" maclist
296         if [ -z "$macfile" ]
297         then
298                 # if no macfile has been specified, fallback to the default name
299                 # and truncate file to avoid aggregating entries over time
300                 macfile="/var/run/hostapd-$ifname.maclist"
301                 echo "" > "$macfile"
302         else
303                 if [ -n "$maclist" ]
304                 then
305                         # to avoid to overwrite the original file, make a copy
306                         # before appending the entries specified by the maclist
307                         # option
308                         cp $macfile $macfile.maclist
309                         macfile=$macfile.maclist
310                 fi
311         fi
312
313         if [ -n "$maclist" ]
314         then
315                 for mac in $maclist; do
316                         echo "$mac" >> $macfile
317                 done
318         fi
319
320         config_get macfilter "$vif" macfilter
321         case "$macfilter" in
322                 allow)
323                         append "$var" "macaddr_acl=1" "$N"
324                         append "$var" "accept_mac_file=$macfile" "$N"
325                         ;;
326                 deny)
327                         append "$var" "macaddr_acl=0" "$N"
328                         append "$var" "deny_mac_file=$macfile" "$N"
329                         ;;
330         esac
331 }
332
333 hostapd_set_log_options() {
334         local var="$1"
335         local cfg="$2"
336         local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
337
338         config_get log_level "$cfg" log_level 2
339
340         config_get_bool log_80211  "$cfg" log_80211  1
341         config_get_bool log_8021x  "$cfg" log_8021x  1
342         config_get_bool log_radius "$cfg" log_radius 1
343         config_get_bool log_wpa    "$cfg" log_wpa    1
344         config_get_bool log_driver "$cfg" log_driver 1
345         config_get_bool log_iapp   "$cfg" log_iapp   1
346         config_get_bool log_mlme   "$cfg" log_mlme   1
347
348         local log_mask=$((       \
349                 ($log_80211  << 0) | \
350                 ($log_8021x  << 1) | \
351                 ($log_radius << 2) | \
352                 ($log_wpa    << 3) | \
353                 ($log_driver << 4) | \
354                 ($log_iapp   << 5) | \
355                 ($log_mlme   << 6)   \
356         ))
357
358         append "$var" "logger_syslog=$log_mask" "$N"
359         append "$var" "logger_syslog_level=$log_level" "$N"
360         append "$var" "logger_stdout=$log_mask" "$N"
361         append "$var" "logger_stdout_level=$log_level" "$N"
362 }
363
364 hostapd_setup_vif() {
365         local vif="$1"
366         local driver="$2"
367         local ifname device channel hwmode
368
369         hostapd_cfg=
370
371         config_get ifname "$vif" ifname
372         config_get device "$vif" device
373         config_get channel "$device" channel
374         config_get hwmode "$device" hwmode
375
376         hostapd_set_log_options hostapd_cfg "$device"
377         hostapd_set_bss_options hostapd_cfg "$vif"
378
379         case "$hwmode" in
380                 *bg|*gdt|*gst|*fh) hwmode=g;;
381                 *adt|*ast) hwmode=a;;
382         esac
383         [ "$channel" = auto ] && channel=
384         [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
385         cat > /var/run/hostapd-$ifname.conf <<EOF
386 driver=$driver
387 interface=$ifname
388 ${hwmode:+hw_mode=${hwmode#11}}
389 ${channel:+channel=$channel}
390 $hostapd_cfg
391 EOF
392         hostapd -P /var/run/wifi-$ifname.pid -B /var/run/hostapd-$ifname.conf
393 }
394