test_wext.py 9.2 KB

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