test_ap_open.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  197. # this does not really get much useful output with mac80211_hwsim currently,
  198. # but run through the channel survey update couple of times
  199. for i in range(0, 10):
  200. hwsim_utils.test_connectivity(dev[0], hapd)
  201. hwsim_utils.test_connectivity(dev[0], hapd)
  202. hwsim_utils.test_connectivity(dev[0], hapd)
  203. time.sleep(0.15)
  204. def test_ap_bss_load_fail(dev, apdev):
  205. """BSS Load update failing to get survey data"""
  206. hapd = hostapd.add_ap(apdev[0],
  207. { "ssid": "open",
  208. "bss_load_update_period": "1" })
  209. with fail_test(hapd, 1, "wpa_driver_nl80211_get_survey"):
  210. wait_fail_trigger(hapd, "GET_FAIL")
  211. def hapd_out_of_mem(hapd, apdev, count, func):
  212. with alloc_fail(hapd, count, func):
  213. started = False
  214. try:
  215. hostapd.add_ap(apdev, { "ssid": "open" })
  216. started = True
  217. except:
  218. pass
  219. if started:
  220. raise Exception("hostapd interface started even with memory allocation failure: %d:%s" % (count, func))
  221. def test_ap_open_out_of_memory(dev, apdev):
  222. """hostapd failing to setup interface due to allocation failure"""
  223. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  224. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
  225. for i in range(1, 3):
  226. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
  227. for i in range(1, 5):
  228. hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
  229. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
  230. hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
  231. for i in range(1, 3):
  232. hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
  233. # eloop_register_read_sock() call from i802_init()
  234. hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;?eloop_register_sock;?eloop_register_read_sock;=i802_init")
  235. # verify that a new interface can still be added when memory allocation does
  236. # not fail
  237. hostapd.add_ap(apdev[1], { "ssid": "open" })
  238. def test_bssid_black_white_list(dev, apdev):
  239. """BSSID black/white list"""
  240. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  241. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  242. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  243. bssid_whitelist=apdev[1]['bssid'])
  244. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  245. bssid_blacklist=apdev[1]['bssid'])
  246. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  247. bssid_whitelist="00:00:00:00:00:00/00:00:00:00:00:00",
  248. bssid_blacklist=apdev[1]['bssid'])
  249. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  250. raise Exception("dev[0] connected to unexpected AP")
  251. if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
  252. raise Exception("dev[1] connected to unexpected AP")
  253. if dev[2].get_status_field('bssid') != apdev[0]['bssid']:
  254. raise Exception("dev[2] connected to unexpected AP")
  255. dev[0].request("REMOVE_NETWORK all")
  256. dev[1].request("REMOVE_NETWORK all")
  257. dev[2].request("REMOVE_NETWORK all")
  258. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  259. bssid_whitelist="00:00:00:00:00:00", wait_connect=False)
  260. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  261. bssid_whitelist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
  262. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  263. bssid_blacklist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
  264. if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
  265. raise Exception("dev[0] connected to unexpected AP")
  266. if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
  267. raise Exception("dev[1] connected to unexpected AP")
  268. dev[0].request("REMOVE_NETWORK all")
  269. dev[1].request("REMOVE_NETWORK all")
  270. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  271. if ev is not None:
  272. raise Exception("Unexpected dev[2] connectin")
  273. dev[2].request("REMOVE_NETWORK all")
  274. def test_ap_open_wpas_in_bridge(dev, apdev):
  275. """Open mode AP and wpas interface in a bridge"""
  276. br_ifname='sta-br0'
  277. ifname='wlan5'
  278. try:
  279. _test_ap_open_wpas_in_bridge(dev, apdev)
  280. finally:
  281. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'down'])
  282. subprocess.call(['brctl', 'delif', br_ifname, ifname])
  283. subprocess.call(['brctl', 'delbr', br_ifname])
  284. subprocess.call(['iw', ifname, 'set', '4addr', 'off'])
  285. def _test_ap_open_wpas_in_bridge(dev, apdev):
  286. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  287. br_ifname='sta-br0'
  288. ifname='wlan5'
  289. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  290. # First, try a failure case of adding an interface
  291. try:
  292. wpas.interface_add(ifname, br_ifname=br_ifname)
  293. raise Exception("Interface addition succeeded unexpectedly")
  294. except Exception, e:
  295. if "Failed to add" in str(e):
  296. logger.info("Ignore expected interface_add failure due to missing bridge interface: " + str(e))
  297. else:
  298. raise
  299. # Next, add the bridge interface and add the interface again
  300. subprocess.call(['brctl', 'addbr', br_ifname])
  301. subprocess.call(['brctl', 'setfd', br_ifname, '0'])
  302. subprocess.call(['ip', 'link', 'set', 'dev', br_ifname, 'up'])
  303. subprocess.call(['iw', ifname, 'set', '4addr', 'on'])
  304. subprocess.check_call(['brctl', 'addif', br_ifname, ifname])
  305. wpas.interface_add(ifname, br_ifname=br_ifname)
  306. wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
  307. @remote_compatible
  308. def test_ap_open_start_disabled(dev, apdev):
  309. """AP with open mode and beaconing disabled"""
  310. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  311. "start_disabled": "1" })
  312. bssid = apdev[0]['bssid']
  313. dev[0].flush_scan_cache()
  314. dev[0].scan(freq=2412, only_new=True)
  315. if dev[0].get_bss(bssid) is not None:
  316. raise Exception("AP was seen beaconing")
  317. if "OK" not in hapd.request("RELOAD"):
  318. raise Exception("RELOAD failed")
  319. dev[0].scan_for_bss(bssid, freq=2412)
  320. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  321. @remote_compatible
  322. def test_ap_open_start_disabled2(dev, apdev):
  323. """AP with open mode and beaconing disabled (2)"""
  324. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  325. "start_disabled": "1" })
  326. bssid = apdev[0]['bssid']
  327. dev[0].flush_scan_cache()
  328. dev[0].scan(freq=2412, only_new=True)
  329. if dev[0].get_bss(bssid) is not None:
  330. raise Exception("AP was seen beaconing")
  331. if "OK" not in hapd.request("UPDATE_BEACON"):
  332. raise Exception("UPDATE_BEACON failed")
  333. dev[0].scan_for_bss(bssid, freq=2412)
  334. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  335. if "OK" not in hapd.request("UPDATE_BEACON"):
  336. raise Exception("UPDATE_BEACON failed")
  337. dev[0].request("DISCONNECT")
  338. dev[0].wait_disconnected()
  339. dev[0].request("RECONNECT")
  340. dev[0].wait_connected()
  341. @remote_compatible
  342. def test_ap_open_ifdown(dev, apdev):
  343. """AP with open mode and external ifconfig down"""
  344. params = { "ssid": "open",
  345. "ap_max_inactivity": "1" }
  346. hapd = hostapd.add_ap(apdev[0], params)
  347. bssid = apdev[0]['bssid']
  348. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  349. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412")
  350. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
  351. ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=10)
  352. if ev is None:
  353. raise Exception("Timeout on AP-STA-DISCONNECTED (1)")
  354. ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=5)
  355. if ev is None:
  356. raise Exception("Timeout on AP-STA-DISCONNECTED (2)")
  357. ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=5)
  358. if ev is None:
  359. raise Exception("No INTERFACE-DISABLED event")
  360. # The following wait tests beacon loss detection in mac80211 on dev0.
  361. # dev1 is used to test stopping of AP side functionality on client polling.
  362. dev[1].request("REMOVE_NETWORK all")
  363. hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
  364. dev[0].wait_disconnected()
  365. dev[1].wait_disconnected()
  366. ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
  367. if ev is None:
  368. raise Exception("No INTERFACE-ENABLED event")
  369. dev[0].wait_connected()
  370. hwsim_utils.test_connectivity(dev[0], hapd)
  371. def test_ap_open_disconnect_in_ps(dev, apdev, params):
  372. """Disconnect with the client in PS to regression-test a kernel bug"""
  373. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  374. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  375. bg_scan_period="0")
  376. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  377. if ev is None:
  378. raise Exception("No connection event received from hostapd")
  379. time.sleep(0.2)
  380. hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_MANUAL_POLL)
  381. try:
  382. # inject some traffic
  383. sa = hapd.own_addr()
  384. da = dev[0].own_addr()
  385. hapd.request('DATA_TEST_CONFIG 1')
  386. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  387. hapd.request('DATA_TEST_CONFIG 0')
  388. # let the AP send couple of Beacon frames
  389. time.sleep(0.3)
  390. # disconnect - with traffic pending - shouldn't cause kernel warnings
  391. dev[0].request("DISCONNECT")
  392. finally:
  393. hwsim_utils.set_powersave(dev[0], hwsim_utils.PS_DISABLED)
  394. time.sleep(0.2)
  395. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  396. "wlan_mgt.tim.partial_virtual_bitmap",
  397. ["wlan_mgt.tim.partial_virtual_bitmap"])
  398. if out is not None:
  399. state = 0
  400. for l in out.splitlines():
  401. pvb = int(l, 16)
  402. if pvb > 0 and state == 0:
  403. state = 1
  404. elif pvb == 0 and state == 1:
  405. state = 2
  406. if state != 2:
  407. raise Exception("Didn't observe TIM bit getting set and unset (state=%d)" % state)
  408. @remote_compatible
  409. def test_ap_open_select_network(dev, apdev):
  410. """Open mode connection and SELECT_NETWORK to change network"""
  411. hapd1 = hostapd.add_ap(apdev[0], { "ssid": "open" })
  412. bssid1 = apdev[0]['bssid']
  413. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open2" })
  414. bssid2 = apdev[1]['bssid']
  415. id1 = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  416. only_add_network=True)
  417. id2 = dev[0].connect("open2", key_mgmt="NONE", scan_freq="2412")
  418. hwsim_utils.test_connectivity(dev[0], hapd2)
  419. dev[0].select_network(id1)
  420. dev[0].wait_connected()
  421. res = dev[0].request("BLACKLIST")
  422. if bssid1 in res or bssid2 in res:
  423. raise Exception("Unexpected blacklist entry")
  424. hwsim_utils.test_connectivity(dev[0], hapd1)
  425. dev[0].select_network(id2)
  426. dev[0].wait_connected()
  427. hwsim_utils.test_connectivity(dev[0], hapd2)
  428. res = dev[0].request("BLACKLIST")
  429. if bssid1 in res or bssid2 in res:
  430. raise Exception("Unexpected blacklist entry(2)")
  431. @remote_compatible
  432. def test_ap_open_disable_enable(dev, apdev):
  433. """AP with open mode getting disabled and re-enabled"""
  434. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  435. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  436. bg_scan_period="0")
  437. for i in range(2):
  438. hapd.request("DISABLE")
  439. dev[0].wait_disconnected()
  440. hapd.request("ENABLE")
  441. dev[0].wait_connected()
  442. hwsim_utils.test_connectivity(dev[0], hapd)
  443. def sta_enable_disable(dev, bssid):
  444. dev.scan_for_bss(bssid, freq=2412)
  445. work_id = dev.request("RADIO_WORK add block-work")
  446. ev = dev.wait_event(["EXT-RADIO-WORK-START"])
  447. if ev is None:
  448. raise Exception("Timeout while waiting radio work to start")
  449. id = dev.connect("open", key_mgmt="NONE", scan_freq="2412",
  450. only_add_network=True)
  451. dev.request("ENABLE_NETWORK %d" % id)
  452. if "connect@" not in dev.request("RADIO_WORK show"):
  453. raise Exception("connect radio work missing")
  454. dev.request("DISABLE_NETWORK %d" % id)
  455. dev.request("RADIO_WORK done " + work_id)
  456. ok = False
  457. for i in range(30):
  458. if "connect@" not in dev.request("RADIO_WORK show"):
  459. ok = True
  460. break
  461. time.sleep(0.1)
  462. if not ok:
  463. raise Exception("connect radio work not completed")
  464. ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  465. if ev is not None:
  466. raise Exception("Unexpected connection")
  467. dev.request("DISCONNECT")
  468. def test_ap_open_sta_enable_disable(dev, apdev):
  469. """AP with open mode and wpa_supplicant ENABLE/DISABLE_NETWORK"""
  470. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  471. bssid = apdev[0]['bssid']
  472. sta_enable_disable(dev[0], bssid)
  473. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  474. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  475. sta_enable_disable(wpas, bssid)
  476. @remote_compatible
  477. def test_ap_open_select_twice(dev, apdev):
  478. """AP with open mode and select network twice"""
  479. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  480. only_add_network=True)
  481. dev[0].select_network(id)
  482. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  483. if ev is None:
  484. raise Exception("No result reported")
  485. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  486. # Verify that the second SELECT_NETWORK starts a new scan immediately by
  487. # waiting less than the default scan period.
  488. dev[0].select_network(id)
  489. dev[0].wait_connected(timeout=3)
  490. @remote_compatible
  491. def test_ap_open_reassoc_not_found(dev, apdev):
  492. """AP with open mode and REASSOCIATE not finding a match"""
  493. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  494. only_add_network=True)
  495. dev[0].select_network(id)
  496. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  497. if ev is None:
  498. raise Exception("No result reported")
  499. dev[0].request("DISCONNECT")
  500. time.sleep(0.1)
  501. dev[0].dump_monitor()
  502. dev[0].request("REASSOCIATE")
  503. ev = dev[0].wait_event(["CTRL-EVENT-NETWORK-NOT-FOUND"], timeout=10)
  504. if ev is None:
  505. raise Exception("No result reported")
  506. dev[0].request("DISCONNECT")
  507. @remote_compatible
  508. def test_ap_open_sta_statistics(dev, apdev):
  509. """AP with open mode and STA statistics"""
  510. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  511. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  512. addr = dev[0].own_addr()
  513. stats1 = hapd.get_sta(addr)
  514. logger.info("stats1: " + str(stats1))
  515. time.sleep(0.4)
  516. stats2 = hapd.get_sta(addr)
  517. logger.info("stats2: " + str(stats2))
  518. hwsim_utils.test_connectivity(dev[0], hapd)
  519. stats3 = hapd.get_sta(addr)
  520. logger.info("stats3: " + str(stats3))
  521. # Cannot require specific inactive_msec changes without getting rid of all
  522. # unrelated traffic, so for now, just print out the results in the log for
  523. # manual checks.
  524. @remote_compatible
  525. def test_ap_open_poll_sta(dev, apdev):
  526. """AP with open mode and STA poll"""
  527. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  528. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  529. addr = dev[0].own_addr()
  530. if "OK" not in hapd.request("POLL_STA " + addr):
  531. raise Exception("POLL_STA failed")
  532. ev = hapd.wait_event(["AP-STA-POLL-OK"], timeout=5)
  533. if ev is None:
  534. raise Exception("Poll response not seen")
  535. if addr not in ev:
  536. raise Exception("Unexpected poll response: " + ev)
  537. def test_ap_open_pmf_default(dev, apdev):
  538. """AP with open mode (no security) configuration and pmf=2"""
  539. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  540. dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
  541. ieee80211w="2", wait_connect=False)
  542. dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
  543. ieee80211w="1")
  544. try:
  545. dev[0].request("SET pmf 2")
  546. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  547. dev[0].request("DISCONNECT")
  548. dev[0].wait_disconnected()
  549. finally:
  550. dev[0].request("SET pmf 0")
  551. dev[2].request("DISCONNECT")
  552. dev[2].wait_disconnected()
  553. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
  554. if ev is not None:
  555. raise Exception("Unexpected dev[1] connection")
  556. dev[1].request("DISCONNECT")
  557. def test_ap_open_drv_fail(dev, apdev):
  558. """AP with open mode and driver operations failing"""
  559. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  560. with fail_test(dev[0], 1, "wpa_driver_nl80211_authenticate"):
  561. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  562. wait_connect=False)
  563. wait_fail_trigger(dev[0], "GET_FAIL")
  564. dev[0].request("REMOVE_NETWORK all")
  565. with fail_test(dev[0], 1, "wpa_driver_nl80211_associate"):
  566. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
  567. wait_connect=False)
  568. wait_fail_trigger(dev[0], "GET_FAIL")
  569. dev[0].request("REMOVE_NETWORK all")
  570. def run_multicast_to_unicast(dev, apdev, convert):
  571. params = { "ssid": "open" }
  572. params["multicast_to_unicast"] = "1" if convert else "0"
  573. hapd = hostapd.add_ap(apdev[0], params)
  574. dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
  575. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  576. ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
  577. if ev is None:
  578. raise Exception("No connection event received from hostapd")
  579. hwsim_utils.test_connectivity(dev[0], hapd, multicast_to_unicast=convert)
  580. dev[0].request("DISCONNECT")
  581. ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
  582. if ev is None:
  583. raise Exception("No disconnection event received from hostapd")
  584. def test_ap_open_multicast_to_unicast(dev, apdev):
  585. """Multicast-to-unicast conversion enabled"""
  586. run_multicast_to_unicast(dev, apdev, True)
  587. def test_ap_open_multicast_to_unicast_disabled(dev, apdev):
  588. """Multicast-to-unicast conversion disabled"""
  589. run_multicast_to_unicast(dev, apdev, False)
  590. def test_ap_open_drop_duplicate(dev, apdev, params):
  591. """AP dropping duplicate management frames"""
  592. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  593. "interworking": "1" })
  594. hapd.set("ext_mgmt_frame_handling", "1")
  595. bssid = hapd.own_addr().replace(':', '')
  596. addr = "020304050607"
  597. auth = "b0003a01" + bssid + addr + bssid + '1000000001000000'
  598. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
  599. raise Exception("MGMT_RX_PROCESS failed")
  600. auth = "b0083a01" + bssid + addr + bssid + '1000000001000000'
  601. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % auth):
  602. raise Exception("MGMT_RX_PROCESS failed")
  603. ies = "00046f70656e010802040b160c12182432043048606c2d1a3c101bffff0000000000000000000001000000000000000000007f0a04000a020140004000013b155151525354737475767778797a7b7c7d7e7f808182dd070050f202000100"
  604. assoc_req = "00003a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  605. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
  606. raise Exception("MGMT_RX_PROCESS failed")
  607. assoc_req = "00083a01" + bssid + addr + bssid + "2000" + "21040500" + ies
  608. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % assoc_req):
  609. raise Exception("MGMT_RX_PROCESS failed")
  610. reassoc_req = "20083a01" + 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" % reassoc_req):
  612. raise Exception("MGMT_RX_PROCESS failed")
  613. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % reassoc_req):
  614. raise Exception("MGMT_RX_PROCESS failed")
  615. action = "d0003a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
  616. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
  617. raise Exception("MGMT_RX_PROCESS failed")
  618. action = "d0083a01" + bssid + addr + bssid + "1000" + "040a006c0200000600000102000101"
  619. if "OK" not in hapd.request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=%s" % action):
  620. raise Exception("MGMT_RX_PROCESS failed")
  621. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  622. "wlan.fc.type == 0", ["wlan.fc.subtype"])
  623. num_auth = 0
  624. num_assoc = 0
  625. num_reassoc = 0
  626. num_action = 0
  627. for subtype in out.splitlines():
  628. val = int(subtype)
  629. if val == 11:
  630. num_auth += 1
  631. elif val == 1:
  632. num_assoc += 1
  633. elif val == 3:
  634. num_reassoc += 1
  635. elif val == 13:
  636. num_action += 1
  637. if num_auth != 1:
  638. raise Exception("Unexpected number of Authentication frames: %d" % num_auth)
  639. if num_assoc != 1:
  640. raise Exception("Unexpected number of association frames: %d" % num_assoc)
  641. if num_reassoc != 1:
  642. raise Exception("Unexpected number of reassociation frames: %d" % num_reassoc)
  643. if num_action != 1:
  644. raise Exception("Unexpected number of Action frames: %d" % num_action)
  645. def test_ap_open_select_network_freq(dev, apdev):
  646. """AP with open mode and use for SELECT_NETWORK freq parameter"""
  647. hapd = hostapd.add_ap(apdev[0], { "ssid": "open" })
  648. id = dev[0].connect("open", key_mgmt="NONE", only_add_network=True)
  649. dev[0].select_network(id, freq=2412)
  650. start = os.times()[4]
  651. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
  652. if ev is None:
  653. raise Exception("Scan not started")
  654. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
  655. if ev is None:
  656. raise Exception("Scan not completed")
  657. end = os.times()[4]
  658. logger.info("Scan duration: {} seconds".format(end - start))
  659. if end - start > 3:
  660. raise Exception("Scan took unexpectedly long time")
  661. dev[0].wait_connected()
  662. def test_ap_open_noncountry(dev, apdev):
  663. """AP with open mode and noncountry entity as Country String"""
  664. _test_ap_open_country(dev, apdev, "XX", "0x58")
  665. def test_ap_open_country_table_e4(dev, apdev):
  666. """AP with open mode and Table E-4 Country String"""
  667. _test_ap_open_country(dev, apdev, "DE", "0x04")
  668. def test_ap_open_country_indoor(dev, apdev):
  669. """AP with open mode and indoor country code"""
  670. _test_ap_open_country(dev, apdev, "DE", "0x49")
  671. def test_ap_open_country_outdoor(dev, apdev):
  672. """AP with open mode and outdoor country code"""
  673. _test_ap_open_country(dev, apdev, "DE", "0x4f")
  674. def _test_ap_open_country(dev, apdev, country_code, country3):
  675. try:
  676. run_ap_open_country(dev, apdev, country_code, country3)
  677. finally:
  678. dev[0].request("DISCONNECT")
  679. set_world_reg(apdev[0], apdev[1], dev[0])
  680. dev[0].flush_scan_cache()
  681. def run_ap_open_country(dev, apdev, country_code, country3):
  682. hapd = hostapd.add_ap(apdev[0], { "ssid": "open",
  683. "country_code": country_code,
  684. "country3": country3,
  685. "ieee80211d": "1" })
  686. dev[0].scan_for_bss(hapd.own_addr(), freq=2412)
  687. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  688. dev[0].request("DISCONNECT")
  689. dev[0].wait_disconnected()
  690. def test_ap_open_disable_select(dev, apdev):
  691. """DISABLE_NETWORK for connected AP followed by SELECT_NETWORK"""
  692. hapd1 = hostapd.add_ap(apdev[0], { "ssid": "open" })
  693. hapd2 = hostapd.add_ap(apdev[1], { "ssid": "open" })
  694. id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  695. dev[0].request("DISABLE_NETWORK %d" % id)
  696. dev[0].wait_disconnected()
  697. res = dev[0].request("BLACKLIST")
  698. if hapd1.own_addr() in res or hapd2.own_addr() in res:
  699. raise Exception("Unexpected blacklist entry added")
  700. dev[0].request("SELECT_NETWORK %d" % id)
  701. dev[0].wait_connected()