test_wpas_mesh.py 17 KB

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