test_wpas_mesh.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #!/usr/bin/python
  2. #
  3. # wpa_supplicant mesh mode tests
  4. # Copyright (c) 2014, cozybit Inc.
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import logging
  9. logger = logging.getLogger()
  10. import hwsim_utils
  11. from wpasupplicant import WpaSupplicant
  12. def check_mesh_scan(dev, params, other_started=False):
  13. if not other_started:
  14. dev.dump_monitor()
  15. id = dev.request("SCAN " + params)
  16. if "FAIL" in id:
  17. raise Exception("Failed to start scan")
  18. id = int(id)
  19. if other_started:
  20. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  21. if ev is None:
  22. raise Exception("Other scan did not start")
  23. if "id=" + str(id) in ev:
  24. raise Exception("Own scan id unexpectedly included in start event")
  25. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  26. if ev is None:
  27. raise Exception("Other scan did not complete")
  28. if "id=" + str(id) in ev:
  29. raise Exception(
  30. "Own scan id unexpectedly included in completed event")
  31. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  32. if ev is None:
  33. raise Exception("Scan did not start")
  34. if "id=" + str(id) not in ev:
  35. raise Exception("Scan id not included in start event")
  36. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  37. if ev is None:
  38. raise Exception("Scan did not complete")
  39. if "id=" + str(id) not in ev:
  40. raise Exception("Scan id not included in completed event")
  41. res = dev.request("SCAN_RESULTS")
  42. if res.find("[MESH]") < 0:
  43. raise Exception("Scan did not contain a MESH network")
  44. bssid = res.splitlines()[1].split(' ')[0]
  45. bss = dev.get_bss(bssid)
  46. if bss is None:
  47. raise Exception("Could not get BSS entry for mesh")
  48. if 'mesh_capability' not in bss:
  49. raise Exception("mesh_capability missing from BSS entry")
  50. def check_mesh_group_added(dev):
  51. ev = dev.wait_event(["MESH-GROUP-STARTED"])
  52. if ev is None:
  53. raise Exception("Test exception: Couldn't join mesh")
  54. def check_mesh_group_removed(dev):
  55. ev = dev.wait_event(["MESH-GROUP-REMOVED"])
  56. if ev is None:
  57. raise Exception("Test exception: Couldn't leave mesh")
  58. def check_mesh_peer_connected(dev):
  59. ev = dev.wait_event(["MESH-PEER-CONNECTED"])
  60. if ev is None:
  61. raise Exception("Test exception: Remote peer did not connect.")
  62. def check_mesh_peer_disconnected(dev):
  63. ev = dev.wait_event(["MESH-PEER-DISCONNECTED"])
  64. if ev is None:
  65. raise Exception("Test exception: Peer disconnect event not detected.")
  66. def test_wpas_add_set_remove_support(dev):
  67. """wpa_supplicant MESH add/set/remove network support"""
  68. id = dev[0].add_network()
  69. dev[0].set_network(id, "mode", "5")
  70. dev[0].remove_network(id)
  71. def add_open_mesh_network(dev, ht_mode=False, start=True):
  72. id = dev.add_network()
  73. dev.set_network(id, "mode", "5")
  74. dev.set_network_quoted(id, "ssid", "wpas-mesh-open")
  75. dev.set_network(id, "key_mgmt", "NONE")
  76. dev.set_network(id, "frequency", "2412")
  77. if ht_mode:
  78. dev.set_network(id, "mesh_ht_mode", ht_mode)
  79. if start:
  80. dev.mesh_group_add(id)
  81. return id
  82. def test_wpas_mesh_group_added(dev):
  83. """wpa_supplicant MESH group add"""
  84. add_open_mesh_network(dev[0])
  85. # Check for MESH-GROUP-STARTED event
  86. check_mesh_group_added(dev[0])
  87. def test_wpas_mesh_group_remove(dev):
  88. """wpa_supplicant MESH group remove"""
  89. add_open_mesh_network(dev[0])
  90. # Check for MESH-GROUP-STARTED event
  91. check_mesh_group_added(dev[0])
  92. dev[0].mesh_group_remove()
  93. # Check for MESH-GROUP-REMOVED event
  94. check_mesh_group_removed(dev[0])
  95. dev[0].mesh_group_remove()
  96. def test_wpas_mesh_peer_connected(dev):
  97. """wpa_supplicant MESH peer connected"""
  98. add_open_mesh_network(dev[0])
  99. add_open_mesh_network(dev[1])
  100. # Check for mesh joined
  101. check_mesh_group_added(dev[0])
  102. check_mesh_group_added(dev[1])
  103. # Check for peer connected
  104. check_mesh_peer_connected(dev[0])
  105. check_mesh_peer_connected(dev[1])
  106. def test_wpas_mesh_peer_disconnected(dev):
  107. """wpa_supplicant MESH peer disconnected"""
  108. add_open_mesh_network(dev[0])
  109. add_open_mesh_network(dev[1])
  110. # Check for mesh joined
  111. check_mesh_group_added(dev[0])
  112. check_mesh_group_added(dev[1])
  113. # Check for peer connected
  114. check_mesh_peer_connected(dev[0])
  115. check_mesh_peer_connected(dev[1])
  116. # Remove group on dev 1
  117. dev[1].mesh_group_remove()
  118. # Device 0 should get a disconnection event
  119. check_mesh_peer_disconnected(dev[0])
  120. def test_wpas_mesh_mode_scan(dev):
  121. """wpa_supplicant MESH scan support"""
  122. add_open_mesh_network(dev[0], ht_mode="HT40+")
  123. add_open_mesh_network(dev[1], ht_mode="HT40+")
  124. # Check for mesh joined
  125. check_mesh_group_added(dev[0])
  126. check_mesh_group_added(dev[1])
  127. # Check for Mesh scan
  128. check_mesh_scan(dev[0], "use_id=1")
  129. def wrap_wpas_mesh_test(test, dev, apdev):
  130. def _test_connectivity(dev1, dev2):
  131. return hwsim_utils.test_connectivity(dev1, dev2)
  132. return test(dev, apdev, _test_connectivity)
  133. def _test_wpas_mesh_open(dev, apdev, test_connectivity):
  134. add_open_mesh_network(dev[0], ht_mode="HT40+")
  135. add_open_mesh_network(dev[1], ht_mode="HT40+")
  136. # Check for mesh joined
  137. check_mesh_group_added(dev[0])
  138. check_mesh_group_added(dev[1])
  139. # Check for peer connected
  140. check_mesh_peer_connected(dev[0])
  141. check_mesh_peer_connected(dev[1])
  142. # Test connectivity 0->1 and 1->0
  143. test_connectivity(dev[0], dev[1])
  144. def test_wpas_mesh_open(dev, apdev):
  145. """wpa_supplicant open MESH network connectivity"""
  146. return wrap_wpas_mesh_test(_test_wpas_mesh_open, dev, apdev)
  147. def _test_wpas_mesh_open_no_auto(dev, apdev, test_connectivity):
  148. id = add_open_mesh_network(dev[0], start=False)
  149. dev[0].set_network(id, "dot11MeshMaxRetries", "16")
  150. dev[0].set_network(id, "dot11MeshRetryTimeout", "255")
  151. dev[0].mesh_group_add(id)
  152. id = add_open_mesh_network(dev[1], start=False)
  153. dev[1].set_network(id, "no_auto_peer", "1")
  154. dev[1].mesh_group_add(id)
  155. # Check for mesh joined
  156. check_mesh_group_added(dev[0])
  157. check_mesh_group_added(dev[1])
  158. # Check for peer connected
  159. check_mesh_peer_connected(dev[0])
  160. check_mesh_peer_connected(dev[1])
  161. # Test connectivity 0->1 and 1->0
  162. test_connectivity(dev[0], dev[1])
  163. def test_wpas_mesh_open_no_auto(dev, apdev):
  164. """wpa_supplicant open MESH network connectivity"""
  165. return wrap_wpas_mesh_test(_test_wpas_mesh_open_no_auto, dev, apdev)
  166. def add_mesh_secure_net(dev, psk=True):
  167. id = dev.add_network()
  168. dev.set_network(id, "mode", "5")
  169. dev.set_network_quoted(id, "ssid", "wpas-mesh-sec")
  170. dev.set_network(id, "key_mgmt", "SAE")
  171. dev.set_network(id, "frequency", "2412")
  172. if psk:
  173. dev.set_network_quoted(id, "psk", "thisismypassphrase!")
  174. return id
  175. def _test_wpas_mesh_secure(dev, apdev, test_connectivity):
  176. dev[0].request("SET sae_groups ")
  177. id = add_mesh_secure_net(dev[0])
  178. dev[0].mesh_group_add(id)
  179. dev[1].request("SET sae_groups ")
  180. id = add_mesh_secure_net(dev[1])
  181. dev[1].mesh_group_add(id)
  182. # Check for mesh joined
  183. check_mesh_group_added(dev[0])
  184. check_mesh_group_added(dev[1])
  185. # Check for peer connected
  186. check_mesh_peer_connected(dev[0])
  187. check_mesh_peer_connected(dev[1])
  188. # Test connectivity 0->1 and 1->0
  189. test_connectivity(dev[0], dev[1])
  190. def test_wpas_mesh_secure(dev, apdev):
  191. """wpa_supplicant secure MESH network connectivity"""
  192. return wrap_wpas_mesh_test(_test_wpas_mesh_secure, dev, apdev)
  193. def test_wpas_mesh_secure_sae_group_mismatch(dev, apdev):
  194. """wpa_supplicant secure MESH and SAE group mismatch"""
  195. addr0 = dev[0].p2p_interface_addr()
  196. addr1 = dev[1].p2p_interface_addr()
  197. addr2 = dev[2].p2p_interface_addr()
  198. dev[0].request("SET sae_groups 19 25")
  199. id = add_mesh_secure_net(dev[0])
  200. dev[0].mesh_group_add(id)
  201. dev[1].request("SET sae_groups 19")
  202. id = add_mesh_secure_net(dev[1])
  203. dev[1].mesh_group_add(id)
  204. dev[2].request("SET sae_groups 26")
  205. id = add_mesh_secure_net(dev[2])
  206. dev[2].mesh_group_add(id)
  207. check_mesh_group_added(dev[0])
  208. check_mesh_group_added(dev[1])
  209. check_mesh_group_added(dev[2])
  210. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"])
  211. if ev is None:
  212. raise Exception("Remote peer did not connect")
  213. if addr1 not in ev:
  214. raise Exception("Unexpected peer connected: " + ev)
  215. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"])
  216. if ev is None:
  217. raise Exception("Remote peer did not connect")
  218. if addr0 not in ev:
  219. raise Exception("Unexpected peer connected: " + ev)
  220. ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  221. if ev is not None:
  222. raise Exception("Unexpected peer connection at dev[2]: " + ev)
  223. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  224. if ev is not None:
  225. raise Exception("Unexpected peer connection: " + ev)
  226. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  227. if ev is not None:
  228. raise Exception("Unexpected peer connection: " + ev)
  229. dev[0].request("SET sae_groups ")
  230. dev[1].request("SET sae_groups ")
  231. dev[2].request("SET sae_groups ")
  232. def test_wpas_mesh_secure_sae_missing_password(dev, apdev):
  233. """wpa_supplicant secure MESH and missing SAE password"""
  234. id = add_mesh_secure_net(dev[0], psk=False)
  235. dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
  236. dev[0].mesh_group_add(id)
  237. ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
  238. timeout=5)
  239. if ev is None:
  240. raise Exception("Timeout on mesh start event")
  241. if "MESH-GROUP-STARTED" in ev:
  242. raise Exception("Unexpected mesh group start")
  243. ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
  244. if ev is not None:
  245. raise Exception("Unexpected mesh group start")
  246. def _test_wpas_mesh_secure_no_auto(dev, apdev, test_connectivity):
  247. dev[0].request("SET sae_groups 19")
  248. id = add_mesh_secure_net(dev[0])
  249. dev[0].mesh_group_add(id)
  250. dev[1].request("SET sae_groups 19")
  251. id = add_mesh_secure_net(dev[1])
  252. dev[1].set_network(id, "no_auto_peer", "1")
  253. dev[1].mesh_group_add(id)
  254. # Check for mesh joined
  255. check_mesh_group_added(dev[0])
  256. check_mesh_group_added(dev[1])
  257. # Check for peer connected
  258. check_mesh_peer_connected(dev[0])
  259. check_mesh_peer_connected(dev[1])
  260. # Test connectivity 0->1 and 1->0
  261. test_connectivity(dev[0], dev[1])
  262. dev[0].request("SET sae_groups ")
  263. dev[1].request("SET sae_groups ")
  264. def test_wpas_mesh_secure_no_auto(dev, apdev):
  265. """wpa_supplicant secure MESH network connectivity"""
  266. return wrap_wpas_mesh_test(_test_wpas_mesh_secure_no_auto, dev, apdev)
  267. def test_wpas_mesh_ctrl(dev):
  268. """wpa_supplicant ctrl_iface mesh command error cases"""
  269. if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
  270. raise Exception("Unexpected MESH_GROUP_ADD success")
  271. id = dev[0].add_network()
  272. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  273. raise Exception("Unexpected MESH_GROUP_ADD success")
  274. dev[0].set_network(id, "mode", "5")
  275. dev[0].set_network(id, "key_mgmt", "WPA-PSK")
  276. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  277. raise Exception("Unexpected MESH_GROUP_ADD success")
  278. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
  279. raise Exception("Unexpected MESH_GROUP_REMOVE success")
  280. def test_wpas_mesh_dynamic_interface(dev):
  281. """wpa_supplicant mesh with dynamic interface"""
  282. mesh0 = None
  283. mesh1 = None
  284. try:
  285. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  286. if "FAIL" in mesh0:
  287. raise Exception("MESH_INTERFACE_ADD failed")
  288. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  289. if "FAIL" in mesh1:
  290. raise Exception("MESH_INTERFACE_ADD failed")
  291. wpas0 = WpaSupplicant(ifname=mesh0)
  292. wpas1 = WpaSupplicant(ifname=mesh1)
  293. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  294. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  295. add_open_mesh_network(wpas0)
  296. add_open_mesh_network(wpas1)
  297. check_mesh_group_added(wpas0)
  298. check_mesh_group_added(wpas1)
  299. check_mesh_peer_connected(wpas0)
  300. check_mesh_peer_connected(wpas1)
  301. hwsim_utils.test_connectivity(wpas0, wpas1)
  302. # Must not allow MESH_GROUP_REMOVE on dynamic interface
  303. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
  304. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  305. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
  306. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  307. # Must not allow MESH_GROUP_REMOVE on another radio interface
  308. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
  309. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  310. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
  311. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  312. wpas0.remove_ifname()
  313. wpas1.remove_ifname()
  314. if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  315. raise Exception("MESH_GROUP_REMOVE failed")
  316. if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  317. raise Exception("MESH_GROUP_REMOVE failed")
  318. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  319. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  320. if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  321. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  322. logger.info("Make sure another dynamic group can be added")
  323. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  324. if "FAIL" in mesh0:
  325. raise Exception("MESH_INTERFACE_ADD failed")
  326. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  327. if "FAIL" in mesh1:
  328. raise Exception("MESH_INTERFACE_ADD failed")
  329. wpas0 = WpaSupplicant(ifname=mesh0)
  330. wpas1 = WpaSupplicant(ifname=mesh1)
  331. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  332. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  333. add_open_mesh_network(wpas0)
  334. add_open_mesh_network(wpas1)
  335. check_mesh_group_added(wpas0)
  336. check_mesh_group_added(wpas1)
  337. check_mesh_peer_connected(wpas0)
  338. check_mesh_peer_connected(wpas1)
  339. hwsim_utils.test_connectivity(wpas0, wpas1)
  340. finally:
  341. if mesh0:
  342. dev[0].request("MESH_GROUP_REMOVE " + mesh0)
  343. if mesh1:
  344. dev[1].request("MESH_GROUP_REMOVE " + mesh1)