test_wext.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Deprecated WEXT driver interface in wpa_supplicant
  2. # Copyright (c) 2013-2015, 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 os
  9. import hostapd
  10. import hwsim_utils
  11. from wpasupplicant import WpaSupplicant
  12. from utils import HwsimSkip, skip_with_fips
  13. from test_rfkill import get_rfkill
  14. def get_wext_interface():
  15. if not os.path.exists("/proc/net/wireless"):
  16. raise HwsimSkip("WEXT support not included in the kernel")
  17. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  18. try:
  19. wpas.interface_add("wlan5", driver="wext")
  20. except Exception, e:
  21. wpas.close_ctrl()
  22. raise HwsimSkip("WEXT driver support not included in wpa_supplicant")
  23. return wpas
  24. def test_wext_open(dev, apdev):
  25. """WEXT driver interface with open network"""
  26. wpas = get_wext_interface()
  27. params = { "ssid": "wext-open" }
  28. hapd = hostapd.add_ap(apdev[0], params)
  29. wpas.connect("wext-open", key_mgmt="NONE")
  30. hwsim_utils.test_connectivity(wpas, hapd)
  31. def test_wext_wpa2_psk(dev, apdev):
  32. """WEXT driver interface with WPA2-PSK"""
  33. wpas = get_wext_interface()
  34. params = hostapd.wpa2_params(ssid="wext-wpa2-psk", passphrase="12345678")
  35. hapd = hostapd.add_ap(apdev[0], params)
  36. wpas.connect("wext-wpa2-psk", psk="12345678")
  37. hwsim_utils.test_connectivity(wpas, hapd)
  38. if "RSSI=" not in wpas.request("SIGNAL_POLL"):
  39. raise Exception("Missing RSSI from SIGNAL_POLL")
  40. wpas.dump_monitor()
  41. hapd.request("DEAUTHENTICATE " + wpas.p2p_interface_addr())
  42. wpas.wait_disconnected(timeout=15)
  43. def test_wext_wpa_psk(dev, apdev):
  44. """WEXT driver interface with WPA-PSK"""
  45. skip_with_fips(dev[0])
  46. wpas = get_wext_interface()
  47. params = hostapd.wpa_params(ssid="wext-wpa-psk", passphrase="12345678")
  48. hapd = hostapd.add_ap(apdev[0], params)
  49. testfile = "/sys/kernel/debug/ieee80211/%s/netdev:%s/tkip_mic_test" % (hapd.get_driver_status_field("phyname"), apdev[0]['ifname'])
  50. if not os.path.exists(testfile):
  51. wpas.close_ctrl()
  52. raise HwsimSkip("tkip_mic_test not supported in mac80211")
  53. wpas.connect("wext-wpa-psk", psk="12345678")
  54. hwsim_utils.test_connectivity(wpas, hapd)
  55. with open(testfile, "w") as f:
  56. f.write(wpas.p2p_interface_addr())
  57. ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=1)
  58. if ev is not None:
  59. raise Exception("Unexpected disconnection on first Michael MIC failure")
  60. with open(testfile, "w") as f:
  61. f.write("ff:ff:ff:ff:ff:ff")
  62. ev = wpas.wait_disconnected(timeout=10,
  63. error="No disconnection after two Michael MIC failures")
  64. if "reason=14 locally_generated=1" not in ev:
  65. raise Exception("Unexpected disconnection reason: " + ev)
  66. def test_wext_pmksa_cache(dev, apdev):
  67. """PMKSA caching with WEXT"""
  68. wpas = get_wext_interface()
  69. params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
  70. hostapd.add_ap(apdev[0], params)
  71. bssid = apdev[0]['bssid']
  72. wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
  73. eap="GPSK", identity="gpsk user",
  74. password="abcdefghijklmnop0123456789abcdef",
  75. scan_freq="2412")
  76. pmksa = wpas.get_pmksa(bssid)
  77. if pmksa is None:
  78. raise Exception("No PMKSA cache entry created")
  79. if pmksa['opportunistic'] != '0':
  80. raise Exception("Unexpected opportunistic PMKSA cache entry")
  81. hostapd.add_ap(apdev[1], params)
  82. bssid2 = apdev[1]['bssid']
  83. wpas.dump_monitor()
  84. logger.info("Roam to AP2")
  85. # It can take some time for the second AP to become ready to reply to Probe
  86. # Request frames especially under heavy CPU load, so allow couple of rounds
  87. # of scanning to avoid reporting errors incorrectly just because of scans
  88. # not having seen the target AP.
  89. for i in range(3):
  90. wpas.scan()
  91. if wpas.get_bss(bssid2) is not None:
  92. break
  93. logger.info("Scan again to find target AP")
  94. wpas.request("ROAM " + bssid2)
  95. ev = wpas.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
  96. if ev is None:
  97. raise Exception("EAP success timed out")
  98. wpas.wait_connected(timeout=10, error="Roaming timed out")
  99. pmksa2 = wpas.get_pmksa(bssid2)
  100. if pmksa2 is None:
  101. raise Exception("No PMKSA cache entry found")
  102. if pmksa2['opportunistic'] != '0':
  103. raise Exception("Unexpected opportunistic PMKSA cache entry")
  104. wpas.dump_monitor()
  105. logger.info("Roam back to AP1")
  106. wpas.scan()
  107. wpas.request("ROAM " + bssid)
  108. ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
  109. "CTRL-EVENT-CONNECTED"], timeout=15)
  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. pmksa1b = wpas.get_pmksa(bssid)
  115. if pmksa1b is None:
  116. raise Exception("No PMKSA cache entry found")
  117. if pmksa['pmkid'] != pmksa1b['pmkid']:
  118. raise Exception("Unexpected PMKID change for AP1")
  119. wpas.dump_monitor()
  120. if "FAIL" in wpas.request("PMKSA_FLUSH"):
  121. raise Exception("PMKSA_FLUSH failed")
  122. if wpas.get_pmksa(bssid) is not None or wpas.get_pmksa(bssid2) is not None:
  123. raise Exception("PMKSA_FLUSH did not remove PMKSA entries")
  124. wpas.wait_disconnected(timeout=5)
  125. wpas.wait_connected(timeout=15, error="Reconnection timed out")
  126. def test_wext_wep_open_auth(dev, apdev):
  127. """WEP Open System authentication"""
  128. wpas = get_wext_interface()
  129. hapd = hostapd.add_ap(apdev[0],
  130. { "ssid": "wep-open",
  131. "wep_key0": '"hello"' })
  132. wpas.connect("wep-open", key_mgmt="NONE", wep_key0='"hello"',
  133. scan_freq="2412")
  134. hwsim_utils.test_connectivity(wpas, hapd)
  135. if "[WEP]" not in wpas.request("SCAN_RESULTS"):
  136. raise Exception("WEP flag not indicated in scan results")
  137. def test_wext_wep_shared_key_auth(dev, apdev):
  138. """WEP Shared Key authentication"""
  139. wpas = get_wext_interface()
  140. hapd = hostapd.add_ap(apdev[0],
  141. { "ssid": "wep-shared-key",
  142. "wep_key0": '"hello12345678"',
  143. "auth_algs": "2" })
  144. wpas.connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
  145. wep_key0='"hello12345678"', scan_freq="2412")
  146. hwsim_utils.test_connectivity(wpas, hapd)
  147. wpas.request("REMOVE_NETWORK all")
  148. wpas.wait_disconnected(timeout=5)
  149. wpas.connect("wep-shared-key", key_mgmt="NONE", auth_alg="OPEN SHARED",
  150. wep_key0='"hello12345678"', scan_freq="2412")
  151. def test_wext_pmf(dev, apdev):
  152. """WEXT driver interface with WPA2-PSK and PMF"""
  153. wpas = get_wext_interface()
  154. params = hostapd.wpa2_params(ssid="wext-wpa2-psk", passphrase="12345678")
  155. params["wpa_key_mgmt"] = "WPA-PSK-SHA256"
  156. params["ieee80211w"] = "2"
  157. hapd = hostapd.add_ap(apdev[0], params)
  158. wpas.connect("wext-wpa2-psk", psk="12345678", ieee80211w="1",
  159. key_mgmt="WPA-PSK WPA-PSK-SHA256", proto="WPA2",
  160. scan_freq="2412")
  161. hwsim_utils.test_connectivity(wpas, hapd)
  162. addr = wpas.p2p_interface_addr()
  163. hapd.request("DEAUTHENTICATE " + addr)
  164. wpas.wait_disconnected(timeout=5)
  165. def test_wext_scan_hidden(dev, apdev):
  166. """WEXT with hidden SSID"""
  167. wpas = get_wext_interface()
  168. hapd = hostapd.add_ap(apdev[0], { "ssid": "test-scan",
  169. "ignore_broadcast_ssid": "1" })
  170. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "test-scan2",
  171. "ignore_broadcast_ssid": "1" })
  172. id1 = wpas.connect("test-scan", key_mgmt="NONE", scan_ssid="1",
  173. only_add_network=True)
  174. wpas.request("SCAN scan_id=%d" % id1)
  175. ev = wpas.wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  176. if ev is None:
  177. raise Exception("Scan did not complete")
  178. if "test-scan" not in wpas.request("SCAN_RESULTS"):
  179. raise Exception("Did not find hidden SSID in scan")
  180. id = wpas.connect("test-scan2", key_mgmt="NONE", scan_ssid="1",
  181. only_add_network=True)
  182. wpas.connect_network(id, timeout=30)
  183. wpas.request("DISCONNECT")
  184. hapd2.disable()
  185. hapd.disable()
  186. wpas.interface_remove("wlan5")
  187. wpas.interface_add("wlan5")
  188. wpas.flush_scan_cache(freq=2412)
  189. wpas.flush_scan_cache()
  190. def test_wext_rfkill(dev, apdev):
  191. """WEXT and rfkill block/unblock"""
  192. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  193. wpas.interface_add("wlan5")
  194. rfk = get_rfkill(wpas)
  195. wpas.interface_remove("wlan5")
  196. wpas = get_wext_interface()
  197. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  198. wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
  199. try:
  200. logger.info("rfkill block")
  201. rfk.block()
  202. wpas.wait_disconnected(timeout=10,
  203. error="Missing disconnection event on rfkill block")
  204. logger.info("rfkill unblock")
  205. rfk.unblock()
  206. wpas.wait_connected(timeout=20,
  207. error="Missing connection event on rfkill unblock")
  208. hwsim_utils.test_connectivity(wpas, hapd)
  209. finally:
  210. rfk.unblock()