test_ap_hs20.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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].hs20_enable()
  117. id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
  118. 'password': "secret",
  119. 'domain': "example.com" })
  120. interworking_select(dev[0], bssid, "home")
  121. dev[0].remove_cred(id)
  122. id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
  123. 'password': "secret",
  124. 'domain': "no.match.example.com" })
  125. interworking_select(dev[0], bssid, "roaming")
  126. dev[0].set_cred_quoted(id, "realm", "no.match.example.com");
  127. interworking_select(dev[0], bssid, no_match=True)
  128. def test_ap_hs20_ext_sim(dev, apdev):
  129. """Hotspot 2.0 with external SIM processing"""
  130. if not hlr_auc_gw_available():
  131. return "skip"
  132. bssid = apdev[0]['bssid']
  133. params = hs20_ap_params()
  134. params['hessid'] = bssid
  135. params['anqp_3gpp_cell_net'] = "232,01"
  136. params['domain_name'] = "wlan.mnc001.mcc232.3gppnetwork.org"
  137. hostapd.add_ap(apdev[0]['ifname'], params)
  138. dev[0].hs20_enable()
  139. dev[0].request("SET external_sim 1")
  140. dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM" })
  141. interworking_select(dev[0], "home")
  142. interworking_ext_sim_connect(dev[0], bssid, "SIM")
  143. check_sp_type(dev[0], "home")
  144. def test_ap_hs20_ext_sim_roaming(dev, apdev):
  145. """Hotspot 2.0 with external SIM processing in roaming network"""
  146. if not hlr_auc_gw_available():
  147. return "skip"
  148. bssid = apdev[0]['bssid']
  149. params = hs20_ap_params()
  150. params['hessid'] = bssid
  151. params['anqp_3gpp_cell_net'] = "244,91;310,026;232,01;234,56"
  152. params['domain_name'] = "wlan.mnc091.mcc244.3gppnetwork.org"
  153. hostapd.add_ap(apdev[0]['ifname'], params)
  154. dev[0].hs20_enable()
  155. dev[0].request("SET external_sim 1")
  156. dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM" })
  157. interworking_select(dev[0], "roaming")
  158. interworking_ext_sim_connect(dev[0], bssid, "SIM")
  159. check_sp_type(dev[0], "roaming")
  160. def test_ap_hs20_username(dev, apdev):
  161. """Hotspot 2.0 connection in username/password credential"""
  162. bssid = apdev[0]['bssid']
  163. params = hs20_ap_params()
  164. params['hessid'] = bssid
  165. hostapd.add_ap(apdev[0]['ifname'], params)
  166. dev[0].hs20_enable()
  167. id = dev[0].add_cred_values({ 'realm': "example.com",
  168. 'username': "hs20-test",
  169. 'password': "password",
  170. 'domain': "example.com" })
  171. interworking_select(dev[0], bssid, "home")
  172. interworking_connect(dev[0], bssid, "TTLS")
  173. check_sp_type(dev[0], "home")
  174. def test_ap_hs20_username_roaming(dev, apdev):
  175. """Hotspot 2.0 connection in username/password credential (roaming)"""
  176. bssid = apdev[0]['bssid']
  177. params = hs20_ap_params()
  178. params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
  179. "0,roaming.example.com,21[2:4][5:7]",
  180. "0,another.example.com" ]
  181. params['domain_name'] = "another.example.com"
  182. params['hessid'] = bssid
  183. hostapd.add_ap(apdev[0]['ifname'], params)
  184. dev[0].hs20_enable()
  185. id = dev[0].add_cred_values({ 'realm': "roaming.example.com",
  186. 'username': "hs20-test",
  187. 'password': "password",
  188. 'domain': "example.com" })
  189. interworking_select(dev[0], bssid, "roaming")
  190. interworking_connect(dev[0], bssid, "TTLS")
  191. check_sp_type(dev[0], "roaming")
  192. def test_ap_hs20_username_unknown(dev, apdev):
  193. """Hotspot 2.0 connection in username/password credential (no domain in cred)"""
  194. bssid = apdev[0]['bssid']
  195. params = hs20_ap_params()
  196. params['hessid'] = bssid
  197. hostapd.add_ap(apdev[0]['ifname'], params)
  198. dev[0].hs20_enable()
  199. id = dev[0].add_cred_values({ 'realm': "example.com",
  200. 'username': "hs20-test",
  201. 'password': "password" })
  202. interworking_select(dev[0], bssid, "unknown")
  203. interworking_connect(dev[0], bssid, "TTLS")
  204. check_sp_type(dev[0], "unknown")
  205. def test_ap_hs20_username_unknown2(dev, apdev):
  206. """Hotspot 2.0 connection in username/password credential (no domain advertized)"""
  207. bssid = apdev[0]['bssid']
  208. params = hs20_ap_params()
  209. params['hessid'] = bssid
  210. del params['domain_name']
  211. hostapd.add_ap(apdev[0]['ifname'], params)
  212. dev[0].hs20_enable()
  213. id = dev[0].add_cred_values({ 'realm': "example.com",
  214. 'username': "hs20-test",
  215. 'password': "password",
  216. 'domain': "example.com" })
  217. interworking_select(dev[0], bssid, "unknown")
  218. interworking_connect(dev[0], bssid, "TTLS")
  219. check_sp_type(dev[0], "unknown")
  220. def test_ap_hs20_multiple_connects(dev, apdev):
  221. """Hotspot 2.0 connection through multiple network selections"""
  222. bssid = apdev[0]['bssid']
  223. params = hs20_ap_params()
  224. params['hessid'] = bssid
  225. hostapd.add_ap(apdev[0]['ifname'], params)
  226. dev[0].hs20_enable()
  227. values = { 'realm': "example.com",
  228. 'username': "hs20-test",
  229. 'password': "password",
  230. 'domain': "example.com" }
  231. id = dev[0].add_cred_values(values)
  232. for i in range(0, 3):
  233. logger.info("Starting Interworking network selection")
  234. dev[0].request("INTERWORKING_SELECT auto")
  235. while True:
  236. ev = dev[0].wait_event(["INTERWORKING-NO-MATCH",
  237. "INTERWORKING-ALREADY-CONNECTED",
  238. "CTRL-EVENT-CONNECTED"], timeout=15)
  239. if ev is None:
  240. raise Exception("Connection timed out")
  241. if "INTERWORKING-NO-MATCH" in ev:
  242. raise Exception("Matching AP not found")
  243. if "CTRL-EVENT-CONNECTED" in ev:
  244. break
  245. if i == 2 and "INTERWORKING-ALREADY-CONNECTED" in ev:
  246. break
  247. if i == 0:
  248. dev[0].request("DISCONNECT")
  249. dev[0].dump_monitor()
  250. networks = dev[0].list_networks()
  251. if len(networks) > 1:
  252. raise Exception("Duplicated network block detected")
  253. def test_ap_hs20_disallow_aps(dev, apdev):
  254. """Hotspot 2.0 connection and disallow_aps"""
  255. bssid = apdev[0]['bssid']
  256. params = hs20_ap_params()
  257. params['hessid'] = bssid
  258. hostapd.add_ap(apdev[0]['ifname'], params)
  259. dev[0].hs20_enable()
  260. values = { 'realm': "example.com",
  261. 'username': "hs20-test",
  262. 'password': "password",
  263. 'domain': "example.com" }
  264. id = dev[0].add_cred_values(values)
  265. logger.info("Verify disallow_aps bssid")
  266. dev[0].request("SET disallow_aps bssid " + bssid.translate(None, ':'))
  267. dev[0].request("INTERWORKING_SELECT auto")
  268. ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=15)
  269. if ev is None:
  270. raise Exception("Network selection timed out")
  271. dev[0].dump_monitor()
  272. logger.info("Verify disallow_aps ssid")
  273. dev[0].request("SET disallow_aps ssid 746573742d68733230")
  274. dev[0].request("INTERWORKING_SELECT auto")
  275. ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=15)
  276. if ev is None:
  277. raise Exception("Network selection timed out")
  278. dev[0].dump_monitor()
  279. logger.info("Verify disallow_aps clear")
  280. dev[0].request("SET disallow_aps ")
  281. interworking_select(dev[0], bssid, "home")
  282. dev[0].request("SET disallow_aps bssid " + bssid.translate(None, ':'))
  283. ret = dev[0].request("INTERWORKING_CONNECT " + bssid)
  284. if "FAIL" not in ret:
  285. raise Exception("INTERWORKING_CONNECT to disallowed BSS not rejected")
  286. def policy_test(dev, ap, values, only_one=True):
  287. dev.dump_monitor()
  288. logger.info("Verify network selection to AP " + ap['ifname'])
  289. bssid = ap['bssid']
  290. dev.hs20_enable()
  291. id = dev.add_cred_values(values)
  292. dev.request("INTERWORKING_SELECT auto")
  293. while True:
  294. ev = dev.wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH",
  295. "CTRL-EVENT-CONNECTED"], timeout=15)
  296. if ev is None:
  297. raise Exception("Connection timed out")
  298. if "INTERWORKING-NO-MATCH" in ev:
  299. raise Exception("Matching AP not found")
  300. if only_one and "INTERWORKING-AP" in ev and bssid not in ev:
  301. raise Exception("Unexpected AP claimed acceptable")
  302. if "CTRL-EVENT-CONNECTED" in ev:
  303. if bssid not in ev:
  304. raise Exception("Connected to incorrect BSS")
  305. break
  306. conn_bssid = dev.get_status_field("bssid")
  307. if conn_bssid != bssid:
  308. raise Exception("bssid information points to incorrect BSS")
  309. dev.remove_cred(id)
  310. dev.dump_monitor()
  311. def default_cred():
  312. return { 'realm': "example.com",
  313. 'username': "hs20-test",
  314. 'password': "password" }
  315. def test_ap_hs20_req_roaming_consortium(dev, apdev):
  316. """Hotspot 2.0 required roaming consortium"""
  317. params = hs20_ap_params()
  318. hostapd.add_ap(apdev[0]['ifname'], params)
  319. params = hs20_ap_params()
  320. params['ssid'] = "test-hs20-other"
  321. params['roaming_consortium'] = [ "223344" ]
  322. hostapd.add_ap(apdev[1]['ifname'], params)
  323. values = default_cred()
  324. values['required_roaming_consortium'] = "223344"
  325. policy_test(dev[0], apdev[1], values)
  326. values['required_roaming_consortium'] = "112233"
  327. policy_test(dev[0], apdev[0], values)
  328. def test_ap_hs20_excluded_ssid(dev, apdev):
  329. """Hotspot 2.0 exclusion based on SSID"""
  330. params = hs20_ap_params()
  331. hostapd.add_ap(apdev[0]['ifname'], params)
  332. params = hs20_ap_params()
  333. params['ssid'] = "test-hs20-other"
  334. params['roaming_consortium'] = [ "223344" ]
  335. hostapd.add_ap(apdev[1]['ifname'], params)
  336. values = default_cred()
  337. values['excluded_ssid'] = "test-hs20"
  338. policy_test(dev[0], apdev[1], values)
  339. values['excluded_ssid'] = "test-hs20-other"
  340. policy_test(dev[0], apdev[0], values)