test_wpas_mesh.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 mesh_supported(dev):
  13. flags = int(dev.get_driver_status_field('capa.flags'), 16)
  14. if flags & 0x100000000:
  15. return True
  16. return False
  17. def check_mesh_scan(dev, params, other_started=False, beacon_int=0):
  18. if not other_started:
  19. dev.dump_monitor()
  20. id = dev.request("SCAN " + params)
  21. if "FAIL" in id:
  22. raise Exception("Failed to start scan")
  23. id = int(id)
  24. if other_started:
  25. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  26. if ev is None:
  27. raise Exception("Other scan did not start")
  28. if "id=" + str(id) in ev:
  29. raise Exception("Own scan id unexpectedly included in start event")
  30. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  31. if ev is None:
  32. raise Exception("Other scan did not complete")
  33. if "id=" + str(id) in ev:
  34. raise Exception(
  35. "Own scan id unexpectedly included in completed event")
  36. ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
  37. if ev is None:
  38. raise Exception("Scan did not start")
  39. if "id=" + str(id) not in ev:
  40. raise Exception("Scan id not included in start event")
  41. ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
  42. if ev is None:
  43. raise Exception("Scan did not complete")
  44. if "id=" + str(id) not in ev:
  45. raise Exception("Scan id not included in completed event")
  46. res = dev.request("SCAN_RESULTS")
  47. if res.find("[MESH]") < 0:
  48. raise Exception("Scan did not contain a MESH network")
  49. bssid = res.splitlines()[1].split(' ')[0]
  50. bss = dev.get_bss(bssid)
  51. if bss is None:
  52. raise Exception("Could not get BSS entry for mesh")
  53. if 'mesh_capability' not in bss:
  54. raise Exception("mesh_capability missing from BSS entry")
  55. if beacon_int:
  56. if 'beacon_int' not in bss:
  57. raise Exception("beacon_int missing from BSS entry")
  58. if str(beacon_int) != bss['beacon_int']:
  59. raise Exception("Unexpected beacon_int in BSS entry: " + bss['beacon_int'])
  60. def check_mesh_group_added(dev):
  61. ev = dev.wait_event(["MESH-GROUP-STARTED"])
  62. if ev is None:
  63. raise Exception("Test exception: Couldn't join mesh")
  64. def check_mesh_group_removed(dev):
  65. ev = dev.wait_event(["MESH-GROUP-REMOVED"])
  66. if ev is None:
  67. raise Exception("Test exception: Couldn't leave mesh")
  68. def check_mesh_peer_connected(dev, timeout=10):
  69. ev = dev.wait_event(["MESH-PEER-CONNECTED"], timeout=timeout)
  70. if ev is None:
  71. raise Exception("Test exception: Remote peer did not connect.")
  72. def check_mesh_peer_disconnected(dev):
  73. ev = dev.wait_event(["MESH-PEER-DISCONNECTED"])
  74. if ev is None:
  75. raise Exception("Test exception: Peer disconnect event not detected.")
  76. def test_wpas_add_set_remove_support(dev):
  77. """wpa_supplicant MESH add/set/remove network support"""
  78. id = dev[0].add_network()
  79. dev[0].set_network(id, "mode", "5")
  80. dev[0].remove_network(id)
  81. def add_open_mesh_network(dev, ht_mode=False, freq="2412", start=True,
  82. beacon_int=0):
  83. id = dev.add_network()
  84. dev.set_network(id, "mode", "5")
  85. dev.set_network_quoted(id, "ssid", "wpas-mesh-open")
  86. dev.set_network(id, "key_mgmt", "NONE")
  87. dev.set_network(id, "frequency", freq)
  88. if ht_mode:
  89. dev.set_network(id, "mesh_ht_mode", ht_mode)
  90. if beacon_int:
  91. dev.set_network(id, "beacon_int", str(beacon_int))
  92. if start:
  93. dev.mesh_group_add(id)
  94. return id
  95. def test_wpas_mesh_group_added(dev):
  96. """wpa_supplicant MESH group add"""
  97. if not mesh_supported(dev[0]):
  98. return "skip"
  99. add_open_mesh_network(dev[0])
  100. # Check for MESH-GROUP-STARTED event
  101. check_mesh_group_added(dev[0])
  102. def test_wpas_mesh_group_remove(dev):
  103. """wpa_supplicant MESH group remove"""
  104. if not mesh_supported(dev[0]):
  105. return "skip"
  106. add_open_mesh_network(dev[0], ht_mode="NOHT")
  107. # Check for MESH-GROUP-STARTED event
  108. check_mesh_group_added(dev[0])
  109. dev[0].mesh_group_remove()
  110. # Check for MESH-GROUP-REMOVED event
  111. check_mesh_group_removed(dev[0])
  112. dev[0].mesh_group_remove()
  113. def test_wpas_mesh_peer_connected(dev):
  114. """wpa_supplicant MESH peer connected"""
  115. if not mesh_supported(dev[0]):
  116. return "skip"
  117. add_open_mesh_network(dev[0], ht_mode="HT20", beacon_int=160)
  118. add_open_mesh_network(dev[1], ht_mode="HT20", beacon_int=160)
  119. # Check for mesh joined
  120. check_mesh_group_added(dev[0])
  121. check_mesh_group_added(dev[1])
  122. # Check for peer connected
  123. check_mesh_peer_connected(dev[0])
  124. check_mesh_peer_connected(dev[1])
  125. def test_wpas_mesh_peer_disconnected(dev):
  126. """wpa_supplicant MESH peer disconnected"""
  127. if not mesh_supported(dev[0]):
  128. return "skip"
  129. add_open_mesh_network(dev[0])
  130. add_open_mesh_network(dev[1])
  131. # Check for mesh joined
  132. check_mesh_group_added(dev[0])
  133. check_mesh_group_added(dev[1])
  134. # Check for peer connected
  135. check_mesh_peer_connected(dev[0])
  136. check_mesh_peer_connected(dev[1])
  137. # Remove group on dev 1
  138. dev[1].mesh_group_remove()
  139. # Device 0 should get a disconnection event
  140. check_mesh_peer_disconnected(dev[0])
  141. def test_wpas_mesh_mode_scan(dev):
  142. """wpa_supplicant MESH scan support"""
  143. if not mesh_supported(dev[0]):
  144. return "skip"
  145. add_open_mesh_network(dev[0], ht_mode="HT40+")
  146. add_open_mesh_network(dev[1], ht_mode="HT40+", beacon_int=175)
  147. # Check for mesh joined
  148. check_mesh_group_added(dev[0])
  149. check_mesh_group_added(dev[1])
  150. # Check for Mesh scan
  151. check_mesh_scan(dev[0], "use_id=1", beacon_int=175)
  152. def test_wpas_mesh_open(dev, apdev):
  153. """wpa_supplicant open MESH network connectivity"""
  154. if not mesh_supported(dev[0]):
  155. return "skip"
  156. add_open_mesh_network(dev[0], ht_mode="HT40-", freq="2462")
  157. add_open_mesh_network(dev[1], ht_mode="HT40-", freq="2462")
  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 test_wpas_mesh_open_no_auto(dev, apdev):
  167. """wpa_supplicant open MESH network connectivity"""
  168. if not mesh_supported(dev[0]):
  169. return "skip"
  170. id = add_open_mesh_network(dev[0], start=False)
  171. dev[0].set_network(id, "dot11MeshMaxRetries", "16")
  172. dev[0].set_network(id, "dot11MeshRetryTimeout", "255")
  173. dev[0].mesh_group_add(id)
  174. id = add_open_mesh_network(dev[1], start=False)
  175. dev[1].set_network(id, "no_auto_peer", "1")
  176. dev[1].mesh_group_add(id)
  177. # Check for mesh joined
  178. check_mesh_group_added(dev[0])
  179. check_mesh_group_added(dev[1])
  180. # Check for peer connected
  181. check_mesh_peer_connected(dev[0], timeout=30)
  182. check_mesh_peer_connected(dev[1])
  183. # Test connectivity 0->1 and 1->0
  184. hwsim_utils.test_connectivity(dev[0], dev[1])
  185. def add_mesh_secure_net(dev, psk=True):
  186. id = dev.add_network()
  187. dev.set_network(id, "mode", "5")
  188. dev.set_network_quoted(id, "ssid", "wpas-mesh-sec")
  189. dev.set_network(id, "key_mgmt", "SAE")
  190. dev.set_network(id, "frequency", "2412")
  191. if psk:
  192. dev.set_network_quoted(id, "psk", "thisismypassphrase!")
  193. return id
  194. def test_wpas_mesh_secure(dev, apdev):
  195. """wpa_supplicant secure MESH network connectivity"""
  196. if not mesh_supported(dev[0]):
  197. return "skip"
  198. dev[0].request("SET sae_groups ")
  199. id = add_mesh_secure_net(dev[0])
  200. dev[0].mesh_group_add(id)
  201. dev[1].request("SET sae_groups ")
  202. id = add_mesh_secure_net(dev[1])
  203. dev[1].mesh_group_add(id)
  204. # Check for mesh joined
  205. check_mesh_group_added(dev[0])
  206. check_mesh_group_added(dev[1])
  207. # Check for peer connected
  208. check_mesh_peer_connected(dev[0])
  209. check_mesh_peer_connected(dev[1])
  210. # Test connectivity 0->1 and 1->0
  211. hwsim_utils.test_connectivity(dev[0], dev[1])
  212. def test_wpas_mesh_secure_sae_group_mismatch(dev, apdev):
  213. """wpa_supplicant secure MESH and SAE group mismatch"""
  214. if not mesh_supported(dev[0]):
  215. return "skip"
  216. addr0 = dev[0].p2p_interface_addr()
  217. addr1 = dev[1].p2p_interface_addr()
  218. addr2 = dev[2].p2p_interface_addr()
  219. dev[0].request("SET sae_groups 19 25")
  220. id = add_mesh_secure_net(dev[0])
  221. dev[0].mesh_group_add(id)
  222. dev[1].request("SET sae_groups 19")
  223. id = add_mesh_secure_net(dev[1])
  224. dev[1].mesh_group_add(id)
  225. dev[2].request("SET sae_groups 26")
  226. id = add_mesh_secure_net(dev[2])
  227. dev[2].mesh_group_add(id)
  228. check_mesh_group_added(dev[0])
  229. check_mesh_group_added(dev[1])
  230. check_mesh_group_added(dev[2])
  231. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"])
  232. if ev is None:
  233. raise Exception("Remote peer did not connect")
  234. if addr1 not in ev:
  235. raise Exception("Unexpected peer connected: " + ev)
  236. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"])
  237. if ev is None:
  238. raise Exception("Remote peer did not connect")
  239. if addr0 not in ev:
  240. raise Exception("Unexpected peer connected: " + ev)
  241. ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  242. if ev is not None:
  243. raise Exception("Unexpected peer connection at dev[2]: " + ev)
  244. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  245. if ev is not None:
  246. raise Exception("Unexpected peer connection: " + ev)
  247. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  248. if ev is not None:
  249. raise Exception("Unexpected peer connection: " + ev)
  250. dev[0].request("SET sae_groups ")
  251. dev[1].request("SET sae_groups ")
  252. dev[2].request("SET sae_groups ")
  253. def test_wpas_mesh_secure_sae_missing_password(dev, apdev):
  254. """wpa_supplicant secure MESH and missing SAE password"""
  255. if not mesh_supported(dev[0]):
  256. return "skip"
  257. id = add_mesh_secure_net(dev[0], psk=False)
  258. dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
  259. dev[0].mesh_group_add(id)
  260. ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
  261. timeout=5)
  262. if ev is None:
  263. raise Exception("Timeout on mesh start event")
  264. if "MESH-GROUP-STARTED" in ev:
  265. raise Exception("Unexpected mesh group start")
  266. ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
  267. if ev is not None:
  268. raise Exception("Unexpected mesh group start")
  269. def test_wpas_mesh_secure_no_auto(dev, apdev):
  270. """wpa_supplicant secure MESH network connectivity"""
  271. if not mesh_supported(dev[0]):
  272. return "skip"
  273. dev[0].request("SET sae_groups 19")
  274. id = add_mesh_secure_net(dev[0])
  275. dev[0].mesh_group_add(id)
  276. dev[1].request("SET sae_groups 19")
  277. id = add_mesh_secure_net(dev[1])
  278. dev[1].set_network(id, "no_auto_peer", "1")
  279. dev[1].mesh_group_add(id)
  280. # Check for mesh joined
  281. check_mesh_group_added(dev[0])
  282. check_mesh_group_added(dev[1])
  283. # Check for peer connected
  284. check_mesh_peer_connected(dev[0], timeout=30)
  285. check_mesh_peer_connected(dev[1])
  286. # Test connectivity 0->1 and 1->0
  287. hwsim_utils.test_connectivity(dev[0], dev[1])
  288. dev[0].request("SET sae_groups ")
  289. dev[1].request("SET sae_groups ")
  290. def test_wpas_mesh_ctrl(dev):
  291. """wpa_supplicant ctrl_iface mesh command error cases"""
  292. if not mesh_supported(dev[0]):
  293. return "skip"
  294. if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
  295. raise Exception("Unexpected MESH_GROUP_ADD success")
  296. id = dev[0].add_network()
  297. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  298. raise Exception("Unexpected MESH_GROUP_ADD success")
  299. dev[0].set_network(id, "mode", "5")
  300. dev[0].set_network(id, "key_mgmt", "WPA-PSK")
  301. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  302. raise Exception("Unexpected MESH_GROUP_ADD success")
  303. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
  304. raise Exception("Unexpected MESH_GROUP_REMOVE success")
  305. def test_wpas_mesh_dynamic_interface(dev):
  306. """wpa_supplicant mesh with dynamic interface"""
  307. if not mesh_supported(dev[0]):
  308. return "skip"
  309. mesh0 = None
  310. mesh1 = None
  311. try:
  312. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  313. if "FAIL" in mesh0:
  314. raise Exception("MESH_INTERFACE_ADD failed")
  315. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  316. if "FAIL" in mesh1:
  317. raise Exception("MESH_INTERFACE_ADD failed")
  318. wpas0 = WpaSupplicant(ifname=mesh0)
  319. wpas1 = WpaSupplicant(ifname=mesh1)
  320. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  321. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  322. add_open_mesh_network(wpas0)
  323. add_open_mesh_network(wpas1)
  324. check_mesh_group_added(wpas0)
  325. check_mesh_group_added(wpas1)
  326. check_mesh_peer_connected(wpas0)
  327. check_mesh_peer_connected(wpas1)
  328. hwsim_utils.test_connectivity(wpas0, wpas1)
  329. # Must not allow MESH_GROUP_REMOVE on dynamic interface
  330. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
  331. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  332. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
  333. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  334. # Must not allow MESH_GROUP_REMOVE on another radio interface
  335. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
  336. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  337. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
  338. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  339. wpas0.remove_ifname()
  340. wpas1.remove_ifname()
  341. if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  342. raise Exception("MESH_GROUP_REMOVE failed")
  343. if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  344. raise Exception("MESH_GROUP_REMOVE failed")
  345. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  346. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  347. if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  348. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  349. logger.info("Make sure another dynamic group can be added")
  350. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  351. if "FAIL" in mesh0:
  352. raise Exception("MESH_INTERFACE_ADD failed")
  353. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  354. if "FAIL" in mesh1:
  355. raise Exception("MESH_INTERFACE_ADD failed")
  356. wpas0 = WpaSupplicant(ifname=mesh0)
  357. wpas1 = WpaSupplicant(ifname=mesh1)
  358. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  359. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  360. add_open_mesh_network(wpas0)
  361. add_open_mesh_network(wpas1)
  362. check_mesh_group_added(wpas0)
  363. check_mesh_group_added(wpas1)
  364. check_mesh_peer_connected(wpas0)
  365. check_mesh_peer_connected(wpas1)
  366. hwsim_utils.test_connectivity(wpas0, wpas1)
  367. finally:
  368. if mesh0:
  369. dev[0].request("MESH_GROUP_REMOVE " + mesh0)
  370. if mesh1:
  371. dev[1].request("MESH_GROUP_REMOVE " + mesh1)
  372. def test_wpas_mesh_max_peering(dev, apdev):
  373. """Mesh max peering limit"""
  374. if not mesh_supported(dev[0]):
  375. return "skip"
  376. try:
  377. dev[0].request("SET max_peer_links 1")
  378. # first, connect dev[0] and dev[1]
  379. add_open_mesh_network(dev[0])
  380. add_open_mesh_network(dev[1])
  381. for i in range(2):
  382. ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
  383. if ev is None:
  384. raise Exception("dev%d did not connect with any peer" % i)
  385. # add dev[2] which will try to connect with both dev[0] and dev[1],
  386. # but can complete connection only with dev[1]
  387. add_open_mesh_network(dev[2])
  388. for i in range(1, 3):
  389. ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
  390. if ev is None:
  391. raise Exception("dev%d did not connect the second peer" % i)
  392. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  393. if ev is not None:
  394. raise Exception("dev0 connection beyond max peering limit")
  395. ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  396. if ev is not None:
  397. raise Exception("dev2 reported unexpected peering: " + ev)
  398. for i in range(3):
  399. dev[i].mesh_group_remove()
  400. check_mesh_group_removed(dev[i])
  401. finally:
  402. dev[0].request("SET max_peer_links 99")