test_ap_tdls.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # TDLS tests
  2. # Copyright (c) 2013-2014, 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 time
  7. import logging
  8. logger = logging.getLogger()
  9. import subprocess
  10. import hwsim_utils
  11. from hostapd import HostapdGlobal
  12. from hostapd import Hostapd
  13. import hostapd
  14. from wlantest import Wlantest
  15. def start_ap_wpa2_psk(ifname):
  16. params = hostapd.wpa2_params(ssid="test-wpa2-psk", passphrase="12345678")
  17. return hostapd.add_ap(ifname, params)
  18. def connectivity(dev, hapd):
  19. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  20. hwsim_utils.test_connectivity(dev[0], hapd)
  21. hwsim_utils.test_connectivity(dev[1], hapd)
  22. def connect_2sta(dev, ssid, hapd):
  23. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  24. dev[1].connect(ssid, psk="12345678", scan_freq="2412")
  25. connectivity(dev, hapd)
  26. def connect_2sta_wpa2_psk(dev, hapd):
  27. connect_2sta(dev, "test-wpa2-psk", hapd)
  28. def connect_2sta_wpa_psk(dev, hapd):
  29. connect_2sta(dev, "test-wpa-psk", hapd)
  30. def connect_2sta_wpa_psk_mixed(dev, hapd):
  31. dev[0].connect("test-wpa-mixed-psk", psk="12345678", proto="WPA",
  32. scan_freq="2412")
  33. dev[1].connect("test-wpa-mixed-psk", psk="12345678", proto="WPA2",
  34. scan_freq="2412")
  35. connectivity(dev, hapd)
  36. def connect_2sta_wep(dev, hapd):
  37. dev[0].connect("test-wep", key_mgmt="NONE", wep_key0='"hello"',
  38. scan_freq="2412")
  39. dev[1].connect("test-wep", key_mgmt="NONE", wep_key0='"hello"',
  40. scan_freq="2412")
  41. connectivity(dev, hapd)
  42. def connect_2sta_open(dev, hapd, scan_freq="2412"):
  43. dev[0].connect("test-open", key_mgmt="NONE", scan_freq=scan_freq)
  44. dev[1].connect("test-open", key_mgmt="NONE", scan_freq=scan_freq)
  45. connectivity(dev, hapd)
  46. def wlantest_setup():
  47. wt = Wlantest()
  48. wt.flush()
  49. wt.add_passphrase("12345678")
  50. wt.add_wepkey("68656c6c6f")
  51. def wlantest_tdls_packet_counters(bssid, addr0, addr1):
  52. wt = Wlantest()
  53. dl = wt.get_tdls_counter("valid_direct_link", bssid, addr0, addr1)
  54. inv_dl = wt.get_tdls_counter("invalid_direct_link", bssid, addr0, addr1)
  55. ap = wt.get_tdls_counter("valid_ap_path", bssid, addr0, addr1)
  56. inv_ap = wt.get_tdls_counter("invalid_ap_path", bssid, addr0, addr1)
  57. return [dl,inv_dl,ap,inv_ap]
  58. def tdls_check_dl(sta0, sta1, bssid, addr0, addr1):
  59. wt = Wlantest()
  60. wt.tdls_clear(bssid, addr0, addr1)
  61. hwsim_utils.test_connectivity_sta(sta0, sta1)
  62. [dl,inv_dl,ap,inv_ap] = wlantest_tdls_packet_counters(bssid, addr0, addr1)
  63. if dl == 0:
  64. raise Exception("No valid frames through direct link")
  65. if inv_dl > 0:
  66. raise Exception("Invalid frames through direct link")
  67. if ap > 0:
  68. raise Exception("Unexpected frames through AP path")
  69. if inv_ap > 0:
  70. raise Exception("Invalid frames through AP path")
  71. def tdls_check_ap(sta0, sta1, bssid, addr0, addr1):
  72. wt = Wlantest()
  73. wt.tdls_clear(bssid, addr0, addr1);
  74. hwsim_utils.test_connectivity_sta(sta0, sta1)
  75. [dl,inv_dl,ap,inv_ap] = wlantest_tdls_packet_counters(bssid, addr0, addr1)
  76. if dl > 0:
  77. raise Exception("Unexpected frames through direct link")
  78. if inv_dl > 0:
  79. raise Exception("Invalid frames through direct link")
  80. if ap == 0:
  81. raise Exception("No valid frames through AP path")
  82. if inv_ap > 0:
  83. raise Exception("Invalid frames through AP path")
  84. def check_connectivity(sta0, sta1, hapd):
  85. hwsim_utils.test_connectivity_sta(sta0, sta1)
  86. hwsim_utils.test_connectivity(sta0, hapd)
  87. hwsim_utils.test_connectivity(sta1, hapd)
  88. def setup_tdls(sta0, sta1, ap, reverse=False, expect_fail=False):
  89. logger.info("Setup TDLS")
  90. hapd = hostapd.Hostapd(ap['ifname'])
  91. check_connectivity(sta0, sta1, hapd)
  92. bssid = ap['bssid']
  93. addr0 = sta0.p2p_interface_addr()
  94. addr1 = sta1.p2p_interface_addr()
  95. wt = Wlantest()
  96. wt.tdls_clear(bssid, addr0, addr1);
  97. wt.tdls_clear(bssid, addr1, addr0);
  98. sta0.tdls_setup(addr1)
  99. time.sleep(1)
  100. if expect_fail:
  101. tdls_check_ap(sta0, sta1, bssid, addr0, addr1)
  102. return
  103. if reverse:
  104. addr1 = sta0.p2p_interface_addr()
  105. addr0 = sta1.p2p_interface_addr()
  106. conf = wt.get_tdls_counter("setup_conf_ok", bssid, addr0, addr1);
  107. if conf == 0:
  108. raise Exception("No TDLS Setup Confirm (success) seen")
  109. tdls_check_dl(sta0, sta1, bssid, addr0, addr1)
  110. check_connectivity(sta0, sta1, hapd)
  111. def teardown_tdls(sta0, sta1, ap, responder=False, wildcard=False):
  112. logger.info("Teardown TDLS")
  113. hapd = hostapd.Hostapd(ap['ifname'])
  114. check_connectivity(sta0, sta1, hapd)
  115. bssid = ap['bssid']
  116. addr0 = sta0.p2p_interface_addr()
  117. addr1 = sta1.p2p_interface_addr()
  118. if responder:
  119. sta1.tdls_teardown(addr0)
  120. elif wildcard:
  121. sta0.tdls_teardown("*")
  122. else:
  123. sta0.tdls_teardown(addr1)
  124. time.sleep(1)
  125. wt = Wlantest()
  126. teardown = wt.get_tdls_counter("teardown", bssid, addr0, addr1);
  127. if teardown == 0:
  128. raise Exception("No TDLS Setup Teardown seen")
  129. tdls_check_ap(sta0, sta1, bssid, addr0, addr1)
  130. check_connectivity(sta0, sta1, hapd)
  131. def test_ap_tdls_discovery(dev, apdev):
  132. """WPA2-PSK AP and two stations using TDLS discovery"""
  133. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  134. wlantest_setup()
  135. connect_2sta_wpa2_psk(dev, hapd)
  136. dev[0].request("TDLS_DISCOVER " + dev[1].p2p_interface_addr())
  137. time.sleep(0.2)
  138. def test_ap_wpa2_tdls(dev, apdev):
  139. """WPA2-PSK AP and two stations using TDLS"""
  140. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  141. wlantest_setup()
  142. connect_2sta_wpa2_psk(dev, hapd)
  143. setup_tdls(dev[0], dev[1], apdev[0])
  144. teardown_tdls(dev[0], dev[1], apdev[0])
  145. setup_tdls(dev[1], dev[0], apdev[0])
  146. #teardown_tdls(dev[0], dev[1], apdev[0])
  147. def test_ap_wpa2_tdls_concurrent_init(dev, apdev):
  148. """Concurrent TDLS setup initiation"""
  149. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  150. wlantest_setup()
  151. connect_2sta_wpa2_psk(dev, hapd)
  152. dev[0].request("SET tdls_testing 0x80")
  153. setup_tdls(dev[1], dev[0], apdev[0], reverse=True)
  154. def test_ap_wpa2_tdls_concurrent_init2(dev, apdev):
  155. """Concurrent TDLS setup initiation (reverse)"""
  156. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  157. wlantest_setup()
  158. connect_2sta_wpa2_psk(dev, hapd)
  159. dev[1].request("SET tdls_testing 0x80")
  160. setup_tdls(dev[0], dev[1], apdev[0])
  161. def test_ap_wpa2_tdls_decline_resp(dev, apdev):
  162. """Decline TDLS Setup Response"""
  163. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  164. wlantest_setup()
  165. connect_2sta_wpa2_psk(dev, hapd)
  166. dev[1].request("SET tdls_testing 0x200")
  167. setup_tdls(dev[1], dev[0], apdev[0], expect_fail=True)
  168. def test_ap_wpa2_tdls_long_lifetime(dev, apdev):
  169. """TDLS with long TPK lifetime"""
  170. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  171. wlantest_setup()
  172. connect_2sta_wpa2_psk(dev, hapd)
  173. dev[1].request("SET tdls_testing 0x40")
  174. setup_tdls(dev[1], dev[0], apdev[0])
  175. def test_ap_wpa2_tdls_long_frame(dev, apdev):
  176. """TDLS with long setup/teardown frames"""
  177. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  178. wlantest_setup()
  179. connect_2sta_wpa2_psk(dev, hapd)
  180. dev[0].request("SET tdls_testing 0x1")
  181. dev[1].request("SET tdls_testing 0x1")
  182. setup_tdls(dev[1], dev[0], apdev[0])
  183. teardown_tdls(dev[1], dev[0], apdev[0])
  184. setup_tdls(dev[0], dev[1], apdev[0])
  185. def test_ap_wpa2_tdls_reneg(dev, apdev):
  186. """Renegotiate TDLS link"""
  187. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  188. wlantest_setup()
  189. connect_2sta_wpa2_psk(dev, hapd)
  190. setup_tdls(dev[1], dev[0], apdev[0])
  191. setup_tdls(dev[0], dev[1], apdev[0])
  192. def test_ap_wpa2_tdls_wrong_lifetime_resp(dev, apdev):
  193. """Incorrect TPK lifetime in TDLS Setup Response"""
  194. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  195. wlantest_setup()
  196. connect_2sta_wpa2_psk(dev, hapd)
  197. dev[1].request("SET tdls_testing 0x10")
  198. setup_tdls(dev[0], dev[1], apdev[0], expect_fail=True)
  199. def test_ap_wpa2_tdls_diff_rsnie(dev, apdev):
  200. """TDLS with different RSN IEs"""
  201. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  202. wlantest_setup()
  203. connect_2sta_wpa2_psk(dev, hapd)
  204. dev[1].request("SET tdls_testing 0x2")
  205. setup_tdls(dev[1], dev[0], apdev[0])
  206. teardown_tdls(dev[1], dev[0], apdev[0])
  207. def test_ap_wpa2_tdls_wrong_tpk_m2_mic(dev, apdev):
  208. """Incorrect MIC in TDLS Setup Response"""
  209. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  210. wlantest_setup()
  211. connect_2sta_wpa2_psk(dev, hapd)
  212. dev[0].request("SET tdls_testing 0x800")
  213. addr0 = dev[0].p2p_interface_addr()
  214. dev[1].tdls_setup(addr0)
  215. time.sleep(1)
  216. def test_ap_wpa2_tdls_wrong_tpk_m3_mic(dev, apdev):
  217. """Incorrect MIC in TDLS Setup Confirm"""
  218. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  219. wlantest_setup()
  220. connect_2sta_wpa2_psk(dev, hapd)
  221. dev[1].request("SET tdls_testing 0x800")
  222. addr0 = dev[0].p2p_interface_addr()
  223. dev[1].tdls_setup(addr0)
  224. time.sleep(1)
  225. def test_ap_wpa_tdls(dev, apdev):
  226. """WPA-PSK AP and two stations using TDLS"""
  227. hapd = hostapd.add_ap(apdev[0]['ifname'],
  228. hostapd.wpa_params(ssid="test-wpa-psk",
  229. passphrase="12345678"))
  230. wlantest_setup()
  231. connect_2sta_wpa_psk(dev, hapd)
  232. setup_tdls(dev[0], dev[1], apdev[0])
  233. teardown_tdls(dev[0], dev[1], apdev[0])
  234. setup_tdls(dev[1], dev[0], apdev[0])
  235. def test_ap_wpa_mixed_tdls(dev, apdev):
  236. """WPA+WPA2-PSK AP and two stations using TDLS"""
  237. hapd = hostapd.add_ap(apdev[0]['ifname'],
  238. hostapd.wpa_mixed_params(ssid="test-wpa-mixed-psk",
  239. passphrase="12345678"))
  240. wlantest_setup()
  241. connect_2sta_wpa_psk_mixed(dev, hapd)
  242. setup_tdls(dev[0], dev[1], apdev[0])
  243. teardown_tdls(dev[0], dev[1], apdev[0])
  244. setup_tdls(dev[1], dev[0], apdev[0])
  245. def test_ap_wep_tdls(dev, apdev):
  246. """WEP AP and two stations using TDLS"""
  247. hapd = hostapd.add_ap(apdev[0]['ifname'],
  248. { "ssid": "test-wep", "wep_key0": '"hello"' })
  249. wlantest_setup()
  250. connect_2sta_wep(dev, hapd)
  251. setup_tdls(dev[0], dev[1], apdev[0])
  252. teardown_tdls(dev[0], dev[1], apdev[0])
  253. setup_tdls(dev[1], dev[0], apdev[0])
  254. def test_ap_open_tdls(dev, apdev):
  255. """Open AP and two stations using TDLS"""
  256. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "test-open" })
  257. wlantest_setup()
  258. connect_2sta_open(dev, hapd)
  259. setup_tdls(dev[0], dev[1], apdev[0])
  260. teardown_tdls(dev[0], dev[1], apdev[0])
  261. setup_tdls(dev[1], dev[0], apdev[0])
  262. teardown_tdls(dev[1], dev[0], apdev[0], wildcard=True)
  263. def test_ap_wpa2_tdls_bssid_mismatch(dev, apdev):
  264. """TDLS failure due to BSSID mismatch"""
  265. try:
  266. ssid = "test-wpa2-psk"
  267. passphrase = "12345678"
  268. params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
  269. params['bridge'] = 'ap-br0'
  270. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  271. hostapd.add_ap(apdev[1]['ifname'], params)
  272. wlantest_setup()
  273. subprocess.call(['sudo', 'brctl', 'setfd', 'ap-br0', '0'])
  274. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
  275. dev[0].connect(ssid, psk=passphrase, scan_freq="2412",
  276. bssid=apdev[0]['bssid'])
  277. dev[1].connect(ssid, psk=passphrase, scan_freq="2412",
  278. bssid=apdev[1]['bssid'])
  279. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  280. hwsim_utils.test_connectivity_iface(dev[0], hapd, "ap-br0")
  281. hwsim_utils.test_connectivity_iface(dev[1], hapd, "ap-br0")
  282. addr0 = dev[0].p2p_interface_addr()
  283. dev[1].tdls_setup(addr0)
  284. time.sleep(1)
  285. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  286. finally:
  287. subprocess.call(['sudo', 'ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
  288. subprocess.call(['sudo', 'brctl', 'delbr', 'ap-br0'])
  289. def test_ap_wpa2_tdls_responder_teardown(dev, apdev):
  290. """TDLS teardown from responder with WPA2-PSK AP"""
  291. hapd = start_ap_wpa2_psk(apdev[0]['ifname'])
  292. wlantest_setup()
  293. connect_2sta_wpa2_psk(dev, hapd)
  294. setup_tdls(dev[0], dev[1], apdev[0])
  295. teardown_tdls(dev[0], dev[1], apdev[0], responder=True)
  296. def test_ap_open_tdls_vht(dev, apdev):
  297. """Open AP and two stations using TDLS"""
  298. params = { "ssid": "test-open",
  299. "country_code": "DE",
  300. "hw_mode": "a",
  301. "channel": "36",
  302. "ieee80211n": "1",
  303. "ieee80211ac": "1",
  304. "ht_capab": "",
  305. "vht_capab": "",
  306. "vht_oper_chwidth": "0",
  307. "vht_oper_centr_freq_seg0_idx": "0" }
  308. try:
  309. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  310. wlantest_setup()
  311. connect_2sta_open(dev, hapd, scan_freq="5180")
  312. setup_tdls(dev[0], dev[1], apdev[0])
  313. teardown_tdls(dev[0], dev[1], apdev[0])
  314. setup_tdls(dev[1], dev[0], apdev[0])
  315. teardown_tdls(dev[1], dev[0], apdev[0], wildcard=True)
  316. finally:
  317. dev[0].request("DISCONNECT")
  318. dev[1].request("DISCONNECT")
  319. if hapd:
  320. hapd.request("DISABLE")
  321. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  322. dev[0].flush_scan_cache()
  323. dev[1].flush_scan_cache()