test_wext.py 10 KB

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