test_wnm.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. # WNM tests
  2. # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import binascii
  7. import struct
  8. import time
  9. import logging
  10. logger = logging.getLogger()
  11. import subprocess
  12. import hostapd
  13. from wlantest import Wlantest
  14. def test_wnm_bss_transition_mgmt(dev, apdev):
  15. """WNM BSS Transition Management"""
  16. params = { "ssid": "test-wnm",
  17. "time_advertisement": "2",
  18. "time_zone": "EST5",
  19. "wnm_sleep_mode": "1",
  20. "bss_transition": "1" }
  21. hostapd.add_ap(apdev[0]['ifname'], params)
  22. dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  23. dev[0].request("WNM_BSS_QUERY 0")
  24. def test_wnm_disassoc_imminent(dev, apdev):
  25. """WNM Disassociation Imminent"""
  26. params = { "ssid": "test-wnm",
  27. "time_advertisement": "2",
  28. "time_zone": "EST5",
  29. "wnm_sleep_mode": "1",
  30. "bss_transition": "1" }
  31. hostapd.add_ap(apdev[0]['ifname'], params)
  32. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  33. dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  34. addr = dev[0].p2p_interface_addr()
  35. hapd.request("DISASSOC_IMMINENT " + addr + " 10")
  36. ev = dev[0].wait_event(["WNM: Disassociation Imminent"])
  37. if ev is None:
  38. raise Exception("Timeout while waiting for disassociation imminent")
  39. if "Disassociation Timer 10" not in ev:
  40. raise Exception("Unexpected disassociation imminent contents")
  41. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  42. if ev is None:
  43. raise Exception("Timeout while waiting for re-connection scan")
  44. def test_wnm_ess_disassoc_imminent(dev, apdev):
  45. """WNM ESS Disassociation Imminent"""
  46. params = { "ssid": "test-wnm",
  47. "time_advertisement": "2",
  48. "time_zone": "EST5",
  49. "wnm_sleep_mode": "1",
  50. "bss_transition": "1" }
  51. hostapd.add_ap(apdev[0]['ifname'], params)
  52. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  53. dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  54. addr = dev[0].p2p_interface_addr()
  55. hapd.request("ESS_DISASSOC " + addr + " 10 http://example.com/session-info")
  56. ev = dev[0].wait_event(["ESS-DISASSOC-IMMINENT"])
  57. if ev is None:
  58. raise Exception("Timeout while waiting for ESS disassociation imminent")
  59. if "0 1024 http://example.com/session-info" not in ev:
  60. raise Exception("Unexpected ESS disassociation imminent message contents")
  61. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  62. if ev is None:
  63. raise Exception("Timeout while waiting for re-connection scan")
  64. def test_wnm_ess_disassoc_imminent_pmf(dev, apdev):
  65. """WNM ESS Disassociation Imminent"""
  66. params = hostapd.wpa2_params("test-wnm-rsn", "12345678")
  67. params["wpa_key_mgmt"] = "WPA-PSK-SHA256";
  68. params["ieee80211w"] = "2";
  69. params["bss_transition"] = "1"
  70. hostapd.add_ap(apdev[0]['ifname'], params)
  71. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  72. dev[0].connect("test-wnm-rsn", psk="12345678", ieee80211w="2",
  73. key_mgmt="WPA-PSK-SHA256", proto="WPA2", scan_freq="2412")
  74. addr = dev[0].p2p_interface_addr()
  75. hapd.request("ESS_DISASSOC " + addr + " 10 http://example.com/session-info")
  76. ev = dev[0].wait_event(["ESS-DISASSOC-IMMINENT"])
  77. if ev is None:
  78. raise Exception("Timeout while waiting for ESS disassociation imminent")
  79. if "1 1024 http://example.com/session-info" not in ev:
  80. raise Exception("Unexpected ESS disassociation imminent message contents")
  81. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  82. if ev is None:
  83. raise Exception("Timeout while waiting for re-connection scan")
  84. def check_wnm_sleep_mode_enter_exit(hapd, dev, interval=None, tfs_req=None):
  85. addr = dev.p2p_interface_addr()
  86. sta = hapd.get_sta(addr)
  87. if "[WNM_SLEEP_MODE]" in sta['flags']:
  88. raise Exception("Station unexpectedly in WNM-Sleep Mode")
  89. logger.info("Going to WNM Sleep Mode")
  90. extra = ""
  91. if interval is not None:
  92. extra += " interval=" + str(interval)
  93. if tfs_req:
  94. extra += " tfs_req=" + tfs_req
  95. if "OK" not in dev.request("WNM_SLEEP enter" + extra):
  96. raise Exception("WNM_SLEEP failed")
  97. time.sleep(0.5)
  98. sta = hapd.get_sta(addr)
  99. if "[WNM_SLEEP_MODE]" not in sta['flags']:
  100. raise Exception("Station failed to enter WNM-Sleep Mode")
  101. logger.info("Waking up from WNM Sleep Mode")
  102. dev.request("WNM_SLEEP exit")
  103. time.sleep(0.5)
  104. sta = hapd.get_sta(addr)
  105. if "[WNM_SLEEP_MODE]" in sta['flags']:
  106. raise Exception("Station failed to exit WNM-Sleep Mode")
  107. def test_wnm_sleep_mode_open(dev, apdev):
  108. """WNM Sleep Mode - open"""
  109. params = { "ssid": "test-wnm",
  110. "time_advertisement": "2",
  111. "time_zone": "EST5",
  112. "wnm_sleep_mode": "1",
  113. "bss_transition": "1" }
  114. hostapd.add_ap(apdev[0]['ifname'], params)
  115. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  116. dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  117. check_wnm_sleep_mode_enter_exit(hapd, dev[0])
  118. check_wnm_sleep_mode_enter_exit(hapd, dev[0], interval=100)
  119. check_wnm_sleep_mode_enter_exit(hapd, dev[0], tfs_req="5b17010001130e110000071122334455661122334455661234")
  120. cmds = [ "foo",
  121. "exit tfs_req=123 interval=10",
  122. "enter tfs_req=qq interval=10" ]
  123. for cmd in cmds:
  124. if "FAIL" not in dev[0].request("WNM_SLEEP " + cmd):
  125. raise Exception("Invalid WNM_SLEEP accepted")
  126. def test_wnm_sleep_mode_rsn(dev, apdev):
  127. """WNM Sleep Mode - RSN"""
  128. params = hostapd.wpa2_params("test-wnm-rsn", "12345678")
  129. params["time_advertisement"] = "2"
  130. params["time_zone"] = "EST5"
  131. params["wnm_sleep_mode"] = "1"
  132. params["bss_transition"] = "1"
  133. hostapd.add_ap(apdev[0]['ifname'], params)
  134. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  135. dev[0].connect("test-wnm-rsn", psk="12345678", scan_freq="2412")
  136. check_wnm_sleep_mode_enter_exit(hapd, dev[0])
  137. def test_wnm_sleep_mode_rsn_pmf(dev, apdev):
  138. """WNM Sleep Mode - RSN with PMF"""
  139. wt = Wlantest()
  140. wt.flush()
  141. wt.add_passphrase("12345678")
  142. params = hostapd.wpa2_params("test-wnm-rsn", "12345678")
  143. params["wpa_key_mgmt"] = "WPA-PSK-SHA256";
  144. params["ieee80211w"] = "2";
  145. params["time_advertisement"] = "2"
  146. params["time_zone"] = "EST5"
  147. params["wnm_sleep_mode"] = "1"
  148. params["bss_transition"] = "1"
  149. hostapd.add_ap(apdev[0]['ifname'], params)
  150. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  151. dev[0].connect("test-wnm-rsn", psk="12345678", ieee80211w="2",
  152. key_mgmt="WPA-PSK-SHA256", proto="WPA2", scan_freq="2412")
  153. check_wnm_sleep_mode_enter_exit(hapd, dev[0])
  154. MGMT_SUBTYPE_ACTION = 13
  155. ACTION_CATEG_WNM = 10
  156. WNM_ACT_BSS_TM_REQ = 7
  157. WNM_ACT_BSS_TM_RESP = 8
  158. def bss_tm_req(dst, src, dialog_token=1, req_mode=0, disassoc_timer=0,
  159. validity_interval=1):
  160. msg = {}
  161. msg['fc'] = MGMT_SUBTYPE_ACTION << 4
  162. msg['da'] = dst
  163. msg['sa'] = src
  164. msg['bssid'] = src
  165. msg['payload'] = struct.pack("<BBBBHB",
  166. ACTION_CATEG_WNM, WNM_ACT_BSS_TM_REQ,
  167. dialog_token, req_mode, disassoc_timer,
  168. validity_interval)
  169. return msg
  170. def rx_bss_tm_resp(hapd, expect_dialog=None, expect_status=None):
  171. for i in range(0, 100):
  172. resp = hapd.mgmt_rx()
  173. if resp is None:
  174. raise Exception("No BSS TM Response received")
  175. if resp['subtype'] == MGMT_SUBTYPE_ACTION:
  176. break
  177. if i == 99:
  178. raise Exception("Not an Action frame")
  179. payload = resp['payload']
  180. if len(payload) < 2 + 3:
  181. raise Exception("Too short payload")
  182. (category, action) = struct.unpack('BB', payload[0:2])
  183. if category != ACTION_CATEG_WNM or action != WNM_ACT_BSS_TM_RESP:
  184. raise Exception("Not a BSS TM Response")
  185. pos = payload[2:]
  186. (dialog, status, bss_term_delay) = struct.unpack('BBB', pos[0:3])
  187. resp['dialog'] = dialog
  188. resp['status'] = status
  189. resp['bss_term_delay'] = bss_term_delay
  190. pos = pos[3:]
  191. if len(pos) >= 6 and status == 0:
  192. resp['target_bssid'] = binascii.hexlify(pos[0:6])
  193. pos = pos[6:]
  194. resp['candidates'] = pos
  195. if expect_dialog is not None and dialog != expect_dialog:
  196. raise Exception("Unexpected dialog token")
  197. if expect_status is not None and status != expect_status:
  198. raise Exception("Unexpected status code %d" % status)
  199. return resp
  200. def expect_ack(hapd):
  201. ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
  202. if ev is None:
  203. raise Exception("Missing TX status")
  204. if "ok=1" not in ev:
  205. raise Exception("Action frame not acknowledged")
  206. def test_wnm_bss_tm_req(dev, apdev):
  207. """BSS Transition Management Request"""
  208. params = { "ssid": "test-wnm", "bss_transition": "1" }
  209. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  210. dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  211. hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
  212. hapd.set("ext_mgmt_frame_handling", "1")
  213. # truncated BSS TM Request
  214. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  215. req_mode=0x08)
  216. req['payload'] = struct.pack("<BBBBH",
  217. ACTION_CATEG_WNM, WNM_ACT_BSS_TM_REQ,
  218. 1, 0, 0)
  219. hapd.mgmt_tx(req)
  220. expect_ack(hapd)
  221. # no disassociation and no candidate list
  222. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  223. dialog_token=2)
  224. hapd.mgmt_tx(req)
  225. resp = rx_bss_tm_resp(hapd, expect_dialog=2, expect_status=1)
  226. # truncated BSS Termination Duration
  227. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  228. req_mode=0x08)
  229. hapd.mgmt_tx(req)
  230. expect_ack(hapd)
  231. # BSS Termination Duration with TSF=0 and Duration=10
  232. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  233. req_mode=0x08, dialog_token=3)
  234. req['payload'] += struct.pack("<BBQH", 4, 10, 0, 10)
  235. hapd.mgmt_tx(req)
  236. resp = rx_bss_tm_resp(hapd, expect_dialog=3, expect_status=1)
  237. # truncated Session Information URL
  238. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  239. req_mode=0x10)
  240. hapd.mgmt_tx(req)
  241. expect_ack(hapd)
  242. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  243. req_mode=0x10)
  244. req['payload'] += struct.pack("<BBB", 3, 65, 66)
  245. hapd.mgmt_tx(req)
  246. expect_ack(hapd)
  247. # Session Information URL
  248. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  249. req_mode=0x10, dialog_token=4)
  250. req['payload'] += struct.pack("<BBB", 2, 65, 66)
  251. hapd.mgmt_tx(req)
  252. resp = rx_bss_tm_resp(hapd, expect_dialog=4, expect_status=0)
  253. # Preferred Candidate List without any entries
  254. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  255. req_mode=0x01, dialog_token=5)
  256. hapd.mgmt_tx(req)
  257. resp = rx_bss_tm_resp(hapd, expect_dialog=5, expect_status=7)
  258. # Preferred Candidate List with a truncated entry
  259. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  260. req_mode=0x01)
  261. req['payload'] += struct.pack("<BB", 52, 1)
  262. hapd.mgmt_tx(req)
  263. expect_ack(hapd)
  264. # Preferred Candidate List with a too short entry
  265. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  266. req_mode=0x01, dialog_token=6)
  267. req['payload'] += struct.pack("<BB", 52, 0)
  268. hapd.mgmt_tx(req)
  269. resp = rx_bss_tm_resp(hapd, expect_dialog=6, expect_status=7)
  270. # Preferred Candidate List with a non-matching entry
  271. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  272. req_mode=0x01, dialog_token=6)
  273. req['payload'] += struct.pack("<BB6BLBBB", 52, 13,
  274. 1, 2, 3, 4, 5, 6,
  275. 0, 81, 1, 7)
  276. hapd.mgmt_tx(req)
  277. resp = rx_bss_tm_resp(hapd, expect_dialog=6, expect_status=7)
  278. # Preferred Candidate List with a truncated subelement
  279. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  280. req_mode=0x01, dialog_token=7)
  281. req['payload'] += struct.pack("<BB6BLBBBBB", 52, 13 + 2,
  282. 1, 2, 3, 4, 5, 6,
  283. 0, 81, 1, 7,
  284. 1, 1)
  285. hapd.mgmt_tx(req)
  286. resp = rx_bss_tm_resp(hapd, expect_dialog=7, expect_status=7)
  287. # Preferred Candidate List with lots of invalid optional subelements
  288. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  289. req_mode=0x01, dialog_token=8)
  290. subelems = struct.pack("<BBHB", 1, 3, 0, 100)
  291. subelems += struct.pack("<BBB", 2, 1, 65)
  292. subelems += struct.pack("<BB", 3, 0)
  293. subelems += struct.pack("<BBQB", 4, 9, 0, 10)
  294. subelems += struct.pack("<BBHLB", 5, 7, 0, 0, 0)
  295. subelems += struct.pack("<BB", 66, 0)
  296. subelems += struct.pack("<BBBBBB", 70, 4, 0, 0, 0, 0)
  297. subelems += struct.pack("<BB", 71, 0)
  298. req['payload'] += struct.pack("<BB6BLBBB", 52, 13 + len(subelems),
  299. 1, 2, 3, 4, 5, 6,
  300. 0, 81, 1, 7) + subelems
  301. hapd.mgmt_tx(req)
  302. resp = rx_bss_tm_resp(hapd, expect_dialog=8, expect_status=7)
  303. # Preferred Candidate List with lots of valid optional subelements (twice)
  304. req = bss_tm_req(dev[0].p2p_interface_addr(), apdev[0]['bssid'],
  305. req_mode=0x01, dialog_token=8)
  306. # TSF Information
  307. subelems = struct.pack("<BBHH", 1, 4, 0, 100)
  308. # Condensed Country String
  309. subelems += struct.pack("<BBBB", 2, 2, 65, 66)
  310. # BSS Transition Candidate Preference
  311. subelems += struct.pack("<BBB", 3, 1, 100)
  312. # BSS Termination Duration
  313. subelems += struct.pack("<BBQH", 4, 10, 0, 10)
  314. # Bearing
  315. subelems += struct.pack("<BBHLH", 5, 8, 0, 0, 0)
  316. # Measurement Pilot Transmission
  317. subelems += struct.pack("<BBBBB", 66, 3, 0, 0, 0)
  318. # RM Enabled Capabilities
  319. subelems += struct.pack("<BBBBBBB", 70, 5, 0, 0, 0, 0, 0)
  320. # Multiple BSSID
  321. subelems += struct.pack("<BBBB", 71, 2, 0, 0)
  322. req['payload'] += struct.pack("<BB6BLBBB", 52, 13 + len(subelems) * 2,
  323. 1, 2, 3, 4, 5, 6,
  324. 0, 81, 1, 7) + subelems + subelems
  325. hapd.mgmt_tx(req)
  326. resp = rx_bss_tm_resp(hapd, expect_dialog=8, expect_status=7)
  327. def test_wnm_bss_keep_alive(dev, apdev):
  328. """WNM keep-alive"""
  329. params = { "ssid": "test-wnm",
  330. "ap_max_inactivity": "1" }
  331. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  332. addr = dev[0].p2p_interface_addr()
  333. dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  334. start = hapd.get_sta(addr)
  335. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=2)
  336. if ev is not None:
  337. raise Exception("Unexpected disconnection")
  338. end = hapd.get_sta(addr)
  339. if int(end['rx_packets']) <= int(start['rx_packets']):
  340. raise Exception("No keep-alive packets received")
  341. try:
  342. # Disable client keep-alive so that hostapd will verify connection
  343. # with client poll
  344. dev[0].request("SET no_keep_alive 1")
  345. for i in range(60):
  346. sta = hapd.get_sta(addr)
  347. logger.info("timeout_next=%s rx_packets=%s tx_packets=%s" % (sta['timeout_next'], sta['rx_packets'], sta['tx_packets']))
  348. if i > 1 and sta['timeout_next'] != "NULLFUNC POLL" and int(sta['tx_packets']) > int(end['tx_packets']):
  349. break
  350. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.5)
  351. if ev is not None:
  352. raise Exception("Unexpected disconnection (client poll expected)")
  353. finally:
  354. dev[0].request("SET no_keep_alive 0")
  355. if int(sta['tx_packets']) <= int(end['tx_packets']):
  356. raise Exception("No client poll packet seen")
  357. def test_wnm_bss_tm(dev, apdev):
  358. """WNM BSS Transition Management"""
  359. try:
  360. hapd = None
  361. hapd2 = None
  362. params = { "ssid": "test-wnm",
  363. "country_code": "FI",
  364. "ieee80211d": "1",
  365. "hw_mode": "g",
  366. "channel": "1",
  367. "bss_transition": "1" }
  368. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  369. id = dev[0].connect("test-wnm", key_mgmt="NONE", scan_freq="2412")
  370. dev[0].set_network(id, "scan_freq", "")
  371. params = { "ssid": "test-wnm",
  372. "country_code": "FI",
  373. "ieee80211d": "1",
  374. "hw_mode": "a",
  375. "channel": "36",
  376. "bss_transition": "1" }
  377. hapd2 = hostapd.add_ap(apdev[1]['ifname'], params)
  378. addr = dev[0].p2p_interface_addr()
  379. dev[0].dump_monitor()
  380. logger.info("No neighbor list entries")
  381. if "OK" not in hapd.request("BSS_TM_REQ " + addr):
  382. raise Exception("BSS_TM_REQ command failed")
  383. ev = hapd.wait_event(['BSS-TM-RESP'], timeout=10)
  384. if ev is None:
  385. raise Exception("No BSS Transition Management Response")
  386. if addr not in ev:
  387. raise Exception("Unexpected BSS Transition Management Response address")
  388. if "status_code=0" in ev:
  389. raise Exception("BSS transition accepted unexpectedly")
  390. dev[0].dump_monitor()
  391. logger.info("Neighbor list entry, but not claimed as Preferred Candidate List")
  392. if "OK" not in hapd.request("BSS_TM_REQ " + addr + " neighbor=11:22:33:44:55:66,0x0000,81,3,7"):
  393. raise Exception("BSS_TM_REQ command failed")
  394. ev = hapd.wait_event(['BSS-TM-RESP'], timeout=10)
  395. if ev is None:
  396. raise Exception("No BSS Transition Management Response")
  397. if "status_code=0" in ev:
  398. raise Exception("BSS transition accepted unexpectedly")
  399. dev[0].dump_monitor()
  400. logger.info("Preferred Candidate List (no matching neighbor) without Disassociation Imminent")
  401. if "OK" not in hapd.request("BSS_TM_REQ " + addr + " pref=1 neighbor=11:22:33:44:55:66,0x0000,81,3,7,0301ff neighbor=22:33:44:55:66:77,0x0000,1,36,7 neighbor=00:11:22:33:44:55,0x0000,81,4,7,03010a"):
  402. raise Exception("BSS_TM_REQ command failed")
  403. ev = hapd.wait_event(['BSS-TM-RESP'], timeout=10)
  404. if ev is None:
  405. raise Exception("No BSS Transition Management Response")
  406. if "status_code=0" in ev:
  407. raise Exception("BSS transition accepted unexpectedly")
  408. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  409. if ev is None:
  410. raise Exception("No scan started")
  411. dev[0].dump_monitor()
  412. logger.info("Preferred Candidate List (matching neighbor for another BSS) without Disassociation Imminent")
  413. if "OK" not in hapd.request("BSS_TM_REQ " + addr + " pref=1 abridged=1 valid_int=255 neighbor=" + apdev[1]['bssid'] + ",0x0000,115,36,7,0301ff"):
  414. raise Exception("BSS_TM_REQ command failed")
  415. ev = hapd.wait_event(['BSS-TM-RESP'], timeout=10)
  416. if ev is None:
  417. raise Exception("No BSS Transition Management Response")
  418. if "status_code=0" not in ev:
  419. raise Exception("BSS transition request was not accepted: " + ev)
  420. if "target_bssid=" + apdev[1]['bssid'] not in ev:
  421. raise Exception("Unexpected target BSS: " + ev)
  422. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  423. if ev is None:
  424. raise Exception("No reassociation seen");
  425. if apdev[1]['bssid'] not in ev:
  426. raise Exception("Unexpected reassociation target: " + ev)
  427. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.1)
  428. if ev is not None:
  429. raise Exception("Unexpected scan started")
  430. dev[0].dump_monitor()
  431. logger.info("Preferred Candidate List with two matches, no roam needed")
  432. if "OK" not in hapd2.request("BSS_TM_REQ " + addr + " pref=1 abridged=1 valid_int=255 neighbor=" + apdev[0]['bssid'] + ",0x0000,81,1,7,030101 neighbor=" + apdev[1]['bssid'] + ",0x0000,115,36,7,0301ff"):
  433. raise Exception("BSS_TM_REQ command failed")
  434. ev = hapd2.wait_event(['BSS-TM-RESP'], timeout=10)
  435. if ev is None:
  436. raise Exception("No BSS Transition Management Response")
  437. if "status_code=0" not in ev:
  438. raise Exception("BSS transition request was not accepted: " + ev)
  439. if "target_bssid=" + apdev[1]['bssid'] not in ev:
  440. raise Exception("Unexpected target BSS: " + ev)
  441. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=0.1)
  442. if ev is not None:
  443. raise Exception("Unexpected scan started")
  444. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.5)
  445. if ev is not None:
  446. raise Exception("Unexpected reassociation");
  447. finally:
  448. dev[0].request("DISCONNECT")
  449. if hapd:
  450. hapd.request("DISABLE")
  451. if hapd2:
  452. hapd2.request("DISABLE")
  453. subprocess.call(['iw', 'reg', 'set', '00'])
  454. dev[0].flush_scan_cache()