test_wnm.py 21 KB

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