test_pmksa_cache.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 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]['ifname'], params)
  363. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  364. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  365. eap_connect(dev[0], apdev[0], "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]['ifname'], 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. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
  420. stderr=open('/dev/null', 'w'))
  421. subprocess.call(['brctl', 'delbr', 'ap-br0'],
  422. stderr=open('/dev/null', 'w'))
  423. def test_pmksa_cache_preauth(dev, apdev):
  424. """RSN pre-authentication to generate PMKSA cache entry"""
  425. generic_pmksa_cache_preauth(dev, apdev, None,
  426. "pax.user@example.com", "ap-br0")
  427. def test_pmksa_cache_preauth_per_sta_vif(dev, apdev):
  428. """RSN pre-authentication to generate PMKSA cache entry with per_sta_vif"""
  429. extraparams = [{}, {}]
  430. extraparams[0]['per_sta_vif'] = "1"
  431. extraparams[1]['per_sta_vif'] = "1"
  432. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  433. "pax.user@example.com", "ap-br0")
  434. def test_pmksa_cache_preauth_vlan_enabled(dev, apdev):
  435. """RSN pre-authentication to generate PMKSA cache entry (dynamic_vlan optional but station without VLAN set)"""
  436. extraparams = [{}, {}]
  437. extraparams[0]['dynamic_vlan'] = '1'
  438. extraparams[1]['dynamic_vlan'] = '1'
  439. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  440. "pax.user@example.com", "ap-br0")
  441. def test_pmksa_cache_preauth_vlan_enabled_per_sta_vif(dev, apdev):
  442. """RSN pre-authentication to generate PMKSA cache entry (dynamic_vlan optional but station without VLAN set, with per_sta_vif enabled)"""
  443. extraparams = [{}, {}]
  444. extraparams[0]['per_sta_vif'] = "1"
  445. extraparams[1]['per_sta_vif'] = "1"
  446. extraparams[0]['dynamic_vlan'] = '1'
  447. extraparams[1]['dynamic_vlan'] = '1'
  448. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  449. "pax.user@example.com", "ap-br0")
  450. def test_pmksa_cache_preauth_vlan_used(dev, apdev):
  451. """RSN pre-authentication to generate PMKSA cache entry (station with VLAN set)"""
  452. run_pmksa_cache_preauth_vlan_used(dev, apdev, None, force_disconnect=True)
  453. def run_pmksa_cache_preauth_vlan_used(dev, apdev, extraparams=None,
  454. force_disconnect=False):
  455. try:
  456. subprocess.call(['brctl', 'addbr', 'brvlan1'])
  457. subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
  458. if not extraparams:
  459. extraparams = [{}, {}]
  460. extraparams[0]['dynamic_vlan'] = '1'
  461. extraparams[0]['vlan_file'] = 'hostapd.wlan3.vlan'
  462. extraparams[1]['dynamic_vlan'] = '1'
  463. extraparams[1]['vlan_file'] = 'hostapd.wlan4.vlan'
  464. generic_pmksa_cache_preauth(dev, apdev, extraparams,
  465. "vlan1", "brvlan1",
  466. force_disconnect=force_disconnect)
  467. finally:
  468. subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
  469. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
  470. stderr=open('/dev/null', 'w'))
  471. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan4.1', 'down'],
  472. stderr=open('/dev/null', 'w'))
  473. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
  474. stderr=open('/dev/null', 'w'))
  475. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan4.1'],
  476. stderr=open('/dev/null', 'w'))
  477. subprocess.call(['brctl', 'delbr', 'brvlan1'])
  478. def test_pmksa_cache_preauth_vlan_used_per_sta_vif(dev, apdev):
  479. """RSN pre-authentication to generate PMKSA cache entry (station with VLAN set, per_sta_vif=1)"""
  480. extraparams = [{}, {}]
  481. extraparams[0]['per_sta_vif'] = "1"
  482. extraparams[1]['per_sta_vif'] = "1"
  483. run_pmksa_cache_preauth_vlan_used(dev, apdev, extraparams)
  484. def test_pmksa_cache_disabled(dev, apdev):
  485. """PMKSA cache disabling on AP"""
  486. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  487. params['disable_pmksa_caching'] = '1'
  488. hostapd.add_ap(apdev[0]['ifname'], params)
  489. bssid = apdev[0]['bssid']
  490. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  491. eap="GPSK", identity="gpsk user",
  492. password="abcdefghijklmnop0123456789abcdef",
  493. scan_freq="2412")
  494. hostapd.add_ap(apdev[1]['ifname'], params)
  495. bssid2 = apdev[1]['bssid']
  496. dev[0].dump_monitor()
  497. logger.info("Roam to AP2")
  498. dev[0].scan_for_bss(bssid2, freq="2412")
  499. dev[0].request("ROAM " + bssid2)
  500. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  501. if ev is None:
  502. raise Exception("EAP success timed out")
  503. dev[0].wait_connected(timeout=10, error="Roaming timed out")
  504. dev[0].dump_monitor()
  505. logger.info("Roam back to AP1")
  506. dev[0].scan(freq="2412")
  507. dev[0].request("ROAM " + bssid)
  508. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  509. "CTRL-EVENT-CONNECTED"], timeout=20)
  510. if ev is None:
  511. raise Exception("Roaming with the AP timed out")
  512. if "CTRL-EVENT-CONNECTED" in ev:
  513. raise Exception("EAP exchange missing")
  514. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
  515. if ev is None:
  516. raise Exception("Roaming with the AP timed out")
  517. def test_pmksa_cache_ap_expiration(dev, apdev):
  518. """PMKSA cache entry expiring on AP"""
  519. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  520. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  521. bssid = apdev[0]['bssid']
  522. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  523. eap="GPSK", identity="gpsk-user-session-timeout",
  524. password="abcdefghijklmnop0123456789abcdef",
  525. scan_freq="2412")
  526. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  527. if ev is None:
  528. raise Exception("No connection event received from hostapd")
  529. dev[0].request("DISCONNECT")
  530. time.sleep(5)
  531. dev[0].dump_monitor()
  532. dev[0].request("RECONNECT")
  533. ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
  534. "CTRL-EVENT-CONNECTED"], timeout=20)
  535. if ev is None:
  536. raise Exception("Roaming with the AP timed out")
  537. if "CTRL-EVENT-CONNECTED" in ev:
  538. raise Exception("EAP exchange missing")
  539. dev[0].wait_connected(timeout=20, error="Reconnect timed out")
  540. dev[0].dump_monitor()
  541. dev[0].wait_disconnected(timeout=20)
  542. dev[0].wait_connected(timeout=20, error="Reassociation timed out")
  543. def test_pmksa_cache_multiple_sta(dev, apdev):
  544. """PMKSA cache with multiple stations"""
  545. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  546. hostapd.add_ap(apdev[0]['ifname'], params)
  547. bssid = apdev[0]['bssid']
  548. dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  549. eap="GPSK", identity="gpsk-user-session-timeout",
  550. password="abcdefghijklmnop0123456789abcdef",
  551. scan_freq="2412")
  552. dev[1].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  553. eap="GPSK", identity="gpsk user",
  554. password="abcdefghijklmnop0123456789abcdef",
  555. scan_freq="2412")
  556. dev[2].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  557. eap="GPSK", identity="gpsk-user-session-timeout",
  558. password="abcdefghijklmnop0123456789abcdef",
  559. scan_freq="2412")
  560. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  561. wpas.interface_add("wlan5")
  562. wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  563. eap="GPSK", identity="gpsk user",
  564. password="abcdefghijklmnop0123456789abcdef",
  565. scan_freq="2412")
  566. hostapd.add_ap(apdev[1]['ifname'], params)
  567. bssid2 = apdev[1]['bssid']
  568. logger.info("Roam to AP2")
  569. for sta in [ dev[1], dev[0], dev[2], wpas ]:
  570. sta.dump_monitor()
  571. sta.scan_for_bss(bssid2, freq="2412")
  572. sta.request("ROAM " + bssid2)
  573. ev = sta.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  574. if ev is None:
  575. raise Exception("EAP success timed out")
  576. sta.wait_connected(timeout=10, error="Roaming timed out")
  577. logger.info("Roam back to AP1")
  578. for sta in [ dev[1], wpas, dev[0], dev[2] ]:
  579. sta.dump_monitor()
  580. sta.scan(freq="2412")
  581. sta.dump_monitor()
  582. sta.request("ROAM " + bssid)
  583. sta.wait_connected(timeout=10, error="Roaming timed out")
  584. sta.dump_monitor()
  585. time.sleep(4)
  586. logger.info("Roam back to AP2")
  587. for sta in [ dev[1], wpas, dev[0], dev[2] ]:
  588. sta.dump_monitor()
  589. sta.scan(freq="2412")
  590. sta.dump_monitor()
  591. sta.request("ROAM " + bssid2)
  592. sta.wait_connected(timeout=10, error="Roaming timed out")
  593. sta.dump_monitor()
  594. def test_pmksa_cache_opportunistic_multiple_sta(dev, apdev):
  595. """Opportunistic PMKSA caching with multiple stations"""
  596. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  597. params['okc'] = "1"
  598. hostapd.add_ap(apdev[0]['ifname'], params)
  599. bssid = apdev[0]['bssid']
  600. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  601. wpas.interface_add("wlan5")
  602. for sta in [ dev[0], dev[1], dev[2], wpas ]:
  603. sta.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  604. eap="GPSK", identity="gpsk user",
  605. password="abcdefghijklmnop0123456789abcdef", okc=True,
  606. scan_freq="2412")
  607. hostapd.add_ap(apdev[1]['ifname'], params)
  608. bssid2 = apdev[1]['bssid']
  609. logger.info("Roam to AP2")
  610. for sta in [ dev[2], dev[0], wpas, dev[1] ]:
  611. sta.dump_monitor()
  612. sta.scan_for_bss(bssid2, freq="2412")
  613. if "OK" not in sta.request("ROAM " + bssid2):
  614. raise Exception("ROAM command failed")
  615. ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
  616. "CTRL-EVENT-CONNECTED"], timeout=10)
  617. if ev is None:
  618. raise Exception("Roaming with the AP timed out")
  619. if "CTRL-EVENT-EAP-STARTED" in ev:
  620. raise Exception("Unexpected EAP exchange")
  621. pmksa2 = sta.get_pmksa(bssid2)
  622. if pmksa2 is None:
  623. raise Exception("No PMKSA cache entry created")
  624. logger.info("Roam back to AP1")
  625. for sta in [ dev[0], dev[1], dev[2], wpas ]:
  626. sta.dump_monitor()
  627. sta.scan_for_bss(bssid, freq="2412")
  628. sta.request("ROAM " + bssid)
  629. ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
  630. "CTRL-EVENT-CONNECTED"], timeout=10)
  631. if ev is None:
  632. raise Exception("Roaming with the AP timed out")
  633. if "CTRL-EVENT-EAP-STARTED" in ev:
  634. raise Exception("Unexpected EAP exchange")
  635. def test_pmksa_cache_preauth_oom(dev, apdev):
  636. """RSN pre-authentication to generate PMKSA cache entry and OOM"""
  637. try:
  638. _test_pmksa_cache_preauth_oom(dev, apdev)
  639. finally:
  640. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
  641. subprocess.call(['brctl', 'delbr', 'ap-br0'])
  642. def _test_pmksa_cache_preauth_oom(dev, apdev):
  643. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  644. params['bridge'] = 'ap-br0'
  645. hostapd.add_ap(apdev[0]['ifname'], params)
  646. subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
  647. subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  648. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  649. password_hex="0123456789abcdef0123456789abcdef",
  650. bssid=apdev[0]['bssid'])
  651. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  652. params['bridge'] = 'ap-br0'
  653. params['rsn_preauth'] = '1'
  654. params['rsn_preauth_interfaces'] = 'ap-br0'
  655. hapd = hostapd.add_ap(apdev[1]['ifname'], params)
  656. bssid1 = apdev[1]['bssid']
  657. tests = [ (1, "rsn_preauth_receive"),
  658. (2, "rsn_preauth_receive"),
  659. (1, "rsn_preauth_send") ]
  660. for test in tests:
  661. with alloc_fail(hapd, test[0], test[1]):
  662. dev[0].scan_for_bss(bssid1, freq="2412")
  663. if "OK" not in dev[0].request("PREAUTH " + bssid1):
  664. raise Exception("PREAUTH failed")
  665. success = False
  666. count = 0
  667. for i in range(50):
  668. time.sleep(0.1)
  669. pmksa = dev[0].get_pmksa(bssid1)
  670. if pmksa:
  671. success = True
  672. break
  673. state = hapd.request('GET_ALLOC_FAIL')
  674. if state.startswith('0:'):
  675. count += 1
  676. if count > 2:
  677. break
  678. logger.info("PMKSA cache success: " + str(success))
  679. dev[0].request("PMKSA_FLUSH")
  680. dev[0].wait_disconnected()
  681. dev[0].wait_connected()
  682. dev[0].dump_monitor()
  683. def test_pmksa_cache_size_limit(dev, apdev):
  684. """PMKSA cache size limit in wpa_supplicant"""
  685. try:
  686. _test_pmksa_cache_size_limit(dev, apdev)
  687. finally:
  688. try:
  689. hapd = hostapd.HostapdGlobal()
  690. hapd.flush()
  691. hapd.remove(apdev[0]['ifname'])
  692. except:
  693. pass
  694. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  695. bssid = apdev[0]['bssid']
  696. params['bssid'] = bssid
  697. hostapd.add_ap(apdev[0]['ifname'], params)
  698. def _test_pmksa_cache_size_limit(dev, apdev):
  699. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  700. id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  701. eap="GPSK", identity="gpsk user",
  702. password="abcdefghijklmnop0123456789abcdef",
  703. scan_freq="2412", only_add_network=True)
  704. for i in range(33):
  705. bssid = apdev[0]['bssid'][0:15] + "%02x" % i
  706. logger.info("Iteration with BSSID " + bssid)
  707. params['bssid'] = bssid
  708. hostapd.add_ap(apdev[0]['ifname'], params)
  709. dev[0].request("BSS_FLUSH 0")
  710. dev[0].scan_for_bss(bssid, freq=2412, only_new=True)
  711. dev[0].select_network(id)
  712. dev[0].wait_connected()
  713. dev[0].request("DISCONNECT")
  714. dev[0].wait_disconnected()
  715. dev[0].dump_monitor()
  716. entries = len(dev[0].request("PMKSA").splitlines()) - 1
  717. if i == 32:
  718. if entries != 32:
  719. raise Exception("Unexpected number of PMKSA entries after expected removal of the oldest entry")
  720. elif i + 1 != entries:
  721. raise Exception("Unexpected number of PMKSA entries")
  722. hapd = hostapd.HostapdGlobal()
  723. hapd.flush()
  724. hapd.remove(apdev[0]['ifname'])
  725. def test_pmksa_cache_preauth_timeout(dev, apdev):
  726. """RSN pre-authentication timing out"""
  727. try:
  728. _test_pmksa_cache_preauth_timeout(dev, apdev)
  729. finally:
  730. dev[0].request("SET dot11RSNAConfigSATimeout 60")
  731. def _test_pmksa_cache_preauth_timeout(dev, apdev):
  732. dev[0].request("SET dot11RSNAConfigSATimeout 1")
  733. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  734. hostapd.add_ap(apdev[0]['ifname'], params)
  735. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  736. password_hex="0123456789abcdef0123456789abcdef",
  737. bssid=apdev[0]['bssid'])
  738. if "OK" not in dev[0].request("PREAUTH f2:11:22:33:44:55"):
  739. raise Exception("PREAUTH failed")
  740. ev = dev[0].wait_event(["RSN: pre-authentication with"], timeout=5)
  741. if ev is None:
  742. raise Exception("No timeout event seen")
  743. if "timed out" not in ev:
  744. raise Exception("Unexpected event: " + ev)
  745. def test_pmksa_cache_preauth_wpas_oom(dev, apdev):
  746. """RSN pre-authentication OOM in wpa_supplicant"""
  747. params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
  748. hostapd.add_ap(apdev[0]['ifname'], params)
  749. eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
  750. password_hex="0123456789abcdef0123456789abcdef",
  751. bssid=apdev[0]['bssid'])
  752. for i in range(1, 11):
  753. with alloc_fail(dev[0], i, "rsn_preauth_init"):
  754. res = dev[0].request("PREAUTH f2:11:22:33:44:55").strip()
  755. logger.info("Iteration %d - PREAUTH command results: %s" % (i, res))
  756. for j in range(10):
  757. state = dev[0].request('GET_ALLOC_FAIL')
  758. if state.startswith('0:'):
  759. break
  760. time.sleep(0.05)