test_pmksa_cache.py 36 KB

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