Updated to hostap_2_6
[mech_eap.git] / libeap / tests / hwsim / test_ap_params.py
1 # Test various AP mode parameters
2 # Copyright (c) 2014, Qualcomm Atheros, Inc.
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 from remotehost import remote_compatible
8 import logging
9 logger = logging.getLogger()
10 import os
11
12 import hwsim_utils
13 import hostapd
14 from tshark import run_tshark
15 from utils import alloc_fail
16
17 @remote_compatible
18 def test_ap_fragmentation_rts_set_high(dev, apdev):
19     """WPA2-PSK AP with fragmentation and RTS thresholds larger than frame length"""
20     ssid = "test-wpa2-psk"
21     passphrase = 'qwertyuiop'
22     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
23     params['rts_threshold'] = "1000"
24     params['fragm_threshold'] = "2000"
25     hapd = hostapd.add_ap(apdev[0], params)
26     dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
27     hwsim_utils.test_connectivity(dev[0], hapd)
28
29 @remote_compatible
30 def test_ap_fragmentation_open(dev, apdev):
31     """Open AP with fragmentation threshold"""
32     ssid = "fragmentation"
33     params = {}
34     params['ssid'] = ssid
35     params['fragm_threshold'] = "1000"
36     hapd = hostapd.add_ap(apdev[0], params)
37     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
38     hwsim_utils.test_connectivity(dev[0], hapd)
39
40 @remote_compatible
41 def test_ap_fragmentation_wpa2(dev, apdev):
42     """WPA2-PSK AP with fragmentation threshold"""
43     ssid = "test-wpa2-psk"
44     passphrase = 'qwertyuiop'
45     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
46     params['fragm_threshold'] = "1000"
47     hapd = hostapd.add_ap(apdev[0], params)
48     dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
49     hwsim_utils.test_connectivity(dev[0], hapd)
50
51 def test_ap_vendor_elements(dev, apdev):
52     """WPA2-PSK AP with vendor elements added"""
53     bssid = apdev[0]['bssid']
54     ssid = "test-wpa2-psk"
55     passphrase = 'qwertyuiop'
56     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
57     params['vendor_elements'] = "dd0411223301"
58     params['assocresp_elements'] = "dd0411223302"
59     hapd = hostapd.add_ap(apdev[0], params)
60     dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
61     bss = dev[0].get_bss(bssid)
62     if "dd0411223301" not in bss['ie']:
63         raise Exception("Vendor element not shown in scan results")
64
65     hapd.set('vendor_elements', 'dd051122330203dd0400137400dd04001374ff')
66     if "OK" not in hapd.request("UPDATE_BEACON"):
67         raise Exception("UPDATE_BEACON failed")
68     dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
69     bss = dev[1].get_bss(bssid)
70     if "dd0411223301" in bss['ie']:
71         raise Exception("Old vendor element still in scan results")
72     if "dd051122330203" not in bss['ie']:
73         raise Exception("New vendor element not shown in scan results")
74
75 def test_ap_element_parse(dev, apdev):
76     """Information element parsing - extra coverage"""
77     bssid = apdev[0]['bssid']
78     ssid = "test-wpa2-psk"
79     params = { 'ssid': ssid,
80                'vendor_elements': "380501020304059e009e009e009e009e009e00" }
81     hapd = hostapd.add_ap(apdev[0], params)
82     dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
83     bss = dev[0].get_bss(bssid)
84     if "38050102030405" not in bss['ie']:
85         raise Exception("Timeout element not shown in scan results")
86
87 @remote_compatible
88 def test_ap_element_parse_oom(dev, apdev):
89     """Information element parsing OOM"""
90     bssid = apdev[0]['bssid']
91     ssid = "test-wpa2-psk"
92     params = { 'ssid': ssid,
93                'vendor_elements': "dd0d506f9a0a00000600411c440028" }
94     hapd = hostapd.add_ap(apdev[0], params)
95     dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
96     with alloc_fail(dev[0], 1, "wpabuf_alloc;ieee802_11_vendor_ie_concat"):
97         bss = dev[0].get_bss(bssid)
98         logger.info(str(bss))
99
100 def test_ap_country(dev, apdev):
101     """WPA2-PSK AP setting country code and using 5 GHz band"""
102     try:
103         hapd = None
104         bssid = apdev[0]['bssid']
105         ssid = "test-wpa2-psk"
106         passphrase = 'qwertyuiop'
107         params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
108         params['country_code'] = 'FI'
109         params['ieee80211d'] = '1'
110         params['hw_mode'] = 'a'
111         params['channel'] = '36'
112         hapd = hostapd.add_ap(apdev[0], params)
113         dev[0].connect(ssid, psk=passphrase, scan_freq="5180")
114         hwsim_utils.test_connectivity(dev[0], hapd)
115     finally:
116         dev[0].request("DISCONNECT")
117         if hapd:
118             hapd.request("DISABLE")
119         hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
120         dev[0].flush_scan_cache()
121
122 def test_ap_acl_accept(dev, apdev):
123     """MAC ACL accept list"""
124     ssid = "acl"
125     params = {}
126     params['ssid'] = ssid
127     params['accept_mac_file'] = "hostapd.macaddr"
128     hapd = hostapd.add_ap(apdev[0], params)
129     dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
130     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
131     dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
132     dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
133     dev[0].request("REMOVE_NETWORK all")
134     dev[1].request("REMOVE_NETWORK all")
135     hapd.request("SET macaddr_acl 1")
136     dev[1].dump_monitor()
137     dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
138     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
139     ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
140     if ev is not None:
141         raise Exception("Unexpected association")
142
143 def test_ap_acl_deny(dev, apdev):
144     """MAC ACL deny list"""
145     ssid = "acl"
146     params = {}
147     params['ssid'] = ssid
148     params['deny_mac_file'] = "hostapd.macaddr"
149     hapd = hostapd.add_ap(apdev[0], params)
150     dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
151     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
152     dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
153     dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
154     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
155     if ev is not None:
156         raise Exception("Unexpected association")
157
158 @remote_compatible
159 def test_ap_wds_sta(dev, apdev):
160     """WPA2-PSK AP with STA using 4addr mode"""
161     ssid = "test-wpa2-psk"
162     passphrase = 'qwertyuiop'
163     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
164     params['wds_sta'] = "1"
165     params['wds_bridge'] = "wds-br0"
166     hapd = hostapd.add_ap(apdev[0], params)
167
168     try:
169         dev[0].cmd_execute(['brctl', 'addbr', 'wds-br0'])
170         dev[0].cmd_execute(['brctl', 'setfd', 'wds-br0', '0'])
171         dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'up'])
172         dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'on'])
173         dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
174         hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
175                                             max_tries=15)
176     finally:
177         dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'off'])
178         dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'down'])
179         dev[0].cmd_execute(['brctl', 'delbr', 'wds-br0'])
180
181 @remote_compatible
182 def test_ap_inactivity_poll(dev, apdev):
183     """AP using inactivity poll"""
184     ssid = "test-wpa2-psk"
185     passphrase = 'qwertyuiop'
186     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
187     params['ap_max_inactivity'] = "1"
188     hapd = hostapd.add_ap(apdev[0], params)
189     dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
190     hapd.set("ext_mgmt_frame_handling", "1")
191     dev[0].request("DISCONNECT")
192     ev = hapd.wait_event(["MGMT-RX"], timeout=5)
193     if ev is None:
194         raise Exception("MGMT RX wait timed out for Deauth")
195     hapd.set("ext_mgmt_frame_handling", "0")
196     ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=30)
197     if ev is None:
198         raise Exception("STA disconnection on inactivity was not reported")
199
200 @remote_compatible
201 def test_ap_inactivity_disconnect(dev, apdev):
202     """AP using inactivity disconnect"""
203     ssid = "test-wpa2-psk"
204     passphrase = 'qwertyuiop'
205     params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
206     params['ap_max_inactivity'] = "1"
207     params['skip_inactivity_poll'] = "1"
208     hapd = hostapd.add_ap(apdev[0], params)
209     dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
210     hapd.set("ext_mgmt_frame_handling", "1")
211     dev[0].request("DISCONNECT")
212     ev = hapd.wait_event(["MGMT-RX"], timeout=5)
213     if ev is None:
214         raise Exception("MGMT RX wait timed out for Deauth")
215     hapd.set("ext_mgmt_frame_handling", "0")
216     ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=30)
217     if ev is None:
218         raise Exception("STA disconnection on inactivity was not reported")
219
220 @remote_compatible
221 def test_ap_basic_rates(dev, apdev):
222     """Open AP with lots of basic rates"""
223     ssid = "basic rates"
224     params = {}
225     params['ssid'] = ssid
226     params['basic_rates'] = "10 20 55 110 60 90 120 180 240 360 480 540"
227     hostapd.add_ap(apdev[0], params)
228     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
229
230 @remote_compatible
231 def test_ap_short_preamble(dev, apdev):
232     """Open AP with short preamble"""
233     ssid = "short preamble"
234     params = {}
235     params['ssid'] = ssid
236     params['preamble'] = "1"
237     hostapd.add_ap(apdev[0], params)
238     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
239
240 def test_ap_spectrum_management_required(dev, apdev):
241     """Open AP with spectrum management required"""
242     ssid = "spectrum mgmt"
243     params = {}
244     params['ssid'] = ssid
245     params["country_code"] = "JP"
246     params["hw_mode"] = "a"
247     params["channel"] = "36"
248     params["ieee80211d"] = "1"
249     params["local_pwr_constraint"] = "3"
250     params['spectrum_mgmt_required'] = "1"
251     try:
252         hapd = None
253         hapd = hostapd.add_ap(apdev[0], params)
254         dev[0].connect(ssid, key_mgmt="NONE", scan_freq="5180")
255     finally:
256         dev[0].request("DISCONNECT")
257         if hapd:
258             hapd.request("DISABLE")
259         hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
260         dev[0].flush_scan_cache()
261
262 @remote_compatible
263 def test_ap_max_listen_interval(dev, apdev):
264     """Open AP with maximum listen interval limit"""
265     ssid = "listen"
266     params = {}
267     params['ssid'] = ssid
268     params['max_listen_interval'] = "1"
269     hostapd.add_ap(apdev[0], params)
270     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
271     ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
272     if ev is None:
273         raise Exception("Association rejection not reported")
274     if "status_code=51" not in ev:
275         raise Exception("Unexpected ASSOC-REJECT reason")
276
277 @remote_compatible
278 def test_ap_max_num_sta(dev, apdev):
279     """Open AP with maximum STA count"""
280     ssid = "max"
281     params = {}
282     params['ssid'] = ssid
283     params['max_num_sta'] = "1"
284     hostapd.add_ap(apdev[0], params)
285     dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
286     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
287     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
288     if ev is not None:
289         raise Exception("Unexpected association")
290
291 def test_ap_max_num_sta_no_probe_resp(dev, apdev, params):
292     """Maximum STA count and limit on Probe Response frames"""
293     logdir = params['logdir']
294     dev[0].flush_scan_cache()
295     ssid = "max"
296     params = {}
297     params['ssid'] = ssid
298     params['beacon_int'] = "2000"
299     params['max_num_sta'] = "1"
300     params['no_probe_resp_if_max_sta'] = "1"
301     hostapd.add_ap(apdev[0], params)
302     dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
303     dev[0].scan(freq=2412, type="ONLY")
304     dev[0].scan(freq=2412, type="ONLY")
305     seen = dev[0].get_bss(apdev[0]['bssid']) != None
306     dev[1].scan(freq=2412, type="ONLY")
307     if seen:
308         out = run_tshark(os.path.join(logdir, "hwsim0.pcapng"),
309                          "wlan.fc.type_subtype == 5", ["wlan.da" ])
310         if out:
311             if dev[0].own_addr() not in out:
312                 # Discovery happened through Beacon frame reception. That's not
313                 # an error case.
314                 seen = False
315             if dev[1].own_addr() not in out:
316                 raise Exception("No Probe Response frames to dev[1] seen")
317         if seen:
318             raise Exception("AP found unexpectedly")
319
320 @remote_compatible
321 def test_ap_tx_queue_params(dev, apdev):
322     """Open AP with TX queue params set"""
323     ssid = "tx"
324     params = {}
325     params['ssid'] = ssid
326     params['tx_queue_data2_aifs'] = "4"
327     params['tx_queue_data2_cwmin'] = "7"
328     params['tx_queue_data2_cwmax'] = "1023"
329     params['tx_queue_data2_burst'] = "4.2"
330     params['tx_queue_data1_aifs'] = "4"
331     params['tx_queue_data1_cwmin'] = "7"
332     params['tx_queue_data1_cwmax'] = "1023"
333     params['tx_queue_data1_burst'] = "2"
334     hapd = hostapd.add_ap(apdev[0], params)
335     dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
336     hwsim_utils.test_connectivity(dev[0], hapd)