test_wpas_mesh.py 15 KB

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