test_p2p_channel.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 utils import HwsimSkip
  14. from tshark import run_tshark
  15. from wpasupplicant import WpaSupplicant
  16. from hwsim import HWSimRadio
  17. from p2p_utils import *
  18. def set_country(country, dev=None):
  19. subprocess.call(['iw', 'reg', 'set', country])
  20. time.sleep(0.1)
  21. if dev:
  22. for i in range(10):
  23. ev = dev.wait_global_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=15)
  24. if ev is None:
  25. raise Exception("No regdom change event seen")
  26. if "type=COUNTRY alpha2=" + country in ev:
  27. return
  28. raise Exception("No matching regdom event seen for set_country(%s)" % country)
  29. def test_p2p_channel_5ghz(dev):
  30. """P2P group formation with 5 GHz preference"""
  31. try:
  32. set_country("US", dev[0])
  33. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  34. r_dev=dev[1], r_intent=0,
  35. test_data=False)
  36. check_grpform_results(i_res, r_res)
  37. freq = int(i_res['freq'])
  38. if freq < 5000:
  39. raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
  40. remove_group(dev[0], dev[1])
  41. finally:
  42. set_country("00")
  43. dev[1].flush_scan_cache()
  44. def test_p2p_channel_5ghz_no_vht(dev):
  45. """P2P group formation with 5 GHz preference when VHT channels are disallowed"""
  46. try:
  47. set_country("US", dev[0])
  48. dev[0].request("P2P_SET disallow_freq 5180-5240")
  49. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  50. r_dev=dev[1], r_intent=0,
  51. test_data=False)
  52. check_grpform_results(i_res, r_res)
  53. freq = int(i_res['freq'])
  54. if freq < 5000:
  55. raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
  56. remove_group(dev[0], dev[1])
  57. finally:
  58. set_country("00")
  59. dev[0].request("P2P_SET disallow_freq ")
  60. dev[1].flush_scan_cache()
  61. def test_p2p_channel_random_social(dev):
  62. """P2P group formation with 5 GHz preference but all 5 GHz channels disabled"""
  63. try:
  64. set_country("US", dev[0])
  65. dev[0].request("SET p2p_oper_channel 11")
  66. dev[0].request("P2P_SET disallow_freq 5000-6000,2462")
  67. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  68. r_dev=dev[1], r_intent=0,
  69. test_data=False)
  70. check_grpform_results(i_res, r_res)
  71. freq = int(i_res['freq'])
  72. if freq not in [ 2412, 2437, 2462 ]:
  73. raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
  74. remove_group(dev[0], dev[1])
  75. finally:
  76. set_country("00")
  77. dev[0].request("P2P_SET disallow_freq ")
  78. dev[1].flush_scan_cache()
  79. def test_p2p_channel_random(dev):
  80. """P2P group formation with 5 GHz preference but all 5 GHz channels and all social channels disabled"""
  81. try:
  82. set_country("US", dev[0])
  83. dev[0].request("SET p2p_oper_channel 11")
  84. dev[0].request("P2P_SET disallow_freq 5000-6000,2412,2437,2462")
  85. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  86. r_dev=dev[1], r_intent=0,
  87. test_data=False)
  88. check_grpform_results(i_res, r_res)
  89. freq = int(i_res['freq'])
  90. if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
  91. raise Exception("Unexpected channel %d MHz" % freq)
  92. remove_group(dev[0], dev[1])
  93. finally:
  94. set_country("00")
  95. dev[0].request("P2P_SET disallow_freq ")
  96. dev[1].flush_scan_cache()
  97. def test_p2p_channel_random_social_with_op_class_change(dev, apdev, params):
  98. """P2P group formation using random social channel with oper class change needed"""
  99. try:
  100. set_country("US", dev[0])
  101. logger.info("Start group on 5 GHz")
  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 < 5000:
  108. raise Exception("Unexpected channel %d MHz - did not pick 5 GHz preference" % freq)
  109. remove_group(dev[0], dev[1])
  110. logger.info("Disable 5 GHz and try to re-start group based on 5 GHz preference")
  111. dev[0].request("SET p2p_oper_reg_class 115")
  112. dev[0].request("SET p2p_oper_channel 36")
  113. dev[0].request("P2P_SET disallow_freq 5000-6000")
  114. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  115. r_dev=dev[1], r_intent=0,
  116. test_data=False)
  117. check_grpform_results(i_res, r_res)
  118. freq = int(i_res['freq'])
  119. if freq not in [ 2412, 2437, 2462 ]:
  120. raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
  121. remove_group(dev[0], dev[1])
  122. out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
  123. "wifi_p2p.public_action.subtype == 0")
  124. if out is not None:
  125. last = None
  126. for l in out.splitlines():
  127. if "Operating Channel:" not in l:
  128. continue
  129. last = l
  130. if last is None:
  131. raise Exception("Could not find GO Negotiation Request")
  132. if "Operating Class 81" not in last:
  133. raise Exception("Unexpected operating class: " + last.strip())
  134. finally:
  135. set_country("00")
  136. dev[0].request("P2P_SET disallow_freq ")
  137. dev[0].request("SET p2p_oper_reg_class 0")
  138. dev[0].request("SET p2p_oper_channel 0")
  139. dev[1].flush_scan_cache()
  140. def test_p2p_channel_avoid(dev):
  141. """P2P and avoid frequencies driver event"""
  142. try:
  143. set_country("US", dev[0])
  144. if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES 5000-6000,2412,2437,2462"):
  145. raise Exception("Could not simulate driver event")
  146. ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
  147. if ev is None:
  148. raise Exception("No CTRL-EVENT-AVOID-FREQ event")
  149. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  150. r_dev=dev[1], r_intent=0,
  151. test_data=False)
  152. check_grpform_results(i_res, r_res)
  153. freq = int(i_res['freq'])
  154. if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
  155. raise Exception("Unexpected channel %d MHz" % freq)
  156. if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES"):
  157. raise Exception("Could not simulate driver event(2)")
  158. ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
  159. if ev is None:
  160. raise Exception("No CTRL-EVENT-AVOID-FREQ event")
  161. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  162. "AP-CSA-FINISHED"], timeout=1)
  163. if ev is not None:
  164. raise Exception("Unexpected + " + ev + " event")
  165. if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES " + str(freq)):
  166. raise Exception("Could not simulate driver event(3)")
  167. ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
  168. if ev is None:
  169. raise Exception("No CTRL-EVENT-AVOID-FREQ event")
  170. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  171. "AP-CSA-FINISHED"],
  172. timeout=10)
  173. if ev is None:
  174. raise Exception("No P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED event")
  175. finally:
  176. set_country("00")
  177. dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES")
  178. dev[1].flush_scan_cache()
  179. def test_autogo_following_bss(dev, apdev):
  180. """P2P autonomous GO operate on the same channel as station interface"""
  181. if dev[0].get_mcc() > 1:
  182. logger.info("test mode: MCC")
  183. dev[0].request("SET p2p_no_group_iface 0")
  184. channels = { 3 : "2422", 5 : "2432", 9 : "2452" }
  185. for key in channels:
  186. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
  187. "channel" : str(key) })
  188. dev[0].connect("ap-test", key_mgmt="NONE",
  189. scan_freq=str(channels[key]))
  190. res_go = autogo(dev[0])
  191. if res_go['freq'] != channels[key]:
  192. raise Exception("Group operation channel is not the same as on connected station interface")
  193. hwsim_utils.test_connectivity(dev[0], hapd)
  194. dev[0].remove_group(res_go['ifname'])
  195. def test_go_neg_with_bss_connected(dev, apdev):
  196. """P2P channel selection: GO negotiation when station interface is connected"""
  197. dev[0].flush_scan_cache()
  198. dev[1].flush_scan_cache()
  199. dev[0].request("SET p2p_no_group_iface 0")
  200. hapd = hostapd.add_ap(apdev[0]['ifname'],
  201. { "ssid": 'bss-2.4ghz', "channel": '5' })
  202. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2432")
  203. #dev[0] as GO
  204. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[1],
  205. r_intent=1)
  206. check_grpform_results(i_res, r_res)
  207. if i_res['role'] != "GO":
  208. raise Exception("GO not selected according to go_intent")
  209. if i_res['freq'] != "2432":
  210. raise Exception("Group formed on a different frequency than BSS")
  211. hwsim_utils.test_connectivity(dev[0], hapd)
  212. dev[0].remove_group(i_res['ifname'])
  213. dev[1].wait_go_ending_session()
  214. if dev[0].get_mcc() > 1:
  215. logger.info("Skip as-client case due to MCC being enabled")
  216. return;
  217. #dev[0] as client
  218. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
  219. r_intent=10)
  220. check_grpform_results(i_res2, r_res2)
  221. if i_res2['role'] != "client":
  222. raise Exception("GO not selected according to go_intent")
  223. if i_res2['freq'] != "2432":
  224. raise Exception("Group formed on a different frequency than BSS")
  225. hwsim_utils.test_connectivity(dev[0], hapd)
  226. dev[1].remove_group(r_res2['ifname'])
  227. dev[0].wait_go_ending_session()
  228. dev[0].request("DISCONNECT")
  229. hapd.disable()
  230. dev[0].flush_scan_cache()
  231. dev[1].flush_scan_cache()
  232. def test_autogo_with_bss_on_disallowed_chan(dev, apdev):
  233. """P2P channel selection: Autonomous GO with BSS on a disallowed channel"""
  234. with HWSimRadio(n_channels=2) as (radio, iface):
  235. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  236. wpas.interface_add(iface)
  237. wpas.request("SET p2p_no_group_iface 0")
  238. if wpas.get_mcc() < 2:
  239. raise Exception("New radio does not support MCC")
  240. try:
  241. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  242. "channel": '1' })
  243. wpas.request("P2P_SET disallow_freq 2412")
  244. wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  245. res = autogo(wpas)
  246. if res['freq'] == "2412":
  247. raise Exception("GO set on a disallowed channel")
  248. hwsim_utils.test_connectivity(wpas, hapd)
  249. finally:
  250. wpas.request("P2P_SET disallow_freq ")
  251. def test_go_neg_with_bss_on_disallowed_chan(dev, apdev):
  252. """P2P channel selection: GO negotiation with station interface on a disallowed channel"""
  253. with HWSimRadio(n_channels=2) as (radio, iface):
  254. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  255. wpas.interface_add(iface)
  256. wpas.request("SET p2p_no_group_iface 0")
  257. if wpas.get_mcc() < 2:
  258. raise Exception("New radio does not support MCC")
  259. try:
  260. hapd = hostapd.add_ap(apdev[0]['ifname'],
  261. { "ssid": 'bss-2.4ghz', "channel": '1' })
  262. # make sure PBC overlap from old test cases is not maintained
  263. dev[1].flush_scan_cache()
  264. wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  265. wpas.request("P2P_SET disallow_freq 2412")
  266. #wpas as GO
  267. [i_res, r_res] = go_neg_pbc(i_dev=wpas, i_intent=10, r_dev=dev[1],
  268. r_intent=1)
  269. check_grpform_results(i_res, r_res)
  270. if i_res['role'] != "GO":
  271. raise Exception("GO not selected according to go_intent")
  272. if i_res['freq'] == "2412":
  273. raise Exception("Group formed on a disallowed channel")
  274. hwsim_utils.test_connectivity(wpas, hapd)
  275. wpas.remove_group(i_res['ifname'])
  276. dev[1].wait_go_ending_session()
  277. dev[1].flush_scan_cache()
  278. wpas.dump_monitor()
  279. dev[1].dump_monitor()
  280. #wpas as client
  281. [i_res2, r_res2] = go_neg_pbc(i_dev=wpas, i_intent=1, r_dev=dev[1],
  282. r_intent=10)
  283. check_grpform_results(i_res2, r_res2)
  284. if i_res2['role'] != "client":
  285. raise Exception("GO not selected according to go_intent")
  286. if i_res2['freq'] == "2412":
  287. raise Exception("Group formed on a disallowed channel")
  288. hwsim_utils.test_connectivity(wpas, hapd)
  289. dev[1].remove_group(r_res2['ifname'])
  290. wpas.wait_go_ending_session()
  291. ev = dev[1].wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
  292. if ev is None:
  293. raise Exception("Group removal not indicated")
  294. wpas.request("DISCONNECT")
  295. hapd.disable()
  296. finally:
  297. wpas.request("P2P_SET disallow_freq ")
  298. def test_autogo_force_diff_channel(dev, apdev):
  299. """P2P autonomous GO and station interface operate on different channels"""
  300. with HWSimRadio(n_channels=2) as (radio, iface):
  301. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  302. wpas.interface_add(iface)
  303. if wpas.get_mcc() < 2:
  304. raise Exception("New radio does not support MCC")
  305. wpas.request("SET p2p_no_group_iface 0")
  306. hapd = hostapd.add_ap(apdev[0]['ifname'],
  307. {"ssid" : 'ap-test', "channel" : '1'})
  308. wpas.connect("ap-test", key_mgmt = "NONE", scan_freq = "2412")
  309. wpas.dump_monitor()
  310. channels = { 2 : 2417, 5 : 2432, 9 : 2452 }
  311. for key in channels:
  312. res_go = autogo(wpas, channels[key])
  313. wpas.dump_monitor()
  314. hwsim_utils.test_connectivity(wpas, hapd)
  315. if int(res_go['freq']) == 2412:
  316. raise Exception("Group operation channel is: 2412 excepted: " + res_go['freq'])
  317. wpas.remove_group(res_go['ifname'])
  318. wpas.dump_monitor()
  319. def test_go_neg_forced_freq_diff_than_bss_freq(dev, apdev):
  320. """P2P channel selection: GO negotiation with forced freq different than station interface"""
  321. with HWSimRadio(n_channels=2) as (radio, iface):
  322. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  323. wpas.interface_add(iface)
  324. if wpas.get_mcc() < 2:
  325. raise Exception("New radio does not support MCC")
  326. # Clear possible PBC session overlap from previous test case
  327. dev[1].flush_scan_cache()
  328. wpas.request("SET p2p_no_group_iface 0")
  329. hapd = hostapd.add_ap(apdev[0]['ifname'],
  330. { "country_code": 'US',
  331. "ssid": 'bss-5ghz', "hw_mode": 'a',
  332. "channel": '40' })
  333. wpas.connect("bss-5ghz", key_mgmt="NONE", scan_freq="5200")
  334. # GO and peer force the same freq, different than BSS freq,
  335. # wpas to become GO
  336. [i_res, r_res] = go_neg_pbc(i_dev=dev[1], i_intent=1, i_freq=5180,
  337. r_dev=wpas, r_intent=14, r_freq=5180)
  338. check_grpform_results(i_res, r_res)
  339. if i_res['freq'] != "5180":
  340. raise Exception("P2P group formed on unexpected frequency: " + i_res['freq'])
  341. if r_res['role'] != "GO":
  342. raise Exception("GO not selected according to go_intent")
  343. hwsim_utils.test_connectivity(wpas, hapd)
  344. wpas.remove_group(r_res['ifname'])
  345. dev[1].wait_go_ending_session()
  346. dev[1].flush_scan_cache()
  347. # GO and peer force the same freq, different than BSS freq, wpas to
  348. # become client
  349. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[1], i_intent=14, i_freq=2422,
  350. r_dev=wpas, r_intent=1, r_freq=2422)
  351. check_grpform_results(i_res2, r_res2)
  352. if i_res2['freq'] != "2422":
  353. raise Exception("P2P group formed on unexpected frequency: " + i_res2['freq'])
  354. if r_res2['role'] != "client":
  355. raise Exception("GO not selected according to go_intent")
  356. hwsim_utils.test_connectivity(wpas, hapd)
  357. wpas.request("DISCONNECT")
  358. hapd.request("DISABLE")
  359. subprocess.call(['iw', 'reg', 'set', '00'])
  360. wpas.flush_scan_cache()
  361. def test_go_pref_chan_bss_on_diff_chan(dev, apdev):
  362. """P2P channel selection: Station on different channel than GO configured pref channel"""
  363. dev[0].request("SET p2p_no_group_iface 0")
  364. try:
  365. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  366. "channel": '1' })
  367. dev[0].request("SET p2p_pref_chan 81:2")
  368. dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  369. res = autogo(dev[0])
  370. if res['freq'] != "2412":
  371. raise Exception("GO channel did not follow BSS")
  372. hwsim_utils.test_connectivity(dev[0], hapd)
  373. finally:
  374. dev[0].request("SET p2p_pref_chan ")
  375. def test_go_pref_chan_bss_on_disallowed_chan(dev, apdev):
  376. """P2P channel selection: Station interface on different channel than GO configured pref channel, and station channel is disallowed"""
  377. with HWSimRadio(n_channels=2) as (radio, iface):
  378. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  379. wpas.interface_add(iface)
  380. if wpas.get_mcc() < 2:
  381. raise Exception("New radio does not support MCC")
  382. wpas.request("SET p2p_no_group_iface 0")
  383. try:
  384. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  385. "channel": '1' })
  386. wpas.request("P2P_SET disallow_freq 2412")
  387. wpas.request("SET p2p_pref_chan 81:2")
  388. wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  389. res2 = autogo(wpas)
  390. if res2['freq'] != "2417":
  391. raise Exception("GO channel did not follow pref_chan configuration")
  392. hwsim_utils.test_connectivity(wpas, hapd)
  393. finally:
  394. wpas.request("P2P_SET disallow_freq ")
  395. wpas.request("SET p2p_pref_chan ")
  396. def test_no_go_freq(dev, apdev):
  397. """P2P channel selection: no GO freq"""
  398. try:
  399. dev[0].request("SET p2p_no_go_freq 2412")
  400. # dev[0] as client, channel 1 is ok
  401. [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=1,
  402. r_dev=dev[1], r_intent=14, r_freq=2412)
  403. check_grpform_results(i_res, r_res)
  404. if i_res['freq'] != "2412":
  405. raise Exception("P2P group not formed on forced freq")
  406. dev[1].remove_group(r_res['ifname'])
  407. dev[0].wait_go_ending_session()
  408. dev[0].flush_scan_cache()
  409. fail = False
  410. # dev[0] as GO, channel 1 is not allowed
  411. try:
  412. dev[0].request("SET p2p_no_go_freq 2412")
  413. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14,
  414. r_dev=dev[1], r_intent=1, r_freq=2412)
  415. check_grpform_results(i_res2, r_res2)
  416. fail = True
  417. except:
  418. pass
  419. if fail:
  420. raise Exception("GO set on a disallowed freq")
  421. finally:
  422. dev[0].request("SET p2p_no_go_freq ")
  423. def test_go_neg_peers_force_diff_freq(dev, apdev):
  424. """P2P channel selection when peers for different frequency"""
  425. try:
  426. [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14, i_freq=5180,
  427. r_dev=dev[1], r_intent=0, r_freq=5200)
  428. except Exception, e:
  429. return
  430. raise Exception("Unexpected group formation success")
  431. def test_autogo_random_channel(dev, apdev):
  432. """P2P channel selection: GO instantiated on random channel 1, 6, 11"""
  433. freqs = []
  434. go_freqs = ["2412", "2437", "2462"]
  435. for i in range(0, 20):
  436. result = autogo(dev[0])
  437. if result['freq'] not in go_freqs:
  438. raise Exception("Unexpected frequency selected: " + result['freq'])
  439. if result['freq'] not in freqs:
  440. freqs.append(result['freq'])
  441. if len(freqs) == 3:
  442. break
  443. dev[0].remove_group(result['ifname'])
  444. if i == 20:
  445. raise Exception("GO created 20 times and not all social channels were selected. freqs not selected: " + str(list(set(go_freqs) - set(freqs))))
  446. def test_p2p_autogo_pref_chan_disallowed(dev, apdev):
  447. """P2P channel selection: GO preferred channels are disallowed"""
  448. try:
  449. dev[0].request("SET p2p_pref_chan 81:1,81:3,81:6,81:9,81:11")
  450. dev[0].request("P2P_SET disallow_freq 2412,2422,2437,2452,2462")
  451. for i in range(0, 5):
  452. res = autogo(dev[0])
  453. if res['freq'] in [ "2412", "2422", "2437", "2452", "2462" ]:
  454. raise Exception("GO channel is disallowed")
  455. dev[0].remove_group(res['ifname'])
  456. finally:
  457. dev[0].request("P2P_SET disallow_freq ")
  458. dev[0].request("SET p2p_pref_chan ")
  459. def test_p2p_autogo_pref_chan_not_in_regulatory(dev, apdev):
  460. """P2P channel selection: GO preferred channel not allowed in the regulatory rules"""
  461. try:
  462. set_country("US", dev[0])
  463. dev[0].request("SET p2p_pref_chan 124:149")
  464. res = autogo(dev[0], persistent=True)
  465. if res['freq'] != "5745":
  466. raise Exception("Unexpected channel selected: " + res['freq'])
  467. dev[0].remove_group(res['ifname'])
  468. netw = dev[0].list_networks(p2p=True)
  469. if len(netw) != 1:
  470. raise Exception("Unexpected number of network blocks: " + str(netw))
  471. id = netw[0]['id']
  472. set_country("DE", dev[0])
  473. res = autogo(dev[0], persistent=id)
  474. if res['freq'] == "5745":
  475. raise Exception("Unexpected channel selected(2): " + res['freq'])
  476. dev[0].remove_group(res['ifname'])
  477. finally:
  478. dev[0].request("SET p2p_pref_chan ")
  479. set_country("00")
  480. def run_autogo(dev, param):
  481. if "OK" not in dev.global_request("P2P_GROUP_ADD " + param):
  482. raise Exception("P2P_GROUP_ADD failed: " + param)
  483. ev = dev.wait_global_event(["P2P-GROUP-STARTED"], timeout=10)
  484. if ev is None:
  485. raise Exception("GO start up timed out")
  486. res = dev.group_form_result(ev)
  487. dev.remove_group()
  488. return res
  489. def _test_autogo_ht_vht(dev):
  490. res = run_autogo(dev[0], "ht40")
  491. res = run_autogo(dev[0], "vht")
  492. res = run_autogo(dev[0], "freq=2")
  493. freq = int(res['freq'])
  494. if freq < 2412 or freq > 2462:
  495. raise Exception("Unexpected freq=2 channel: " + str(freq))
  496. res = run_autogo(dev[0], "freq=5")
  497. freq = int(res['freq'])
  498. if freq < 5000 or freq >= 6000:
  499. raise Exception("Unexpected freq=5 channel: " + str(freq))
  500. res = run_autogo(dev[0], "freq=5 ht40 vht")
  501. logger.info(str(res))
  502. freq = int(res['freq'])
  503. if freq < 5000 or freq >= 6000:
  504. raise Exception("Unexpected freq=5 ht40 vht channel: " + str(freq))
  505. def test_autogo_ht_vht(dev):
  506. """P2P autonomous GO with HT/VHT parameters"""
  507. try:
  508. set_country("US", dev[0])
  509. _test_autogo_ht_vht(dev)
  510. finally:
  511. set_country("00")
  512. def test_p2p_listen_chan_optimize(dev, apdev):
  513. """P2P listen channel optimization"""
  514. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  515. wpas.interface_add("wlan5")
  516. addr5 = wpas.p2p_dev_addr()
  517. try:
  518. if "OK" not in wpas.request("SET p2p_optimize_listen_chan 1"):
  519. raise Exception("Failed to set p2p_optimize_listen_chan")
  520. wpas.p2p_listen()
  521. if not dev[0].discover_peer(addr5):
  522. raise Exception("Could not discover peer")
  523. peer = dev[0].get_peer(addr5)
  524. lfreq = peer['listen_freq']
  525. wpas.p2p_stop_find()
  526. dev[0].p2p_stop_find()
  527. channel = "1" if lfreq != '2412' else "6"
  528. freq = "2412" if lfreq != '2412' else "2437"
  529. params = { "ssid": "test-open", "channel": channel }
  530. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  531. id = wpas.connect("test-open", key_mgmt="NONE", scan_freq=freq)
  532. wpas.p2p_listen()
  533. if "OK" not in dev[0].request("P2P_FLUSH"):
  534. raise Exception("P2P_FLUSH failed")
  535. if not dev[0].discover_peer(addr5):
  536. raise Exception("Could not discover peer")
  537. peer = dev[0].get_peer(addr5)
  538. lfreq2 = peer['listen_freq']
  539. if lfreq == lfreq2:
  540. raise Exception("Listen channel did not change")
  541. if lfreq2 != freq:
  542. raise Exception("Listen channel not on AP's operating channel")
  543. wpas.p2p_stop_find()
  544. dev[0].p2p_stop_find()
  545. wpas.request("DISCONNECT")
  546. wpas.wait_disconnected()
  547. # for larger coverage, cover case of current channel matching
  548. wpas.select_network(id)
  549. wpas.wait_connected()
  550. wpas.request("DISCONNECT")
  551. wpas.wait_disconnected()
  552. lchannel = "1" if channel != "1" else "6"
  553. lfreq3 = "2412" if channel != "1" else "2437"
  554. if "OK" not in wpas.request("P2P_SET listen_channel " + lchannel):
  555. raise Exception("Failed to set listen channel")
  556. wpas.select_network(id)
  557. wpas.wait_connected()
  558. wpas.p2p_listen()
  559. if "OK" not in dev[0].request("P2P_FLUSH"):
  560. raise Exception("P2P_FLUSH failed")
  561. if not dev[0].discover_peer(addr5):
  562. raise Exception("Could not discover peer")
  563. peer = dev[0].get_peer(addr5)
  564. lfreq4 = peer['listen_freq']
  565. if lfreq4 != lfreq3:
  566. raise Exception("Unexpected Listen channel after configuration")
  567. wpas.p2p_stop_find()
  568. dev[0].p2p_stop_find()
  569. finally:
  570. wpas.request("SET p2p_optimize_listen_chan 0")
  571. def test_p2p_channel_5ghz_only(dev):
  572. """P2P GO start with only 5 GHz band allowed"""
  573. try:
  574. set_country("US", dev[0])
  575. dev[0].request("P2P_SET disallow_freq 2400-2500")
  576. res = autogo(dev[0])
  577. freq = int(res['freq'])
  578. if freq < 5000:
  579. raise Exception("Unexpected channel %d MHz" % freq)
  580. dev[0].remove_group()
  581. finally:
  582. set_country("00")
  583. dev[0].request("P2P_SET disallow_freq ")
  584. def test_p2p_channel_5ghz_165_169_us(dev):
  585. """P2P GO and 5 GHz channels 165 (allowed) and 169 (disallowed) in US"""
  586. try:
  587. set_country("US", dev[0])
  588. res = dev[0].p2p_start_go(freq=5825)
  589. if res['freq'] != "5825":
  590. raise Exception("Unexpected frequency: " + res['freq'])
  591. dev[0].remove_group()
  592. res = dev[0].global_request("P2P_GROUP_ADD freq=5845")
  593. if "FAIL" not in res:
  594. raise Exception("GO on channel 169 allowed unexpectedly")
  595. finally:
  596. set_country("00")
  597. def test_p2p_go_move_reg_change(dev, apdev, params):
  598. """P2P GO move due to regulatory change [long]"""
  599. if not params['long']:
  600. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  601. try:
  602. set_country("US")
  603. dev[0].global_request("P2P_SET disallow_freq 2400-5000")
  604. res = autogo(dev[0])
  605. freq1 = int(res['freq'])
  606. if freq1 < 5000:
  607. raise Exception("Unexpected channel %d MHz" % freq1)
  608. dev[0].global_request("P2P_SET disallow_freq ")
  609. # GO move is not allowed while waiting for initial client connection
  610. time.sleep(20)
  611. set_country("00")
  612. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  613. "AP-CSA-FINISHED"],
  614. timeout=10)
  615. if ev is None:
  616. raise Exception("P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED not seen")
  617. freq2 = dev[0].get_group_status_field('freq')
  618. if freq1 == freq2:
  619. raise Exception("Unexpected freq after group reform=" + freq2)
  620. dev[0].remove_group()
  621. finally:
  622. dev[0].global_request("P2P_SET disallow_freq ")
  623. set_country("00")
  624. def test_p2p_go_move_active(dev, apdev, params):
  625. """P2P GO stays in freq although SCM is possible [long]"""
  626. if dev[0].get_mcc() <= 1:
  627. raise HwsimSkip("Skip due to MCC not being enabled")
  628. if not params['long']:
  629. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  630. dev[0].request("SET p2p_no_group_iface 0")
  631. try:
  632. dev[0].global_request("P2P_SET disallow_freq 2430-6000")
  633. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
  634. "channel" : '11' })
  635. dev[0].connect("ap-test", key_mgmt="NONE",
  636. scan_freq="2462")
  637. res = autogo(dev[0])
  638. freq = int(res['freq'])
  639. if freq > 2430:
  640. raise Exception("Unexpected channel %d MHz" % freq)
  641. # GO move is not allowed while waiting for initial client connection
  642. time.sleep(20)
  643. dev[0].global_request("P2P_SET disallow_freq ")
  644. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  645. "AP-CSA-FINISHED"],
  646. timeout=10)
  647. if ev is not None:
  648. raise Exception("Unexpected P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED seen")
  649. dev[0].remove_group()
  650. finally:
  651. dev[0].global_request("P2P_SET disallow_freq ")
  652. def test_p2p_go_move_scm(dev, apdev, params):
  653. """P2P GO move due to SCM operation preference [long]"""
  654. if dev[0].get_mcc() <= 1:
  655. raise HwsimSkip("Skip due to MCC not being enabled")
  656. if not params['long']:
  657. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  658. dev[0].request("SET p2p_no_group_iface 0")
  659. try:
  660. dev[0].global_request("P2P_SET disallow_freq 2430-6000")
  661. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
  662. "channel" : '11' })
  663. dev[0].connect("ap-test", key_mgmt="NONE",
  664. scan_freq="2462")
  665. dev[0].global_request("SET p2p_go_freq_change_policy 0")
  666. res = autogo(dev[0])
  667. freq = int(res['freq'])
  668. if freq > 2430:
  669. raise Exception("Unexpected channel %d MHz" % freq)
  670. # GO move is not allowed while waiting for initial client connection
  671. time.sleep(20)
  672. dev[0].global_request("P2P_SET disallow_freq ")
  673. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  674. "AP-CSA-FINISHED"], timeout=3)
  675. if ev is None:
  676. raise Exception("P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED not seen")
  677. freq = dev[0].get_group_status_field('freq')
  678. if freq != '2462':
  679. raise Exception("Unexpected freq after group reform=" + freq)
  680. dev[0].remove_group()
  681. finally:
  682. dev[0].global_request("P2P_SET disallow_freq ")
  683. dev[0].global_request("SET p2p_go_freq_change_policy 2")
  684. def test_p2p_go_move_scm_peer_supports(dev, apdev, params):
  685. """P2P GO move due to SCM operation preference (peer supports) [long]"""
  686. if dev[0].get_mcc() <= 1:
  687. raise HwsimSkip("Skip due to MCC not being enabled")
  688. if not params['long']:
  689. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  690. try:
  691. dev[0].global_request("SET p2p_go_freq_change_policy 1")
  692. set_country("US", dev[0])
  693. dev[0].request("SET p2p_no_group_iface 0")
  694. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  695. r_dev=dev[1], r_intent=0,
  696. test_data=False)
  697. check_grpform_results(i_res, r_res)
  698. freq = int(i_res['freq'])
  699. if freq < 5000:
  700. raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
  701. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
  702. "channel" : '11' })
  703. logger.info('Connecting client to to an AP on channel 11');
  704. dev[0].connect("ap-test", key_mgmt="NONE",
  705. scan_freq="2462")
  706. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  707. "AP-CSA-FINISHED"], timeout=3)
  708. if ev is None:
  709. raise Exception("P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED not seen")
  710. freq = dev[0].get_group_status_field('freq')
  711. if freq != '2462':
  712. raise Exception("Unexpected freq after group reform=" + freq)
  713. dev[0].remove_group()
  714. finally:
  715. dev[0].global_request("SET p2p_go_freq_change_policy 2")
  716. set_country("00")
  717. def test_p2p_go_move_scm_peer_does_not_support(dev, apdev, params):
  718. """No P2P GO move due to SCM operation (peer does not supports) [long]"""
  719. if dev[0].get_mcc() <= 1:
  720. raise HwsimSkip("Skip due to MCC not being enabled")
  721. if not params['long']:
  722. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  723. try:
  724. dev[0].global_request("SET p2p_go_freq_change_policy 1")
  725. set_country("US", dev[0])
  726. dev[0].request("SET p2p_no_group_iface 0")
  727. if "OK" not in dev[1].request("DRIVER_EVENT AVOID_FREQUENCIES 2400-2500"):
  728. raise Exception("Could not simulate driver event")
  729. [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
  730. r_dev=dev[1], r_intent=0,
  731. test_data=False)
  732. check_grpform_results(i_res, r_res)
  733. freq = int(i_res['freq'])
  734. if freq < 5000:
  735. raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
  736. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
  737. "channel" : '11' })
  738. logger.info('Connecting client to to an AP on channel 11');
  739. dev[0].connect("ap-test", key_mgmt="NONE",
  740. scan_freq="2462")
  741. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  742. "AP-CSA-FINISHED"],
  743. timeout=10)
  744. if ev is not None:
  745. raise Exception("Unexpected P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED seen")
  746. dev[0].remove_group()
  747. finally:
  748. dev[0].global_request("SET p2p_go_freq_change_policy 2")
  749. set_country("00")
  750. def test_p2p_go_move_scm_multi(dev, apdev, params):
  751. """P2P GO move due to SCM operation preference multiple times [long]"""
  752. if dev[0].get_mcc() <= 1:
  753. raise HwsimSkip("Skip due to MCC not being enabled")
  754. if not params['long']:
  755. raise HwsimSkip("Skip test case with long duration due to --long not specified")
  756. dev[0].request("SET p2p_no_group_iface 0")
  757. try:
  758. dev[0].global_request("P2P_SET disallow_freq 2430-6000")
  759. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test-1',
  760. "channel" : '11' })
  761. dev[0].connect("ap-test-1", key_mgmt="NONE",
  762. scan_freq="2462")
  763. dev[0].global_request("SET p2p_go_freq_change_policy 0")
  764. res = autogo(dev[0])
  765. freq = int(res['freq'])
  766. if freq > 2430:
  767. raise Exception("Unexpected channel %d MHz" % freq)
  768. # GO move is not allowed while waiting for initial client connection
  769. time.sleep(20)
  770. dev[0].global_request("P2P_SET disallow_freq ")
  771. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  772. "AP-CSA-FINISHED"], timeout=3)
  773. if ev is None:
  774. raise Exception("P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED not seen")
  775. freq = dev[0].get_group_status_field('freq')
  776. if freq != '2462':
  777. raise Exception("Unexpected freq after group reform=" + freq)
  778. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test-2',
  779. "channel" : '6' })
  780. dev[0].connect("ap-test-2", key_mgmt="NONE",
  781. scan_freq="2437")
  782. ev = dev[0].wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  783. "AP-CSA-FINISHED"], timeout=5)
  784. if ev is None:
  785. raise Exception("(2) P2P-REMOVE-AND-REFORM-GROUP or AP-CSA-FINISHED not seen")
  786. freq = dev[0].get_group_status_field('freq')
  787. if freq != '2437':
  788. raise Exception("(2) Unexpected freq after group reform=" + freq)
  789. dev[0].remove_group()
  790. finally:
  791. dev[0].global_request("P2P_SET disallow_freq ")
  792. dev[0].global_request("SET p2p_go_freq_change_policy 2")
  793. def test_p2p_delay_go_csa(dev, apdev, params):
  794. """P2P GO CSA delayed when inviting a P2P Device to an active P2P Group"""
  795. with HWSimRadio(n_channels=2) as (radio, iface):
  796. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  797. wpas.interface_add(iface)
  798. wpas.global_request("SET p2p_no_group_iface 0")
  799. if wpas.get_mcc() < 2:
  800. raise Exception("New radio does not support MCC")
  801. addr0 = wpas.p2p_dev_addr()
  802. addr1 = dev[1].p2p_dev_addr()
  803. try:
  804. dev[1].p2p_listen();
  805. if not wpas.discover_peer(addr1, social=True):
  806. raise Exception("Peer " + addr1 + " not found")
  807. wpas.p2p_stop_find()
  808. hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
  809. "channel": '1' })
  810. wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
  811. wpas.global_request("SET p2p_go_freq_change_policy 0")
  812. wpas.dump_monitor()
  813. logger.info("Start GO on channel 6")
  814. res = autogo(wpas, freq=2437)
  815. if res['freq'] != "2437":
  816. raise Exception("GO set on a freq=%s instead of 2437" % res['freq'])
  817. # Start find on dev[1] to run scans with dev[2] in parallel
  818. dev[1].p2p_find(social=True)
  819. # Use another client device to stop the initial client connection
  820. # timeout on the GO
  821. if not dev[2].discover_peer(addr0, social=True):
  822. raise Exception("Peer2 did not find the GO")
  823. dev[2].p2p_stop_find()
  824. pin = dev[2].wps_read_pin()
  825. wpas.p2p_go_authorize_client(pin)
  826. dev[2].global_request("P2P_CONNECT " + addr0 + " " + pin + " join freq=2437")
  827. ev = dev[2].wait_global_event(["P2P-GROUP-STARTED"], timeout=10)
  828. if ev is None:
  829. raise Exception("Peer2 did not get connected")
  830. if not dev[1].discover_peer(addr0, social=True):
  831. raise Exception("Peer did not find the GO")
  832. pin = dev[1].wps_read_pin()
  833. dev[1].global_request("P2P_CONNECT " + addr0 + " " + pin + " join auth")
  834. dev[1].p2p_listen();
  835. # Force P2P GO channel switch on successful invitation signaling
  836. wpas.group_request("SET p2p_go_csa_on_inv 1")
  837. logger.info("Starting invitation")
  838. wpas.p2p_go_authorize_client(pin)
  839. wpas.global_request("P2P_INVITE group=" + wpas.group_ifname + " peer=" + addr1)
  840. ev = dev[1].wait_global_event(["P2P-INVITATION-RECEIVED",
  841. "P2P-GROUP-STARTED"], timeout=10)
  842. if ev is None:
  843. raise Exception("Timeout on invitation on peer")
  844. if "P2P-INVITATION-RECEIVED" in ev:
  845. raise Exception("Unexpected request to accept pre-authorized invitation")
  846. # A P2P GO move is not expected at this stage, as during the
  847. # invitation signaling, the P2P GO includes only its current
  848. # operating channel in the channel list, and as the invitation
  849. # response can only include channels that were also in the
  850. # invitation request channel list, the group common channels
  851. # includes only the current P2P GO operating channel.
  852. ev = wpas.wait_group_event(["P2P-REMOVE-AND-REFORM-GROUP",
  853. "AP-CSA-FINISHED"], timeout=1)
  854. if ev is not None:
  855. raise Exception("Unexpected + " + ev + " event")
  856. finally:
  857. wpas.global_request("SET p2p_go_freq_change_policy 2")