test_ap_wps.py 100 KB

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