test_ap_wps.py 113 KB

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