2 # Copyright (c) 2014, Qualcomm Atheros, Inc.
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger = logging.getLogger()
16 from tshark import run_tshark
17 from utils import alloc_fail
18 from wpasupplicant import WpaSupplicant
20 def test_ap_open(dev, apdev):
21 """AP with open mode (no security) configuration"""
22 _test_ap_open(dev, apdev)
24 def _test_ap_open(dev, apdev):
25 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
26 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
28 ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
30 raise Exception("No connection event received from hostapd")
31 hwsim_utils.test_connectivity(dev[0], hapd)
33 dev[0].request("DISCONNECT")
34 ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
36 raise Exception("No disconnection event received from hostapd")
38 def test_ap_open_packet_loss(dev, apdev):
39 """AP with open mode configuration and large packet loss"""
40 params = { "ssid": "open",
41 "ignore_probe_probability": "0.5",
42 "ignore_auth_probability": "0.5",
43 "ignore_assoc_probability": "0.5",
44 "ignore_reassoc_probability": "0.5" }
45 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
47 dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
50 dev[i].wait_connected(timeout=20)
52 def test_ap_open_unknown_action(dev, apdev):
53 """AP with open mode configuration and unknown Action frame"""
54 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
55 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
56 bssid = apdev[0]['bssid']
57 cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
58 if "FAIL" in dev[0].request(cmd):
59 raise Exception("Could not send test Action frame")
60 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
62 raise Exception("Timeout on MGMT-TX-STATUS")
63 if "result=SUCCESS" not in ev:
64 raise Exception("AP did not ack Action frame")
66 def test_ap_open_invalid_wmm_action(dev, apdev):
67 """AP with open mode configuration and invalid WMM Action frame"""
68 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
69 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
70 bssid = apdev[0]['bssid']
71 cmd = "MGMT_TX {} {} freq=2412 action=1100".format(bssid, bssid)
72 if "FAIL" in dev[0].request(cmd):
73 raise Exception("Could not send test Action frame")
74 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
75 if ev is None or "result=SUCCESS" not in ev:
76 raise Exception("AP did not ack Action frame")
78 def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
79 """Reconnect to open mode AP after inactivity related disconnection"""
80 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
81 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
82 hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
83 dev[0].wait_disconnected(timeout=5)
84 dev[0].wait_connected(timeout=2, error="Timeout on reconnection")
86 def test_ap_open_assoc_timeout(dev, apdev):
87 """AP timing out association"""
89 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
90 dev[0].scan(freq="2412")
91 hapd.set("ext_mgmt_frame_handling", "1")
92 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
94 for i in range(0, 10):
97 raise Exception("MGMT RX wait timed out")
98 if req['subtype'] == 11:
102 raise Exception("Authentication frame not received")
105 resp['fc'] = req['fc']
106 resp['da'] = req['sa']
107 resp['sa'] = req['da']
108 resp['bssid'] = req['bssid']
109 resp['payload'] = struct.pack('<HHH', 0, 2, 0)
113 for i in range(0, 10):
116 raise Exception("MGMT RX wait timed out")
117 if req['subtype'] == 0:
122 raise Exception("Association Request frames not received: assoc=%d" % assoc)
123 hapd.set("ext_mgmt_frame_handling", "0")
124 dev[0].wait_connected(timeout=15)
126 def test_ap_open_id_str(dev, apdev):
127 """AP with open mode and id_str"""
128 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
129 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412", id_str="foo",
131 ev = dev[0].wait_connected(timeout=10)
132 if "id_str=foo" not in ev:
133 raise Exception("CTRL-EVENT-CONNECT did not have matching id_str: " + ev)
134 if dev[0].get_status_field("id_str") != "foo":
135 raise Exception("id_str mismatch")
137 def test_ap_open_select_any(dev, apdev):
138 """AP with open mode and select any network"""
139 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
140 id = dev[0].connect("unknown", key_mgmt="NONE", scan_freq="2412",
141 only_add_network=True)
142 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
143 only_add_network=True)
144 dev[0].select_network(id)
145 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND",
146 "CTRL-EVENT-CONNECTED"], timeout=10)
148 raise Exception("No result reported")
149 if "CTRL-EVENT-CONNECTED" in ev:
150 raise Exception("Unexpected connection")
152 dev[0].select_network("any")
153 dev[0].wait_connected(timeout=10)
155 def test_ap_open_unexpected_assoc_event(dev, apdev):
156 """AP with open mode and unexpected association event"""
157 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
158 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
159 dev[0].request("DISCONNECT")
160 dev[0].wait_disconnected(timeout=15)
161 dev[0].dump_monitor()
162 # This will be accepted due to matching network
163 subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
165 dev[0].wait_connected(timeout=15)
166 dev[0].dump_monitor()
168 dev[0].request("REMOVE_NETWORK all")
169 dev[0].wait_disconnected(timeout=5)
170 dev[0].dump_monitor()
171 # This will result in disconnection due to no matching network
172 subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
174 dev[0].wait_disconnected(timeout=15)
176 def test_ap_bss_load(dev, apdev):
177 """AP with open mode (no security) configuration"""
178 hapd = hostapd.add_ap(apdev[0]['ifname'],
180 "bss_load_update_period": "10" })
181 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
182 # this does not really get much useful output with mac80211_hwsim currently,
183 # but run through the channel survey update couple of times
184 for i in range(0, 10):
185 hwsim_utils.test_connectivity(dev[0], hapd)
186 hwsim_utils.test_connectivity(dev[0], hapd)
187 hwsim_utils.test_connectivity(dev[0], hapd)
190 def hapd_out_of_mem(hapd, apdev, count, func):
191 with alloc_fail(hapd, count, func):
194 hostapd.add_ap(apdev['ifname'], { "ssid": "open" })
199 raise Exception("hostapd interface started even with memory allocation failure: %d:%s" % (count, func))
201 def test_ap_open_out_of_memory(dev, apdev):
202 """hostapd failing to setup interface due to allocation failure"""
203 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
204 hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
206 for i in range(1, 3):
207 hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
209 for i in range(1, 5):
210 hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
212 hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
214 hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
216 for i in range(1, 4):
217 hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
219 # eloop_register_read_sock() call from i802_init()
220 hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;?eloop_register_sock;?eloop_register_read_sock;=i802_init")
222 # verify that a new interface can still be added when memory allocation does
224 hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
226 def test_bssid_black_white_list(dev, apdev):
227 """BSSID black/white list"""
228 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
229 hapd2 = hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
231 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
232 bssid_whitelist=apdev[1]['bssid'])
233 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
234 bssid_blacklist=apdev[1]['bssid'])
235 dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
236 bssid_whitelist="00:00:00:00:00:00/00:00:00:00:00:00",
237 bssid_blacklist=apdev[1]['bssid'])
238 if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
239 raise Exception("dev[0] connected to unexpected AP")
240 if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
241 raise Exception("dev[1] connected to unexpected AP")
242 if dev[2].get_status_field('bssid') != apdev[0]['bssid']:
243 raise Exception("dev[2] connected to unexpected AP")
244 dev[0].request("REMOVE_NETWORK all")
245 dev[1].request("REMOVE_NETWORK all")
246 dev[2].request("REMOVE_NETWORK all")
248 dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
249 bssid_whitelist="00:00:00:00:00:00", wait_connect=False)
250 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
251 bssid_whitelist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
252 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
253 bssid_blacklist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
254 if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
255 raise Exception("dev[0] connected to unexpected AP")
256 if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
257 raise Exception("dev[1] connected to unexpected AP")
258 dev[0].request("REMOVE_NETWORK all")
259 dev[1].request("REMOVE_NETWORK all")
260 ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
262 raise Exception("Unexpected dev[2] connectin")
263 dev[2].request("REMOVE_NETWORK all")
265 def test_ap_open_wpas_in_bridge(dev, apdev):
266 """Open mode AP and wpas interface in a bridge"""
270 _test_ap_open_wpas_in_bridge(dev, apdev)
272 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
273 subprocess.call(['brctl', 'delif', br_ifname, ifname])
274 subprocess.call(['brctl', 'delbr', br_ifname])
275 subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
277 def _test_ap_open_wpas_in_bridge(dev, apdev):
278 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
282 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
283 # First, try a failure case of adding an interface
285 wpas.interface_add(ifname, br_ifname=br_ifname)
286 raise Exception("Interface addition succeeded unexpectedly")
288 if "Failed to add" in str(e):
289 logger.info("Ignore expected interface_add failure due to missing bridge interface: " + str(e))
293 # Next, add the bridge interface and add the interface again
294 subprocess.call(['brctl', 'addbr', br_ifname])
295 subprocess.call(['brctl', 'setfd', br_ifname, '0'])
296 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
297 subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
298 subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
299 wpas.interface_add(ifname, br_ifname=br_ifname)
301 wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
303 def test_ap_open_start_disabled(dev, apdev):
304 """AP with open mode and beaconing disabled"""
305 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open",
306 "start_disabled": "1" })
307 bssid = apdev[0]['bssid']
309 dev[0].flush_scan_cache()
310 dev[0].scan(freq=2412, only_new=True)
311 if dev[0].get_bss(bssid) is not None:
312 raise Exception("AP was seen beaconing")
313 if "OK" not in hapd.request("RELOAD"):
314 raise Exception("RELOAD failed")
315 dev[0].scan_for_bss(bssid, freq=2412)
316 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
318 def test_ap_open_start_disabled2(dev, apdev):
319 """AP with open mode and beaconing disabled (2)"""
320 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open",
321 "start_disabled": "1" })
322 bssid = apdev[0]['bssid']
324 dev[0].flush_scan_cache()
325 dev[0].scan(freq=2412, only_new=True)
326 if dev[0].get_bss(bssid) is not None:
327 raise Exception("AP was seen beaconing")
328 if "OK" not in hapd.request("UPDATE_BEACON"):
329 raise Exception("UPDATE_BEACON failed")
330 dev[0].scan_for_bss(bssid, freq=2412)
331 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
332 if "OK" not in hapd.request("UPDATE_BEACON"):
333 raise Exception("UPDATE_BEACON failed")
334 dev[0].request("DISCONNECT")
335 dev[0].wait_disconnected()
336 dev[0].request("RECONNECT")
337 dev[0].wait_connected()
339 def test_ap_open_ifdown(dev, apdev):
340 """AP with open mode and external ifconfig down"""
341 params = { "ssid": "open",
342 "ap_max_inactivity": "1" }
343 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
344 bssid = apdev[0]['bssid']
346 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
347 dev[1].connect("open", key_mgmt="NONE", scan_freq="2412")
348 subprocess.call(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
349 ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=10)
351 raise Exception("Timeout on AP-STA-DISCONNECTED (1)")
352 ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=5)
354 raise Exception("Timeout on AP-STA-DISCONNECTED (2)")
355 ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=5)
357 raise Exception("No INTERFACE-DISABLED event")
358 # The following wait tests beacon loss detection in mac80211 on dev0.
359 # dev1 is used to test stopping of AP side functionality on client polling.
360 dev[1].request("REMOVE_NETWORK all")
361 subprocess.call(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
362 dev[0].wait_disconnected()
363 dev[1].wait_disconnected()
364 ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
366 raise Exception("No INTERFACE-ENABLED event")
367 dev[0].wait_connected()
368 hwsim_utils.test_connectivity(dev[0], hapd)
370 def test_ap_open_disconnect_in_ps(dev, apdev, params):
371 """Disconnect with the client in PS to regression-test a kernel bug"""
372 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
373 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
375 ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
377 raise Exception("No connection event received from hostapd")
380 hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_MANUAL_POLL)
382 # inject some traffic
384 da = dev[0].own_addr()
385 hapd.request('DATA_TEST_CONFIG 1')
386 hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
387 hapd.request('DATA_TEST_CONFIG 0')
389 # let the AP send couple of Beacon frames
392 # disconnect - with traffic pending - shouldn't cause kernel warnings
393 dev[0].request("DISCONNECT")
395 hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_DISABLED)
398 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
399 "wlan_mgt.tim.partial_virtual_bitmap",
400 ["wlan_mgt.tim.partial_virtual_bitmap"])
403 for l in out.splitlines():
405 if pvb > 0 and state == 0:
407 elif pvb == 0 and state == 1:
410 raise Exception("Didn't observe TIM bit getting set and unset (state=%d)" % state)
412 def test_ap_open_select_network(dev, apdev):
413 """Open mode connection and SELECT_NETWORK to change network"""
414 hapd1 = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
415 bssid1 = apdev[0]['bssid']
416 hapd2 = hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open2" })
417 bssid2 = apdev[1]['bssid']
419 id1 = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
420 only_add_network=True)
421 id2 = dev[0].connect("open2", key_mgmt="NONE", scan_freq="2412")
422 hwsim_utils.test_connectivity(dev[0], hapd2)
424 dev[0].select_network(id1)
425 dev[0].wait_connected()
426 res = dev[0].request("BLACKLIST")
427 if bssid1 in res or bssid2 in res:
428 raise Exception("Unexpected blacklist entry")
429 hwsim_utils.test_connectivity(dev[0], hapd1)
431 dev[0].select_network(id2)
432 dev[0].wait_connected()
433 hwsim_utils.test_connectivity(dev[0], hapd2)
434 res = dev[0].request("BLACKLIST")
435 if bssid1 in res or bssid2 in res:
436 raise Exception("Unexpected blacklist entry(2)")
438 def test_ap_open_disable_enable(dev, apdev):
439 """AP with open mode getting disabled and re-enabled"""
440 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
441 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
445 hapd.request("DISABLE")
446 dev[0].wait_disconnected()
447 hapd.request("ENABLE")
448 dev[0].wait_connected()
449 hwsim_utils.test_connectivity(dev[0], hapd)
451 def sta_enable_disable(dev, bssid):
452 dev.scan_for_bss(bssid, freq=2412)
453 work_id = dev.request("RADIO_WORK add block-work")
454 ev = dev.wait_event(["EXT-RADIO-WORK-START"])
456 raise Exception("Timeout while waiting radio work to start")
457 id = dev.connect("open", key_mgmt="NONE", scan_freq="2412",
458 only_add_network=True)
459 dev.request("ENABLE_NETWORK %d" % id)
460 if "connect@" not in dev.request("RADIO_WORK show"):
461 raise Exception("connect radio work missing")
462 dev.request("DISABLE_NETWORK %d" % id)
463 dev.request("RADIO_WORK done " + work_id)
467 if "connect@" not in dev.request("RADIO_WORK show"):
472 raise Exception("connect radio work not completed")
473 ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
475 raise Exception("Unexpected connection")
476 dev.request("DISCONNECT")
478 def test_ap_open_sta_enable_disable(dev, apdev):
479 """AP with open mode and wpa_supplicant ENABLE/DISABLE_NETWORK"""
480 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
481 bssid = apdev[0]['bssid']
483 sta_enable_disable(dev[0], bssid)
485 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
486 wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
487 sta_enable_disable(wpas, bssid)
489 def test_ap_open_select_twice(dev, apdev):
490 """AP with open mode and select network twice"""
491 id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
492 only_add_network=True)
493 dev[0].select_network(id)
494 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
496 raise Exception("No result reported")
497 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
498 # Verify that the second SELECT_NETWORK starts a new scan immediately by
499 # waiting less than the default scan period.
500 dev[0].select_network(id)
501 dev[0].wait_connected(timeout=3)
503 def test_ap_open_reassoc_not_found(dev, apdev):
504 """AP with open mode and REASSOCIATE not finding a match"""
505 id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
506 only_add_network=True)
507 dev[0].select_network(id)
508 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
510 raise Exception("No result reported")
511 dev[0].request("DISCONNECT")
514 dev[0].dump_monitor()
516 dev[0].request("REASSOCIATE")
517 ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
519 raise Exception("No result reported")
520 dev[0].request("DISCONNECT")