test_ap_wps.py 91 KB

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