test_ap_wps.py 87 KB

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