test_ap_dynamic.py 18 KB

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