test_ap_wps.py 96 KB

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