test_ap_wps.py 72 KB

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