test_ap_dynamic.py 16 KB

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