test_wpas_mesh.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. from utils import HwsimSkip
  13. def check_mesh_support(dev):
  14. flags = int(dev.get_driver_status_field('capa.flags'), 16)
  15. if flags & 0x100000000 == 0:
  16. raise HwsimSkip("Driver does not support mesh")
  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. check_mesh_support(dev[0])
  98. add_open_mesh_network(dev[0])
  99. # Check for MESH-GROUP-STARTED event
  100. check_mesh_group_added(dev[0])
  101. def test_wpas_mesh_group_remove(dev):
  102. """wpa_supplicant MESH group remove"""
  103. check_mesh_support(dev[0])
  104. add_open_mesh_network(dev[0], ht_mode="NOHT")
  105. # Check for MESH-GROUP-STARTED event
  106. check_mesh_group_added(dev[0])
  107. dev[0].mesh_group_remove()
  108. # Check for MESH-GROUP-REMOVED event
  109. check_mesh_group_removed(dev[0])
  110. dev[0].mesh_group_remove()
  111. def test_wpas_mesh_peer_connected(dev):
  112. """wpa_supplicant MESH peer connected"""
  113. check_mesh_support(dev[0])
  114. add_open_mesh_network(dev[0], ht_mode="HT20", beacon_int=160)
  115. add_open_mesh_network(dev[1], ht_mode="HT20", beacon_int=160)
  116. # Check for mesh joined
  117. check_mesh_group_added(dev[0])
  118. check_mesh_group_added(dev[1])
  119. # Check for peer connected
  120. check_mesh_peer_connected(dev[0])
  121. check_mesh_peer_connected(dev[1])
  122. def test_wpas_mesh_peer_disconnected(dev):
  123. """wpa_supplicant MESH peer disconnected"""
  124. check_mesh_support(dev[0])
  125. add_open_mesh_network(dev[0])
  126. add_open_mesh_network(dev[1])
  127. # Check for mesh joined
  128. check_mesh_group_added(dev[0])
  129. check_mesh_group_added(dev[1])
  130. # Check for peer connected
  131. check_mesh_peer_connected(dev[0])
  132. check_mesh_peer_connected(dev[1])
  133. # Remove group on dev 1
  134. dev[1].mesh_group_remove()
  135. # Device 0 should get a disconnection event
  136. check_mesh_peer_disconnected(dev[0])
  137. def test_wpas_mesh_mode_scan(dev):
  138. """wpa_supplicant MESH scan support"""
  139. check_mesh_support(dev[0])
  140. add_open_mesh_network(dev[0], ht_mode="HT40+")
  141. add_open_mesh_network(dev[1], ht_mode="HT40+", beacon_int=175)
  142. # Check for mesh joined
  143. check_mesh_group_added(dev[0])
  144. check_mesh_group_added(dev[1])
  145. # Check for Mesh scan
  146. check_mesh_scan(dev[0], "use_id=1", beacon_int=175)
  147. def test_wpas_mesh_open(dev, apdev):
  148. """wpa_supplicant open MESH network connectivity"""
  149. check_mesh_support(dev[0])
  150. add_open_mesh_network(dev[0], ht_mode="HT40-", freq="2462")
  151. add_open_mesh_network(dev[1], ht_mode="HT40-", freq="2462")
  152. # Check for mesh joined
  153. check_mesh_group_added(dev[0])
  154. check_mesh_group_added(dev[1])
  155. # Check for peer connected
  156. check_mesh_peer_connected(dev[0])
  157. check_mesh_peer_connected(dev[1])
  158. # Test connectivity 0->1 and 1->0
  159. hwsim_utils.test_connectivity(dev[0], dev[1])
  160. def test_wpas_mesh_open_no_auto(dev, apdev):
  161. """wpa_supplicant open MESH network connectivity"""
  162. check_mesh_support(dev[0])
  163. id = add_open_mesh_network(dev[0], start=False)
  164. dev[0].set_network(id, "dot11MeshMaxRetries", "16")
  165. dev[0].set_network(id, "dot11MeshRetryTimeout", "255")
  166. dev[0].mesh_group_add(id)
  167. id = add_open_mesh_network(dev[1], start=False)
  168. dev[1].set_network(id, "no_auto_peer", "1")
  169. dev[1].mesh_group_add(id)
  170. # Check for mesh joined
  171. check_mesh_group_added(dev[0])
  172. check_mesh_group_added(dev[1])
  173. # Check for peer connected
  174. check_mesh_peer_connected(dev[0], timeout=30)
  175. check_mesh_peer_connected(dev[1])
  176. # Test connectivity 0->1 and 1->0
  177. hwsim_utils.test_connectivity(dev[0], dev[1])
  178. def add_mesh_secure_net(dev, psk=True):
  179. id = dev.add_network()
  180. dev.set_network(id, "mode", "5")
  181. dev.set_network_quoted(id, "ssid", "wpas-mesh-sec")
  182. dev.set_network(id, "key_mgmt", "SAE")
  183. dev.set_network(id, "frequency", "2412")
  184. if psk:
  185. dev.set_network_quoted(id, "psk", "thisismypassphrase!")
  186. return id
  187. def test_wpas_mesh_secure(dev, apdev):
  188. """wpa_supplicant secure MESH network connectivity"""
  189. check_mesh_support(dev[0])
  190. dev[0].request("SET sae_groups ")
  191. id = add_mesh_secure_net(dev[0])
  192. dev[0].mesh_group_add(id)
  193. dev[1].request("SET sae_groups ")
  194. id = add_mesh_secure_net(dev[1])
  195. dev[1].mesh_group_add(id)
  196. # Check for mesh joined
  197. check_mesh_group_added(dev[0])
  198. check_mesh_group_added(dev[1])
  199. # Check for peer connected
  200. check_mesh_peer_connected(dev[0])
  201. check_mesh_peer_connected(dev[1])
  202. # Test connectivity 0->1 and 1->0
  203. hwsim_utils.test_connectivity(dev[0], dev[1])
  204. def test_wpas_mesh_secure_sae_group_mismatch(dev, apdev):
  205. """wpa_supplicant secure MESH and SAE group mismatch"""
  206. check_mesh_support(dev[0])
  207. addr0 = dev[0].p2p_interface_addr()
  208. addr1 = dev[1].p2p_interface_addr()
  209. addr2 = dev[2].p2p_interface_addr()
  210. dev[0].request("SET sae_groups 19 25")
  211. id = add_mesh_secure_net(dev[0])
  212. dev[0].mesh_group_add(id)
  213. dev[1].request("SET sae_groups 19")
  214. id = add_mesh_secure_net(dev[1])
  215. dev[1].mesh_group_add(id)
  216. dev[2].request("SET sae_groups 26")
  217. id = add_mesh_secure_net(dev[2])
  218. dev[2].mesh_group_add(id)
  219. check_mesh_group_added(dev[0])
  220. check_mesh_group_added(dev[1])
  221. check_mesh_group_added(dev[2])
  222. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"])
  223. if ev is None:
  224. raise Exception("Remote peer did not connect")
  225. if addr1 not in ev:
  226. raise Exception("Unexpected peer connected: " + ev)
  227. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"])
  228. if ev is None:
  229. raise Exception("Remote peer did not connect")
  230. if addr0 not in ev:
  231. raise Exception("Unexpected peer connected: " + ev)
  232. ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  233. if ev is not None:
  234. raise Exception("Unexpected peer connection at dev[2]: " + ev)
  235. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  236. if ev is not None:
  237. raise Exception("Unexpected peer connection: " + ev)
  238. ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  239. if ev is not None:
  240. raise Exception("Unexpected peer connection: " + ev)
  241. dev[0].request("SET sae_groups ")
  242. dev[1].request("SET sae_groups ")
  243. dev[2].request("SET sae_groups ")
  244. def test_wpas_mesh_secure_sae_missing_password(dev, apdev):
  245. """wpa_supplicant secure MESH and missing SAE password"""
  246. check_mesh_support(dev[0])
  247. id = add_mesh_secure_net(dev[0], psk=False)
  248. dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
  249. dev[0].mesh_group_add(id)
  250. ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
  251. timeout=5)
  252. if ev is None:
  253. raise Exception("Timeout on mesh start event")
  254. if "MESH-GROUP-STARTED" in ev:
  255. raise Exception("Unexpected mesh group start")
  256. ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
  257. if ev is not None:
  258. raise Exception("Unexpected mesh group start")
  259. def test_wpas_mesh_secure_no_auto(dev, apdev):
  260. """wpa_supplicant secure MESH network connectivity"""
  261. check_mesh_support(dev[0])
  262. dev[0].request("SET sae_groups 19")
  263. id = add_mesh_secure_net(dev[0])
  264. dev[0].mesh_group_add(id)
  265. dev[1].request("SET sae_groups 19")
  266. id = add_mesh_secure_net(dev[1])
  267. dev[1].set_network(id, "no_auto_peer", "1")
  268. dev[1].mesh_group_add(id)
  269. # Check for mesh joined
  270. check_mesh_group_added(dev[0])
  271. check_mesh_group_added(dev[1])
  272. # Check for peer connected
  273. check_mesh_peer_connected(dev[0], timeout=30)
  274. check_mesh_peer_connected(dev[1])
  275. # Test connectivity 0->1 and 1->0
  276. hwsim_utils.test_connectivity(dev[0], dev[1])
  277. dev[0].request("SET sae_groups ")
  278. dev[1].request("SET sae_groups ")
  279. def test_wpas_mesh_ctrl(dev):
  280. """wpa_supplicant ctrl_iface mesh command error cases"""
  281. check_mesh_support(dev[0])
  282. if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
  283. raise Exception("Unexpected MESH_GROUP_ADD success")
  284. id = dev[0].add_network()
  285. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  286. raise Exception("Unexpected MESH_GROUP_ADD success")
  287. dev[0].set_network(id, "mode", "5")
  288. dev[0].set_network(id, "key_mgmt", "WPA-PSK")
  289. if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
  290. raise Exception("Unexpected MESH_GROUP_ADD success")
  291. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
  292. raise Exception("Unexpected MESH_GROUP_REMOVE success")
  293. def test_wpas_mesh_dynamic_interface(dev):
  294. """wpa_supplicant mesh with dynamic interface"""
  295. check_mesh_support(dev[0])
  296. mesh0 = None
  297. mesh1 = None
  298. try:
  299. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  300. if "FAIL" in mesh0:
  301. raise Exception("MESH_INTERFACE_ADD failed")
  302. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  303. if "FAIL" in mesh1:
  304. raise Exception("MESH_INTERFACE_ADD failed")
  305. wpas0 = WpaSupplicant(ifname=mesh0)
  306. wpas1 = WpaSupplicant(ifname=mesh1)
  307. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  308. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  309. add_open_mesh_network(wpas0)
  310. add_open_mesh_network(wpas1)
  311. check_mesh_group_added(wpas0)
  312. check_mesh_group_added(wpas1)
  313. check_mesh_peer_connected(wpas0)
  314. check_mesh_peer_connected(wpas1)
  315. hwsim_utils.test_connectivity(wpas0, wpas1)
  316. # Must not allow MESH_GROUP_REMOVE on dynamic interface
  317. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
  318. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  319. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
  320. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  321. # Must not allow MESH_GROUP_REMOVE on another radio interface
  322. if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
  323. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  324. if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
  325. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  326. wpas0.remove_ifname()
  327. wpas1.remove_ifname()
  328. if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  329. raise Exception("MESH_GROUP_REMOVE failed")
  330. if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  331. raise Exception("MESH_GROUP_REMOVE failed")
  332. if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
  333. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  334. if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
  335. raise Exception("Invalid MESH_GROUP_REMOVE accepted")
  336. logger.info("Make sure another dynamic group can be added")
  337. mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
  338. if "FAIL" in mesh0:
  339. raise Exception("MESH_INTERFACE_ADD failed")
  340. mesh1 = dev[1].request("MESH_INTERFACE_ADD")
  341. if "FAIL" in mesh1:
  342. raise Exception("MESH_INTERFACE_ADD failed")
  343. wpas0 = WpaSupplicant(ifname=mesh0)
  344. wpas1 = WpaSupplicant(ifname=mesh1)
  345. logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
  346. logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
  347. add_open_mesh_network(wpas0)
  348. add_open_mesh_network(wpas1)
  349. check_mesh_group_added(wpas0)
  350. check_mesh_group_added(wpas1)
  351. check_mesh_peer_connected(wpas0)
  352. check_mesh_peer_connected(wpas1)
  353. hwsim_utils.test_connectivity(wpas0, wpas1)
  354. finally:
  355. if mesh0:
  356. dev[0].request("MESH_GROUP_REMOVE " + mesh0)
  357. if mesh1:
  358. dev[1].request("MESH_GROUP_REMOVE " + mesh1)
  359. def test_wpas_mesh_max_peering(dev, apdev):
  360. """Mesh max peering limit"""
  361. check_mesh_support(dev[0])
  362. try:
  363. dev[0].request("SET max_peer_links 1")
  364. # first, connect dev[0] and dev[1]
  365. add_open_mesh_network(dev[0])
  366. add_open_mesh_network(dev[1])
  367. for i in range(2):
  368. ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
  369. if ev is None:
  370. raise Exception("dev%d did not connect with any peer" % i)
  371. # add dev[2] which will try to connect with both dev[0] and dev[1],
  372. # but can complete connection only with dev[1]
  373. add_open_mesh_network(dev[2])
  374. for i in range(1, 3):
  375. ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
  376. if ev is None:
  377. raise Exception("dev%d did not connect the second peer" % i)
  378. ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
  379. if ev is not None:
  380. raise Exception("dev0 connection beyond max peering limit")
  381. ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
  382. if ev is not None:
  383. raise Exception("dev2 reported unexpected peering: " + ev)
  384. for i in range(3):
  385. dev[i].mesh_group_remove()
  386. check_mesh_group_removed(dev[i])
  387. finally:
  388. dev[0].request("SET max_peer_links 99")