Updated to hostap_2_6
[mech_eap.git] / libeap / tests / hwsim / test_ap_dynamic.py
1 # Test cases for dynamic BSS changes with hostapd
2 # Copyright (c) 2013, 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 time
9 import subprocess
10 import logging
11 logger = logging.getLogger()
12 import os
13
14 import hwsim_utils
15 import hostapd
16 from utils import alloc_fail, require_under_vm
17 from test_ap_acs import force_prev_ap_on_24g
18
19 @remote_compatible
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",
26                         scan_freq="2412")
27     dev[0].request("DISCONNECT")
28
29     logger.info("Change SSID dynamically")
30     res = hapd.request("SET ssid test-wpa2-psk-new")
31     if "OK" not in res:
32         raise Exception("SET command failed")
33     res = hapd.request("RELOAD")
34     if "OK" not in res:
35         raise Exception("RELOAD command failed")
36
37     dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
38     dev[0].connect_network(id)
39
40 def multi_check(dev, check, scan_opt=True):
41     id = []
42     num_bss = len(check)
43     for i in range(0, num_bss):
44         dev[i].request("BSS_FLUSH 0")
45         dev[i].dump_monitor()
46     for i in range(0, num_bss):
47         if check[i]:
48             continue
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):
52         if not check[i]:
53             continue
54         bssid = '02:00:00:00:03:0' + str(i)
55         if scan_opt:
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))
59     first = True
60     for i in range(num_bss):
61         if not check[i]:
62             timeout=0.2 if first else 0.01
63             first = False
64             ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
65             if ev:
66                 raise Exception("Unexpected connection")
67
68     for i in range(0, num_bss):
69         dev[i].remove_network(id[i])
70     for i in range(num_bss):
71         if check[i]:
72             dev[i].wait_disconnected(timeout=5)
73
74     res = ''
75     for i in range(0, num_bss):
76         res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
77
78     for i in range(0, num_bss):
79         if not check[i]:
80             bssid = '02:00:00:00:03:0' + str(i)
81             if bssid in res:
82                 raise Exception("Unexpected BSS" + str(i) + " in scan results")
83
84 def test_ap_bss_add_remove(dev, apdev):
85     """Dynamic BSS add/remove operations with hostapd"""
86     try:
87         _test_ap_bss_add_remove(dev, apdev)
88     finally:
89         for i in range(3):
90             dev[i].request("SCAN_INTERVAL 5")
91
92 def _test_ap_bss_add_remove(dev, apdev):
93     for i in range(3):
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 ])
106
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 ])
112
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 ])
118
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 ])
126
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 ])
136
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 ])
150
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 ])
154
155 def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
156     """Dynamic BSS add during HT40 co-ex scan"""
157     for i in range(3):
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)
166
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)
172
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 ])
177
178 def test_ap_multi_bss_config(dev, apdev):
179     """hostapd start with a multi-BSS configuration file"""
180     for i in range(3):
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')
187     hapd.enable()
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 ])
195
196     hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
197     hapd.enable()
198     hostapd.remove_bss(apdev[0], ifname1)
199     multi_check(dev, [ False, False, False ])
200
201 def invalid_ap(ap):
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")
206     try:
207         hapd.enable()
208         started = True
209     except Exception, e:
210         started = False
211     if started:
212         raise Exception("ENABLE command succeeded unexpectedly")
213     return hapd
214
215 @remote_compatible
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])
219
220     logger.info("Fix configuration and start AP again")
221     hapd.set("channel", "1")
222     hapd.enable()
223     dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
224
225 @remote_compatible
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])
231
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])
239
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")
250     hapd.enable()
251     hostapd.remove_bss(apdev[0])
252
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")
263     hapd.enable()
264     hostapd.remove_bss(apdev[0], ifname2)
265
266 @remote_compatible
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])
275
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")
286     hapd.enable()
287     hostapd.remove_bss(apdev[0])
288
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")
299     hapd.enable()
300     hostapd.remove_bss(apdev[0], ifname2)
301
302 @remote_compatible
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")
307     hapd.enable()
308     ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
309     if ev is None:
310         raise Exception("AP startup timed out")
311     dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
312     hapd.disable()
313     ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
314     if ev is None:
315         raise Exception("AP disabling timed out")
316     dev[0].wait_disconnected(timeout=10)
317     hapd.enable()
318     ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
319     if ev is None:
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)
323
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')
328     hapd.disable()
329     if "FAIL" not in hapd.request("DISABLE"):
330         raise Exception("Second DISABLE accepted unexpectedly")
331     hapd.enable()
332     hapd.disable()
333     if "FAIL" not in hapd.request("DISABLE"):
334         raise Exception("Second DISABLE accepted unexpectedly")
335
336 def test_ap_bss_add_many(dev, apdev):
337     """Large number of BSS add operations with hostapd"""
338     try:
339         _test_ap_bss_add_many(dev, apdev)
340     finally:
341         dev[0].request("SCAN_INTERVAL 5")
342         ifname = apdev[0]['ifname']
343         hapd = hostapd.HostapdGlobal(apdev[0])
344         hapd.flush()
345         for i in range(16):
346             ifname2 = ifname + '-' + str(i)
347             hapd.remove(ifname2)
348         try:
349             os.remove('/tmp/hwsim-bss.conf')
350         except:
351             pass
352
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'
357     for i in range(16):
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)
369         os.remove(fname)
370
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)
375     for i in range(16):
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)
381
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,
388                            "type", "__ap"])
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"])
392
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)
397         if "OK" in res:
398             raise Exception("add_bss succeeded")
399
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" })
403
404     ifname1 = apdev[0]['ifname']
405     ifname2 = apdev[0]['ifname'] + '-2'
406
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')
413
414     hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
415
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')
420
421     hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
422     hostapd.remove_bss(apdev[0], ifname2)
423     hostapd.remove_bss(apdev[0], ifname1)
424
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")
433
434     hwsim_utils.test_connectivity(dev[0], hapd1)
435     hwsim_utils.test_connectivity(dev[1], hapd2)
436
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")
443
444 @remote_compatible
445 def test_ap_add_with_driver(dev, apdev):
446     """Add hostapd interface with driver specified"""
447     ifname = apdev[0]['ifname']
448     try:
449        hostname = apdev[0]['hostname']
450     except:
451        hostname = None
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)
456     hapd.set_defaults()
457     hapd.set("ssid", "dynamic")
458     hapd.enable()
459     ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
460     if ev is None:
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()
465     hapd.disable()
466
467 def test_ap_iapp(dev, apdev):
468     """IAPP and multiple BSSes"""
469     require_under_vm()
470     try:
471         _test_ap_iapp(dev, apdev)
472     finally:
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'))
477
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])
485
486     params = { "ssid": "test-1",
487                "bridge": br_ifname,
488                "iapp_interface": br_ifname }
489     hapd = hostapd.add_ap(apdev[0], params)
490
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")
494
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'])
499
500     dev[0].request("DISCONNECT")
501     dev[1].request("DISCONNECT")
502     dev[0].wait_disconnected()
503     dev[1].wait_disconnected()
504
505     hapd.disable()
506     hapd2.disable()