test_ap_wps.py 113 KB

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