test_ap_dynamic.py 14 KB

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