test_ap_wps.py 98 KB

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