test_wext.py 9.5 KB

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