test_wpas_mesh.py 17 KB

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