test_wpas_mesh.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 test_wpas_mesh_open(dev, apdev):
  138. """wpa_supplicant open MESH network connectivity"""
  139. add_open_mesh_network(dev[0], ht_mode="HT40-", freq="2462")
  140. add_open_mesh_network(dev[1], ht_mode="HT40-", freq="2462")
  141. # Check for mesh joined
  142. check_mesh_group_added(dev[0])
  143. check_mesh_group_added(dev[1])
  144. # Check for peer connected
  145. check_mesh_peer_connected(dev[0])
  146. check_mesh_peer_connected(dev[1])
  147. # Test connectivity 0->1 and 1->0
  148. hwsim_utils.test_connectivity(dev[0], dev[1])
  149. def test_wpas_mesh_open_no_auto(dev, apdev):
  150. """wpa_supplicant open MESH network connectivity"""
  151. id = add_open_mesh_network(dev[0], start=False)
  152. dev[0].set_network(id, "dot11MeshMaxRetries", "16")
  153. dev[0].set_network(id, "dot11MeshRetryTimeout", "255")
  154. dev[0].mesh_group_add(id)
  155. id = add_open_mesh_network(dev[1], start=False)
  156. dev[1].set_network(id, "no_auto_peer", "1")
  157. dev[1].mesh_group_add(id)
  158. # Check for mesh joined
  159. check_mesh_group_added(dev[0])
  160. check_mesh_group_added(dev[1])
  161. # Check for peer connected
  162. check_mesh_peer_connected(dev[0])
  163. check_mesh_peer_connected(dev[1])
  164. # Test connectivity 0->1 and 1->0
  165. hwsim_utils.test_connectivity(dev[0], dev[1])
  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):
  176. """wpa_supplicant secure MESH network connectivity"""
  177. dev[0].request("SET sae_groups ")
  178. id = add_mesh_secure_net(dev[0])
  179. dev[0].mesh_group_add(id)
  180. dev[1].request("SET sae_groups ")
  181. id = add_mesh_secure_net(dev[1])
  182. dev[1].mesh_group_add(id)
  183. # Check for mesh joined
  184. check_mesh_group_added(dev[0])
  185. check_mesh_group_added(dev[1])
  186. # Check for peer connected
  187. check_mesh_peer_connected(dev[0])
  188. check_mesh_peer_connected(dev[1])
  189. # Test connectivity 0->1 and 1->0
  190. hwsim_utils.test_connectivity(dev[0], dev[1])
  191. def test_wpas_mesh_secure_sae_group_mismatch(dev, apdev):
  192. """wpa_supplicant secure MESH and SAE group mismatch"""
  193. addr0 = dev[0].p2p_interface_addr()
  194. addr1 = dev[1].p2p_interface_addr()
  195. addr2 = dev[2].p2p_interface_addr()
  196. dev[0].request("SET sae_groups 19 25")
  197. id = add_mesh_secure_net(dev[0])
  198. dev[0].mesh_group_add(id)
  199. dev[1].request("SET sae_groups 19")
  200. id = add_mesh_secure_net(dev[1])
  201. dev[1].mesh_group_add(id)
  202. dev[2].request("SET sae_groups 26")
  203. id = add_mesh_secure_net(dev[2])
  204. dev[2].mesh_group_add(id)
  205. check_mesh_group_added(dev[0])
  206. check_mesh_group_added(dev[1])
  207. check_mesh_group_added(dev[2])
  208. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"])
  209. if ev is None:
  210. raise Exception("Remote peer did not connect")
  211. if addr1 not in ev:
  212. raise Exception("Unexpected peer connected: " + ev)
  213. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"])
  214. if ev is None:
  215. raise Exception("Remote peer did not connect")
  216. if addr0 not in ev:
  217. raise Exception("Unexpected peer connected: " + ev)
  218. ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  219. if ev is not None:
  220. raise Exception("Unexpected peer connection at dev[2]: " + ev)
  221. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  222. if ev is not None:
  223. raise Exception("Unexpected peer connection: " + ev)
  224. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  225. if ev is not None:
  226. raise Exception("Unexpected peer connection: " + ev)
  227. dev[0].request("SET sae_groups ")
  228. dev[1].request("SET sae_groups ")
  229. dev[2].request("SET sae_groups ")
  230. def test_wpas_mesh_secure_sae_missing_password(dev, apdev):
  231. """wpa_supplicant secure MESH and missing SAE password"""
  232. id = add_mesh_secure_net(dev[0], psk=False)
  233. dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
  234. dev[0].mesh_group_add(id)
  235. ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
  236. timeout=5)
  237. if ev is None:
  238. raise Exception("Timeout on mesh start event")
  239. if "MESH-GROUP-STARTED" in ev:
  240. raise Exception("Unexpected mesh group start")
  241. ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
  242. if ev is not None:
  243. raise Exception("Unexpected mesh group start")
  244. def test_wpas_mesh_secure_no_auto(dev, apdev):
  245. """wpa_supplicant secure MESH network connectivity"""
  246. dev[0].request("SET sae_groups 19")
  247. id = add_mesh_secure_net(dev[0])
  248. dev[0].mesh_group_add(id)
  249. dev[1].request("SET sae_groups 19")
  250. id = add_mesh_secure_net(dev[1])
  251. dev[1].set_network(id, "no_auto_peer", "1")
  252. dev[1].mesh_group_add(id)
  253. # Check for mesh joined
  254. check_mesh_group_added(dev[0])
  255. check_mesh_group_added(dev[1])
  256. # Check for peer connected
  257. check_mesh_peer_connected(dev[0])
  258. check_mesh_peer_connected(dev[1])
  259. # Test connectivity 0->1 and 1->0
  260. hwsim_utils.test_connectivity(dev[0], dev[1])
  261. dev[0].request("SET sae_groups ")
  262. dev[1].request("SET sae_groups ")
  263. def test_wpas_mesh_ctrl(dev):
  264. """wpa_supplicant ctrl_iface mesh command error cases"""
  265. if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
  266. raise Exception("Unexpected MESH_GROUP_ADD success")
  267. id = dev[0].add_network()
  268. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  269. raise Exception("Unexpected MESH_GROUP_ADD success")
  270. dev[0].set_network(id, "mode", "5")
  271. dev[0].set_network(id, "key_mgmt", "WPA-PSK")
  272. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  273. raise Exception("Unexpected MESH_GROUP_ADD success")
  274. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
  275. raise Exception("Unexpected MESH_GROUP_REMOVE success")
  276. def test_wpas_mesh_dynamic_interface(dev):
  277. """wpa_supplicant mesh with dynamic interface"""
  278. mesh0 = None
  279. mesh1 = None
  280. try:
  281. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  282. if "FAIL" in mesh0:
  283. raise Exception("MESH_INTERFACE_ADD failed")
  284. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  285. if "FAIL" in mesh1:
  286. raise Exception("MESH_INTERFACE_ADD failed")
  287. wpas0 = WpaSupplicant(ifname=mesh0)
  288. wpas1 = WpaSupplicant(ifname=mesh1)
  289. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  290. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  291. add_open_mesh_network(wpas0)
  292. add_open_mesh_network(wpas1)
  293. check_mesh_group_added(wpas0)
  294. check_mesh_group_added(wpas1)
  295. check_mesh_peer_connected(wpas0)
  296. check_mesh_peer_connected(wpas1)
  297. hwsim_utils.test_connectivity(wpas0, wpas1)
  298. # Must not allow MESH_GROUP_REMOVE on dynamic interface
  299. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
  300. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  301. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
  302. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  303. # Must not allow MESH_GROUP_REMOVE on another radio interface
  304. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
  305. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  306. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
  307. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  308. wpas0.remove_ifname()
  309. wpas1.remove_ifname()
  310. if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  311. raise Exception("MESH_GROUP_REMOVE failed")
  312. if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  313. raise Exception("MESH_GROUP_REMOVE failed")
  314. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  315. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  316. if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  317. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  318. logger.info("Make sure another dynamic group can be added")
  319. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  320. if "FAIL" in mesh0:
  321. raise Exception("MESH_INTERFACE_ADD failed")
  322. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  323. if "FAIL" in mesh1:
  324. raise Exception("MESH_INTERFACE_ADD failed")
  325. wpas0 = WpaSupplicant(ifname=mesh0)
  326. wpas1 = WpaSupplicant(ifname=mesh1)
  327. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  328. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  329. add_open_mesh_network(wpas0)
  330. add_open_mesh_network(wpas1)
  331. check_mesh_group_added(wpas0)
  332. check_mesh_group_added(wpas1)
  333. check_mesh_peer_connected(wpas0)
  334. check_mesh_peer_connected(wpas1)
  335. hwsim_utils.test_connectivity(wpas0, wpas1)
  336. finally:
  337. if mesh0:
  338. dev[0].request("MESH_GROUP_REMOVE " + mesh0)
  339. if mesh1:
  340. dev[1].request("MESH_GROUP_REMOVE " + mesh1)
  341. def test_wpas_mesh_max_peering(dev, apdev):
  342. """Mesh max peering limit"""
  343. try:
  344. dev[0].request("SET max_peer_links 1")
  345. for i in range(3):
  346. add_open_mesh_network(dev[i])
  347. for i in range(3):
  348. ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
  349. if ev is None:
  350. raise Exception("dev%d did not connect with any peer" % i)
  351. for i in range(1, 3):
  352. ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
  353. if ev is None:
  354. raise Exception("dev%d did not connect the second peer" % i)
  355. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  356. if ev is not None:
  357. raise Exception("dev0 connection beyond max peering limit")
  358. for i in range(3):
  359. dev[i].mesh_group_remove()
  360. check_mesh_group_removed(dev[i])
  361. finally:
  362. dev[0].request("SET max_peer_links 99")