test_ap_wps.py 108 KB

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