test_ap_hs20.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #!/usr/bin/python
  2. #
  3. # Hotspot 2.0 tests
  4. # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import subprocess
  10. import logging
  11. logger = logging.getLogger()
  12. import os.path
  13. import subprocess
  14. import hostapd
  15. def hs20_ap_params():
  16. params = hostapd.wpa2_params(ssid="test-hs20")
  17. params['wpa_key_mgmt'] = "WPA-EAP"
  18. params['ieee80211w'] = "1"
  19. params['ieee8021x'] = "1"
  20. params['auth_server_addr'] = "127.0.0.1"
  21. params['auth_server_port'] = "1812"
  22. params['auth_server_shared_secret'] = "radius"
  23. params['interworking'] = "1"
  24. params['access_network_type'] = "14"
  25. params['internet'] = "1"
  26. params['asra'] = "0"
  27. params['esr'] = "0"
  28. params['uesa'] = "0"
  29. params['venue_group'] = "7"
  30. params['venue_type'] = "1"
  31. params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
  32. params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
  33. "fedcba" ]
  34. params['domain_name'] = "example.com,another.example.com"
  35. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  36. "0,another.example.com" ]
  37. params['hs20'] = "1"
  38. params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
  39. params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
  40. params['hs20_operating_class'] = "5173"
  41. params['anqp_3gpp_cell_net'] = "244,91"
  42. return params
  43. def interworking_select(dev, bssid, type=None, no_match=False):
  44. dev.dump_monitor()
  45. dev.request("INTERWORKING_SELECT")
  46. ev = dev.wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH"],
  47. timeout=15)
  48. if ev is None:
  49. raise Exception("Network selection timed out");
  50. if no_match:
  51. if "INTERWORKING-NO-MATCH" not in ev:
  52. raise Exception("Unexpected network match")
  53. return
  54. if "INTERWORKING-NO-MATCH" in ev:
  55. raise Exception("Matching network not found")
  56. if bssid not in ev:
  57. raise Exception("Unexpected BSSID in match")
  58. if type and "type=" + type not in ev:
  59. raise Exception("Network type not recognized correctly")
  60. def check_sp_type(dev, sp_type):
  61. type = dev.get_status_field("sp_type")
  62. if type is None:
  63. raise Exception("sp_type not available")
  64. if type != sp_type:
  65. raise Exception("sp_type did not indicate home network")
  66. def hlr_auc_gw_available():
  67. if not os.path.exists("/tmp/hlr_auc_gw.sock"):
  68. logger.info("No hlr_auc_gw available");
  69. return False
  70. if not os.path.exists("../../hostapd/hlr_auc_gw"):
  71. logger.info("No hlr_auc_gw available");
  72. return False
  73. return True
  74. def interworking_ext_sim_connect(dev, bssid, method):
  75. dev.request("INTERWORKING_CONNECT " + bssid)
  76. ev = dev.wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
  77. if ev is None:
  78. raise Exception("Network connected timed out")
  79. if "(" + method + ")" not in ev:
  80. raise Exception("Unexpected EAP method selection")
  81. ev = dev.wait_event(["CTRL-REQ-SIM"], timeout=15)
  82. if ev is None:
  83. raise Exception("Wait for external SIM processing request timed out")
  84. p = ev.split(':', 2)
  85. if p[1] != "GSM-AUTH":
  86. raise Exception("Unexpected CTRL-REQ-SIM type")
  87. id = p[0].split('-')[3]
  88. rand = p[2].split(' ')[0]
  89. res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
  90. "-m",
  91. "auth_serv/hlr_auc_gw.milenage_db",
  92. "GSM-AUTH-REQ 232010000000000 " + rand])
  93. if "GSM-AUTH-RESP" not in res:
  94. raise Exception("Unexpected hlr_auc_gw response")
  95. resp = res.split(' ')[2].rstrip()
  96. dev.request("CTRL-RSP-SIM-" + id + ":GSM-AUTH:" + resp)
  97. ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  98. if ev is None:
  99. raise Exception("Connection timed out")
  100. def interworking_connect(dev, bssid, method):
  101. dev.request("INTERWORKING_CONNECT " + bssid)
  102. ev = dev.wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
  103. if ev is None:
  104. raise Exception("Network connected timed out")
  105. if "(" + method + ")" not in ev:
  106. raise Exception("Unexpected EAP method selection")
  107. ev = dev.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  108. if ev is None:
  109. raise Exception("Connection timed out")
  110. def test_ap_hs20_select(dev, apdev):
  111. """Hotspot 2.0 network selection"""
  112. bssid = apdev[0]['bssid']
  113. params = hs20_ap_params()
  114. params['hessid'] = bssid
  115. hostapd.add_ap(apdev[0]['ifname'], params)
  116. dev[0].request("SET ignore_old_scan_res 1")
  117. dev[0].hs20_enable()
  118. id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
  119. 'password': "secret",
  120. 'domain': "example.com" })
  121. interworking_select(dev[0], bssid, "home")
  122. dev[0].remove_cred(id)
  123. id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
  124. 'password': "secret",
  125. 'domain': "no.match.example.com" })
  126. interworking_select(dev[0], bssid, "roaming")
  127. dev[0].set_cred_quoted(id, "realm", "no.match.example.com");
  128. interworking_select(dev[0], bssid, no_match=True)
  129. def test_ap_hs20_ext_sim(dev, apdev):
  130. """Hotspot 2.0 with external SIM processing"""
  131. if not hlr_auc_gw_available():
  132. return "skip"
  133. bssid = apdev[0]['bssid']
  134. params = hs20_ap_params()
  135. params['hessid'] = bssid
  136. params['anqp_3gpp_cell_net'] = "232,01"
  137. params['domain_name'] = "wlan.mnc001.mcc232.3gppnetwork.org"
  138. hostapd.add_ap(apdev[0]['ifname'], params)
  139. dev[0].request("SET ignore_old_scan_res 1")
  140. dev[0].hs20_enable()
  141. dev[0].request("SET external_sim 1")
  142. dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM" })
  143. interworking_select(dev[0], "home")
  144. interworking_ext_sim_connect(dev[0], bssid, "SIM")
  145. check_sp_type(dev[0], "home")
  146. def test_ap_hs20_ext_sim_roaming(dev, apdev):
  147. """Hotspot 2.0 with external SIM processing in roaming network"""
  148. if not hlr_auc_gw_available():
  149. return "skip"
  150. bssid = apdev[0]['bssid']
  151. params = hs20_ap_params()
  152. params['hessid'] = bssid
  153. params['anqp_3gpp_cell_net'] = "244,91;310,026;232,01;234,56"
  154. params['domain_name'] = "wlan.mnc091.mcc244.3gppnetwork.org"
  155. hostapd.add_ap(apdev[0]['ifname'], params)
  156. dev[0].request("SET ignore_old_scan_res 1")
  157. dev[0].hs20_enable()
  158. dev[0].request("SET external_sim 1")
  159. dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM" })
  160. interworking_select(dev[0], "roaming")
  161. interworking_ext_sim_connect(dev[0], bssid, "SIM")
  162. check_sp_type(dev[0], "roaming")
  163. def test_ap_hs20_username(dev, apdev):
  164. """Hotspot 2.0 connection in username/password credential"""
  165. bssid = apdev[0]['bssid']
  166. params = hs20_ap_params()
  167. params['hessid'] = bssid
  168. hostapd.add_ap(apdev[0]['ifname'], params)
  169. dev[0].request("SET ignore_old_scan_res 1")
  170. dev[0].hs20_enable()
  171. id = dev[0].add_cred_values({ 'realm': "example.com",
  172. 'username': "hs20-test",
  173. 'password': "password",
  174. 'domain': "example.com" })
  175. interworking_select(dev[0], bssid, "home")
  176. interworking_connect(dev[0], bssid, "TTLS")
  177. check_sp_type(dev[0], "home")
  178. def test_ap_hs20_username_roaming(dev, apdev):
  179. """Hotspot 2.0 connection in username/password credential (roaming)"""
  180. bssid = apdev[0]['bssid']
  181. params = hs20_ap_params()
  182. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  183. "0,roaming.example.com,21[2:4][5:7]",
  184. "0,another.example.com" ]
  185. params['domain_name'] = "another.example.com"
  186. params['hessid'] = bssid
  187. hostapd.add_ap(apdev[0]['ifname'], params)
  188. dev[0].request("SET ignore_old_scan_res 1")
  189. dev[0].hs20_enable()
  190. id = dev[0].add_cred_values({ 'realm': "roaming.example.com",
  191. 'username': "hs20-test",
  192. 'password': "password",
  193. 'domain': "example.com" })
  194. interworking_select(dev[0], bssid, "roaming")
  195. interworking_connect(dev[0], bssid, "TTLS")
  196. check_sp_type(dev[0], "roaming")
  197. def test_ap_hs20_username_unknown(dev, apdev):
  198. """Hotspot 2.0 connection in username/password credential (no domain in cred)"""
  199. bssid = apdev[0]['bssid']
  200. params = hs20_ap_params()
  201. params['hessid'] = bssid
  202. hostapd.add_ap(apdev[0]['ifname'], params)
  203. dev[0].request("SET ignore_old_scan_res 1")
  204. dev[0].hs20_enable()
  205. id = dev[0].add_cred_values({ 'realm': "example.com",
  206. 'username': "hs20-test",
  207. 'password': "password" })
  208. interworking_select(dev[0], bssid, "unknown")
  209. interworking_connect(dev[0], bssid, "TTLS")
  210. check_sp_type(dev[0], "unknown")
  211. def test_ap_hs20_username_unknown2(dev, apdev):
  212. """Hotspot 2.0 connection in username/password credential (no domain advertized)"""
  213. bssid = apdev[0]['bssid']
  214. params = hs20_ap_params()
  215. params['hessid'] = bssid
  216. del params['domain_name']
  217. hostapd.add_ap(apdev[0]['ifname'], params)
  218. dev[0].request("SET ignore_old_scan_res 1")
  219. dev[0].hs20_enable()
  220. id = dev[0].add_cred_values({ 'realm': "example.com",
  221. 'username': "hs20-test",
  222. 'password': "password",
  223. 'domain': "example.com" })
  224. interworking_select(dev[0], bssid, "unknown")
  225. interworking_connect(dev[0], bssid, "TTLS")
  226. check_sp_type(dev[0], "unknown")
  227. def test_ap_hs20_gas_while_associated(dev, apdev):
  228. """Hotspot 2.0 connection with GAS query while associated"""
  229. bssid = apdev[0]['bssid']
  230. params = hs20_ap_params()
  231. params['hessid'] = bssid
  232. hostapd.add_ap(apdev[0]['ifname'], params)
  233. dev[0].request("SET ignore_old_scan_res 1")
  234. dev[0].hs20_enable()
  235. id = dev[0].add_cred_values({ 'realm': "example.com",
  236. 'username': "hs20-test",
  237. 'password': "password",
  238. 'domain': "example.com" })
  239. interworking_select(dev[0], bssid, "home")
  240. interworking_connect(dev[0], bssid, "TTLS")
  241. logger.info("Verifying GAS query while associated")
  242. dev[0].request("FETCH_ANQP")
  243. for i in range(0, 6):
  244. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  245. if ev is None:
  246. raise Exception("Operation timed out")
  247. def test_ap_hs20_gas_frag_while_associated(dev, apdev):
  248. """Hotspot 2.0 connection with fragmented GAS query while associated"""
  249. bssid = apdev[0]['bssid']
  250. params = hs20_ap_params()
  251. params['hessid'] = bssid
  252. hostapd.add_ap(apdev[0]['ifname'], params)
  253. hapd = hostapd.Hostapd(apdev[0]['ifname'])
  254. hapd.set("gas_frag_limit", "50")
  255. dev[0].request("SET ignore_old_scan_res 1")
  256. dev[0].hs20_enable()
  257. id = dev[0].add_cred_values({ 'realm': "example.com",
  258. 'username': "hs20-test",
  259. 'password': "password",
  260. 'domain': "example.com" })
  261. interworking_select(dev[0], bssid, "home")
  262. interworking_connect(dev[0], bssid, "TTLS")
  263. logger.info("Verifying GAS query while associated")
  264. dev[0].request("FETCH_ANQP")
  265. for i in range(0, 6):
  266. ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
  267. if ev is None:
  268. raise Exception("Operation timed out")
  269. def test_ap_hs20_multiple_connects(dev, apdev):
  270. """Hotspot 2.0 connection through multiple network selections"""
  271. bssid = apdev[0]['bssid']
  272. params = hs20_ap_params()
  273. params['hessid'] = bssid
  274. hostapd.add_ap(apdev[0]['ifname'], params)
  275. dev[0].request("SET ignore_old_scan_res 1")
  276. dev[0].hs20_enable()
  277. values = { 'realm': "example.com",
  278. 'username': "hs20-test",
  279. 'password': "password",
  280. 'domain': "example.com" }
  281. id = dev[0].add_cred_values(values)
  282. for i in range(0, 3):
  283. logger.info("Starting Interworking network selection")
  284. dev[0].request("INTERWORKING_SELECT auto")
  285. while True:
  286. ev = dev[0].wait_event(["INTERWORKING-NO-MATCH",
  287. "INTERWORKING-ALREADY-CONNECTED",
  288. "CTRL-EVENT-CONNECTED"], timeout=15)
  289. if ev is None:
  290. raise Exception("Connection timed out")
  291. if "INTERWORKING-NO-MATCH" in ev:
  292. raise Exception("Matching AP not found")
  293. if "CTRL-EVENT-CONNECTED" in ev:
  294. break
  295. if i == 2 and "INTERWORKING-ALREADY-CONNECTED" in ev:
  296. break
  297. if i == 0:
  298. dev[0].request("DISCONNECT")
  299. dev[0].dump_monitor()
  300. networks = dev[0].list_networks()
  301. if len(networks) > 1:
  302. raise Exception("Duplicated network block detected")
  303. def test_ap_hs20_disallow_aps(dev, apdev):
  304. """Hotspot 2.0 connection and disallow_aps"""
  305. bssid = apdev[0]['bssid']
  306. params = hs20_ap_params()
  307. params['hessid'] = bssid
  308. hostapd.add_ap(apdev[0]['ifname'], params)
  309. dev[0].request("SET ignore_old_scan_res 1")
  310. dev[0].hs20_enable()
  311. values = { 'realm': "example.com",
  312. 'username': "hs20-test",
  313. 'password': "password",
  314. 'domain': "example.com" }
  315. id = dev[0].add_cred_values(values)
  316. logger.info("Verify disallow_aps bssid")
  317. dev[0].request("SET disallow_aps bssid " + bssid.translate(None, ':'))
  318. dev[0].request("INTERWORKING_SELECT auto")
  319. ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=15)
  320. if ev is None:
  321. raise Exception("Network selection timed out")
  322. dev[0].dump_monitor()
  323. logger.info("Verify disallow_aps ssid")
  324. dev[0].request("SET disallow_aps ssid 746573742d68733230")
  325. dev[0].request("INTERWORKING_SELECT auto")
  326. ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=15)
  327. if ev is None:
  328. raise Exception("Network selection timed out")
  329. dev[0].dump_monitor()
  330. logger.info("Verify disallow_aps clear")
  331. dev[0].request("SET disallow_aps ")
  332. interworking_select(dev[0], bssid, "home")
  333. dev[0].request("SET disallow_aps bssid " + bssid.translate(None, ':'))
  334. ret = dev[0].request("INTERWORKING_CONNECT " + bssid)
  335. if "FAIL" not in ret:
  336. raise Exception("INTERWORKING_CONNECT to disallowed BSS not rejected")
  337. def policy_test(dev, ap, values, only_one=True):
  338. dev.dump_monitor()
  339. logger.info("Verify network selection to AP " + ap['ifname'])
  340. bssid = ap['bssid']
  341. dev.request("SET ignore_old_scan_res 1")
  342. dev.hs20_enable()
  343. id = dev.add_cred_values(values)
  344. dev.request("INTERWORKING_SELECT auto")
  345. while True:
  346. ev = dev.wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH",
  347. "CTRL-EVENT-CONNECTED"], timeout=15)
  348. if ev is None:
  349. raise Exception("Connection timed out")
  350. if "INTERWORKING-NO-MATCH" in ev:
  351. raise Exception("Matching AP not found")
  352. if only_one and "INTERWORKING-AP" in ev and bssid not in ev:
  353. raise Exception("Unexpected AP claimed acceptable")
  354. if "CTRL-EVENT-CONNECTED" in ev:
  355. if bssid not in ev:
  356. raise Exception("Connected to incorrect BSS")
  357. break
  358. conn_bssid = dev.get_status_field("bssid")
  359. if conn_bssid != bssid:
  360. raise Exception("bssid information points to incorrect BSS")
  361. dev.remove_cred(id)
  362. dev.dump_monitor()
  363. def default_cred():
  364. return { 'realm': "example.com",
  365. 'username': "hs20-test",
  366. 'password': "password" }
  367. def test_ap_hs20_req_roaming_consortium(dev, apdev):
  368. """Hotspot 2.0 required roaming consortium"""
  369. params = hs20_ap_params()
  370. hostapd.add_ap(apdev[0]['ifname'], params)
  371. params = hs20_ap_params()
  372. params['ssid'] = "test-hs20-other"
  373. params['roaming_consortium'] = [ "223344" ]
  374. hostapd.add_ap(apdev[1]['ifname'], params)
  375. values = default_cred()
  376. values['required_roaming_consortium'] = "223344"
  377. policy_test(dev[0], apdev[1], values)
  378. values['required_roaming_consortium'] = "112233"
  379. policy_test(dev[0], apdev[0], values)
  380. def test_ap_hs20_excluded_ssid(dev, apdev):
  381. """Hotspot 2.0 exclusion based on SSID"""
  382. params = hs20_ap_params()
  383. hostapd.add_ap(apdev[0]['ifname'], params)
  384. params = hs20_ap_params()
  385. params['ssid'] = "test-hs20-other"
  386. params['roaming_consortium'] = [ "223344" ]
  387. hostapd.add_ap(apdev[1]['ifname'], params)
  388. values = default_cred()
  389. values['excluded_ssid'] = "test-hs20"
  390. policy_test(dev[0], apdev[1], values)
  391. values['excluded_ssid'] = "test-hs20-other"
  392. policy_test(dev[0], apdev[0], values)