test_ap_dynamic.py 17 KB

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