test_ap_wps.py 96 KB

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