test_ap_open.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. # enable power save mode
  385. hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_ENABLED)
  386. time.sleep(0.1)
  387. try:
  388. # inject some traffic
  389. sa = hapd.own_addr()
  390. da = dev[0].own_addr()
  391. hapd.request('DATA_TEST_CONFIG 1')
  392. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  393. hapd.request('DATA_TEST_CONFIG 0')
  394. # let the AP send couple of Beacon frames
  395. time.sleep(0.3)
  396. # disconnect - with traffic pending - shouldn't cause kernel warnings
  397. dev[0].request("DISCONNECT")
  398. finally:
  399. hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_DISABLED)
  400. time.sleep(0.2)
  401. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  402. "wlan_mgt.tim.partial_virtual_bitmap",
  403. ["wlan_mgt.tim.partial_virtual_bitmap"])
  404. if out is not None:
  405. state = 0
  406. for l in out.splitlines():
  407. pvb = int(l, 16)
  408. if pvb > 0 and state == 0:
  409. state = 1
  410. elif pvb == 0 and state == 1:
  411. state = 2
  412. if state != 2:
  413. raise Exception("Didn't observe TIM bit getting set and unset (state=%d)" % state)
  414. @remote_compatible
  415. def test_ap_open_select_network(dev, apdev):
  416. """Open mode connection and SELECT_NETWORK to change network"""
  417. hapd1 = hostapd.add_ap(apdev[0], { "ssid": "open" })
  418. bssid1 = apdev[0]['bssid']
  419. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open2" })
  420. bssid2 = apdev[1]['bssid']
  421. id1 = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  422. only_add_network=True)
  423. id2 = dev[0].connect("open2", key_mgmt="NONE", scan_freq="2412")
  424. hwsim_utils.test_connectivity(dev[0], hapd2)
  425. dev[0].select_network(id1)
  426. dev[0].wait_connected()
  427. res = dev[0].request("BLACKLIST")
  428. if bssid1 in res or bssid2 in res:
  429. raise Exception("Unexpected blacklist entry")
  430. hwsim_utils.test_connectivity(dev[0], hapd1)
  431. dev[0].select_network(id2)
  432. dev[0].wait_connected()
  433. hwsim_utils.test_connectivity(dev[0], hapd2)
  434. res = dev[0].request("BLACKLIST")
  435. if bssid1 in res or bssid2 in res:
  436. raise Exception("Unexpected blacklist entry(2)")
  437. @remote_compatible
  438. def test_ap_open_disable_enable(dev, apdev):
  439. """AP with open mode getting disabled and re-enabled"""
  440. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  441. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  442. bg_scan_period="0")
  443. for i in range(2):
  444. hapd.request("DISABLE")
  445. dev[0].wait_disconnected()
  446. hapd.request("ENABLE")
  447. dev[0].wait_connected()
  448. hwsim_utils.test_connectivity(dev[0], hapd)
  449. def sta_enable_disable(dev, bssid):
  450. dev.scan_for_bss(bssid, freq=2412)
  451. work_id = dev.request("RADIO_WORK add block-work")
  452. ev = dev.wait_event(["EXT-RADIO-WORK-START"])
  453. if ev is None:
  454. raise Exception("Timeout while waiting radio work to start")
  455. id = dev.connect("open", key_mgmt="NONE", scan_freq="2412",
  456. only_add_network=True)
  457. dev.request("ENABLE_NETWORK %d" % id)
  458. if "connect@" not in dev.request("RADIO_WORK show"):
  459. raise Exception("connect radio work missing")
  460. dev.request("DISABLE_NETWORK %d" % id)
  461. dev.request("RADIO_WORK done " + work_id)
  462. ok = False
  463. for i in range(30):
  464. if "connect@" not in dev.request("RADIO_WORK show"):
  465. ok = True
  466. break
  467. time.sleep(0.1)
  468. if not ok:
  469. raise Exception("connect radio work not completed")
  470. ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  471. if ev is not None:
  472. raise Exception("Unexpected connection")
  473. dev.request("DISCONNECT")
  474. def test_ap_open_sta_enable_disable(dev, apdev):
  475. """AP with open mode and wpa_supplicant ENABLE/DISABLE_NETWORK"""
  476. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  477. bssid = apdev[0]['bssid']
  478. sta_enable_disable(dev[0], bssid)
  479. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  480. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  481. sta_enable_disable(wpas, bssid)
  482. @remote_compatible
  483. def test_ap_open_select_twice(dev, apdev):
  484. """AP with open mode and select network twice"""
  485. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  486. only_add_network=True)
  487. dev[0].select_network(id)
  488. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  489. if ev is None:
  490. raise Exception("No result reported")
  491. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  492. # Verify that the second SELECT_NETWORK starts a new scan immediately by
  493. # waiting less than the default scan period.
  494. dev[0].select_network(id)
  495. dev[0].wait_connected(timeout=3)
  496. @remote_compatible
  497. def test_ap_open_reassoc_not_found(dev, apdev):
  498. """AP with open mode and REASSOCIATE not finding a match"""
  499. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  500. only_add_network=True)
  501. dev[0].select_network(id)
  502. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  503. if ev is None:
  504. raise Exception("No result reported")
  505. dev[0].request("DISCONNECT")
  506. time.sleep(0.1)
  507. dev[0].dump_monitor()
  508. dev[0].request("REASSOCIATE")
  509. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  510. if ev is None:
  511. raise Exception("No result reported")
  512. dev[0].request("DISCONNECT")
  513. @remote_compatible
  514. def test_ap_open_sta_statistics(dev, apdev):
  515. """AP with open mode and STA statistics"""
  516. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  517. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  518. addr = dev[0].own_addr()
  519. stats1 = hapd.get_sta(addr)
  520. logger.info("stats1: " + str(stats1))
  521. time.sleep(0.4)
  522. stats2 = hapd.get_sta(addr)
  523. logger.info("stats2: " + str(stats2))
  524. hwsim_utils.test_connectivity(dev[0], hapd)
  525. stats3 = hapd.get_sta(addr)
  526. logger.info("stats3: " + str(stats3))
  527. # Cannot require specific inactive_msec changes without getting rid of all
  528. # unrelated traffic, so for now, just print out the results in the log for
  529. # manual checks.
  530. @remote_compatible
  531. def test_ap_open_poll_sta(dev, apdev):
  532. """AP with open mode and STA poll"""
  533. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  534. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  535. addr = dev[0].own_addr()
  536. if "OK" not in hapd.request("POLL_STA " + addr):
  537. raise Exception("POLL_STA failed")
  538. ev = hapd.wait_event(["AP-STA-POLL-OK"], timeout=5)
  539. if ev is None:
  540. raise Exception("Poll response not seen")
  541. if addr not in ev:
  542. raise Exception("Unexpected poll response: " + ev)
  543. def test_ap_open_pmf_default(dev, apdev):
  544. """AP with open mode (no security) configuration and pmf=2"""
  545. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  546. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  547. ieee80211w="2", wait_connect=False)
  548. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  549. ieee80211w="1")
  550. try:
  551. dev[0].request("SET pmf 2")
  552. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  553. dev[0].request("DISCONNECT")
  554. dev[0].wait_disconnected()
  555. finally:
  556. dev[0].request("SET pmf 0")
  557. dev[2].request("DISCONNECT")
  558. dev[2].wait_disconnected()
  559. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  560. if ev is not None:
  561. raise Exception("Unexpected dev[1] connection")
  562. dev[1].request("DISCONNECT")
  563. def test_ap_open_drv_fail(dev, apdev):
  564. """AP with open mode and driver operations failing"""
  565. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  566. with fail_test(dev[0], 1, "wpa_driver_nl80211_authenticate"):
  567. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  568. wait_connect=False)
  569. wait_fail_trigger(dev[0], "GET_FAIL")
  570. dev[0].request("REMOVE_NETWORK all")
  571. with fail_test(dev[0], 1, "wpa_driver_nl80211_associate"):
  572. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  573. wait_connect=False)
  574. wait_fail_trigger(dev[0], "GET_FAIL")
  575. dev[0].request("REMOVE_NETWORK all")
  576. def run_multicast_to_unicast(dev, apdev, convert):
  577. params = { "ssid": "open" }
  578. params["multicast_to_unicast"] = "1" if convert else "0"
  579. hapd = hostapd.add_ap(apdev[0], params)
  580. dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
  581. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  582. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  583. if ev is None:
  584. raise Exception("No connection event received from hostapd")
  585. hwsim_utils.test_connectivity(dev[0], hapd, multicast_to_unicast=convert)
  586. dev[0].request("DISCONNECT")
  587. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  588. if ev is None:
  589. raise Exception("No disconnection event received from hostapd")
  590. def test_ap_open_multicast_to_unicast(dev, apdev):
  591. """Multicast-to-unicast conversion enabled"""
  592. run_multicast_to_unicast(dev, apdev, True)
  593. def test_ap_open_multicast_to_unicast_disabled(dev, apdev):
  594. """Multicast-to-unicast conversion disabled"""
  595. run_multicast_to_unicast(dev, apdev, False)
  596. def test_ap_open_drop_duplicate(dev, apdev, params):
  597. """AP dropping duplicate management frames"""
  598. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  599. "interworking": "1" })
  600. hapd.set("ext_mgmt_frame_handling", "1")
  601. bssid = hapd.own_addr().replace(':', '')
  602. addr = "020304050607"
  603. auth = "b0003a01" + bssid + addr + bssid + '1000000001000000'
  604. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
  605. raise Exception("MGMT_RX_PROCESS failed")
  606. auth = "b0083a01" + bssid + addr + bssid + '1000000001000000'
  607. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
  608. raise Exception("MGMT_RX_PROCESS failed")
  609. ies = "00046f70656e010802040b160c12182432043048606c2d1a3c101bffff0000000000000000000001000000000000000000007f0a04000a020140004000013b155151525354737475767778797a7b7c7d7e7f808182dd070050f202000100"
  610. assoc_req = "00003a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  611. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
  612. raise Exception("MGMT_RX_PROCESS failed")
  613. assoc_req = "00083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  614. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
  615. raise Exception("MGMT_RX_PROCESS failed")
  616. reassoc_req = "20083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  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. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % reassoc_req):
  620. raise Exception("MGMT_RX_PROCESS failed")
  621. action = "d0003a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
  622. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
  623. raise Exception("MGMT_RX_PROCESS failed")
  624. action = "d0083a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
  625. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
  626. raise Exception("MGMT_RX_PROCESS failed")
  627. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  628. "wlan.fc.type == 0", ["wlan.fc.subtype"])
  629. num_auth = 0
  630. num_assoc = 0
  631. num_reassoc = 0
  632. num_action = 0
  633. for subtype in out.splitlines():
  634. val = int(subtype)
  635. if val == 11:
  636. num_auth += 1
  637. elif val == 1:
  638. num_assoc += 1
  639. elif val == 3:
  640. num_reassoc += 1
  641. elif val == 13:
  642. num_action += 1
  643. if num_auth != 1:
  644. raise Exception("Unexpected number of Authentication frames: %d" % num_auth)
  645. if num_assoc != 1:
  646. raise Exception("Unexpected number of association frames: %d" % num_assoc)
  647. if num_reassoc != 1:
  648. raise Exception("Unexpected number of reassociation frames: %d" % num_reassoc)
  649. if num_action != 1:
  650. raise Exception("Unexpected number of Action frames: %d" % num_action)
  651. def test_ap_open_select_network_freq(dev, apdev):
  652. """AP with open mode and use for SELECT_NETWORK freq parameter"""
  653. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  654. id = dev[0].connect("open", key_mgmt="NONE", only_add_network=True)
  655. dev[0].select_network(id, freq=2412)
  656. start = os.times()[4]
  657. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  658. if ev is None:
  659. raise Exception("Scan not started")
  660. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  661. if ev is None:
  662. raise Exception("Scan not completed")
  663. end = os.times()[4]
  664. logger.info("Scan duration: {} seconds".format(end - start))
  665. if end - start > 3:
  666. raise Exception("Scan took unexpectedly long time")
  667. dev[0].wait_connected()
  668. def test_ap_open_noncountry(dev, apdev):
  669. """AP with open mode and noncountry entity as Country String"""
  670. _test_ap_open_country(dev, apdev, "XX", "0x58")
  671. def test_ap_open_country_table_e4(dev, apdev):
  672. """AP with open mode and Table E-4 Country String"""
  673. _test_ap_open_country(dev, apdev, "DE", "0x04")
  674. def test_ap_open_country_indoor(dev, apdev):
  675. """AP with open mode and indoor country code"""
  676. _test_ap_open_country(dev, apdev, "DE", "0x49")
  677. def test_ap_open_country_outdoor(dev, apdev):
  678. """AP with open mode and outdoor country code"""
  679. _test_ap_open_country(dev, apdev, "DE", "0x4f")
  680. def _test_ap_open_country(dev, apdev, country_code, country3):
  681. try:
  682. run_ap_open_country(dev, apdev, country_code, country3)
  683. finally:
  684. dev[0].request("DISCONNECT")
  685. set_world_reg(apdev[0], apdev[1], dev[0])
  686. dev[0].flush_scan_cache()
  687. def run_ap_open_country(dev, apdev, country_code, country3):
  688. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  689. "country_code": country_code,
  690. "country3": country3,
  691. "ieee80211d": "1" })
  692. dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
  693. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  694. dev[0].request("DISCONNECT")
  695. dev[0].wait_disconnected()
  696. def test_ap_open_disable_select(dev, apdev):
  697. """DISABLE_NETWORK for connected AP followed by SELECT_NETWORK"""
  698. hapd1 = hostapd.add_ap(apdev[0], { "ssid": "open" })
  699. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  700. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  701. dev[0].request("DISABLE_NETWORK %d" % id)
  702. dev[0].wait_disconnected()
  703. res = dev[0].request("BLACKLIST")
  704. if hapd1.own_addr() in res or hapd2.own_addr() in res:
  705. raise Exception("Unexpected blacklist entry added")
  706. dev[0].request("SELECT_NETWORK %d" % id)
  707. dev[0].wait_connected()
  708. def test_ap_open_reassoc_same(dev, apdev):
  709. """AP with open mode and STA reassociating back to same AP without auth exchange"""
  710. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  711. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  712. try:
  713. dev[0].request("SET reassoc_same_bss_optim 1")
  714. dev[0].request("REATTACH")
  715. dev[0].wait_connected()
  716. hwsim_utils.test_connectivity(dev[0], hapd)
  717. finally:
  718. dev[0].request("SET reassoc_same_bss_optim 0")