Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[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 import time
8 import subprocess
9 import logging
10 logger = logging.getLogger()
11 import os
12
13 import hwsim_utils
14 import hostapd
15 from utils import alloc_fail
16 from test_ap_acs import force_prev_ap_on_24g
17
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",
24                         scan_freq="2412")
25     dev[0].request("DISCONNECT")
26
27     logger.info("Change SSID dynamically")
28     hapd = hostapd.Hostapd(apdev[0]['ifname'])
29     res = hapd.request("SET ssid test-wpa2-psk-new")
30     if "OK" not in res:
31         raise Exception("SET command failed")
32     res = hapd.request("RELOAD")
33     if "OK" not in res:
34         raise Exception("RELOAD command failed")
35
36     dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
37     dev[0].connect_network(id)
38
39 def multi_check(dev, check, scan_opt=True):
40     id = []
41     num_bss = len(check)
42     for i in range(0, num_bss):
43         dev[i].request("BSS_FLUSH 0")
44         dev[i].dump_monitor()
45     for i in range(0, num_bss):
46         if check[i]:
47             continue
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):
51         if not check[i]:
52             continue
53         bssid = '02:00:00:00:03:0' + str(i)
54         if scan_opt:
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))
58     first = True
59     for i in range(num_bss):
60         if not check[i]:
61             timeout=0.2 if first else 0.01
62             first = False
63             ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
64             if ev:
65                 raise Exception("Unexpected connection")
66
67     for i in range(0, num_bss):
68         dev[i].remove_network(id[i])
69     for i in range(num_bss):
70         if check[i]:
71             dev[i].wait_disconnected(timeout=5)
72
73     res = ''
74     for i in range(0, num_bss):
75         res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
76
77     for i in range(0, num_bss):
78         if not check[i]:
79             bssid = '02:00:00:00:03:0' + str(i)
80             if bssid in res:
81                 raise Exception("Unexpected BSS" + str(i) + " in scan results")
82
83 def test_ap_bss_add_remove(dev, apdev):
84     """Dynamic BSS add/remove operations with hostapd"""
85     try:
86         _test_ap_bss_add_remove(dev, apdev)
87     finally:
88         for i in range(3):
89             dev[i].request("SCAN_INTERVAL 5")
90
91 def _test_ap_bss_add_remove(dev, apdev):
92     for i in range(3):
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 ])
104
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 ])
110
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 ])
116
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 ])
124
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 ])
134
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 ])
148
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 ])
152
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)
162
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)
168
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 ])
173
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)
182     hapd.enable()
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 ])
190
191     hostapd.add_iface(ifname1, 'multi-bss.conf')
192     hapd = hostapd.Hostapd(ifname1)
193     hapd.enable()
194     hostapd.remove_bss(ifname1)
195     multi_check(dev, [ False, False, False ])
196
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)
202     if not hapd.ping():
203         raise Exception("Could not ping hostapd")
204     hapd.set_defaults()
205     hapd.set("ssid", "invalid-config")
206     hapd.set("channel", "12345")
207     try:
208         hapd.enable()
209         started = True
210     except Exception, e:
211         started = False
212     if started:
213         raise Exception("ENABLE command succeeded unexpectedly")
214     return hapd
215
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)
221
222     logger.info("Fix configuration and start AP again")
223     hapd.set("channel", "1")
224     hapd.enable()
225     dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
226
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)
234
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)
243     hapd.remove(ifname)
244
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)
253     hapd.set_defaults()
254     hapd.set("ssid", "test-acs-remove")
255     hapd.set("channel", "0")
256     hapd.set("bss", ifname2)
257     hapd.set("ssid", "test-acs-remove2")
258     hapd.enable()
259     hapd_global.remove(ifname)
260
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)
269     hapd.set_defaults()
270     hapd.set("ssid", "test-acs-remove")
271     hapd.set("channel", "0")
272     hapd.set("bss", ifname2)
273     hapd.set("ssid", "test-acs-remove2")
274     hapd.enable()
275     hapd_global.remove(ifname2)
276
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)
285     hapd.remove(ifname)
286
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)
294     hapd.set_defaults()
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")
300     hapd.enable()
301     hapd_global.remove(ifname)
302
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)
310     hapd.set_defaults()
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")
316     hapd.enable()
317     hapd_global.remove(ifname2)
318
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)
325     hapd.set_defaults()
326     hapd.set("ssid", "dynamic")
327     hapd.enable()
328     ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
329     if ev is None:
330         raise Exception("AP startup timed out")
331     dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
332     hapd.disable()
333     ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
334     if ev is None:
335         raise Exception("AP disabling timed out")
336     dev[0].wait_disconnected(timeout=10)
337     hapd.enable()
338     ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
339     if ev is None:
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)
343
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'])
349     hapd.disable()
350     if "FAIL" not in hapd.request("DISABLE"):
351         raise Exception("Second DISABLE accepted unexpectedly")
352     hapd.enable()
353     hapd.disable()
354     if "FAIL" not in hapd.request("DISABLE"):
355         raise Exception("Second DISABLE accepted unexpectedly")
356
357 def test_ap_bss_add_many(dev, apdev):
358     """Large number of BSS add operations with hostapd"""
359     try:
360         _test_ap_bss_add_many(dev, apdev)
361     finally:
362         dev[0].request("SCAN_INTERVAL 5")
363         ifname = apdev[0]['ifname']
364         hapd = hostapd.HostapdGlobal()
365         hapd.flush()
366         for i in range(16):
367             ifname2 = ifname + '-' + str(i)
368             hapd.remove(ifname2)
369         try:
370             os.remove('/tmp/hwsim-bss.conf')
371         except:
372             pass
373
374 def _test_ap_bss_add_many(dev, apdev):
375     ifname = apdev[0]['ifname']
376     phy = 'phy3'
377     hostapd.add_bss(phy, ifname, 'bss-1.conf')
378     hapd = hostapd.HostapdGlobal()
379     fname = '/tmp/hwsim-bss.conf'
380     for i in range(16):
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)
392         os.remove(fname)
393
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)
398     for i in range(16):
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)
403         hapd.remove(ifname2)
404
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,
411                            "type", "__ap"])
412     hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
413     hostapd.remove_bss(ifname2)
414     subprocess.check_call(["iw", "dev", ifname2, "del"])
415
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)
420         if "OK" in res:
421             raise Exception("add_bss succeeded")
422
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" })
426
427     ifname1 = apdev[0]['ifname']
428     ifname2 = apdev[0]['ifname'] + '-2'
429
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')
436
437     hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
438
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')
443
444     hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
445     hostapd.remove_bss(ifname2)
446     hostapd.remove_bss(ifname1)
447
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")
456
457     hapd1 = hostapd.Hostapd(ifname1)
458     hapd2 = hostapd.Hostapd(ifname2)
459
460     hwsim_utils.test_connectivity(dev[0], hapd1)
461     hwsim_utils.test_connectivity(dev[1], hapd2)
462
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")
469
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)
476     hapd.set_defaults()
477     hapd.set("ssid", "dynamic")
478     hapd.enable()
479     ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
480     if ev is None:
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()
485     hapd.disable()