test_ap_open.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. from remotehost import remote_compatible
  7. import logging
  8. logger = logging.getLogger()
  9. import struct
  10. import subprocess
  11. import time
  12. import os
  13. import hostapd
  14. import hwsim_utils
  15. from tshark import run_tshark
  16. from utils import alloc_fail, fail_test, wait_fail_trigger
  17. from wpasupplicant import WpaSupplicant
  18. from test_ap_ht import set_world_reg
  19. @remote_compatible
  20. def test_ap_open(dev, apdev):
  21. """AP with open mode (no security) configuration"""
  22. _test_ap_open(dev, apdev)
  23. def _test_ap_open(dev, apdev):
  24. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  25. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  26. bg_scan_period="0")
  27. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  28. if ev is None:
  29. raise Exception("No connection event received from hostapd")
  30. hwsim_utils.test_connectivity(dev[0], hapd)
  31. dev[0].request("DISCONNECT")
  32. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  33. if ev is None:
  34. raise Exception("No disconnection event received from hostapd")
  35. def test_ap_open_packet_loss(dev, apdev):
  36. """AP with open mode configuration and large packet loss"""
  37. params = { "ssid": "open",
  38. "ignore_probe_probability": "0.5",
  39. "ignore_auth_probability": "0.5",
  40. "ignore_assoc_probability": "0.5",
  41. "ignore_reassoc_probability": "0.5" }
  42. hapd = hostapd.add_ap(apdev[0], params)
  43. for i in range(0, 3):
  44. dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
  45. wait_connect=False)
  46. for i in range(0, 3):
  47. dev[i].wait_connected(timeout=20)
  48. @remote_compatible
  49. def test_ap_open_unknown_action(dev, apdev):
  50. """AP with open mode configuration and unknown Action frame"""
  51. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  52. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  53. bssid = apdev[0]['bssid']
  54. cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
  55. if "FAIL" in dev[0].request(cmd):
  56. raise Exception("Could not send test Action frame")
  57. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  58. if ev is None:
  59. raise Exception("Timeout on MGMT-TX-STATUS")
  60. if "result=SUCCESS" not in ev:
  61. raise Exception("AP did not ack Action frame")
  62. def test_ap_open_invalid_wmm_action(dev, apdev):
  63. """AP with open mode configuration and invalid WMM Action frame"""
  64. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  65. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  66. bssid = apdev[0]['bssid']
  67. cmd = "MGMT_TX {} {} freq=2412 action=1100".format(bssid, bssid)
  68. if "FAIL" in dev[0].request(cmd):
  69. raise Exception("Could not send test Action frame")
  70. ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
  71. if ev is None or "result=SUCCESS" not in ev:
  72. raise Exception("AP did not ack Action frame")
  73. @remote_compatible
  74. def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
  75. """Reconnect to open mode AP after inactivity related disconnection"""
  76. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  77. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  78. hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
  79. dev[0].wait_disconnected(timeout=5)
  80. dev[0].wait_connected(timeout=2, error="Timeout on reconnection")
  81. @remote_compatible
  82. def test_ap_open_assoc_timeout(dev, apdev):
  83. """AP timing out association"""
  84. ssid = "test"
  85. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  86. dev[0].scan(freq="2412")
  87. hapd.set("ext_mgmt_frame_handling", "1")
  88. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  89. wait_connect=False)
  90. for i in range(0, 10):
  91. req = hapd.mgmt_rx()
  92. if req is None:
  93. raise Exception("MGMT RX wait timed out")
  94. if req['subtype'] == 11:
  95. break
  96. req = None
  97. if not req:
  98. raise Exception("Authentication frame not received")
  99. resp = {}
  100. resp['fc'] = req['fc']
  101. resp['da'] = req['sa']
  102. resp['sa'] = req['da']
  103. resp['bssid'] = req['bssid']
  104. resp['payload'] = struct.pack('<HHH', 0, 2, 0)
  105. hapd.mgmt_tx(resp)
  106. assoc = 0
  107. for i in range(0, 10):
  108. req = hapd.mgmt_rx()
  109. if req is None:
  110. raise Exception("MGMT RX wait timed out")
  111. if req['subtype'] == 0:
  112. assoc += 1
  113. if assoc == 3:
  114. break
  115. if assoc != 3:
  116. raise Exception("Association Request frames not received: assoc=%d" % assoc)
  117. hapd.set("ext_mgmt_frame_handling", "0")
  118. dev[0].wait_connected(timeout=15)
  119. @remote_compatible
  120. def test_ap_open_id_str(dev, apdev):
  121. """AP with open mode and id_str"""
  122. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  123. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412", id_str="foo",
  124. wait_connect=False)
  125. ev = dev[0].wait_connected(timeout=10)
  126. if "id_str=foo" not in ev:
  127. raise Exception("CTRL-EVENT-CONNECT did not have matching id_str: " + ev)
  128. if dev[0].get_status_field("id_str") != "foo":
  129. raise Exception("id_str mismatch")
  130. @remote_compatible
  131. def test_ap_open_select_any(dev, apdev):
  132. """AP with open mode and select any network"""
  133. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  134. id = dev[0].connect("unknown", key_mgmt="NONE", scan_freq="2412",
  135. only_add_network=True)
  136. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  137. only_add_network=True)
  138. dev[0].select_network(id)
  139. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND",
  140. "CTRL-EVENT-CONNECTED"], timeout=10)
  141. if ev is None:
  142. raise Exception("No result reported")
  143. if "CTRL-EVENT-CONNECTED" in ev:
  144. raise Exception("Unexpected connection")
  145. dev[0].select_network("any")
  146. dev[0].wait_connected(timeout=10)
  147. @remote_compatible
  148. def test_ap_open_unexpected_assoc_event(dev, apdev):
  149. """AP with open mode and unexpected association event"""
  150. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  151. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  152. dev[0].request("DISCONNECT")
  153. dev[0].wait_disconnected(timeout=15)
  154. dev[0].dump_monitor()
  155. # This will be accepted due to matching network
  156. dev[0].cmd_execute(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
  157. apdev[0]['bssid']])
  158. dev[0].wait_connected(timeout=15)
  159. dev[0].dump_monitor()
  160. dev[0].request("REMOVE_NETWORK all")
  161. dev[0].wait_disconnected(timeout=5)
  162. dev[0].dump_monitor()
  163. # This will result in disconnection due to no matching network
  164. dev[0].cmd_execute(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
  165. apdev[0]['bssid']])
  166. dev[0].wait_disconnected(timeout=15)
  167. def test_ap_open_external_assoc(dev, apdev):
  168. """AP with open mode and external association"""
  169. hapd = hostapd.add_ap(apdev[0], { "ssid": "open-ext-assoc" })
  170. try:
  171. dev[0].request("STA_AUTOCONNECT 0")
  172. id = dev[0].connect("open-ext-assoc", key_mgmt="NONE", scan_freq="2412",
  173. only_add_network=True)
  174. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  175. dev[0].dump_monitor()
  176. # This will be accepted due to matching network
  177. dev[0].cmd_execute(['iw', 'dev', dev[0].ifname, 'connect',
  178. 'open-ext-assoc', "2412", apdev[0]['bssid']])
  179. ev = dev[0].wait_event([ "CTRL-EVENT-DISCONNECTED",
  180. "CTRL-EVENT-CONNECTED" ], timeout=10)
  181. if ev is None:
  182. raise Exception("Connection timed out")
  183. if "CTRL-EVENT-DISCONNECTED" in ev:
  184. raise Exception("Unexpected disconnection event")
  185. dev[0].dump_monitor()
  186. dev[0].request("DISCONNECT")
  187. dev[0].wait_disconnected(timeout=5)
  188. finally:
  189. dev[0].request("STA_AUTOCONNECT 1")
  190. @remote_compatible
  191. def test_ap_bss_load(dev, apdev):
  192. """AP with open mode (no security) configuration"""
  193. hapd = hostapd.add_ap(apdev[0],
  194. { "ssid": "open",
  195. "bss_load_update_period": "10",
  196. "chan_util_avg_period": "20" })
  197. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  198. # this does not really get much useful output with mac80211_hwsim currently,
  199. # but run through the channel survey update couple of times
  200. for i in range(0, 10):
  201. hwsim_utils.test_connectivity(dev[0], hapd)
  202. hwsim_utils.test_connectivity(dev[0], hapd)
  203. hwsim_utils.test_connectivity(dev[0], hapd)
  204. time.sleep(0.15)
  205. avg = hapd.get_status_field("chan_util_avg")
  206. if avg is None:
  207. raise Exception("No STATUS chan_util_avg seen")
  208. def test_ap_bss_load_fail(dev, apdev):
  209. """BSS Load update failing to get survey data"""
  210. hapd = hostapd.add_ap(apdev[0],
  211. { "ssid": "open",
  212. "bss_load_update_period": "1" })
  213. with fail_test(hapd, 1, "wpa_driver_nl80211_get_survey"):
  214. wait_fail_trigger(hapd, "GET_FAIL")
  215. def hapd_out_of_mem(hapd, apdev, count, func):
  216. with alloc_fail(hapd, count, func):
  217. started = False
  218. try:
  219. hostapd.add_ap(apdev, { "ssid": "open" })
  220. started = True
  221. except:
  222. pass
  223. if started:
  224. raise Exception("hostapd interface started even with memory allocation failure: %d:%s" % (count, func))
  225. def test_ap_open_out_of_memory(dev, apdev):
  226. """hostapd failing to setup interface due to allocation failure"""
  227. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  228. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
  229. for i in range(1, 3):
  230. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
  231. for i in range(1, 5):
  232. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
  233. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
  234. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
  235. for i in range(1, 3):
  236. hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
  237. # eloop_register_read_sock() call from i802_init()
  238. hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;?eloop_register_sock;?eloop_register_read_sock;=i802_init")
  239. # verify that a new interface can still be added when memory allocation does
  240. # not fail
  241. hostapd.add_ap(apdev[1], { "ssid": "open" })
  242. def test_bssid_black_white_list(dev, apdev):
  243. """BSSID black/white list"""
  244. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  245. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  246. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  247. bssid_whitelist=apdev[1]['bssid'])
  248. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  249. bssid_blacklist=apdev[1]['bssid'])
  250. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  251. bssid_whitelist="00:00:00:00:00:00/00:00:00:00:00:00",
  252. bssid_blacklist=apdev[1]['bssid'])
  253. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  254. raise Exception("dev[0] connected to unexpected AP")
  255. if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
  256. raise Exception("dev[1] connected to unexpected AP")
  257. if dev[2].get_status_field('bssid') != apdev[0]['bssid']:
  258. raise Exception("dev[2] connected to unexpected AP")
  259. dev[0].request("REMOVE_NETWORK all")
  260. dev[1].request("REMOVE_NETWORK all")
  261. dev[2].request("REMOVE_NETWORK all")
  262. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  263. bssid_whitelist="00:00:00:00:00:00", wait_connect=False)
  264. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  265. bssid_whitelist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
  266. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  267. bssid_blacklist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
  268. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  269. raise Exception("dev[0] connected to unexpected AP")
  270. if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
  271. raise Exception("dev[1] connected to unexpected AP")
  272. dev[0].request("REMOVE_NETWORK all")
  273. dev[1].request("REMOVE_NETWORK all")
  274. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  275. if ev is not None:
  276. raise Exception("Unexpected dev[2] connectin")
  277. dev[2].request("REMOVE_NETWORK all")
  278. def test_ap_open_wpas_in_bridge(dev, apdev):
  279. """Open mode AP and wpas interface in a bridge"""
  280. br_ifname='sta-br0'
  281. ifname='wlan5'
  282. try:
  283. _test_ap_open_wpas_in_bridge(dev, apdev)
  284. finally:
  285. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  286. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  287. subprocess.call(['brctl', 'delbr', br_ifname])
  288. subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
  289. def _test_ap_open_wpas_in_bridge(dev, apdev):
  290. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  291. br_ifname='sta-br0'
  292. ifname='wlan5'
  293. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  294. # First, try a failure case of adding an interface
  295. try:
  296. wpas.interface_add(ifname, br_ifname=br_ifname)
  297. raise Exception("Interface addition succeeded unexpectedly")
  298. except Exception, e:
  299. if "Failed to add" in str(e):
  300. logger.info("Ignore expected interface_add failure due to missing bridge interface: " + str(e))
  301. else:
  302. raise
  303. # Next, add the bridge interface and add the interface again
  304. subprocess.call(['brctl', 'addbr', br_ifname])
  305. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  306. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  307. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  308. subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
  309. wpas.interface_add(ifname, br_ifname=br_ifname)
  310. wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
  311. @remote_compatible
  312. def test_ap_open_start_disabled(dev, apdev):
  313. """AP with open mode and beaconing disabled"""
  314. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  315. "start_disabled": "1" })
  316. bssid = apdev[0]['bssid']
  317. dev[0].flush_scan_cache()
  318. dev[0].scan(freq=2412, only_new=True)
  319. if dev[0].get_bss(bssid) is not None:
  320. raise Exception("AP was seen beaconing")
  321. if "OK" not in hapd.request("RELOAD"):
  322. raise Exception("RELOAD failed")
  323. dev[0].scan_for_bss(bssid, freq=2412)
  324. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  325. @remote_compatible
  326. def test_ap_open_start_disabled2(dev, apdev):
  327. """AP with open mode and beaconing disabled (2)"""
  328. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  329. "start_disabled": "1" })
  330. bssid = apdev[0]['bssid']
  331. dev[0].flush_scan_cache()
  332. dev[0].scan(freq=2412, only_new=True)
  333. if dev[0].get_bss(bssid) is not None:
  334. raise Exception("AP was seen beaconing")
  335. if "OK" not in hapd.request("UPDATE_BEACON"):
  336. raise Exception("UPDATE_BEACON failed")
  337. dev[0].scan_for_bss(bssid, freq=2412)
  338. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  339. if "OK" not in hapd.request("UPDATE_BEACON"):
  340. raise Exception("UPDATE_BEACON failed")
  341. dev[0].request("DISCONNECT")
  342. dev[0].wait_disconnected()
  343. dev[0].request("RECONNECT")
  344. dev[0].wait_connected()
  345. @remote_compatible
  346. def test_ap_open_ifdown(dev, apdev):
  347. """AP with open mode and external ifconfig down"""
  348. params = { "ssid": "open",
  349. "ap_max_inactivity": "1" }
  350. hapd = hostapd.add_ap(apdev[0], params)
  351. bssid = apdev[0]['bssid']
  352. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  353. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412")
  354. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
  355. ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=10)
  356. if ev is None:
  357. raise Exception("Timeout on AP-STA-DISCONNECTED (1)")
  358. ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=5)
  359. if ev is None:
  360. raise Exception("Timeout on AP-STA-DISCONNECTED (2)")
  361. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=5)
  362. if ev is None:
  363. raise Exception("No INTERFACE-DISABLED event")
  364. # The following wait tests beacon loss detection in mac80211 on dev0.
  365. # dev1 is used to test stopping of AP side functionality on client polling.
  366. dev[1].request("REMOVE_NETWORK all")
  367. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
  368. dev[0].wait_disconnected()
  369. dev[1].wait_disconnected()
  370. ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
  371. if ev is None:
  372. raise Exception("No INTERFACE-ENABLED event")
  373. dev[0].wait_connected()
  374. hwsim_utils.test_connectivity(dev[0], hapd)
  375. def test_ap_open_disconnect_in_ps(dev, apdev, params):
  376. """Disconnect with the client in PS to regression-test a kernel bug"""
  377. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  378. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  379. bg_scan_period="0")
  380. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  381. if ev is None:
  382. raise Exception("No connection event received from hostapd")
  383. time.sleep(0.2)
  384. hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_MANUAL_POLL)
  385. try:
  386. # inject some traffic
  387. sa = hapd.own_addr()
  388. da = dev[0].own_addr()
  389. hapd.request('DATA_TEST_CONFIG 1')
  390. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  391. hapd.request('DATA_TEST_CONFIG 0')
  392. # let the AP send couple of Beacon frames
  393. time.sleep(0.3)
  394. # disconnect - with traffic pending - shouldn't cause kernel warnings
  395. dev[0].request("DISCONNECT")
  396. finally:
  397. hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_DISABLED)
  398. time.sleep(0.2)
  399. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  400. "wlan_mgt.tim.partial_virtual_bitmap",
  401. ["wlan_mgt.tim.partial_virtual_bitmap"])
  402. if out is not None:
  403. state = 0
  404. for l in out.splitlines():
  405. pvb = int(l, 16)
  406. if pvb > 0 and state == 0:
  407. state = 1
  408. elif pvb == 0 and state == 1:
  409. state = 2
  410. if state != 2:
  411. raise Exception("Didn't observe TIM bit getting set and unset (state=%d)" % state)
  412. @remote_compatible
  413. def test_ap_open_select_network(dev, apdev):
  414. """Open mode connection and SELECT_NETWORK to change network"""
  415. hapd1 = hostapd.add_ap(apdev[0], { "ssid": "open" })
  416. bssid1 = apdev[0]['bssid']
  417. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open2" })
  418. bssid2 = apdev[1]['bssid']
  419. id1 = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  420. only_add_network=True)
  421. id2 = dev[0].connect("open2", key_mgmt="NONE", scan_freq="2412")
  422. hwsim_utils.test_connectivity(dev[0], hapd2)
  423. dev[0].select_network(id1)
  424. dev[0].wait_connected()
  425. res = dev[0].request("BLACKLIST")
  426. if bssid1 in res or bssid2 in res:
  427. raise Exception("Unexpected blacklist entry")
  428. hwsim_utils.test_connectivity(dev[0], hapd1)
  429. dev[0].select_network(id2)
  430. dev[0].wait_connected()
  431. hwsim_utils.test_connectivity(dev[0], hapd2)
  432. res = dev[0].request("BLACKLIST")
  433. if bssid1 in res or bssid2 in res:
  434. raise Exception("Unexpected blacklist entry(2)")
  435. @remote_compatible
  436. def test_ap_open_disable_enable(dev, apdev):
  437. """AP with open mode getting disabled and re-enabled"""
  438. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  439. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  440. bg_scan_period="0")
  441. for i in range(2):
  442. hapd.request("DISABLE")
  443. dev[0].wait_disconnected()
  444. hapd.request("ENABLE")
  445. dev[0].wait_connected()
  446. hwsim_utils.test_connectivity(dev[0], hapd)
  447. def sta_enable_disable(dev, bssid):
  448. dev.scan_for_bss(bssid, freq=2412)
  449. work_id = dev.request("RADIO_WORK add block-work")
  450. ev = dev.wait_event(["EXT-RADIO-WORK-START"])
  451. if ev is None:
  452. raise Exception("Timeout while waiting radio work to start")
  453. id = dev.connect("open", key_mgmt="NONE", scan_freq="2412",
  454. only_add_network=True)
  455. dev.request("ENABLE_NETWORK %d" % id)
  456. if "connect@" not in dev.request("RADIO_WORK show"):
  457. raise Exception("connect radio work missing")
  458. dev.request("DISABLE_NETWORK %d" % id)
  459. dev.request("RADIO_WORK done " + work_id)
  460. ok = False
  461. for i in range(30):
  462. if "connect@" not in dev.request("RADIO_WORK show"):
  463. ok = True
  464. break
  465. time.sleep(0.1)
  466. if not ok:
  467. raise Exception("connect radio work not completed")
  468. ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  469. if ev is not None:
  470. raise Exception("Unexpected connection")
  471. dev.request("DISCONNECT")
  472. def test_ap_open_sta_enable_disable(dev, apdev):
  473. """AP with open mode and wpa_supplicant ENABLE/DISABLE_NETWORK"""
  474. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  475. bssid = apdev[0]['bssid']
  476. sta_enable_disable(dev[0], bssid)
  477. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  478. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  479. sta_enable_disable(wpas, bssid)
  480. @remote_compatible
  481. def test_ap_open_select_twice(dev, apdev):
  482. """AP with open mode and select network twice"""
  483. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  484. only_add_network=True)
  485. dev[0].select_network(id)
  486. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  487. if ev is None:
  488. raise Exception("No result reported")
  489. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  490. # Verify that the second SELECT_NETWORK starts a new scan immediately by
  491. # waiting less than the default scan period.
  492. dev[0].select_network(id)
  493. dev[0].wait_connected(timeout=3)
  494. @remote_compatible
  495. def test_ap_open_reassoc_not_found(dev, apdev):
  496. """AP with open mode and REASSOCIATE not finding a match"""
  497. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  498. only_add_network=True)
  499. dev[0].select_network(id)
  500. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  501. if ev is None:
  502. raise Exception("No result reported")
  503. dev[0].request("DISCONNECT")
  504. time.sleep(0.1)
  505. dev[0].dump_monitor()
  506. dev[0].request("REASSOCIATE")
  507. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  508. if ev is None:
  509. raise Exception("No result reported")
  510. dev[0].request("DISCONNECT")
  511. @remote_compatible
  512. def test_ap_open_sta_statistics(dev, apdev):
  513. """AP with open mode and STA statistics"""
  514. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  515. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  516. addr = dev[0].own_addr()
  517. stats1 = hapd.get_sta(addr)
  518. logger.info("stats1: " + str(stats1))
  519. time.sleep(0.4)
  520. stats2 = hapd.get_sta(addr)
  521. logger.info("stats2: " + str(stats2))
  522. hwsim_utils.test_connectivity(dev[0], hapd)
  523. stats3 = hapd.get_sta(addr)
  524. logger.info("stats3: " + str(stats3))
  525. # Cannot require specific inactive_msec changes without getting rid of all
  526. # unrelated traffic, so for now, just print out the results in the log for
  527. # manual checks.
  528. @remote_compatible
  529. def test_ap_open_poll_sta(dev, apdev):
  530. """AP with open mode and STA poll"""
  531. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  532. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  533. addr = dev[0].own_addr()
  534. if "OK" not in hapd.request("POLL_STA " + addr):
  535. raise Exception("POLL_STA failed")
  536. ev = hapd.wait_event(["AP-STA-POLL-OK"], timeout=5)
  537. if ev is None:
  538. raise Exception("Poll response not seen")
  539. if addr not in ev:
  540. raise Exception("Unexpected poll response: " + ev)
  541. def test_ap_open_pmf_default(dev, apdev):
  542. """AP with open mode (no security) configuration and pmf=2"""
  543. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  544. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  545. ieee80211w="2", wait_connect=False)
  546. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  547. ieee80211w="1")
  548. try:
  549. dev[0].request("SET pmf 2")
  550. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  551. dev[0].request("DISCONNECT")
  552. dev[0].wait_disconnected()
  553. finally:
  554. dev[0].request("SET pmf 0")
  555. dev[2].request("DISCONNECT")
  556. dev[2].wait_disconnected()
  557. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  558. if ev is not None:
  559. raise Exception("Unexpected dev[1] connection")
  560. dev[1].request("DISCONNECT")
  561. def test_ap_open_drv_fail(dev, apdev):
  562. """AP with open mode and driver operations failing"""
  563. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  564. with fail_test(dev[0], 1, "wpa_driver_nl80211_authenticate"):
  565. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  566. wait_connect=False)
  567. wait_fail_trigger(dev[0], "GET_FAIL")
  568. dev[0].request("REMOVE_NETWORK all")
  569. with fail_test(dev[0], 1, "wpa_driver_nl80211_associate"):
  570. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  571. wait_connect=False)
  572. wait_fail_trigger(dev[0], "GET_FAIL")
  573. dev[0].request("REMOVE_NETWORK all")
  574. def run_multicast_to_unicast(dev, apdev, convert):
  575. params = { "ssid": "open" }
  576. params["multicast_to_unicast"] = "1" if convert else "0"
  577. hapd = hostapd.add_ap(apdev[0], params)
  578. dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
  579. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  580. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  581. if ev is None:
  582. raise Exception("No connection event received from hostapd")
  583. hwsim_utils.test_connectivity(dev[0], hapd, multicast_to_unicast=convert)
  584. dev[0].request("DISCONNECT")
  585. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  586. if ev is None:
  587. raise Exception("No disconnection event received from hostapd")
  588. def test_ap_open_multicast_to_unicast(dev, apdev):
  589. """Multicast-to-unicast conversion enabled"""
  590. run_multicast_to_unicast(dev, apdev, True)
  591. def test_ap_open_multicast_to_unicast_disabled(dev, apdev):
  592. """Multicast-to-unicast conversion disabled"""
  593. run_multicast_to_unicast(dev, apdev, False)
  594. def test_ap_open_drop_duplicate(dev, apdev, params):
  595. """AP dropping duplicate management frames"""
  596. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  597. "interworking": "1" })
  598. hapd.set("ext_mgmt_frame_handling", "1")
  599. bssid = hapd.own_addr().replace(':', '')
  600. addr = "020304050607"
  601. auth = "b0003a01" + bssid + addr + bssid + '1000000001000000'
  602. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
  603. raise Exception("MGMT_RX_PROCESS failed")
  604. auth = "b0083a01" + bssid + addr + bssid + '1000000001000000'
  605. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
  606. raise Exception("MGMT_RX_PROCESS failed")
  607. ies = "00046f70656e010802040b160c12182432043048606c2d1a3c101bffff0000000000000000000001000000000000000000007f0a04000a020140004000013b155151525354737475767778797a7b7c7d7e7f808182dd070050f202000100"
  608. assoc_req = "00003a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  609. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
  610. raise Exception("MGMT_RX_PROCESS failed")
  611. assoc_req = "00083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  612. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
  613. raise Exception("MGMT_RX_PROCESS failed")
  614. reassoc_req = "20083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  615. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % reassoc_req):
  616. raise Exception("MGMT_RX_PROCESS failed")
  617. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % reassoc_req):
  618. raise Exception("MGMT_RX_PROCESS failed")
  619. action = "d0003a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
  620. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
  621. raise Exception("MGMT_RX_PROCESS failed")
  622. action = "d0083a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
  623. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
  624. raise Exception("MGMT_RX_PROCESS failed")
  625. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  626. "wlan.fc.type == 0", ["wlan.fc.subtype"])
  627. num_auth = 0
  628. num_assoc = 0
  629. num_reassoc = 0
  630. num_action = 0
  631. for subtype in out.splitlines():
  632. val = int(subtype)
  633. if val == 11:
  634. num_auth += 1
  635. elif val == 1:
  636. num_assoc += 1
  637. elif val == 3:
  638. num_reassoc += 1
  639. elif val == 13:
  640. num_action += 1
  641. if num_auth != 1:
  642. raise Exception("Unexpected number of Authentication frames: %d" % num_auth)
  643. if num_assoc != 1:
  644. raise Exception("Unexpected number of association frames: %d" % num_assoc)
  645. if num_reassoc != 1:
  646. raise Exception("Unexpected number of reassociation frames: %d" % num_reassoc)
  647. if num_action != 1:
  648. raise Exception("Unexpected number of Action frames: %d" % num_action)
  649. def test_ap_open_select_network_freq(dev, apdev):
  650. """AP with open mode and use for SELECT_NETWORK freq parameter"""
  651. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  652. id = dev[0].connect("open", key_mgmt="NONE", only_add_network=True)
  653. dev[0].select_network(id, freq=2412)
  654. start = os.times()[4]
  655. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  656. if ev is None:
  657. raise Exception("Scan not started")
  658. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  659. if ev is None:
  660. raise Exception("Scan not completed")
  661. end = os.times()[4]
  662. logger.info("Scan duration: {} seconds".format(end - start))
  663. if end - start > 3:
  664. raise Exception("Scan took unexpectedly long time")
  665. dev[0].wait_connected()
  666. def test_ap_open_noncountry(dev, apdev):
  667. """AP with open mode and noncountry entity as Country String"""
  668. _test_ap_open_country(dev, apdev, "XX", "0x58")
  669. def test_ap_open_country_table_e4(dev, apdev):
  670. """AP with open mode and Table E-4 Country String"""
  671. _test_ap_open_country(dev, apdev, "DE", "0x04")
  672. def test_ap_open_country_indoor(dev, apdev):
  673. """AP with open mode and indoor country code"""
  674. _test_ap_open_country(dev, apdev, "DE", "0x49")
  675. def test_ap_open_country_outdoor(dev, apdev):
  676. """AP with open mode and outdoor country code"""
  677. _test_ap_open_country(dev, apdev, "DE", "0x4f")
  678. def _test_ap_open_country(dev, apdev, country_code, country3):
  679. try:
  680. run_ap_open_country(dev, apdev, country_code, country3)
  681. finally:
  682. dev[0].request("DISCONNECT")
  683. set_world_reg(apdev[0], apdev[1], dev[0])
  684. dev[0].flush_scan_cache()
  685. def run_ap_open_country(dev, apdev, country_code, country3):
  686. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  687. "country_code": country_code,
  688. "country3": country3,
  689. "ieee80211d": "1" })
  690. dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
  691. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  692. dev[0].request("DISCONNECT")
  693. dev[0].wait_disconnected()
  694. def test_ap_open_disable_select(dev, apdev):
  695. """DISABLE_NETWORK for connected AP followed by SELECT_NETWORK"""
  696. hapd1 = hostapd.add_ap(apdev[0], { "ssid": "open" })
  697. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  698. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  699. dev[0].request("DISABLE_NETWORK %d" % id)
  700. dev[0].wait_disconnected()
  701. res = dev[0].request("BLACKLIST")
  702. if hapd1.own_addr() in res or hapd2.own_addr() in res:
  703. raise Exception("Unexpected blacklist entry added")
  704. dev[0].request("SELECT_NETWORK %d" % id)
  705. dev[0].wait_connected()
  706. def test_ap_open_reassoc_same(dev, apdev):
  707. """AP with open mode and STA reassociating back to same AP without auth exchange"""
  708. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  709. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  710. try:
  711. dev[0].request("SET reassoc_same_bss_optim 1")
  712. dev[0].request("REATTACH")
  713. dev[0].wait_connected()
  714. hwsim_utils.test_connectivity(dev[0], hapd)
  715. finally:
  716. dev[0].request("SET reassoc_same_bss_optim 0")