test_ap_wps.py 108 KB

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