test_ap_dynamic.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 hwsim_utils
  11. import hostapd
  12. def test_ap_change_ssid(dev, apdev):
  13. """Dynamic SSID change with hostapd and WPA2-PSK"""
  14. params = hostapd.wpa2_params(ssid="test-wpa2-psk-start",
  15. passphrase="12345678")
  16. hostapd.add_ap(apdev[0]['ifname'], params)
  17. id = dev[0].connect("test-wpa2-psk-start", psk="12345678",
  18. scan_freq="2412")
  19. dev[0].request("DISCONNECT")
  20. logger.info("Change SSID dynamically")
  21. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  22. res = hapd.request("SET ssid test-wpa2-psk-new")
  23. if "OK" not in res:
  24. raise Exception("SET command failed")
  25. res = hapd.request("RELOAD")
  26. if "OK" not in res:
  27. raise Exception("RELOAD command failed")
  28. dev[0].set_network_quoted(id, "ssid", "test-wpa2-psk-new")
  29. dev[0].connect_network(id)
  30. def multi_check(dev, check):
  31. id = []
  32. num_bss = len(check)
  33. for i in range(0, num_bss):
  34. dev[i].request("BSS_FLUSH 0")
  35. dev[i].dump_monitor()
  36. id.append(dev[i].connect("bss-" + str(i + 1), key_mgmt="NONE",
  37. scan_freq="2412", wait_connect=check[i]))
  38. for i in range(0, num_bss):
  39. if not check[i]:
  40. ev = dev[i].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.2)
  41. if ev:
  42. raise Exception("Unexpected connection")
  43. for i in range(0, num_bss):
  44. dev[i].remove_network(id[i])
  45. time.sleep(0.3)
  46. res = ''
  47. for i in range(0, num_bss):
  48. res = res + dev[i].request("BSS RANGE=ALL MASK=0x2")
  49. for i in range(0, num_bss):
  50. if not check[i]:
  51. bssid = '02:00:00:00:03:0' + str(i)
  52. if bssid in res:
  53. raise Exception("Unexpected BSS" + str(i) + " in scan results")
  54. def test_ap_bss_add_remove(dev, apdev):
  55. """Dynamic BSS add/remove operations with hostapd"""
  56. ifname1 = apdev[0]['ifname']
  57. ifname2 = apdev[0]['ifname'] + '-2'
  58. ifname3 = apdev[0]['ifname'] + '-3'
  59. logger.info("Set up three BSSes one by one")
  60. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  61. multi_check(dev, [ True, False, False ])
  62. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  63. multi_check(dev, [ True, True, False ])
  64. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  65. multi_check(dev, [ True, True, True ])
  66. logger.info("Remove the last BSS and re-add it")
  67. hostapd.remove_bss(ifname3)
  68. multi_check(dev, [ True, True, False ])
  69. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  70. multi_check(dev, [ True, True, True ])
  71. logger.info("Remove the middle BSS and re-add it")
  72. hostapd.remove_bss(ifname2)
  73. multi_check(dev, [ True, False, True ])
  74. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  75. multi_check(dev, [ True, True, True ])
  76. logger.info("Remove the first BSS and re-add it and other BSSs")
  77. hostapd.remove_bss(ifname1)
  78. multi_check(dev, [ False, False, False ])
  79. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  80. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  81. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  82. multi_check(dev, [ True, True, True ])
  83. logger.info("Remove two BSSes and re-add them")
  84. hostapd.remove_bss(ifname2)
  85. multi_check(dev, [ True, False, True ])
  86. hostapd.remove_bss(ifname3)
  87. multi_check(dev, [ True, False, False ])
  88. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  89. multi_check(dev, [ True, True, False ])
  90. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  91. multi_check(dev, [ True, True, True ])
  92. logger.info("Remove three BSSes in and re-add them")
  93. hostapd.remove_bss(ifname3)
  94. multi_check(dev, [ True, True, False ])
  95. hostapd.remove_bss(ifname2)
  96. multi_check(dev, [ True, False, False ])
  97. hostapd.remove_bss(ifname1)
  98. multi_check(dev, [ False, False, False ])
  99. hostapd.add_bss('phy3', ifname1, 'bss-1.conf')
  100. multi_check(dev, [ True, False, False ])
  101. hostapd.add_bss('phy3', ifname2, 'bss-2.conf')
  102. multi_check(dev, [ True, True, False ])
  103. hostapd.add_bss('phy3', ifname3, 'bss-3.conf')
  104. multi_check(dev, [ True, True, True ])
  105. logger.info("Test error handling if a duplicate ifname is tried")
  106. hostapd.add_bss('phy3', ifname3, 'bss-3.conf', ignore_error=True)
  107. multi_check(dev, [ True, True, True ])
  108. def test_ap_bss_add_remove_during_ht_scan(dev, apdev):
  109. """Dynamic BSS add during HT40 co-ex scan"""
  110. ifname1 = apdev[0]['ifname']
  111. ifname2 = apdev[0]['ifname'] + '-2'
  112. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  113. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  114. multi_check(dev, [ True, True ])
  115. hostapd.remove_bss(ifname2)
  116. hostapd.remove_bss(ifname1)
  117. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  118. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  119. hostapd.remove_bss(ifname2)
  120. multi_check(dev, [ True, False ])
  121. hostapd.remove_bss(ifname1)
  122. hostapd.add_bss('phy3', ifname1, 'bss-ht40-1.conf')
  123. hostapd.add_bss('phy3', ifname2, 'bss-ht40-2.conf')
  124. hostapd.remove_bss(ifname1)
  125. multi_check(dev, [ False, False ])
  126. def test_ap_multi_bss_config(dev, apdev):
  127. """hostapd start with a multi-BSS configuration file"""
  128. ifname1 = apdev[0]['ifname']
  129. ifname2 = apdev[0]['ifname'] + '-2'
  130. ifname3 = apdev[0]['ifname'] + '-3'
  131. logger.info("Set up three BSSes with one configuration file")
  132. hostapd.add_iface(ifname1, 'multi-bss.conf')
  133. hapd = hostapd.Hostapd(ifname1)
  134. hapd.enable()
  135. multi_check(dev, [ True, True, True ])
  136. hostapd.remove_bss(ifname2)
  137. multi_check(dev, [ True, False, True ])
  138. hostapd.remove_bss(ifname3)
  139. multi_check(dev, [ True, False, False ])
  140. hostapd.remove_bss(ifname1)
  141. multi_check(dev, [ False, False, False ])
  142. hostapd.add_iface(ifname1, 'multi-bss.conf')
  143. hapd = hostapd.Hostapd(ifname1)
  144. hapd.enable()
  145. hostapd.remove_bss(ifname1)
  146. multi_check(dev, [ False, False, False ])
  147. def invalid_ap(hapd_global, ifname):
  148. logger.info("Trying to start AP " + ifname + " with invalid configuration")
  149. hapd_global.remove(ifname)
  150. hapd_global.add(ifname)
  151. hapd = hostapd.Hostapd(ifname)
  152. if not hapd.ping():
  153. raise Exception("Could not ping hostapd")
  154. hapd.set_defaults()
  155. hapd.set("ssid", "invalid-config")
  156. hapd.set("channel", "12345")
  157. try:
  158. hapd.enable()
  159. started = True
  160. except Exception, e:
  161. started = False
  162. if started:
  163. raise Exception("ENABLE command succeeded unexpectedly")
  164. return hapd
  165. def test_ap_invalid_config(dev, apdev):
  166. """Try to start AP with invalid configuration and fix configuration"""
  167. hapd_global = hostapd.HostapdGlobal()
  168. ifname = apdev[0]['ifname']
  169. hapd = invalid_ap(hapd_global, ifname)
  170. logger.info("Fix configuration and start AP again")
  171. hapd.set("channel", "1")
  172. hapd.enable()
  173. dev[0].connect("invalid-config", key_mgmt="NONE", scan_freq="2412")
  174. def test_ap_invalid_config2(dev, apdev):
  175. """Try to start AP with invalid configuration and remove interface"""
  176. hapd_global = hostapd.HostapdGlobal()
  177. ifname = apdev[0]['ifname']
  178. hapd = invalid_ap(hapd_global, ifname)
  179. logger.info("Remove interface with failed configuration")
  180. hapd_global.remove(ifname)
  181. def test_ap_remove_during_acs(dev, apdev):
  182. """Remove interface during ACS"""
  183. params = hostapd.wpa2_params(ssid="test-acs-remove", passphrase="12345678")
  184. params['channel'] = '0'
  185. ifname = apdev[0]['ifname']
  186. hapd = hostapd.HostapdGlobal()
  187. hostapd.add_ap(ifname, params)
  188. hapd.remove(ifname)
  189. def test_ap_remove_during_acs2(dev, apdev):
  190. """Remove BSS during ACS in multi-BSS configuration"""
  191. ifname = apdev[0]['ifname']
  192. ifname2 = ifname + "-2"
  193. hapd_global = hostapd.HostapdGlobal()
  194. hapd_global.add(ifname)
  195. hapd = hostapd.Hostapd(ifname)
  196. hapd.set_defaults()
  197. hapd.set("ssid", "test-acs-remove")
  198. hapd.set("channel", "0")
  199. hapd.set("bss", ifname2)
  200. hapd.set("ssid", "test-acs-remove2")
  201. hapd.enable()
  202. hapd_global.remove(ifname)
  203. def test_ap_remove_during_acs3(dev, apdev):
  204. """Remove second BSS during ACS in multi-BSS configuration"""
  205. ifname = apdev[0]['ifname']
  206. ifname2 = ifname + "-2"
  207. hapd_global = hostapd.HostapdGlobal()
  208. hapd_global.add(ifname)
  209. hapd = hostapd.Hostapd(ifname)
  210. hapd.set_defaults()
  211. hapd.set("ssid", "test-acs-remove")
  212. hapd.set("channel", "0")
  213. hapd.set("bss", ifname2)
  214. hapd.set("ssid", "test-acs-remove2")
  215. hapd.enable()
  216. hapd_global.remove(ifname2)
  217. def test_ap_remove_during_ht_coex_scan(dev, apdev):
  218. """Remove interface during HT co-ex scan"""
  219. params = hostapd.wpa2_params(ssid="test-ht-remove", passphrase="12345678")
  220. params['channel'] = '1'
  221. params['ht_capab'] = "[HT40+]"
  222. ifname = apdev[0]['ifname']
  223. hapd = hostapd.HostapdGlobal()
  224. hostapd.add_ap(ifname, params)
  225. hapd.remove(ifname)
  226. def test_ap_remove_during_ht_coex_scan2(dev, apdev):
  227. """Remove BSS during HT co-ex scan in multi-BSS configuration"""
  228. ifname = apdev[0]['ifname']
  229. ifname2 = ifname + "-2"
  230. hapd_global = hostapd.HostapdGlobal()
  231. hapd_global.add(ifname)
  232. hapd = hostapd.Hostapd(ifname)
  233. hapd.set_defaults()
  234. hapd.set("ssid", "test-ht-remove")
  235. hapd.set("channel", "1")
  236. hapd.set("ht_capab", "[HT40+]")
  237. hapd.set("bss", ifname2)
  238. hapd.set("ssid", "test-ht-remove2")
  239. hapd.enable()
  240. hapd_global.remove(ifname)
  241. def test_ap_remove_during_ht_coex_scan3(dev, apdev):
  242. """Remove second 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(ifname2)
  256. def test_ap_enable_disable_reenable(dev, apdev):
  257. """Enable, disable, re-enable AP"""
  258. ifname = apdev[0]['ifname']
  259. hapd_global = hostapd.HostapdGlobal()
  260. hapd_global.add(ifname)
  261. hapd = hostapd.Hostapd(ifname)
  262. hapd.set_defaults()
  263. hapd.set("ssid", "dynamic")
  264. hapd.enable()
  265. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  266. if ev is None:
  267. raise Exception("AP startup timed out")
  268. dev[0].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  269. hapd.disable()
  270. ev = hapd.wait_event(["AP-DISABLED"], timeout=30)
  271. if ev is None:
  272. raise Exception("AP disabling timed out")
  273. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  274. if ev is None:
  275. raise Exception("STA disconnect event timed out")
  276. hapd.enable()
  277. ev = hapd.wait_event(["AP-ENABLED"], timeout=30)
  278. if ev is None:
  279. raise Exception("AP startup timed out")
  280. dev[1].connect("dynamic", key_mgmt="NONE", scan_freq="2412")
  281. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"])
  282. if ev is None:
  283. raise Exception("STA connect event timed out")
  284. def test_ap_double_disable(dev, apdev):
  285. """Double DISABLE regression test"""
  286. hostapd.add_bss('phy3', apdev[0]['ifname'], 'bss-1.conf')
  287. hostapd.add_bss('phy3', 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")