hostapd: append nasid to config for all WPA types
[openwrt/svn-archive/archive.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*|*8021x*)
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 ownip "$vif" ownip
133 append "$var" "own_ip_addr=$ownip" "$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
198
199 config_get_bool ext_registrar "$vif" ext_registrar 0
200 [ "$ext_registrar" -gt 0 -a -n "$bridge" ] && append "$var" "upnp_iface=$bridge" "$N"
201
202 append "$var" "eap_server=1" "$N"
203 [ -n "$wps_pin" ] && append "$var" "ap_pin=$wps_pin" "$N"
204 append "$var" "wps_state=${wps_not_configured:-2}" "$N"
205 append "$var" "ap_setup_locked=0" "$N"
206 append "$var" "device_type=$device_type" "$N"
207 append "$var" "device_name=$device_name" "$N"
208 append "$var" "manufacturer=$manufacturer" "$N"
209 append "$var" "config_methods=$config_methods" "$N"
210 }
211
212 append "$var" "ssid=$ssid" "$N"
213 [ -n "$bridge" ] && append "$var" "bridge=$bridge" "$N"
214 [ -n "$ieee80211d" ] && append "$var" "ieee80211d=$ieee80211d" "$N"
215 [ -n "$iapp_interface" ] && append "$var" iapp_interface=$(uci_get_state network "$iapp_interface" ifname "$iapp_interface") "$N"
216
217 if [ "$wpa" -ge "1" ]
218 then
219 config_get nasid "$vif" nasid
220 [ -n "$nasid" ] && append "$var" "nas_identifier=$nasid" "$N"
221 fi
222
223 if [ "$wpa" -ge "2" ]
224 then
225 # RSN -> allow preauthentication. You have two
226 # options, rsn_preauth for production or rsn_preauth_testing
227 # for validation / testing.
228 if [ -n "$bridge" -a "$rsn_preauth" = 1 ]
229 then
230 append "$var" "rsn_preauth=1" "$N"
231 append "$var" "rsn_preauth_interfaces=$bridge" "$N"
232 append "$var" "okc=1" "$N"
233 else
234 # RSN preauthentication testings hould disable
235 # Opportunistic Key Caching (okc) as otherwise the PMKSA
236 # entry for a test could come from the Opportunistic Key Caching
237 config_get rsn_preauth_testing "$vif" rsn_preauth_testing
238 if [ -n "$bridge" -a "$rsn_preauth_testing" = 1 ]
239 then
240 append "$var" "rsn_preauth=1" "$N"
241 append "$var" "rsn_preauth_interfaces=$bridge" "$N"
242 append "$var" "okc=0" "$N"
243 fi
244 fi
245
246 # RSN -> allow management frame protection
247 config_get ieee80211w "$vif" ieee80211w
248 case "$ieee80211w" in
249 [012])
250 append "$var" "ieee80211w=$ieee80211w" "$N"
251 [ "$ieee80211w" -gt "0" ] && {
252 config_get ieee80211w_max_timeout "$vif" ieee80211w_max_timeout
253 config_get ieee80211w_retry_timeout "$vif" ieee80211w_retry_timeout
254 [ -n "$ieee80211w_max_timeout" ] && \
255 append "$var" "assoc_sa_query_max_timeout=$ieee80211w_max_timeout" "$N"
256 [ -n "$ieee80211w_retry_timeout" ] && \
257 append "$var" "assoc_sa_query_retry_timeout=$ieee80211w_retry_timeout" "$N"
258 }
259 ;;
260 esac
261 fi
262
263 config_get macfile "$vif" macfile
264 config_get maclist "$vif" maclist
265 if [ -z "$macfile" ]
266 then
267 # if no macfile has been specified, fallback to the default name
268 # and truncate file to avoid aggregating entries over time
269 macfile="/var/run/hostapd-$ifname.maclist"
270 echo "" > "$macfile"
271 else
272 if [ -n "$maclist" ]
273 then
274 # to avoid to overwrite the original file, make a copy
275 # before appending the entries specified by the maclist
276 # option
277 cp $macfile $macfile.maclist
278 macfile=$macfile.maclist
279 fi
280 fi
281
282 if [ -n "$maclist" ]
283 then
284 for mac in $maclist; do
285 echo "$mac" >> $macfile
286 done
287 fi
288
289 config_get macfilter "$vif" macfilter
290 case "$macfilter" in
291 allow)
292 append "$var" "macaddr_acl=1" "$N"
293 append "$var" "accept_mac_file=$macfile" "$N"
294 ;;
295 deny)
296 append "$var" "macaddr_acl=0" "$N"
297 append "$var" "deny_mac_file=$macfile" "$N"
298 ;;
299 esac
300 }
301
302 hostapd_set_log_options() {
303 local var="$1"
304 local cfg="$2"
305 local log_level log_80211 log_8021x log_radius log_wpa log_driver log_iapp log_mlme
306
307 config_get log_level "$cfg" log_level 2
308
309 config_get_bool log_80211 "$cfg" log_80211 1
310 config_get_bool log_8021x "$cfg" log_8021x 1
311 config_get_bool log_radius "$cfg" log_radius 1
312 config_get_bool log_wpa "$cfg" log_wpa 1
313 config_get_bool log_driver "$cfg" log_driver 1
314 config_get_bool log_iapp "$cfg" log_iapp 1
315 config_get_bool log_mlme "$cfg" log_mlme 1
316
317 local log_mask=$(( \
318 ($log_80211 << 0) | \
319 ($log_8021x << 1) | \
320 ($log_radius << 2) | \
321 ($log_wpa << 3) | \
322 ($log_driver << 4) | \
323 ($log_iapp << 5) | \
324 ($log_mlme << 6) \
325 ))
326
327 append "$var" "logger_syslog=$log_mask" "$N"
328 append "$var" "logger_syslog_level=$log_level" "$N"
329 append "$var" "logger_stdout=$log_mask" "$N"
330 append "$var" "logger_stdout_level=$log_level" "$N"
331 }
332
333 hostapd_setup_vif() {
334 local vif="$1"
335 local driver="$2"
336 local ifname device channel hwmode
337
338 hostapd_cfg=
339
340 config_get ifname "$vif" ifname
341 config_get device "$vif" device
342 config_get channel "$device" channel
343 config_get hwmode "$device" hwmode
344
345 hostapd_set_log_options hostapd_cfg "$device"
346 hostapd_set_bss_options hostapd_cfg "$vif"
347
348 case "$hwmode" in
349 *bg|*gdt|*gst|*fh) hwmode=g;;
350 *adt|*ast) hwmode=a;;
351 esac
352 [ "$channel" = auto ] && channel=
353 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
354 cat > /var/run/hostapd-$ifname.conf <<EOF
355 driver=$driver
356 interface=$ifname
357 ${hwmode:+hw_mode=${hwmode#11}}
358 ${channel:+channel=$channel}
359 $hostapd_cfg
360 EOF
361 hostapd -P /var/run/wifi-$ifname.pid -B /var/run/hostapd-$ifname.conf
362 }
363