1 # Test cases for dynamic BSS changes with hostapd
2 # Copyright (c) 2013, Qualcomm Atheros, Inc.
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
10 logger = logging.getLogger()
15 from utils import alloc_fail
16 from test_ap_acs import force_prev_ap_on_24g
18 def test_ap_change_ssid(dev, apdev):
19 """Dynamic SSID change with hostapd and WPA2-PSK"""
20 params = hostapd.wpa2_params(ssid="test-wpa2-psk-start",
21 passphrase="12345678")
22 hostapd.add_ap(apdev[0]['ifname'], params)
23 id = dev[0].connect("test-wpa2-psk-start", psk="12345678",
25 dev[0].request("DISCONNECT")
27 logger.info("Change SSID dynamically")
28 hapd = hostapd.Hostapd(apdev[0]['ifname'])
29 res = hapd.request("SET ssid test-wpa2-psk-new")
31 raise Exception("SET command failed")
32 res = hapd.request("RELOAD")
34 raise Exception("RELOAD command failed")
36 dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
37 dev[0].connect_network(id)
39 def multi_check(dev, check, scan_opt=True):
42 for i in range(0, num_bss):
43 dev[i].request("BSS_FLUSH 0")
45 for i in range(0, num_bss):
48 id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
49 scan_freq="2412", wait_connect=False))
50 for i in range(num_bss):
53 bssid = '02:00:00:00:03:0' + str(i)
55 dev[i].scan_for_bss(bssid, freq=2412)
56 id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
57 scan_freq="2412", wait_connect=True))
59 for i in range(num_bss):
61 timeout=0.2 if first else 0.01
63 ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
65 raise Exception("Unexpected connection")
67 for i in range(0, num_bss):
68 dev[i].remove_network(id[i])
69 for i in range(num_bss):
71 dev[i].wait_disconnected(timeout=5)
74 for i in range(0, num_bss):
75 res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
77 for i in range(0, num_bss):
79 bssid = '02:00:00:00:03:0' + str(i)
81 raise Exception("Unexpected BSS" + str(i) + " in scan results")
83 def test_ap_bss_add_remove(dev, apdev):
84 """Dynamic BSS add/remove operations with hostapd"""
86 _test_ap_bss_add_remove(dev, apdev)
89 dev[i].request("SCAN_INTERVAL 5")
91 def _test_ap_bss_add_remove(dev, apdev):
93 dev[i].request("SCAN_INTERVAL 1")
94 ifname1 = apdev[0]['ifname']
95 ifname2 = apdev[0]['ifname'] + '-2'
96 ifname3 = apdev[0]['ifname'] + '-3'
97 logger.info("Set up three BSSes one by one")
98 hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
99 multi_check(dev, [ True, False, False ])
100 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
101 multi_check(dev, [ True, True, False ])
102 hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
103 multi_check(dev, [ True, True, True ])
105 logger.info("Remove the last BSS and re-add it")
106 hostapd.remove_bss(ifname3)
107 multi_check(dev, [ True, True, False ])
108 hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
109 multi_check(dev, [ True, True, True ])
111 logger.info("Remove the middle BSS and re-add it")
112 hostapd.remove_bss(ifname2)
113 multi_check(dev, [ True, False, True ])
114 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
115 multi_check(dev, [ True, True, True ])
117 logger.info("Remove the first BSS and re-add it and other BSSs")
118 hostapd.remove_bss(ifname1)
119 multi_check(dev, [ False, False, False ])
120 hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
121 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
122 hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
123 multi_check(dev, [ True, True, True ])
125 logger.info("Remove two BSSes and re-add them")
126 hostapd.remove_bss(ifname2)
127 multi_check(dev, [ True, False, True ])
128 hostapd.remove_bss(ifname3)
129 multi_check(dev, [ True, False, False ])
130 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
131 multi_check(dev, [ True, True, False ])
132 hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
133 multi_check(dev, [ True, True, True ])
135 logger.info("Remove three BSSes in and re-add them")
136 hostapd.remove_bss(ifname3)
137 multi_check(dev, [ True, True, False ])
138 hostapd.remove_bss(ifname2)
139 multi_check(dev, [ True, False, False ])
140 hostapd.remove_bss(ifname1)
141 multi_check(dev, [ False, False, False ])
142 hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
143 multi_check(dev, [ True, False, False ])
144 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
145 multi_check(dev, [ True, True, False ])
146 hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
147 multi_check(dev, [ True, True, True ])
149 logger.info("Test error handling if a duplicate ifname is tried")
150 hostapd.add_bss('phy3', ifname3, 'bss-3.conf', ignore_error=True)
151 multi_check(dev, [ True, True, True ])
153 def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
154 """Dynamic BSS add during HT40 co-ex scan"""
155 ifname1 = apdev[0]['ifname']
156 ifname2 = apdev[0]['ifname'] + '-2'
157 hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
158 hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
159 multi_check(dev, [ True, True ], scan_opt=False)
160 hostapd.remove_bss(ifname2)
161 hostapd.remove_bss(ifname1)
163 hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
164 hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
165 hostapd.remove_bss(ifname2)
166 multi_check(dev, [ True, False ], scan_opt=False)
167 hostapd.remove_bss(ifname1)
169 hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
170 hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
171 hostapd.remove_bss(ifname1)
172 multi_check(dev, [ False, False ])
174 def test_ap_multi_bss_config(dev, apdev):
175 """hostapd start with a multi-BSS configuration file"""
176 ifname1 = apdev[0]['ifname']
177 ifname2 = apdev[0]['ifname'] + '-2'
178 ifname3 = apdev[0]['ifname'] + '-3'
179 logger.info("Set up three BSSes with one configuration file")
180 hostapd.add_iface(ifname1, 'multi-bss.conf')
181 hapd = hostapd.Hostapd(ifname1)
183 multi_check(dev, [ True, True, True ])
184 hostapd.remove_bss(ifname2)
185 multi_check(dev, [ True, False, True ])
186 hostapd.remove_bss(ifname3)
187 multi_check(dev, [ True, False, False ])
188 hostapd.remove_bss(ifname1)
189 multi_check(dev, [ False, False, False ])
191 hostapd.add_iface(ifname1, 'multi-bss.conf')
192 hapd = hostapd.Hostapd(ifname1)
194 hostapd.remove_bss(ifname1)
195 multi_check(dev, [ False, False, False ])
197 def invalid_ap(hapd_global, ifname):
198 logger.info("Trying to start AP " + ifname + " with invalid configuration")
199 hapd_global.remove(ifname)
200 hapd_global.add(ifname)
201 hapd = hostapd.Hostapd(ifname)
203 raise Exception("Could not ping hostapd")
205 hapd.set("ssid", "invalid-config")
206 hapd.set("channel", "12345")
213 raise Exception("ENABLE command succeeded unexpectedly")
216 def test_ap_invalid_config(dev, apdev):
217 """Try to start AP with invalid configuration and fix configuration"""
218 hapd_global = hostapd.HostapdGlobal()
219 ifname = apdev[0]['ifname']
220 hapd = invalid_ap(hapd_global, ifname)
222 logger.info("Fix configuration and start AP again")
223 hapd.set("channel", "1")
225 dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
227 def test_ap_invalid_config2(dev, apdev):
228 """Try to start AP with invalid configuration and remove interface"""
229 hapd_global = hostapd.HostapdGlobal()
230 ifname = apdev[0]['ifname']
231 hapd = invalid_ap(hapd_global, ifname)
232 logger.info("Remove interface with failed configuration")
233 hapd_global.remove(ifname)
235 def test_ap_remove_during_acs(dev, apdev):
236 """Remove interface during ACS"""
237 force_prev_ap_on_24g(apdev[0])
238 params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
239 params['channel'] = '0'
240 ifname = apdev[0]['ifname']
241 hapd = hostapd.HostapdGlobal()
242 hostapd.add_ap(ifname, params)
245 def test_ap_remove_during_acs2(dev, apdev):
246 """Remove BSS during ACS in multi-BSS configuration"""
247 force_prev_ap_on_24g(apdev[0])
248 ifname = apdev[0]['ifname']
249 ifname2 = ifname + "-2"
250 hapd_global = hostapd.HostapdGlobal()
251 hapd_global.add(ifname)
252 hapd = hostapd.Hostapd(ifname)
254 hapd.set("ssid", "test-acs-remove")
255 hapd.set("channel", "0")
256 hapd.set("bss", ifname2)
257 hapd.set("ssid", "test-acs-remove2")
259 hapd_global.remove(ifname)
261 def test_ap_remove_during_acs3(dev, apdev):
262 """Remove second BSS during ACS in multi-BSS configuration"""
263 force_prev_ap_on_24g(apdev[0])
264 ifname = apdev[0]['ifname']
265 ifname2 = ifname + "-2"
266 hapd_global = hostapd.HostapdGlobal()
267 hapd_global.add(ifname)
268 hapd = hostapd.Hostapd(ifname)
270 hapd.set("ssid", "test-acs-remove")
271 hapd.set("channel", "0")
272 hapd.set("bss", ifname2)
273 hapd.set("ssid", "test-acs-remove2")
275 hapd_global.remove(ifname2)
277 def test_ap_remove_during_ht_coex_scan(dev, apdev):
278 """Remove interface during HT co-ex scan"""
279 params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
280 params['channel'] = '1'
281 params['ht_capab'] = "[HT40+]"
282 ifname = apdev[0]['ifname']
283 hapd = hostapd.HostapdGlobal()
284 hostapd.add_ap(ifname, params)
287 def test_ap_remove_during_ht_coex_scan2(dev, apdev):
288 """Remove BSS during HT co-ex scan in multi-BSS configuration"""
289 ifname = apdev[0]['ifname']
290 ifname2 = ifname + "-2"
291 hapd_global = hostapd.HostapdGlobal()
292 hapd_global.add(ifname)
293 hapd = hostapd.Hostapd(ifname)
295 hapd.set("ssid", "test-ht-remove")
296 hapd.set("channel", "1")
297 hapd.set("ht_capab", "[HT40+]")
298 hapd.set("bss", ifname2)
299 hapd.set("ssid", "test-ht-remove2")
301 hapd_global.remove(ifname)
303 def test_ap_remove_during_ht_coex_scan3(dev, apdev):
304 """Remove second BSS during HT co-ex scan in multi-BSS configuration"""
305 ifname = apdev[0]['ifname']
306 ifname2 = ifname + "-2"
307 hapd_global = hostapd.HostapdGlobal()
308 hapd_global.add(ifname)
309 hapd = hostapd.Hostapd(ifname)
311 hapd.set("ssid", "test-ht-remove")
312 hapd.set("channel", "1")
313 hapd.set("ht_capab", "[HT40+]")
314 hapd.set("bss", ifname2)
315 hapd.set("ssid", "test-ht-remove2")
317 hapd_global.remove(ifname2)
319 def test_ap_enable_disable_reenable(dev, apdev):
320 """Enable, disable, re-enable AP"""
321 ifname = apdev[0]['ifname']
322 hapd_global = hostapd.HostapdGlobal()
323 hapd_global.add(ifname)
324 hapd = hostapd.Hostapd(ifname)
326 hapd.set("ssid", "dynamic")
328 ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
330 raise Exception("AP startup timed out")
331 dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
333 ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
335 raise Exception("AP disabling timed out")
336 dev[0].wait_disconnected(timeout=10)
338 ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
340 raise Exception("AP startup timed out")
341 dev[1].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
342 dev[0].wait_connected(timeout=10)
344 def test_ap_double_disable(dev, apdev):
345 """Double DISABLE regression test"""
346 hostapd.add_bss('phy3', apdev[0]['ifname'], 'bss-1.conf')
347 hostapd.add_bss('phy3', apdev[0]['ifname'] + '-2', 'bss-2.conf')
348 hapd = hostapd.Hostapd(apdev[0]['ifname'])
350 if "FAIL" not in hapd.request("DISABLE"):
351 raise Exception("Second DISABLE accepted unexpectedly")
354 if "FAIL" not in hapd.request("DISABLE"):
355 raise Exception("Second DISABLE accepted unexpectedly")
357 def test_ap_bss_add_many(dev, apdev):
358 """Large number of BSS add operations with hostapd"""
360 _test_ap_bss_add_many(dev, apdev)
362 dev[0].request("SCAN_INTERVAL 5")
363 ifname = apdev[0]['ifname']
364 hapd = hostapd.HostapdGlobal()
367 ifname2 = ifname + '-' + str(i)
370 os.remove('/tmp/hwsim-bss.conf')
374 def _test_ap_bss_add_many(dev, apdev):
375 ifname = apdev[0]['ifname']
377 hostapd.add_bss(phy, ifname, 'bss-1.conf')
378 hapd = hostapd.HostapdGlobal()
379 fname = '/tmp/hwsim-bss.conf'
381 ifname2 = ifname + '-' + str(i)
382 with open(fname, 'w') as f:
383 f.write("driver=nl80211\n")
384 f.write("hw_mode=g\n")
385 f.write("channel=1\n")
386 f.write("ieee80211n=1\n")
387 f.write("interface=%s\n" % ifname2)
388 f.write("bssid=02:00:00:00:03:%02x\n" % (i + 1))
389 f.write("ctrl_interface=/var/run/hostapd\n")
390 f.write("ssid=test-%d\n" % i)
391 hostapd.add_bss(phy, ifname2, fname)
394 dev[0].request("SCAN_INTERVAL 1")
395 dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
396 dev[0].request("DISCONNECT")
397 dev[0].wait_disconnected(timeout=5)
399 dev[0].connect("test-%d" % i, key_mgmt="NONE", scan_freq="2412")
400 dev[0].request("DISCONNECT")
401 dev[0].wait_disconnected(timeout=5)
402 ifname2 = ifname + '-' + str(i)
405 def test_ap_bss_add_reuse_existing(dev, apdev):
406 """Dynamic BSS add operation reusing existing interface"""
407 ifname1 = apdev[0]['ifname']
408 ifname2 = apdev[0]['ifname'] + '-2'
409 hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
410 subprocess.check_call(["iw", "dev", ifname1, "interface", "add", ifname2,
412 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
413 hostapd.remove_bss(ifname2)
414 subprocess.check_call(["iw", "dev", ifname2, "del"])
416 def hapd_bss_out_of_mem(hapd, phy, confname, count, func):
417 with alloc_fail(hapd, count, func):
418 hapd_global = hostapd.HostapdGlobal()
419 res = hapd_global.ctrl.request("ADD bss_config=" + phy + ":" + confname)
421 raise Exception("add_bss succeeded")
423 def test_ap_bss_add_out_of_memory(dev, apdev):
424 """Running out of memory while adding a BSS"""
425 hapd2 = hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
427 ifname1 = apdev[0]['ifname']
428 ifname2 = apdev[0]['ifname'] + '-2'
430 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf', 1, 'hostapd_add_iface')
431 for i in range(1, 3):
432 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
433 i, 'hostapd_interface_init_bss')
434 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
435 1, 'ieee802_11_build_ap_params')
437 hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
439 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
440 1, 'hostapd_interface_init_bss')
441 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
442 1, 'ieee802_11_build_ap_params')
444 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
445 hostapd.remove_bss(ifname2)
446 hostapd.remove_bss(ifname1)
448 def test_ap_multi_bss(dev, apdev):
449 """Multiple BSSes with hostapd"""
450 ifname1 = apdev[0]['ifname']
451 ifname2 = apdev[0]['ifname'] + '-2'
452 hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
453 hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
454 dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
455 dev[1].connect("bss-2", key_mgmt="NONE", scan_freq="2412")
457 hapd1 = hostapd.Hostapd(ifname1)
458 hapd2 = hostapd.Hostapd(ifname2)
460 hwsim_utils.test_connectivity(dev[0], hapd1)
461 hwsim_utils.test_connectivity(dev[1], hapd2)
463 sta0 = hapd1.get_sta(dev[0].own_addr())
464 sta1 = hapd2.get_sta(dev[1].own_addr())
465 if 'rx_packets' not in sta0 or int(sta0['rx_packets']) < 1:
466 raise Exception("sta0 did not report receiving packets")
467 if 'rx_packets' not in sta1 or int(sta1['rx_packets']) < 1:
468 raise Exception("sta1 did not report receiving packets")
470 def test_ap_add_with_driver(dev, apdev):
471 """Add hostapd interface with driver specified"""
472 ifname = apdev[0]['ifname']
473 hapd_global = hostapd.HostapdGlobal()
474 hapd_global.add(ifname, driver="nl80211")
475 hapd = hostapd.Hostapd(ifname)
477 hapd.set("ssid", "dynamic")
479 ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
481 raise Exception("AP startup timed out")
482 dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
483 dev[0].request("DISCONNECT")
484 dev[0].wait_disconnected()