]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_ap_open.py
tests: Add basic power saving tests for ap_open
[thirdparty/hostap.git] / tests / hwsim / test_ap_open.py
1 # Open mode AP tests
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 struct
11 import subprocess
12 import time
13 import os
14
15 import hostapd
16 import hwsim_utils
17 from tshark import run_tshark
18 from utils import *
19 from wpasupplicant import WpaSupplicant
20 from wlantest import WlantestCapture
21 from test_ap_ht import set_world_reg
22
23 @remote_compatible
24 def test_ap_open(dev, apdev):
25 """AP with open mode (no security) configuration"""
26 _test_ap_open(dev, apdev)
27
28 def _test_ap_open(dev, apdev):
29 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
30 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
31 bg_scan_period="0")
32 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
33 if ev is None:
34 raise Exception("No connection event received from hostapd")
35 hwsim_utils.test_connectivity(dev[0], hapd)
36
37 dev[0].request("DISCONNECT")
38 ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=5)
39 if ev is None:
40 raise Exception("No disconnection event received from hostapd")
41
42 def test_ap_open_packet_loss(dev, apdev):
43 """AP with open mode configuration and large packet loss"""
44 params = {"ssid": "open",
45 "ignore_probe_probability": "0.5",
46 "ignore_auth_probability": "0.5",
47 "ignore_assoc_probability": "0.5",
48 "ignore_reassoc_probability": "0.5"}
49 hapd = hostapd.add_ap(apdev[0], params)
50 for i in range(0, 3):
51 dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
52 wait_connect=False)
53 for i in range(0, 3):
54 dev[i].wait_connected(timeout=20)
55
56 @remote_compatible
57 def test_ap_open_unknown_action(dev, apdev):
58 """AP with open mode configuration and unknown Action frame"""
59 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
60 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
61 bssid = apdev[0]['bssid']
62 cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
63 if "FAIL" in dev[0].request(cmd):
64 raise Exception("Could not send test Action frame")
65 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
66 if ev is None:
67 raise Exception("Timeout on MGMT-TX-STATUS")
68 if "result=SUCCESS" not in ev:
69 raise Exception("AP did not ack Action frame")
70
71 def test_ap_open_invalid_wmm_action(dev, apdev):
72 """AP with open mode configuration and invalid WMM Action frame"""
73 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
74 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
75 bssid = apdev[0]['bssid']
76 cmd = "MGMT_TX {} {} freq=2412 action=1100".format(bssid, bssid)
77 if "FAIL" in dev[0].request(cmd):
78 raise Exception("Could not send test Action frame")
79 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
80 if ev is None or "result=SUCCESS" not in ev:
81 raise Exception("AP did not ack Action frame")
82
83 @remote_compatible
84 def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
85 """Reconnect to open mode AP after inactivity related disconnection"""
86 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
87 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
88 hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
89 dev[0].wait_disconnected(timeout=5)
90 dev[0].wait_connected(timeout=2, error="Timeout on reconnection")
91
92 @remote_compatible
93 def test_ap_open_assoc_timeout(dev, apdev):
94 """AP timing out association"""
95 ssid = "test"
96 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
97 dev[0].scan(freq="2412")
98 hapd.set("ext_mgmt_frame_handling", "1")
99 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
100 wait_connect=False)
101 for i in range(0, 10):
102 req = hapd.mgmt_rx()
103 if req is None:
104 raise Exception("MGMT RX wait timed out")
105 if req['subtype'] == 11:
106 break
107 req = None
108 if not req:
109 raise Exception("Authentication frame not received")
110
111 resp = {}
112 resp['fc'] = req['fc']
113 resp['da'] = req['sa']
114 resp['sa'] = req['da']
115 resp['bssid'] = req['bssid']
116 resp['payload'] = struct.pack('<HHH', 0, 2, 0)
117 hapd.mgmt_tx(resp)
118
119 assoc = 0
120 for i in range(0, 10):
121 req = hapd.mgmt_rx()
122 if req is None:
123 raise Exception("MGMT RX wait timed out")
124 if req['subtype'] == 0:
125 assoc += 1
126 if assoc == 3:
127 break
128 if assoc != 3:
129 raise Exception("Association Request frames not received: assoc=%d" % assoc)
130 hapd.set("ext_mgmt_frame_handling", "0")
131 dev[0].wait_connected(timeout=15)
132
133 def test_ap_open_auth_drop_sta(dev, apdev):
134 """AP dropping station after successful authentication"""
135 hapd = hostapd.add_ap(apdev[0]['ifname'], {"ssid": "open"})
136 dev[0].scan(freq="2412")
137 hapd.set("ext_mgmt_frame_handling", "1")
138 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
139 wait_connect=False)
140 for i in range(0, 10):
141 req = hapd.mgmt_rx()
142 if req is None:
143 raise Exception("MGMT RX wait timed out")
144 if req['subtype'] == 11:
145 break
146 req = None
147 if not req:
148 raise Exception("Authentication frame not received")
149
150 # turn off before sending successful response
151 hapd.set("ext_mgmt_frame_handling", "0")
152
153 resp = {}
154 resp['fc'] = req['fc']
155 resp['da'] = req['sa']
156 resp['sa'] = req['da']
157 resp['bssid'] = req['bssid']
158 resp['payload'] = struct.pack('<HHH', 0, 2, 0)
159 hapd.mgmt_tx(resp)
160
161 dev[0].wait_connected(timeout=15)
162
163 @remote_compatible
164 def test_ap_open_id_str(dev, apdev):
165 """AP with open mode and id_str"""
166 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
167 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412", id_str="foo",
168 wait_connect=False)
169 ev = dev[0].wait_connected(timeout=10)
170 if "id_str=foo" not in ev:
171 raise Exception("CTRL-EVENT-CONNECT did not have matching id_str: " + ev)
172 if dev[0].get_status_field("id_str") != "foo":
173 raise Exception("id_str mismatch")
174
175 @remote_compatible
176 def test_ap_open_select_any(dev, apdev):
177 """AP with open mode and select any network"""
178 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
179 id = dev[0].connect("unknown", key_mgmt="NONE", scan_freq="2412",
180 only_add_network=True)
181 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
182 only_add_network=True)
183 dev[0].select_network(id)
184 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND",
185 "CTRL-EVENT-CONNECTED"], timeout=10)
186 if ev is None:
187 raise Exception("No result reported")
188 if "CTRL-EVENT-CONNECTED" in ev:
189 raise Exception("Unexpected connection")
190
191 dev[0].select_network("any")
192 dev[0].wait_connected(timeout=10)
193
194 @remote_compatible
195 def test_ap_open_unexpected_assoc_event(dev, apdev):
196 """AP with open mode and unexpected association event"""
197 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
198 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
199 dev[0].request("DISCONNECT")
200 dev[0].wait_disconnected(timeout=15)
201 dev[0].dump_monitor()
202 # This will be accepted due to matching network
203 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
204 apdev[0]['bssid']])
205 dev[0].wait_connected(timeout=15)
206 dev[0].dump_monitor()
207
208 dev[0].request("REMOVE_NETWORK all")
209 dev[0].wait_disconnected(timeout=5)
210 dev[0].dump_monitor()
211 # This will result in disconnection due to no matching network
212 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
213 apdev[0]['bssid']])
214 dev[0].wait_disconnected(timeout=15)
215
216 def test_ap_open_external_assoc(dev, apdev):
217 """AP with open mode and external association"""
218 hapd = hostapd.add_ap(apdev[0], {"ssid": "open-ext-assoc"})
219 try:
220 dev[0].request("STA_AUTOCONNECT 0")
221 id = dev[0].connect("open-ext-assoc", key_mgmt="NONE", scan_freq="2412",
222 only_add_network=True)
223 dev[0].request("ENABLE_NETWORK %s no-connect" % id)
224 dev[0].dump_monitor()
225 # This will be accepted due to matching network
226 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname, 'connect',
227 'open-ext-assoc', "2412", apdev[0]['bssid']])
228 ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED",
229 "CTRL-EVENT-CONNECTED"], timeout=10)
230 if ev is None:
231 raise Exception("Connection timed out")
232 if "CTRL-EVENT-DISCONNECTED" in ev:
233 raise Exception("Unexpected disconnection event")
234 dev[0].dump_monitor()
235 dev[0].request("DISCONNECT")
236 dev[0].wait_disconnected(timeout=5)
237 finally:
238 dev[0].request("STA_AUTOCONNECT 1")
239
240 @remote_compatible
241 def test_ap_bss_load(dev, apdev):
242 """AP with open mode (no security) configuration"""
243 hapd = hostapd.add_ap(apdev[0],
244 {"ssid": "open",
245 "bss_load_update_period": "10",
246 "chan_util_avg_period": "20"})
247 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
248 # this does not really get much useful output with mac80211_hwsim currently,
249 # but run through the channel survey update couple of times
250 for i in range(0, 10):
251 hwsim_utils.test_connectivity(dev[0], hapd)
252 hwsim_utils.test_connectivity(dev[0], hapd)
253 hwsim_utils.test_connectivity(dev[0], hapd)
254 time.sleep(0.15)
255 avg = hapd.get_status_field("chan_util_avg")
256 if avg is None:
257 raise Exception("No STATUS chan_util_avg seen")
258
259 def test_ap_bss_load_fail(dev, apdev):
260 """BSS Load update failing to get survey data"""
261 hapd = hostapd.add_ap(apdev[0],
262 {"ssid": "open",
263 "bss_load_update_period": "1"})
264 with fail_test(hapd, 1, "wpa_driver_nl80211_get_survey"):
265 wait_fail_trigger(hapd, "GET_FAIL")
266
267 def hapd_out_of_mem(hapd, apdev, count, func):
268 with alloc_fail(hapd, count, func):
269 started = False
270 try:
271 hostapd.add_ap(apdev, {"ssid": "open"})
272 started = True
273 except:
274 pass
275 if started:
276 raise Exception("hostapd interface started even with memory allocation failure: %d:%s" % (count, func))
277
278 def test_ap_open_out_of_memory(dev, apdev):
279 """hostapd failing to setup interface due to allocation failure"""
280 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
281 hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
282
283 for i in range(1, 3):
284 hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
285
286 for i in range(1, 5):
287 hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
288
289 hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
290
291 hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
292
293 for i in range(1, 3):
294 hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
295
296 # eloop_register_read_sock() call from i802_init()
297 hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;?eloop_register_sock;?eloop_register_read_sock;=i802_init")
298
299 # verify that a new interface can still be added when memory allocation does
300 # not fail
301 hostapd.add_ap(apdev[1], {"ssid": "open"})
302
303 def test_bssid_black_white_list(dev, apdev):
304 """BSSID black/white list"""
305 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
306 hapd2 = hostapd.add_ap(apdev[1], {"ssid": "open"})
307
308 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
309 bssid_whitelist=apdev[1]['bssid'])
310 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
311 bssid_blacklist=apdev[1]['bssid'])
312 dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
313 bssid_whitelist="00:00:00:00:00:00/00:00:00:00:00:00",
314 bssid_blacklist=apdev[1]['bssid'])
315 if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
316 raise Exception("dev[0] connected to unexpected AP")
317 if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
318 raise Exception("dev[1] connected to unexpected AP")
319 if dev[2].get_status_field('bssid') != apdev[0]['bssid']:
320 raise Exception("dev[2] connected to unexpected AP")
321 dev[0].request("REMOVE_NETWORK all")
322 dev[1].request("REMOVE_NETWORK all")
323 dev[2].request("REMOVE_NETWORK all")
324
325 dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
326 bssid_whitelist="00:00:00:00:00:00", wait_connect=False)
327 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
328 bssid_whitelist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
329 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
330 bssid_blacklist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
331 if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
332 raise Exception("dev[0] connected to unexpected AP")
333 if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
334 raise Exception("dev[1] connected to unexpected AP")
335 dev[0].request("REMOVE_NETWORK all")
336 dev[1].request("REMOVE_NETWORK all")
337 ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
338 if ev is not None:
339 raise Exception("Unexpected dev[2] connectin")
340 dev[2].request("REMOVE_NETWORK all")
341
342 def test_ap_open_wpas_in_bridge(dev, apdev):
343 """Open mode AP and wpas interface in a bridge"""
344 br_ifname = 'sta-br0'
345 ifname = 'wlan5'
346 try:
347 _test_ap_open_wpas_in_bridge(dev, apdev)
348 finally:
349 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
350 subprocess.call(['brctl', 'delif', br_ifname, ifname])
351 subprocess.call(['brctl', 'delbr', br_ifname])
352 subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
353
354 def _test_ap_open_wpas_in_bridge(dev, apdev):
355 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
356
357 br_ifname = 'sta-br0'
358 ifname = 'wlan5'
359 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
360 # First, try a failure case of adding an interface
361 try:
362 wpas.interface_add(ifname, br_ifname=br_ifname)
363 raise Exception("Interface addition succeeded unexpectedly")
364 except Exception as e:
365 if "Failed to add" in str(e):
366 logger.info("Ignore expected interface_add failure due to missing bridge interface: " + str(e))
367 else:
368 raise
369
370 # Next, add the bridge interface and add the interface again
371 subprocess.call(['brctl', 'addbr', br_ifname])
372 subprocess.call(['brctl', 'setfd', br_ifname, '0'])
373 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
374 subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
375 subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
376 wpas.interface_add(ifname, br_ifname=br_ifname)
377
378 wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
379
380 @remote_compatible
381 def test_ap_open_start_disabled(dev, apdev):
382 """AP with open mode and beaconing disabled"""
383 hapd = hostapd.add_ap(apdev[0], {"ssid": "open",
384 "start_disabled": "1"})
385 bssid = apdev[0]['bssid']
386
387 dev[0].flush_scan_cache()
388 dev[0].scan(freq=2412, only_new=True)
389 if dev[0].get_bss(bssid) is not None:
390 raise Exception("AP was seen beaconing")
391 if "OK" not in hapd.request("RELOAD"):
392 raise Exception("RELOAD failed")
393 dev[0].scan_for_bss(bssid, freq=2412)
394 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
395
396 @remote_compatible
397 def test_ap_open_start_disabled2(dev, apdev):
398 """AP with open mode and beaconing disabled (2)"""
399 hapd = hostapd.add_ap(apdev[0], {"ssid": "open",
400 "start_disabled": "1"})
401 bssid = apdev[0]['bssid']
402
403 dev[0].flush_scan_cache()
404 dev[0].scan(freq=2412, only_new=True)
405 if dev[0].get_bss(bssid) is not None:
406 raise Exception("AP was seen beaconing")
407 if "OK" not in hapd.request("UPDATE_BEACON"):
408 raise Exception("UPDATE_BEACON failed")
409 dev[0].scan_for_bss(bssid, freq=2412)
410 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
411 if "OK" not in hapd.request("UPDATE_BEACON"):
412 raise Exception("UPDATE_BEACON failed")
413 dev[0].request("DISCONNECT")
414 dev[0].wait_disconnected()
415 dev[0].request("RECONNECT")
416 dev[0].wait_connected()
417
418 @remote_compatible
419 def test_ap_open_ifdown(dev, apdev):
420 """AP with open mode and external ifconfig down"""
421 params = {"ssid": "open",
422 "ap_max_inactivity": "1"}
423 hapd = hostapd.add_ap(apdev[0], params)
424 bssid = apdev[0]['bssid']
425
426 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
427 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412")
428 hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
429 ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=10)
430 if ev is None:
431 raise Exception("Timeout on AP-STA-DISCONNECTED (1)")
432 ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=5)
433 if ev is None:
434 raise Exception("Timeout on AP-STA-DISCONNECTED (2)")
435 ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=5)
436 if ev is None:
437 raise Exception("No INTERFACE-DISABLED event")
438 # The following wait tests beacon loss detection in mac80211 on dev0.
439 # dev1 is used to test stopping of AP side functionality on client polling.
440 dev[1].request("REMOVE_NETWORK all")
441 hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
442 dev[0].wait_disconnected()
443 dev[1].wait_disconnected()
444 ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
445 if ev is None:
446 raise Exception("No INTERFACE-ENABLED event")
447 dev[0].wait_connected()
448 hwsim_utils.test_connectivity(dev[0], hapd)
449
450 def test_ap_open_disconnect_in_ps(dev, apdev, params):
451 """Disconnect with the client in PS to regression-test a kernel bug"""
452 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
453 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
454 bg_scan_period="0")
455 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
456 if ev is None:
457 raise Exception("No connection event received from hostapd")
458
459 time.sleep(0.2)
460 # enable power save mode
461 hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_ENABLED)
462 time.sleep(0.1)
463 try:
464 # inject some traffic
465 sa = hapd.own_addr()
466 da = dev[0].own_addr()
467 hapd.request('DATA_TEST_CONFIG 1')
468 hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
469 hapd.request('DATA_TEST_CONFIG 0')
470
471 # let the AP send couple of Beacon frames
472 time.sleep(0.3)
473
474 # disconnect - with traffic pending - shouldn't cause kernel warnings
475 dev[0].request("DISCONNECT")
476 finally:
477 hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_DISABLED)
478
479 time.sleep(0.2)
480 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
481 "wlan_mgt.tim.partial_virtual_bitmap",
482 ["wlan_mgt.tim.partial_virtual_bitmap"])
483 if out is not None:
484 state = 0
485 for l in out.splitlines():
486 pvb = int(l, 16)
487 if pvb > 0 and state == 0:
488 state = 1
489 elif pvb == 0 and state == 1:
490 state = 2
491 if state != 2:
492 raise Exception("Didn't observe TIM bit getting set and unset (state=%d)" % state)
493
494 def test_ap_open_sta_ps(dev, apdev):
495 """Station power save operation"""
496 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
497 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
498 bg_scan_period="0")
499 hapd.wait_sta()
500
501 time.sleep(0.2)
502 try:
503 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname,
504 'set', 'power_save', 'on'])
505 run_ap_open_sta_ps(dev, hapd)
506 finally:
507 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname,
508 'set', 'power_save', 'on'])
509
510 def run_ap_open_sta_ps(dev, hapd):
511 hwsim_utils.test_connectivity(dev[0], hapd)
512 # Give time to enter PS
513 time.sleep(0.2)
514
515 phyname = dev[0].get_driver_status_field("phyname")
516 hw_conf = '/sys/kernel/debug/ieee80211/' + phyname + '/hw_conf'
517
518 try:
519 ok = False
520 for i in range(10):
521 with open(hw_conf, 'r') as f:
522 val = int(f.read())
523 if val & 2:
524 ok = True
525 break
526 time.sleep(0.2)
527
528 if not ok:
529 raise Exception("STA did not enter power save")
530 except FileNotFoundError:
531 raise HwsimSkip("Kernel does not support inspecting HW PS state")
532
533 def test_ap_open_ps_mc_buf(dev, apdev, params):
534 """Multicast buffering with a station in power save"""
535 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
536 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
537 bg_scan_period="0")
538 hapd.wait_sta()
539
540 buffered_mcast = 0
541 try:
542 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname,
543 'set', 'power_save', 'on'])
544 # Give time to enter PS
545 time.sleep(0.3)
546
547 for i in range(10):
548 # Verify that multicast frames are released
549 hwsim_utils.run_multicast_connectivity_test(hapd, dev[0])
550
551 # Check frames were buffered until DTIM
552 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
553 "wlan.fc.type_subtype == 0x0008",
554 ["wlan.tim.bmapctl.multicast"])
555 for line in out.splitlines():
556 buffered_mcast = int(line)
557 if buffered_mcast == 1:
558 break
559 if buffered_mcast == 1:
560 break
561 finally:
562 dev[0].cmd_execute(['iw', 'dev', dev[0].ifname,
563 'set', 'power_save', 'off'])
564
565 if buffered_mcast != 1:
566 raise Exception("AP did not buffer multicast frames")
567
568 @remote_compatible
569 def test_ap_open_select_network(dev, apdev):
570 """Open mode connection and SELECT_NETWORK to change network"""
571 hapd1 = hostapd.add_ap(apdev[0], {"ssid": "open"})
572 bssid1 = apdev[0]['bssid']
573 hapd2 = hostapd.add_ap(apdev[1], {"ssid": "open2"})
574 bssid2 = apdev[1]['bssid']
575
576 id1 = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
577 only_add_network=True)
578 id2 = dev[0].connect("open2", key_mgmt="NONE", scan_freq="2412")
579 hwsim_utils.test_connectivity(dev[0], hapd2)
580
581 dev[0].select_network(id1)
582 dev[0].wait_connected()
583 res = dev[0].request("BLACKLIST")
584 if bssid1 in res or bssid2 in res:
585 raise Exception("Unexpected blacklist entry")
586 hwsim_utils.test_connectivity(dev[0], hapd1)
587
588 dev[0].select_network(id2)
589 dev[0].wait_connected()
590 hwsim_utils.test_connectivity(dev[0], hapd2)
591 res = dev[0].request("BLACKLIST")
592 if bssid1 in res or bssid2 in res:
593 raise Exception("Unexpected blacklist entry(2)")
594
595 @remote_compatible
596 def test_ap_open_disable_enable(dev, apdev):
597 """AP with open mode getting disabled and re-enabled"""
598 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
599 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
600 bg_scan_period="0")
601
602 for i in range(2):
603 hapd.request("DISABLE")
604 dev[0].wait_disconnected()
605 hapd.request("ENABLE")
606 dev[0].wait_connected()
607 hwsim_utils.test_connectivity(dev[0], hapd)
608
609 def sta_enable_disable(dev, bssid):
610 dev.scan_for_bss(bssid, freq=2412)
611 work_id = dev.request("RADIO_WORK add block-work")
612 ev = dev.wait_event(["EXT-RADIO-WORK-START"])
613 if ev is None:
614 raise Exception("Timeout while waiting radio work to start")
615 id = dev.connect("open", key_mgmt="NONE", scan_freq="2412",
616 only_add_network=True)
617 dev.request("ENABLE_NETWORK %d" % id)
618 if "connect@" not in dev.request("RADIO_WORK show"):
619 raise Exception("connect radio work missing")
620 dev.request("DISABLE_NETWORK %d" % id)
621 dev.request("RADIO_WORK done " + work_id)
622
623 ok = False
624 for i in range(30):
625 if "connect@" not in dev.request("RADIO_WORK show"):
626 ok = True
627 break
628 time.sleep(0.1)
629 if not ok:
630 raise Exception("connect radio work not completed")
631 ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
632 if ev is not None:
633 raise Exception("Unexpected connection")
634 dev.request("DISCONNECT")
635
636 def test_ap_open_sta_enable_disable(dev, apdev):
637 """AP with open mode and wpa_supplicant ENABLE/DISABLE_NETWORK"""
638 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
639 bssid = apdev[0]['bssid']
640
641 sta_enable_disable(dev[0], bssid)
642
643 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
644 wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
645 sta_enable_disable(wpas, bssid)
646
647 @remote_compatible
648 def test_ap_open_select_twice(dev, apdev):
649 """AP with open mode and select network twice"""
650 id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
651 only_add_network=True)
652 dev[0].select_network(id)
653 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
654 if ev is None:
655 raise Exception("No result reported")
656 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
657 # Verify that the second SELECT_NETWORK starts a new scan immediately by
658 # waiting less than the default scan period.
659 dev[0].select_network(id)
660 dev[0].wait_connected(timeout=3)
661
662 @remote_compatible
663 def test_ap_open_reassoc_not_found(dev, apdev):
664 """AP with open mode and REASSOCIATE not finding a match"""
665 id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
666 only_add_network=True)
667 dev[0].select_network(id)
668 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
669 if ev is None:
670 raise Exception("No result reported")
671 dev[0].request("DISCONNECT")
672
673 time.sleep(0.1)
674 dev[0].dump_monitor()
675
676 dev[0].request("REASSOCIATE")
677 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
678 if ev is None:
679 raise Exception("No result reported")
680 dev[0].request("DISCONNECT")
681
682 @remote_compatible
683 def test_ap_open_sta_statistics(dev, apdev):
684 """AP with open mode and STA statistics"""
685 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
686 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
687 addr = dev[0].own_addr()
688
689 stats1 = hapd.get_sta(addr)
690 logger.info("stats1: " + str(stats1))
691 time.sleep(0.4)
692 stats2 = hapd.get_sta(addr)
693 logger.info("stats2: " + str(stats2))
694 hwsim_utils.test_connectivity(dev[0], hapd)
695 stats3 = hapd.get_sta(addr)
696 logger.info("stats3: " + str(stats3))
697
698 # Cannot require specific inactive_msec changes without getting rid of all
699 # unrelated traffic, so for now, just print out the results in the log for
700 # manual checks.
701
702 @remote_compatible
703 def test_ap_open_poll_sta(dev, apdev):
704 """AP with open mode and STA poll"""
705 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
706 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
707 addr = dev[0].own_addr()
708
709 if "OK" not in hapd.request("POLL_STA " + addr):
710 raise Exception("POLL_STA failed")
711 ev = hapd.wait_event(["AP-STA-POLL-OK"], timeout=5)
712 if ev is None:
713 raise Exception("Poll response not seen")
714 if addr not in ev:
715 raise Exception("Unexpected poll response: " + ev)
716
717 def test_ap_open_poll_sta_no_ack(dev, apdev):
718 """AP with open mode and STA poll without ACK"""
719 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
720 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
721 addr = dev[0].own_addr()
722
723 hapd.set("ext_mgmt_frame_handling", "1")
724 dev[0].request("DISCONNECT")
725 dev[0].wait_disconnected()
726 hapd.set("ext_mgmt_frame_handling", "0")
727 if "OK" not in hapd.request("POLL_STA " + addr):
728 raise Exception("POLL_STA failed")
729 ev = hapd.wait_event(["AP-STA-POLL-OK"], timeout=1)
730 if ev is not None:
731 raise Exception("Unexpected poll response reported")
732
733 def test_ap_open_pmf_default(dev, apdev):
734 """AP with open mode (no security) configuration and pmf=2"""
735 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
736 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
737 ieee80211w="2", wait_connect=False)
738 dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
739 ieee80211w="1")
740 try:
741 dev[0].request("SET pmf 2")
742 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
743
744 dev[0].request("DISCONNECT")
745 dev[0].wait_disconnected()
746 finally:
747 dev[0].request("SET pmf 0")
748 dev[2].request("DISCONNECT")
749 dev[2].wait_disconnected()
750
751 ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
752 if ev is not None:
753 raise Exception("Unexpected dev[1] connection")
754 dev[1].request("DISCONNECT")
755
756 def test_ap_open_drv_fail(dev, apdev):
757 """AP with open mode and driver operations failing"""
758 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
759
760 with fail_test(dev[0], 1, "wpa_driver_nl80211_authenticate"):
761 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
762 wait_connect=False)
763 wait_fail_trigger(dev[0], "GET_FAIL")
764 dev[0].request("REMOVE_NETWORK all")
765
766 with fail_test(dev[0], 1, "wpa_driver_nl80211_associate"):
767 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
768 wait_connect=False)
769 wait_fail_trigger(dev[0], "GET_FAIL")
770 dev[0].request("REMOVE_NETWORK all")
771
772 def run_multicast_to_unicast(dev, apdev, convert):
773 params = {"ssid": "open"}
774 params["multicast_to_unicast"] = "1" if convert else "0"
775 hapd = hostapd.add_ap(apdev[0], params)
776 dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
777 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
778 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
779 if ev is None:
780 raise Exception("No connection event received from hostapd")
781 hwsim_utils.test_connectivity(dev[0], hapd, multicast_to_unicast=convert)
782 dev[0].request("DISCONNECT")
783 ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=5)
784 if ev is None:
785 raise Exception("No disconnection event received from hostapd")
786
787 def test_ap_open_multicast_to_unicast(dev, apdev):
788 """Multicast-to-unicast conversion enabled"""
789 run_multicast_to_unicast(dev, apdev, True)
790
791 def test_ap_open_multicast_to_unicast_disabled(dev, apdev):
792 """Multicast-to-unicast conversion disabled"""
793 run_multicast_to_unicast(dev, apdev, False)
794
795 def test_ap_open_drop_duplicate(dev, apdev, params):
796 """AP dropping duplicate management frames"""
797 hapd = hostapd.add_ap(apdev[0], {"ssid": "open",
798 "interworking": "1"})
799 hapd.set("ext_mgmt_frame_handling", "1")
800 bssid = hapd.own_addr().replace(':', '')
801 addr = "020304050607"
802 auth = "b0003a01" + bssid + addr + bssid + '1000000001000000'
803 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
804 raise Exception("MGMT_RX_PROCESS failed")
805 auth = "b0083a01" + bssid + addr + bssid + '1000000001000000'
806 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
807 raise Exception("MGMT_RX_PROCESS failed")
808
809 ies = "00046f70656e010802040b160c12182432043048606c2d1a3c101bffff0000000000000000000001000000000000000000007f0a04000a020140004000013b155151525354737475767778797a7b7c7d7e7f808182dd070050f202000100"
810 assoc_req = "00003a01" + bssid + addr + bssid + "2000" + "21040500" + ies
811 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
812 raise Exception("MGMT_RX_PROCESS failed")
813 assoc_req = "00083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
814 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
815 raise Exception("MGMT_RX_PROCESS failed")
816 reassoc_req = "20083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
817 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % reassoc_req):
818 raise Exception("MGMT_RX_PROCESS failed")
819 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % reassoc_req):
820 raise Exception("MGMT_RX_PROCESS failed")
821
822 action = "d0003a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
823 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
824 raise Exception("MGMT_RX_PROCESS failed")
825
826 action = "d0083a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
827 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
828 raise Exception("MGMT_RX_PROCESS failed")
829
830 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
831 "wlan.fc.type == 0", ["wlan.fc.subtype"])
832 num_auth = 0
833 num_assoc = 0
834 num_reassoc = 0
835 num_action = 0
836 for subtype in out.splitlines():
837 val = int(subtype)
838 if val == 11:
839 num_auth += 1
840 elif val == 1:
841 num_assoc += 1
842 elif val == 3:
843 num_reassoc += 1
844 elif val == 13:
845 num_action += 1
846 if num_auth != 1:
847 raise Exception("Unexpected number of Authentication frames: %d" % num_auth)
848 if num_assoc != 1:
849 raise Exception("Unexpected number of association frames: %d" % num_assoc)
850 if num_reassoc != 1:
851 raise Exception("Unexpected number of reassociation frames: %d" % num_reassoc)
852 if num_action != 1:
853 raise Exception("Unexpected number of Action frames: %d" % num_action)
854
855 def test_ap_open_select_network_freq(dev, apdev):
856 """AP with open mode and use for SELECT_NETWORK freq parameter"""
857 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
858 id = dev[0].connect("open", key_mgmt="NONE", only_add_network=True)
859 dev[0].select_network(id, freq=2412)
860 start = os.times()[4]
861 ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
862 if ev is None:
863 raise Exception("Scan not started")
864 ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
865 if ev is None:
866 raise Exception("Scan not completed")
867 end = os.times()[4]
868 logger.info("Scan duration: {} seconds".format(end - start))
869 if end - start > 3:
870 raise Exception("Scan took unexpectedly long time")
871 dev[0].wait_connected()
872
873 def test_ap_open_noncountry(dev, apdev):
874 """AP with open mode and noncountry entity as Country String"""
875 _test_ap_open_country(dev, apdev, "XX", "0x58")
876
877 def test_ap_open_country_table_e4(dev, apdev):
878 """AP with open mode and Table E-4 Country String"""
879 _test_ap_open_country(dev, apdev, "DE", "0x04")
880
881 def test_ap_open_country_indoor(dev, apdev):
882 """AP with open mode and indoor country code"""
883 _test_ap_open_country(dev, apdev, "DE", "0x49")
884
885 def test_ap_open_country_outdoor(dev, apdev):
886 """AP with open mode and outdoor country code"""
887 _test_ap_open_country(dev, apdev, "DE", "0x4f")
888
889 def _test_ap_open_country(dev, apdev, country_code, country3):
890 try:
891 hapd = None
892 hapd = run_ap_open_country(dev, apdev, country_code, country3)
893 finally:
894 clear_regdom(hapd, dev)
895
896 def run_ap_open_country(dev, apdev, country_code, country3):
897 hapd = hostapd.add_ap(apdev[0], {"ssid": "open",
898 "country_code": country_code,
899 "country3": country3,
900 "ieee80211d": "1"})
901 dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
902 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
903 dev[0].wait_regdom(country_ie=True)
904 return hapd
905
906 def test_ap_open_disable_select(dev, apdev):
907 """DISABLE_NETWORK for connected AP followed by SELECT_NETWORK"""
908 hapd1 = hostapd.add_ap(apdev[0], {"ssid": "open"})
909 hapd2 = hostapd.add_ap(apdev[1], {"ssid": "open"})
910 id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
911
912 dev[0].request("DISABLE_NETWORK %d" % id)
913 dev[0].wait_disconnected()
914 res = dev[0].request("BLACKLIST")
915 if hapd1.own_addr() in res or hapd2.own_addr() in res:
916 raise Exception("Unexpected blacklist entry added")
917 dev[0].request("SELECT_NETWORK %d" % id)
918 dev[0].wait_connected()
919
920 def test_ap_open_reassoc_same(dev, apdev):
921 """AP with open mode and STA reassociating back to same AP without auth exchange"""
922 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
923 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
924 try:
925 dev[0].request("SET reassoc_same_bss_optim 1")
926 dev[0].request("REATTACH")
927 dev[0].wait_connected()
928 hwsim_utils.test_connectivity(dev[0], hapd)
929 finally:
930 dev[0].request("SET reassoc_same_bss_optim 0")
931
932 def test_ap_open_no_reflection(dev, apdev):
933 """AP with open mode, STA sending packets to itself"""
934 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
935 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
936
937 ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=5)
938 if ev is None:
939 raise Exception("No connection event received from hostapd")
940 # test normal connectivity is OK
941 hwsim_utils.test_connectivity(dev[0], hapd)
942
943 # test that we can't talk to ourselves
944 addr = dev[0].own_addr()
945 res = dev[0].request('DATA_TEST_CONFIG 1')
946 try:
947 assert 'OK' in res
948
949 cmd = "DATA_TEST_TX {} {} {}".format(addr, addr, 0)
950 dev[0].request(cmd)
951
952 ev = dev[0].wait_event(["DATA-TEST-RX"], timeout=1)
953
954 if ev is not None and "DATA-TEST-RX {} {}".format(addr, addr) in ev:
955 raise Exception("STA can unexpectedly talk to itself")
956 finally:
957 dev[0].request('DATA_TEST_CONFIG 0')
958
959 def test_ap_no_auth_ack(dev, apdev):
960 """AP not receiving Authentication frame ACK"""
961 hapd = hostapd.add_ap(apdev[0], {"ssid": "open",
962 "ap_max_inactivity": "1"})
963 hapd.set("ext_mgmt_frame_handling", "1")
964 bssid = hapd.own_addr()
965 addr = "02:01:02:03:04:05"
966 frame = "b0003a01" + bssid.replace(':', '') + addr.replace(':', '') + bssid.replace(':', '') + "1000" + "000001000000"
967 if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=" + frame):
968 raise Exception("MGMT_RX_PROCESS failed")
969 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
970 if ev is None:
971 raise Exception("TX status for Authentication frame not reported")
972 if "ok=0 buf=b0" not in ev:
973 raise Exception("Unexpected TX status contents: " + ev)
974
975 # wait for STA to be removed due to timeout
976 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
977 if ev is None:
978 raise Exception("TX status for Deauthentication frame not reported")
979 if "ok=0 buf=c0" not in ev:
980 raise Exception("Unexpected TX status contents (disconnect): " + ev)
981
982 def test_ap_open_layer_2_update(dev, apdev, params):
983 """AP with open mode (no security) and Layer 2 Update frame"""
984 prefix = "ap_open_layer_2_update"
985 ifname = apdev[0]["ifname"]
986 cap = os.path.join(params['logdir'], prefix + "." + ifname + ".pcap")
987
988 hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
989 wt = WlantestCapture(ifname, cap)
990 time.sleep(1)
991
992 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
993 hapd.wait_sta()
994 hwsim_utils.test_connectivity(dev[0], hapd)
995 time.sleep(1)
996 hwsim_utils.test_connectivity(dev[0], hapd)
997 time.sleep(0.5)
998 wt.close()
999
1000 # Check for Layer 2 Update frame and unexpected frames from the station
1001 # that did not fully complete authentication.
1002 res = run_tshark(cap, "basicxid.llc.xid.format == 0x81",
1003 ["eth.src"], wait=False)
1004 real_sta_seen = False
1005 unexpected_sta_seen = False
1006 real_addr = dev[0].own_addr()
1007 for l in res.splitlines():
1008 if l == real_addr:
1009 real_sta_seen = True
1010 else:
1011 unexpected_sta_seen = True
1012 if unexpected_sta_seen:
1013 raise Exception("Layer 2 Update frame from unexpected STA seen")
1014 if not real_sta_seen:
1015 raise Exception("Layer 2 Update frame from real STA not seen")