test_ap_dynamic.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
  165. hapd.enable()
  166. multi_check(dev, [ True, True, True ])
  167. hostapd.remove_bss(apdev[0], ifname2)
  168. multi_check(dev, [ True, False, True ])
  169. hostapd.remove_bss(apdev[0], ifname3)
  170. multi_check(dev, [ True, False, False ])
  171. hostapd.remove_bss(apdev[0], ifname1)
  172. multi_check(dev, [ False, False, False ])
  173. hapd = hostapd.add_iface(apdev[0], 'multi-bss.conf')
  174. hapd.enable()
  175. hostapd.remove_bss(apdev[0], ifname1)
  176. multi_check(dev, [ False, False, False ])
  177. def invalid_ap(ap):
  178. logger.info("Trying to start AP " + ap['ifname'] + " with invalid configuration")
  179. hapd = hostapd.add_ap(ap, {}, no_enable=True)
  180. hapd.set("ssid", "invalid-config")
  181. hapd.set("channel", "12345")
  182. try:
  183. hapd.enable()
  184. started = True
  185. except Exception, e:
  186. started = False
  187. if started:
  188. raise Exception("ENABLE command succeeded unexpectedly")
  189. return hapd
  190. def test_ap_invalid_config(dev, apdev):
  191. """Try to start AP with invalid configuration and fix configuration"""
  192. hapd = invalid_ap(apdev[0])
  193. logger.info("Fix configuration and start AP again")
  194. hapd.set("channel", "1")
  195. hapd.enable()
  196. dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
  197. def test_ap_invalid_config2(dev, apdev):
  198. """Try to start AP with invalid configuration and remove interface"""
  199. hapd = invalid_ap(apdev[0])
  200. logger.info("Remove interface with failed configuration")
  201. hostapd.remove_bss(apdev[0])
  202. def test_ap_remove_during_acs(dev, apdev):
  203. """Remove interface during ACS"""
  204. force_prev_ap_on_24g(apdev[0])
  205. params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
  206. params['channel'] = '0'
  207. hostapd.add_ap(apdev[0], params)
  208. hostapd.remove_bss(apdev[0])
  209. def test_ap_remove_during_acs2(dev, apdev):
  210. """Remove BSS during ACS in multi-BSS configuration"""
  211. force_prev_ap_on_24g(apdev[0])
  212. ifname = apdev[0]['ifname']
  213. ifname2 = ifname + "-2"
  214. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  215. hapd.set("ssid", "test-acs-remove")
  216. hapd.set("channel", "0")
  217. hapd.set("bss", ifname2)
  218. hapd.set("ssid", "test-acs-remove2")
  219. hapd.enable()
  220. hostapd.remove_bss(apdev[0])
  221. def test_ap_remove_during_acs3(dev, apdev):
  222. """Remove second BSS during ACS in multi-BSS configuration"""
  223. force_prev_ap_on_24g(apdev[0])
  224. ifname = apdev[0]['ifname']
  225. ifname2 = ifname + "-2"
  226. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  227. hapd.set("ssid", "test-acs-remove")
  228. hapd.set("channel", "0")
  229. hapd.set("bss", ifname2)
  230. hapd.set("ssid", "test-acs-remove2")
  231. hapd.enable()
  232. hostapd.remove_bss(apdev[0], ifname2)
  233. def test_ap_remove_during_ht_coex_scan(dev, apdev):
  234. """Remove interface during HT co-ex scan"""
  235. params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
  236. params['channel'] = '1'
  237. params['ht_capab'] = "[HT40+]"
  238. ifname = apdev[0]['ifname']
  239. hostapd.add_ap(apdev[0], params)
  240. hostapd.remove_bss(apdev[0])
  241. def test_ap_remove_during_ht_coex_scan2(dev, apdev):
  242. """Remove BSS during HT co-ex scan in multi-BSS configuration"""
  243. ifname = apdev[0]['ifname']
  244. ifname2 = ifname + "-2"
  245. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  246. hapd.set("ssid", "test-ht-remove")
  247. hapd.set("channel", "1")
  248. hapd.set("ht_capab", "[HT40+]")
  249. hapd.set("bss", ifname2)
  250. hapd.set("ssid", "test-ht-remove2")
  251. hapd.enable()
  252. hostapd.remove_bss(apdev[0])
  253. def test_ap_remove_during_ht_coex_scan3(dev, apdev):
  254. """Remove second BSS during HT co-ex scan in multi-BSS configuration"""
  255. ifname = apdev[0]['ifname']
  256. ifname2 = ifname + "-2"
  257. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  258. hapd.set("ssid", "test-ht-remove")
  259. hapd.set("channel", "1")
  260. hapd.set("ht_capab", "[HT40+]")
  261. hapd.set("bss", ifname2)
  262. hapd.set("ssid", "test-ht-remove2")
  263. hapd.enable()
  264. hostapd.remove_bss(apdev[0], ifname2)
  265. def test_ap_enable_disable_reenable(dev, apdev):
  266. """Enable, disable, re-enable AP"""
  267. hapd = hostapd.add_ap(apdev[0], {}, no_enable=True)
  268. hapd.set("ssid", "dynamic")
  269. hapd.enable()
  270. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  271. if ev is None:
  272. raise Exception("AP startup timed out")
  273. dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  274. hapd.disable()
  275. ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
  276. if ev is None:
  277. raise Exception("AP disabling timed out")
  278. dev[0].wait_disconnected(timeout=10)
  279. hapd.enable()
  280. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  281. if ev is None:
  282. raise Exception("AP startup timed out")
  283. dev[1].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  284. dev[0].wait_connected(timeout=10)
  285. def test_ap_double_disable(dev, apdev):
  286. """Double DISABLE regression test"""
  287. hostapd.add_bss(apdev[0], apdev[0]['ifname'], 'bss-1.conf')
  288. hostapd.add_bss(apdev[0], apdev[0]['ifname'] + '-2', 'bss-2.conf')
  289. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  290. hapd.disable()
  291. if "FAIL" not in hapd.request("DISABLE"):
  292. raise Exception("Second DISABLE accepted unexpectedly")
  293. hapd.enable()
  294. hapd.disable()
  295. if "FAIL" not in hapd.request("DISABLE"):
  296. raise Exception("Second DISABLE accepted unexpectedly")
  297. def test_ap_bss_add_many(dev, apdev):
  298. """Large number of BSS add operations with hostapd"""
  299. try:
  300. _test_ap_bss_add_many(dev, apdev)
  301. finally:
  302. dev[0].request("SCAN_INTERVAL 5")
  303. ifname = apdev[0]['ifname']
  304. hapd = hostapd.HostapdGlobal(apdev[0])
  305. hapd.flush()
  306. for i in range(16):
  307. ifname2 = ifname + '-' + str(i)
  308. hapd.remove(ifname2)
  309. try:
  310. os.remove('/tmp/hwsim-bss.conf')
  311. except:
  312. pass
  313. def _test_ap_bss_add_many(dev, apdev):
  314. ifname = apdev[0]['ifname']
  315. hostapd.add_bss(apdev[0], ifname, 'bss-1.conf')
  316. fname = '/tmp/hwsim-bss.conf'
  317. for i in range(16):
  318. ifname2 = ifname + '-' + str(i)
  319. with open(fname, 'w') as f:
  320. f.write("driver=nl80211\n")
  321. f.write("hw_mode=g\n")
  322. f.write("channel=1\n")
  323. f.write("ieee80211n=1\n")
  324. f.write("interface=%s\n" % ifname2)
  325. f.write("bssid=02:00:00:00:03:%02x\n" % (i + 1))
  326. f.write("ctrl_interface=/var/run/hostapd\n")
  327. f.write("ssid=test-%d\n" % i)
  328. hostapd.add_bss(apdev[0], ifname2, fname)
  329. os.remove(fname)
  330. dev[0].request("SCAN_INTERVAL 1")
  331. dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
  332. dev[0].request("DISCONNECT")
  333. dev[0].wait_disconnected(timeout=5)
  334. for i in range(16):
  335. dev[0].connect("test-%d" % i, key_mgmt="NONE", scan_freq="2412")
  336. dev[0].request("DISCONNECT")
  337. dev[0].wait_disconnected(timeout=5)
  338. ifname2 = ifname + '-' + str(i)
  339. hostapd.remove_bss(apdev[0], ifname2)
  340. def test_ap_bss_add_reuse_existing(dev, apdev):
  341. """Dynamic BSS add operation reusing existing interface"""
  342. ifname1 = apdev[0]['ifname']
  343. ifname2 = apdev[0]['ifname'] + '-2'
  344. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  345. subprocess.check_call(["iw", "dev", ifname1, "interface", "add", ifname2,
  346. "type", "__ap"])
  347. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  348. hostapd.remove_bss(apdev[0], ifname2)
  349. subprocess.check_call(["iw", "dev", ifname2, "del"])
  350. def hapd_bss_out_of_mem(hapd, phy, confname, count, func):
  351. with alloc_fail(hapd, count, func):
  352. hapd_global = hostapd.HostapdGlobal()
  353. res = hapd_global.ctrl.request("ADD bss_config=" + phy + ":" + confname)
  354. if "OK" in res:
  355. raise Exception("add_bss succeeded")
  356. def test_ap_bss_add_out_of_memory(dev, apdev):
  357. """Running out of memory while adding a BSS"""
  358. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  359. ifname1 = apdev[0]['ifname']
  360. ifname2 = apdev[0]['ifname'] + '-2'
  361. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf', 1, 'hostapd_add_iface')
  362. for i in range(1, 3):
  363. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
  364. i, 'hostapd_interface_init_bss')
  365. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-1.conf',
  366. 1, 'ieee802_11_build_ap_params')
  367. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  368. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
  369. 1, 'hostapd_interface_init_bss')
  370. hapd_bss_out_of_mem(hapd2, 'phy3', 'bss-2.conf',
  371. 1, 'ieee802_11_build_ap_params')
  372. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  373. hostapd.remove_bss(apdev[0], ifname2)
  374. hostapd.remove_bss(apdev[0], ifname1)
  375. def test_ap_multi_bss(dev, apdev):
  376. """Multiple BSSes with hostapd"""
  377. ifname1 = apdev[0]['ifname']
  378. ifname2 = apdev[0]['ifname'] + '-2'
  379. hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
  380. hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')
  381. dev[0].connect("bss-1", key_mgmt="NONE", scan_freq="2412")
  382. dev[1].connect("bss-2", key_mgmt="NONE", scan_freq="2412")
  383. hapd1 = hostapd.Hostapd(ifname1)
  384. hapd2 = hostapd.Hostapd(ifname2)
  385. hwsim_utils.test_connectivity(dev[0], hapd1)
  386. hwsim_utils.test_connectivity(dev[1], hapd2)
  387. sta0 = hapd1.get_sta(dev[0].own_addr())
  388. sta1 = hapd2.get_sta(dev[1].own_addr())
  389. if 'rx_packets' not in sta0 or int(sta0['rx_packets']) < 1:
  390. raise Exception("sta0 did not report receiving packets")
  391. if 'rx_packets' not in sta1 or int(sta1['rx_packets']) < 1:
  392. raise Exception("sta1 did not report receiving packets")
  393. def test_ap_add_with_driver(dev, apdev):
  394. """Add hostapd interface with driver specified"""
  395. ifname = apdev[0]['ifname']
  396. try:
  397. hostname = apdev[0]['hostname']
  398. except:
  399. hostname = None
  400. hapd_global = hostapd.HostapdGlobal(apdev[0])
  401. hapd_global.add(ifname, driver="nl80211")
  402. port = hapd_global.get_ctrl_iface_port(ifname)
  403. hapd = hostapd.Hostapd(ifname, hostname, port)
  404. hapd.set_defaults()
  405. hapd.set("ssid", "dynamic")
  406. hapd.enable()
  407. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  408. if ev is None:
  409. raise Exception("AP startup timed out")
  410. dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  411. dev[0].request("DISCONNECT")
  412. dev[0].wait_disconnected()
  413. hapd.disable()