test_ap_wps.py 99 KB

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