hostapd: truncate default mac file before adding entries to it (#13797)
[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
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                 *psk*)
77                         config_get psk "$vif" key
78                         if [ ${#psk} -eq 64 ]; then
79                                 append "$var" "wpa_psk=$psk" "$N"
80                         else
81                                 append "$var" "wpa_passphrase=$psk" "$N"
82                         fi
83                         wps_possible=1
84                         [ -n "$wpa_group_rekey"  ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
85                         [ -n "$wpa_pair_rekey"   ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey"    "$N"
86                         [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey"  "$N"
87                 ;;
88                 *wpa*)
89                         # required fields? formats?
90                         # hostapd is particular, maybe a default configuration for failures
91                         config_get auth_server "$vif" auth_server
92                         [ -z "$auth_server" ] && config_get auth_server "$vif" server
93                         append "$var" "auth_server_addr=$auth_server" "$N"
94                         config_get auth_port "$vif" auth_port
95                         [ -z "$auth_port" ] && config_get auth_port "$vif" port
96                         auth_port=${auth_port:-1812}
97                         append "$var" "auth_server_port=$auth_port" "$N"
98                         config_get auth_secret "$vif" auth_secret
99                         [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
100                         append "$var" "auth_server_shared_secret=$auth_secret" "$N"
101                         config_get_bool auth_cache "$vif" auth_cache 0
102                         [ "$auth_cache" -gt 0 ] || append "$var" "disable_pmksa_caching=1" "$N"
103                         [ "$auth_cache" -gt 0 ] || append "$var" "okc=0" "$N"
104                         config_get acct_server "$vif" acct_server
105                         [ -n "$acct_server" ] && append "$var" "acct_server_addr=$acct_server" "$N"
106                         config_get acct_port "$vif" acct_port
107                         [ -n "$acct_port" ] && acct_port=${acct_port:-1813}
108                         [ -n "$acct_port" ] && append "$var" "acct_server_port=$acct_port" "$N"
109                         config_get acct_secret "$vif" acct_secret
110                         [ -n "$acct_secret" ] && append "$var" "acct_server_shared_secret=$acct_secret" "$N"
111                         config_get nasid "$vif" nasid
112                         append "$var" "nas_identifier=$nasid" "$N"
113                         append "$var" "eapol_key_index_workaround=1" "$N"
114                         append "$var" "ieee8021x=1" "$N"
115                         append "$var" "wpa_key_mgmt=WPA-EAP" "$N"
116                         [ -n "$wpa_group_rekey"  ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
117                         [ -n "$wpa_pair_rekey"   ] && append "$var" "wpa_ptk_rekey=$wpa_pair_rekey"    "$N"
118                         [ -n "$wpa_master_rekey" ] && append "$var" "wpa_gmk_rekey=$wpa_master_rekey"  "$N"
119                 ;;
120                 *wep*)
121                         config_get key "$vif" key
122                         key="${key:-1}"
123                         case "$key" in
124                                 [1234])
125                                         for idx in 1 2 3 4; do
126                                                 local zidx
127                                                 zidx=$(($idx - 1))
128                                                 config_get ckey "$vif" "key${idx}"
129                                                 [ -n "$ckey" ] && \
130                                                         append "$var" "wep_key${zidx}=$(prepare_key_wep "$ckey")" "$N"
131                                         done
132                                         append "$var" "wep_default_key=$((key - 1))"  "$N"
133                                 ;;
134                                 *)
135                                         append "$var" "wep_key0=$(prepare_key_wep "$key")" "$N"
136                                         append "$var" "wep_default_key=0" "$N"
137                                         [ -n "$wep_rekey" ] && append "$var" "wep_rekey_period=$wep_rekey" "$N"
138                                 ;;
139                         esac
140                         case "$enc" in
141                                 *shared*)
142                                         auth_algs=2
143                                 ;;
144                                 *mixed*)
145                                         auth_algs=3
146                                 ;;
147                         esac
148                         wpa=0
149                         crypto=
150                 ;;
151                 *)
152                         wpa=0
153                         crypto=
154                 ;;
155         esac
156         append "$var" "auth_algs=${auth_algs:-1}" "$N"
157         append "$var" "wpa=$wpa" "$N"
158         [ -n "$crypto" ] && append "$var" "wpa_pairwise=$crypto" "$N"
159         [ -n "$wpa_group_rekey" ] && append "$var" "wpa_group_rekey=$wpa_group_rekey" "$N"
160
161         config_get ssid "$vif" ssid
162         config_get bridge "$vif" bridge
163         config_get ieee80211d "$vif" ieee80211d
164         config_get iapp_interface "$vif" iapp_interface
165
166         config_get_bool wps_pbc "$vif" wps_pushbutton 0
167         config_get_bool wps_label "$vif" wps_label 0
168
169         config_get config_methods "$vif" wps_config
170         [ "$wps_pbc" -gt 0 ] && append config_methods push_button
171
172         [ -n "$wps_possible" -a -n "$config_methods" ] && {
173                 config_get device_type "$vif" wps_device_type "6-0050F204-1"
174                 config_get device_name "$vif" wps_device_name "OpenWrt AP"
175                 config_get manufacturer "$vif" wps_manufacturer "openwrt.org"
176
177                 append "$var" "eap_server=1" "$N"
178                 append "$var" "wps_state=2" "$N"
179                 append "$var" "ap_setup_locked=1" "$N"
180                 append "$var" "device_type=$device_type" "$N"
181                 append "$var" "device_name=$device_name" "$N"
182                 append "$var" "manufacturer=$manufacturer" "$N"
183                 append "$var" "config_methods=$config_methods" "$N"
184         }
185
186         append "$var" "ssid=$ssid" "$N"
187         [ -n "$bridge" ] && append "$var" "bridge=$bridge" "$N"
188         [ -n "$ieee80211d" ] && append "$var" "ieee80211d=$ieee80211d" "$N"
189         [ -n "$iapp_interface" ] && append "$var" iapp_interface=$(uci_get_state network "$iapp_interface" ifname "$iapp_interface") "$N"
190
191         if [ "$wpa" -ge "2" ]
192         then
193                 # RSN -> allow preauthentication
194                 config_get_bool rsn_preauth "$vif" rsn_preauth "$auth_cache"
195                 if [ -n "$bridge" -a "$rsn_preauth" = 1 ]
196                 then
197                         append "$var" "rsn_preauth=1" "$N"
198                         append "$var" "rsn_preauth_interfaces=$bridge" "$N"
199                 fi
200
201                 # RSN -> allow management frame protection
202                 config_get ieee80211w "$vif" ieee80211w
203                 case "$ieee80211w" in
204                         [012])
205                                 append "$var" "ieee80211w=$ieee80211w" "$N"
206                                 [ "$ieee80211w" -gt "0" ] && {
207                                         config_get ieee80211w_max_timeout "$vif" ieee80211w_max_timeout
208                                         config_get ieee80211w_retry_timeout "$vif" ieee80211w_retry_timeout
209                                         [ -n "$ieee80211w_max_timeout" ] && \
210                                                 append "$var" "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
211                                         [ -n "$ieee80211w_retry_timeout" ] && \
212                                                 append "$var" "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
213                                 }
214                         ;;
215                 esac
216         fi
217
218         config_get macfile "$vif" macfile
219         config_get maclist "$vif" maclist
220         if [ -z "$macfile" ]
221         then
222                 # if no macfile has been specified, fallback to the default name
223                 # and truncate file to avoid aggregating entries over time
224                 macfile="/var/run/hostapd-$ifname.maclist"
225                 echo "" > "$macfile"
226         else
227                 if [ -n "$maclist" ]
228                 then
229                         # to avoid to overwrite the original file, make a copy
230                         # before appending the entries specified by the maclist
231                         # option
232                         cp $macfile $macfile.maclist
233                         macfile=$macfile.maclist
234                 fi
235         fi
236
237         if [ -n "$maclist" ]
238         then
239                 for mac in $maclist; do
240                         echo "$mac" >> $macfile
241                 done
242         fi
243
244         config_get macfilter "$vif" macfilter
245         case "$macfilter" in
246                 allow)
247                         append "$var" "macaddr_acl=1" "$N"
248                         append "$var" "accept_mac_file=$macfile" "$N"
249                         ;;
250                 deny)
251                         append "$var" "macaddr_acl=0" "$N"
252                         append "$var" "deny_mac_file=$macfile" "$N"
253                         ;;
254         esac
255 }
256
257 hostapd_set_log_options() {
258         local var="$1"
259         local cfg="$2"
260         local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
261
262         config_get log_level "$cfg" log_level 2
263
264         config_get_bool log_80211  "$cfg" log_80211  1
265         config_get_bool log_8021x  "$cfg" log_8021x  1
266         config_get_bool log_radius "$cfg" log_radius 1
267         config_get_bool log_wpa    "$cfg" log_wpa    1
268         config_get_bool log_driver "$cfg" log_driver 1
269         config_get_bool log_iapp   "$cfg" log_iapp   1
270         config_get_bool log_mlme   "$cfg" log_mlme   1
271
272         local log_mask=$((       \
273                 ($log_80211  << 0) | \
274                 ($log_8021x  << 1) | \
275                 ($log_radius << 2) | \
276                 ($log_wpa    << 3) | \
277                 ($log_driver << 4) | \
278                 ($log_iapp   << 5) | \
279                 ($log_mlme   << 6)   \
280         ))
281
282         append "$var" "logger_syslog=$log_mask" "$N"
283         append "$var" "logger_syslog_level=$log_level" "$N"
284         append "$var" "logger_stdout=$log_mask" "$N"
285         append "$var" "logger_stdout_level=$log_level" "$N"
286 }
287
288 hostapd_setup_vif() {
289         local vif="$1"
290         local driver="$2"
291         local ifname device channel hwmode
292
293         hostapd_cfg=
294
295         config_get ifname "$vif" ifname
296         config_get device "$vif" device
297         config_get channel "$device" channel
298         config_get hwmode "$device" hwmode
299
300         hostapd_set_log_options hostapd_cfg "$device"
301         hostapd_set_bss_options hostapd_cfg "$vif"
302
303         case "$hwmode" in
304                 *bg|*gdt|*gst|*fh) hwmode=g;;
305                 *adt|*ast) hwmode=a;;
306         esac
307         [ "$channel" = auto ] && channel=
308         [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
309         cat > /var/run/hostapd-$ifname.conf <<EOF
310 driver=$driver
311 interface=$ifname
312 ${hwmode:+hw_mode=${hwmode#11}}
313 ${channel:+channel=$channel}
314 $hostapd_cfg
315 EOF
316         hostapd -P /var/run/wifi-$ifname.pid -B /var/run/hostapd-$ifname.conf
317 }
318