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.
7 from remotehost import remote_compatible
11 logger = logging.getLogger()
16 from utils import alloc_fail, require_under_vm
17 from test_ap_acs import force_prev_ap_on_24g
20 def test_ap_change_ssid(dev, apdev):
21 """Dynamic SSID change with hostapd and WPA2-PSK"""
22 params = hostapd.wpa2_params(ssid="test-wpa2-psk-start",
23 passphrase="12345678")
24 hapd = hostapd.add_ap(apdev[0], params)
25 id = dev[0].connect("test-wpa2-psk-start", psk="12345678",
27 dev[0].request("DISCONNECT")
29 logger.info("Change SSID dynamically")
30 res = hapd.request("SET ssid test-wpa2-psk-new")
32 raise Exception("SET command failed")
33 res = hapd.request("RELOAD")
35 raise Exception("RELOAD command failed")
37 dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
38 dev[0].connect_network(id)
40 def multi_check(dev, check, scan_opt=True):
43 for i in range(0, num_bss):
44 dev[i].request("BSS_FLUSH 0")
46 for i in range(0, num_bss):
49 id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
50 scan_freq="2412", wait_connect=False))
51 for i in range(num_bss):
54 bssid = '02:00:00:00:03:0' + str(i)
56 dev[i].scan_for_bss(bssid, freq=2412)
57 id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
58 scan_freq="2412", wait_connect=True))
60 for i in range(num_bss):
62 timeout=0.2 if first else 0.01
64 ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
66 raise Exception("Unexpected connection")
68 for i in range(0, num_bss):
69 dev[i].remove_network(id[i])
70 for i in range(num_bss):
72 dev[i].wait_disconnected(timeout=5)
75 for i in range(0, num_bss):
76 res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
78 for i in range(0, num_bss):
80 bssid = '02:00:00:00:03:0' + str(i)
82 raise Exception("Unexpected BSS" + str(i) + " in scan results")
84 def test_ap_bss_add_remove(dev, apdev):
85 """Dynamic BSS add/remove operations with hostapd"""
87 _test_ap_bss_add_remove(dev, apdev)
90 dev[i].request("SCAN_INTERVAL 5")
92 def _test_ap_bss_add_remove(dev, apdev):
94 dev[i].flush_scan_cache()
95 dev[i].request("SCAN_INTERVAL 1")
96 ifname1 = apdev[0]['ifname']
97 ifname2 = apdev[0]['ifname'] + '-2'
98 ifname3 = apdev[0]['ifname'] + '-3'
99 logger.info("Set up three BSSes one by one")
100 hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
101 multi_check(dev, [ True, False, False ])
102 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
103 multi_check(dev, [ True, True, False ])
104 hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
105 multi_check(dev, [ True, True, True ])
107 logger.info("Remove the last BSS and re-add it")
108 hostapd.remove_bss(apdev[0], ifname3)
109 multi_check(dev, [ True, True, False ])
110 hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
111 multi_check(dev, [ True, True, True ])
113 logger.info("Remove the middle BSS and re-add it")
114 hostapd.remove_bss(apdev[0], ifname2)
115 multi_check(dev, [ True, False, True ])
116 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
117 multi_check(dev, [ True, True, True ])
119 logger.info("Remove the first BSS and re-add it and other BSSs")
120 hostapd.remove_bss(apdev[0], ifname1)
121 multi_check(dev, [ False, False, False ])
122 hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
123 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
124 hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
125 multi_check(dev, [ True, True, True ])
127 logger.info("Remove two BSSes and re-add them")
128 hostapd.remove_bss(apdev[0], ifname2)
129 multi_check(dev, [ True, False, True ])
130 hostapd.remove_bss(apdev[0], ifname3)
131 multi_check(dev, [ True, False, False ])
132 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
133 multi_check(dev, [ True, True, False ])
134 hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
135 multi_check(dev, [ True, True, True ])
137 logger.info("Remove three BSSes in and re-add them")
138 hostapd.remove_bss(apdev[0], ifname3)
139 multi_check(dev, [ True, True, False ])
140 hostapd.remove_bss(apdev[0], ifname2)
141 multi_check(dev, [ True, False, False ])
142 hostapd.remove_bss(apdev[0], ifname1)
143 multi_check(dev, [ False, False, False ])
144 hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
145 multi_check(dev, [ True, False, False ])
146 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
147 multi_check(dev, [ True, True, False ])
148 hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
149 multi_check(dev, [ True, True, True ])
151 logger.info("Test error handling if a duplicate ifname is tried")
152 hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf', ignore_error=True)
153 multi_check(dev, [ True, True, True ])
155 def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
156 """Dynamic BSS add during HT40 co-ex scan"""
158 dev[i].flush_scan_cache()
159 ifname1 = apdev[0]['ifname']
160 ifname2 = apdev[0]['ifname'] + '-2'
161 hostapd.add_bss(apdev[0], ifname1, 'bss-ht40-1.conf')
162 hostapd.add_bss(apdev[0], ifname2, 'bss-ht40-2.conf')
163 multi_check(dev, [ True, True ], scan_opt=False)
164 hostapd.remove_bss(apdev[0], ifname2)
165 hostapd.remove_bss(apdev[0], ifname1)
167 hostapd.add_bss(apdev[0], ifname1, 'bss-ht40-1.conf')
168 hostapd.add_bss(apdev[0], ifname2, 'bss-ht40-2.conf')
169 hostapd.remove_bss(apdev[0], ifname2)
170 multi_check(dev, [ True, False ], scan_opt=False)
171 hostapd.remove_bss(apdev[0], ifname1)
173 hostapd.add_bss(apdev[0], ifname1, 'bss-ht40-1.conf')
174 hostapd.add_bss(apdev[0], ifname2, 'bss-ht40-2.conf')
175 hostapd.remove_bss(apdev[0], ifname1)
176 multi_check(dev, [ False, False ])
178 def test_ap_multi_bss_config(dev, apdev):
179 """hostapd start with a multi-BSS configuration file"""
181 dev[i].flush_scan_cache()
182 ifname1 = apdev[0]['ifname']
183 ifname2 = apdev[0]['ifname'] + '-2'
184 ifname3 = apdev[0]['ifname'] + '-3'
185 logger.info("Set up three BSSes with one configuration file")
186 hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
188 multi_check(dev, [ True, True, True ])
189 hostapd.remove_bss(apdev[0], ifname2)
190 multi_check(dev, [ True, False, True ])
191 hostapd.remove_bss(apdev[0], ifname3)
192 multi_check(dev, [ True, False, False ])
193 hostapd.remove_bss(apdev[0], ifname1)
194 multi_check(dev, [ False, False, False ])
196 hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
198 hostapd.remove_bss(apdev[0], ifname1)
199 multi_check(dev, [ False, False, False ])
202 logger.info("Trying to start AP " + ap['ifname'] + " with invalid configuration")
203 hapd = hostapd.add_ap(ap, {}, no_enable=True)
204 hapd.set("ssid", "invalid-config")
205 hapd.set("channel", "12345")
212 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 = invalid_ap(apdev[0])
220 logger.info("Fix configuration and start AP again")
221 hapd.set("channel", "1")
223 dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
226 def test_ap_invalid_config2(dev, apdev):
227 """Try to start AP with invalid configuration and remove interface"""
228 hapd = invalid_ap(apdev[0])
229 logger.info("Remove interface with failed configuration")
230 hostapd.remove_bss(apdev[0])
232 def test_ap_remove_during_acs(dev, apdev):
233 """Remove interface during ACS"""
234 force_prev_ap_on_24g(apdev[0])
235 params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
236 params['channel'] = '0'
237 hostapd.add_ap(apdev[0], params)
238 hostapd.remove_bss(apdev[0])
240 def test_ap_remove_during_acs2(dev, apdev):
241 """Remove BSS during ACS in multi-BSS configuration"""
242 force_prev_ap_on_24g(apdev[0])
243 ifname = apdev[0]['ifname']
244 ifname2 = ifname + "-2"
245 hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
246 hapd.set("ssid", "test-acs-remove")
247 hapd.set("channel", "0")
248 hapd.set("bss", ifname2)
249 hapd.set("ssid", "test-acs-remove2")
251 hostapd.remove_bss(apdev[0])
253 def test_ap_remove_during_acs3(dev, apdev):
254 """Remove second BSS during ACS in multi-BSS configuration"""
255 force_prev_ap_on_24g(apdev[0])
256 ifname = apdev[0]['ifname']
257 ifname2 = ifname + "-2"
258 hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
259 hapd.set("ssid", "test-acs-remove")
260 hapd.set("channel", "0")
261 hapd.set("bss", ifname2)
262 hapd.set("ssid", "test-acs-remove2")
264 hostapd.remove_bss(apdev[0], ifname2)
267 def test_ap_remove_during_ht_coex_scan(dev, apdev):
268 """Remove interface during HT co-ex scan"""
269 params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
270 params['channel'] = '1'
271 params['ht_capab'] = "[HT40+]"
272 ifname = apdev[0]['ifname']
273 hostapd.add_ap(apdev[0], params)
274 hostapd.remove_bss(apdev[0])
276 def test_ap_remove_during_ht_coex_scan2(dev, apdev):
277 """Remove BSS during HT co-ex scan in multi-BSS configuration"""
278 ifname = apdev[0]['ifname']
279 ifname2 = ifname + "-2"
280 hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
281 hapd.set("ssid", "test-ht-remove")
282 hapd.set("channel", "1")
283 hapd.set("ht_capab", "[HT40+]")
284 hapd.set("bss", ifname2)
285 hapd.set("ssid", "test-ht-remove2")
287 hostapd.remove_bss(apdev[0])
289 def test_ap_remove_during_ht_coex_scan3(dev, apdev):
290 """Remove second BSS during HT co-ex scan in multi-BSS configuration"""
291 ifname = apdev[0]['ifname']
292 ifname2 = ifname + "-2"
293 hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
294 hapd.set("ssid", "test-ht-remove")
295 hapd.set("channel", "1")
296 hapd.set("ht_capab", "[HT40+]")
297 hapd.set("bss", ifname2)
298 hapd.set("ssid", "test-ht-remove2")
300 hostapd.remove_bss(apdev[0], ifname2)
303 def test_ap_enable_disable_reenable(dev, apdev):
304 """Enable, disable, re-enable AP"""
305 hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
306 hapd.set("ssid", "dynamic")
308 ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
310 raise Exception("AP startup timed out")
311 dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
313 ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
315 raise Exception("AP disabling timed out")
316 dev[0].wait_disconnected(timeout=10)
318 ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
320 raise Exception("AP startup timed out")
321 dev[1].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
322 dev[0].wait_connected(timeout=10)
324 def test_ap_double_disable(dev, apdev):
325 """Double DISABLE regression test"""
326 hapd = hostapd.add_bss(apdev[0], apdev[0]['ifname'], 'bss-1.conf')
327 hostapd.add_bss(apdev[0], apdev[0]['ifname'] + '-2', 'bss-2.conf')
329 if "FAIL" not in hapd.request("DISABLE"):
330 raise Exception("Second DISABLE accepted unexpectedly")
333 if "FAIL" not in hapd.request("DISABLE"):
334 raise Exception("Second DISABLE accepted unexpectedly")
336 def test_ap_bss_add_many(dev, apdev):
337 """Large number of BSS add operations with hostapd"""
339 _test_ap_bss_add_many(dev, apdev)
341 dev[0].request("SCAN_INTERVAL 5")
342 ifname = apdev[0]['ifname']
343 hapd = hostapd.HostapdGlobal(apdev[0])
346 ifname2 = ifname + '-' + str(i)
349 os.remove('/tmp/hwsim-bss.conf')
353 def _test_ap_bss_add_many(dev, apdev):
354 ifname = apdev[0]['ifname']
355 hostapd.add_bss(apdev[0], ifname, 'bss-1.conf')
356 fname = '/tmp/hwsim-bss.conf'
358 ifname2 = ifname + '-' + str(i)
359 with open(fname, 'w') as f:
360 f.write("driver=nl80211\n")
361 f.write("hw_mode=g\n")
362 f.write("channel=1\n")
363 f.write("ieee80211n=1\n")
364 f.write("interface=%s\n" % ifname2)
365 f.write("bssid=02:00:00:00:03:%02x\n" % (i + 1))
366 f.write("ctrl_interface=/var/run/hostapd\n")
367 f.write("ssid=test-%d\n" % i)
368 hostapd.add_bss(apdev[0], ifname2, fname)
371 dev[0].request("SCAN_INTERVAL 1")
372 dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
373 dev[0].request("DISCONNECT")
374 dev[0].wait_disconnected(timeout=5)
376 dev[0].connect("test-%d" % i, key_mgmt="NONE", scan_freq="2412")
377 dev[0].request("DISCONNECT")
378 dev[0].wait_disconnected(timeout=5)
379 ifname2 = ifname + '-' + str(i)
380 hostapd.remove_bss(apdev[0], ifname2)
382 def test_ap_bss_add_reuse_existing(dev, apdev):
383 """Dynamic BSS add operation reusing existing interface"""
384 ifname1 = apdev[0]['ifname']
385 ifname2 = apdev[0]['ifname'] + '-2'
386 hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
387 subprocess.check_call(["iw", "dev", ifname1, "interface", "add", ifname2,
389 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
390 hostapd.remove_bss(apdev[0], ifname2)
391 subprocess.check_call(["iw", "dev", ifname2, "del"])
393 def hapd_bss_out_of_mem(hapd, phy, confname, count, func):
394 with alloc_fail(hapd, count, func):
395 hapd_global = hostapd.HostapdGlobal()
396 res = hapd_global.ctrl.request("ADD bss_config=" + phy + ":" + confname)
398 raise Exception("add_bss succeeded")
400 def test_ap_bss_add_out_of_memory(dev, apdev):
401 """Running out of memory while adding a BSS"""
402 hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
404 ifname1 = apdev[0]['ifname']
405 ifname2 = apdev[0]['ifname'] + '-2'
407 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf', 1, 'hostapd_add_iface')
408 for i in range(1, 3):
409 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
410 i, 'hostapd_interface_init_bss')
411 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
412 1, 'ieee802_11_build_ap_params')
414 hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
416 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
417 1, 'hostapd_interface_init_bss')
418 hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
419 1, 'ieee802_11_build_ap_params')
421 hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
422 hostapd.remove_bss(apdev[0], ifname2)
423 hostapd.remove_bss(apdev[0], ifname1)
425 def test_ap_multi_bss(dev, apdev):
426 """Multiple BSSes with hostapd"""
427 ifname1 = apdev[0]['ifname']
428 ifname2 = apdev[0]['ifname'] + '-2'
429 hapd1 = hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
430 hapd2 = hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
431 dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
432 dev[1].connect("bss-2", key_mgmt="NONE", scan_freq="2412")
434 hwsim_utils.test_connectivity(dev[0], hapd1)
435 hwsim_utils.test_connectivity(dev[1], hapd2)
437 sta0 = hapd1.get_sta(dev[0].own_addr())
438 sta1 = hapd2.get_sta(dev[1].own_addr())
439 if 'rx_packets' not in sta0 or int(sta0['rx_packets']) < 1:
440 raise Exception("sta0 did not report receiving packets")
441 if 'rx_packets' not in sta1 or int(sta1['rx_packets']) < 1:
442 raise Exception("sta1 did not report receiving packets")
445 def test_ap_add_with_driver(dev, apdev):
446 """Add hostapd interface with driver specified"""
447 ifname = apdev[0]['ifname']
449 hostname = apdev[0]['hostname']
452 hapd_global = hostapd.HostapdGlobal(apdev[0])
453 hapd_global.add(ifname, driver="nl80211")
454 port = hapd_global.get_ctrl_iface_port(ifname)
455 hapd = hostapd.Hostapd(ifname, hostname, port)
457 hapd.set("ssid", "dynamic")
459 ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
461 raise Exception("AP startup timed out")
462 dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
463 dev[0].request("DISCONNECT")
464 dev[0].wait_disconnected()
467 def test_ap_iapp(dev, apdev):
468 """IAPP and multiple BSSes"""
471 _test_ap_iapp(dev, apdev)
473 subprocess.call(['ifconfig', 'br-multicast', 'down'],
474 stderr=open('/dev/null', 'w'))
475 subprocess.call(['brctl', 'delbr', 'br-multicast'],
476 stderr=open('/dev/null', 'w'))
478 def _test_ap_iapp(dev, apdev):
479 br_ifname = 'br-multicast'
480 subprocess.call(['brctl', 'addbr', br_ifname])
481 subprocess.call(['brctl', 'setfd', br_ifname, '0'])
482 subprocess.call(['ip', 'addr', 'add', '10.174.65.206/31', 'dev', br_ifname])
483 subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
484 subprocess.call(['ip', 'route', 'add', '224.0.0.0/4', 'dev', br_ifname])
486 params = { "ssid": "test-1",
488 "iapp_interface": br_ifname }
489 hapd = hostapd.add_ap(apdev[0], params)
491 dev[0].scan_for_bss(apdev[0]['bssid'], freq=2412)
492 dev[0].connect("test-1", key_mgmt="NONE", scan_freq="2412")
493 dev[1].connect("test-1", key_mgmt="NONE", scan_freq="2412")
495 hapd2 = hostapd.add_ap(apdev[1], params)
496 dev[0].scan_for_bss(apdev[1]['bssid'], freq=2412)
497 dev[0].roam(apdev[1]['bssid'])
498 dev[0].roam(apdev[0]['bssid'])
500 dev[0].request("DISCONNECT")
501 dev[1].request("DISCONNECT")
502 dev[0].wait_disconnected()
503 dev[1].wait_disconnected()