test_wext.py 9.3 KB

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