test_pmksa_cache.py 46 KB

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