test_ap_wps.py 96 KB

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