test_ap_dynamic.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. import time
  7. import subprocess
  8. import logging
  9. logger = logging.getLogger()
  10. import os
  11. import hwsim_utils
  12. import hostapd
  13. from utils import alloc_fail
  14. from test_ap_acs import force_prev_ap_on_24g
  15. def test_ap_change_ssid(dev, apdev):
  16. """Dynamic SSID change with hostapd and WPA2-PSK"""
  17. params = hostapd.wpa2_params(ssid="test-wpa2-psk-start",
  18. passphrase="12345678")
  19. hapd = hostapd.add_ap(apdev[0], params)
  20. id = dev[0].connect("test-wpa2-psk-start", psk="12345678",
  21. scan_freq="2412")
  22. dev[0].request("DISCONNECT")
  23. logger.info("Change SSID dynamically")
  24. res = hapd.request("SET ssid test-wpa2-psk-new")
  25. if "OK" not in res:
  26. raise Exception("SET command failed")
  27. res = hapd.request("RELOAD")
  28. if "OK" not in res:
  29. raise Exception("RELOAD command failed")
  30. dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
  31. dev[0].connect_network(id)
  32. def multi_check(dev, check, scan_opt=True):
  33. id = []
  34. num_bss = len(check)
  35. for i in range(0, num_bss):
  36. dev[i].request("BSS_FLUSH 0")
  37. dev[i].dump_monitor()
  38. for i in range(0, num_bss):
  39. if check[i]:
  40. continue
  41. id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
  42. scan_freq="2412", wait_connect=False))
  43. for i in range(num_bss):
  44. if not check[i]:
  45. continue
  46. bssid = '02:00:00:00:03:0' + str(i)
  47. if scan_opt:
  48. dev[i].scan_for_bss(bssid, freq=2412)
  49. id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
  50. scan_freq="2412", wait_connect=True))
  51. first = True
  52. for i in range(num_bss):
  53. if not check[i]:
  54. timeout=0.2 if first else 0.01
  55. first = False
  56. ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
  57. if ev:
  58. raise Exception("Unexpected connection")
  59. for i in range(0, num_bss):
  60. dev[i].remove_network(id[i])
  61. for i in range(num_bss):
  62. if check[i]:
  63. dev[i].wait_disconnected(timeout=5)
  64. res = ''
  65. for i in range(0, num_bss):
  66. res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
  67. for i in range(0, num_bss):
  68. if not check[i]:
  69. bssid = '02:00:00:00:03:0' + str(i)
  70. if bssid in res:
  71. raise Exception("Unexpected BSS" + str(i) + " in scan results")
  72. def test_ap_bss_add_remove(dev, apdev):
  73. """Dynamic BSS add/remove operations with hostapd"""
  74. try:
  75. _test_ap_bss_add_remove(dev, apdev)
  76. finally:
  77. for i in range(3):
  78. dev[i].request("SCAN_INTERVAL 5")
  79. def _test_ap_bss_add_remove(dev, apdev):
  80. for i in range(3):
  81. dev[i].flush_scan_cache()
  82. dev[i].request("SCAN_INTERVAL 1")
  83. ifname1 = apdev[0]['ifname']
  84. ifname2 = apdev[0]['ifname'] + '-2'
  85. ifname3 = apdev[0]['ifname'] + '-3'
  86. logger.info("Set up three BSSes one by one")
  87. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  88. multi_check(dev, [ True, False, False ])
  89. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  90. multi_check(dev, [ True, True, False ])
  91. hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
  92. multi_check(dev, [ True, True, True ])
  93. logger.info("Remove the last BSS and re-add it")
  94. hostapd.remove_bss(apdev[0], ifname3)
  95. multi_check(dev, [ True, True, False ])
  96. hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
  97. multi_check(dev, [ True, True, True ])
  98. logger.info("Remove the middle BSS and re-add it")
  99. hostapd.remove_bss(apdev[0], ifname2)
  100. multi_check(dev, [ True, False, True ])
  101. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  102. multi_check(dev, [ True, True, True ])
  103. logger.info("Remove the first BSS and re-add it and other BSSs")
  104. hostapd.remove_bss(apdev[0], ifname1)
  105. multi_check(dev, [ False, False, False ])
  106. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  107. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  108. hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
  109. multi_check(dev, [ True, True, True ])
  110. logger.info("Remove two BSSes and re-add them")
  111. hostapd.remove_bss(apdev[0], ifname2)
  112. multi_check(dev, [ True, False, True ])
  113. hostapd.remove_bss(apdev[0], ifname3)
  114. multi_check(dev, [ True, False, False ])
  115. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  116. multi_check(dev, [ True, True, False ])
  117. hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
  118. multi_check(dev, [ True, True, True ])
  119. logger.info("Remove three BSSes in and re-add them")
  120. hostapd.remove_bss(apdev[0], ifname3)
  121. multi_check(dev, [ True, True, False ])
  122. hostapd.remove_bss(apdev[0], ifname2)
  123. multi_check(dev, [ True, False, False ])
  124. hostapd.remove_bss(apdev[0], ifname1)
  125. multi_check(dev, [ False, False, False ])
  126. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  127. multi_check(dev, [ True, False, False ])
  128. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  129. multi_check(dev, [ True, True, False ])
  130. hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf')
  131. multi_check(dev, [ True, True, True ])
  132. logger.info("Test error handling if a duplicate ifname is tried")
  133. hostapd.add_bss(apdev[0], ifname3, 'bss-3.conf', ignore_error=True)
  134. multi_check(dev, [ True, True, True ])
  135. def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
  136. """Dynamic BSS add during HT40 co-ex scan"""
  137. for i in range(3):
  138. dev[i].flush_scan_cache()
  139. ifname1 = apdev[0]['ifname']
  140. ifname2 = apdev[0]['ifname'] + '-2'
  141. hostapd.add_bss(apdev[0], ifname1, 'bss-ht40-1.conf')
  142. hostapd.add_bss(apdev[0], ifname2, 'bss-ht40-2.conf')
  143. multi_check(dev, [ True, True ], scan_opt=False)
  144. hostapd.remove_bss(apdev[0], ifname2)
  145. hostapd.remove_bss(apdev[0], ifname1)
  146. hostapd.add_bss(apdev[0], ifname1, 'bss-ht40-1.conf')
  147. hostapd.add_bss(apdev[0], ifname2, 'bss-ht40-2.conf')
  148. hostapd.remove_bss(apdev[0], ifname2)
  149. multi_check(dev, [ True, False ], scan_opt=False)
  150. hostapd.remove_bss(apdev[0], ifname1)
  151. hostapd.add_bss(apdev[0], ifname1, 'bss-ht40-1.conf')
  152. hostapd.add_bss(apdev[0], ifname2, 'bss-ht40-2.conf')
  153. hostapd.remove_bss(apdev[0], ifname1)
  154. multi_check(dev, [ False, False ])
  155. def test_ap_multi_bss_config(dev, apdev):
  156. """hostapd start with a multi-BSS configuration file"""
  157. for i in range(3):
  158. dev[i].flush_scan_cache()
  159. ifname1 = apdev[0]['ifname']
  160. ifname2 = apdev[0]['ifname'] + '-2'
  161. ifname3 = apdev[0]['ifname'] + '-3'
  162. logger.info("Set up three BSSes with one configuration file")
  163. hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
  164. hapd.enable()
  165. multi_check(dev, [ True, True, True ])
  166. hostapd.remove_bss(apdev[0], ifname2)
  167. multi_check(dev, [ True, False, True ])
  168. hostapd.remove_bss(apdev[0], ifname3)
  169. multi_check(dev, [ True, False, False ])
  170. hostapd.remove_bss(apdev[0], ifname1)
  171. multi_check(dev, [ False, False, False ])
  172. hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
  173. hapd.enable()
  174. hostapd.remove_bss(apdev[0], ifname1)
  175. multi_check(dev, [ False, False, False ])
  176. def invalid_ap(ap):
  177. logger.info("Trying to start AP " + ap['ifname'] + " with invalid configuration")
  178. hapd = hostapd.add_ap(ap, {}, no_enable=True)
  179. hapd.set("ssid", "invalid-config")
  180. hapd.set("channel", "12345")
  181. try:
  182. hapd.enable()
  183. started = True
  184. except Exception, e:
  185. started = False
  186. if started:
  187. raise Exception("ENABLE command succeeded unexpectedly")
  188. return hapd
  189. def test_ap_invalid_config(dev, apdev):
  190. """Try to start AP with invalid configuration and fix configuration"""
  191. hapd = invalid_ap(apdev[0])
  192. logger.info("Fix configuration and start AP again")
  193. hapd.set("channel", "1")
  194. hapd.enable()
  195. dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
  196. def test_ap_invalid_config2(dev, apdev):
  197. """Try to start AP with invalid configuration and remove interface"""
  198. hapd = invalid_ap(apdev[0])
  199. logger.info("Remove interface with failed configuration")
  200. hostapd.remove_bss(apdev[0])
  201. def test_ap_remove_during_acs(dev, apdev):
  202. """Remove interface during ACS"""
  203. force_prev_ap_on_24g(apdev[0])
  204. params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
  205. params['channel'] = '0'
  206. hostapd.add_ap(apdev[0], params)
  207. hostapd.remove_bss(apdev[0])
  208. def test_ap_remove_during_acs2(dev, apdev):
  209. """Remove BSS during ACS in multi-BSS configuration"""
  210. force_prev_ap_on_24g(apdev[0])
  211. ifname = apdev[0]['ifname']
  212. ifname2 = ifname + "-2"
  213. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  214. hapd.set("ssid", "test-acs-remove")
  215. hapd.set("channel", "0")
  216. hapd.set("bss", ifname2)
  217. hapd.set("ssid", "test-acs-remove2")
  218. hapd.enable()
  219. hostapd.remove_bss(apdev[0])
  220. def test_ap_remove_during_acs3(dev, apdev):
  221. """Remove second BSS during ACS in multi-BSS configuration"""
  222. force_prev_ap_on_24g(apdev[0])
  223. ifname = apdev[0]['ifname']
  224. ifname2 = ifname + "-2"
  225. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  226. hapd.set("ssid", "test-acs-remove")
  227. hapd.set("channel", "0")
  228. hapd.set("bss", ifname2)
  229. hapd.set("ssid", "test-acs-remove2")
  230. hapd.enable()
  231. hostapd.remove_bss(apdev[0], ifname2)
  232. def test_ap_remove_during_ht_coex_scan(dev, apdev):
  233. """Remove interface during HT co-ex scan"""
  234. params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
  235. params['channel'] = '1'
  236. params['ht_capab'] = "[HT40+]"
  237. ifname = apdev[0]['ifname']
  238. hostapd.add_ap(apdev[0], params)
  239. hostapd.remove_bss(apdev[0])
  240. def test_ap_remove_during_ht_coex_scan2(dev, apdev):
  241. """Remove BSS during HT co-ex scan in multi-BSS configuration"""
  242. ifname = apdev[0]['ifname']
  243. ifname2 = ifname + "-2"
  244. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  245. hapd.set("ssid", "test-ht-remove")
  246. hapd.set("channel", "1")
  247. hapd.set("ht_capab", "[HT40+]")
  248. hapd.set("bss", ifname2)
  249. hapd.set("ssid", "test-ht-remove2")
  250. hapd.enable()
  251. hostapd.remove_bss(apdev[0])
  252. def test_ap_remove_during_ht_coex_scan3(dev, apdev):
  253. """Remove second BSS during HT co-ex scan in multi-BSS configuration"""
  254. ifname = apdev[0]['ifname']
  255. ifname2 = ifname + "-2"
  256. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  257. hapd.set("ssid", "test-ht-remove")
  258. hapd.set("channel", "1")
  259. hapd.set("ht_capab", "[HT40+]")
  260. hapd.set("bss", ifname2)
  261. hapd.set("ssid", "test-ht-remove2")
  262. hapd.enable()
  263. hostapd.remove_bss(apdev[0], ifname2)
  264. def test_ap_enable_disable_reenable(dev, apdev):
  265. """Enable, disable, re-enable AP"""
  266. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  267. hapd.set("ssid", "dynamic")
  268. hapd.enable()
  269. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  270. if ev is None:
  271. raise Exception("AP startup timed out")
  272. dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  273. hapd.disable()
  274. ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
  275. if ev is None:
  276. raise Exception("AP disabling timed out")
  277. dev[0].wait_disconnected(timeout=10)
  278. hapd.enable()
  279. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  280. if ev is None:
  281. raise Exception("AP startup timed out")
  282. dev[1].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  283. dev[0].wait_connected(timeout=10)
  284. def test_ap_double_disable(dev, apdev):
  285. """Double DISABLE regression test"""
  286. hostapd.add_bss(apdev[0], apdev[0]['ifname'], 'bss-1.conf')
  287. hostapd.add_bss(apdev[0], apdev[0]['ifname'] + '-2', 'bss-2.conf')
  288. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  289. hapd.disable()
  290. if "FAIL" not in hapd.request("DISABLE"):
  291. raise Exception("Second DISABLE accepted unexpectedly")
  292. hapd.enable()
  293. hapd.disable()
  294. if "FAIL" not in hapd.request("DISABLE"):
  295. raise Exception("Second DISABLE accepted unexpectedly")
  296. def test_ap_bss_add_many(dev, apdev):
  297. """Large number of BSS add operations with hostapd"""
  298. try:
  299. _test_ap_bss_add_many(dev, apdev)
  300. finally:
  301. dev[0].request("SCAN_INTERVAL 5")
  302. ifname = apdev[0]['ifname']
  303. hapd = hostapd.HostapdGlobal(apdev[0])
  304. hapd.flush()
  305. for i in range(16):
  306. ifname2 = ifname + '-' + str(i)
  307. hapd.remove(ifname2)
  308. try:
  309. os.remove('/tmp/hwsim-bss.conf')
  310. except:
  311. pass
  312. def _test_ap_bss_add_many(dev, apdev):
  313. ifname = apdev[0]['ifname']
  314. hostapd.add_bss(apdev[0], ifname, 'bss-1.conf')
  315. fname = '/tmp/hwsim-bss.conf'
  316. for i in range(16):
  317. ifname2 = ifname + '-' + str(i)
  318. with open(fname, 'w') as f:
  319. f.write("driver=nl80211\n")
  320. f.write("hw_mode=g\n")
  321. f.write("channel=1\n")
  322. f.write("ieee80211n=1\n")
  323. f.write("interface=%s\n" % ifname2)
  324. f.write("bssid=02:00:00:00:03:%02x\n" % (i + 1))
  325. f.write("ctrl_interface=/var/run/hostapd\n")
  326. f.write("ssid=test-%d\n" % i)
  327. hostapd.add_bss(apdev[0], ifname2, fname)
  328. os.remove(fname)
  329. dev[0].request("SCAN_INTERVAL 1")
  330. dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
  331. dev[0].request("DISCONNECT")
  332. dev[0].wait_disconnected(timeout=5)
  333. for i in range(16):
  334. dev[0].connect("test-%d" % i, key_mgmt="NONE", scan_freq="2412")
  335. dev[0].request("DISCONNECT")
  336. dev[0].wait_disconnected(timeout=5)
  337. ifname2 = ifname + '-' + str(i)
  338. hostapd.remove_bss(apdev[0], ifname2)
  339. def test_ap_bss_add_reuse_existing(dev, apdev):
  340. """Dynamic BSS add operation reusing existing interface"""
  341. ifname1 = apdev[0]['ifname']
  342. ifname2 = apdev[0]['ifname'] + '-2'
  343. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  344. subprocess.check_call(["iw", "dev", ifname1, "interface", "add", ifname2,
  345. "type", "__ap"])
  346. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  347. hostapd.remove_bss(apdev[0], ifname2)
  348. subprocess.check_call(["iw", "dev", ifname2, "del"])
  349. def hapd_bss_out_of_mem(hapd, phy, confname, count, func):
  350. with alloc_fail(hapd, count, func):
  351. hapd_global = hostapd.HostapdGlobal()
  352. res = hapd_global.ctrl.request("ADD bss_config=" + phy + ":" + confname)
  353. if "OK" in res:
  354. raise Exception("add_bss succeeded")
  355. def test_ap_bss_add_out_of_memory(dev, apdev):
  356. """Running out of memory while adding a BSS"""
  357. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  358. ifname1 = apdev[0]['ifname']
  359. ifname2 = apdev[0]['ifname'] + '-2'
  360. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf', 1, 'hostapd_add_iface')
  361. for i in range(1, 3):
  362. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
  363. i, 'hostapd_interface_init_bss')
  364. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
  365. 1, 'ieee802_11_build_ap_params')
  366. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  367. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
  368. 1, 'hostapd_interface_init_bss')
  369. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
  370. 1, 'ieee802_11_build_ap_params')
  371. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  372. hostapd.remove_bss(apdev[0], ifname2)
  373. hostapd.remove_bss(apdev[0], ifname1)
  374. def test_ap_multi_bss(dev, apdev):
  375. """Multiple BSSes with hostapd"""
  376. ifname1 = apdev[0]['ifname']
  377. ifname2 = apdev[0]['ifname'] + '-2'
  378. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  379. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  380. dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
  381. dev[1].connect("bss-2", key_mgmt="NONE", scan_freq="2412")
  382. hapd1 = hostapd.Hostapd(ifname1)
  383. hapd2 = hostapd.Hostapd(ifname2)
  384. hwsim_utils.test_connectivity(dev[0], hapd1)
  385. hwsim_utils.test_connectivity(dev[1], hapd2)
  386. sta0 = hapd1.get_sta(dev[0].own_addr())
  387. sta1 = hapd2.get_sta(dev[1].own_addr())
  388. if 'rx_packets' not in sta0 or int(sta0['rx_packets']) < 1:
  389. raise Exception("sta0 did not report receiving packets")
  390. if 'rx_packets' not in sta1 or int(sta1['rx_packets']) < 1:
  391. raise Exception("sta1 did not report receiving packets")
  392. def test_ap_add_with_driver(dev, apdev):
  393. """Add hostapd interface with driver specified"""
  394. ifname = apdev[0]['ifname']
  395. try:
  396. hostname = apdev[0]['hostname']
  397. except:
  398. hostname = None
  399. hapd_global = hostapd.HostapdGlobal(apdev[0])
  400. hapd_global.add(ifname, driver="nl80211")
  401. port = hapd_global.get_ctrl_iface_port(ifname)
  402. hapd = hostapd.Hostapd(ifname, hostname, port)
  403. hapd.set_defaults()
  404. hapd.set("ssid", "dynamic")
  405. hapd.enable()
  406. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  407. if ev is None:
  408. raise Exception("AP startup timed out")
  409. dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  410. dev[0].request("DISCONNECT")
  411. dev[0].wait_disconnected()
  412. hapd.disable()