test_wext.py 9.7 KB

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