test_pmksa_cache.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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
  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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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]['ifname'], 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. hostapd.add_ap(apdev[0]['ifname'], 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. def test_pmksa_cache_expiration_disconnect(dev, apdev):
  279. """PMKSA cache entry expiration (disconnect)"""
  280. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  281. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  282. bssid = apdev[0]['bssid']
  283. dev[0].request("SET dot11RSNAConfigPMKLifetime 2")
  284. dev[0].request("SET dot11RSNAConfigPMKReauthThreshold 100")
  285. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  286. eap="GPSK", identity="gpsk user",
  287. password="abcdefghijklmnop0123456789abcdef",
  288. scan_freq="2412")
  289. pmksa = dev[0].get_pmksa(bssid)
  290. if pmksa is None:
  291. raise Exception("No PMKSA cache entry created")
  292. hapd.request("SET auth_server_shared_secret incorrect")
  293. logger.info("Wait for PMKSA cache entry to expire")
  294. ev = dev[0].wait_event(["WPA: Key negotiation completed",
  295. "CTRL-EVENT-DISCONNECTED"], timeout=15)
  296. if ev is None:
  297. raise Exception("No EAP reauthentication seen")
  298. if "CTRL-EVENT-DISCONNECTED" not in ev:
  299. raise Exception("Missing disconnection")
  300. hapd.request("SET auth_server_shared_secret radius")
  301. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=15)
  302. if ev is None:
  303. raise Exception("No EAP reauthentication seen")
  304. pmksa2 = dev[0].get_pmksa(bssid)
  305. if pmksa['pmkid'] == pmksa2['pmkid']:
  306. raise Exception("PMKID did not change")
  307. def test_pmksa_cache_and_cui(dev, apdev):
  308. """PMKSA cache and Chargeable-User-Identity"""
  309. params = hostapd.wpa2_eap_params(ssid="cui")
  310. params['radius_request_cui'] = '1'
  311. params['acct_server_addr'] = "127.0.0.1"
  312. params['acct_server_port'] = "1813"
  313. params['acct_server_shared_secret'] = "radius"
  314. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  315. bssid = apdev[0]['bssid']
  316. dev[0].connect("cui", proto="RSN", key_mgmt="WPA-EAP",
  317. eap="GPSK", identity="gpsk-cui",
  318. password="abcdefghijklmnop0123456789abcdef",
  319. scan_freq="2412")
  320. pmksa = dev[0].get_pmksa(bssid)
  321. if pmksa is None:
  322. raise Exception("No PMKSA cache entry created")
  323. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  324. if ev is None:
  325. raise Exception("No connection event received from hostapd")
  326. dev[0].dump_monitor()
  327. logger.info("Disconnect and reconnect to the same AP")
  328. dev[0].request("DISCONNECT")
  329. dev[0].wait_disconnected()
  330. dev[0].request("RECONNECT")
  331. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  332. "CTRL-EVENT-CONNECTED"], timeout=10)
  333. if ev is None:
  334. raise Exception("Reconnect timed out")
  335. if "CTRL-EVENT-EAP-STARTED" in ev:
  336. raise Exception("Unexpected EAP exchange")
  337. pmksa1b = dev[0].get_pmksa(bssid)
  338. if pmksa1b is None:
  339. raise Exception("No PMKSA cache entry found")
  340. if pmksa['pmkid'] != pmksa1b['pmkid']:
  341. raise Exception("Unexpected PMKID change for AP1")
  342. dev[0].request("REAUTHENTICATE")
  343. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  344. if ev is None:
  345. raise Exception("EAP success timed out")
  346. for i in range(0, 20):
  347. state = dev[0].get_status_field("wpa_state")
  348. if state == "COMPLETED":
  349. break
  350. time.sleep(0.1)
  351. if state != "COMPLETED":
  352. raise Exception("Reauthentication did not complete")
  353. def test_pmksa_cache_preauth(dev, apdev):
  354. """RSN pre-authentication to generate PMKSA cache entry"""
  355. try:
  356. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  357. params['bridge'] = 'ap-br0'
  358. hostapd.add_ap(apdev[0]['ifname'], params)
  359. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  360. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  361. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  362. password_hex="0123456789abcdef0123456789abcdef")
  363. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  364. params['bridge'] = 'ap-br0'
  365. params['rsn_preauth'] = '1'
  366. params['rsn_preauth_interfaces'] = 'ap-br0'
  367. hostapd.add_ap(apdev[1]['ifname'], params)
  368. bssid1 = apdev[1]['bssid']
  369. dev[0].scan(freq="2412")
  370. success = False
  371. status_seen = False
  372. for i in range(0, 50):
  373. if not status_seen:
  374. status = dev[0].request("STATUS")
  375. if "Pre-authentication EAPOL state machines:" in status:
  376. status_seen = True
  377. time.sleep(0.1)
  378. pmksa = dev[0].get_pmksa(bssid1)
  379. if pmksa:
  380. success = True
  381. break
  382. if not success:
  383. raise Exception("No PMKSA cache entry created from pre-authentication")
  384. if not status_seen:
  385. raise Exception("Pre-authentication EAPOL status was not available")
  386. dev[0].scan(freq="2412")
  387. if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
  388. raise Exception("Scan results missing RSN element info")
  389. dev[0].request("ROAM " + bssid1)
  390. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  391. "CTRL-EVENT-CONNECTED"], timeout=10)
  392. if ev is None:
  393. raise Exception("Roaming with the AP timed out")
  394. if "CTRL-EVENT-EAP-STARTED" in ev:
  395. raise Exception("Unexpected EAP exchange")
  396. pmksa2 = dev[0].get_pmksa(bssid1)
  397. if pmksa2 is None:
  398. raise Exception("No PMKSA cache entry")
  399. if pmksa['pmkid'] != pmksa2['pmkid']:
  400. raise Exception("Unexpected PMKID change")
  401. finally:
  402. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
  403. subprocess.call(['brctl', 'delbr', 'ap-br0'])
  404. def test_pmksa_cache_preauth_vlan_enabled(dev, apdev):
  405. """RSN pre-authentication to generate PMKSA cache entry (dynamic_vlan optional but station without VLAN set)"""
  406. try:
  407. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  408. params['bridge'] = 'ap-br0'
  409. params['dynamic_vlan'] = '1'
  410. hostapd.add_ap(apdev[0]['ifname'], params)
  411. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  412. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  413. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  414. password_hex="0123456789abcdef0123456789abcdef")
  415. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  416. params['bridge'] = 'ap-br0'
  417. params['rsn_preauth'] = '1'
  418. params['rsn_preauth_interfaces'] = 'ap-br0'
  419. params['dynamic_vlan'] = '1'
  420. hostapd.add_ap(apdev[1]['ifname'], params)
  421. bssid1 = apdev[1]['bssid']
  422. dev[0].scan(freq="2412")
  423. success = False
  424. status_seen = False
  425. for i in range(0, 50):
  426. if not status_seen:
  427. status = dev[0].request("STATUS")
  428. if "Pre-authentication EAPOL state machines:" in status:
  429. status_seen = True
  430. time.sleep(0.1)
  431. pmksa = dev[0].get_pmksa(bssid1)
  432. if pmksa:
  433. success = True
  434. break
  435. if not success:
  436. raise Exception("No PMKSA cache entry created from pre-authentication")
  437. if not status_seen:
  438. raise Exception("Pre-authentication EAPOL status was not available")
  439. dev[0].scan(freq="2412")
  440. if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
  441. raise Exception("Scan results missing RSN element info")
  442. dev[0].request("ROAM " + bssid1)
  443. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  444. "CTRL-EVENT-CONNECTED"], timeout=10)
  445. if ev is None:
  446. raise Exception("Roaming with the AP timed out")
  447. if "CTRL-EVENT-EAP-STARTED" in ev:
  448. raise Exception("Unexpected EAP exchange")
  449. pmksa2 = dev[0].get_pmksa(bssid1)
  450. if pmksa2 is None:
  451. raise Exception("No PMKSA cache entry")
  452. if pmksa['pmkid'] != pmksa2['pmkid']:
  453. raise Exception("Unexpected PMKID change")
  454. finally:
  455. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
  456. subprocess.call(['brctl', 'delbr', 'ap-br0'])
  457. def test_pmksa_cache_preauth_vlan_used(dev, apdev):
  458. """RSN pre-authentication to generate PMKSA cache entry (station with VLAN set)"""
  459. try:
  460. subprocess.call(['brctl', 'addbr', 'brvlan1'])
  461. subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
  462. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  463. params['bridge'] = 'ap-br0'
  464. params['dynamic_vlan'] = '1'
  465. params['vlan_file'] = 'hostapd.wlan3.vlan'
  466. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  467. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  468. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  469. eap_connect(dev[0], apdev[0], "PAX", "vlan1",
  470. password_hex="0123456789abcdef0123456789abcdef")
  471. # Verify connectivity in the correct VLAN
  472. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  473. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  474. params['bridge'] = 'ap-br0'
  475. params['rsn_preauth'] = '1'
  476. params['rsn_preauth_interfaces'] = 'brvlan1'
  477. params['dynamic_vlan'] = '1'
  478. params['vlan_file'] = 'hostapd.wlan4.vlan'
  479. hostapd.add_ap(apdev[1]['ifname'], params)
  480. bssid1 = apdev[1]['bssid']
  481. dev[0].scan(freq="2412")
  482. success = False
  483. status_seen = False
  484. for i in range(0, 50):
  485. if not status_seen:
  486. status = dev[0].request("STATUS")
  487. if "Pre-authentication EAPOL state machines:" in status:
  488. status_seen = True
  489. time.sleep(0.1)
  490. pmksa = dev[0].get_pmksa(bssid1)
  491. if pmksa:
  492. success = True
  493. break
  494. if not success:
  495. raise Exception("No PMKSA cache entry created from pre-authentication")
  496. if not status_seen:
  497. raise Exception("Pre-authentication EAPOL status was not available")
  498. dev[0].scan(freq="2412")
  499. if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
  500. raise Exception("Scan results missing RSN element info")
  501. dev[0].request("ROAM " + bssid1)
  502. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  503. "CTRL-EVENT-CONNECTED"], timeout=10)
  504. if ev is None:
  505. raise Exception("Roaming with the AP timed out")
  506. if "CTRL-EVENT-EAP-STARTED" in ev:
  507. raise Exception("Unexpected EAP exchange")
  508. pmksa2 = dev[0].get_pmksa(bssid1)
  509. if pmksa2 is None:
  510. raise Exception("No PMKSA cache entry")
  511. if pmksa['pmkid'] != pmksa2['pmkid']:
  512. raise Exception("Unexpected PMKID change")
  513. # Verify connectivity in the correct VLAN
  514. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  515. # Disconnect the STA from both APs to avoid forceful ifdown by the
  516. # test script on a VLAN that this has an associated STA. That used to
  517. # trigger a mac80211 warning.
  518. dev[0].request("DISCONNECT")
  519. hapd.request("DISABLE")
  520. finally:
  521. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
  522. stderr=open('/dev/null', 'w'))
  523. subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
  524. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
  525. stderr=open('/dev/null', 'w'))
  526. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan4.1', 'down'],
  527. stderr=open('/dev/null', 'w'))
  528. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
  529. stderr=open('/dev/null', 'w'))
  530. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan4.1'],
  531. stderr=open('/dev/null', 'w'))
  532. subprocess.call(['brctl', 'delbr', 'ap-br0'],
  533. stderr=open('/dev/null', 'w'))
  534. subprocess.call(['brctl', 'delbr', 'brvlan1'])
  535. def test_pmksa_cache_disabled(dev, apdev):
  536. """PMKSA cache disabling on AP"""
  537. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  538. params['disable_pmksa_caching'] = '1'
  539. hostapd.add_ap(apdev[0]['ifname'], params)
  540. bssid = apdev[0]['bssid']
  541. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  542. eap="GPSK", identity="gpsk user",
  543. password="abcdefghijklmnop0123456789abcdef",
  544. scan_freq="2412")
  545. hostapd.add_ap(apdev[1]['ifname'], params)
  546. bssid2 = apdev[1]['bssid']
  547. dev[0].dump_monitor()
  548. logger.info("Roam to AP2")
  549. dev[0].scan_for_bss(bssid2, freq="2412")
  550. dev[0].request("ROAM " + bssid2)
  551. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  552. if ev is None:
  553. raise Exception("EAP success timed out")
  554. dev[0].wait_connected(timeout=10, error="Roaming timed out")
  555. dev[0].dump_monitor()
  556. logger.info("Roam back to AP1")
  557. dev[0].scan(freq="2412")
  558. dev[0].request("ROAM " + bssid)
  559. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  560. "CTRL-EVENT-CONNECTED"], timeout=20)
  561. if ev is None:
  562. raise Exception("Roaming with the AP timed out")
  563. if "CTRL-EVENT-CONNECTED" in ev:
  564. raise Exception("EAP exchange missing")
  565. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  566. if ev is None:
  567. raise Exception("Roaming with the AP timed out")
  568. def test_pmksa_cache_ap_expiration(dev, apdev):
  569. """PMKSA cache entry expiring on AP"""
  570. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  571. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  572. bssid = apdev[0]['bssid']
  573. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  574. eap="GPSK", identity="gpsk-user-session-timeout",
  575. password="abcdefghijklmnop0123456789abcdef",
  576. scan_freq="2412")
  577. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  578. if ev is None:
  579. raise Exception("No connection event received from hostapd")
  580. dev[0].request("DISCONNECT")
  581. time.sleep(5)
  582. dev[0].dump_monitor()
  583. dev[0].request("RECONNECT")
  584. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  585. "CTRL-EVENT-CONNECTED"], timeout=20)
  586. if ev is None:
  587. raise Exception("Roaming with the AP timed out")
  588. if "CTRL-EVENT-CONNECTED" in ev:
  589. raise Exception("EAP exchange missing")
  590. dev[0].wait_connected(timeout=20, error="Reconnect timed out")
  591. dev[0].dump_monitor()
  592. dev[0].wait_disconnected(timeout=20)
  593. dev[0].wait_connected(timeout=20, error="Reassociation timed out")
  594. def test_pmksa_cache_multiple_sta(dev, apdev):
  595. """PMKSA cache with multiple stations"""
  596. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  597. hostapd.add_ap(apdev[0]['ifname'], params)
  598. bssid = apdev[0]['bssid']
  599. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  600. eap="GPSK", identity="gpsk-user-session-timeout",
  601. password="abcdefghijklmnop0123456789abcdef",
  602. scan_freq="2412")
  603. dev[1].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  604. eap="GPSK", identity="gpsk user",
  605. password="abcdefghijklmnop0123456789abcdef",
  606. scan_freq="2412")
  607. dev[2].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  608. eap="GPSK", identity="gpsk-user-session-timeout",
  609. password="abcdefghijklmnop0123456789abcdef",
  610. scan_freq="2412")
  611. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  612. wpas.interface_add("wlan5")
  613. wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  614. eap="GPSK", identity="gpsk user",
  615. password="abcdefghijklmnop0123456789abcdef",
  616. scan_freq="2412")
  617. hostapd.add_ap(apdev[1]['ifname'], params)
  618. bssid2 = apdev[1]['bssid']
  619. logger.info("Roam to AP2")
  620. for sta in [ dev[1], dev[0], dev[2], wpas ]:
  621. sta.dump_monitor()
  622. sta.scan_for_bss(bssid2, freq="2412")
  623. sta.request("ROAM " + bssid2)
  624. ev = sta.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  625. if ev is None:
  626. raise Exception("EAP success timed out")
  627. sta.wait_connected(timeout=10, error="Roaming timed out")
  628. logger.info("Roam back to AP1")
  629. for sta in [ dev[1], wpas, dev[0], dev[2] ]:
  630. sta.dump_monitor()
  631. sta.scan(freq="2412")
  632. sta.dump_monitor()
  633. sta.request("ROAM " + bssid)
  634. sta.wait_connected(timeout=10, error="Roaming timed out")
  635. sta.dump_monitor()
  636. time.sleep(4)
  637. logger.info("Roam back to AP2")
  638. for sta in [ dev[1], wpas, dev[0], dev[2] ]:
  639. sta.dump_monitor()
  640. sta.scan(freq="2412")
  641. sta.dump_monitor()
  642. sta.request("ROAM " + bssid2)
  643. sta.wait_connected(timeout=10, error="Roaming timed out")
  644. sta.dump_monitor()
  645. def test_pmksa_cache_opportunistic_multiple_sta(dev, apdev):
  646. """Opportunistic PMKSA caching with multiple stations"""
  647. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  648. params['okc'] = "1"
  649. hostapd.add_ap(apdev[0]['ifname'], params)
  650. bssid = apdev[0]['bssid']
  651. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  652. wpas.interface_add("wlan5")
  653. for sta in [ dev[0], dev[1], dev[2], wpas ]:
  654. sta.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  655. eap="GPSK", identity="gpsk user",
  656. password="abcdefghijklmnop0123456789abcdef", okc=True,
  657. scan_freq="2412")
  658. hostapd.add_ap(apdev[1]['ifname'], params)
  659. bssid2 = apdev[1]['bssid']
  660. logger.info("Roam to AP2")
  661. for sta in [ dev[2], dev[0], wpas, dev[1] ]:
  662. sta.dump_monitor()
  663. sta.scan_for_bss(bssid2, freq="2412")
  664. if "OK" not in sta.request("ROAM " + bssid2):
  665. raise Exception("ROAM command failed")
  666. ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
  667. "CTRL-EVENT-CONNECTED"], timeout=10)
  668. if ev is None:
  669. raise Exception("Roaming with the AP timed out")
  670. if "CTRL-EVENT-EAP-STARTED" in ev:
  671. raise Exception("Unexpected EAP exchange")
  672. pmksa2 = sta.get_pmksa(bssid2)
  673. if pmksa2 is None:
  674. raise Exception("No PMKSA cache entry created")
  675. logger.info("Roam back to AP1")
  676. for sta in [ dev[0], dev[1], dev[2], wpas ]:
  677. sta.dump_monitor()
  678. sta.scan_for_bss(bssid, freq="2412")
  679. sta.request("ROAM " + bssid)
  680. ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
  681. "CTRL-EVENT-CONNECTED"], timeout=10)
  682. if ev is None:
  683. raise Exception("Roaming with the AP timed out")
  684. if "CTRL-EVENT-EAP-STARTED" in ev:
  685. raise Exception("Unexpected EAP exchange")
  686. def test_pmksa_cache_preauth_oom(dev, apdev):
  687. """RSN pre-authentication to generate PMKSA cache entry and OOM"""
  688. try:
  689. _test_pmksa_cache_preauth_oom(dev, apdev)
  690. finally:
  691. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
  692. subprocess.call(['brctl', 'delbr', 'ap-br0'])
  693. def _test_pmksa_cache_preauth_oom(dev, apdev):
  694. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  695. params['bridge'] = 'ap-br0'
  696. hostapd.add_ap(apdev[0]['ifname'], params)
  697. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  698. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  699. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  700. password_hex="0123456789abcdef0123456789abcdef",
  701. bssid=apdev[0]['bssid'])
  702. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  703. params['bridge'] = 'ap-br0'
  704. params['rsn_preauth'] = '1'
  705. params['rsn_preauth_interfaces'] = 'ap-br0'
  706. hapd = hostapd.add_ap(apdev[1]['ifname'], params)
  707. bssid1 = apdev[1]['bssid']
  708. tests = [ (1, "rsn_preauth_receive"),
  709. (2, "rsn_preauth_receive"),
  710. (1, "rsn_preauth_send") ]
  711. for test in tests:
  712. with alloc_fail(hapd, test[0], test[1]):
  713. dev[0].scan_for_bss(bssid1, freq="2412")
  714. if "OK" not in dev[0].request("PREAUTH " + bssid1):
  715. raise Exception("PREAUTH failed")
  716. success = False
  717. count = 0
  718. for i in range(50):
  719. time.sleep(0.1)
  720. pmksa = dev[0].get_pmksa(bssid1)
  721. if pmksa:
  722. success = True
  723. break
  724. state = hapd.request('GET_ALLOC_FAIL')
  725. if state.startswith('0:'):
  726. count += 1
  727. if count > 2:
  728. break
  729. logger.info("PMKSA cache success: " + str(success))
  730. dev[0].request("PMKSA_FLUSH")
  731. dev[0].wait_disconnected()
  732. dev[0].wait_connected()
  733. dev[0].dump_monitor()
  734. def test_pmksa_cache_size_limit(dev, apdev):
  735. """PMKSA cache size limit in wpa_supplicant"""
  736. try:
  737. _test_pmksa_cache_size_limit(dev, apdev)
  738. finally:
  739. try:
  740. hapd = hostapd.HostapdGlobal()
  741. hapd.flush()
  742. hapd.remove(apdev[0]['ifname'])
  743. except:
  744. pass
  745. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  746. bssid = apdev[0]['bssid']
  747. params['bssid'] = bssid
  748. hostapd.add_ap(apdev[0]['ifname'], params)
  749. def _test_pmksa_cache_size_limit(dev, apdev):
  750. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  751. id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  752. eap="GPSK", identity="gpsk user",
  753. password="abcdefghijklmnop0123456789abcdef",
  754. scan_freq="2412", only_add_network=True)
  755. for i in range(33):
  756. bssid = apdev[0]['bssid'][0:15] + "%02x" % i
  757. logger.info("Iteration with BSSID " + bssid)
  758. params['bssid'] = bssid
  759. hostapd.add_ap(apdev[0]['ifname'], params)
  760. dev[0].request("BSS_FLUSH 0")
  761. dev[0].scan_for_bss(bssid, freq=2412, only_new=True)
  762. dev[0].select_network(id)
  763. dev[0].wait_connected()
  764. dev[0].request("DISCONNECT")
  765. dev[0].wait_disconnected()
  766. dev[0].dump_monitor()
  767. entries = len(dev[0].request("PMKSA").splitlines()) - 1
  768. if i == 32:
  769. if entries != 32:
  770. raise Exception("Unexpected number of PMKSA entries after expected removal of the oldest entry")
  771. elif i + 1 != entries:
  772. raise Exception("Unexpected number of PMKSA entries")
  773. hapd = hostapd.HostapdGlobal()
  774. hapd.flush()
  775. hapd.remove(apdev[0]['ifname'])
  776. def test_pmksa_cache_preauth_timeout(dev, apdev):
  777. """RSN pre-authentication timing out"""
  778. try:
  779. _test_pmksa_cache_preauth_timeout(dev, apdev)
  780. finally:
  781. dev[0].request("SET dot11RSNAConfigSATimeout 60")
  782. def _test_pmksa_cache_preauth_timeout(dev, apdev):
  783. dev[0].request("SET dot11RSNAConfigSATimeout 1")
  784. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  785. hostapd.add_ap(apdev[0]['ifname'], params)
  786. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  787. password_hex="0123456789abcdef0123456789abcdef",
  788. bssid=apdev[0]['bssid'])
  789. if "OK" not in dev[0].request("PREAUTH f2:11:22:33:44:55"):
  790. raise Exception("PREAUTH failed")
  791. ev = dev[0].wait_event(["RSN: pre-authentication with"], timeout=5)
  792. if ev is None:
  793. raise Exception("No timeout event seen")
  794. if "timed out" not in ev:
  795. raise Exception("Unexpected event: " + ev)
  796. def test_pmksa_cache_preauth_wpas_oom(dev, apdev):
  797. """RSN pre-authentication OOM in wpa_supplicant"""
  798. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  799. hostapd.add_ap(apdev[0]['ifname'], params)
  800. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  801. password_hex="0123456789abcdef0123456789abcdef",
  802. bssid=apdev[0]['bssid'])
  803. for i in range(1, 11):
  804. with alloc_fail(dev[0], i, "rsn_preauth_init"):
  805. res = dev[0].request("PREAUTH f2:11:22:33:44:55").strip()
  806. logger.info("Iteration %d - PREAUTH command results: %s" % (i, res))
  807. for j in range(10):
  808. state = dev[0].request('GET_ALLOC_FAIL')
  809. if state.startswith('0:'):
  810. break
  811. time.sleep(0.05)