test_wpas_mesh.py 17 KB

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