test_ap_wps.py 85 KB

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