test_ap_wps.py 114 KB

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