test_ap_wps.py 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. # WPS tests
  2. # Copyright (c) 2013-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 os
  7. import time
  8. import subprocess
  9. import logging
  10. logger = logging.getLogger()
  11. import re
  12. import socket
  13. import httplib
  14. import urlparse
  15. import urllib
  16. import xml.etree.ElementTree as ET
  17. import StringIO
  18. import hwsim_utils
  19. import hostapd
  20. from wpasupplicant import WpaSupplicant
  21. def test_ap_wps_init(dev, apdev):
  22. """Initial AP configuration with first WPS Enrollee"""
  23. ssid = "test-wps"
  24. hostapd.add_ap(apdev[0]['ifname'],
  25. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  26. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  27. logger.info("WPS provisioning step")
  28. hapd.request("WPS_PBC")
  29. if "PBC Status: Active" not in hapd.request("WPS_GET_STATUS"):
  30. raise Exception("PBC status not shown correctly")
  31. id = dev[0].add_network()
  32. dev[0].set_network_quoted(id, "ssid", "home")
  33. dev[0].set_network_quoted(id, "psk", "12345678")
  34. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  35. id = dev[0].add_network()
  36. dev[0].set_network_quoted(id, "ssid", "home2")
  37. dev[0].set_network(id, "bssid", "00:11:22:33:44:55")
  38. dev[0].set_network(id, "key_mgmt", "NONE")
  39. dev[0].request("ENABLE_NETWORK %s no-connect" % id)
  40. dev[0].request("WPS_PBC")
  41. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  42. if ev is None:
  43. raise Exception("Association with the AP timed out")
  44. status = dev[0].get_status()
  45. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  46. raise Exception("Not fully connected")
  47. if status['ssid'] != ssid:
  48. raise Exception("Unexpected SSID")
  49. if status['pairwise_cipher'] != 'CCMP':
  50. raise Exception("Unexpected encryption configuration")
  51. if status['key_mgmt'] != 'WPA2-PSK':
  52. raise Exception("Unexpected key_mgmt")
  53. status = hapd.request("WPS_GET_STATUS")
  54. if "PBC Status: Disabled" not in status:
  55. raise Exception("PBC status not shown correctly")
  56. if "Last WPS result: Success" not in status:
  57. raise Exception("Last WPS result not shown correctly")
  58. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  59. raise Exception("Peer address not shown correctly")
  60. conf = hapd.request("GET_CONFIG")
  61. if "wps_state=configured" not in conf:
  62. raise Exception("AP not in WPS configured state")
  63. if "rsn_pairwise_cipher=CCMP TKIP" not in conf:
  64. raise Exception("Unexpected rsn_pairwise_cipher")
  65. if "wpa_pairwise_cipher=CCMP TKIP" not in conf:
  66. raise Exception("Unexpected wpa_pairwise_cipher")
  67. if "group_cipher=TKIP" not in conf:
  68. raise Exception("Unexpected group_cipher")
  69. if len(dev[0].list_networks()) != 3:
  70. raise Exception("Unexpected number of network blocks")
  71. def test_ap_wps_init_2ap_pbc(dev, apdev):
  72. """Initial two-radio AP configuration with first WPS PBC Enrollee"""
  73. ssid = "test-wps"
  74. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  75. hostapd.add_ap(apdev[0]['ifname'], params)
  76. hostapd.add_ap(apdev[1]['ifname'], params)
  77. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  78. logger.info("WPS provisioning step")
  79. hapd.request("WPS_PBC")
  80. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  81. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  82. bss = dev[0].get_bss(apdev[0]['bssid'])
  83. if "[WPS-PBC]" not in bss['flags']:
  84. raise Exception("WPS-PBC flag missing from AP1")
  85. bss = dev[0].get_bss(apdev[1]['bssid'])
  86. if "[WPS-PBC]" not in bss['flags']:
  87. raise Exception("WPS-PBC flag missing from AP2")
  88. dev[0].dump_monitor()
  89. dev[0].request("SET wps_cred_processing 2")
  90. dev[0].request("WPS_PBC")
  91. ev = dev[0].wait_event(["WPS-CRED-RECEIVED"], timeout=30)
  92. dev[0].request("SET wps_cred_processing 0")
  93. if ev is None:
  94. raise Exception("WPS cred event not seen")
  95. if "100e" not in ev:
  96. raise Exception("WPS attributes not included in the cred event")
  97. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  98. if ev is None:
  99. raise Exception("Association with the AP timed out")
  100. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  101. dev[1].scan_for_bss(apdev[1]['bssid'], freq="2412")
  102. bss = dev[1].get_bss(apdev[0]['bssid'])
  103. if "[WPS-PBC]" in bss['flags']:
  104. raise Exception("WPS-PBC flag not cleared from AP1")
  105. bss = dev[1].get_bss(apdev[1]['bssid'])
  106. if "[WPS-PBC]" in bss['flags']:
  107. raise Exception("WPS-PBC flag not cleared from AP2")
  108. def test_ap_wps_init_2ap_pin(dev, apdev):
  109. """Initial two-radio AP configuration with first WPS PIN Enrollee"""
  110. ssid = "test-wps"
  111. params = { "ssid": ssid, "eap_server": "1", "wps_state": "1" }
  112. hostapd.add_ap(apdev[0]['ifname'], params)
  113. hostapd.add_ap(apdev[1]['ifname'], params)
  114. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  115. logger.info("WPS provisioning step")
  116. pin = dev[0].wps_read_pin()
  117. hapd.request("WPS_PIN any " + pin)
  118. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  119. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  120. bss = dev[0].get_bss(apdev[0]['bssid'])
  121. if "[WPS-AUTH]" not in bss['flags']:
  122. raise Exception("WPS-AUTH flag missing from AP1")
  123. bss = dev[0].get_bss(apdev[1]['bssid'])
  124. if "[WPS-AUTH]" not in bss['flags']:
  125. raise Exception("WPS-AUTH flag missing from AP2")
  126. dev[0].dump_monitor()
  127. dev[0].request("WPS_PIN any " + pin)
  128. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  129. if ev is None:
  130. raise Exception("Association with the AP timed out")
  131. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  132. dev[1].scan_for_bss(apdev[1]['bssid'], freq="2412")
  133. bss = dev[1].get_bss(apdev[0]['bssid'])
  134. if "[WPS-AUTH]" in bss['flags']:
  135. raise Exception("WPS-AUTH flag not cleared from AP1")
  136. bss = dev[1].get_bss(apdev[1]['bssid'])
  137. if "[WPS-AUTH]" in bss['flags']:
  138. raise Exception("WPS-AUTH flag not cleared from AP2")
  139. def test_ap_wps_init_through_wps_config(dev, apdev):
  140. """Initial AP configuration using wps_config command"""
  141. ssid = "test-wps-init-config"
  142. hostapd.add_ap(apdev[0]['ifname'],
  143. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  144. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  145. if "FAIL" in hapd.request("WPS_CONFIG " + ssid.encode("hex") + " WPA2PSK CCMP " + "12345678".encode("hex")):
  146. raise Exception("WPS_CONFIG command failed")
  147. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=5)
  148. if ev is None:
  149. raise Exception("Timeout on WPS-NEW-AP-SETTINGS events")
  150. # It takes some time for the AP to update Beacon and Probe Response frames,
  151. # so wait here before requesting the scan to be started to avoid adding
  152. # extra five second wait to the test due to fetching obsolete scan results.
  153. hapd.ping()
  154. time.sleep(0.2)
  155. dev[0].connect(ssid, psk="12345678", scan_freq="2412", proto="WPA2",
  156. pairwise="CCMP", group="CCMP")
  157. def test_ap_wps_conf(dev, apdev):
  158. """WPS PBC provisioning with configured AP"""
  159. ssid = "test-wps-conf"
  160. hostapd.add_ap(apdev[0]['ifname'],
  161. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  162. "wpa_passphrase": "12345678", "wpa": "2",
  163. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  164. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  165. logger.info("WPS provisioning step")
  166. hapd.request("WPS_PBC")
  167. dev[0].dump_monitor()
  168. dev[0].request("WPS_PBC")
  169. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  170. if ev is None:
  171. raise Exception("Association with the AP timed out")
  172. status = dev[0].get_status()
  173. if status['wpa_state'] != 'COMPLETED':
  174. raise Exception("Not fully connected")
  175. if status['bssid'] != apdev[0]['bssid']:
  176. raise Exception("Unexpected BSSID")
  177. if status['ssid'] != ssid:
  178. raise Exception("Unexpected SSID")
  179. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  180. raise Exception("Unexpected encryption configuration")
  181. if status['key_mgmt'] != 'WPA2-PSK':
  182. raise Exception("Unexpected key_mgmt")
  183. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  184. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  185. raise Exception("Device name not available in STA command")
  186. def test_ap_wps_conf_5ghz(dev, apdev):
  187. """WPS PBC provisioning with configured AP on 5 GHz band"""
  188. try:
  189. ssid = "test-wps-conf"
  190. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  191. "wpa_passphrase": "12345678", "wpa": "2",
  192. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  193. "country_code": "FI", "hw_mode": "a", "channel": "36" }
  194. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  195. logger.info("WPS provisioning step")
  196. hapd.request("WPS_PBC")
  197. dev[0].request("WPS_PBC")
  198. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  199. if ev is None:
  200. raise Exception("Association with the AP timed out")
  201. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  202. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  203. raise Exception("Device name not available in STA command")
  204. finally:
  205. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  206. def test_ap_wps_conf_chan14(dev, apdev):
  207. """WPS PBC provisioning with configured AP on channel 14"""
  208. try:
  209. ssid = "test-wps-conf"
  210. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  211. "wpa_passphrase": "12345678", "wpa": "2",
  212. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  213. "country_code": "JP", "hw_mode": "b", "channel": "14" }
  214. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  215. logger.info("WPS provisioning step")
  216. hapd.request("WPS_PBC")
  217. dev[0].request("WPS_PBC")
  218. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  219. if ev is None:
  220. raise Exception("Association with the AP timed out")
  221. sta = hapd.get_sta(dev[0].p2p_interface_addr())
  222. if 'wpsDeviceName' not in sta or sta['wpsDeviceName'] != "Device A":
  223. raise Exception("Device name not available in STA command")
  224. finally:
  225. subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
  226. def test_ap_wps_twice(dev, apdev):
  227. """WPS provisioning with twice to change passphrase"""
  228. ssid = "test-wps-twice"
  229. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  230. "wpa_passphrase": "12345678", "wpa": "2",
  231. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" }
  232. hostapd.add_ap(apdev[0]['ifname'], params)
  233. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  234. logger.info("WPS provisioning step")
  235. hapd.request("WPS_PBC")
  236. dev[0].dump_monitor()
  237. dev[0].request("WPS_PBC")
  238. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  239. if ev is None:
  240. raise Exception("Association with the AP timed out")
  241. dev[0].request("DISCONNECT")
  242. logger.info("Restart AP with different passphrase and re-run WPS")
  243. hapd_global = hostapd.HostapdGlobal()
  244. hapd_global.remove(apdev[0]['ifname'])
  245. params['wpa_passphrase'] = 'another passphrase'
  246. hostapd.add_ap(apdev[0]['ifname'], params)
  247. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  248. logger.info("WPS provisioning step")
  249. hapd.request("WPS_PBC")
  250. dev[0].dump_monitor()
  251. dev[0].request("WPS_PBC any")
  252. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  253. if ev is None:
  254. raise Exception("Association with the AP timed out")
  255. networks = dev[0].list_networks()
  256. if len(networks) > 1:
  257. raise Exception("Unexpected duplicated network block present")
  258. def test_ap_wps_incorrect_pin(dev, apdev):
  259. """WPS PIN provisioning with incorrect PIN"""
  260. ssid = "test-wps-incorrect-pin"
  261. hostapd.add_ap(apdev[0]['ifname'],
  262. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  263. "wpa_passphrase": "12345678", "wpa": "2",
  264. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  265. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  266. logger.info("WPS provisioning attempt 1")
  267. hapd.request("WPS_PIN any 12345670")
  268. dev[0].dump_monitor()
  269. dev[0].request("WPS_PIN any 55554444")
  270. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  271. if ev is None:
  272. raise Exception("WPS operation timed out")
  273. if "config_error=18" not in ev:
  274. raise Exception("Incorrect config_error reported")
  275. if "msg=8" not in ev:
  276. raise Exception("PIN error detected on incorrect message")
  277. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  278. if ev is None:
  279. raise Exception("Timeout on disconnection event")
  280. dev[0].request("WPS_CANCEL")
  281. # if a scan was in progress, wait for it to complete before trying WPS again
  282. ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 5)
  283. status = hapd.request("WPS_GET_STATUS")
  284. if "Last WPS result: Failed" not in status:
  285. raise Exception("WPS failure result not shown correctly")
  286. logger.info("WPS provisioning attempt 2")
  287. hapd.request("WPS_PIN any 12345670")
  288. dev[0].dump_monitor()
  289. dev[0].request("WPS_PIN any 12344444")
  290. ev = dev[0].wait_event(["WPS-FAIL"], timeout=30)
  291. if ev is None:
  292. raise Exception("WPS operation timed out")
  293. if "config_error=18" not in ev:
  294. raise Exception("Incorrect config_error reported")
  295. if "msg=10" not in ev:
  296. raise Exception("PIN error detected on incorrect message")
  297. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  298. if ev is None:
  299. raise Exception("Timeout on disconnection event")
  300. def test_ap_wps_conf_pin(dev, apdev):
  301. """WPS PIN provisioning with configured AP"""
  302. ssid = "test-wps-conf-pin"
  303. hostapd.add_ap(apdev[0]['ifname'],
  304. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  305. "wpa_passphrase": "12345678", "wpa": "2",
  306. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  307. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  308. logger.info("WPS provisioning step")
  309. pin = dev[0].wps_read_pin()
  310. hapd.request("WPS_PIN any " + pin)
  311. dev[0].dump_monitor()
  312. dev[0].request("WPS_PIN any " + pin)
  313. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  314. if ev is None:
  315. raise Exception("Association with the AP timed out")
  316. status = dev[0].get_status()
  317. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  318. raise Exception("Not fully connected")
  319. if status['ssid'] != ssid:
  320. raise Exception("Unexpected SSID")
  321. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  322. raise Exception("Unexpected encryption configuration")
  323. if status['key_mgmt'] != 'WPA2-PSK':
  324. raise Exception("Unexpected key_mgmt")
  325. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  326. bss = dev[1].get_bss(apdev[0]['bssid'])
  327. if "[WPS-AUTH]" in bss['flags']:
  328. raise Exception("WPS-AUTH flag not cleared")
  329. logger.info("Try to connect from another station using the same PIN")
  330. pin = dev[1].request("WPS_PIN any")
  331. ev = dev[1].wait_event(["WPS-M2D","CTRL-EVENT-CONNECTED"], timeout=30)
  332. if ev is None:
  333. raise Exception("Operation timed out")
  334. if "WPS-M2D" not in ev:
  335. raise Exception("Unexpected WPS operation started")
  336. hapd.request("WPS_PIN any " + pin)
  337. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  338. if ev is None:
  339. raise Exception("Association with the AP timed out")
  340. def test_ap_wps_conf_pin_v1(dev, apdev):
  341. """WPS PIN provisioning with configured WPS v1.0 AP"""
  342. ssid = "test-wps-conf-pin-v1"
  343. hostapd.add_ap(apdev[0]['ifname'],
  344. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  345. "wpa_passphrase": "12345678", "wpa": "2",
  346. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  347. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  348. logger.info("WPS provisioning step")
  349. pin = dev[0].wps_read_pin()
  350. hapd.request("SET wps_version_number 0x10")
  351. hapd.request("WPS_PIN any " + pin)
  352. found = False
  353. for i in range(0, 10):
  354. dev[0].scan(freq="2412")
  355. if "[WPS-PIN]" in dev[0].request("SCAN_RESULTS"):
  356. found = True
  357. break
  358. if not found:
  359. hapd.request("SET wps_version_number 0x20")
  360. raise Exception("WPS-PIN flag not seen in scan results")
  361. dev[0].dump_monitor()
  362. dev[0].request("WPS_PIN any " + pin)
  363. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  364. hapd.request("SET wps_version_number 0x20")
  365. if ev is None:
  366. raise Exception("Association with the AP timed out")
  367. def test_ap_wps_conf_pin_2sta(dev, apdev):
  368. """Two stations trying to use WPS PIN at the same time"""
  369. ssid = "test-wps-conf-pin2"
  370. hostapd.add_ap(apdev[0]['ifname'],
  371. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  372. "wpa_passphrase": "12345678", "wpa": "2",
  373. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  374. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  375. logger.info("WPS provisioning step")
  376. pin = "12345670"
  377. pin2 = "55554444"
  378. hapd.request("WPS_PIN " + dev[0].get_status_field("uuid") + " " + pin)
  379. hapd.request("WPS_PIN " + dev[1].get_status_field("uuid") + " " + pin)
  380. dev[0].dump_monitor()
  381. dev[1].dump_monitor()
  382. dev[0].request("WPS_PIN any " + pin)
  383. dev[1].request("WPS_PIN any " + pin)
  384. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  385. if ev is None:
  386. raise Exception("Association with the AP timed out")
  387. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  388. if ev is None:
  389. raise Exception("Association with the AP timed out")
  390. def test_ap_wps_conf_pin_timeout(dev, apdev):
  391. """WPS PIN provisioning with configured AP timing out PIN"""
  392. ssid = "test-wps-conf-pin"
  393. hostapd.add_ap(apdev[0]['ifname'],
  394. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  395. "wpa_passphrase": "12345678", "wpa": "2",
  396. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  397. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  398. addr = dev[0].p2p_interface_addr()
  399. pin = dev[0].wps_read_pin()
  400. if "FAIL" not in hapd.request("WPS_PIN "):
  401. raise Exception("Unexpected success on invalid WPS_PIN")
  402. hapd.request("WPS_PIN any " + pin + " 1")
  403. time.sleep(1.1)
  404. dev[0].request("WPS_PIN any " + pin)
  405. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=20)
  406. if ev is None:
  407. raise Exception("WPS-PIN-NEEDED event timed out")
  408. ev = dev[0].wait_event(["WPS-M2D"])
  409. if ev is None:
  410. raise Exception("M2D not reported")
  411. dev[0].request("WPS_CANCEL")
  412. hapd.request("WPS_PIN any " + pin + " 20 " + addr)
  413. dev[0].request("WPS_PIN any " + pin)
  414. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  415. if ev is None:
  416. raise Exception("Association with the AP timed out")
  417. def test_ap_wps_reg_connect(dev, apdev):
  418. """WPS registrar using AP PIN to connect"""
  419. ssid = "test-wps-reg-ap-pin"
  420. appin = "12345670"
  421. hostapd.add_ap(apdev[0]['ifname'],
  422. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  423. "wpa_passphrase": "12345678", "wpa": "2",
  424. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  425. "ap_pin": appin})
  426. logger.info("WPS provisioning step")
  427. dev[0].dump_monitor()
  428. dev[0].wps_reg(apdev[0]['bssid'], appin)
  429. status = dev[0].get_status()
  430. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  431. raise Exception("Not fully connected")
  432. if status['ssid'] != ssid:
  433. raise Exception("Unexpected SSID")
  434. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  435. raise Exception("Unexpected encryption configuration")
  436. if status['key_mgmt'] != 'WPA2-PSK':
  437. raise Exception("Unexpected key_mgmt")
  438. def check_wps_reg_failure(dev, ap, appin):
  439. dev.request("WPS_REG " + ap['bssid'] + " " + appin)
  440. ev = dev.wait_event(["WPS-SUCCESS", "WPS-FAIL"], timeout=15)
  441. if ev is None:
  442. raise Exception("WPS operation timed out")
  443. if "WPS-SUCCESS" in ev:
  444. raise Exception("WPS operation succeeded unexpectedly")
  445. if "config_error=15" not in ev:
  446. raise Exception("WPS setup locked state was not reported correctly")
  447. def test_ap_wps_random_ap_pin(dev, apdev):
  448. """WPS registrar using random AP PIN"""
  449. ssid = "test-wps-reg-random-ap-pin"
  450. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  451. hostapd.add_ap(apdev[0]['ifname'],
  452. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  453. "wpa_passphrase": "12345678", "wpa": "2",
  454. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  455. "device_name": "Wireless AP", "manufacturer": "Company",
  456. "model_name": "WAP", "model_number": "123",
  457. "serial_number": "12345", "device_type": "6-0050F204-1",
  458. "os_version": "01020300",
  459. "config_methods": "label push_button",
  460. "uuid": ap_uuid, "upnp_iface": "lo" })
  461. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  462. appin = hapd.request("WPS_AP_PIN random")
  463. if "FAIL" in appin:
  464. raise Exception("Could not generate random AP PIN")
  465. if appin not in hapd.request("WPS_AP_PIN get"):
  466. raise Exception("Could not fetch current AP PIN")
  467. logger.info("WPS provisioning step")
  468. dev[0].wps_reg(apdev[0]['bssid'], appin)
  469. hapd.request("WPS_AP_PIN disable")
  470. logger.info("WPS provisioning step with AP PIN disabled")
  471. check_wps_reg_failure(dev[1], apdev[0], appin)
  472. logger.info("WPS provisioning step with AP PIN reset")
  473. appin = "12345670"
  474. hapd.request("WPS_AP_PIN set " + appin)
  475. dev[1].wps_reg(apdev[0]['bssid'], appin)
  476. dev[0].request("REMOVE_NETWORK all")
  477. dev[1].request("REMOVE_NETWORK all")
  478. dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  479. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  480. logger.info("WPS provisioning step after AP PIN timeout")
  481. hapd.request("WPS_AP_PIN disable")
  482. appin = hapd.request("WPS_AP_PIN random 1")
  483. time.sleep(1.1)
  484. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  485. raise Exception("AP PIN unexpectedly still enabled")
  486. check_wps_reg_failure(dev[0], apdev[0], appin)
  487. logger.info("WPS provisioning step after AP PIN timeout(2)")
  488. hapd.request("WPS_AP_PIN disable")
  489. appin = "12345670"
  490. hapd.request("WPS_AP_PIN set " + appin + " 1")
  491. time.sleep(1.1)
  492. if "FAIL" not in hapd.request("WPS_AP_PIN get"):
  493. raise Exception("AP PIN unexpectedly still enabled")
  494. check_wps_reg_failure(dev[1], apdev[0], appin)
  495. def test_ap_wps_reg_config(dev, apdev):
  496. """WPS registrar configuring an AP using AP PIN"""
  497. ssid = "test-wps-init-ap-pin"
  498. appin = "12345670"
  499. hostapd.add_ap(apdev[0]['ifname'],
  500. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  501. "ap_pin": appin})
  502. logger.info("WPS configuration step")
  503. dev[0].dump_monitor()
  504. new_ssid = "wps-new-ssid"
  505. new_passphrase = "1234567890"
  506. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  507. new_passphrase)
  508. status = dev[0].get_status()
  509. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  510. raise Exception("Not fully connected")
  511. if status['ssid'] != new_ssid:
  512. raise Exception("Unexpected SSID")
  513. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  514. raise Exception("Unexpected encryption configuration")
  515. if status['key_mgmt'] != 'WPA2-PSK':
  516. raise Exception("Unexpected key_mgmt")
  517. logger.info("Re-configure back to open")
  518. dev[0].request("REMOVE_NETWORK all")
  519. dev[0].flush_scan_cache()
  520. dev[0].dump_monitor()
  521. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-open", "OPEN", "NONE", "")
  522. status = dev[0].get_status()
  523. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  524. raise Exception("Not fully connected")
  525. if status['ssid'] != "wps-open":
  526. raise Exception("Unexpected SSID")
  527. if status['key_mgmt'] != 'NONE':
  528. raise Exception("Unexpected key_mgmt")
  529. def test_ap_wps_reg_config_ext_processing(dev, apdev):
  530. """WPS registrar configuring an AP with external config processing"""
  531. ssid = "test-wps-init-ap-pin"
  532. appin = "12345670"
  533. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  534. "wps_cred_processing": "1", "ap_pin": appin}
  535. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  536. new_ssid = "wps-new-ssid"
  537. new_passphrase = "1234567890"
  538. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPA2PSK", "CCMP",
  539. new_passphrase, no_wait=True)
  540. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  541. if ev is None:
  542. raise Exception("WPS registrar operation timed out")
  543. ev = hapd.wait_event(["WPS-NEW-AP-SETTINGS"], timeout=15)
  544. if ev is None:
  545. raise Exception("WPS configuration timed out")
  546. if "1026" not in ev:
  547. raise Exception("AP Settings missing from event")
  548. hapd.request("SET wps_cred_processing 0")
  549. if "FAIL" in hapd.request("WPS_CONFIG " + new_ssid.encode("hex") + " WPA2PSK CCMP " + new_passphrase.encode("hex")):
  550. raise Exception("WPS_CONFIG command failed")
  551. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  552. if ev is None:
  553. raise Exception("Association with the AP timed out")
  554. def test_ap_wps_reg_config_tkip(dev, apdev):
  555. """WPS registrar configuring AP to use TKIP and AP upgrading to TKIP+CCMP"""
  556. ssid = "test-wps-init-ap"
  557. appin = "12345670"
  558. hostapd.add_ap(apdev[0]['ifname'],
  559. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  560. "ap_pin": appin})
  561. logger.info("WPS configuration step")
  562. dev[0].request("SET wps_version_number 0x10")
  563. dev[0].dump_monitor()
  564. new_ssid = "wps-new-ssid-with-tkip"
  565. new_passphrase = "1234567890"
  566. dev[0].wps_reg(apdev[0]['bssid'], appin, new_ssid, "WPAPSK", "TKIP",
  567. new_passphrase)
  568. logger.info("Re-connect to verify WPA2 mixed mode")
  569. dev[0].request("DISCONNECT")
  570. id = 0
  571. dev[0].set_network(id, "pairwise", "CCMP")
  572. dev[0].set_network(id, "proto", "RSN")
  573. dev[0].connect_network(id)
  574. status = dev[0].get_status()
  575. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  576. raise Exception("Not fully connected: wpa_state={} bssid={}".format(status['wpa_state'], status['bssid']))
  577. if status['ssid'] != new_ssid:
  578. raise Exception("Unexpected SSID")
  579. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  580. raise Exception("Unexpected encryption configuration")
  581. if status['key_mgmt'] != 'WPA2-PSK':
  582. raise Exception("Unexpected key_mgmt")
  583. def test_ap_wps_setup_locked(dev, apdev):
  584. """WPS registrar locking up AP setup on AP PIN failures"""
  585. ssid = "test-wps-incorrect-ap-pin"
  586. appin = "12345670"
  587. hostapd.add_ap(apdev[0]['ifname'],
  588. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  589. "wpa_passphrase": "12345678", "wpa": "2",
  590. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  591. "ap_pin": appin})
  592. new_ssid = "wps-new-ssid-test"
  593. new_passphrase = "1234567890"
  594. ap_setup_locked=False
  595. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  596. dev[0].dump_monitor()
  597. logger.info("Try incorrect AP PIN - attempt " + pin)
  598. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  599. "CCMP", new_passphrase, no_wait=True)
  600. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"])
  601. if ev is None:
  602. raise Exception("Timeout on receiving WPS operation failure event")
  603. if "CTRL-EVENT-CONNECTED" in ev:
  604. raise Exception("Unexpected connection")
  605. if "config_error=15" in ev:
  606. logger.info("AP Setup Locked")
  607. ap_setup_locked=True
  608. elif "config_error=18" not in ev:
  609. raise Exception("config_error=18 not reported")
  610. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  611. if ev is None:
  612. raise Exception("Timeout on disconnection event")
  613. time.sleep(0.1)
  614. if not ap_setup_locked:
  615. raise Exception("AP setup was not locked")
  616. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  617. status = hapd.request("WPS_GET_STATUS")
  618. if "Last WPS result: Failed" not in status:
  619. raise Exception("WPS failure result not shown correctly")
  620. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  621. raise Exception("Peer address not shown correctly")
  622. time.sleep(0.5)
  623. dev[0].dump_monitor()
  624. logger.info("WPS provisioning step")
  625. pin = dev[0].wps_read_pin()
  626. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  627. hapd.request("WPS_PIN any " + pin)
  628. dev[0].request("WPS_PIN any " + pin)
  629. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  630. if ev is None:
  631. raise Exception("WPS success was not reported")
  632. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  633. if ev is None:
  634. raise Exception("Association with the AP timed out")
  635. appin = hapd.request("WPS_AP_PIN random")
  636. if "FAIL" in appin:
  637. raise Exception("Could not generate random AP PIN")
  638. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=10)
  639. if ev is None:
  640. raise Exception("Failed to unlock AP PIN")
  641. def test_ap_wps_setup_locked_timeout(dev, apdev):
  642. """WPS re-enabling AP PIN after timeout"""
  643. ssid = "test-wps-incorrect-ap-pin"
  644. appin = "12345670"
  645. hostapd.add_ap(apdev[0]['ifname'],
  646. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  647. "wpa_passphrase": "12345678", "wpa": "2",
  648. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  649. "ap_pin": appin})
  650. new_ssid = "wps-new-ssid-test"
  651. new_passphrase = "1234567890"
  652. ap_setup_locked=False
  653. for pin in ["55554444", "1234", "12345678", "00000000", "11111111"]:
  654. dev[0].dump_monitor()
  655. logger.info("Try incorrect AP PIN - attempt " + pin)
  656. dev[0].wps_reg(apdev[0]['bssid'], pin, new_ssid, "WPA2PSK",
  657. "CCMP", new_passphrase, no_wait=True)
  658. ev = dev[0].wait_event(["WPS-FAIL", "CTRL-EVENT-CONNECTED"], timeout=15)
  659. if ev is None:
  660. raise Exception("Timeout on receiving WPS operation failure event")
  661. if "CTRL-EVENT-CONNECTED" in ev:
  662. raise Exception("Unexpected connection")
  663. if "config_error=15" in ev:
  664. logger.info("AP Setup Locked")
  665. ap_setup_locked=True
  666. break
  667. elif "config_error=18" not in ev:
  668. raise Exception("config_error=18 not reported")
  669. ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"])
  670. if ev is None:
  671. raise Exception("Timeout on disconnection event")
  672. time.sleep(0.1)
  673. if not ap_setup_locked:
  674. raise Exception("AP setup was not locked")
  675. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  676. ev = hapd.wait_event(["WPS-AP-SETUP-UNLOCKED"], timeout=80)
  677. if ev is None:
  678. raise Exception("AP PIN did not get unlocked on 60 second timeout")
  679. def test_ap_wps_pbc_overlap_2ap(dev, apdev):
  680. """WPS PBC session overlap with two active APs"""
  681. hostapd.add_ap(apdev[0]['ifname'],
  682. { "ssid": "wps1", "eap_server": "1", "wps_state": "2",
  683. "wpa_passphrase": "12345678", "wpa": "2",
  684. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  685. "wps_independent": "1"})
  686. hostapd.add_ap(apdev[1]['ifname'],
  687. { "ssid": "wps2", "eap_server": "1", "wps_state": "2",
  688. "wpa_passphrase": "123456789", "wpa": "2",
  689. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  690. "wps_independent": "1"})
  691. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  692. hapd.request("WPS_PBC")
  693. hapd2 = hostapd.Hostapd(apdev[1]['ifname'])
  694. hapd2.request("WPS_PBC")
  695. logger.info("WPS provisioning step")
  696. dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
  697. dev[0].scan_for_bss(apdev[1]['bssid'], freq="2412")
  698. dev[0].request("WPS_PBC")
  699. ev = dev[0].wait_event(["WPS-OVERLAP-DETECTED"], timeout=15)
  700. if ev is None:
  701. raise Exception("PBC session overlap not detected")
  702. def test_ap_wps_pbc_overlap_2sta(dev, apdev):
  703. """WPS PBC session overlap with two active STAs"""
  704. ssid = "test-wps-pbc-overlap"
  705. hostapd.add_ap(apdev[0]['ifname'],
  706. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  707. "wpa_passphrase": "12345678", "wpa": "2",
  708. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  709. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  710. logger.info("WPS provisioning step")
  711. hapd.request("WPS_PBC")
  712. dev[0].dump_monitor()
  713. dev[1].dump_monitor()
  714. dev[0].request("WPS_PBC")
  715. dev[1].request("WPS_PBC")
  716. ev = dev[0].wait_event(["WPS-M2D"], timeout=15)
  717. if ev is None:
  718. raise Exception("PBC session overlap not detected (dev0)")
  719. if "config_error=12" not in ev:
  720. raise Exception("PBC session overlap not correctly reported (dev0)")
  721. ev = dev[1].wait_event(["WPS-M2D"], timeout=15)
  722. if ev is None:
  723. raise Exception("PBC session overlap not detected (dev1)")
  724. if "config_error=12" not in ev:
  725. raise Exception("PBC session overlap not correctly reported (dev1)")
  726. hapd.request("WPS_CANCEL")
  727. ret = hapd.request("WPS_PBC")
  728. if "FAIL" not in ret:
  729. raise Exception("PBC mode allowed to be started while PBC overlap still active")
  730. def test_ap_wps_cancel(dev, apdev):
  731. """WPS AP cancelling enabled config method"""
  732. ssid = "test-wps-ap-cancel"
  733. hostapd.add_ap(apdev[0]['ifname'],
  734. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  735. "wpa_passphrase": "12345678", "wpa": "2",
  736. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  737. bssid = apdev[0]['bssid']
  738. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  739. logger.info("Verify PBC enable/cancel")
  740. hapd.request("WPS_PBC")
  741. dev[0].scan(freq="2412")
  742. dev[0].scan(freq="2412")
  743. bss = dev[0].get_bss(apdev[0]['bssid'])
  744. if "[WPS-PBC]" not in bss['flags']:
  745. raise Exception("WPS-PBC flag missing")
  746. if "FAIL" in hapd.request("WPS_CANCEL"):
  747. raise Exception("WPS_CANCEL failed")
  748. dev[0].scan(freq="2412")
  749. dev[0].scan(freq="2412")
  750. bss = dev[0].get_bss(apdev[0]['bssid'])
  751. if "[WPS-PBC]" in bss['flags']:
  752. raise Exception("WPS-PBC flag not cleared")
  753. logger.info("Verify PIN enable/cancel")
  754. hapd.request("WPS_PIN any 12345670")
  755. dev[0].scan(freq="2412")
  756. dev[0].scan(freq="2412")
  757. bss = dev[0].get_bss(apdev[0]['bssid'])
  758. if "[WPS-AUTH]" not in bss['flags']:
  759. raise Exception("WPS-AUTH flag missing")
  760. if "FAIL" in hapd.request("WPS_CANCEL"):
  761. raise Exception("WPS_CANCEL failed")
  762. dev[0].scan(freq="2412")
  763. dev[0].scan(freq="2412")
  764. bss = dev[0].get_bss(apdev[0]['bssid'])
  765. if "[WPS-AUTH]" in bss['flags']:
  766. raise Exception("WPS-AUTH flag not cleared")
  767. def test_ap_wps_er_add_enrollee(dev, apdev):
  768. """WPS ER configuring AP and adding a new enrollee using PIN"""
  769. ssid = "wps-er-add-enrollee"
  770. ap_pin = "12345670"
  771. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  772. hostapd.add_ap(apdev[0]['ifname'],
  773. { "ssid": ssid, "eap_server": "1", "wps_state": "1",
  774. "device_name": "Wireless AP", "manufacturer": "Company",
  775. "model_name": "WAP", "model_number": "123",
  776. "serial_number": "12345", "device_type": "6-0050F204-1",
  777. "os_version": "01020300",
  778. "config_methods": "label push_button",
  779. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  780. logger.info("WPS configuration step")
  781. new_passphrase = "1234567890"
  782. dev[0].dump_monitor()
  783. dev[0].wps_reg(apdev[0]['bssid'], ap_pin, ssid, "WPA2PSK", "CCMP",
  784. new_passphrase)
  785. status = dev[0].get_status()
  786. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  787. raise Exception("Not fully connected")
  788. if status['ssid'] != ssid:
  789. raise Exception("Unexpected SSID")
  790. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'CCMP':
  791. raise Exception("Unexpected encryption configuration")
  792. if status['key_mgmt'] != 'WPA2-PSK':
  793. raise Exception("Unexpected key_mgmt")
  794. logger.info("Start ER")
  795. dev[0].request("WPS_ER_START ifname=lo")
  796. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  797. if ev is None:
  798. raise Exception("AP discovery timed out")
  799. if ap_uuid not in ev:
  800. raise Exception("Expected AP UUID not found")
  801. logger.info("Learn AP configuration through UPnP")
  802. dev[0].dump_monitor()
  803. dev[0].request("WPS_ER_LEARN " + ap_uuid + " " + ap_pin)
  804. ev = dev[0].wait_event(["WPS-ER-AP-SETTINGS"], timeout=15)
  805. if ev is None:
  806. raise Exception("AP learn timed out")
  807. if ap_uuid not in ev:
  808. raise Exception("Expected AP UUID not in settings")
  809. if "ssid=" + ssid not in ev:
  810. raise Exception("Expected SSID not in settings")
  811. if "key=" + new_passphrase not in ev:
  812. raise Exception("Expected passphrase not in settings")
  813. logger.info("Add Enrollee using ER")
  814. pin = dev[1].wps_read_pin()
  815. dev[0].dump_monitor()
  816. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  817. dev[1].dump_monitor()
  818. dev[1].request("WPS_PIN any " + pin)
  819. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=30)
  820. if ev is None:
  821. raise Exception("Enrollee did not report success")
  822. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  823. if ev is None:
  824. raise Exception("Association with the AP timed out")
  825. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  826. if ev is None:
  827. raise Exception("WPS ER did not report success")
  828. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  829. logger.info("Add a specific Enrollee using ER")
  830. pin = dev[2].wps_read_pin()
  831. addr2 = dev[2].p2p_interface_addr()
  832. dev[0].dump_monitor()
  833. dev[2].dump_monitor()
  834. dev[2].request("WPS_PIN any " + pin)
  835. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=10)
  836. if ev is None:
  837. raise Exception("Enrollee not seen")
  838. if addr2 not in ev:
  839. raise Exception("Unexpected Enrollee MAC address")
  840. dev[0].request("WPS_ER_PIN " + addr2 + " " + pin + " " + addr2)
  841. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  842. if ev is None:
  843. raise Exception("Association with the AP timed out")
  844. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  845. if ev is None:
  846. raise Exception("WPS ER did not report success")
  847. logger.info("Verify registrar selection behavior")
  848. dev[0].request("WPS_ER_PIN any " + pin + " " + dev[1].p2p_interface_addr())
  849. dev[1].request("DISCONNECT")
  850. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  851. dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
  852. dev[1].scan(freq="2412")
  853. bss = dev[1].get_bss(apdev[0]['bssid'])
  854. if "[WPS-AUTH]" not in bss['flags']:
  855. # It is possible for scan to miss an update especially when running
  856. # tests under load with multiple VMs, so allow another attempt.
  857. dev[1].scan(freq="2412")
  858. bss = dev[1].get_bss(apdev[0]['bssid'])
  859. if "[WPS-AUTH]" not in bss['flags']:
  860. raise Exception("WPS-AUTH flag missing")
  861. logger.info("Stop ER")
  862. dev[0].dump_monitor()
  863. dev[0].request("WPS_ER_STOP")
  864. ev = dev[0].wait_event(["WPS-ER-AP-REMOVE"])
  865. if ev is None:
  866. raise Exception("WPS ER unsubscription timed out")
  867. # It takes some time for the UPnP UNSUBSCRIBE command to go through, so wait
  868. # a bit before verifying that the scan results have changed.
  869. time.sleep(0.2)
  870. for i in range(0, 10):
  871. dev[1].request("BSS_FLUSH 0")
  872. dev[1].scan(freq="2412", only_new=True)
  873. bss = dev[1].get_bss(apdev[0]['bssid'])
  874. if bss and 'flags' in bss and "[WPS-AUTH]" not in bss['flags']:
  875. break
  876. logger.debug("WPS-AUTH flag was still in place - wait a bit longer")
  877. time.sleep(0.1)
  878. if "[WPS-AUTH]" in bss['flags']:
  879. raise Exception("WPS-AUTH flag not removed")
  880. def test_ap_wps_er_add_enrollee_pbc(dev, apdev):
  881. """WPS ER connected to AP and adding a new enrollee using PBC"""
  882. ssid = "wps-er-add-enrollee-pbc"
  883. ap_pin = "12345670"
  884. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  885. hostapd.add_ap(apdev[0]['ifname'],
  886. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  887. "wpa_passphrase": "12345678", "wpa": "2",
  888. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  889. "device_name": "Wireless AP", "manufacturer": "Company",
  890. "model_name": "WAP", "model_number": "123",
  891. "serial_number": "12345", "device_type": "6-0050F204-1",
  892. "os_version": "01020300",
  893. "config_methods": "label push_button",
  894. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  895. logger.info("Learn AP configuration")
  896. dev[0].dump_monitor()
  897. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  898. status = dev[0].get_status()
  899. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  900. raise Exception("Not fully connected")
  901. logger.info("Start ER")
  902. dev[0].request("WPS_ER_START ifname=lo")
  903. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  904. if ev is None:
  905. raise Exception("AP discovery timed out")
  906. if ap_uuid not in ev:
  907. raise Exception("Expected AP UUID not found")
  908. logger.info("Use learned network configuration on ER")
  909. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  910. logger.info("Add Enrollee using ER and PBC")
  911. dev[0].dump_monitor()
  912. enrollee = dev[1].p2p_interface_addr()
  913. dev[1].dump_monitor()
  914. dev[1].request("WPS_PBC")
  915. for i in range(0, 2):
  916. ev = dev[0].wait_event(["WPS-ER-ENROLLEE-ADD"], timeout=15)
  917. if ev is None:
  918. raise Exception("Enrollee discovery timed out")
  919. if enrollee in ev:
  920. break
  921. if i == 1:
  922. raise Exception("Expected Enrollee not found")
  923. dev[0].request("WPS_ER_PBC " + enrollee)
  924. ev = dev[1].wait_event(["WPS-SUCCESS"], timeout=15)
  925. if ev is None:
  926. raise Exception("Enrollee did not report success")
  927. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  928. if ev is None:
  929. raise Exception("Association with the AP timed out")
  930. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  931. if ev is None:
  932. raise Exception("WPS ER did not report success")
  933. hwsim_utils.test_connectivity_sta(dev[0], dev[1])
  934. # verify BSSID selection of the AP instead of UUID
  935. if "FAIL" in dev[0].request("WPS_ER_SET_CONFIG " + apdev[0]['bssid'] + " 0"):
  936. raise Exception("Could not select AP based on BSSID")
  937. def test_ap_wps_er_v10_add_enrollee_pin(dev, apdev):
  938. """WPS v1.0 ER connected to AP and adding a new enrollee using PIN"""
  939. ssid = "wps-er-add-enrollee-pbc"
  940. ap_pin = "12345670"
  941. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  942. hostapd.add_ap(apdev[0]['ifname'],
  943. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  944. "wpa_passphrase": "12345678", "wpa": "2",
  945. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  946. "device_name": "Wireless AP", "manufacturer": "Company",
  947. "model_name": "WAP", "model_number": "123",
  948. "serial_number": "12345", "device_type": "6-0050F204-1",
  949. "os_version": "01020300",
  950. "config_methods": "label push_button",
  951. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  952. logger.info("Learn AP configuration")
  953. dev[0].request("SET wps_version_number 0x10")
  954. dev[0].dump_monitor()
  955. dev[0].wps_reg(apdev[0]['bssid'], ap_pin)
  956. status = dev[0].get_status()
  957. if status['wpa_state'] != 'COMPLETED' or status['bssid'] != apdev[0]['bssid']:
  958. raise Exception("Not fully connected")
  959. logger.info("Start ER")
  960. dev[0].request("WPS_ER_START ifname=lo")
  961. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  962. if ev is None:
  963. raise Exception("AP discovery timed out")
  964. if ap_uuid not in ev:
  965. raise Exception("Expected AP UUID not found")
  966. logger.info("Use learned network configuration on ER")
  967. dev[0].request("WPS_ER_SET_CONFIG " + ap_uuid + " 0")
  968. logger.info("Add Enrollee using ER and PIN")
  969. enrollee = dev[1].p2p_interface_addr()
  970. pin = dev[1].wps_read_pin()
  971. dev[0].dump_monitor()
  972. dev[0].request("WPS_ER_PIN any " + pin + " " + enrollee)
  973. dev[1].dump_monitor()
  974. dev[1].request("WPS_PIN any " + pin)
  975. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  976. if ev is None:
  977. raise Exception("Association with the AP timed out")
  978. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=15)
  979. if ev is None:
  980. raise Exception("WPS ER did not report success")
  981. def test_ap_wps_er_config_ap(dev, apdev):
  982. """WPS ER configuring AP over UPnP"""
  983. ssid = "wps-er-ap-config"
  984. ap_pin = "12345670"
  985. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  986. hostapd.add_ap(apdev[0]['ifname'],
  987. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  988. "wpa_passphrase": "12345678", "wpa": "2",
  989. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  990. "device_name": "Wireless AP", "manufacturer": "Company",
  991. "model_name": "WAP", "model_number": "123",
  992. "serial_number": "12345", "device_type": "6-0050F204-1",
  993. "os_version": "01020300",
  994. "config_methods": "label push_button",
  995. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo"})
  996. logger.info("Connect ER to the AP")
  997. dev[0].connect(ssid, psk="12345678", scan_freq="2412")
  998. logger.info("WPS configuration step")
  999. dev[0].request("WPS_ER_START ifname=lo")
  1000. ev = dev[0].wait_event(["WPS-ER-AP-ADD"], timeout=15)
  1001. if ev is None:
  1002. raise Exception("AP discovery timed out")
  1003. if ap_uuid not in ev:
  1004. raise Exception("Expected AP UUID not found")
  1005. new_passphrase = "1234567890"
  1006. dev[0].request("WPS_ER_CONFIG " + apdev[0]['bssid'] + " " + ap_pin + " " +
  1007. ssid.encode("hex") + " WPA2PSK CCMP " +
  1008. new_passphrase.encode("hex"))
  1009. ev = dev[0].wait_event(["WPS-SUCCESS"])
  1010. if ev is None:
  1011. raise Exception("WPS ER configuration operation timed out")
  1012. dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"])
  1013. dev[0].connect(ssid, psk="1234567890", scan_freq="2412")
  1014. def test_ap_wps_fragmentation(dev, apdev):
  1015. """WPS with fragmentation in EAP-WSC and mixed mode WPA+WPA2"""
  1016. ssid = "test-wps-fragmentation"
  1017. appin = "12345670"
  1018. hostapd.add_ap(apdev[0]['ifname'],
  1019. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1020. "wpa_passphrase": "12345678", "wpa": "3",
  1021. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1022. "wpa_pairwise": "TKIP", "ap_pin": appin,
  1023. "fragment_size": "50" })
  1024. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1025. logger.info("WPS provisioning step (PBC)")
  1026. hapd.request("WPS_PBC")
  1027. dev[0].dump_monitor()
  1028. dev[0].request("SET wps_fragment_size 50")
  1029. dev[0].request("WPS_PBC")
  1030. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1031. if ev is None:
  1032. raise Exception("Association with the AP timed out")
  1033. status = dev[0].get_status()
  1034. if status['wpa_state'] != 'COMPLETED':
  1035. raise Exception("Not fully connected")
  1036. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1037. raise Exception("Unexpected encryption configuration")
  1038. if status['key_mgmt'] != 'WPA2-PSK':
  1039. raise Exception("Unexpected key_mgmt")
  1040. logger.info("WPS provisioning step (PIN)")
  1041. pin = dev[1].wps_read_pin()
  1042. hapd.request("WPS_PIN any " + pin)
  1043. dev[1].request("SET wps_fragment_size 50")
  1044. dev[1].request("WPS_PIN any " + pin)
  1045. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1046. if ev is None:
  1047. raise Exception("Association with the AP timed out")
  1048. status = dev[1].get_status()
  1049. if status['wpa_state'] != 'COMPLETED':
  1050. raise Exception("Not fully connected")
  1051. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1052. raise Exception("Unexpected encryption configuration")
  1053. if status['key_mgmt'] != 'WPA2-PSK':
  1054. raise Exception("Unexpected key_mgmt")
  1055. logger.info("WPS connection as registrar")
  1056. dev[2].request("SET wps_fragment_size 50")
  1057. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1058. status = dev[2].get_status()
  1059. if status['wpa_state'] != 'COMPLETED':
  1060. raise Exception("Not fully connected")
  1061. if status['pairwise_cipher'] != 'CCMP' or status['group_cipher'] != 'TKIP':
  1062. raise Exception("Unexpected encryption configuration")
  1063. if status['key_mgmt'] != 'WPA2-PSK':
  1064. raise Exception("Unexpected key_mgmt")
  1065. def test_ap_wps_new_version_sta(dev, apdev):
  1066. """WPS compatibility with new version number on the station"""
  1067. ssid = "test-wps-ver"
  1068. hostapd.add_ap(apdev[0]['ifname'],
  1069. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1070. "wpa_passphrase": "12345678", "wpa": "2",
  1071. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1072. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1073. logger.info("WPS provisioning step")
  1074. hapd.request("WPS_PBC")
  1075. dev[0].dump_monitor()
  1076. dev[0].request("SET wps_version_number 0x43")
  1077. dev[0].request("SET wps_vendor_ext_m1 000137100100020001")
  1078. dev[0].request("WPS_PBC")
  1079. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1080. if ev is None:
  1081. raise Exception("Association with the AP timed out")
  1082. def test_ap_wps_new_version_ap(dev, apdev):
  1083. """WPS compatibility with new version number on the AP"""
  1084. ssid = "test-wps-ver"
  1085. hostapd.add_ap(apdev[0]['ifname'],
  1086. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1087. "wpa_passphrase": "12345678", "wpa": "2",
  1088. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1089. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1090. logger.info("WPS provisioning step")
  1091. if "FAIL" in hapd.request("SET wps_version_number 0x43"):
  1092. raise Exception("Failed to enable test functionality")
  1093. hapd.request("WPS_PBC")
  1094. dev[0].dump_monitor()
  1095. dev[0].request("WPS_PBC")
  1096. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1097. hapd.request("SET wps_version_number 0x20")
  1098. if ev is None:
  1099. raise Exception("Association with the AP timed out")
  1100. def test_ap_wps_check_pin(dev, apdev):
  1101. """Verify PIN checking through control interface"""
  1102. hostapd.add_ap(apdev[0]['ifname'],
  1103. { "ssid": "wps", "eap_server": "1", "wps_state": "2",
  1104. "wpa_passphrase": "12345678", "wpa": "2",
  1105. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP" })
  1106. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1107. for t in [ ("12345670", "12345670"),
  1108. ("12345678", "FAIL-CHECKSUM"),
  1109. ("12345", "FAIL"),
  1110. ("123456789", "FAIL"),
  1111. ("1234-5670", "12345670"),
  1112. ("1234 5670", "12345670"),
  1113. ("1-2.3:4 5670", "12345670") ]:
  1114. res = hapd.request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  1115. res2 = dev[0].request("WPS_CHECK_PIN " + t[0]).rstrip('\n')
  1116. if res != res2:
  1117. raise Exception("Unexpected difference in WPS_CHECK_PIN responses")
  1118. if res != t[1]:
  1119. raise Exception("Incorrect WPS_CHECK_PIN response {} (expected {})".format(res, t[1]))
  1120. if "FAIL" not in hapd.request("WPS_CHECK_PIN 12345"):
  1121. raise Exception("Unexpected WPS_CHECK_PIN success")
  1122. if "FAIL" not in hapd.request("WPS_CHECK_PIN 123456789"):
  1123. raise Exception("Unexpected WPS_CHECK_PIN success")
  1124. for i in range(0, 10):
  1125. pin = dev[0].request("WPS_PIN get")
  1126. rpin = dev[0].request("WPS_CHECK_PIN " + pin).rstrip('\n')
  1127. if pin != rpin:
  1128. raise Exception("Random PIN validation failed for " + pin)
  1129. def test_ap_wps_wep_config(dev, apdev):
  1130. """WPS 2.0 AP rejecting WEP configuration"""
  1131. ssid = "test-wps-config"
  1132. appin = "12345670"
  1133. hostapd.add_ap(apdev[0]['ifname'],
  1134. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1135. "ap_pin": appin})
  1136. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1137. dev[0].wps_reg(apdev[0]['bssid'], appin, "wps-new-ssid-wep", "OPEN", "WEP",
  1138. "hello", no_wait=True)
  1139. ev = hapd.wait_event(["WPS-FAIL"], timeout=15)
  1140. if ev is None:
  1141. raise Exception("WPS-FAIL timed out")
  1142. if "reason=2" not in ev:
  1143. raise Exception("Unexpected reason code in WPS-FAIL")
  1144. status = hapd.request("WPS_GET_STATUS")
  1145. if "Last WPS result: Failed" not in status:
  1146. raise Exception("WPS failure result not shown correctly")
  1147. if "Failure Reason: WEP Prohibited" not in status:
  1148. raise Exception("Failure reason not reported correctly")
  1149. if "Peer Address: " + dev[0].p2p_interface_addr() not in status:
  1150. raise Exception("Peer address not shown correctly")
  1151. def test_ap_wps_wep_enroll(dev, apdev):
  1152. """WPS 2.0 STA rejecting WEP configuration"""
  1153. ssid = "test-wps-wep"
  1154. hostapd.add_ap(apdev[0]['ifname'],
  1155. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1156. "skip_cred_build": "1", "extra_cred": "wps-wep-cred" })
  1157. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1158. hapd.request("WPS_PBC")
  1159. dev[0].request("WPS_PBC")
  1160. ev = dev[0].wait_event(["WPS-FAIL"], timeout=15)
  1161. if ev is None:
  1162. raise Exception("WPS-FAIL event timed out")
  1163. if "msg=12" not in ev or "reason=2 (WEP Prohibited)" not in ev:
  1164. raise Exception("Unexpected WPS-FAIL event: " + ev)
  1165. def test_ap_wps_ie_fragmentation(dev, apdev):
  1166. """WPS AP using fragmented WPS IE"""
  1167. ssid = "test-wps-ie-fragmentation"
  1168. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1169. "wpa_passphrase": "12345678", "wpa": "2",
  1170. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1171. "device_name": "1234567890abcdef1234567890abcdef",
  1172. "manufacturer": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
  1173. "model_name": "1234567890abcdef1234567890abcdef",
  1174. "model_number": "1234567890abcdef1234567890abcdef",
  1175. "serial_number": "1234567890abcdef1234567890abcdef" }
  1176. hostapd.add_ap(apdev[0]['ifname'], params)
  1177. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1178. hapd.request("WPS_PBC")
  1179. dev[0].request("WPS_PBC")
  1180. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1181. if ev is None:
  1182. raise Exception("Association with the AP timed out")
  1183. bss = dev[0].get_bss(apdev[0]['bssid'])
  1184. if "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1185. logger.info("Device Name not received correctly")
  1186. logger.info(bss)
  1187. # This can fail if Probe Response frame is missed and Beacon frame was
  1188. # used to fill in the BSS entry. This can happen, e.g., during heavy
  1189. # load every now and then and is not really an error, so try to
  1190. # workaround by runnign another scan.
  1191. dev[0].scan(freq="2412", only_new=True)
  1192. bss = dev[0].get_bss(apdev[0]['bssid'])
  1193. if not bss or "wps_device_name" not in bss or bss['wps_device_name'] != "1234567890abcdef1234567890abcdef":
  1194. logger.info(bss)
  1195. raise Exception("Device Name not received correctly")
  1196. if len(re.findall("dd..0050f204", bss['ie'])) != 2:
  1197. raise Exception("Unexpected number of WPS IEs")
  1198. def get_psk(pskfile):
  1199. psks = {}
  1200. with open(pskfile, "r") as f:
  1201. lines = f.read().splitlines()
  1202. for l in lines:
  1203. if l == "# WPA PSKs":
  1204. continue
  1205. (addr,psk) = l.split(' ')
  1206. psks[addr] = psk
  1207. return psks
  1208. def test_ap_wps_per_station_psk(dev, apdev):
  1209. """WPS PBC provisioning with per-station PSK"""
  1210. addr0 = dev[0].p2p_dev_addr()
  1211. addr1 = dev[1].p2p_dev_addr()
  1212. addr2 = dev[2].p2p_dev_addr()
  1213. ssid = "wps"
  1214. appin = "12345670"
  1215. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1216. try:
  1217. os.remove(pskfile)
  1218. except:
  1219. pass
  1220. try:
  1221. with open(pskfile, "w") as f:
  1222. f.write("# WPA PSKs\n")
  1223. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1224. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1225. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1226. "wpa_psk_file": pskfile }
  1227. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1228. logger.info("First enrollee")
  1229. hapd.request("WPS_PBC")
  1230. dev[0].request("WPS_PBC")
  1231. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1232. if ev is None:
  1233. raise Exception("Association with the AP timed out (1)")
  1234. logger.info("Second enrollee")
  1235. hapd.request("WPS_PBC")
  1236. dev[1].request("WPS_PBC")
  1237. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1238. if ev is None:
  1239. raise Exception("Association with the AP timed out (2)")
  1240. logger.info("External registrar")
  1241. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1242. logger.info("Verifying PSK results")
  1243. psks = get_psk(pskfile)
  1244. if addr0 not in psks:
  1245. raise Exception("No PSK recorded for sta0")
  1246. if addr1 not in psks:
  1247. raise Exception("No PSK recorded for sta1")
  1248. if addr2 not in psks:
  1249. raise Exception("No PSK recorded for sta2")
  1250. if psks[addr0] == psks[addr1]:
  1251. raise Exception("Same PSK recorded for sta0 and sta1")
  1252. if psks[addr0] == psks[addr2]:
  1253. raise Exception("Same PSK recorded for sta0 and sta2")
  1254. if psks[addr1] == psks[addr2]:
  1255. raise Exception("Same PSK recorded for sta1 and sta2")
  1256. dev[0].request("REMOVE_NETWORK all")
  1257. logger.info("Second external registrar")
  1258. dev[0].wps_reg(apdev[0]['bssid'], appin)
  1259. psks2 = get_psk(pskfile)
  1260. if addr0 not in psks2:
  1261. raise Exception("No PSK recorded for sta0(reg)")
  1262. if psks[addr0] == psks2[addr0]:
  1263. raise Exception("Same PSK recorded for sta0(enrollee) and sta0(reg)")
  1264. finally:
  1265. os.remove(pskfile)
  1266. def test_ap_wps_per_station_psk_failure(dev, apdev):
  1267. """WPS PBC provisioning with per-station PSK (file not writable)"""
  1268. addr0 = dev[0].p2p_dev_addr()
  1269. addr1 = dev[1].p2p_dev_addr()
  1270. addr2 = dev[2].p2p_dev_addr()
  1271. ssid = "wps"
  1272. appin = "12345670"
  1273. pskfile = "/tmp/ap_wps_per_enrollee_psk.psk_file"
  1274. try:
  1275. os.remove(pskfile)
  1276. except:
  1277. pass
  1278. try:
  1279. with open(pskfile, "w") as f:
  1280. f.write("# WPA PSKs\n")
  1281. params = { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1282. "wpa": "2", "wpa_key_mgmt": "WPA-PSK",
  1283. "rsn_pairwise": "CCMP", "ap_pin": appin,
  1284. "wpa_psk_file": pskfile }
  1285. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  1286. if "FAIL" in hapd.request("SET wpa_psk_file /tmp/does/not/exists/ap_wps_per_enrollee_psk_failure.psk_file"):
  1287. raise Exception("Failed to set wpa_psk_file")
  1288. logger.info("First enrollee")
  1289. hapd.request("WPS_PBC")
  1290. dev[0].request("WPS_PBC")
  1291. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1292. if ev is None:
  1293. raise Exception("Association with the AP timed out (1)")
  1294. logger.info("Second enrollee")
  1295. hapd.request("WPS_PBC")
  1296. dev[1].request("WPS_PBC")
  1297. ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1298. if ev is None:
  1299. raise Exception("Association with the AP timed out (2)")
  1300. logger.info("External registrar")
  1301. dev[2].wps_reg(apdev[0]['bssid'], appin)
  1302. logger.info("Verifying PSK results")
  1303. psks = get_psk(pskfile)
  1304. if len(psks) > 0:
  1305. raise Exception("PSK recorded unexpectedly")
  1306. finally:
  1307. os.remove(pskfile)
  1308. def test_ap_wps_pin_request_file(dev, apdev):
  1309. """WPS PIN provisioning with configured AP"""
  1310. ssid = "wps"
  1311. pinfile = "/tmp/ap_wps_pin_request_file.log"
  1312. if os.path.exists(pinfile):
  1313. subprocess.call(['sudo', 'rm', pinfile])
  1314. hostapd.add_ap(apdev[0]['ifname'],
  1315. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1316. "wps_pin_requests": pinfile,
  1317. "wpa_passphrase": "12345678", "wpa": "2",
  1318. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  1319. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1320. uuid = dev[0].get_status_field("uuid")
  1321. pin = dev[0].wps_read_pin()
  1322. try:
  1323. dev[0].request("WPS_PIN any " + pin)
  1324. ev = hapd.wait_event(["WPS-PIN-NEEDED"], timeout=15)
  1325. if ev is None:
  1326. raise Exception("PIN needed event not shown")
  1327. if uuid not in ev:
  1328. raise Exception("UUID mismatch")
  1329. dev[0].request("WPS_CANCEL")
  1330. success = False
  1331. with open(pinfile, "r") as f:
  1332. lines = f.readlines()
  1333. for l in lines:
  1334. if uuid in l:
  1335. success = True
  1336. break
  1337. if not success:
  1338. raise Exception("PIN request entry not in the log file")
  1339. finally:
  1340. subprocess.call(['sudo', 'rm', pinfile])
  1341. def test_ap_wps_auto_setup_with_config_file(dev, apdev):
  1342. """WPS auto-setup with configuration file"""
  1343. conffile = "/tmp/ap_wps_auto_setup_with_config_file.conf"
  1344. ifname = apdev[0]['ifname']
  1345. try:
  1346. with open(conffile, "w") as f:
  1347. f.write("driver=nl80211\n")
  1348. f.write("hw_mode=g\n")
  1349. f.write("channel=1\n")
  1350. f.write("ieee80211n=1\n")
  1351. f.write("interface=%s\n" % ifname)
  1352. f.write("ctrl_interface=/var/run/hostapd\n")
  1353. f.write("ssid=wps\n")
  1354. f.write("eap_server=1\n")
  1355. f.write("wps_state=1\n")
  1356. hostapd.add_bss('phy3', ifname, conffile)
  1357. hapd = hostapd.Hostapd(ifname)
  1358. hapd.request("WPS_PBC")
  1359. dev[0].request("WPS_PBC")
  1360. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1361. if ev is None:
  1362. raise Exception("Association with the AP timed out")
  1363. with open(conffile, "r") as f:
  1364. lines = f.read().splitlines()
  1365. vals = dict()
  1366. for l in lines:
  1367. try:
  1368. [name,value] = l.split('=', 1)
  1369. vals[name] = value
  1370. except ValueError, e:
  1371. if "# WPS configuration" in l:
  1372. pass
  1373. else:
  1374. raise Exception("Unexpected configuration line: " + l)
  1375. if vals['ieee80211n'] != '1' or vals['wps_state'] != '2' or "WPA-PSK" not in vals['wpa_key_mgmt']:
  1376. raise Exception("Incorrect configuration: " + str(vals))
  1377. finally:
  1378. subprocess.call(['sudo', 'rm', conffile])
  1379. def test_ap_wps_pbc_timeout(dev, apdev, params):
  1380. """wpa_supplicant PBC walk time [long]"""
  1381. if not params['long']:
  1382. logger.info("Skip test case with long duration due to --long not specified")
  1383. return "skip"
  1384. ssid = "test-wps"
  1385. hostapd.add_ap(apdev[0]['ifname'],
  1386. { "ssid": ssid, "eap_server": "1", "wps_state": "1" })
  1387. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1388. logger.info("Start WPS_PBC and wait for PBC walk time expiration")
  1389. if "OK" not in dev[0].request("WPS_PBC"):
  1390. raise Exception("WPS_PBC failed")
  1391. ev = dev[0].wait_event(["WPS-TIMEOUT"], timeout=150)
  1392. if ev is None:
  1393. raise Exception("WPS-TIMEOUT not reported")
  1394. def add_ssdp_ap(ifname, ap_uuid):
  1395. ssid = "wps-ssdp"
  1396. ap_pin = "12345670"
  1397. hostapd.add_ap(ifname,
  1398. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1399. "wpa_passphrase": "12345678", "wpa": "2",
  1400. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP",
  1401. "device_name": "Wireless AP", "manufacturer": "Company",
  1402. "model_name": "WAP", "model_number": "123",
  1403. "serial_number": "12345", "device_type": "6-0050F204-1",
  1404. "os_version": "01020300",
  1405. "config_methods": "label push_button",
  1406. "ap_pin": ap_pin, "uuid": ap_uuid, "upnp_iface": "lo",
  1407. "friendly_name": "WPS Access Point",
  1408. "manufacturer_url": "http://www.example.com/",
  1409. "model_description": "Wireless Access Point",
  1410. "model_url": "http://www.example.com/model/",
  1411. "upc": "123456789012" })
  1412. def ssdp_send(msg, no_recv=False):
  1413. socket.setdefaulttimeout(1)
  1414. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1415. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1416. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1417. sock.bind(("127.0.0.1", 0))
  1418. sock.sendto(msg, ("239.255.255.250", 1900))
  1419. if no_recv:
  1420. return None
  1421. return sock.recv(1000)
  1422. def ssdp_send_msearch(st):
  1423. msg = '\r\n'.join([
  1424. 'M-SEARCH * HTTP/1.1',
  1425. 'HOST: 239.255.255.250:1900',
  1426. 'MX: 1',
  1427. 'MAN: "ssdp:discover"',
  1428. 'ST: ' + st,
  1429. '', ''])
  1430. return ssdp_send(msg)
  1431. def test_ap_wps_ssdp_msearch(dev, apdev):
  1432. """WPS AP and SSDP M-SEARCH messages"""
  1433. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1434. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1435. msg = '\r\n'.join([
  1436. 'M-SEARCH * HTTP/1.1',
  1437. 'Host: 239.255.255.250:1900',
  1438. 'Mx: 1',
  1439. 'Man: "ssdp:discover"',
  1440. 'St: urn:schemas-wifialliance-org:device:WFADevice:1',
  1441. '', ''])
  1442. ssdp_send(msg)
  1443. msg = '\r\n'.join([
  1444. 'M-SEARCH * HTTP/1.1',
  1445. 'host:\t239.255.255.250:1900\t\t\t\t \t\t',
  1446. 'mx: \t1\t\t ',
  1447. 'man: \t \t "ssdp:discover" ',
  1448. 'st: urn:schemas-wifialliance-org:device:WFADevice:1\t\t',
  1449. '', ''])
  1450. ssdp_send(msg)
  1451. ssdp_send_msearch("ssdp:all")
  1452. ssdp_send_msearch("upnp:rootdevice")
  1453. ssdp_send_msearch("uuid:" + ap_uuid)
  1454. ssdp_send_msearch("urn:schemas-wifialliance-org:service:WFAWLANConfig:1")
  1455. ssdp_send_msearch("urn:schemas-wifialliance-org:device:WFADevice:1");
  1456. msg = '\r\n'.join([
  1457. 'M-SEARCH * HTTP/1.1',
  1458. 'HOST:\t239.255.255.250:1900',
  1459. 'MAN: "ssdp:discover"',
  1460. 'MX: 130',
  1461. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1462. '', ''])
  1463. ssdp_send(msg, no_recv=True)
  1464. def test_ap_wps_ssdp_invalid_msearch(dev, apdev):
  1465. """WPS AP and invalid SSDP M-SEARCH messages"""
  1466. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1467. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1468. socket.setdefaulttimeout(1)
  1469. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1470. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1471. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1472. sock.bind(("127.0.0.1", 0))
  1473. logger.debug("Missing MX")
  1474. msg = '\r\n'.join([
  1475. 'M-SEARCH * HTTP/1.1',
  1476. 'HOST: 239.255.255.250:1900',
  1477. 'MAN: "ssdp:discover"',
  1478. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1479. '', ''])
  1480. sock.sendto(msg, ("239.255.255.250", 1900))
  1481. logger.debug("Negative MX")
  1482. msg = '\r\n'.join([
  1483. 'M-SEARCH * HTTP/1.1',
  1484. 'HOST: 239.255.255.250:1900',
  1485. 'MX: -1',
  1486. 'MAN: "ssdp:discover"',
  1487. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1488. '', ''])
  1489. sock.sendto(msg, ("239.255.255.250", 1900))
  1490. logger.debug("Invalid MX")
  1491. msg = '\r\n'.join([
  1492. 'M-SEARCH * HTTP/1.1',
  1493. 'HOST: 239.255.255.250:1900',
  1494. 'MX; 1',
  1495. 'MAN: "ssdp:discover"',
  1496. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1497. '', ''])
  1498. sock.sendto(msg, ("239.255.255.250", 1900))
  1499. logger.debug("Missing MAN")
  1500. msg = '\r\n'.join([
  1501. 'M-SEARCH * HTTP/1.1',
  1502. 'HOST: 239.255.255.250:1900',
  1503. 'MX: 1',
  1504. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1505. '', ''])
  1506. sock.sendto(msg, ("239.255.255.250", 1900))
  1507. logger.debug("Invalid MAN")
  1508. msg = '\r\n'.join([
  1509. 'M-SEARCH * HTTP/1.1',
  1510. 'HOST: 239.255.255.250:1900',
  1511. 'MX: 1',
  1512. 'MAN: foo',
  1513. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1514. '', ''])
  1515. sock.sendto(msg, ("239.255.255.250", 1900))
  1516. msg = '\r\n'.join([
  1517. 'M-SEARCH * HTTP/1.1',
  1518. 'HOST: 239.255.255.250:1900',
  1519. 'MX: 1',
  1520. 'MAN; "ssdp:discover"',
  1521. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1522. '', ''])
  1523. sock.sendto(msg, ("239.255.255.250", 1900))
  1524. logger.debug("Missing HOST")
  1525. msg = '\r\n'.join([
  1526. 'M-SEARCH * HTTP/1.1',
  1527. 'MAN: "ssdp:discover"',
  1528. 'MX: 1',
  1529. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1530. '', ''])
  1531. sock.sendto(msg, ("239.255.255.250", 1900))
  1532. logger.debug("Missing ST")
  1533. msg = '\r\n'.join([
  1534. 'M-SEARCH * HTTP/1.1',
  1535. 'HOST: 239.255.255.250:1900',
  1536. 'MAN: "ssdp:discover"',
  1537. 'MX: 1',
  1538. '', ''])
  1539. sock.sendto(msg, ("239.255.255.250", 1900))
  1540. logger.debug("Mismatching ST")
  1541. msg = '\r\n'.join([
  1542. 'M-SEARCH * HTTP/1.1',
  1543. 'HOST: 239.255.255.250:1900',
  1544. 'MAN: "ssdp:discover"',
  1545. 'MX: 1',
  1546. 'ST: uuid:16d5f8a9-4ee4-4f5e-81f9-cc6e2f47f42d',
  1547. '', ''])
  1548. sock.sendto(msg, ("239.255.255.250", 1900))
  1549. msg = '\r\n'.join([
  1550. 'M-SEARCH * HTTP/1.1',
  1551. 'HOST: 239.255.255.250:1900',
  1552. 'MAN: "ssdp:discover"',
  1553. 'MX: 1',
  1554. 'ST: foo:bar',
  1555. '', ''])
  1556. sock.sendto(msg, ("239.255.255.250", 1900))
  1557. msg = '\r\n'.join([
  1558. 'M-SEARCH * HTTP/1.1',
  1559. 'HOST: 239.255.255.250:1900',
  1560. 'MAN: "ssdp:discover"',
  1561. 'MX: 1',
  1562. 'ST: foobar',
  1563. '', ''])
  1564. sock.sendto(msg, ("239.255.255.250", 1900))
  1565. logger.debug("Invalid ST")
  1566. msg = '\r\n'.join([
  1567. 'M-SEARCH * HTTP/1.1',
  1568. 'HOST: 239.255.255.250:1900',
  1569. 'MAN: "ssdp:discover"',
  1570. 'MX: 1',
  1571. 'ST; urn:schemas-wifialliance-org:device:WFADevice:1',
  1572. '', ''])
  1573. sock.sendto(msg, ("239.255.255.250", 1900))
  1574. logger.debug("Invalid M-SEARCH")
  1575. msg = '\r\n'.join([
  1576. 'M+SEARCH * HTTP/1.1',
  1577. 'HOST: 239.255.255.250:1900',
  1578. 'MAN: "ssdp:discover"',
  1579. 'MX: 1',
  1580. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1581. '', ''])
  1582. sock.sendto(msg, ("239.255.255.250", 1900))
  1583. msg = '\r\n'.join([
  1584. 'M-SEARCH-* HTTP/1.1',
  1585. 'HOST: 239.255.255.250:1900',
  1586. 'MAN: "ssdp:discover"',
  1587. 'MX: 1',
  1588. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1589. '', ''])
  1590. sock.sendto(msg, ("239.255.255.250", 1900))
  1591. logger.debug("Invalid message format")
  1592. sock.sendto("NOTIFY * HTTP/1.1", ("239.255.255.250", 1900))
  1593. msg = '\r'.join([
  1594. 'M-SEARCH * HTTP/1.1',
  1595. 'HOST: 239.255.255.250:1900',
  1596. 'MAN: "ssdp:discover"',
  1597. 'MX: 1',
  1598. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1599. '', ''])
  1600. sock.sendto(msg, ("239.255.255.250", 1900))
  1601. try:
  1602. r = sock.recv(1000)
  1603. raise Exception("Unexpected M-SEARCH response: " + r)
  1604. except socket.timeout:
  1605. pass
  1606. logger.debug("Valid M-SEARCH")
  1607. msg = '\r\n'.join([
  1608. 'M-SEARCH * HTTP/1.1',
  1609. 'HOST: 239.255.255.250:1900',
  1610. 'MAN: "ssdp:discover"',
  1611. 'MX: 1',
  1612. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1613. '', ''])
  1614. sock.sendto(msg, ("239.255.255.250", 1900))
  1615. try:
  1616. r = sock.recv(1000)
  1617. pass
  1618. except socket.timeout:
  1619. raise Exception("No SSDP response")
  1620. def test_ap_wps_ssdp_burst(dev, apdev):
  1621. """WPS AP and SSDP burst"""
  1622. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1623. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1624. msg = '\r\n'.join([
  1625. 'M-SEARCH * HTTP/1.1',
  1626. 'HOST: 239.255.255.250:1900',
  1627. 'MAN: "ssdp:discover"',
  1628. 'MX: 1',
  1629. 'ST: urn:schemas-wifialliance-org:device:WFADevice:1',
  1630. '', ''])
  1631. socket.setdefaulttimeout(1)
  1632. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1633. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1634. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1635. sock.bind(("127.0.0.1", 0))
  1636. for i in range(0, 25):
  1637. sock.sendto(msg, ("239.255.255.250", 1900))
  1638. resp = 0
  1639. while True:
  1640. try:
  1641. r = sock.recv(1000)
  1642. if not r.startswith("HTTP/1.1 200 OK\r\n"):
  1643. raise Exception("Unexpected message: " + r)
  1644. resp += 1
  1645. except socket.timeout:
  1646. break
  1647. if resp < 20:
  1648. raise Exception("Too few SSDP responses")
  1649. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  1650. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1651. sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
  1652. sock.bind(("127.0.0.1", 0))
  1653. for i in range(0, 25):
  1654. sock.sendto(msg, ("239.255.255.250", 1900))
  1655. while True:
  1656. try:
  1657. r = sock.recv(1000)
  1658. if ap_uuid in r:
  1659. break
  1660. except socket.timeout:
  1661. raise Exception("No SSDP response")
  1662. def ssdp_get_location(uuid):
  1663. res = ssdp_send_msearch("uuid:" + uuid)
  1664. location = None
  1665. for l in res.splitlines():
  1666. if l.lower().startswith("location:"):
  1667. location = l.split(':', 1)[1].strip()
  1668. break
  1669. if location is None:
  1670. raise Exception("No UPnP location found")
  1671. return location
  1672. def upnp_get_urls(location):
  1673. conn = urllib.urlopen(location)
  1674. tree = ET.parse(conn)
  1675. root = tree.getroot()
  1676. urn = '{urn:schemas-upnp-org:device-1-0}'
  1677. service = root.find("./" + urn + "device/" + urn + "serviceList/" + urn + "service")
  1678. res = {}
  1679. res['scpd_url'] = urlparse.urljoin(location, service.find(urn + 'SCPDURL').text)
  1680. res['control_url'] = urlparse.urljoin(location, service.find(urn + 'controlURL').text)
  1681. res['event_sub_url'] = urlparse.urljoin(location, service.find(urn + 'eventSubURL').text)
  1682. return res
  1683. def upnp_soap_action(conn, path, action, include_soap_action=True, soap_action_override=None):
  1684. soapns = 'http://schemas.xmlsoap.org/soap/envelope/'
  1685. wpsns = 'urn:schemas-wifialliance-org:service:WFAWLANConfig:1'
  1686. ET.register_namespace('soapenv', soapns)
  1687. ET.register_namespace('wfa', wpsns)
  1688. attrib = {}
  1689. attrib['{%s}encodingStyle' % soapns] = 'http://schemas.xmlsoap.org/soap/encoding/'
  1690. root = ET.Element("{%s}Envelope" % soapns, attrib=attrib)
  1691. body = ET.SubElement(root, "{%s}Body" % soapns)
  1692. act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
  1693. tree = ET.ElementTree(root)
  1694. soap = StringIO.StringIO()
  1695. tree.write(soap, xml_declaration=True, encoding='utf-8')
  1696. headers = { "Content-type": 'text/xml; charset="utf-8"' }
  1697. if include_soap_action:
  1698. headers["SOAPAction"] = '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#%s"' % action
  1699. elif soap_action_override:
  1700. headers["SOAPAction"] = soap_action_override
  1701. conn.request("POST", path, soap.getvalue(), headers)
  1702. return conn.getresponse()
  1703. def test_ap_wps_upnp(dev, apdev):
  1704. """WPS AP and UPnP operations"""
  1705. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1706. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1707. location = ssdp_get_location(ap_uuid)
  1708. urls = upnp_get_urls(location)
  1709. conn = urllib.urlopen(urls['scpd_url'])
  1710. scpd = conn.read()
  1711. conn = urllib.urlopen(urlparse.urljoin(location, "unknown.html"))
  1712. if conn.getcode() != 404:
  1713. raise Exception("Unexpected HTTP response to GET unknown URL")
  1714. url = urlparse.urlparse(location)
  1715. conn = httplib.HTTPConnection(url.netloc)
  1716. #conn.set_debuglevel(1)
  1717. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1718. "SOAPAction": '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo"' }
  1719. conn.request("POST", "hello", "\r\n\r\n", headers)
  1720. resp = conn.getresponse()
  1721. if resp.status != 404:
  1722. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1723. conn.request("UNKNOWN", "hello", "\r\n\r\n", headers)
  1724. resp = conn.getresponse()
  1725. if resp.status != 501:
  1726. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1727. headers = { "Content-type": 'text/xml; charset="utf-8"',
  1728. "SOAPAction": '"urn:some-unknown-action#GetDeviceInfo"' }
  1729. ctrlurl = urlparse.urlparse(urls['control_url'])
  1730. conn.request("POST", ctrlurl.path, "\r\n\r\n", headers)
  1731. resp = conn.getresponse()
  1732. if resp.status != 401:
  1733. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1734. logger.debug("GetDeviceInfo without SOAPAction header")
  1735. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1736. include_soap_action=False)
  1737. if resp.status != 401:
  1738. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1739. logger.debug("GetDeviceInfo with invalid SOAPAction header")
  1740. for act in [ "foo",
  1741. "urn:schemas-wifialliance-org:service:WFAWLANConfig:1#GetDeviceInfo",
  1742. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:1"',
  1743. '"urn:schemas-wifialliance-org:service:WFAWLANConfig:123#GetDevice']:
  1744. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo",
  1745. include_soap_action=False,
  1746. soap_action_override=act)
  1747. if resp.status != 401:
  1748. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1749. resp = upnp_soap_action(conn, ctrlurl.path, "GetDeviceInfo")
  1750. if resp.status != 200:
  1751. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1752. dev = resp.read()
  1753. if "NewDeviceInfo" not in dev:
  1754. raise Exception("Unexpected GetDeviceInfo response")
  1755. logger.debug("PutMessage without required parameters")
  1756. resp = upnp_soap_action(conn, ctrlurl.path, "PutMessage")
  1757. if resp.status != 600:
  1758. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1759. logger.debug("PutWLANResponse without required parameters")
  1760. resp = upnp_soap_action(conn, ctrlurl.path, "PutWLANResponse")
  1761. if resp.status != 600:
  1762. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1763. logger.debug("SetSelectedRegistrar from unregistered ER")
  1764. resp = upnp_soap_action(conn, ctrlurl.path, "SetSelectedRegistrar")
  1765. if resp.status != 501:
  1766. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1767. logger.debug("Unknown action")
  1768. resp = upnp_soap_action(conn, ctrlurl.path, "Unknown")
  1769. if resp.status != 401:
  1770. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1771. def test_ap_wps_upnp_subscribe(dev, apdev):
  1772. """WPS AP and UPnP event subscription"""
  1773. ap_uuid = "27ea801a-9e5c-4e73-bd82-f89cbcd10d7e"
  1774. add_ssdp_ap(apdev[0]['ifname'], ap_uuid)
  1775. location = ssdp_get_location(ap_uuid)
  1776. urls = upnp_get_urls(location)
  1777. eventurl = urlparse.urlparse(urls['event_sub_url'])
  1778. url = urlparse.urlparse(location)
  1779. conn = httplib.HTTPConnection(url.netloc)
  1780. #conn.set_debuglevel(1)
  1781. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1782. "timeout": "Second-1234" }
  1783. conn.request("SUBSCRIBE", "hello", "\r\n\r\n", headers)
  1784. resp = conn.getresponse()
  1785. if resp.status != 412:
  1786. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1787. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1788. resp = conn.getresponse()
  1789. if resp.status != 412:
  1790. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1791. headers = { "NT": "upnp:event",
  1792. "timeout": "Second-1234" }
  1793. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1794. resp = conn.getresponse()
  1795. if resp.status != 412:
  1796. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1797. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1798. "NT": "upnp:foobar",
  1799. "timeout": "Second-1234" }
  1800. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1801. resp = conn.getresponse()
  1802. if resp.status != 400:
  1803. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1804. logger.debug("Valid subscription")
  1805. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1806. "NT": "upnp:event",
  1807. "timeout": "Second-1234" }
  1808. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1809. resp = conn.getresponse()
  1810. if resp.status != 200:
  1811. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1812. sid = resp.getheader("sid")
  1813. logger.debug("Subscription SID " + sid)
  1814. logger.debug("Invalid re-subscription")
  1815. headers = { "NT": "upnp:event",
  1816. "sid": "123456734567854",
  1817. "timeout": "Second-1234" }
  1818. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1819. resp = conn.getresponse()
  1820. if resp.status != 400:
  1821. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1822. logger.debug("Invalid re-subscription")
  1823. headers = { "NT": "upnp:event",
  1824. "sid": "uuid:123456734567854",
  1825. "timeout": "Second-1234" }
  1826. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1827. resp = conn.getresponse()
  1828. if resp.status != 400:
  1829. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1830. logger.debug("Invalid re-subscription")
  1831. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1832. "NT": "upnp:event",
  1833. "sid": sid,
  1834. "timeout": "Second-1234" }
  1835. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1836. resp = conn.getresponse()
  1837. if resp.status != 400:
  1838. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1839. logger.debug("SID mismatch in re-subscription")
  1840. headers = { "NT": "upnp:event",
  1841. "sid": "uuid:4c2bca79-1ff4-4e43-85d4-952a2b8a51fb",
  1842. "timeout": "Second-1234" }
  1843. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1844. resp = conn.getresponse()
  1845. if resp.status != 412:
  1846. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1847. logger.debug("Valid re-subscription")
  1848. headers = { "NT": "upnp:event",
  1849. "sid": sid,
  1850. "timeout": "Second-1234" }
  1851. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1852. resp = conn.getresponse()
  1853. if resp.status != 200:
  1854. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1855. sid2 = resp.getheader("sid")
  1856. logger.debug("Subscription SID " + sid2)
  1857. if sid != sid2:
  1858. raise Exception("Unexpected SID change")
  1859. logger.debug("Valid re-subscription")
  1860. headers = { "NT": "upnp:event",
  1861. "sid": "uuid: \t \t" + sid.split(':')[1],
  1862. "timeout": "Second-1234" }
  1863. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1864. resp = conn.getresponse()
  1865. if resp.status != 200:
  1866. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1867. logger.debug("Invalid unsubscription")
  1868. headers = { "sid": sid }
  1869. conn.request("UNSUBSCRIBE", "/hello", "\r\n\r\n", headers)
  1870. resp = conn.getresponse()
  1871. if resp.status != 412:
  1872. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1873. headers = { "foo": "bar" }
  1874. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1875. resp = conn.getresponse()
  1876. if resp.status != 412:
  1877. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1878. logger.debug("Valid unsubscription")
  1879. headers = { "sid": sid }
  1880. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1881. resp = conn.getresponse()
  1882. if resp.status != 200:
  1883. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1884. logger.debug("Unsubscription for not existing SID")
  1885. headers = { "sid": sid }
  1886. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1887. resp = conn.getresponse()
  1888. if resp.status != 412:
  1889. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1890. logger.debug("Invalid unsubscription")
  1891. headers = { "sid": " \t \tfoo" }
  1892. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1893. resp = conn.getresponse()
  1894. if resp.status != 400:
  1895. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1896. logger.debug("Invalid unsubscription")
  1897. headers = { "sid": "uuid:\t \tfoo" }
  1898. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1899. resp = conn.getresponse()
  1900. if resp.status != 400:
  1901. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1902. logger.debug("Invalid unsubscription")
  1903. headers = { "NT": "upnp:event",
  1904. "sid": sid }
  1905. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1906. resp = conn.getresponse()
  1907. if resp.status != 400:
  1908. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1909. headers = { "callback": '<http://127.0.0.1:12345/event>',
  1910. "sid": sid }
  1911. conn.request("UNSUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1912. resp = conn.getresponse()
  1913. if resp.status != 400:
  1914. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1915. logger.debug("Valid subscription with multiple callbacks")
  1916. headers = { "callback": '<http://127.0.0.1:12345/event> <http://127.0.0.1:12345/event>\t<http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event><http://127.0.0.1:12345/event>',
  1917. "NT": "upnp:event",
  1918. "timeout": "Second-1234" }
  1919. conn.request("SUBSCRIBE", eventurl.path, "\r\n\r\n", headers)
  1920. resp = conn.getresponse()
  1921. if resp.status != 200:
  1922. raise Exception("Unexpected HTTP response: %s" % resp.status)
  1923. sid = resp.getheader("sid")
  1924. logger.debug("Subscription SID " + sid)
  1925. def test_ap_wps_disabled(dev, apdev):
  1926. """WPS operations while WPS is disabled"""
  1927. ssid = "test-wps-disabled"
  1928. hostapd.add_ap(apdev[0]['ifname'], { "ssid": ssid })
  1929. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1930. if "FAIL" not in hapd.request("WPS_PBC"):
  1931. raise Exception("WPS_PBC succeeded unexpectedly")
  1932. if "FAIL" not in hapd.request("WPS_CANCEL"):
  1933. raise Exception("WPS_CANCEL succeeded unexpectedly")
  1934. def test_ap_wps_mixed_cred(dev, apdev):
  1935. """WPS 2.0 STA merging mixed mode WPA/WPA2 credentials"""
  1936. ssid = "test-wps-wep"
  1937. hostapd.add_ap(apdev[0]['ifname'],
  1938. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1939. "skip_cred_build": "1", "extra_cred": "wps-mixed-cred" })
  1940. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1941. hapd.request("WPS_PBC")
  1942. dev[0].request("WPS_PBC")
  1943. ev = dev[0].wait_event(["WPS-SUCCESS"], timeout=30)
  1944. if ev is None:
  1945. raise Exception("WPS-SUCCESS event timed out")
  1946. nets = dev[0].list_networks()
  1947. if len(nets) != 1:
  1948. raise Exception("Unexpected number of network blocks")
  1949. id = nets[0]['id']
  1950. proto = dev[0].get_network(id, "proto")
  1951. if proto != "WPA RSN":
  1952. raise Exception("Unexpected merged proto field value: " + proto)
  1953. pairwise = dev[0].get_network(id, "pairwise")
  1954. if pairwise != "CCMP TKIP" and pairwise != "CCMP GCMP TKIP":
  1955. raise Exception("Unexpected merged pairwise field value: " + pairwise)
  1956. def test_ap_wps_while_connected(dev, apdev):
  1957. """WPS PBC provisioning while connected to another AP"""
  1958. ssid = "test-wps-conf"
  1959. hostapd.add_ap(apdev[0]['ifname'],
  1960. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1961. "wpa_passphrase": "12345678", "wpa": "2",
  1962. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  1963. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1964. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
  1965. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  1966. logger.info("WPS provisioning step")
  1967. hapd.request("WPS_PBC")
  1968. dev[0].dump_monitor()
  1969. dev[0].request("WPS_PBC")
  1970. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1971. if ev is None:
  1972. raise Exception("Association with the AP timed out")
  1973. status = dev[0].get_status()
  1974. if status['bssid'] != apdev[0]['bssid']:
  1975. raise Exception("Unexpected BSSID")
  1976. def test_ap_wps_while_connected_no_autoconnect(dev, apdev):
  1977. """WPS PBC provisioning while connected to another AP and STA_AUTOCONNECT disabled"""
  1978. ssid = "test-wps-conf"
  1979. hostapd.add_ap(apdev[0]['ifname'],
  1980. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  1981. "wpa_passphrase": "12345678", "wpa": "2",
  1982. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  1983. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  1984. hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
  1985. try:
  1986. dev[0].request("STA_AUTOCONNECT 0")
  1987. dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
  1988. logger.info("WPS provisioning step")
  1989. hapd.request("WPS_PBC")
  1990. dev[0].dump_monitor()
  1991. dev[0].request("WPS_PBC")
  1992. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  1993. if ev is None:
  1994. raise Exception("Association with the AP timed out")
  1995. status = dev[0].get_status()
  1996. if status['bssid'] != apdev[0]['bssid']:
  1997. raise Exception("Unexpected BSSID")
  1998. finally:
  1999. dev[0].request("STA_AUTOCONNECT 1")
  2000. def test_ap_wps_from_event(dev, apdev):
  2001. """WPS PBC event on AP to enable PBC"""
  2002. ssid = "test-wps-conf"
  2003. hapd = hostapd.add_ap(apdev[0]['ifname'],
  2004. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2005. "wpa_passphrase": "12345678", "wpa": "2",
  2006. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  2007. dev[0].dump_monitor()
  2008. dev[0].request("WPS_PBC")
  2009. ev = hapd.wait_event(['WPS-ENROLLEE-SEEN'], timeout=15)
  2010. if ev is None:
  2011. raise Exception("No WPS-ENROLLEE-SEEN event on AP")
  2012. vals = ev.split(' ')
  2013. if vals[1] != dev[0].p2p_interface_addr():
  2014. raise Exception("Unexpected enrollee address: " + vals[1])
  2015. if vals[5] != '4':
  2016. raise Exception("Unexpected Device Password Id: " + vals[5])
  2017. hapd.request("WPS_PBC")
  2018. ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  2019. if ev is None:
  2020. raise Exception("Association with the AP timed out")
  2021. def test_ap_wps_ap_scan_2(dev, apdev):
  2022. """AP_SCAN 2 for WPS"""
  2023. ssid = "test-wps-conf"
  2024. hapd = hostapd.add_ap(apdev[0]['ifname'],
  2025. { "ssid": ssid, "eap_server": "1", "wps_state": "2",
  2026. "wpa_passphrase": "12345678", "wpa": "2",
  2027. "wpa_key_mgmt": "WPA-PSK", "rsn_pairwise": "CCMP"})
  2028. hapd.request("WPS_PBC")
  2029. wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
  2030. wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
  2031. if "OK" not in wpas.request("AP_SCAN 2"):
  2032. raise Exception("Failed to set AP_SCAN 2")
  2033. wpas.request("WPS_PBC")
  2034. ev = wpas.wait_event(["WPS-SUCCESS"], timeout=15)
  2035. if ev is None:
  2036. raise Exception("WPS-SUCCESS event timed out")
  2037. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  2038. if ev is None:
  2039. raise Exception("Association with the AP timed out")
  2040. wpas.request("DISCONNECT")
  2041. wpas.request("BSS_FLUSH 0")
  2042. wpas.dump_monitor()
  2043. wpas.request("REASSOCIATE")
  2044. ev = wpas.wait_event(["CTRL-EVENT-CONNECTED"], timeout=30)
  2045. if ev is None:
  2046. raise Exception("Association with the AP timed out")