test_p2p_channel.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. # P2P channel selection test cases
  2. # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import logging
  7. logger = logging.getLogger()
  8. import os
  9. import subprocess
  10. import time
  11. import hostapd
  12. import hwsim_utils
  13. from test_p2p_grpform import go_neg_pin_authorized
  14. from test_p2p_grpform import check_grpform_results
  15. from test_p2p_grpform import remove_group
  16. from test_p2p_grpform import go_neg_pbc
  17. from test_p2p_autogo import autogo
  18. def set_country(country):
  19. subprocess.call(['sudo', 'iw', 'reg', 'set', country])
  20. time.sleep(0.1)
  21. def test_p2p_channel_5ghz(dev):
  22. """P2P group formation with 5 GHz preference"""
  23. try:
  24. set_country("US")
  25. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  26. r_dev=dev[1], r_intent=0,
  27. test_data=False)
  28. check_grpform_results(i_res, r_res)
  29. freq = int(i_res['freq'])
  30. if freq < 5000:
  31. raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
  32. remove_group(dev[0], dev[1])
  33. finally:
  34. set_country("00")
  35. def test_p2p_channel_5ghz_no_vht(dev):
  36. """P2P group formation with 5 GHz preference when VHT channels are disallowed"""
  37. try:
  38. set_country("US")
  39. dev[0].request("P2P_SET disallow_freq 5180-5240")
  40. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  41. r_dev=dev[1], r_intent=0,
  42. test_data=False)
  43. check_grpform_results(i_res, r_res)
  44. freq = int(i_res['freq'])
  45. if freq < 5000:
  46. raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
  47. remove_group(dev[0], dev[1])
  48. finally:
  49. set_country("00")
  50. dev[0].request("P2P_SET disallow_freq ")
  51. def test_p2p_channel_random_social(dev):
  52. """P2P group formation with 5 GHz preference but all 5 GHz channels disabled"""
  53. try:
  54. set_country("US")
  55. dev[0].request("SET p2p_oper_channel 11")
  56. dev[0].request("P2P_SET disallow_freq 5000-6000,2462")
  57. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  58. r_dev=dev[1], r_intent=0,
  59. test_data=False)
  60. check_grpform_results(i_res, r_res)
  61. freq = int(i_res['freq'])
  62. if freq not in [ 2412, 2437, 2462 ]:
  63. raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
  64. remove_group(dev[0], dev[1])
  65. finally:
  66. set_country("00")
  67. dev[0].request("P2P_SET disallow_freq ")
  68. def test_p2p_channel_random(dev):
  69. """P2P group formation with 5 GHz preference but all 5 GHz channels and all social channels disabled"""
  70. try:
  71. set_country("US")
  72. dev[0].request("SET p2p_oper_channel 11")
  73. dev[0].request("P2P_SET disallow_freq 5000-6000,2412,2437,2462")
  74. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  75. r_dev=dev[1], r_intent=0,
  76. test_data=False)
  77. check_grpform_results(i_res, r_res)
  78. freq = int(i_res['freq'])
  79. if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
  80. raise Exception("Unexpected channel %d MHz" % freq)
  81. remove_group(dev[0], dev[1])
  82. finally:
  83. set_country("00")
  84. dev[0].request("P2P_SET disallow_freq ")
  85. def test_p2p_channel_random_social_with_op_class_change(dev, apdev, params):
  86. """P2P group formation using random social channel with oper class change needed"""
  87. try:
  88. set_country("US")
  89. logger.info("Start group on 5 GHz")
  90. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  91. r_dev=dev[1], r_intent=0,
  92. test_data=False)
  93. check_grpform_results(i_res, r_res)
  94. freq = int(i_res['freq'])
  95. if freq < 5000:
  96. raise Exception("Unexpected channel %d MHz - did not pick 5 GHz preference" % freq)
  97. remove_group(dev[0], dev[1])
  98. logger.info("Disable 5 GHz and try to re-start group based on 5 GHz preference")
  99. dev[0].request("SET p2p_oper_reg_class 115")
  100. dev[0].request("SET p2p_oper_channel 36")
  101. dev[0].request("P2P_SET disallow_freq 5000-6000")
  102. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  103. r_dev=dev[1], r_intent=0,
  104. test_data=False)
  105. check_grpform_results(i_res, r_res)
  106. freq = int(i_res['freq'])
  107. if freq not in [ 2412, 2437, 2462 ]:
  108. raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
  109. remove_group(dev[0], dev[1])
  110. try:
  111. arg = [ "tshark",
  112. "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
  113. "-Y", "wifi_p2p.public_action.subtype == 0",
  114. "-V" ]
  115. cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
  116. stderr=open('/dev/null', 'w'))
  117. except Exception, e:
  118. logger.info("Could run run tshark check: " + str(e))
  119. cmd = None
  120. pass
  121. if cmd:
  122. last = None
  123. for l in cmd.stdout.read().splitlines():
  124. if "Operating Channel:" not in l:
  125. continue
  126. last = l
  127. if last is None:
  128. raise Exception("Could not find GO Negotiation Request")
  129. if "Operating Class 81" not in last:
  130. raise Exception("Unexpected operating class: " + last.strip())
  131. finally:
  132. set_country("00")
  133. dev[0].request("P2P_SET disallow_freq ")
  134. dev[0].request("SET p2p_oper_reg_class 81")
  135. dev[0].request("SET p2p_oper_channel 11")
  136. def test_p2p_channel_avoid(dev):
  137. """P2P and avoid frequencies driver event"""
  138. try:
  139. set_country("US")
  140. if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES 5000-6000,2412,2437,2462"):
  141. raise Exception("Could not simulate driver event")
  142. ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
  143. if ev is None:
  144. raise Exception("No CTRL-EVENT-AVOID-FREQ event")
  145. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  146. r_dev=dev[1], r_intent=0,
  147. test_data=False)
  148. check_grpform_results(i_res, r_res)
  149. freq = int(i_res['freq'])
  150. if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
  151. raise Exception("Unexpected channel %d MHz" % freq)
  152. if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES"):
  153. raise Exception("Could not simulate driver event(2)")
  154. ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
  155. if ev is None:
  156. raise Exception("No CTRL-EVENT-AVOID-FREQ event")
  157. ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=1)
  158. if ev is not None:
  159. raise Exception("Unexpected P2P-REMOVE-AND-REFORM-GROUP event")
  160. if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES " + str(freq)):
  161. raise Exception("Could not simulate driver event(3)")
  162. ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
  163. if ev is None:
  164. raise Exception("No CTRL-EVENT-AVOID-FREQ event")
  165. ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=10)
  166. if ev is None:
  167. raise Exception("No P2P-REMOVE-AND-REFORM-GROUP event")
  168. finally:
  169. set_country("00")
  170. dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES")
  171. def test_autogo_following_bss(dev, apdev):
  172. """P2P autonomous GO operate on the same channel as station interface"""
  173. if dev[0].get_mcc() > 1:
  174. logger.info("test mode: MCC")
  175. dev[0].request("SET p2p_no_group_iface 0")
  176. channels = { 3 : "2422", 5 : "2432", 9 : "2452" }
  177. for key in channels:
  178. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
  179. "channel" : str(key) })
  180. dev[0].connect("ap-test", key_mgmt="NONE",
  181. scan_freq=str(channels[key]))
  182. res_go = autogo(dev[0])
  183. if res_go['freq'] != channels[key]:
  184. raise Exception("Group operation channel is not the same as on connected station interface")
  185. hwsim_utils.test_connectivity(dev[0], hapd)
  186. dev[0].remove_group(res_go['ifname'])
  187. def test_go_neg_with_bss_connected(dev, apdev):
  188. """P2P channel selection: GO negotiation when station interface is connected"""
  189. dev[0].request("SET p2p_no_group_iface 0")
  190. hapd = hostapd.add_ap(apdev[0]['ifname'],
  191. { "ssid": 'bss-2.4ghz', "channel": '5' })
  192. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2432")
  193. #dev[0] as GO
  194. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[1],
  195. r_intent=1)
  196. check_grpform_results(i_res, r_res)
  197. if i_res['role'] != "GO":
  198. raise Exception("GO not selected according to go_intent")
  199. if i_res['freq'] != "2432":
  200. raise Exception("Group formed on a different frequency than BSS")
  201. hwsim_utils.test_connectivity(dev[0], hapd)
  202. dev[0].remove_group(i_res['ifname'])
  203. if dev[0].get_mcc() > 1:
  204. logger.info("Skip as-client case due to MCC being enabled")
  205. return;
  206. #dev[0] as client
  207. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
  208. r_intent=10)
  209. check_grpform_results(i_res2, r_res2)
  210. if i_res2['role'] != "client":
  211. raise Exception("GO not selected according to go_intent")
  212. if i_res2['freq'] != "2432":
  213. raise Exception("Group formed on a different frequency than BSS")
  214. hwsim_utils.test_connectivity(dev[0], hapd)
  215. def test_autogo_with_bss_on_disallowed_chan(dev, apdev):
  216. """P2P channel selection: Autonomous GO with BSS on a disallowed channel"""
  217. dev[0].request("SET p2p_no_group_iface 0")
  218. if dev[0].get_mcc() < 2:
  219. logger.info("Skipping test because driver does not support MCC")
  220. return "skip"
  221. try:
  222. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  223. "channel": '1' })
  224. dev[0].request("P2P_SET disallow_freq 2412")
  225. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  226. res = autogo(dev[0])
  227. if res['freq'] == "2412":
  228. raise Exception("GO set on a disallowed channel")
  229. hwsim_utils.test_connectivity(dev[0], hapd)
  230. finally:
  231. dev[0].request("P2P_SET disallow_freq ")
  232. def test_go_neg_with_bss_on_disallowed_chan(dev, apdev):
  233. """P2P channel selection: GO negotiation with station interface on a disallowed channel"""
  234. dev[0].request("SET p2p_no_group_iface 0")
  235. if dev[0].get_mcc() < 2:
  236. logger.info("Skipping test because driver does not support MCC")
  237. return "skip"
  238. try:
  239. hapd = hostapd.add_ap(apdev[0]['ifname'],
  240. { "ssid": 'bss-2.4ghz', "channel": '1' })
  241. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  242. dev[0].request("P2P_SET disallow_freq 2412")
  243. #dev[0] as GO
  244. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[1],
  245. r_intent=1)
  246. check_grpform_results(i_res, r_res)
  247. if i_res['role'] != "GO":
  248. raise Exception("GO not selected according to go_intent")
  249. if i_res['freq'] == "2412":
  250. raise Exception("Group formed on a disallowed channel")
  251. hwsim_utils.test_connectivity(dev[0], hapd)
  252. dev[0].remove_group(i_res['ifname'])
  253. #dev[0] as client
  254. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
  255. r_intent=10)
  256. check_grpform_results(i_res2, r_res2)
  257. if i_res2['role'] != "client":
  258. raise Exception("GO not selected according to go_intent")
  259. if i_res2['freq'] == "2412":
  260. raise Exception("Group formed on a disallowed channel")
  261. hwsim_utils.test_connectivity(dev[0], hapd)
  262. finally:
  263. dev[0].request("P2P_SET disallow_freq ")
  264. def test_autogo_force_diff_channel(dev, apdev):
  265. """P2P autonomous GO and station interface operate on different channels"""
  266. if dev[0].get_mcc() < 2:
  267. logger.info("Skiping test because the driver doesn't support MCC")
  268. return "skip"
  269. dev[0].request("SET p2p_no_group_iface 0")
  270. hapd = hostapd.add_ap(apdev[0]['ifname'],
  271. {"ssid" : 'ap-test', "channel" : '1'})
  272. dev[0].connect("ap-test", key_mgmt = "NONE", scan_freq = "2412")
  273. channels = { 2 : 2417, 5 : 2432, 9 : 2452 }
  274. for key in channels:
  275. res_go = autogo(dev[0], channels[key])
  276. hwsim_utils.test_connectivity(dev[0], hapd)
  277. if int(res_go['freq']) == 2412:
  278. raise Exception("Group operation channel is: 2412 excepted: " + res_go['freq'])
  279. dev[0].remove_group(res_go['ifname'])
  280. def test_go_neg_forced_freq_diff_than_bss_freq(dev, apdev):
  281. """P2P channel selection: GO negotiation with forced freq different than station interface"""
  282. if dev[0].get_mcc() < 2:
  283. logger.info("Skipping test because driver does not support MCC")
  284. return "skip"
  285. dev[0].request("SET p2p_no_group_iface 0")
  286. hapd = hostapd.add_ap(apdev[0]['ifname'],
  287. { "country_code": 'US',
  288. "ssid": 'bss-5ghz', "hw_mode": 'a',
  289. "channel": '40' })
  290. dev[0].connect("bss-5ghz", key_mgmt="NONE", scan_freq="5200")
  291. # GO and peer force the same freq, different than BSS freq,
  292. # dev[0] to become GO
  293. [i_res, r_res] = go_neg_pbc(i_dev=dev[1], i_intent=1, i_freq=5180,
  294. r_dev=dev[0], r_intent=14, r_freq=5180)
  295. check_grpform_results(i_res, r_res)
  296. if i_res['freq'] != "5180":
  297. raise Exception("P2P group formed on unexpected frequency: " + i_res['freq'])
  298. if r_res['role'] != "GO":
  299. raise Exception("GO not selected according to go_intent")
  300. hwsim_utils.test_connectivity(dev[0], hapd)
  301. dev[0].remove_group(r_res['ifname'])
  302. # GO and peer force the same freq, different than BSS freq, dev[0] to
  303. # become client
  304. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[1], i_intent=14, i_freq=2422,
  305. r_dev=dev[0], r_intent=1, r_freq=2422)
  306. check_grpform_results(i_res2, r_res2)
  307. if i_res2['freq'] != "2422":
  308. raise Exception("P2P group formed on unexpected frequency: " + i_res2['freq'])
  309. if r_res2['role'] != "client":
  310. raise Exception("GO not selected according to go_intent")
  311. hwsim_utils.test_connectivity(dev[0], hapd)
  312. def test_go_pref_chan_bss_on_diff_chan(dev, apdev):
  313. """P2P channel selection: Station on different channel than GO configured pref channel"""
  314. dev[0].request("SET p2p_no_group_iface 0")
  315. try:
  316. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  317. "channel": '1' })
  318. dev[0].request("SET p2p_pref_chan 81:2")
  319. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  320. res = autogo(dev[0])
  321. if res['freq'] != "2412":
  322. raise Exception("GO channel did not follow BSS")
  323. hwsim_utils.test_connectivity(dev[0], hapd)
  324. finally:
  325. dev[0].request("SET p2p_pref_chan ")
  326. def test_go_pref_chan_bss_on_disallowed_chan(dev, apdev):
  327. """P2P channel selection: Station interface on different channel than GO configured pref channel, and station channel is disallowed"""
  328. if dev[0].get_mcc() < 2:
  329. logger.info("Skipping test because driver does not support MCC")
  330. return "skip"
  331. dev[0].request("SET p2p_no_group_iface 0")
  332. try:
  333. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  334. "channel": '1' })
  335. dev[0].request("P2P_SET disallow_freq 2412")
  336. dev[0].request("SET p2p_pref_chan 81:2")
  337. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  338. res2 = autogo(dev[0])
  339. if res2['freq'] != "2417":
  340. raise Exception("GO channel did not follow pref_chan configuration")
  341. hwsim_utils.test_connectivity(dev[0], hapd)
  342. finally:
  343. dev[0].request("P2P_SET disallow_freq ")
  344. dev[0].request("SET p2p_pref_chan ")
  345. def test_no_go_freq(dev, apdev):
  346. """P2P channel selection: no GO freq"""
  347. try:
  348. dev[0].request("SET p2p_no_go_freq 2412")
  349. # dev[0] as client, channel 1 is ok
  350. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=1,
  351. r_dev=dev[1], r_intent=14, r_freq=2412)
  352. check_grpform_results(i_res, r_res)
  353. if i_res['freq'] != "2412":
  354. raise Exception("P2P group not formed on forced freq")
  355. dev[1].remove_group(r_res['ifname'])
  356. fail = False
  357. # dev[0] as GO, channel 1 is not allowed
  358. try:
  359. dev[0].request("SET p2p_no_go_freq 2412")
  360. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14,
  361. r_dev=dev[1], r_intent=1, r_freq=2412)
  362. check_grpform_results(i_res2, r_res2)
  363. fail = True
  364. except:
  365. pass
  366. if fail:
  367. raise Exception("GO set on a disallowed freq")
  368. finally:
  369. dev[0].request("SET p2p_no_go_freq ")
  370. def test_go_neg_peers_force_diff_freq(dev, apdev):
  371. try:
  372. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14, i_freq=5180,
  373. r_dev=dev[1], r_intent=0, r_freq=5200)
  374. except Exception, e:
  375. return
  376. raise Exception("Unexpected group formation success")
  377. def test_autogo_random_channel(dev, apdev):
  378. """P2P channel selection: GO instantiated on random channel 1, 6, 11"""
  379. freqs = []
  380. go_freqs = ["2412", "2437", "2462"]
  381. for i in range(0, 20):
  382. result = autogo(dev[0])
  383. if result['freq'] not in go_freqs:
  384. raise Exception("Unexpected frequency selected: " + result['freq'])
  385. if result['freq'] not in freqs:
  386. freqs.append(result['freq'])
  387. if len(freqs) == 3:
  388. break
  389. dev[0].remove_group(result['ifname'])
  390. if i == 20:
  391. raise Exception("GO created 20 times and not all social channels were selected. freqs not selected: " + str(list(set(go_freqs) - set(freqs))))
  392. def test_p2p_autogo_pref_chan_disallowed(dev, apdev):
  393. """P2P channel selection: GO preferred channels are disallowed"""
  394. try:
  395. dev[0].request("SET p2p_pref_chan 81:1,81:3,81:6,81:9,81:11")
  396. dev[0].request("P2P_SET disallow_freq 2412,2422,2437,2452,2462")
  397. for i in range(0, 5):
  398. res = autogo(dev[0])
  399. if res['freq'] in [ "2412", "2422", "2437", "2452", "2462" ]:
  400. raise Exception("GO channel is disallowed")
  401. dev[0].remove_group(res['ifname'])
  402. finally:
  403. dev[0].request("P2P_SET disallow_freq ")
  404. dev[0].request("SET p2p_pref_chan ")
  405. def test_p2p_autogo_pref_chan_not_in_regulatory(dev, apdev):
  406. """P2P channel selection: GO preferred channel not allowed in the regulatory rules"""
  407. try:
  408. set_country("US")
  409. dev[0].request("SET p2p_pref_chan 124:149")
  410. res = autogo(dev[0], persistent=True)
  411. if res['freq'] != "5745":
  412. raise Exception("Unexpected channel selected: " + res['freq'])
  413. dev[0].remove_group(res['ifname'])
  414. netw = dev[0].list_networks()
  415. if len(netw) != 1:
  416. raise Exception("Unexpected number of network blocks: " + str(netw))
  417. id = netw[0]['id']
  418. set_country("DE")
  419. res = autogo(dev[0], persistent=id)
  420. if res['freq'] == "5745":
  421. raise Exception("Unexpected channel selected(2): " + res['freq'])
  422. dev[0].remove_group(res['ifname'])
  423. finally:
  424. dev[0].request("SET p2p_pref_chan ")
  425. set_country("00")