test_pmksa_cache.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. # WPA2-Enterprise PMKSA caching 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 logging
  7. logger = logging.getLogger()
  8. import subprocess
  9. import time
  10. import hostapd
  11. import hwsim_utils
  12. from wpasupplicant import WpaSupplicant
  13. from utils import alloc_fail, HwsimSkip
  14. from test_ap_eap import eap_connect
  15. def test_pmksa_cache_on_roam_back(dev, apdev):
  16. """PMKSA cache to skip EAP on reassociation back to same AP"""
  17. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  18. hostapd.add_ap(apdev[0], params)
  19. bssid = apdev[0]['bssid']
  20. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  21. eap="GPSK", identity="gpsk user",
  22. password="abcdefghijklmnop0123456789abcdef",
  23. scan_freq="2412")
  24. pmksa = dev[0].get_pmksa(bssid)
  25. if pmksa is None:
  26. raise Exception("No PMKSA cache entry created")
  27. if pmksa['opportunistic'] != '0':
  28. raise Exception("Unexpected opportunistic PMKSA cache entry")
  29. hostapd.add_ap(apdev[1], params)
  30. bssid2 = apdev[1]['bssid']
  31. dev[0].dump_monitor()
  32. logger.info("Roam to AP2")
  33. # It can take some time for the second AP to become ready to reply to Probe
  34. # Request frames especially under heavy CPU load, so allow couple of rounds
  35. # of scanning to avoid reporting errors incorrectly just because of scans
  36. # not having seen the target AP.
  37. for i in range(0, 10):
  38. dev[0].scan(freq="2412")
  39. if dev[0].get_bss(bssid2) is not None:
  40. break
  41. logger.info("Scan again to find target AP")
  42. dev[0].request("ROAM " + bssid2)
  43. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  44. if ev is None:
  45. raise Exception("EAP success timed out")
  46. dev[0].wait_connected(timeout=10, error="Roaming timed out")
  47. pmksa2 = dev[0].get_pmksa(bssid2)
  48. if pmksa2 is None:
  49. raise Exception("No PMKSA cache entry found")
  50. if pmksa2['opportunistic'] != '0':
  51. raise Exception("Unexpected opportunistic PMKSA cache entry")
  52. dev[0].dump_monitor()
  53. logger.info("Roam back to AP1")
  54. dev[0].scan(freq="2412")
  55. dev[0].request("ROAM " + bssid)
  56. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  57. "CTRL-EVENT-CONNECTED"], timeout=10)
  58. if ev is None:
  59. raise Exception("Roaming with the AP timed out")
  60. if "CTRL-EVENT-EAP-STARTED" in ev:
  61. raise Exception("Unexpected EAP exchange")
  62. pmksa1b = dev[0].get_pmksa(bssid)
  63. if pmksa1b is None:
  64. raise Exception("No PMKSA cache entry found")
  65. if pmksa['pmkid'] != pmksa1b['pmkid']:
  66. raise Exception("Unexpected PMKID change for AP1")
  67. dev[0].dump_monitor()
  68. if "FAIL" in dev[0].request("PMKSA_FLUSH"):
  69. raise Exception("PMKSA_FLUSH failed")
  70. if dev[0].get_pmksa(bssid) is not None or dev[0].get_pmksa(bssid2) is not None:
  71. raise Exception("PMKSA_FLUSH did not remove PMKSA entries")
  72. dev[0].wait_disconnected(timeout=5)
  73. dev[0].wait_connected(timeout=15, error="Reconnection timed out")
  74. def test_pmksa_cache_and_reauth(dev, apdev):
  75. """PMKSA caching and EAPOL reauthentication"""
  76. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  77. hapd = hostapd.add_ap(apdev[0], params)
  78. bssid = apdev[0]['bssid']
  79. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  80. eap="GPSK", identity="gpsk user",
  81. password="abcdefghijklmnop0123456789abcdef",
  82. scan_freq="2412")
  83. hostapd.add_ap(apdev[1], params)
  84. bssid2 = apdev[1]['bssid']
  85. dev[0].dump_monitor()
  86. logger.info("Roam to AP2")
  87. # It can take some time for the second AP to become ready to reply to Probe
  88. # Request frames especially under heavy CPU load, so allow couple of rounds
  89. # of scanning to avoid reporting errors incorrectly just because of scans
  90. # not having seen the target AP.
  91. for i in range(0, 10):
  92. dev[0].scan(freq="2412")
  93. if dev[0].get_bss(bssid2) is not None:
  94. break
  95. logger.info("Scan again to find target AP")
  96. dev[0].request("ROAM " + bssid2)
  97. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  98. if ev is None:
  99. raise Exception("EAP success timed out")
  100. dev[0].wait_connected(timeout=10, error="Roaming timed out")
  101. dev[0].dump_monitor()
  102. logger.info("Roam back to AP1")
  103. dev[0].scan(freq="2412")
  104. dev[0].request("ROAM " + bssid)
  105. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  106. "CTRL-EVENT-CONNECTED"], timeout=10)
  107. if ev is None:
  108. raise Exception("Roaming with the AP timed out")
  109. if "CTRL-EVENT-EAP-STARTED" in ev:
  110. raise Exception("Unexpected EAP exchange")
  111. # Verify EAPOL reauthentication after PMKSA caching
  112. hapd.request("EAPOL_REAUTH " + dev[0].own_addr())
  113. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
  114. if ev is None:
  115. raise Exception("EAP authentication did not start")
  116. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=5)
  117. if ev is None:
  118. raise Exception("EAP authentication did not succeed")
  119. def test_pmksa_cache_opportunistic_only_on_sta(dev, apdev):
  120. """Opportunistic PMKSA caching enabled only on station"""
  121. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  122. hostapd.add_ap(apdev[0], params)
  123. bssid = apdev[0]['bssid']
  124. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  125. eap="GPSK", identity="gpsk user",
  126. password="abcdefghijklmnop0123456789abcdef", okc=True,
  127. scan_freq="2412")
  128. pmksa = dev[0].get_pmksa(bssid)
  129. if pmksa is None:
  130. raise Exception("No PMKSA cache entry created")
  131. if pmksa['opportunistic'] != '0':
  132. raise Exception("Unexpected opportunistic PMKSA cache entry")
  133. hostapd.add_ap(apdev[1], params)
  134. bssid2 = apdev[1]['bssid']
  135. dev[0].dump_monitor()
  136. logger.info("Roam to AP2")
  137. dev[0].scan(freq="2412")
  138. dev[0].request("ROAM " + bssid2)
  139. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  140. if ev is None:
  141. raise Exception("EAP success timed out")
  142. dev[0].wait_connected(timeout=10, error="Roaming timed out")
  143. pmksa2 = dev[0].get_pmksa(bssid2)
  144. if pmksa2 is None:
  145. raise Exception("No PMKSA cache entry found")
  146. if pmksa2['opportunistic'] != '0':
  147. raise Exception("Unexpected opportunistic PMKSA cache entry")
  148. dev[0].dump_monitor()
  149. logger.info("Roam back to AP1")
  150. dev[0].scan(freq="2412")
  151. dev[0].request("ROAM " + bssid)
  152. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  153. "CTRL-EVENT-CONNECTED"], timeout=10)
  154. if ev is None:
  155. raise Exception("Roaming with the AP timed out")
  156. if "CTRL-EVENT-EAP-STARTED" in ev:
  157. raise Exception("Unexpected EAP exchange")
  158. pmksa1b = dev[0].get_pmksa(bssid)
  159. if pmksa1b is None:
  160. raise Exception("No PMKSA cache entry found")
  161. if pmksa['pmkid'] != pmksa1b['pmkid']:
  162. raise Exception("Unexpected PMKID change for AP1")
  163. def test_pmksa_cache_opportunistic(dev, apdev):
  164. """Opportunistic PMKSA caching"""
  165. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  166. params['okc'] = "1"
  167. hostapd.add_ap(apdev[0], params)
  168. bssid = apdev[0]['bssid']
  169. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  170. eap="GPSK", identity="gpsk user",
  171. password="abcdefghijklmnop0123456789abcdef", okc=True,
  172. scan_freq="2412")
  173. pmksa = dev[0].get_pmksa(bssid)
  174. if pmksa is None:
  175. raise Exception("No PMKSA cache entry created")
  176. if pmksa['opportunistic'] != '0':
  177. raise Exception("Unexpected opportunistic PMKSA cache entry")
  178. hostapd.add_ap(apdev[1], params)
  179. bssid2 = apdev[1]['bssid']
  180. dev[0].dump_monitor()
  181. logger.info("Roam to AP2")
  182. dev[0].scan(freq="2412")
  183. dev[0].request("ROAM " + bssid2)
  184. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  185. "CTRL-EVENT-CONNECTED"], timeout=10)
  186. if ev is None:
  187. raise Exception("Roaming with the AP timed out")
  188. if "CTRL-EVENT-EAP-STARTED" in ev:
  189. raise Exception("Unexpected EAP exchange")
  190. pmksa2 = dev[0].get_pmksa(bssid2)
  191. if pmksa2 is None:
  192. raise Exception("No PMKSA cache entry created")
  193. dev[0].dump_monitor()
  194. logger.info("Roam back to AP1")
  195. dev[0].scan(freq="2412")
  196. dev[0].request("ROAM " + bssid)
  197. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  198. "CTRL-EVENT-CONNECTED"], timeout=10)
  199. if ev is None:
  200. raise Exception("Roaming with the AP timed out")
  201. if "CTRL-EVENT-EAP-STARTED" in ev:
  202. raise Exception("Unexpected EAP exchange")
  203. pmksa1b = dev[0].get_pmksa(bssid)
  204. if pmksa1b is None:
  205. raise Exception("No PMKSA cache entry found")
  206. if pmksa['pmkid'] != pmksa1b['pmkid']:
  207. raise Exception("Unexpected PMKID change for AP1")
  208. def test_pmksa_cache_opportunistic_connect(dev, apdev):
  209. """Opportunistic PMKSA caching with connect API"""
  210. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  211. params['okc'] = "1"
  212. hostapd.add_ap(apdev[0], params)
  213. bssid = apdev[0]['bssid']
  214. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  215. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  216. wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  217. eap="GPSK", identity="gpsk user",
  218. password="abcdefghijklmnop0123456789abcdef", okc=True,
  219. scan_freq="2412")
  220. pmksa = wpas.get_pmksa(bssid)
  221. if pmksa is None:
  222. raise Exception("No PMKSA cache entry created")
  223. if pmksa['opportunistic'] != '0':
  224. raise Exception("Unexpected opportunistic PMKSA cache entry")
  225. hostapd.add_ap(apdev[1], params)
  226. bssid2 = apdev[1]['bssid']
  227. wpas.dump_monitor()
  228. logger.info("Roam to AP2")
  229. wpas.scan_for_bss(bssid2, freq="2412", force_scan=True)
  230. wpas.request("ROAM " + bssid2)
  231. ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
  232. "CTRL-EVENT-CONNECTED"], timeout=10)
  233. if ev is None:
  234. raise Exception("Roaming with the AP timed out")
  235. if "CTRL-EVENT-EAP-STARTED" in ev:
  236. raise Exception("Unexpected EAP exchange")
  237. pmksa2 = wpas.get_pmksa(bssid2)
  238. if pmksa2 is None:
  239. raise Exception("No PMKSA cache entry created")
  240. wpas.dump_monitor()
  241. logger.info("Roam back to AP1")
  242. wpas.scan(freq="2412")
  243. wpas.request("ROAM " + bssid)
  244. ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
  245. "CTRL-EVENT-CONNECTED"], timeout=10)
  246. if ev is None:
  247. raise Exception("Roaming with the AP timed out")
  248. if "CTRL-EVENT-EAP-STARTED" in ev:
  249. raise Exception("Unexpected EAP exchange")
  250. pmksa1b = wpas.get_pmksa(bssid)
  251. if pmksa1b is None:
  252. raise Exception("No PMKSA cache entry found")
  253. if pmksa['pmkid'] != pmksa1b['pmkid']:
  254. raise Exception("Unexpected PMKID change for AP1")
  255. def test_pmksa_cache_expiration(dev, apdev):
  256. """PMKSA cache entry expiration"""
  257. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  258. hapd = hostapd.add_ap(apdev[0], params)
  259. bssid = apdev[0]['bssid']
  260. dev[0].request("SET dot11RSNAConfigPMKLifetime 10")
  261. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  262. eap="GPSK", identity="gpsk user",
  263. password="abcdefghijklmnop0123456789abcdef",
  264. scan_freq="2412")
  265. pmksa = dev[0].get_pmksa(bssid)
  266. if pmksa is None:
  267. raise Exception("No PMKSA cache entry created")
  268. logger.info("Wait for PMKSA cache entry to expire")
  269. ev = dev[0].wait_event(["WPA: Key negotiation completed",
  270. "CTRL-EVENT-DISCONNECTED"], timeout=15)
  271. if ev is None:
  272. raise Exception("No EAP reauthentication seen")
  273. if "CTRL-EVENT-DISCONNECTED" in ev:
  274. raise Exception("Unexpected disconnection")
  275. pmksa2 = dev[0].get_pmksa(bssid)
  276. if pmksa['pmkid'] == pmksa2['pmkid']:
  277. raise Exception("PMKID did not change")
  278. hwsim_utils.test_connectivity(dev[0], hapd)
  279. def test_pmksa_cache_expiration_disconnect(dev, apdev):
  280. """PMKSA cache entry expiration (disconnect)"""
  281. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  282. hapd = hostapd.add_ap(apdev[0], params)
  283. bssid = apdev[0]['bssid']
  284. dev[0].request("SET dot11RSNAConfigPMKLifetime 2")
  285. dev[0].request("SET dot11RSNAConfigPMKReauthThreshold 100")
  286. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  287. eap="GPSK", identity="gpsk user",
  288. password="abcdefghijklmnop0123456789abcdef",
  289. scan_freq="2412")
  290. pmksa = dev[0].get_pmksa(bssid)
  291. if pmksa is None:
  292. raise Exception("No PMKSA cache entry created")
  293. hapd.request("SET auth_server_shared_secret incorrect")
  294. logger.info("Wait for PMKSA cache entry to expire")
  295. ev = dev[0].wait_event(["WPA: Key negotiation completed",
  296. "CTRL-EVENT-DISCONNECTED"], timeout=15)
  297. if ev is None:
  298. raise Exception("No EAP reauthentication seen")
  299. if "CTRL-EVENT-DISCONNECTED" not in ev:
  300. raise Exception("Missing disconnection")
  301. hapd.request("SET auth_server_shared_secret radius")
  302. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=15)
  303. if ev is None:
  304. raise Exception("No EAP reauthentication seen")
  305. pmksa2 = dev[0].get_pmksa(bssid)
  306. if pmksa['pmkid'] == pmksa2['pmkid']:
  307. raise Exception("PMKID did not change")
  308. def test_pmksa_cache_and_cui(dev, apdev):
  309. """PMKSA cache and Chargeable-User-Identity"""
  310. params = hostapd.wpa2_eap_params(ssid="cui")
  311. params['radius_request_cui'] = '1'
  312. params['acct_server_addr'] = "127.0.0.1"
  313. params['acct_server_port'] = "1813"
  314. params['acct_server_shared_secret'] = "radius"
  315. hapd = hostapd.add_ap(apdev[0], params)
  316. bssid = apdev[0]['bssid']
  317. dev[0].connect("cui", proto="RSN", key_mgmt="WPA-EAP",
  318. eap="GPSK", identity="gpsk-cui",
  319. password="abcdefghijklmnop0123456789abcdef",
  320. scan_freq="2412")
  321. pmksa = dev[0].get_pmksa(bssid)
  322. if pmksa is None:
  323. raise Exception("No PMKSA cache entry created")
  324. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  325. if ev is None:
  326. raise Exception("No connection event received from hostapd")
  327. dev[0].dump_monitor()
  328. logger.info("Disconnect and reconnect to the same AP")
  329. dev[0].request("DISCONNECT")
  330. dev[0].wait_disconnected()
  331. dev[0].request("RECONNECT")
  332. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  333. "CTRL-EVENT-CONNECTED"], timeout=10)
  334. if ev is None:
  335. raise Exception("Reconnect timed out")
  336. if "CTRL-EVENT-EAP-STARTED" in ev:
  337. raise Exception("Unexpected EAP exchange")
  338. pmksa1b = dev[0].get_pmksa(bssid)
  339. if pmksa1b is None:
  340. raise Exception("No PMKSA cache entry found")
  341. if pmksa['pmkid'] != pmksa1b['pmkid']:
  342. raise Exception("Unexpected PMKID change for AP1")
  343. dev[0].request("REAUTHENTICATE")
  344. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  345. if ev is None:
  346. raise Exception("EAP success timed out")
  347. for i in range(0, 20):
  348. state = dev[0].get_status_field("wpa_state")
  349. if state == "COMPLETED":
  350. break
  351. time.sleep(0.1)
  352. if state != "COMPLETED":
  353. raise Exception("Reauthentication did not complete")
  354. def generic_pmksa_cache_preauth(dev, apdev, extraparams, identity, databridge,
  355. force_disconnect=False):
  356. if not extraparams:
  357. extraparams = [{}, {}]
  358. try:
  359. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  360. params['bridge'] = 'ap-br0'
  361. for key, value in extraparams[0].iteritems():
  362. params[key] = value
  363. hapd = hostapd.add_ap(apdev[0], params)
  364. hapd.cmd_execute(['brctl', 'setfd', 'ap-br0', '0'])
  365. hapd.cmd_execute(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  366. eap_connect(dev[0], hapd, "PAX", identity,
  367. password_hex="0123456789abcdef0123456789abcdef")
  368. # Verify connectivity in the correct VLAN
  369. hwsim_utils.test_connectivity_iface(dev[0], hapd, databridge)
  370. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  371. params['bridge'] = 'ap-br0'
  372. params['rsn_preauth'] = '1'
  373. params['rsn_preauth_interfaces'] = databridge
  374. for key, value in extraparams[1].iteritems():
  375. params[key] = value
  376. hostapd.add_ap(apdev[1], params)
  377. bssid1 = apdev[1]['bssid']
  378. dev[0].scan(freq="2412")
  379. success = False
  380. status_seen = False
  381. for i in range(0, 50):
  382. if not status_seen:
  383. status = dev[0].request("STATUS")
  384. if "Pre-authentication EAPOL state machines:" in status:
  385. status_seen = True
  386. time.sleep(0.1)
  387. pmksa = dev[0].get_pmksa(bssid1)
  388. if pmksa:
  389. success = True
  390. break
  391. if not success:
  392. raise Exception("No PMKSA cache entry created from pre-authentication")
  393. if not status_seen:
  394. raise Exception("Pre-authentication EAPOL status was not available")
  395. dev[0].scan(freq="2412")
  396. if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
  397. raise Exception("Scan results missing RSN element info")
  398. dev[0].request("ROAM " + bssid1)
  399. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  400. "CTRL-EVENT-CONNECTED"], timeout=10)
  401. if ev is None:
  402. raise Exception("Roaming with the AP timed out")
  403. if "CTRL-EVENT-EAP-STARTED" in ev:
  404. raise Exception("Unexpected EAP exchange")
  405. pmksa2 = dev[0].get_pmksa(bssid1)
  406. if pmksa2 is None:
  407. raise Exception("No PMKSA cache entry")
  408. if pmksa['pmkid'] != pmksa2['pmkid']:
  409. raise Exception("Unexpected PMKID change")
  410. # Verify connectivity in the correct VLAN
  411. hwsim_utils.test_connectivity_iface(dev[0], hapd, databridge)
  412. if not force_disconnect:
  413. return
  414. # Disconnect the STA from both APs to avoid forceful ifdown by the
  415. # test script on a VLAN that this has an associated STA. That used to
  416. # trigger a mac80211 warning.
  417. dev[0].request("DISCONNECT")
  418. hapd.request("DISABLE")
  419. finally:
  420. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev',
  421. 'ap-br0', 'down', '2>', '/dev/null'],
  422. shell=True)
  423. hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', 'ap-br0',
  424. '2>', '/dev/null'], shell=True)
  425. def test_pmksa_cache_preauth(dev, apdev):
  426. """RSN pre-authentication to generate PMKSA cache entry"""
  427. generic_pmksa_cache_preauth(dev, apdev, None,
  428. "pax.user@example.com", "ap-br0")
  429. def test_pmksa_cache_preauth_per_sta_vif(dev, apdev):
  430. """RSN pre-authentication to generate PMKSA cache entry with per_sta_vif"""
  431. extraparams = [{}, {}]
  432. extraparams[0]['per_sta_vif'] = "1"
  433. extraparams[1]['per_sta_vif'] = "1"
  434. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  435. "pax.user@example.com", "ap-br0")
  436. def test_pmksa_cache_preauth_vlan_enabled(dev, apdev):
  437. """RSN pre-authentication to generate PMKSA cache entry (dynamic_vlan optional but station without VLAN set)"""
  438. extraparams = [{}, {}]
  439. extraparams[0]['dynamic_vlan'] = '1'
  440. extraparams[1]['dynamic_vlan'] = '1'
  441. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  442. "pax.user@example.com", "ap-br0")
  443. def test_pmksa_cache_preauth_vlan_enabled_per_sta_vif(dev, apdev):
  444. """RSN pre-authentication to generate PMKSA cache entry (dynamic_vlan optional but station without VLAN set, with per_sta_vif enabled)"""
  445. extraparams = [{}, {}]
  446. extraparams[0]['per_sta_vif'] = "1"
  447. extraparams[1]['per_sta_vif'] = "1"
  448. extraparams[0]['dynamic_vlan'] = '1'
  449. extraparams[1]['dynamic_vlan'] = '1'
  450. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  451. "pax.user@example.com", "ap-br0")
  452. def test_pmksa_cache_preauth_vlan_used(dev, apdev):
  453. """RSN pre-authentication to generate PMKSA cache entry (station with VLAN set)"""
  454. run_pmksa_cache_preauth_vlan_used(dev, apdev, None, force_disconnect=True)
  455. def run_pmksa_cache_preauth_vlan_used(dev, apdev, extraparams=None,
  456. force_disconnect=False):
  457. try:
  458. subprocess.call(['brctl', 'addbr', 'brvlan1'])
  459. subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
  460. if not extraparams:
  461. extraparams = [{}, {}]
  462. extraparams[0]['dynamic_vlan'] = '1'
  463. extraparams[0]['vlan_file'] = 'hostapd.wlan3.vlan'
  464. extraparams[1]['dynamic_vlan'] = '1'
  465. extraparams[1]['vlan_file'] = 'hostapd.wlan4.vlan'
  466. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  467. "vlan1", "brvlan1",
  468. force_disconnect=force_disconnect)
  469. finally:
  470. subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
  471. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
  472. stderr=open('/dev/null', 'w'))
  473. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan4.1', 'down'],
  474. stderr=open('/dev/null', 'w'))
  475. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
  476. stderr=open('/dev/null', 'w'))
  477. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan4.1'],
  478. stderr=open('/dev/null', 'w'))
  479. subprocess.call(['brctl', 'delbr', 'brvlan1'])
  480. def test_pmksa_cache_preauth_vlan_used_per_sta_vif(dev, apdev):
  481. """RSN pre-authentication to generate PMKSA cache entry (station with VLAN set, per_sta_vif=1)"""
  482. extraparams = [{}, {}]
  483. extraparams[0]['per_sta_vif'] = "1"
  484. extraparams[1]['per_sta_vif'] = "1"
  485. run_pmksa_cache_preauth_vlan_used(dev, apdev, extraparams)
  486. def test_pmksa_cache_disabled(dev, apdev):
  487. """PMKSA cache disabling on AP"""
  488. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  489. params['disable_pmksa_caching'] = '1'
  490. hostapd.add_ap(apdev[0], params)
  491. bssid = apdev[0]['bssid']
  492. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  493. eap="GPSK", identity="gpsk user",
  494. password="abcdefghijklmnop0123456789abcdef",
  495. scan_freq="2412")
  496. hostapd.add_ap(apdev[1], params)
  497. bssid2 = apdev[1]['bssid']
  498. dev[0].dump_monitor()
  499. logger.info("Roam to AP2")
  500. dev[0].scan_for_bss(bssid2, freq="2412")
  501. dev[0].request("ROAM " + bssid2)
  502. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  503. if ev is None:
  504. raise Exception("EAP success timed out")
  505. dev[0].wait_connected(timeout=10, error="Roaming timed out")
  506. dev[0].dump_monitor()
  507. logger.info("Roam back to AP1")
  508. dev[0].scan(freq="2412")
  509. dev[0].request("ROAM " + bssid)
  510. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  511. "CTRL-EVENT-CONNECTED"], timeout=20)
  512. if ev is None:
  513. raise Exception("Roaming with the AP timed out")
  514. if "CTRL-EVENT-CONNECTED" in ev:
  515. raise Exception("EAP exchange missing")
  516. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  517. if ev is None:
  518. raise Exception("Roaming with the AP timed out")
  519. def test_pmksa_cache_ap_expiration(dev, apdev):
  520. """PMKSA cache entry expiring on AP"""
  521. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  522. hapd = hostapd.add_ap(apdev[0], params)
  523. bssid = apdev[0]['bssid']
  524. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  525. eap="GPSK", identity="gpsk-user-session-timeout",
  526. password="abcdefghijklmnop0123456789abcdef",
  527. scan_freq="2412")
  528. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  529. if ev is None:
  530. raise Exception("No connection event received from hostapd")
  531. dev[0].request("DISCONNECT")
  532. time.sleep(5)
  533. dev[0].dump_monitor()
  534. dev[0].request("RECONNECT")
  535. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  536. "CTRL-EVENT-CONNECTED"], timeout=20)
  537. if ev is None:
  538. raise Exception("Roaming with the AP timed out")
  539. if "CTRL-EVENT-CONNECTED" in ev:
  540. raise Exception("EAP exchange missing")
  541. dev[0].wait_connected(timeout=20, error="Reconnect timed out")
  542. dev[0].dump_monitor()
  543. dev[0].wait_disconnected(timeout=20)
  544. dev[0].wait_connected(timeout=20, error="Reassociation timed out")
  545. def test_pmksa_cache_multiple_sta(dev, apdev):
  546. """PMKSA cache with multiple stations"""
  547. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  548. hostapd.add_ap(apdev[0], params)
  549. bssid = apdev[0]['bssid']
  550. for d in dev:
  551. d.flush_scan_cache()
  552. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  553. eap="GPSK", identity="gpsk-user-session-timeout",
  554. password="abcdefghijklmnop0123456789abcdef",
  555. scan_freq="2412")
  556. dev[1].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  557. eap="GPSK", identity="gpsk user",
  558. password="abcdefghijklmnop0123456789abcdef",
  559. scan_freq="2412")
  560. dev[2].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  561. eap="GPSK", identity="gpsk-user-session-timeout",
  562. password="abcdefghijklmnop0123456789abcdef",
  563. scan_freq="2412")
  564. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  565. wpas.interface_add("wlan5")
  566. wpas.flush_scan_cache()
  567. wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  568. eap="GPSK", identity="gpsk user",
  569. password="abcdefghijklmnop0123456789abcdef",
  570. scan_freq="2412")
  571. hostapd.add_ap(apdev[1], params)
  572. bssid2 = apdev[1]['bssid']
  573. logger.info("Roam to AP2")
  574. for sta in [ dev[1], dev[0], dev[2], wpas ]:
  575. sta.dump_monitor()
  576. sta.scan_for_bss(bssid2, freq="2412")
  577. if "OK" not in sta.request("ROAM " + bssid2):
  578. raise Exception("ROAM command failed (" + sta.ifname + ")")
  579. ev = sta.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  580. if ev is None:
  581. raise Exception("EAP success timed out")
  582. sta.wait_connected(timeout=10, error="Roaming timed out")
  583. sta.dump_monitor()
  584. logger.info("Roam back to AP1")
  585. for sta in [ dev[1], wpas, dev[0], dev[2] ]:
  586. sta.dump_monitor()
  587. sta.scan(freq="2412")
  588. sta.dump_monitor()
  589. sta.request("ROAM " + bssid)
  590. sta.wait_connected(timeout=10, error="Roaming timed out")
  591. sta.dump_monitor()
  592. time.sleep(4)
  593. logger.info("Roam back to AP2")
  594. for sta in [ dev[1], wpas, dev[0], dev[2] ]:
  595. sta.dump_monitor()
  596. sta.scan(freq="2412")
  597. sta.dump_monitor()
  598. sta.request("ROAM " + bssid2)
  599. sta.wait_connected(timeout=10, error="Roaming timed out")
  600. sta.dump_monitor()
  601. def test_pmksa_cache_opportunistic_multiple_sta(dev, apdev):
  602. """Opportunistic PMKSA caching with multiple stations"""
  603. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  604. params['okc'] = "1"
  605. hostapd.add_ap(apdev[0], params)
  606. bssid = apdev[0]['bssid']
  607. for d in dev:
  608. d.flush_scan_cache()
  609. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  610. wpas.interface_add("wlan5")
  611. wpas.flush_scan_cache()
  612. for sta in [ dev[0], dev[1], dev[2], wpas ]:
  613. sta.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  614. eap="GPSK", identity="gpsk user",
  615. password="abcdefghijklmnop0123456789abcdef", okc=True,
  616. scan_freq="2412")
  617. hostapd.add_ap(apdev[1], params)
  618. bssid2 = apdev[1]['bssid']
  619. logger.info("Roam to AP2")
  620. for sta in [ dev[2], dev[0], wpas, dev[1] ]:
  621. sta.dump_monitor()
  622. sta.scan_for_bss(bssid2, freq="2412")
  623. if "OK" not in sta.request("ROAM " + bssid2):
  624. raise Exception("ROAM command failed")
  625. ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
  626. "CTRL-EVENT-CONNECTED"], timeout=10)
  627. if ev is None:
  628. raise Exception("Roaming with the AP timed out")
  629. if "CTRL-EVENT-EAP-STARTED" in ev:
  630. raise Exception("Unexpected EAP exchange")
  631. pmksa2 = sta.get_pmksa(bssid2)
  632. if pmksa2 is None:
  633. raise Exception("No PMKSA cache entry created")
  634. sta.dump_monitor()
  635. logger.info("Roam back to AP1")
  636. for sta in [ dev[0], dev[1], dev[2], wpas ]:
  637. sta.dump_monitor()
  638. sta.scan_for_bss(bssid, freq="2412")
  639. sta.request("ROAM " + bssid)
  640. ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
  641. "CTRL-EVENT-CONNECTED"], timeout=10)
  642. if ev is None:
  643. raise Exception("Roaming with the AP timed out")
  644. if "CTRL-EVENT-EAP-STARTED" in ev:
  645. raise Exception("Unexpected EAP exchange")
  646. def test_pmksa_cache_preauth_oom(dev, apdev):
  647. """RSN pre-authentication to generate PMKSA cache entry and OOM"""
  648. try:
  649. _test_pmksa_cache_preauth_oom(dev, apdev)
  650. finally:
  651. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', 'ap-br0',
  652. 'down'])
  653. hostapd.cmd_execute(apdev[0], ['brctl', 'delbr', 'ap-br0'])
  654. def _test_pmksa_cache_preauth_oom(dev, apdev):
  655. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  656. params['bridge'] = 'ap-br0'
  657. hapd = hostapd.add_ap(apdev[0], params)
  658. hostapd.cmd_execute(apdev[0], ['brctl', 'setfd', 'ap-br0', '0'])
  659. hostapd.cmd_execute(apdev[0], ['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  660. eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
  661. password_hex="0123456789abcdef0123456789abcdef",
  662. bssid=apdev[0]['bssid'])
  663. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  664. params['bridge'] = 'ap-br0'
  665. params['rsn_preauth'] = '1'
  666. params['rsn_preauth_interfaces'] = 'ap-br0'
  667. hapd = hostapd.add_ap(apdev[1], params)
  668. bssid1 = apdev[1]['bssid']
  669. tests = [ (1, "rsn_preauth_receive"),
  670. (2, "rsn_preauth_receive"),
  671. (1, "rsn_preauth_send") ]
  672. for test in tests:
  673. with alloc_fail(hapd, test[0], test[1]):
  674. dev[0].scan_for_bss(bssid1, freq="2412")
  675. if "OK" not in dev[0].request("PREAUTH " + bssid1):
  676. raise Exception("PREAUTH failed")
  677. success = False
  678. count = 0
  679. for i in range(50):
  680. time.sleep(0.1)
  681. pmksa = dev[0].get_pmksa(bssid1)
  682. if pmksa:
  683. success = True
  684. break
  685. state = hapd.request('GET_ALLOC_FAIL')
  686. if state.startswith('0:'):
  687. count += 1
  688. if count > 2:
  689. break
  690. logger.info("PMKSA cache success: " + str(success))
  691. dev[0].request("PMKSA_FLUSH")
  692. dev[0].wait_disconnected()
  693. dev[0].wait_connected()
  694. dev[0].dump_monitor()
  695. def test_pmksa_cache_size_limit(dev, apdev):
  696. """PMKSA cache size limit in wpa_supplicant"""
  697. try:
  698. _test_pmksa_cache_size_limit(dev, apdev)
  699. finally:
  700. try:
  701. hapd = hostapd.HostapdGlobal(apdev[0])
  702. hapd.flush()
  703. hapd.remove(apdev[0]['ifname'])
  704. except:
  705. pass
  706. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  707. bssid = apdev[0]['bssid']
  708. params['bssid'] = bssid
  709. hostapd.add_ap(apdev[0], params)
  710. def _test_pmksa_cache_size_limit(dev, apdev):
  711. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  712. id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  713. eap="GPSK", identity="gpsk user",
  714. password="abcdefghijklmnop0123456789abcdef",
  715. scan_freq="2412", only_add_network=True)
  716. for i in range(33):
  717. bssid = apdev[0]['bssid'][0:15] + "%02x" % i
  718. logger.info("Iteration with BSSID " + bssid)
  719. params['bssid'] = bssid
  720. hostapd.add_ap(apdev[0], params)
  721. dev[0].request("BSS_FLUSH 0")
  722. dev[0].scan_for_bss(bssid, freq=2412, only_new=True)
  723. dev[0].select_network(id)
  724. dev[0].wait_connected()
  725. dev[0].request("DISCONNECT")
  726. dev[0].wait_disconnected()
  727. dev[0].dump_monitor()
  728. entries = len(dev[0].request("PMKSA").splitlines()) - 1
  729. if i == 32:
  730. if entries != 32:
  731. raise Exception("Unexpected number of PMKSA entries after expected removal of the oldest entry")
  732. elif i + 1 != entries:
  733. raise Exception("Unexpected number of PMKSA entries")
  734. hapd = hostapd.HostapdGlobal(apdev[0])
  735. hapd.flush()
  736. hapd.remove(apdev[0]['ifname'])
  737. def test_pmksa_cache_preauth_timeout(dev, apdev):
  738. """RSN pre-authentication timing out"""
  739. try:
  740. _test_pmksa_cache_preauth_timeout(dev, apdev)
  741. finally:
  742. dev[0].request("SET dot11RSNAConfigSATimeout 60")
  743. def _test_pmksa_cache_preauth_timeout(dev, apdev):
  744. dev[0].request("SET dot11RSNAConfigSATimeout 1")
  745. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  746. hapd = hostapd.add_ap(apdev[0], params)
  747. eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
  748. password_hex="0123456789abcdef0123456789abcdef",
  749. bssid=apdev[0]['bssid'])
  750. if "OK" not in dev[0].request("PREAUTH f2:11:22:33:44:55"):
  751. raise Exception("PREAUTH failed")
  752. ev = dev[0].wait_event(["RSN: pre-authentication with"], timeout=5)
  753. if ev is None:
  754. raise Exception("No timeout event seen")
  755. if "timed out" not in ev:
  756. raise Exception("Unexpected event: " + ev)
  757. def test_pmksa_cache_preauth_wpas_oom(dev, apdev):
  758. """RSN pre-authentication OOM in wpa_supplicant"""
  759. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  760. hapd = hostapd.add_ap(apdev[0], params)
  761. eap_connect(dev[0], hapd, "PAX", "pax.user@example.com",
  762. password_hex="0123456789abcdef0123456789abcdef",
  763. bssid=apdev[0]['bssid'])
  764. for i in range(1, 11):
  765. with alloc_fail(dev[0], i, "rsn_preauth_init"):
  766. res = dev[0].request("PREAUTH f2:11:22:33:44:55").strip()
  767. logger.info("Iteration %d - PREAUTH command results: %s" % (i, res))
  768. for j in range(10):
  769. state = dev[0].request('GET_ALLOC_FAIL')
  770. if state.startswith('0:'):
  771. break
  772. time.sleep(0.05)
  773. def test_pmksa_cache_ctrl(dev, apdev):
  774. """PMKSA cache control interface operations"""
  775. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  776. hapd = hostapd.add_ap(apdev[0], params)
  777. bssid = apdev[0]['bssid']
  778. addr = dev[0].own_addr()
  779. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  780. eap="GPSK", identity="gpsk user",
  781. password="abcdefghijklmnop0123456789abcdef",
  782. scan_freq="2412")
  783. pmksa_sta = dev[0].get_pmksa(bssid)
  784. if pmksa_sta is None:
  785. raise Exception("No PMKSA cache entry created on STA")
  786. pmksa_ap = hapd.get_pmksa(addr)
  787. if pmksa_ap is None:
  788. raise Exception("No PMKSA cache entry created on AP")
  789. if pmksa_sta['pmkid'] != pmksa_ap['pmkid']:
  790. raise Exception("PMKID mismatch in PMKSA cache entries")
  791. if "OK" not in hapd.request("PMKSA_FLUSH"):
  792. raise Exception("PMKSA_FLUSH failed")
  793. pmksa_ap = hapd.get_pmksa(addr)
  794. if pmksa_ap is not None:
  795. raise Exception("PMKSA cache entry was not removed on AP")
  796. dev[0].request("DISCONNECT")
  797. dev[0].wait_disconnected()
  798. dev[0].request("RECONNECT")
  799. dev[0].wait_connected()
  800. pmksa_sta2 = dev[0].get_pmksa(bssid)
  801. if pmksa_sta2 is None:
  802. raise Exception("No PMKSA cache entry created on STA after reconnect")
  803. pmksa_ap2 = hapd.get_pmksa(addr)
  804. if pmksa_ap2 is None:
  805. raise Exception("No PMKSA cache entry created on AP after reconnect")
  806. if pmksa_sta2['pmkid'] != pmksa_ap2['pmkid']:
  807. raise Exception("PMKID mismatch in PMKSA cache entries after reconnect")
  808. if pmksa_sta2['pmkid'] == pmksa_sta['pmkid']:
  809. raise Exception("PMKID did not change after reconnect")
  810. def test_pmksa_cache_ctrl_events(dev, apdev):
  811. """PMKSA cache control interface events"""
  812. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  813. hapd = hostapd.add_ap(apdev[0], params)
  814. bssid = apdev[0]['bssid']
  815. id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  816. eap="GPSK", identity="gpsk user",
  817. password="abcdefghijklmnop0123456789abcdef",
  818. scan_freq="2412", wait_connect=False)
  819. ev = dev[0].wait_event(["PMKSA-CACHE-ADDED"], timeout=15)
  820. if ev is None:
  821. raise Exception("No PMKSA-CACHE-ADDED event")
  822. dev[0].wait_connected()
  823. items = ev.split(' ')
  824. if items[1] != bssid:
  825. raise Exception("BSSID mismatch: " + ev)
  826. if int(items[2]) != id:
  827. raise Exception("network_id mismatch: " + ev)
  828. dev[0].request("PMKSA_FLUSH")
  829. ev = dev[0].wait_event(["PMKSA-CACHE-REMOVED"], timeout=15)
  830. if ev is None:
  831. raise Exception("No PMKSA-CACHE-REMOVED event")
  832. dev[0].wait_disconnected()
  833. dev[0].request("DISCONNECT")
  834. items = ev.split(' ')
  835. if items[1] != bssid:
  836. raise Exception("BSSID mismatch: " + ev)
  837. if int(items[2]) != id:
  838. raise Exception("network_id mismatch: " + ev)
  839. def test_pmksa_cache_ctrl_ext(dev, apdev):
  840. """PMKSA cache control interface for external management"""
  841. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  842. hapd = hostapd.add_ap(apdev[0], params)
  843. bssid = apdev[0]['bssid']
  844. id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  845. eap="GPSK", identity="gpsk user",
  846. password="abcdefghijklmnop0123456789abcdef",
  847. scan_freq="2412")
  848. res1 = dev[0].request("PMKSA_GET %d" % id)
  849. logger.info("PMKSA_GET: " + res1)
  850. if "UNKNOWN COMMAND" in res1:
  851. raise HwsimSkip("PMKSA_GET not supported in the build")
  852. if bssid not in res1:
  853. raise Exception("PMKSA cache entry missing")
  854. hostapd.add_ap(apdev[1], params)
  855. bssid2 = apdev[1]['bssid']
  856. dev[0].scan_for_bss(bssid2, freq=2412, force_scan=True)
  857. dev[0].request("ROAM " + bssid2)
  858. dev[0].wait_connected()
  859. res2 = dev[0].request("PMKSA_GET %d" % id)
  860. logger.info("PMKSA_GET: " + res2)
  861. if bssid not in res2:
  862. raise Exception("PMKSA cache entry 1 missing")
  863. if bssid2 not in res2:
  864. raise Exception("PMKSA cache entry 2 missing")
  865. dev[0].request("REMOVE_NETWORK all")
  866. dev[0].wait_disconnected()
  867. dev[0].request("PMKSA_FLUSH")
  868. id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  869. eap="GPSK", identity="gpsk user",
  870. password="abcdefghijklmnop0123456789abcdef",
  871. scan_freq="2412", only_add_network=True)
  872. res3 = dev[0].request("PMKSA_GET %d" % id)
  873. if res3 != '':
  874. raise Exception("Unexpected PMKSA cache entry remains: " + res3)
  875. res4 = dev[0].request("PMKSA_GET %d" % (id + 1234))
  876. if not res4.startswith('FAIL'):
  877. raise Exception("Unexpected PMKSA cache entry for unknown network: " + res4)
  878. for entry in res2.splitlines():
  879. if "OK" not in dev[0].request("PMKSA_ADD %d %s" % (id, entry)):
  880. raise Exception("Failed to add PMKSA entry")
  881. dev[0].select_network(id)
  882. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  883. "CTRL-EVENT-CONNECTED"], timeout=15)
  884. if ev is None:
  885. raise Exception("Connection with the AP timed out")
  886. if "CTRL-EVENT-EAP-STARTED" in ev:
  887. raise Exception("Unexpected EAP exchange after external PMKSA cache restore")