test_ibss.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. # IBSS test cases
  2. # Copyright (c) 2013, 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 time
  9. import re
  10. import subprocess
  11. import hwsim_utils
  12. def connect_ibss_cmd(dev, id):
  13. dev.dump_monitor()
  14. dev.select_network(id, freq="2412")
  15. def wait_ibss_connection(dev):
  16. logger.info(dev.ifname + " waiting for IBSS start/join to complete")
  17. ev = dev.wait_connected(timeout=20,
  18. error="Connection to the IBSS timed out")
  19. exp = r'<.>(CTRL-EVENT-CONNECTED) - Connection to ([0-9a-f:]*) completed.*'
  20. s = re.split(exp, ev)
  21. if len(s) < 3:
  22. return None
  23. return s[2]
  24. def wait_4way_handshake(dev1, dev2):
  25. logger.info(dev1.ifname + " waiting for 4-way handshake completion with " + dev2.ifname + " " + dev2.p2p_interface_addr())
  26. ev = dev1.wait_event(["IBSS-RSN-COMPLETED " + dev2.p2p_interface_addr()],
  27. timeout=20)
  28. if ev is None:
  29. raise Exception("4-way handshake in IBSS timed out")
  30. def wait_4way_handshake2(dev1, dev2, dev3):
  31. logger.info(dev1.ifname + " waiting for 4-way handshake completion with " + dev2.ifname + " " + dev2.p2p_interface_addr() + " and " + dev3.p2p_interface_addr())
  32. ev = dev1.wait_event(["IBSS-RSN-COMPLETED " + dev2.p2p_interface_addr(),
  33. "IBSS-RSN-COMPLETED " + dev3.p2p_interface_addr()],
  34. timeout=20)
  35. if ev is None:
  36. raise Exception("4-way handshake in IBSS timed out")
  37. ev = dev1.wait_event(["IBSS-RSN-COMPLETED " + dev2.p2p_interface_addr(),
  38. "IBSS-RSN-COMPLETED " + dev3.p2p_interface_addr()],
  39. timeout=20)
  40. if ev is None:
  41. raise Exception("4-way handshake in IBSS timed out")
  42. def add_ibss(dev, ssid, psk=None, proto=None, key_mgmt=None, pairwise=None,
  43. group=None, beacon_int=None, bssid=None, scan_freq=None,
  44. wep_key0=None):
  45. id = dev.add_network()
  46. dev.set_network(id, "mode", "1")
  47. dev.set_network(id, "frequency", "2412")
  48. if scan_freq:
  49. dev.set_network(id, "scan_freq", str(scan_freq))
  50. dev.set_network_quoted(id, "ssid", ssid)
  51. if psk:
  52. dev.set_network_quoted(id, "psk", psk)
  53. if proto:
  54. dev.set_network(id, "proto", proto)
  55. if key_mgmt:
  56. dev.set_network(id, "key_mgmt", key_mgmt)
  57. if pairwise:
  58. dev.set_network(id, "pairwise", pairwise)
  59. if group:
  60. dev.set_network(id, "group", group)
  61. if beacon_int:
  62. dev.set_network(id, "beacon_int", beacon_int)
  63. if bssid:
  64. dev.set_network(id, "bssid", bssid)
  65. if wep_key0:
  66. dev.set_network(id, "wep_key0", wep_key0)
  67. dev.request("ENABLE_NETWORK " + str(id) + " no-connect")
  68. return id
  69. def add_ibss_rsn(dev, ssid):
  70. return add_ibss(dev, ssid, "12345678", "RSN", "WPA-PSK", "CCMP", "CCMP")
  71. def add_ibss_rsn_tkip(dev, ssid):
  72. return add_ibss(dev, ssid, "12345678", "RSN", "WPA-PSK", "TKIP", "TKIP")
  73. def add_ibss_wpa_none(dev, ssid):
  74. return add_ibss(dev, ssid, "12345678", "WPA", "WPA-NONE", "TKIP", "TKIP")
  75. def add_ibss_wpa_none_ccmp(dev, ssid):
  76. return add_ibss(dev, ssid, "12345678", "WPA", "WPA-NONE", "CCMP", "CCMP")
  77. def test_ibss_rsn(dev):
  78. """IBSS RSN"""
  79. ssid="ibss-rsn"
  80. logger.info("Start IBSS on the first STA")
  81. id = add_ibss_rsn(dev[0], ssid)
  82. connect_ibss_cmd(dev[0], id)
  83. bssid0 = wait_ibss_connection(dev[0])
  84. logger.info("Join two STAs to the IBSS")
  85. id = add_ibss_rsn(dev[1], ssid)
  86. connect_ibss_cmd(dev[1], id)
  87. bssid1 = wait_ibss_connection(dev[1])
  88. if bssid0 != bssid1:
  89. logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
  90. # try to merge with a scan
  91. dev[1].scan()
  92. wait_4way_handshake(dev[0], dev[1])
  93. wait_4way_handshake(dev[1], dev[0])
  94. id = add_ibss_rsn(dev[2], ssid)
  95. connect_ibss_cmd(dev[2], id)
  96. bssid2 = wait_ibss_connection(dev[2])
  97. if bssid0 != bssid2:
  98. logger.info("STA0 BSSID " + bssid0 + " differs from STA2 BSSID " + bssid2)
  99. # try to merge with a scan
  100. dev[2].scan()
  101. wait_4way_handshake(dev[0], dev[2])
  102. wait_4way_handshake2(dev[2], dev[0], dev[1])
  103. # Allow some time for all peers to complete key setup
  104. time.sleep(3)
  105. hwsim_utils.test_connectivity(dev[0], dev[1])
  106. hwsim_utils.test_connectivity(dev[0], dev[2])
  107. hwsim_utils.test_connectivity(dev[1], dev[2])
  108. dev[1].request("REMOVE_NETWORK all")
  109. time.sleep(1)
  110. id = add_ibss_rsn(dev[1], ssid)
  111. connect_ibss_cmd(dev[1], id)
  112. bssid1 = wait_ibss_connection(dev[1])
  113. if bssid0 != bssid1:
  114. logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
  115. # try to merge with a scan
  116. dev[1].scan()
  117. wait_4way_handshake(dev[0], dev[1])
  118. wait_4way_handshake(dev[1], dev[0])
  119. time.sleep(3)
  120. hwsim_utils.test_connectivity(dev[0], dev[1])
  121. if "OK" not in dev[0].request("IBSS_RSN " + dev[1].p2p_interface_addr()):
  122. raise Exception("IBSS_RSN command failed")
  123. def test_ibss_wpa_none(dev):
  124. """IBSS WPA-None"""
  125. ssid="ibss-wpa-none"
  126. logger.info("Start IBSS on the first STA")
  127. id = add_ibss_wpa_none(dev[0], ssid)
  128. connect_ibss_cmd(dev[0], id)
  129. bssid0 = wait_ibss_connection(dev[0])
  130. # This is a bit ugly, but no one really cares about WPA-None, so there may
  131. # not be enough justification to clean this up.. For now, wpa_supplicant
  132. # will show two connection events with mac80211_hwsim where the first one
  133. # comes with all zeros address.
  134. if bssid0 == "00:00:00:00:00:00":
  135. logger.info("Waiting for real BSSID on the first STA")
  136. bssid0 = wait_ibss_connection(dev[0])
  137. logger.info("Join two STAs to the IBSS")
  138. id = add_ibss_wpa_none(dev[1], ssid)
  139. connect_ibss_cmd(dev[1], id)
  140. id = add_ibss_wpa_none(dev[2], ssid)
  141. connect_ibss_cmd(dev[2], id)
  142. bssid1 = wait_ibss_connection(dev[1])
  143. if bssid0 != bssid1:
  144. logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
  145. bssid1 = wait_ibss_connection(dev[1])
  146. bssid2 = wait_ibss_connection(dev[2])
  147. if bssid0 != bssid2:
  148. logger.info("STA0 BSSID " + bssid0 + " differs from STA2 BSSID " + bssid2)
  149. bssid2 = wait_ibss_connection(dev[2])
  150. logger.info("bssid0=%s bssid1=%s bssid2=%s" % (bssid0, bssid1, bssid2))
  151. bss = dev[0].get_bss(bssid0)
  152. if not bss:
  153. bss = dev[1].get_bss(bssid1)
  154. if not bss:
  155. raise Exception("Could not find BSS entry for IBSS")
  156. if 'flags' not in bss:
  157. raise Exception("Could not get BSS flags from BSS table")
  158. if "[WPA-None-TKIP]" not in bss['flags']:
  159. raise Exception("Unexpected BSS flags: " + bss['flags'])
  160. # Allow some time for all peers to complete key setup
  161. time.sleep(1)
  162. # This is supposed to work, but looks like WPA-None does not work with
  163. # mac80211 currently..
  164. try:
  165. hwsim_utils.test_connectivity(dev[0], dev[1])
  166. except Exception, e:
  167. logger.info("Ignoring known connectivity failure: " + str(e))
  168. try:
  169. hwsim_utils.test_connectivity(dev[0], dev[2])
  170. except Exception, e:
  171. logger.info("Ignoring known connectivity failure: " + str(e))
  172. try:
  173. hwsim_utils.test_connectivity(dev[1], dev[2])
  174. except Exception, e:
  175. logger.info("Ignoring known connectivity failure: " + str(e))
  176. def test_ibss_wpa_none_ccmp(dev):
  177. """IBSS WPA-None/CCMP"""
  178. ssid="ibss-wpa-none"
  179. logger.info("Start IBSS on the first STA")
  180. id = add_ibss_wpa_none(dev[0], ssid)
  181. connect_ibss_cmd(dev[0], id)
  182. bssid0 = wait_ibss_connection(dev[0])
  183. # This is a bit ugly, but no one really cares about WPA-None, so there may
  184. # not be enough justification to clean this up.. For now, wpa_supplicant
  185. # will show two connection events with mac80211_hwsim where the first one
  186. # comes with all zeros address.
  187. if bssid0 == "00:00:00:00:00:00":
  188. logger.info("Waiting for real BSSID on the first STA")
  189. bssid0 = wait_ibss_connection(dev[0])
  190. logger.info("Join a STA to the IBSS")
  191. id = add_ibss_wpa_none(dev[1], ssid)
  192. connect_ibss_cmd(dev[1], id)
  193. bssid1 = wait_ibss_connection(dev[1])
  194. if bssid0 != bssid1:
  195. logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
  196. bssid1 = wait_ibss_connection(dev[1])
  197. logger.info("bssid0=%s bssid1=%s" % (bssid0, bssid1))
  198. # Allow some time for all peers to complete key setup
  199. time.sleep(1)
  200. # This is supposed to work, but looks like WPA-None does not work with
  201. # mac80211 currently..
  202. try:
  203. hwsim_utils.test_connectivity(dev[0], dev[1])
  204. except Exception, e:
  205. logger.info("Ignoring known connectivity failure: " + str(e))
  206. def test_ibss_open(dev):
  207. """IBSS open (no security)"""
  208. ssid="ibss"
  209. id = add_ibss(dev[0], ssid, key_mgmt="NONE", beacon_int="150")
  210. connect_ibss_cmd(dev[0], id)
  211. bssid0 = wait_ibss_connection(dev[0])
  212. id = add_ibss(dev[1], ssid, key_mgmt="NONE", beacon_int="200")
  213. connect_ibss_cmd(dev[1], id)
  214. bssid1 = wait_ibss_connection(dev[1])
  215. if bssid0 != bssid1:
  216. logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
  217. res = dev[0].request("SCAN_RESULTS")
  218. if "[IBSS]" not in res:
  219. res = dev[1].request("SCAN_RESULTS")
  220. if "[IBSS]" not in res:
  221. raise Exception("IBSS flag missing from scan results: " + res)
  222. bss = dev[0].get_bss(bssid0)
  223. if not bss:
  224. bss = dev[1].get_bss(bssid1)
  225. if not bss:
  226. raise Exception("Could not find BSS entry for IBSS")
  227. if 'flags' not in bss:
  228. raise Exception("Could not get BSS flags from BSS table")
  229. if "[IBSS]" not in bss['flags']:
  230. raise Exception("Unexpected BSS flags: " + bss['flags'])
  231. def test_ibss_open_fixed_bssid(dev):
  232. """IBSS open (no security) and fixed BSSID"""
  233. ssid="ibss"
  234. bssid="02:11:22:33:44:55"
  235. try:
  236. dev[0].request("AP_SCAN 2")
  237. add_ibss(dev[0], ssid, key_mgmt="NONE", bssid=bssid, beacon_int="150")
  238. dev[0].request("REASSOCIATE")
  239. dev[1].request("AP_SCAN 2")
  240. add_ibss(dev[1], ssid, key_mgmt="NONE", bssid=bssid, beacon_int="200")
  241. dev[1].request("REASSOCIATE")
  242. bssid0 = wait_ibss_connection(dev[0])
  243. bssid1 = wait_ibss_connection(dev[1])
  244. if bssid0 != bssid:
  245. raise Exception("STA0 BSSID " + bssid0 + " differs from fixed BSSID " + bssid)
  246. if bssid1 != bssid:
  247. raise Exception("STA0 BSSID " + bssid0 + " differs from fixed BSSID " + bssid)
  248. finally:
  249. dev[0].request("AP_SCAN 1")
  250. dev[1].request("AP_SCAN 1")
  251. def test_ibss_open_retry(dev):
  252. """IBSS open (no security) with cfg80211 retry workaround"""
  253. subprocess.check_call(['iw', 'dev', dev[0].ifname, 'set', 'type', 'adhoc'])
  254. subprocess.check_call(['iw', 'dev', dev[0].ifname, 'ibss', 'join',
  255. 'ibss-test', '2412', 'HT20', 'fixed-freq',
  256. '02:22:33:44:55:66'])
  257. ssid="ibss"
  258. try:
  259. dev[0].request("AP_SCAN 2")
  260. id = add_ibss(dev[0], ssid, key_mgmt="NONE", beacon_int="150",
  261. bssid="02:33:44:55:66:77", scan_freq=2412)
  262. #connect_ibss_cmd(dev[0], id)
  263. dev[0].request("REASSOCIATE")
  264. bssid0 = wait_ibss_connection(dev[0])
  265. subprocess.check_call(['iw', 'dev', dev[0].ifname, 'ibss', 'leave'])
  266. time.sleep(1)
  267. dev[0].request("DISCONNECT")
  268. finally:
  269. dev[0].request("AP_SCAN 1")
  270. def test_ibss_rsn_tkip(dev):
  271. """IBSS RSN with TKIP as the cipher"""
  272. ssid="ibss-rsn-tkip"
  273. id = add_ibss_rsn_tkip(dev[0], ssid)
  274. connect_ibss_cmd(dev[0], id)
  275. bssid0 = wait_ibss_connection(dev[0])
  276. id = add_ibss_rsn_tkip(dev[1], ssid)
  277. connect_ibss_cmd(dev[1], id)
  278. bssid1 = wait_ibss_connection(dev[1])
  279. if bssid0 != bssid1:
  280. logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
  281. # try to merge with a scan
  282. dev[1].scan()
  283. wait_4way_handshake(dev[0], dev[1])
  284. wait_4way_handshake(dev[1], dev[0])
  285. def test_ibss_wep(dev):
  286. """IBSS with WEP"""
  287. ssid="ibss-wep"
  288. id = add_ibss(dev[0], ssid, key_mgmt="NONE", wep_key0='"hello"')
  289. connect_ibss_cmd(dev[0], id)
  290. bssid0 = wait_ibss_connection(dev[0])
  291. id = add_ibss(dev[1], ssid, key_mgmt="NONE", wep_key0='"hello"')
  292. connect_ibss_cmd(dev[1], id)
  293. bssid1 = wait_ibss_connection(dev[1])
  294. def test_ibss_rsn_error_case(dev):
  295. """IBSS RSN regression test for IBSS_RSN prior IBSS setup"""
  296. if "FAIL" not in dev[0].request("IBSS_RSN 02:03:04:05:06:07"):
  297. raise Exception("Unexpected IBSS_RSN result")