test_ap_dynamic.py 14 KB

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