test_ap_track.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. # Test cases for hostapd tracking unconnected stations
  2. # Copyright (c) 2015, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import logging
  7. logger = logging.getLogger()
  8. import subprocess
  9. import time
  10. import hostapd
  11. from wpasupplicant import WpaSupplicant
  12. from utils import parse_ie
  13. def test_ap_track_sta(dev, apdev):
  14. """Dualband AP tracking unconnected stations"""
  15. try:
  16. _test_ap_track_sta(dev, apdev)
  17. finally:
  18. subprocess.call(['iw', 'reg', 'set', '00'])
  19. def _test_ap_track_sta(dev, apdev):
  20. params = { "ssid": "track",
  21. "country_code": "US",
  22. "hw_mode": "g",
  23. "channel": "6",
  24. "track_sta_max_num": "2" }
  25. hapd = hostapd.add_ap(apdev[0], params)
  26. bssid = apdev[0]['bssid']
  27. params = { "ssid": "track",
  28. "country_code": "US",
  29. "hw_mode": "a",
  30. "channel": "40",
  31. "track_sta_max_num": "100",
  32. "track_sta_max_age": "1" }
  33. hapd2 = hostapd.add_ap(apdev[1], params)
  34. bssid2 = apdev[1]['bssid']
  35. for i in range(2):
  36. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  37. dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
  38. dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
  39. dev[2].scan_for_bss(bssid2, freq=5200, force_scan=True)
  40. addr0 = dev[0].own_addr()
  41. addr1 = dev[1].own_addr()
  42. addr2 = dev[2].own_addr()
  43. track = hapd.request("TRACK_STA_LIST")
  44. if addr0 not in track or addr1 not in track:
  45. raise Exception("Station missing from 2.4 GHz tracking")
  46. if addr2 in track:
  47. raise Exception("Unexpected station included in 2.4 GHz tracking")
  48. track = hapd2.request("TRACK_STA_LIST")
  49. if addr0 not in track or addr2 not in track:
  50. raise Exception("Station missing from 5 GHz tracking")
  51. if addr1 in track:
  52. raise Exception("Unexpected station included in 5 GHz tracking")
  53. # Test expiration
  54. time.sleep(1.1)
  55. track = hapd.request("TRACK_STA_LIST")
  56. if addr0 not in track or addr1 not in track:
  57. raise Exception("Station missing from 2.4 GHz tracking (expiration)")
  58. track = hapd2.request("TRACK_STA_LIST")
  59. if addr0 in track or addr2 in track:
  60. raise Exception("Station not expired from 5 GHz tracking")
  61. # Test maximum list length
  62. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  63. dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
  64. dev[2].scan_for_bss(bssid, freq=2437, force_scan=True)
  65. track = hapd.request("TRACK_STA_LIST")
  66. if len(track.splitlines()) != 2:
  67. raise Exception("Unexpected number of entries: %d" % len(track.splitlines()))
  68. if addr1 not in track or addr2 not in track:
  69. raise Exception("Station missing from 2.4 GHz tracking (max limit)")
  70. def test_ap_track_sta_no_probe_resp(dev, apdev):
  71. """Dualband AP not replying to probes from dualband STA on 2.4 GHz"""
  72. try:
  73. _test_ap_track_sta_no_probe_resp(dev, apdev)
  74. finally:
  75. subprocess.call(['iw', 'reg', 'set', '00'])
  76. def _test_ap_track_sta_no_probe_resp(dev, apdev):
  77. dev[0].flush_scan_cache()
  78. params = { "ssid": "track",
  79. "country_code": "US",
  80. "hw_mode": "g",
  81. "channel": "6",
  82. "beacon_int": "10000",
  83. "no_probe_resp_if_seen_on": apdev[1]['ifname'] }
  84. hapd = hostapd.add_ap(apdev[0], params)
  85. bssid = apdev[0]['bssid']
  86. params = { "ssid": "track",
  87. "country_code": "US",
  88. "hw_mode": "a",
  89. "channel": "40",
  90. "track_sta_max_num": "100" }
  91. hapd2 = hostapd.add_ap(apdev[1], params)
  92. bssid2 = apdev[1]['bssid']
  93. dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
  94. dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
  95. dev[0].scan(freq=2437, type="ONLY")
  96. dev[0].scan(freq=2437, type="ONLY")
  97. bss = dev[0].get_bss(bssid)
  98. if bss:
  99. ie = parse_ie(bss['ie'])
  100. # Check whether this is from a Beacon frame (TIM element included) since
  101. # it is possible that a Beacon frame was received during the active
  102. # scan. This test should fail only if a Probe Response frame was
  103. # received.
  104. if 5 not in ie:
  105. raise Exception("2.4 GHz AP found unexpectedly")
  106. def test_ap_track_sta_no_auth(dev, apdev):
  107. """Dualband AP rejecting authentication from dualband STA on 2.4 GHz"""
  108. try:
  109. _test_ap_track_sta_no_auth(dev, apdev)
  110. finally:
  111. subprocess.call(['iw', 'reg', 'set', '00'])
  112. def _test_ap_track_sta_no_auth(dev, apdev):
  113. params = { "ssid": "track",
  114. "country_code": "US",
  115. "hw_mode": "g",
  116. "channel": "6",
  117. "track_sta_max_num": "100",
  118. "no_auth_if_seen_on": apdev[1]['ifname'] }
  119. hapd = hostapd.add_ap(apdev[0], params)
  120. bssid = apdev[0]['bssid']
  121. params = { "ssid": "track",
  122. "country_code": "US",
  123. "hw_mode": "a",
  124. "channel": "40",
  125. "track_sta_max_num": "100" }
  126. hapd2 = hostapd.add_ap(apdev[1], params)
  127. bssid2 = apdev[1]['bssid']
  128. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  129. dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
  130. dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
  131. dev[1].connect("track", key_mgmt="NONE", scan_freq="2437")
  132. dev[0].connect("track", key_mgmt="NONE", scan_freq="2437",
  133. freq_list="2437", wait_connect=False)
  134. dev[1].request("DISCONNECT")
  135. ev = dev[0].wait_event([ "CTRL-EVENT-CONNECTED",
  136. "CTRL-EVENT-AUTH-REJECT" ], timeout=10)
  137. if ev is None:
  138. raise Exception("Unknown connection result")
  139. if "CTRL-EVENT-CONNECTED" in ev:
  140. raise Exception("Unexpected connection")
  141. if "status_code=82" not in ev:
  142. raise Exception("Unexpected rejection reason: " + ev)
  143. if "ie=34" not in ev:
  144. raise Exception("No Neighbor Report element: " + ev)
  145. dev[0].request("DISCONNECT")
  146. def test_ap_track_sta_no_auth_passive(dev, apdev):
  147. """AP rejecting authentication from dualband STA on 2.4 GHz (passive)"""
  148. try:
  149. _test_ap_track_sta_no_auth_passive(dev, apdev)
  150. finally:
  151. subprocess.call(['iw', 'reg', 'set', '00'])
  152. def _test_ap_track_sta_no_auth_passive(dev, apdev):
  153. dev[0].flush_scan_cache()
  154. params = { "ssid": "track",
  155. "country_code": "US",
  156. "hw_mode": "g",
  157. "channel": "6",
  158. "no_auth_if_seen_on": apdev[1]['ifname'] }
  159. hapd = hostapd.add_ap(apdev[0], params)
  160. bssid = apdev[0]['bssid']
  161. params = { "ssid": "track",
  162. "country_code": "US",
  163. "hw_mode": "a",
  164. "channel": "40",
  165. "interworking": "1",
  166. "venue_name": "eng:Venue",
  167. "track_sta_max_num": "100" }
  168. hapd2 = hostapd.add_ap(apdev[1], params)
  169. bssid2 = apdev[1]['bssid']
  170. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  171. for i in range(10):
  172. dev[0].request("SCAN freq=5200 passive=1")
  173. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=5)
  174. if ev is None:
  175. raise Exception("Scan did not complete")
  176. if dev[0].get_bss(bssid2):
  177. break
  178. if i == 9:
  179. raise Exception("AP not found with passive scans")
  180. if "OK" not in dev[0].request("ANQP_GET " + bssid2 + " 258"):
  181. raise Exception("ANQP_GET command failed")
  182. ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
  183. if ev is None or "Venue Name" not in ev:
  184. raise Exception("Did not receive Venue Name")
  185. dev[0].connect("track", key_mgmt="NONE", scan_freq="2437",
  186. freq_list="2437", wait_connect=False)
  187. ev = dev[0].wait_event([ "CTRL-EVENT-CONNECTED",
  188. "CTRL-EVENT-AUTH-REJECT" ], timeout=10)
  189. if ev is None:
  190. raise Exception("Unknown connection result")
  191. if "CTRL-EVENT-CONNECTED" in ev:
  192. raise Exception("Unexpected connection")
  193. if "status_code=82" not in ev:
  194. raise Exception("Unexpected rejection reason: " + ev)
  195. dev[0].request("DISCONNECT")
  196. def test_ap_track_sta_force_5ghz(dev, apdev):
  197. """Dualband AP forcing dualband STA to connect on 5 GHz"""
  198. try:
  199. _test_ap_track_sta_force_5ghz(dev, apdev)
  200. finally:
  201. subprocess.call(['iw', 'reg', 'set', '00'])
  202. def _test_ap_track_sta_force_5ghz(dev, apdev):
  203. params = { "ssid": "track",
  204. "country_code": "US",
  205. "hw_mode": "g",
  206. "channel": "6",
  207. "no_probe_resp_if_seen_on": apdev[1]['ifname'],
  208. "no_auth_if_seen_on": apdev[1]['ifname'] }
  209. hapd = hostapd.add_ap(apdev[0], params)
  210. bssid = apdev[0]['bssid']
  211. params = { "ssid": "track",
  212. "country_code": "US",
  213. "hw_mode": "a",
  214. "channel": "40",
  215. "track_sta_max_num": "100" }
  216. hapd2 = hostapd.add_ap(apdev[1], params)
  217. bssid2 = apdev[1]['bssid']
  218. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  219. dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
  220. dev[0].connect("track", key_mgmt="NONE", scan_freq="2437 5200")
  221. freq = dev[0].get_status_field('freq')
  222. if freq != '5200':
  223. raise Exception("Unexpected operating channel")
  224. dev[0].request("DISCONNECT")
  225. def test_ap_track_sta_force_2ghz(dev, apdev):
  226. """Dualband AP forcing dualband STA to connect on 2.4 GHz"""
  227. try:
  228. _test_ap_track_sta_force_2ghz(dev, apdev)
  229. finally:
  230. subprocess.call(['iw', 'reg', 'set', '00'])
  231. def _test_ap_track_sta_force_2ghz(dev, apdev):
  232. params = { "ssid": "track",
  233. "country_code": "US",
  234. "hw_mode": "g",
  235. "channel": "6",
  236. "track_sta_max_num": "100" }
  237. hapd = hostapd.add_ap(apdev[0], params)
  238. bssid = apdev[0]['bssid']
  239. params = { "ssid": "track",
  240. "country_code": "US",
  241. "hw_mode": "a",
  242. "channel": "40",
  243. "no_probe_resp_if_seen_on": apdev[0]['ifname'],
  244. "no_auth_if_seen_on": apdev[0]['ifname'] }
  245. hapd2 = hostapd.add_ap(apdev[1], params)
  246. bssid2 = apdev[1]['bssid']
  247. dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
  248. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  249. dev[0].connect("track", key_mgmt="NONE", scan_freq="2437 5200")
  250. freq = dev[0].get_status_field('freq')
  251. if freq != '2437':
  252. raise Exception("Unexpected operating channel")
  253. dev[0].request("DISCONNECT")
  254. def test_ap_track_taxonomy(dev, apdev):
  255. """AP tracking STA taxonomy"""
  256. try:
  257. _test_ap_track_taxonomy(dev, apdev)
  258. finally:
  259. dev[1].request("SET p2p_disabled 0")
  260. subprocess.call(['iw', 'reg', 'set', '00'])
  261. dev[0].flush_scan_cache()
  262. dev[1].flush_scan_cache()
  263. dev[2].flush_scan_cache()
  264. def _test_ap_track_taxonomy(dev, apdev):
  265. params = { "ssid": "track",
  266. "country_code": "US",
  267. "hw_mode": "g",
  268. "channel": "6",
  269. "track_sta_max_num": "2" }
  270. hapd = hostapd.add_ap(apdev[0], params)
  271. bssid = apdev[0]['bssid']
  272. dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
  273. addr0 = dev[0].own_addr()
  274. dev[0].connect("track", key_mgmt="NONE", scan_freq="2437")
  275. dev[1].request("SET p2p_disabled 1")
  276. dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
  277. addr1 = dev[1].own_addr()
  278. dev[1].connect("track", key_mgmt="NONE", scan_freq="2437")
  279. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  280. wpas.interface_add("wlan5")
  281. wpas.request("SET model_name track test")
  282. wpas.scan_for_bss(bssid, freq=2437, force_scan=True)
  283. addr = wpas.own_addr()
  284. wpas.connect("track", key_mgmt="NONE", scan_freq="2437")
  285. if "FAIL" not in hapd.request("SIGNATURE abc"):
  286. raise Exception("SIGNATURE failure not reported (1)")
  287. if "FAIL" not in hapd.request("SIGNATURE 22:33:44:55:66:77"):
  288. raise Exception("SIGNATURE failure not reported (2)")
  289. res = hapd.request("SIGNATURE " + addr0)
  290. logger.info("sta0: " + res)
  291. if not res.startswith("wifi4|probe:"):
  292. raise Exception("Unexpected SIGNATURE prefix")
  293. if "|assoc:" not in res:
  294. raise Exception("Missing assoc info in SIGNATURE")
  295. if "wps:track_test" in res:
  296. raise Exception("Unexpected WPS model name")
  297. res = hapd.request("SIGNATURE " + addr1)
  298. logger.info("sta1: " + res)
  299. if not res.startswith("wifi4|probe:"):
  300. raise Exception("Unexpected SIGNATURE prefix")
  301. if "|assoc:" not in res:
  302. raise Exception("Missing assoc info in SIGNATURE")
  303. if "wps:" in res:
  304. raise Exception("Unexpected WPS info");
  305. if ",221(0050f2,4)," in res:
  306. raise Exception("Unexpected WPS IE info");
  307. if ",221(506f9a,9)," in res:
  308. raise Exception("Unexpected P2P IE info");
  309. res = hapd.request("SIGNATURE " + addr)
  310. logger.info("sta: " + res)
  311. if not res.startswith("wifi4|probe:"):
  312. raise Exception("Unexpected SIGNATURE prefix")
  313. if "|assoc:" not in res:
  314. raise Exception("Missing assoc info in SIGNATURE")
  315. if "wps:track_test" not in res:
  316. raise Exception("Missing WPS model name")
  317. if ",221(0050f2,4)," not in res:
  318. raise Exception("Missing WPS IE info");
  319. if ",221(506f9a,9)," not in res:
  320. raise Exception("Missing P2P IE info");
  321. addr2 = dev[2].own_addr()
  322. res = hapd.request("SIGNATURE " + addr2)
  323. if "FAIL" not in res:
  324. raise Exception("Unexpected SIGNATURE success for sta2 (1)")
  325. for i in range(10):
  326. dev[2].request("SCAN freq=2437 passive=1")
  327. ev = dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
  328. if ev is None:
  329. raise Exception("Scan did not complete")
  330. if dev[2].get_bss(bssid):
  331. break
  332. res = hapd.request("SIGNATURE " + addr2)
  333. if "FAIL" not in res:
  334. raise Exception("Unexpected SIGNATURE success for sta2 (2)")
  335. dev[2].connect("track", key_mgmt="NONE", scan_freq="2437")
  336. res = hapd.request("SIGNATURE " + addr2)
  337. if "FAIL" not in res and len(res) > 0:
  338. raise Exception("Unexpected SIGNATURE success for sta2 (3)")
  339. dev[2].scan_for_bss(bssid, freq=2437, force_scan=True)
  340. res = hapd.request("SIGNATURE " + addr2)
  341. logger.info("sta2: " + res)
  342. if not res.startswith("wifi4|probe:"):
  343. raise Exception("Unexpected SIGNATURE prefix")
  344. if "|assoc:" not in res:
  345. raise Exception("Missing assoc info in SIGNATURE")