test_sta_dynamic.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. # Dynamic wpa_supplicant interface
  2. # Copyright (c) 2013, 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 subprocess
  9. import time
  10. import hwsim_utils
  11. import hostapd
  12. from wpasupplicant import WpaSupplicant
  13. def test_sta_dynamic(dev, apdev):
  14. """Dynamically added wpa_supplicant interface"""
  15. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  16. hostapd.add_ap(apdev[0], params)
  17. logger.info("Create a dynamic wpa_supplicant interface and connect")
  18. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  19. wpas.interface_add("wlan5")
  20. wpas.connect("sta-dynamic", psk="12345678", scan_freq="2412")
  21. def test_sta_ap_scan_0(dev, apdev):
  22. """Dynamically added wpa_supplicant interface with AP_SCAN 0 connection"""
  23. hostapd.add_ap(apdev[0], { "ssid": "test" })
  24. bssid = apdev[0]['bssid']
  25. logger.info("Create a dynamic wpa_supplicant interface and connect")
  26. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  27. wpas.interface_add("wlan5")
  28. if "OK" not in wpas.request("AP_SCAN 0"):
  29. raise Exception("Failed to set AP_SCAN 2")
  30. id = wpas.connect("", key_mgmt="NONE", bssid=bssid,
  31. only_add_network=True)
  32. wpas.request("ENABLE_NETWORK " + str(id) + " no-connect")
  33. wpas.request("SCAN")
  34. time.sleep(0.5)
  35. subprocess.call(['iw', wpas.ifname, 'connect', 'test', '2412'])
  36. wpas.wait_connected(timeout=10)
  37. wpas.request("SCAN")
  38. wpas.wait_connected(timeout=5)
  39. def test_sta_ap_scan_2(dev, apdev):
  40. """Dynamically added wpa_supplicant interface with AP_SCAN 2 connection"""
  41. hostapd.add_ap(apdev[0], { "ssid": "test" })
  42. bssid = apdev[0]['bssid']
  43. logger.info("Create a dynamic wpa_supplicant interface and connect")
  44. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  45. wpas.interface_add("wlan5")
  46. if "FAIL" not in wpas.request("AP_SCAN -1"):
  47. raise Exception("Invalid AP_SCAN -1 accepted")
  48. if "FAIL" not in wpas.request("AP_SCAN 3"):
  49. raise Exception("Invalid AP_SCAN 3 accepted")
  50. if "OK" not in wpas.request("AP_SCAN 2"):
  51. raise Exception("Failed to set AP_SCAN 2")
  52. id = wpas.connect("", key_mgmt="NONE", bssid=bssid,
  53. only_add_network=True)
  54. wpas.request("ENABLE_NETWORK " + str(id) + " no-connect")
  55. subprocess.call(['iw', wpas.ifname, 'scan', 'trigger', 'freq', '2412'])
  56. time.sleep(1)
  57. subprocess.call(['iw', wpas.ifname, 'connect', 'test', '2412'])
  58. wpas.wait_connected(timeout=10)
  59. wpas.request("SET disallow_aps bssid " + bssid)
  60. wpas.wait_disconnected(timeout=10)
  61. subprocess.call(['iw', wpas.ifname, 'connect', 'test', '2412'])
  62. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  63. if ev is not None:
  64. raise Exception("Unexpected connection reported")
  65. def test_sta_ap_scan_2b(dev, apdev):
  66. """Dynamically added wpa_supplicant interface with AP_SCAN 2 operation"""
  67. hapd = hostapd.add_ap(apdev[0], { "ssid": "test" })
  68. bssid = apdev[0]['bssid']
  69. logger.info("Create a dynamic wpa_supplicant interface and connect")
  70. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  71. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  72. if "OK" not in wpas.request("AP_SCAN 2"):
  73. raise Exception("Failed to set AP_SCAN 2")
  74. id = wpas.connect("test", key_mgmt="NONE", bssid=bssid)
  75. wpas.request("DISCONNECT")
  76. wpas.set_network(id, "disabled", "1")
  77. id2 = wpas.add_network()
  78. wpas.set_network_quoted(id2, "ssid", "test2")
  79. wpas.set_network(id2, "key_mgmt", "NONE")
  80. wpas.set_network(id2, "disabled", "0")
  81. wpas.request("REASSOCIATE")
  82. ev = wpas.wait_event(["CTRL-EVENT-ASSOC-REJECT"], timeout=15)
  83. if ev is None:
  84. raise Exception("Association rejection not reported")
  85. hapd.disable()
  86. wpas.set_network(id, "disabled", "0")
  87. wpas.set_network(id2, "disabled", "1")
  88. for i in range(3):
  89. ev = wpas.wait_event(["CTRL-EVENT-ASSOC-REJECT"], timeout=15)
  90. if ev is None:
  91. raise Exception("Association rejection not reported")
  92. wpas.request("DISCONNECT")
  93. def test_sta_dynamic_down_up(dev, apdev):
  94. """Dynamically added wpa_supplicant interface down/up"""
  95. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  96. hapd = hostapd.add_ap(apdev[0], params)
  97. logger.info("Create a dynamic wpa_supplicant interface and connect")
  98. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  99. wpas.interface_add("wlan5")
  100. wpas.connect("sta-dynamic", psk="12345678", scan_freq="2412")
  101. hwsim_utils.test_connectivity(wpas, hapd)
  102. subprocess.call(['ifconfig', wpas.ifname, 'down'])
  103. wpas.wait_disconnected(timeout=10)
  104. if wpas.get_status_field("wpa_state") != "INTERFACE_DISABLED":
  105. raise Exception("Unexpected wpa_state")
  106. subprocess.call(['ifconfig', wpas.ifname, 'up'])
  107. wpas.wait_connected(timeout=15, error="Reconnection not reported")
  108. hwsim_utils.test_connectivity(wpas, hapd)
  109. def test_sta_dynamic_ext_mac_addr_change(dev, apdev):
  110. """Dynamically added wpa_supplicant interface with external MAC address change"""
  111. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  112. hapd = hostapd.add_ap(apdev[0], params)
  113. logger.info("Create a dynamic wpa_supplicant interface and connect")
  114. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  115. wpas.interface_add("wlan5")
  116. wpas.connect("sta-dynamic", psk="12345678", scan_freq="2412")
  117. hwsim_utils.test_connectivity(wpas, hapd)
  118. subprocess.call(['ifconfig', wpas.ifname, 'down'])
  119. wpas.wait_disconnected(timeout=10)
  120. if wpas.get_status_field("wpa_state") != "INTERFACE_DISABLED":
  121. raise Exception("Unexpected wpa_state")
  122. prev_addr = wpas.p2p_interface_addr()
  123. new_addr = '02:11:22:33:44:55'
  124. try:
  125. subprocess.call(['ip', 'link', 'set', 'dev', wpas.ifname,
  126. 'address', new_addr])
  127. subprocess.call(['ifconfig', wpas.ifname, 'up'])
  128. wpas.wait_connected(timeout=15, error="Reconnection not reported")
  129. if wpas.get_driver_status_field('addr') != new_addr:
  130. raise Exception("Address change not reported")
  131. hwsim_utils.test_connectivity(wpas, hapd)
  132. sta = hapd.get_sta(new_addr)
  133. if sta['addr'] != new_addr:
  134. raise Exception("STA association with new address not found")
  135. finally:
  136. subprocess.call(['ifconfig', wpas.ifname, 'down'])
  137. subprocess.call(['ip', 'link', 'set', 'dev', wpas.ifname,
  138. 'address', prev_addr])
  139. subprocess.call(['ifconfig', wpas.ifname, 'up'])
  140. def test_sta_dynamic_random_mac_addr(dev, apdev):
  141. """Dynamically added wpa_supplicant interface and random MAC address"""
  142. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  143. hapd = hostapd.add_ap(apdev[0], params)
  144. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  145. wpas.interface_add("wlan5")
  146. addr0 = wpas.get_driver_status_field("addr")
  147. wpas.request("SET preassoc_mac_addr 1")
  148. wpas.request("SET rand_addr_lifetime 0")
  149. id = wpas.connect("sta-dynamic", psk="12345678", mac_addr="1",
  150. scan_freq="2412")
  151. addr1 = wpas.get_driver_status_field("addr")
  152. if addr0 == addr1:
  153. raise Exception("Random MAC address not used")
  154. sta = hapd.get_sta(addr0)
  155. if sta['addr'] != "FAIL":
  156. raise Exception("Unexpected STA association with permanent address")
  157. sta = hapd.get_sta(addr1)
  158. if sta['addr'] != addr1:
  159. raise Exception("STA association with random address not found")
  160. wpas.request("DISCONNECT")
  161. wpas.connect_network(id)
  162. addr2 = wpas.get_driver_status_field("addr")
  163. if addr1 != addr2:
  164. raise Exception("Random MAC address changed unexpectedly")
  165. wpas.remove_network(id)
  166. id = wpas.connect("sta-dynamic", psk="12345678", mac_addr="1",
  167. scan_freq="2412")
  168. addr2 = wpas.get_driver_status_field("addr")
  169. if addr1 == addr2:
  170. raise Exception("Random MAC address did not change")
  171. def test_sta_dynamic_random_mac_addr_keep_oui(dev, apdev):
  172. """Dynamically added wpa_supplicant interface and random MAC address (keep OUI)"""
  173. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  174. hapd = hostapd.add_ap(apdev[0], params)
  175. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  176. wpas.interface_add("wlan5")
  177. addr0 = wpas.get_driver_status_field("addr")
  178. wpas.request("SET preassoc_mac_addr 2")
  179. wpas.request("SET rand_addr_lifetime 0")
  180. id = wpas.connect("sta-dynamic", psk="12345678", mac_addr="2",
  181. scan_freq="2412")
  182. addr1 = wpas.get_driver_status_field("addr")
  183. if addr0 == addr1:
  184. raise Exception("Random MAC address not used")
  185. if addr1[3:8] != addr0[3:8]:
  186. raise Exception("OUI was not kept")
  187. sta = hapd.get_sta(addr0)
  188. if sta['addr'] != "FAIL":
  189. raise Exception("Unexpected STA association with permanent address")
  190. sta = hapd.get_sta(addr1)
  191. if sta['addr'] != addr1:
  192. raise Exception("STA association with random address not found")
  193. wpas.request("DISCONNECT")
  194. wpas.connect_network(id)
  195. addr2 = wpas.get_driver_status_field("addr")
  196. if addr1 != addr2:
  197. raise Exception("Random MAC address changed unexpectedly")
  198. wpas.remove_network(id)
  199. id = wpas.connect("sta-dynamic", psk="12345678", mac_addr="2",
  200. scan_freq="2412")
  201. addr2 = wpas.get_driver_status_field("addr")
  202. if addr1 == addr2:
  203. raise Exception("Random MAC address did not change")
  204. if addr2[3:8] != addr0[3:8]:
  205. raise Exception("OUI was not kept")
  206. def test_sta_dynamic_random_mac_addr_scan(dev, apdev):
  207. """Dynamically added wpa_supplicant interface and random MAC address for scan"""
  208. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  209. hapd = hostapd.add_ap(apdev[0], params)
  210. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  211. wpas.interface_add("wlan5")
  212. addr0 = wpas.get_driver_status_field("addr")
  213. wpas.request("SET preassoc_mac_addr 1")
  214. wpas.request("SET rand_addr_lifetime 0")
  215. id = wpas.connect("sta-dynamic", psk="12345678", scan_freq="2412")
  216. addr1 = wpas.get_driver_status_field("addr")
  217. if addr0 != addr1:
  218. raise Exception("Random MAC address used unexpectedly")
  219. def test_sta_dynamic_random_mac_addr_scan_keep_oui(dev, apdev):
  220. """Dynamically added wpa_supplicant interface and random MAC address for scan (keep OUI)"""
  221. params = hostapd.wpa2_params(ssid="sta-dynamic", passphrase="12345678")
  222. hapd = hostapd.add_ap(apdev[0], params)
  223. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  224. wpas.interface_add("wlan5")
  225. addr0 = wpas.get_driver_status_field("addr")
  226. wpas.request("SET preassoc_mac_addr 2")
  227. wpas.request("SET rand_addr_lifetime 0")
  228. id = wpas.connect("sta-dynamic", psk="12345678", scan_freq="2412")
  229. addr1 = wpas.get_driver_status_field("addr")
  230. if addr0 != addr1:
  231. raise Exception("Random MAC address used unexpectedly")