test_ap_open.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # Open mode AP tests
  2. # Copyright (c) 2014, Qualcomm Atheros, Inc.
  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 struct
  9. import subprocess
  10. import time
  11. import hostapd
  12. import hwsim_utils
  13. from utils import alloc_fail
  14. from wpasupplicant import WpaSupplicant
  15. def test_ap_open(dev, apdev):
  16. """AP with open mode (no security) configuration"""
  17. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  18. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  19. bg_scan_period="0")
  20. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  21. if ev is None:
  22. raise Exception("No connection event received from hostapd")
  23. hwsim_utils.test_connectivity(dev[0], hapd)
  24. dev[0].request("DISCONNECT")
  25. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  26. if ev is None:
  27. raise Exception("No disconnection event received from hostapd")
  28. def test_ap_open_packet_loss(dev, apdev):
  29. """AP with open mode configuration and large packet loss"""
  30. params = { "ssid": "open",
  31. "ignore_probe_probability": "0.5",
  32. "ignore_auth_probability": "0.5",
  33. "ignore_assoc_probability": "0.5",
  34. "ignore_reassoc_probability": "0.5" }
  35. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  36. for i in range(0, 3):
  37. dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
  38. wait_connect=False)
  39. for i in range(0, 3):
  40. dev[i].wait_connected(timeout=20)
  41. def test_ap_open_unknown_action(dev, apdev):
  42. """AP with open mode configuration and unknown Action frame"""
  43. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  44. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  45. bssid = apdev[0]['bssid']
  46. cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
  47. if "FAIL" in dev[0].request(cmd):
  48. raise Exception("Could not send test Action frame")
  49. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  50. if ev is None:
  51. raise Exception("Timeout on MGMT-TX-STATUS")
  52. if "result=SUCCESS" not in ev:
  53. raise Exception("AP did not ack Action frame")
  54. def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
  55. """Reconnect to open mode AP after inactivity related disconnection"""
  56. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  57. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  58. hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
  59. dev[0].wait_disconnected(timeout=5)
  60. dev[0].wait_connected(timeout=2, error="Timeout on reconnection")
  61. def test_ap_open_assoc_timeout(dev, apdev):
  62. """AP timing out association"""
  63. ssid = "test"
  64. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  65. dev[0].scan(freq="2412")
  66. hapd.set("ext_mgmt_frame_handling", "1")
  67. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  68. wait_connect=False)
  69. for i in range(0, 10):
  70. req = hapd.mgmt_rx()
  71. if req is None:
  72. raise Exception("MGMT RX wait timed out")
  73. if req['subtype'] == 11:
  74. break
  75. req = None
  76. if not req:
  77. raise Exception("Authentication frame not received")
  78. resp = {}
  79. resp['fc'] = req['fc']
  80. resp['da'] = req['sa']
  81. resp['sa'] = req['da']
  82. resp['bssid'] = req['bssid']
  83. resp['payload'] = struct.pack('<HHH', 0, 2, 0)
  84. hapd.mgmt_tx(resp)
  85. assoc = 0
  86. for i in range(0, 10):
  87. req = hapd.mgmt_rx()
  88. if req is None:
  89. raise Exception("MGMT RX wait timed out")
  90. if req['subtype'] == 0:
  91. assoc += 1
  92. if assoc == 3:
  93. break
  94. if assoc != 3:
  95. raise Exception("Association Request frames not received: assoc=%d" % assoc)
  96. hapd.set("ext_mgmt_frame_handling", "0")
  97. dev[0].wait_connected(timeout=15)
  98. def test_ap_open_id_str(dev, apdev):
  99. """AP with open mode and id_str"""
  100. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  101. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412", id_str="foo",
  102. wait_connect=False)
  103. ev = dev[0].wait_connected(timeout=10)
  104. if "id_str=foo" not in ev:
  105. raise Exception("CTRL-EVENT-CONNECT did not have matching id_str: " + ev)
  106. if dev[0].get_status_field("id_str") != "foo":
  107. raise Exception("id_str mismatch")
  108. def test_ap_open_select_any(dev, apdev):
  109. """AP with open mode and select any network"""
  110. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  111. id = dev[0].connect("unknown", key_mgmt="NONE", scan_freq="2412",
  112. only_add_network=True)
  113. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  114. only_add_network=True)
  115. dev[0].select_network(id)
  116. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
  117. if ev is not None:
  118. raise Exception("Unexpected connection")
  119. dev[0].select_network("any")
  120. dev[0].wait_connected(timeout=10)
  121. def test_ap_open_unexpected_assoc_event(dev, apdev):
  122. """AP with open mode and unexpected association event"""
  123. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  124. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  125. dev[0].request("DISCONNECT")
  126. dev[0].wait_disconnected(timeout=15)
  127. dev[0].dump_monitor()
  128. # This will be accepted due to matching network
  129. subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
  130. apdev[0]['bssid']])
  131. dev[0].wait_connected(timeout=15)
  132. dev[0].dump_monitor()
  133. dev[0].request("REMOVE_NETWORK all")
  134. dev[0].wait_disconnected(timeout=5)
  135. dev[0].dump_monitor()
  136. # This will result in disconnection due to no matching network
  137. subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
  138. apdev[0]['bssid']])
  139. dev[0].wait_disconnected(timeout=15)
  140. def test_ap_bss_load(dev, apdev):
  141. """AP with open mode (no security) configuration"""
  142. hapd = hostapd.add_ap(apdev[0]['ifname'],
  143. { "ssid": "open",
  144. "bss_load_update_period": "10" })
  145. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  146. # this does not really get much useful output with mac80211_hwsim currently,
  147. # but run through the channel survey update couple of times
  148. for i in range(0, 10):
  149. hwsim_utils.test_connectivity(dev[0], hapd)
  150. hwsim_utils.test_connectivity(dev[0], hapd)
  151. hwsim_utils.test_connectivity(dev[0], hapd)
  152. time.sleep(0.15)
  153. def hapd_out_of_mem(hapd, apdev, count, func):
  154. with alloc_fail(hapd, count, func):
  155. started = False
  156. try:
  157. hostapd.add_ap(apdev['ifname'], { "ssid": "open" })
  158. started = True
  159. except:
  160. pass
  161. if started:
  162. raise Exception("hostapd interface started even with memory allocation failure: " + arg)
  163. def test_ap_open_out_of_memory(dev, apdev):
  164. """hostapd failing to setup interface due to allocation failure"""
  165. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  166. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
  167. for i in range(1, 3):
  168. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
  169. for i in range(1, 5):
  170. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
  171. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
  172. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
  173. for i in range(1, 4):
  174. hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
  175. # eloop_register_read_sock() call from i802_init()
  176. hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;eloop_register_sock;?eloop_register_read_sock;=i802_init")
  177. # verify that a new interface can still be added when memory allocation does
  178. # not fail
  179. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
  180. def test_bssid_black_white_list(dev, apdev):
  181. """BSSID black/white list"""
  182. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  183. hapd2 = hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
  184. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  185. bssid_whitelist=apdev[1]['bssid'])
  186. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  187. bssid_blacklist=apdev[1]['bssid'])
  188. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  189. bssid_whitelist="00:00:00:00:00:00/00:00:00:00:00:00",
  190. bssid_blacklist=apdev[1]['bssid'])
  191. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  192. raise Exception("dev[0] connected to unexpected AP")
  193. if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
  194. raise Exception("dev[1] connected to unexpected AP")
  195. if dev[2].get_status_field('bssid') != apdev[0]['bssid']:
  196. raise Exception("dev[2] connected to unexpected AP")
  197. dev[0].request("REMOVE_NETWORK all")
  198. dev[1].request("REMOVE_NETWORK all")
  199. dev[2].request("REMOVE_NETWORK all")
  200. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  201. bssid_whitelist="00:00:00:00:00:00", wait_connect=False)
  202. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  203. bssid_whitelist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
  204. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  205. bssid_blacklist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
  206. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  207. raise Exception("dev[0] connected to unexpected AP")
  208. if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
  209. raise Exception("dev[1] connected to unexpected AP")
  210. dev[0].request("REMOVE_NETWORK all")
  211. dev[1].request("REMOVE_NETWORK all")
  212. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  213. if ev is not None:
  214. raise Exception("Unexpected dev[2] connectin")
  215. dev[2].request("REMOVE_NETWORK all")
  216. def test_ap_open_wpas_in_bridge(dev, apdev):
  217. """Open mode AP and wpas interface in a bridge"""
  218. br_ifname='sta-br0'
  219. ifname='wlan5'
  220. try:
  221. _test_ap_open_wpas_in_bridge(dev, apdev)
  222. finally:
  223. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  224. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  225. subprocess.call(['brctl', 'delbr', br_ifname])
  226. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  227. def _test_ap_open_wpas_in_bridge(dev, apdev):
  228. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
  229. br_ifname='sta-br0'
  230. ifname='wlan5'
  231. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  232. # First, try a failure case of adding an interface
  233. try:
  234. wpas.interface_add(ifname, br_ifname=br_ifname)
  235. raise Exception("Interface addition succeeded unexpectedly")
  236. except Exception, e:
  237. if "Failed to add" in str(e):
  238. logger.info("Ignore expected interface_add failure due to missing bridge interface: " + str(e))
  239. else:
  240. raise
  241. # Next, add the bridge interface and add the interface again
  242. subprocess.call(['brctl', 'addbr', br_ifname])
  243. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  244. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  245. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  246. subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
  247. wpas.interface_add(ifname, br_ifname=br_ifname)
  248. wpas.connect("open", key_mgmt="NONE", scan_freq="2412")