test_ap_dynamic.py 15 KB

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