tests: Fix interworking_select() bssid argument in some test cases
[mech_eap.git] / tests / hwsim / test_ap_hs20.py
1 # Hotspot 2.0 tests
2 # Copyright (c) 2013-2015, 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
7 from remotehost import remote_compatible
8 import base64
9 import binascii
10 import struct
11 import time
12 import logging
13 logger = logging.getLogger()
14 import os
15 import os.path
16 import socket
17 import subprocess
18
19 import hostapd
20 from utils import HwsimSkip, skip_with_fips, alloc_fail, wait_fail_trigger
21 import hwsim_utils
22 from tshark import run_tshark
23 from wlantest import Wlantest
24 from wpasupplicant import WpaSupplicant
25 from test_ap_eap import check_eap_capa, check_domain_match_full
26
27 def hs20_ap_params(ssid="test-hs20"):
28     params = hostapd.wpa2_params(ssid=ssid)
29     params['wpa_key_mgmt'] = "WPA-EAP"
30     params['ieee80211w'] = "1"
31     params['ieee8021x'] = "1"
32     params['auth_server_addr'] = "127.0.0.1"
33     params['auth_server_port'] = "1812"
34     params['auth_server_shared_secret'] = "radius"
35     params['interworking'] = "1"
36     params['access_network_type'] = "14"
37     params['internet'] = "1"
38     params['asra'] = "0"
39     params['esr'] = "0"
40     params['uesa'] = "0"
41     params['venue_group'] = "7"
42     params['venue_type'] = "1"
43     params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
44     params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
45                                      "fedcba" ]
46     params['domain_name'] = "example.com,another.example.com"
47     params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
48                             "0,another.example.com" ]
49     params['hs20'] = "1"
50     params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
51     params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
52     params['hs20_operating_class'] = "5173"
53     params['anqp_3gpp_cell_net'] = "244,91"
54     return params
55
56 def check_auto_select(dev, bssid):
57     dev.scan_for_bss(bssid, freq="2412")
58     dev.request("INTERWORKING_SELECT auto freq=2412")
59     ev = dev.wait_connected(timeout=15)
60     if bssid not in ev:
61         raise Exception("Connected to incorrect network")
62     dev.request("REMOVE_NETWORK all")
63     dev.wait_disconnected()
64     dev.dump_monitor()
65
66 def interworking_select(dev, bssid, type=None, no_match=False, freq=None):
67     dev.dump_monitor()
68     if bssid and freq and not no_match:
69         dev.scan_for_bss(bssid, freq=freq)
70     freq_extra = " freq=" + str(freq) if freq else ""
71     dev.request("INTERWORKING_SELECT" + freq_extra)
72     ev = dev.wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH"],
73                         timeout=15)
74     if ev is None:
75         raise Exception("Network selection timed out")
76     if no_match:
77         if "INTERWORKING-NO-MATCH" not in ev:
78             raise Exception("Unexpected network match")
79         return
80     if "INTERWORKING-NO-MATCH" in ev:
81         logger.info("Matching network not found - try again")
82         dev.dump_monitor()
83         dev.request("INTERWORKING_SELECT" + freq_extra)
84         ev = dev.wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH"],
85                             timeout=15)
86         if ev is None:
87             raise Exception("Network selection timed out")
88         if "INTERWORKING-NO-MATCH" in ev:
89             raise Exception("Matching network not found")
90     if bssid and bssid not in ev:
91         raise Exception("Unexpected BSSID in match")
92     if type and "type=" + type not in ev:
93         raise Exception("Network type not recognized correctly")
94
95 def check_sp_type(dev, sp_type):
96     type = dev.get_status_field("sp_type")
97     if type is None:
98         raise Exception("sp_type not available")
99     if type != sp_type:
100         raise Exception("sp_type did not indicate home network")
101
102 def hlr_auc_gw_available():
103     if not os.path.exists("/tmp/hlr_auc_gw.sock"):
104         raise HwsimSkip("No hlr_auc_gw socket available")
105     if not os.path.exists("../../hostapd/hlr_auc_gw"):
106         raise HwsimSkip("No hlr_auc_gw available")
107
108 def interworking_ext_sim_connect(dev, bssid, method):
109     dev.request("INTERWORKING_CONNECT " + bssid)
110     interworking_ext_sim_auth(dev, method)
111
112 def interworking_ext_sim_auth(dev, method):
113     ev = dev.wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
114     if ev is None:
115         raise Exception("Network connected timed out")
116     if "(" + method + ")" not in ev:
117         raise Exception("Unexpected EAP method selection")
118
119     ev = dev.wait_event(["CTRL-REQ-SIM"], timeout=15)
120     if ev is None:
121         raise Exception("Wait for external SIM processing request timed out")
122     p = ev.split(':', 2)
123     if p[1] != "GSM-AUTH":
124         raise Exception("Unexpected CTRL-REQ-SIM type")
125     id = p[0].split('-')[3]
126     rand = p[2].split(' ')[0]
127
128     res = subprocess.check_output(["../../hostapd/hlr_auc_gw",
129                                    "-m",
130                                    "auth_serv/hlr_auc_gw.milenage_db",
131                                    "GSM-AUTH-REQ 232010000000000 " + rand])
132     if "GSM-AUTH-RESP" not in res:
133         raise Exception("Unexpected hlr_auc_gw response")
134     resp = res.split(' ')[2].rstrip()
135
136     dev.request("CTRL-RSP-SIM-" + id + ":GSM-AUTH:" + resp)
137     dev.wait_connected(timeout=15)
138
139 def interworking_connect(dev, bssid, method):
140     dev.request("INTERWORKING_CONNECT " + bssid)
141     interworking_auth(dev, method)
142
143 def interworking_auth(dev, method):
144     ev = dev.wait_event(["CTRL-EVENT-EAP-METHOD"], timeout=15)
145     if ev is None:
146         raise Exception("Network connected timed out")
147     if "(" + method + ")" not in ev:
148         raise Exception("Unexpected EAP method selection")
149
150     dev.wait_connected(timeout=15)
151
152 def check_probe_resp(wt, bssid_unexpected, bssid_expected):
153     if bssid_unexpected:
154         count = wt.get_bss_counter("probe_response", bssid_unexpected)
155         if count > 0:
156             raise Exception("Unexpected Probe Response frame from AP")
157
158     if bssid_expected:
159         count = wt.get_bss_counter("probe_response", bssid_expected)
160         if count == 0:
161             raise Exception("No Probe Response frame from AP")
162
163 def test_ap_anqp_sharing(dev, apdev):
164     """ANQP sharing within ESS and explicit unshare"""
165     check_eap_capa(dev[0], "MSCHAPV2")
166     dev[0].flush_scan_cache()
167
168     bssid = apdev[0]['bssid']
169     params = hs20_ap_params()
170     params['hessid'] = bssid
171     hostapd.add_ap(apdev[0], params)
172
173     bssid2 = apdev[1]['bssid']
174     params = hs20_ap_params()
175     params['hessid'] = bssid
176     params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]" ]
177     hostapd.add_ap(apdev[1], params)
178
179     dev[0].hs20_enable()
180     id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
181                                   'password': "secret",
182                                   'domain': "example.com" })
183     logger.info("Normal network selection with shared ANQP results")
184     dev[0].scan_for_bss(bssid, freq="2412")
185     dev[0].scan_for_bss(bssid2, freq="2412")
186     interworking_select(dev[0], None, "home", freq="2412")
187     dev[0].dump_monitor()
188
189     logger.debug("BSS entries:\n" + dev[0].request("BSS RANGE=ALL"))
190     res1 = dev[0].get_bss(bssid)
191     res2 = dev[0].get_bss(bssid2)
192     if 'anqp_nai_realm' not in res1:
193         raise Exception("anqp_nai_realm not found for AP1")
194     if 'anqp_nai_realm' not in res2:
195         raise Exception("anqp_nai_realm not found for AP2")
196     if res1['anqp_nai_realm'] != res2['anqp_nai_realm']:
197         raise Exception("ANQP results were not shared between BSSes")
198
199     logger.info("Explicit ANQP request to unshare ANQP results")
200     dev[0].request("ANQP_GET " + bssid + " 263")
201     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
202     if ev is None:
203         raise Exception("ANQP operation timed out")
204
205     dev[0].request("ANQP_GET " + bssid2 + " 263")
206     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
207     if ev is None:
208         raise Exception("ANQP operation timed out")
209
210     res1 = dev[0].get_bss(bssid)
211     res2 = dev[0].get_bss(bssid2)
212     if res1['anqp_nai_realm'] == res2['anqp_nai_realm']:
213         raise Exception("ANQP results were not unshared")
214
215 def test_ap_anqp_sharing_oom(dev, apdev):
216     """ANQP sharing within ESS and explicit unshare OOM"""
217     check_eap_capa(dev[0], "MSCHAPV2")
218     dev[0].flush_scan_cache()
219
220     bssid = apdev[0]['bssid']
221     params = hs20_ap_params()
222     params['hessid'] = bssid
223     hostapd.add_ap(apdev[0], params)
224
225     bssid2 = apdev[1]['bssid']
226     params = hs20_ap_params()
227     params['hessid'] = bssid
228     params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]" ]
229     hostapd.add_ap(apdev[1], params)
230
231     dev[0].hs20_enable()
232     id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
233                                   'password': "secret",
234                                   'domain': "example.com" })
235     dev[0].scan_for_bss(bssid, freq="2412")
236     dev[0].scan_for_bss(bssid2, freq="2412")
237     interworking_select(dev[0], None, "home", freq="2412")
238     dev[0].dump_monitor()
239
240     with alloc_fail(dev[0], 1, "wpa_bss_anqp_clone"):
241         dev[0].request("ANQP_GET " + bssid + " 263")
242         ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
243         if ev is None:
244             raise Exception("ANQP operation timed out")
245
246 def test_ap_nai_home_realm_query(dev, apdev):
247     """NAI Home Realm Query"""
248     check_eap_capa(dev[0], "MSCHAPV2")
249     bssid = apdev[0]['bssid']
250     params = hs20_ap_params()
251     params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
252                             "0,another.example.org" ]
253     hostapd.add_ap(apdev[0], params)
254
255     dev[0].scan(freq="2412")
256     dev[0].request("HS20_GET_NAI_HOME_REALM_LIST " + bssid + " realm=example.com")
257     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
258     if ev is None:
259         raise Exception("ANQP operation timed out")
260     nai1 = dev[0].get_bss(bssid)['anqp_nai_realm']
261     dev[0].dump_monitor()
262
263     dev[0].request("ANQP_GET " + bssid + " 263")
264     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
265     if ev is None:
266         raise Exception("ANQP operation timed out")
267     nai2 = dev[0].get_bss(bssid)['anqp_nai_realm']
268
269     if len(nai1) >= len(nai2):
270         raise Exception("Unexpected NAI Realm list response lengths")
271     if "example.com".encode('hex') not in nai1:
272         raise Exception("Home realm not reported")
273     if "example.org".encode('hex') in nai1:
274         raise Exception("Non-home realm reported")
275     if "example.com".encode('hex') not in nai2:
276         raise Exception("Home realm not reported in wildcard query")
277     if "example.org".encode('hex') not in nai2:
278         raise Exception("Non-home realm not reported in wildcard query ")
279
280     cmds = [ "foo",
281              "00:11:22:33:44:55 123",
282              "00:11:22:33:44:55 qq" ]
283     for cmd in cmds:
284         if "FAIL" not in dev[0].request("HS20_GET_NAI_HOME_REALM_LIST " + cmd):
285             raise Exception("Invalid HS20_GET_NAI_HOME_REALM_LIST accepted: " + cmd)
286
287     dev[0].dump_monitor()
288     if "OK" not in dev[0].request("HS20_GET_NAI_HOME_REALM_LIST " + bssid):
289         raise Exception("HS20_GET_NAI_HOME_REALM_LIST failed")
290     ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
291     if ev is None:
292         raise Exception("ANQP operation timed out")
293     ev = dev[0].wait_event(["RX-ANQP"], timeout=0.1)
294     if ev is not None:
295         raise Exception("Unexpected ANQP response: " + ev)
296
297     dev[0].dump_monitor()
298     if "OK" not in dev[0].request("HS20_GET_NAI_HOME_REALM_LIST " + bssid + " 01000b6578616d706c652e636f6d"):
299         raise Exception("HS20_GET_NAI_HOME_REALM_LIST failed")
300     ev = dev[0].wait_event(["RX-ANQP"], timeout=10)
301     if ev is None:
302         raise Exception("No ANQP response")
303     if "NAI Realm list" not in ev:
304         raise Exception("Missing NAI Realm list: " + ev)
305
306     dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
307                              'password': "secret",
308                              'domain': "example.com" })
309     dev[0].dump_monitor()
310     if "OK" not in dev[0].request("HS20_GET_NAI_HOME_REALM_LIST " + bssid):
311         raise Exception("HS20_GET_NAI_HOME_REALM_LIST failed")
312     ev = dev[0].wait_event(["RX-ANQP"], timeout=10)
313     if ev is None:
314         raise Exception("No ANQP response")
315     if "NAI Realm list" not in ev:
316         raise Exception("Missing NAI Realm list: " + ev)
317
318 @remote_compatible
319 def test_ap_interworking_scan_filtering(dev, apdev):
320     """Interworking scan filtering with HESSID and access network type"""
321     try:
322         _test_ap_interworking_scan_filtering(dev, apdev)
323     finally:
324         dev[0].request("SET hessid 00:00:00:00:00:00")
325         dev[0].request("SET access_network_type 15")
326
327 def _test_ap_interworking_scan_filtering(dev, apdev):
328     bssid = apdev[0]['bssid']
329     params = hs20_ap_params()
330     ssid = "test-hs20-ap1"
331     params['ssid'] = ssid
332     params['hessid'] = bssid
333     hapd0 = hostapd.add_ap(apdev[0], params)
334
335     bssid2 = apdev[1]['bssid']
336     params = hs20_ap_params()
337     ssid2 = "test-hs20-ap2"
338     params['ssid'] = ssid2
339     params['hessid'] = bssid2
340     params['access_network_type'] = "1"
341     del params['venue_group']
342     del params['venue_type']
343     hostapd.add_ap(apdev[1], params)
344
345     dev[0].hs20_enable()
346
347     Wlantest.setup(hapd0)
348     wt = Wlantest()
349     wt.flush()
350
351     logger.info("Check probe request filtering based on HESSID")
352
353     dev[0].request("SET hessid " + bssid2)
354     dev[0].scan(freq="2412")
355     time.sleep(0.03)
356     check_probe_resp(wt, bssid, bssid2)
357
358     logger.info("Check probe request filtering based on access network type")
359
360     wt.clear_bss_counters(bssid)
361     wt.clear_bss_counters(bssid2)
362     dev[0].request("SET hessid 00:00:00:00:00:00")
363     dev[0].request("SET access_network_type 14")
364     dev[0].scan(freq="2412")
365     time.sleep(0.03)
366     check_probe_resp(wt, bssid2, bssid)
367
368     wt.clear_bss_counters(bssid)
369     wt.clear_bss_counters(bssid2)
370     dev[0].request("SET hessid 00:00:00:00:00:00")
371     dev[0].request("SET access_network_type 1")
372     dev[0].scan(freq="2412")
373     time.sleep(0.03)
374     check_probe_resp(wt, bssid, bssid2)
375
376     logger.info("Check probe request filtering based on HESSID and ANT")
377
378     wt.clear_bss_counters(bssid)
379     wt.clear_bss_counters(bssid2)
380     dev[0].request("SET hessid " + bssid)
381     dev[0].request("SET access_network_type 14")
382     dev[0].scan(freq="2412")
383     time.sleep(0.03)
384     check_probe_resp(wt, bssid2, bssid)
385
386     wt.clear_bss_counters(bssid)
387     wt.clear_bss_counters(bssid2)
388     dev[0].request("SET hessid " + bssid2)
389     dev[0].request("SET access_network_type 14")
390     dev[0].scan(freq="2412")
391     time.sleep(0.03)
392     check_probe_resp(wt, bssid, None)
393     check_probe_resp(wt, bssid2, None)
394
395     wt.clear_bss_counters(bssid)
396     wt.clear_bss_counters(bssid2)
397     dev[0].request("SET hessid " + bssid)
398     dev[0].request("SET access_network_type 1")
399     dev[0].scan(freq="2412")
400     time.sleep(0.03)
401     check_probe_resp(wt, bssid, None)
402     check_probe_resp(wt, bssid2, None)
403
404 def test_ap_hs20_select(dev, apdev):
405     """Hotspot 2.0 network selection"""
406     bssid = apdev[0]['bssid']
407     params = hs20_ap_params()
408     params['hessid'] = bssid
409     hostapd.add_ap(apdev[0], params)
410
411     dev[0].hs20_enable()
412     id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
413                                   'password': "secret",
414                                   'domain': "example.com" })
415     interworking_select(dev[0], bssid, "home")
416
417     dev[0].remove_cred(id)
418     id = dev[0].add_cred_values({ 'realm': "example.com", 'username': "test",
419                                   'password': "secret",
420                                   'domain': "no.match.example.com" })
421     interworking_select(dev[0], bssid, "roaming", freq="2412")
422
423     dev[0].set_cred_quoted(id, "realm", "no.match.example.com")
424     interworking_select(dev[0], bssid, no_match=True, freq="2412")
425
426     res = dev[0].request("SCAN_RESULTS")
427     if "[HS20]" not in res:
428         raise Exception("HS20 flag missing from scan results: " + res)
429
430     bssid2 = apdev[1]['bssid']
431     params = hs20_ap_params()
432     params['nai_realm'] = [ "0,example.org,21" ]
433     params['hessid'] = bssid2
434     params['domain_name'] = "example.org"
435     hostapd.add_ap(apdev[1], params)
436     dev[0].remove_cred(id)
437     id = dev[0].add_cred_values({ 'realm': "example.org", 'username': "test",
438                                   'password': "secret",
439                                   'domain': "example.org" })
440     interworking_select(dev[0], bssid2, "home", freq="2412")
441
442 def hs20_simulated_sim(dev, ap, method):
443     bssid = ap['bssid']
444     params = hs20_ap_params()
445     params['hessid'] = bssid
446     params['anqp_3gpp_cell_net'] = "555,444"
447     params['domain_name'] = "wlan.mnc444.mcc555.3gppnetwork.org"
448     hostapd.add_ap(ap, params)
449
450     dev.hs20_enable()
451     dev.add_cred_values({ 'imsi': "555444-333222111", 'eap': method,
452                           'milenage': "5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123"})
453     interworking_select(dev, bssid, "home", freq="2412")
454     interworking_connect(dev, bssid, method)
455     check_sp_type(dev, "home")
456
457 def test_ap_hs20_sim(dev, apdev):
458     """Hotspot 2.0 with simulated SIM and EAP-SIM"""
459     hlr_auc_gw_available()
460     hs20_simulated_sim(dev[0], apdev[0], "SIM")
461     dev[0].request("INTERWORKING_SELECT auto freq=2412")
462     ev = dev[0].wait_event(["INTERWORKING-ALREADY-CONNECTED"], timeout=15)
463     if ev is None:
464         raise Exception("Timeout on already-connected event")
465
466 def test_ap_hs20_sim_invalid(dev, apdev):
467     """Hotspot 2.0 with simulated SIM and EAP-SIM - invalid IMSI"""
468     hlr_auc_gw_available()
469     bssid = apdev[0]['bssid']
470     params = hs20_ap_params()
471     params['hessid'] = bssid
472     params['anqp_3gpp_cell_net'] = "555,444"
473     params['domain_name'] = "wlan.mnc444.mcc555.3gppnetwork.org"
474     hostapd.add_ap(apdev[0], params)
475
476     dev[0].hs20_enable()
477     dev[0].add_cred_values({ 'imsi': "555444-3332221110", 'eap': "SIM",
478                           'milenage': "5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123"})
479     # This hits "No valid IMSI available" in build_root_nai()
480     interworking_select(dev[0], bssid, freq="2412")
481
482 def test_ap_hs20_aka(dev, apdev):
483     """Hotspot 2.0 with simulated USIM and EAP-AKA"""
484     hlr_auc_gw_available()
485     hs20_simulated_sim(dev[0], apdev[0], "AKA")
486
487 def test_ap_hs20_aka_prime(dev, apdev):
488     """Hotspot 2.0 with simulated USIM and EAP-AKA'"""
489     hlr_auc_gw_available()
490     hs20_simulated_sim(dev[0], apdev[0], "AKA'")
491
492 def test_ap_hs20_ext_sim(dev, apdev):
493     """Hotspot 2.0 with external SIM processing"""
494     hlr_auc_gw_available()
495     bssid = apdev[0]['bssid']
496     params = hs20_ap_params()
497     params['hessid'] = bssid
498     params['anqp_3gpp_cell_net'] = "232,01"
499     params['domain_name'] = "wlan.mnc001.mcc232.3gppnetwork.org"
500     hostapd.add_ap(apdev[0], params)
501
502     dev[0].hs20_enable()
503     try:
504         dev[0].request("SET external_sim 1")
505         dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM" })
506         interworking_select(dev[0], bssid, "home", freq="2412")
507         interworking_ext_sim_connect(dev[0], bssid, "SIM")
508         check_sp_type(dev[0], "home")
509     finally:
510         dev[0].request("SET external_sim 0")
511
512 def test_ap_hs20_ext_sim_roaming(dev, apdev):
513     """Hotspot 2.0 with external SIM processing in roaming network"""
514     hlr_auc_gw_available()
515     bssid = apdev[0]['bssid']
516     params = hs20_ap_params()
517     params['hessid'] = bssid
518     params['anqp_3gpp_cell_net'] = "244,91;310,026;232,01;234,56"
519     params['domain_name'] = "wlan.mnc091.mcc244.3gppnetwork.org"
520     hostapd.add_ap(apdev[0], params)
521
522     dev[0].hs20_enable()
523     try:
524         dev[0].request("SET external_sim 1")
525         dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM" })
526         interworking_select(dev[0], bssid, "roaming", freq="2412")
527         interworking_ext_sim_connect(dev[0], bssid, "SIM")
528         check_sp_type(dev[0], "roaming")
529     finally:
530         dev[0].request("SET external_sim 0")
531
532 def test_ap_hs20_username(dev, apdev):
533     """Hotspot 2.0 connection in username/password credential"""
534     check_eap_capa(dev[0], "MSCHAPV2")
535     bssid = apdev[0]['bssid']
536     params = hs20_ap_params()
537     params['hessid'] = bssid
538     params['disable_dgaf'] = '1'
539     hostapd.add_ap(apdev[0], params)
540
541     dev[0].hs20_enable()
542     id = dev[0].add_cred_values({ 'realm': "example.com",
543                                   'username': "hs20-test",
544                                   'password': "password",
545                                   'ca_cert': "auth_serv/ca.pem",
546                                   'domain': "example.com",
547                                   'update_identifier': "1234" })
548     interworking_select(dev[0], bssid, "home", freq="2412")
549     interworking_connect(dev[0], bssid, "TTLS")
550     check_sp_type(dev[0], "home")
551     status = dev[0].get_status()
552     if status['pairwise_cipher'] != "CCMP":
553         raise Exception("Unexpected pairwise cipher")
554     if status['hs20'] != "2":
555         raise Exception("Unexpected HS 2.0 support indication")
556
557     dev[1].connect("test-hs20", key_mgmt="WPA-EAP", eap="TTLS",
558                    identity="hs20-test", password="password",
559                    ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
560                    scan_freq="2412")
561
562 def test_ap_hs20_connect_api(dev, apdev):
563     """Hotspot 2.0 connection with connect API"""
564     check_eap_capa(dev[0], "MSCHAPV2")
565     bssid = apdev[0]['bssid']
566     params = hs20_ap_params()
567     params['hessid'] = bssid
568     params['disable_dgaf'] = '1'
569     hostapd.add_ap(apdev[0], params)
570
571     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
572     wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
573     wpas.hs20_enable()
574     wpas.flush_scan_cache()
575     id = wpas.add_cred_values({ 'realm': "example.com",
576                                   'username': "hs20-test",
577                                   'password': "password",
578                                   'ca_cert': "auth_serv/ca.pem",
579                                   'domain': "example.com",
580                                   'update_identifier': "1234" })
581     interworking_select(wpas, bssid, "home", freq="2412")
582     interworking_connect(wpas, bssid, "TTLS")
583     check_sp_type(wpas, "home")
584     status = wpas.get_status()
585     if status['pairwise_cipher'] != "CCMP":
586         raise Exception("Unexpected pairwise cipher")
587     if status['hs20'] != "2":
588         raise Exception("Unexpected HS 2.0 support indication")
589
590 def test_ap_hs20_auto_interworking(dev, apdev):
591     """Hotspot 2.0 connection with auto_interworking=1"""
592     check_eap_capa(dev[0], "MSCHAPV2")
593     bssid = apdev[0]['bssid']
594     params = hs20_ap_params()
595     params['hessid'] = bssid
596     params['disable_dgaf'] = '1'
597     hostapd.add_ap(apdev[0], params)
598
599     dev[0].hs20_enable(auto_interworking=True)
600     id = dev[0].add_cred_values({ 'realm': "example.com",
601                                   'username': "hs20-test",
602                                   'password': "password",
603                                   'ca_cert': "auth_serv/ca.pem",
604                                   'domain': "example.com",
605                                   'update_identifier': "1234" })
606     dev[0].request("REASSOCIATE")
607     dev[0].wait_connected(timeout=15)
608     check_sp_type(dev[0], "home")
609     status = dev[0].get_status()
610     if status['pairwise_cipher'] != "CCMP":
611         raise Exception("Unexpected pairwise cipher")
612     if status['hs20'] != "2":
613         raise Exception("Unexpected HS 2.0 support indication")
614
615 @remote_compatible
616 def test_ap_hs20_auto_interworking_no_match(dev, apdev):
617     """Hotspot 2.0 connection with auto_interworking=1 and no matching network"""
618     hapd = hostapd.add_ap(apdev[0], { "ssid": "mismatch" })
619
620     dev[0].hs20_enable(auto_interworking=True)
621     id = dev[0].connect("mismatch", psk="12345678", scan_freq="2412",
622                         only_add_network=True)
623     dev[0].request("ENABLE_NETWORK " + str(id) + " no-connect")
624
625     id = dev[0].add_cred_values({ 'realm': "example.com",
626                                   'username': "hs20-test",
627                                   'password': "password",
628                                   'ca_cert': "auth_serv/ca.pem",
629                                   'domain': "example.com",
630                                   'update_identifier': "1234" })
631     dev[0].request("INTERWORKING_SELECT auto freq=2412")
632     time.sleep(0.1)
633     dev[0].dump_monitor()
634     for i in range(5):
635         logger.info("start ping")
636         if "PONG" not in dev[0].ctrl.request("PING", timeout=2):
637             raise Exception("PING failed")
638         logger.info("ping done")
639         fetch = 0
640         scan = 0
641         for j in range(15):
642             ev = dev[0].wait_event([ "ANQP fetch completed",
643                                      "CTRL-EVENT-SCAN-RESULTS" ], timeout=0.05)
644             if ev is None:
645                 break
646             if "ANQP fetch completed" in ev:
647                 fetch += 1
648             else:
649                 scan += 1
650         if fetch > 2 * scan + 3:
651             raise Exception("Too many ANQP fetch iterations")
652         dev[0].dump_monitor()
653     dev[0].request("DISCONNECT")
654
655 @remote_compatible
656 def test_ap_hs20_auto_interworking_no_cred_match(dev, apdev):
657     """Hotspot 2.0 connection with auto_interworking=1 but no cred match"""
658     bssid = apdev[0]['bssid']
659     params = { "ssid": "test" }
660     hostapd.add_ap(apdev[0], params)
661
662     dev[0].hs20_enable(auto_interworking=True)
663     dev[0].add_cred_values({ 'realm': "example.com",
664                              'username': "hs20-test",
665                              'password': "password",
666                              'ca_cert': "auth_serv/ca.pem",
667                              'domain': "example.com" })
668
669     id = dev[0].connect("test", psk="12345678", only_add_network=True)
670     dev[0].request("ENABLE_NETWORK %s" % id)
671     logger.info("Verify that scanning continues when there is partial network block match")
672     for i in range(0, 2):
673         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
674         if ev is None:
675             raise Exception("Scan timed out")
676         logger.info("Scan completed")
677
678 def eap_test(dev, ap, eap_params, method, user):
679     bssid = ap['bssid']
680     params = hs20_ap_params()
681     params['nai_realm'] = [ "0,example.com," + eap_params ]
682     hostapd.add_ap(ap, params)
683
684     dev.hs20_enable()
685     dev.add_cred_values({ 'realm': "example.com",
686                           'ca_cert': "auth_serv/ca.pem",
687                           'username': user,
688                           'password': "password" })
689     interworking_select(dev, bssid, freq="2412")
690     interworking_connect(dev, bssid, method)
691
692 @remote_compatible
693 def test_ap_hs20_eap_unknown(dev, apdev):
694     """Hotspot 2.0 connection with unknown EAP method"""
695     bssid = apdev[0]['bssid']
696     params = hs20_ap_params()
697     params['nai_realm'] = "0,example.com,99"
698     hostapd.add_ap(apdev[0], params)
699
700     dev[0].hs20_enable()
701     dev[0].add_cred_values(default_cred())
702     interworking_select(dev[0], None, no_match=True, freq="2412")
703
704 def test_ap_hs20_eap_peap_mschapv2(dev, apdev):
705     """Hotspot 2.0 connection with PEAP/MSCHAPV2"""
706     check_eap_capa(dev[0], "MSCHAPV2")
707     eap_test(dev[0], apdev[0], "25[3:26]", "PEAP", "user")
708
709 def test_ap_hs20_eap_peap_default(dev, apdev):
710     """Hotspot 2.0 connection with PEAP/MSCHAPV2 (as default)"""
711     check_eap_capa(dev[0], "MSCHAPV2")
712     eap_test(dev[0], apdev[0], "25", "PEAP", "user")
713
714 def test_ap_hs20_eap_peap_gtc(dev, apdev):
715     """Hotspot 2.0 connection with PEAP/GTC"""
716     eap_test(dev[0], apdev[0], "25[3:6]", "PEAP", "user")
717
718 @remote_compatible
719 def test_ap_hs20_eap_peap_unknown(dev, apdev):
720     """Hotspot 2.0 connection with PEAP/unknown"""
721     bssid = apdev[0]['bssid']
722     params = hs20_ap_params()
723     params['nai_realm'] = "0,example.com,25[3:99]"
724     hostapd.add_ap(apdev[0], params)
725
726     dev[0].hs20_enable()
727     dev[0].add_cred_values(default_cred())
728     interworking_select(dev[0], None, no_match=True, freq="2412")
729
730 def test_ap_hs20_eap_ttls_chap(dev, apdev):
731     """Hotspot 2.0 connection with TTLS/CHAP"""
732     skip_with_fips(dev[0])
733     eap_test(dev[0], apdev[0], "21[2:2]", "TTLS", "chap user")
734
735 def test_ap_hs20_eap_ttls_mschap(dev, apdev):
736     """Hotspot 2.0 connection with TTLS/MSCHAP"""
737     skip_with_fips(dev[0])
738     eap_test(dev[0], apdev[0], "21[2:3]", "TTLS", "mschap user")
739
740 def test_ap_hs20_eap_ttls_eap_mschapv2(dev, apdev):
741     """Hotspot 2.0 connection with TTLS/EAP-MSCHAPv2"""
742     check_eap_capa(dev[0], "MSCHAPV2")
743     eap_test(dev[0], apdev[0], "21[3:26][6:7][99:99]", "TTLS", "user")
744
745 @remote_compatible
746 def test_ap_hs20_eap_ttls_eap_unknown(dev, apdev):
747     """Hotspot 2.0 connection with TTLS/EAP-unknown"""
748     bssid = apdev[0]['bssid']
749     params = hs20_ap_params()
750     params['nai_realm'] = "0,example.com,21[3:99]"
751     hostapd.add_ap(apdev[0], params)
752
753     dev[0].hs20_enable()
754     dev[0].add_cred_values(default_cred())
755     interworking_select(dev[0], None, no_match=True, freq="2412")
756
757 @remote_compatible
758 def test_ap_hs20_eap_ttls_eap_unsupported(dev, apdev):
759     """Hotspot 2.0 connection with TTLS/EAP-OTP(unsupported)"""
760     bssid = apdev[0]['bssid']
761     params = hs20_ap_params()
762     params['nai_realm'] = "0,example.com,21[3:5]"
763     hostapd.add_ap(apdev[0], params)
764
765     dev[0].hs20_enable()
766     dev[0].add_cred_values(default_cred())
767     interworking_select(dev[0], None, no_match=True, freq="2412")
768
769 @remote_compatible
770 def test_ap_hs20_eap_ttls_unknown(dev, apdev):
771     """Hotspot 2.0 connection with TTLS/unknown"""
772     bssid = apdev[0]['bssid']
773     params = hs20_ap_params()
774     params['nai_realm'] = "0,example.com,21[2:5]"
775     hostapd.add_ap(apdev[0], params)
776
777     dev[0].hs20_enable()
778     dev[0].add_cred_values(default_cred())
779     interworking_select(dev[0], None, no_match=True, freq="2412")
780
781 def test_ap_hs20_eap_fast_mschapv2(dev, apdev):
782     """Hotspot 2.0 connection with FAST/EAP-MSCHAPV2"""
783     check_eap_capa(dev[0], "FAST")
784     eap_test(dev[0], apdev[0], "43[3:26]", "FAST", "user")
785
786 def test_ap_hs20_eap_fast_gtc(dev, apdev):
787     """Hotspot 2.0 connection with FAST/EAP-GTC"""
788     check_eap_capa(dev[0], "FAST")
789     eap_test(dev[0], apdev[0], "43[3:6]", "FAST", "user")
790
791 def test_ap_hs20_eap_tls(dev, apdev):
792     """Hotspot 2.0 connection with EAP-TLS"""
793     bssid = apdev[0]['bssid']
794     params = hs20_ap_params()
795     params['nai_realm'] = [ "0,example.com,13[5:6]" ]
796     hostapd.add_ap(apdev[0], params)
797
798     dev[0].hs20_enable()
799     dev[0].add_cred_values({ 'realm': "example.com",
800                              'username': "certificate-user",
801                              'ca_cert': "auth_serv/ca.pem",
802                              'client_cert': "auth_serv/user.pem",
803                              'private_key': "auth_serv/user.key"})
804     interworking_select(dev[0], bssid, freq="2412")
805     interworking_connect(dev[0], bssid, "TLS")
806
807 @remote_compatible
808 def test_ap_hs20_eap_cert_unknown(dev, apdev):
809     """Hotspot 2.0 connection with certificate, but unknown EAP method"""
810     bssid = apdev[0]['bssid']
811     params = hs20_ap_params()
812     params['nai_realm'] = [ "0,example.com,99[5:6]" ]
813     hostapd.add_ap(apdev[0], params)
814
815     dev[0].hs20_enable()
816     dev[0].add_cred_values({ 'realm': "example.com",
817                              'username': "certificate-user",
818                              'ca_cert': "auth_serv/ca.pem",
819                              'client_cert': "auth_serv/user.pem",
820                              'private_key': "auth_serv/user.key"})
821     interworking_select(dev[0], None, no_match=True, freq="2412")
822
823 @remote_compatible
824 def test_ap_hs20_eap_cert_unsupported(dev, apdev):
825     """Hotspot 2.0 connection with certificate, but unsupported TTLS"""
826     bssid = apdev[0]['bssid']
827     params = hs20_ap_params()
828     params['nai_realm'] = [ "0,example.com,21[5:6]" ]
829     hostapd.add_ap(apdev[0], params)
830
831     dev[0].hs20_enable()
832     dev[0].add_cred_values({ 'realm': "example.com",
833                              'username': "certificate-user",
834                              'ca_cert': "auth_serv/ca.pem",
835                              'client_cert': "auth_serv/user.pem",
836                              'private_key': "auth_serv/user.key"})
837     interworking_select(dev[0], None, no_match=True, freq="2412")
838
839 @remote_compatible
840 def test_ap_hs20_eap_invalid_cred(dev, apdev):
841     """Hotspot 2.0 connection with invalid cred configuration"""
842     bssid = apdev[0]['bssid']
843     params = hs20_ap_params()
844     hostapd.add_ap(apdev[0], params)
845
846     dev[0].hs20_enable()
847     dev[0].add_cred_values({ 'realm': "example.com",
848                              'username': "certificate-user",
849                              'client_cert': "auth_serv/user.pem" })
850     interworking_select(dev[0], None, no_match=True, freq="2412")
851
852 def test_ap_hs20_nai_realms(dev, apdev):
853     """Hotspot 2.0 connection and multiple NAI realms and TTLS/PAP"""
854     bssid = apdev[0]['bssid']
855     params = hs20_ap_params()
856     params['hessid'] = bssid
857     params['nai_realm'] = [ "0,no.match.here;example.com;no.match.here.either,21[2:1][5:7]" ]
858     hostapd.add_ap(apdev[0], params)
859
860     dev[0].hs20_enable()
861     id = dev[0].add_cred_values({ 'realm': "example.com",
862                                   'ca_cert': "auth_serv/ca.pem",
863                                   'username': "pap user",
864                                   'password': "password",
865                                   'domain': "example.com" })
866     interworking_select(dev[0], bssid, "home", freq="2412")
867     interworking_connect(dev[0], bssid, "TTLS")
868     check_sp_type(dev[0], "home")
869
870 def test_ap_hs20_roaming_consortium(dev, apdev):
871     """Hotspot 2.0 connection based on roaming consortium match"""
872     bssid = apdev[0]['bssid']
873     params = hs20_ap_params()
874     params['hessid'] = bssid
875     hostapd.add_ap(apdev[0], params)
876
877     dev[0].hs20_enable()
878     for consortium in [ "112233", "1020304050", "010203040506", "fedcba" ]:
879         id = dev[0].add_cred_values({ 'username': "user",
880                                       'password': "password",
881                                       'domain': "example.com",
882                                       'ca_cert': "auth_serv/ca.pem",
883                                       'roaming_consortium': consortium,
884                                       'eap': "PEAP" })
885         interworking_select(dev[0], bssid, "home", freq="2412")
886         interworking_connect(dev[0], bssid, "PEAP")
887         check_sp_type(dev[0], "home")
888         dev[0].request("INTERWORKING_SELECT auto freq=2412")
889         ev = dev[0].wait_event(["INTERWORKING-ALREADY-CONNECTED"], timeout=15)
890         if ev is None:
891             raise Exception("Timeout on already-connected event")
892         dev[0].remove_cred(id)
893
894 def test_ap_hs20_username_roaming(dev, apdev):
895     """Hotspot 2.0 connection in username/password credential (roaming)"""
896     check_eap_capa(dev[0], "MSCHAPV2")
897     bssid = apdev[0]['bssid']
898     params = hs20_ap_params()
899     params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
900                             "0,roaming.example.com,21[2:4][5:7]",
901                             "0,another.example.com" ]
902     params['domain_name'] = "another.example.com"
903     params['hessid'] = bssid
904     hostapd.add_ap(apdev[0], params)
905
906     dev[0].hs20_enable()
907     id = dev[0].add_cred_values({ 'realm': "roaming.example.com",
908                                   'username': "hs20-test",
909                                   'password': "password",
910                                   'ca_cert': "auth_serv/ca.pem",
911                                   'domain': "example.com" })
912     interworking_select(dev[0], bssid, "roaming", freq="2412")
913     interworking_connect(dev[0], bssid, "TTLS")
914     check_sp_type(dev[0], "roaming")
915
916 def test_ap_hs20_username_unknown(dev, apdev):
917     """Hotspot 2.0 connection in username/password credential (no domain in cred)"""
918     check_eap_capa(dev[0], "MSCHAPV2")
919     bssid = apdev[0]['bssid']
920     params = hs20_ap_params()
921     params['hessid'] = bssid
922     hostapd.add_ap(apdev[0], params)
923
924     dev[0].hs20_enable()
925     id = dev[0].add_cred_values({ 'realm': "example.com",
926                                   'ca_cert': "auth_serv/ca.pem",
927                                   'username': "hs20-test",
928                                   'password': "password" })
929     interworking_select(dev[0], bssid, "unknown", freq="2412")
930     interworking_connect(dev[0], bssid, "TTLS")
931     check_sp_type(dev[0], "unknown")
932
933 def test_ap_hs20_username_unknown2(dev, apdev):
934     """Hotspot 2.0 connection in username/password credential (no domain advertized)"""
935     check_eap_capa(dev[0], "MSCHAPV2")
936     bssid = apdev[0]['bssid']
937     params = hs20_ap_params()
938     params['hessid'] = bssid
939     del params['domain_name']
940     hostapd.add_ap(apdev[0], params)
941
942     dev[0].hs20_enable()
943     id = dev[0].add_cred_values({ 'realm': "example.com",
944                                   'ca_cert': "auth_serv/ca.pem",
945                                   'username': "hs20-test",
946                                   'password': "password",
947                                   'domain': "example.com" })
948     interworking_select(dev[0], bssid, "unknown", freq="2412")
949     interworking_connect(dev[0], bssid, "TTLS")
950     check_sp_type(dev[0], "unknown")
951
952 def test_ap_hs20_gas_while_associated(dev, apdev):
953     """Hotspot 2.0 connection with GAS query while associated"""
954     check_eap_capa(dev[0], "MSCHAPV2")
955     bssid = apdev[0]['bssid']
956     params = hs20_ap_params()
957     params['hessid'] = bssid
958     hostapd.add_ap(apdev[0], params)
959
960     dev[0].hs20_enable()
961     id = dev[0].add_cred_values({ 'realm': "example.com",
962                                   'ca_cert': "auth_serv/ca.pem",
963                                   'username': "hs20-test",
964                                   'password': "password",
965                                   'domain': "example.com" })
966     interworking_select(dev[0], bssid, "home", freq="2412")
967     interworking_connect(dev[0], bssid, "TTLS")
968
969     logger.info("Verifying GAS query while associated")
970     dev[0].request("FETCH_ANQP")
971     for i in range(0, 6):
972         ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
973         if ev is None:
974             raise Exception("Operation timed out")
975
976 def test_ap_hs20_gas_with_another_ap_while_associated(dev, apdev):
977     """GAS query with another AP while associated"""
978     check_eap_capa(dev[0], "MSCHAPV2")
979     bssid = apdev[0]['bssid']
980     params = hs20_ap_params()
981     params['hessid'] = bssid
982     hostapd.add_ap(apdev[0], params)
983
984     bssid2 = apdev[1]['bssid']
985     params = hs20_ap_params()
986     params['hessid'] = bssid2
987     params['nai_realm'] = [ "0,no-match.example.org,13[5:6],21[2:4][5:7]" ]
988     hostapd.add_ap(apdev[1], params)
989
990     dev[0].hs20_enable()
991     id = dev[0].add_cred_values({ 'realm': "example.com",
992                                   'ca_cert': "auth_serv/ca.pem",
993                                   'username': "hs20-test",
994                                   'password': "password",
995                                   'domain': "example.com" })
996     interworking_select(dev[0], bssid, "home", freq="2412")
997     interworking_connect(dev[0], bssid, "TTLS")
998     dev[0].dump_monitor()
999
1000     logger.info("Verifying GAS query with same AP while associated")
1001     dev[0].request("ANQP_GET " + bssid + " 263")
1002     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
1003     if ev is None:
1004         raise Exception("ANQP operation timed out")
1005     dev[0].dump_monitor()
1006
1007     logger.info("Verifying GAS query with another AP while associated")
1008     dev[0].scan_for_bss(bssid2, 2412)
1009     dev[0].request("ANQP_GET " + bssid2 + " 263")
1010     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
1011     if ev is None:
1012         raise Exception("ANQP operation timed out")
1013
1014 def test_ap_hs20_gas_while_associated_with_pmf(dev, apdev):
1015     """Hotspot 2.0 connection with GAS query while associated and using PMF"""
1016     check_eap_capa(dev[0], "MSCHAPV2")
1017     try:
1018         _test_ap_hs20_gas_while_associated_with_pmf(dev, apdev)
1019     finally:
1020         dev[0].request("SET pmf 0")
1021
1022 def _test_ap_hs20_gas_while_associated_with_pmf(dev, apdev):
1023     bssid = apdev[0]['bssid']
1024     params = hs20_ap_params()
1025     params['hessid'] = bssid
1026     hostapd.add_ap(apdev[0], params)
1027
1028     bssid2 = apdev[1]['bssid']
1029     params = hs20_ap_params()
1030     params['hessid'] = bssid2
1031     params['nai_realm'] = [ "0,no-match.example.org,13[5:6],21[2:4][5:7]" ]
1032     hostapd.add_ap(apdev[1], params)
1033
1034     dev[0].hs20_enable()
1035     dev[0].request("SET pmf 2")
1036     id = dev[0].add_cred_values({ 'realm': "example.com",
1037                                   'ca_cert': "auth_serv/ca.pem",
1038                                   'username': "hs20-test",
1039                                   'password': "password",
1040                                   'domain': "example.com" })
1041     interworking_select(dev[0], bssid, "home", freq="2412")
1042     interworking_connect(dev[0], bssid, "TTLS")
1043
1044     logger.info("Verifying GAS query while associated")
1045     dev[0].request("FETCH_ANQP")
1046     for i in range(0, 2 * 6):
1047         ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
1048         if ev is None:
1049             raise Exception("Operation timed out")
1050
1051 def test_ap_hs20_gas_with_another_ap_while_using_pmf(dev, apdev):
1052     """GAS query with another AP while associated and using PMF"""
1053     check_eap_capa(dev[0], "MSCHAPV2")
1054     try:
1055         _test_ap_hs20_gas_with_another_ap_while_using_pmf(dev, apdev)
1056     finally:
1057         dev[0].request("SET pmf 0")
1058
1059 def _test_ap_hs20_gas_with_another_ap_while_using_pmf(dev, apdev):
1060     bssid = apdev[0]['bssid']
1061     params = hs20_ap_params()
1062     params['hessid'] = bssid
1063     hostapd.add_ap(apdev[0], params)
1064
1065     bssid2 = apdev[1]['bssid']
1066     params = hs20_ap_params()
1067     params['hessid'] = bssid2
1068     params['nai_realm'] = [ "0,no-match.example.org,13[5:6],21[2:4][5:7]" ]
1069     hostapd.add_ap(apdev[1], params)
1070
1071     dev[0].hs20_enable()
1072     dev[0].request("SET pmf 2")
1073     id = dev[0].add_cred_values({ 'realm': "example.com",
1074                                   'ca_cert': "auth_serv/ca.pem",
1075                                   'username': "hs20-test",
1076                                   'password': "password",
1077                                   'domain': "example.com" })
1078     interworking_select(dev[0], bssid, "home", freq="2412")
1079     interworking_connect(dev[0], bssid, "TTLS")
1080     dev[0].dump_monitor()
1081
1082     logger.info("Verifying GAS query with same AP while associated")
1083     dev[0].request("ANQP_GET " + bssid + " 263")
1084     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
1085     if ev is None:
1086         raise Exception("ANQP operation timed out")
1087     dev[0].dump_monitor()
1088
1089     logger.info("Verifying GAS query with another AP while associated")
1090     dev[0].scan_for_bss(bssid2, 2412)
1091     dev[0].request("ANQP_GET " + bssid2 + " 263")
1092     ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
1093     if ev is None:
1094         raise Exception("ANQP operation timed out")
1095
1096 def test_ap_hs20_gas_frag_while_associated(dev, apdev):
1097     """Hotspot 2.0 connection with fragmented GAS query while associated"""
1098     check_eap_capa(dev[0], "MSCHAPV2")
1099     bssid = apdev[0]['bssid']
1100     params = hs20_ap_params()
1101     params['hessid'] = bssid
1102     hapd = hostapd.add_ap(apdev[0], params)
1103     hapd.set("gas_frag_limit", "50")
1104
1105     dev[0].hs20_enable()
1106     id = dev[0].add_cred_values({ 'realm': "example.com",
1107                                   'ca_cert': "auth_serv/ca.pem",
1108                                   'username': "hs20-test",
1109                                   'password': "password",
1110                                   'domain': "example.com" })
1111     interworking_select(dev[0], bssid, "home", freq="2412")
1112     interworking_connect(dev[0], bssid, "TTLS")
1113
1114     logger.info("Verifying GAS query while associated")
1115     dev[0].request("FETCH_ANQP")
1116     for i in range(0, 6):
1117         ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
1118         if ev is None:
1119             raise Exception("Operation timed out")
1120
1121 def test_ap_hs20_multiple_connects(dev, apdev):
1122     """Hotspot 2.0 connection through multiple network selections"""
1123     check_eap_capa(dev[0], "MSCHAPV2")
1124     bssid = apdev[0]['bssid']
1125     params = hs20_ap_params()
1126     params['hessid'] = bssid
1127     hostapd.add_ap(apdev[0], params)
1128
1129     dev[0].hs20_enable()
1130     values = { 'realm': "example.com",
1131                'ca_cert': "auth_serv/ca.pem",
1132                'username': "hs20-test",
1133                'password': "password",
1134                'domain': "example.com" }
1135     id = dev[0].add_cred_values(values)
1136
1137     dev[0].scan_for_bss(bssid, freq="2412")
1138
1139     for i in range(0, 3):
1140         logger.info("Starting Interworking network selection")
1141         dev[0].request("INTERWORKING_SELECT auto freq=2412")
1142         while True:
1143             ev = dev[0].wait_event(["INTERWORKING-NO-MATCH",
1144                                     "INTERWORKING-ALREADY-CONNECTED",
1145                                     "CTRL-EVENT-CONNECTED"], timeout=15)
1146             if ev is None:
1147                 raise Exception("Connection timed out")
1148             if "INTERWORKING-NO-MATCH" in ev:
1149                 raise Exception("Matching AP not found")
1150             if "CTRL-EVENT-CONNECTED" in ev:
1151                 break
1152             if i == 2 and "INTERWORKING-ALREADY-CONNECTED" in ev:
1153                 break
1154         if i == 0:
1155             dev[0].request("DISCONNECT")
1156         dev[0].dump_monitor()
1157
1158     networks = dev[0].list_networks()
1159     if len(networks) > 1:
1160         raise Exception("Duplicated network block detected")
1161
1162 def test_ap_hs20_disallow_aps(dev, apdev):
1163     """Hotspot 2.0 connection and disallow_aps"""
1164     bssid = apdev[0]['bssid']
1165     params = hs20_ap_params()
1166     params['hessid'] = bssid
1167     hostapd.add_ap(apdev[0], params)
1168
1169     dev[0].hs20_enable()
1170     values = { 'realm': "example.com",
1171                'ca_cert': "auth_serv/ca.pem",
1172                'username': "hs20-test",
1173                'password': "password",
1174                'domain': "example.com" }
1175     id = dev[0].add_cred_values(values)
1176
1177     dev[0].scan_for_bss(bssid, freq="2412")
1178
1179     logger.info("Verify disallow_aps bssid")
1180     dev[0].request("SET disallow_aps bssid " + bssid.translate(None, ':'))
1181     dev[0].request("INTERWORKING_SELECT auto")
1182     ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=15)
1183     if ev is None:
1184         raise Exception("Network selection timed out")
1185     dev[0].dump_monitor()
1186
1187     logger.info("Verify disallow_aps ssid")
1188     dev[0].request("SET disallow_aps ssid 746573742d68733230")
1189     dev[0].request("INTERWORKING_SELECT auto freq=2412")
1190     ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=15)
1191     if ev is None:
1192         raise Exception("Network selection timed out")
1193     dev[0].dump_monitor()
1194
1195     logger.info("Verify disallow_aps clear")
1196     dev[0].request("SET disallow_aps ")
1197     interworking_select(dev[0], bssid, "home", freq="2412")
1198
1199     dev[0].request("SET disallow_aps bssid " + bssid.translate(None, ':'))
1200     ret = dev[0].request("INTERWORKING_CONNECT " + bssid)
1201     if "FAIL" not in ret:
1202         raise Exception("INTERWORKING_CONNECT to disallowed BSS not rejected")
1203
1204     if "FAIL" not in dev[0].request("INTERWORKING_CONNECT foo"):
1205         raise Exception("Invalid INTERWORKING_CONNECT not rejected")
1206     if "FAIL" not in dev[0].request("INTERWORKING_CONNECT 00:11:22:33:44:55"):
1207         raise Exception("Invalid INTERWORKING_CONNECT not rejected")
1208
1209 def policy_test(dev, ap, values, only_one=True):
1210     dev.dump_monitor()
1211     if ap:
1212         logger.info("Verify network selection to AP " + ap['ifname'])
1213         bssid = ap['bssid']
1214         dev.scan_for_bss(bssid, freq="2412")
1215     else:
1216         logger.info("Verify network selection")
1217         bssid = None
1218     dev.hs20_enable()
1219     id = dev.add_cred_values(values)
1220     dev.request("INTERWORKING_SELECT auto freq=2412")
1221     events = []
1222     while True:
1223         ev = dev.wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH",
1224                              "INTERWORKING-BLACKLISTED",
1225                              "INTERWORKING-SELECTED"], timeout=15)
1226         if ev is None:
1227             raise Exception("Network selection timed out")
1228         events.append(ev)
1229         if "INTERWORKING-NO-MATCH" in ev:
1230             raise Exception("Matching AP not found")
1231         if bssid and only_one and "INTERWORKING-AP" in ev and bssid not in ev:
1232             raise Exception("Unexpected AP claimed acceptable")
1233         if "INTERWORKING-SELECTED" in ev:
1234             if bssid and bssid not in ev:
1235                 raise Exception("Selected incorrect BSS")
1236             break
1237
1238     ev = dev.wait_connected(timeout=15)
1239     if bssid and bssid not in ev:
1240         raise Exception("Connected to incorrect BSS")
1241
1242     conn_bssid = dev.get_status_field("bssid")
1243     if bssid and conn_bssid != bssid:
1244         raise Exception("bssid information points to incorrect BSS")
1245
1246     dev.remove_cred(id)
1247     dev.dump_monitor()
1248     return events
1249
1250 def default_cred(domain=None, user="hs20-test"):
1251     cred = { 'realm': "example.com",
1252              'ca_cert': "auth_serv/ca.pem",
1253              'username': user,
1254              'password': "password" }
1255     if domain:
1256         cred['domain'] = domain
1257     return cred
1258
1259 def test_ap_hs20_prefer_home(dev, apdev):
1260     """Hotspot 2.0 required roaming consortium"""
1261     check_eap_capa(dev[0], "MSCHAPV2")
1262     params = hs20_ap_params()
1263     params['domain_name'] = "example.org"
1264     hostapd.add_ap(apdev[0], params)
1265
1266     params = hs20_ap_params()
1267     params['ssid'] = "test-hs20-other"
1268     params['domain_name'] = "example.com"
1269     hostapd.add_ap(apdev[1], params)
1270
1271     values = default_cred()
1272     values['domain'] = "example.com"
1273     policy_test(dev[0], apdev[1], values, only_one=False)
1274     values['domain'] = "example.org"
1275     policy_test(dev[0], apdev[0], values, only_one=False)
1276
1277 def test_ap_hs20_req_roaming_consortium(dev, apdev):
1278     """Hotspot 2.0 required roaming consortium"""
1279     check_eap_capa(dev[0], "MSCHAPV2")
1280     params = hs20_ap_params()
1281     hostapd.add_ap(apdev[0], params)
1282
1283     params = hs20_ap_params()
1284     params['ssid'] = "test-hs20-other"
1285     params['roaming_consortium'] = [ "223344" ]
1286     hostapd.add_ap(apdev[1], params)
1287
1288     values = default_cred()
1289     values['required_roaming_consortium'] = "223344"
1290     policy_test(dev[0], apdev[1], values)
1291     values['required_roaming_consortium'] = "112233"
1292     policy_test(dev[0], apdev[0], values)
1293
1294     id = dev[0].add_cred()
1295     dev[0].set_cred(id, "required_roaming_consortium", "112233")
1296     dev[0].set_cred(id, "required_roaming_consortium", "112233445566778899aabbccddeeff")
1297
1298     for val in [ "", "1", "11", "1122", "1122334", "112233445566778899aabbccddeeff00" ]:
1299         if "FAIL" not in dev[0].request('SET_CRED {} required_roaming_consortium {}'.format(id, val)):
1300             raise Exception("Invalid roaming consortium value accepted: " + val)
1301
1302 def test_ap_hs20_excluded_ssid(dev, apdev):
1303     """Hotspot 2.0 exclusion based on SSID"""
1304     check_eap_capa(dev[0], "MSCHAPV2")
1305     params = hs20_ap_params()
1306     params['roaming_consortium'] = [ "223344" ]
1307     params['anqp_3gpp_cell_net'] = "555,444"
1308     hostapd.add_ap(apdev[0], params)
1309
1310     params = hs20_ap_params()
1311     params['ssid'] = "test-hs20-other"
1312     params['roaming_consortium'] = [ "223344" ]
1313     params['anqp_3gpp_cell_net'] = "555,444"
1314     hostapd.add_ap(apdev[1], params)
1315
1316     values = default_cred()
1317     values['excluded_ssid'] = "test-hs20"
1318     events = policy_test(dev[0], apdev[1], values)
1319     ev = [e for e in events if "INTERWORKING-BLACKLISTED " + apdev[0]['bssid'] in e]
1320     if len(ev) != 1:
1321         raise Exception("Excluded network not reported")
1322     values['excluded_ssid'] = "test-hs20-other"
1323     events = policy_test(dev[0], apdev[0], values)
1324     ev = [e for e in events if "INTERWORKING-BLACKLISTED " + apdev[1]['bssid'] in e]
1325     if len(ev) != 1:
1326         raise Exception("Excluded network not reported")
1327
1328     values = default_cred()
1329     values['roaming_consortium'] = "223344"
1330     values['eap'] = "TTLS"
1331     values['phase2'] = "auth=MSCHAPV2"
1332     values['excluded_ssid'] = "test-hs20"
1333     events = policy_test(dev[0], apdev[1], values)
1334     ev = [e for e in events if "INTERWORKING-BLACKLISTED " + apdev[0]['bssid'] in e]
1335     if len(ev) != 1:
1336         raise Exception("Excluded network not reported")
1337
1338     values = { 'imsi': "555444-333222111", 'eap': "SIM",
1339                'milenage': "5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123",
1340                'excluded_ssid': "test-hs20" }
1341     events = policy_test(dev[0], apdev[1], values)
1342     ev = [e for e in events if "INTERWORKING-BLACKLISTED " + apdev[0]['bssid'] in e]
1343     if len(ev) != 1:
1344         raise Exception("Excluded network not reported")
1345
1346 def test_ap_hs20_roam_to_higher_prio(dev, apdev):
1347     """Hotspot 2.0 and roaming from current to higher priority network"""
1348     check_eap_capa(dev[0], "MSCHAPV2")
1349     bssid = apdev[0]['bssid']
1350     params = hs20_ap_params(ssid="test-hs20-visited")
1351     params['domain_name'] = "visited.example.org"
1352     hostapd.add_ap(apdev[0], params)
1353
1354     dev[0].hs20_enable()
1355     id = dev[0].add_cred_values({ 'realm': "example.com",
1356                                   'ca_cert': "auth_serv/ca.pem",
1357                                   'username': "hs20-test",
1358                                   'password': "password",
1359                                   'domain': "example.com" })
1360     logger.info("Connect to the only network option")
1361     interworking_select(dev[0], bssid, "roaming", freq="2412")
1362     dev[0].dump_monitor()
1363     interworking_connect(dev[0], bssid, "TTLS")
1364
1365     logger.info("Start another AP (home operator) and reconnect")
1366     bssid2 = apdev[1]['bssid']
1367     params = hs20_ap_params(ssid="test-hs20-home")
1368     params['domain_name'] = "example.com"
1369     hostapd.add_ap(apdev[1], params)
1370
1371     dev[0].scan_for_bss(bssid2, freq="2412", force_scan=True)
1372     dev[0].request("INTERWORKING_SELECT auto freq=2412")
1373     ev = dev[0].wait_event(["INTERWORKING-NO-MATCH",
1374                             "INTERWORKING-ALREADY-CONNECTED",
1375                             "CTRL-EVENT-CONNECTED"], timeout=15)
1376     if ev is None:
1377         raise Exception("Connection timed out")
1378     if "INTERWORKING-NO-MATCH" in ev:
1379         raise Exception("Matching AP not found")
1380     if "INTERWORKING-ALREADY-CONNECTED" in ev:
1381         raise Exception("Unexpected AP selected")
1382     if bssid2 not in ev:
1383         raise Exception("Unexpected BSSID after reconnection")
1384
1385 def test_ap_hs20_domain_suffix_match_full(dev, apdev):
1386     """Hotspot 2.0 and domain_suffix_match"""
1387     check_domain_match_full(dev[0])
1388     check_eap_capa(dev[0], "MSCHAPV2")
1389     bssid = apdev[0]['bssid']
1390     params = hs20_ap_params()
1391     hostapd.add_ap(apdev[0], params)
1392
1393     dev[0].hs20_enable()
1394     id = dev[0].add_cred_values({ 'realm': "example.com",
1395                                   'username': "hs20-test",
1396                                   'password': "password",
1397                                   'ca_cert': "auth_serv/ca.pem",
1398                                   'domain': "example.com",
1399                                   'domain_suffix_match': "server.w1.fi" })
1400     interworking_select(dev[0], bssid, "home", freq="2412")
1401     dev[0].dump_monitor()
1402     interworking_connect(dev[0], bssid, "TTLS")
1403     dev[0].request("REMOVE_NETWORK all")
1404     dev[0].dump_monitor()
1405
1406     dev[0].set_cred_quoted(id, "domain_suffix_match", "no-match.example.com")
1407     interworking_select(dev[0], bssid, "home", freq="2412")
1408     dev[0].dump_monitor()
1409     dev[0].request("INTERWORKING_CONNECT " + bssid)
1410     ev = dev[0].wait_event(["CTRL-EVENT-EAP-TLS-CERT-ERROR"])
1411     if ev is None:
1412         raise Exception("TLS certificate error not reported")
1413     if "Domain suffix mismatch" not in ev:
1414         raise Exception("Domain suffix mismatch not reported")
1415
1416 def test_ap_hs20_domain_suffix_match(dev, apdev):
1417     """Hotspot 2.0 and domain_suffix_match"""
1418     check_eap_capa(dev[0], "MSCHAPV2")
1419     check_domain_match_full(dev[0])
1420     bssid = apdev[0]['bssid']
1421     params = hs20_ap_params()
1422     hostapd.add_ap(apdev[0], params)
1423
1424     dev[0].hs20_enable()
1425     id = dev[0].add_cred_values({ 'realm': "example.com",
1426                                   'username': "hs20-test",
1427                                   'password': "password",
1428                                   'ca_cert': "auth_serv/ca.pem",
1429                                   'domain': "example.com",
1430                                   'domain_suffix_match': "w1.fi" })
1431     interworking_select(dev[0], bssid, "home", freq="2412")
1432     dev[0].dump_monitor()
1433     interworking_connect(dev[0], bssid, "TTLS")
1434
1435 def test_ap_hs20_roaming_partner_preference(dev, apdev):
1436     """Hotspot 2.0 and roaming partner preference"""
1437     check_eap_capa(dev[0], "MSCHAPV2")
1438     params = hs20_ap_params()
1439     params['domain_name'] = "roaming.example.org"
1440     hostapd.add_ap(apdev[0], params)
1441
1442     params = hs20_ap_params()
1443     params['ssid'] = "test-hs20-other"
1444     params['domain_name'] = "roaming.example.net"
1445     hostapd.add_ap(apdev[1], params)
1446
1447     logger.info("Verify default vs. specified preference")
1448     values = default_cred()
1449     values['roaming_partner'] = "roaming.example.net,1,127,*"
1450     policy_test(dev[0], apdev[1], values, only_one=False)
1451     values['roaming_partner'] = "roaming.example.net,1,129,*"
1452     policy_test(dev[0], apdev[0], values, only_one=False)
1453
1454     logger.info("Verify partial FQDN match")
1455     values['roaming_partner'] = "example.net,0,0,*"
1456     policy_test(dev[0], apdev[1], values, only_one=False)
1457     values['roaming_partner'] = "example.net,0,255,*"
1458     policy_test(dev[0], apdev[0], values, only_one=False)
1459
1460 def test_ap_hs20_max_bss_load(dev, apdev):
1461     """Hotspot 2.0 and maximum BSS load"""
1462     check_eap_capa(dev[0], "MSCHAPV2")
1463     params = hs20_ap_params()
1464     params['bss_load_test'] = "12:200:20000"
1465     hostapd.add_ap(apdev[0], params)
1466
1467     params = hs20_ap_params()
1468     params['ssid'] = "test-hs20-other"
1469     params['bss_load_test'] = "5:20:10000"
1470     hostapd.add_ap(apdev[1], params)
1471
1472     logger.info("Verify maximum BSS load constraint")
1473     values = default_cred()
1474     values['domain'] = "example.com"
1475     values['max_bss_load'] = "100"
1476     events = policy_test(dev[0], apdev[1], values, only_one=False)
1477
1478     ev = [e for e in events if "INTERWORKING-AP " + apdev[0]['bssid'] in e]
1479     if len(ev) != 1 or "over_max_bss_load=1" not in ev[0]:
1480         raise Exception("Maximum BSS Load case not noticed")
1481     ev = [e for e in events if "INTERWORKING-AP " + apdev[1]['bssid'] in e]
1482     if len(ev) != 1 or "over_max_bss_load=1" in ev[0]:
1483         raise Exception("Maximum BSS Load case reported incorrectly")
1484
1485     logger.info("Verify maximum BSS load does not prevent connection")
1486     values['max_bss_load'] = "1"
1487     events = policy_test(dev[0], None, values)
1488
1489     ev = [e for e in events if "INTERWORKING-AP " + apdev[0]['bssid'] in e]
1490     if len(ev) != 1 or "over_max_bss_load=1" not in ev[0]:
1491         raise Exception("Maximum BSS Load case not noticed")
1492     ev = [e for e in events if "INTERWORKING-AP " + apdev[1]['bssid'] in e]
1493     if len(ev) != 1 or "over_max_bss_load=1" not in ev[0]:
1494         raise Exception("Maximum BSS Load case not noticed")
1495
1496 def test_ap_hs20_max_bss_load2(dev, apdev):
1497     """Hotspot 2.0 and maximum BSS load with one AP not advertising"""
1498     check_eap_capa(dev[0], "MSCHAPV2")
1499     params = hs20_ap_params()
1500     params['bss_load_test'] = "12:200:20000"
1501     hostapd.add_ap(apdev[0], params)
1502
1503     params = hs20_ap_params()
1504     params['ssid'] = "test-hs20-other"
1505     hostapd.add_ap(apdev[1], params)
1506
1507     logger.info("Verify maximum BSS load constraint with AP advertisement")
1508     values = default_cred()
1509     values['domain'] = "example.com"
1510     values['max_bss_load'] = "100"
1511     events = policy_test(dev[0], apdev[1], values, only_one=False)
1512
1513     ev = [e for e in events if "INTERWORKING-AP " + apdev[0]['bssid'] in e]
1514     if len(ev) != 1 or "over_max_bss_load=1" not in ev[0]:
1515         raise Exception("Maximum BSS Load case not noticed")
1516     ev = [e for e in events if "INTERWORKING-AP " + apdev[1]['bssid'] in e]
1517     if len(ev) != 1 or "over_max_bss_load=1" in ev[0]:
1518         raise Exception("Maximum BSS Load case reported incorrectly")
1519
1520 def test_ap_hs20_multi_cred_sp_prio(dev, apdev):
1521     """Hotspot 2.0 multi-cred sp_priority"""
1522     check_eap_capa(dev[0], "MSCHAPV2")
1523     try:
1524         _test_ap_hs20_multi_cred_sp_prio(dev, apdev)
1525     finally:
1526         dev[0].request("SET external_sim 0")
1527
1528 def _test_ap_hs20_multi_cred_sp_prio(dev, apdev):
1529     hlr_auc_gw_available()
1530     bssid = apdev[0]['bssid']
1531     params = hs20_ap_params()
1532     params['hessid'] = bssid
1533     del params['domain_name']
1534     params['anqp_3gpp_cell_net'] = "232,01"
1535     hostapd.add_ap(apdev[0], params)
1536
1537     dev[0].hs20_enable()
1538     dev[0].scan_for_bss(bssid, freq="2412")
1539     dev[0].request("SET external_sim 1")
1540     id1 = dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM",
1541                                    'provisioning_sp': "example.com",
1542                                    'sp_priority' :"1" })
1543     id2 = dev[0].add_cred_values({ 'realm': "example.com",
1544                                    'ca_cert': "auth_serv/ca.pem",
1545                                    'username': "hs20-test",
1546                                    'password': "password",
1547                                    'domain': "example.com",
1548                                    'provisioning_sp': "example.com",
1549                                    'sp_priority': "2" })
1550     dev[0].dump_monitor()
1551     dev[0].scan_for_bss(bssid, freq="2412")
1552     dev[0].request("INTERWORKING_SELECT auto freq=2412")
1553     interworking_ext_sim_auth(dev[0], "SIM")
1554     check_sp_type(dev[0], "unknown")
1555     dev[0].request("REMOVE_NETWORK all")
1556
1557     dev[0].set_cred(id1, "sp_priority", "2")
1558     dev[0].set_cred(id2, "sp_priority", "1")
1559     dev[0].dump_monitor()
1560     dev[0].request("INTERWORKING_SELECT auto freq=2412")
1561     interworking_auth(dev[0], "TTLS")
1562     check_sp_type(dev[0], "unknown")
1563
1564 def test_ap_hs20_multi_cred_sp_prio2(dev, apdev):
1565     """Hotspot 2.0 multi-cred sp_priority with two BSSes"""
1566     check_eap_capa(dev[0], "MSCHAPV2")
1567     try:
1568         _test_ap_hs20_multi_cred_sp_prio2(dev, apdev)
1569     finally:
1570         dev[0].request("SET external_sim 0")
1571
1572 def _test_ap_hs20_multi_cred_sp_prio2(dev, apdev):
1573     hlr_auc_gw_available()
1574     bssid = apdev[0]['bssid']
1575     params = hs20_ap_params()
1576     params['hessid'] = bssid
1577     del params['nai_realm']
1578     del params['domain_name']
1579     params['anqp_3gpp_cell_net'] = "232,01"
1580     hostapd.add_ap(apdev[0], params)
1581
1582     bssid2 = apdev[1]['bssid']
1583     params = hs20_ap_params()
1584     params['ssid'] = "test-hs20-other"
1585     params['hessid'] = bssid2
1586     del params['domain_name']
1587     del params['anqp_3gpp_cell_net']
1588     hostapd.add_ap(apdev[1], params)
1589
1590     dev[0].hs20_enable()
1591     dev[0].request("SET external_sim 1")
1592     id1 = dev[0].add_cred_values({ 'imsi': "23201-0000000000", 'eap': "SIM",
1593                                    'provisioning_sp': "example.com",
1594                                    'sp_priority': "1" })
1595     id2 = dev[0].add_cred_values({ 'realm': "example.com",
1596                                    'ca_cert': "auth_serv/ca.pem",
1597                                    'username': "hs20-test",
1598                                    'password': "password",
1599                                    'domain': "example.com",
1600                                    'provisioning_sp': "example.com",
1601                                    'sp_priority': "2" })
1602     dev[0].dump_monitor()
1603     dev[0].scan_for_bss(bssid, freq="2412")
1604     dev[0].scan_for_bss(bssid2, freq="2412")
1605     dev[0].request("INTERWORKING_SELECT auto freq=2412")
1606     interworking_ext_sim_auth(dev[0], "SIM")
1607     check_sp_type(dev[0], "unknown")
1608     conn_bssid = dev[0].get_status_field("bssid")
1609     if conn_bssid != bssid:
1610         raise Exception("Connected to incorrect BSS")
1611     dev[0].request("REMOVE_NETWORK all")
1612
1613     dev[0].set_cred(id1, "sp_priority", "2")
1614     dev[0].set_cred(id2, "sp_priority", "1")
1615     dev[0].dump_monitor()
1616     dev[0].request("INTERWORKING_SELECT auto freq=2412")
1617     interworking_auth(dev[0], "TTLS")
1618     check_sp_type(dev[0], "unknown")
1619     conn_bssid = dev[0].get_status_field("bssid")
1620     if conn_bssid != bssid2:
1621         raise Exception("Connected to incorrect BSS")
1622
1623 def test_ap_hs20_multi_cred_sp_prio_same(dev, apdev):
1624     """Hotspot 2.0 multi-cred and same sp_priority"""
1625     check_eap_capa(dev[0], "MSCHAPV2")
1626     hlr_auc_gw_available()
1627     bssid = apdev[0]['bssid']
1628     params = hs20_ap_params()
1629     params['hessid'] = bssid
1630     del params['domain_name']
1631     params['anqp_3gpp_cell_net'] = "232,01"
1632     hostapd.add_ap(apdev[0], params)
1633
1634     dev[0].hs20_enable()
1635     dev[0].scan_for_bss(bssid, freq="2412")
1636     id1 = dev[0].add_cred_values({ 'realm': "example.com",
1637                                    'ca_cert': "auth_serv/ca.pem",
1638                                    'username': "hs20-test",
1639                                    'password': "password",
1640                                    'domain': "domain1.example.com",
1641                                    'provisioning_sp': "example.com",
1642                                    'sp_priority': "1" })
1643     id2 = dev[0].add_cred_values({ 'realm': "example.com",
1644                                    'ca_cert': "auth_serv/ca.pem",
1645                                    'username': "hs20-test",
1646                                    'password': "password",
1647                                    'domain': "domain2.example.com",
1648                                    'provisioning_sp': "example.com",
1649                                    'sp_priority': "1" })
1650     dev[0].dump_monitor()
1651     dev[0].scan_for_bss(bssid, freq="2412")
1652     check_auto_select(dev[0], bssid)
1653
1654 def check_conn_capab_selection(dev, type, missing):
1655     dev.request("INTERWORKING_SELECT freq=2412")
1656     ev = dev.wait_event(["INTERWORKING-AP"])
1657     if ev is None:
1658         raise Exception("Network selection timed out")
1659     if "type=" + type not in ev:
1660         raise Exception("Unexpected network type")
1661     if missing and "conn_capab_missing=1" not in ev:
1662         raise Exception("conn_capab_missing not reported")
1663     if not missing and "conn_capab_missing=1" in ev:
1664         raise Exception("conn_capab_missing reported unexpectedly")
1665
1666 def conn_capab_cred(domain=None, req_conn_capab=None):
1667     cred = default_cred(domain=domain)
1668     if req_conn_capab:
1669         cred['req_conn_capab'] = req_conn_capab
1670     return cred
1671
1672 def test_ap_hs20_req_conn_capab(dev, apdev):
1673     """Hotspot 2.0 network selection with req_conn_capab"""
1674     check_eap_capa(dev[0], "MSCHAPV2")
1675     bssid = apdev[0]['bssid']
1676     params = hs20_ap_params()
1677     hostapd.add_ap(apdev[0], params)
1678
1679     dev[0].hs20_enable()
1680     dev[0].scan_for_bss(bssid, freq="2412")
1681     logger.info("Not used in home network")
1682     values = conn_capab_cred(domain="example.com", req_conn_capab="6:1234")
1683     id = dev[0].add_cred_values(values)
1684     check_conn_capab_selection(dev[0], "home", False)
1685
1686     logger.info("Used in roaming network")
1687     dev[0].remove_cred(id)
1688     values = conn_capab_cred(domain="example.org", req_conn_capab="6:1234")
1689     id = dev[0].add_cred_values(values)
1690     check_conn_capab_selection(dev[0], "roaming", True)
1691
1692     logger.info("Verify that req_conn_capab does not prevent connection if no other network is available")
1693     check_auto_select(dev[0], bssid)
1694
1695     logger.info("Additional req_conn_capab checks")
1696
1697     dev[0].remove_cred(id)
1698     values = conn_capab_cred(domain="example.org", req_conn_capab="1:0")
1699     id = dev[0].add_cred_values(values)
1700     check_conn_capab_selection(dev[0], "roaming", True)
1701
1702     dev[0].remove_cred(id)
1703     values = conn_capab_cred(domain="example.org", req_conn_capab="17:5060")
1704     id = dev[0].add_cred_values(values)
1705     check_conn_capab_selection(dev[0], "roaming", True)
1706
1707     bssid2 = apdev[1]['bssid']
1708     params = hs20_ap_params(ssid="test-hs20b")
1709     params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0", "50:0:1" ]
1710     hostapd.add_ap(apdev[1], params)
1711
1712     dev[0].remove_cred(id)
1713     values = conn_capab_cred(domain="example.org", req_conn_capab="50")
1714     id = dev[0].add_cred_values(values)
1715     dev[0].set_cred(id, "req_conn_capab", "6:22")
1716     dev[0].scan_for_bss(bssid2, freq="2412")
1717     dev[0].request("INTERWORKING_SELECT freq=2412")
1718     for i in range(0, 2):
1719         ev = dev[0].wait_event(["INTERWORKING-AP"])
1720         if ev is None:
1721             raise Exception("Network selection timed out")
1722         if bssid in ev and "conn_capab_missing=1" not in ev:
1723             raise Exception("Missing protocol connection capability not reported")
1724         if bssid2 in ev and "conn_capab_missing=1" in ev:
1725             raise Exception("Protocol connection capability not reported correctly")
1726
1727 def test_ap_hs20_req_conn_capab_and_roaming_partner_preference(dev, apdev):
1728     """Hotspot 2.0 and req_conn_capab with roaming partner preference"""
1729     check_eap_capa(dev[0], "MSCHAPV2")
1730     bssid = apdev[0]['bssid']
1731     params = hs20_ap_params()
1732     params['domain_name'] = "roaming.example.org"
1733     params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0", "50:0:1" ]
1734     hostapd.add_ap(apdev[0], params)
1735
1736     bssid2 = apdev[1]['bssid']
1737     params = hs20_ap_params(ssid="test-hs20-b")
1738     params['domain_name'] = "roaming.example.net"
1739     hostapd.add_ap(apdev[1], params)
1740
1741     values = default_cred()
1742     values['roaming_partner'] = "roaming.example.net,1,127,*"
1743     id = dev[0].add_cred_values(values)
1744     check_auto_select(dev[0], bssid2)
1745
1746     dev[0].set_cred(id, "req_conn_capab", "50")
1747     check_auto_select(dev[0], bssid)
1748
1749     dev[0].remove_cred(id)
1750     id = dev[0].add_cred_values(values)
1751     dev[0].set_cred(id, "req_conn_capab", "51")
1752     check_auto_select(dev[0], bssid2)
1753
1754 def check_bandwidth_selection(dev, type, below):
1755     dev.request("INTERWORKING_SELECT freq=2412")
1756     ev = dev.wait_event(["INTERWORKING-AP"])
1757     if ev is None:
1758         raise Exception("Network selection timed out")
1759     logger.debug("BSS entries:\n" + dev.request("BSS RANGE=ALL"))
1760     if "type=" + type not in ev:
1761         raise Exception("Unexpected network type")
1762     if below and "below_min_backhaul=1" not in ev:
1763         raise Exception("below_min_backhaul not reported")
1764     if not below and "below_min_backhaul=1" in ev:
1765         raise Exception("below_min_backhaul reported unexpectedly")
1766
1767 def bw_cred(domain=None, dl_home=None, ul_home=None, dl_roaming=None, ul_roaming=None):
1768     cred = default_cred(domain=domain)
1769     if dl_home:
1770         cred['min_dl_bandwidth_home'] = str(dl_home)
1771     if ul_home:
1772         cred['min_ul_bandwidth_home'] = str(ul_home)
1773     if dl_roaming:
1774         cred['min_dl_bandwidth_roaming'] = str(dl_roaming)
1775     if ul_roaming:
1776         cred['min_ul_bandwidth_roaming'] = str(ul_roaming)
1777     return cred
1778
1779 def test_ap_hs20_min_bandwidth_home(dev, apdev):
1780     """Hotspot 2.0 network selection with min bandwidth (home)"""
1781     check_eap_capa(dev[0], "MSCHAPV2")
1782     bssid = apdev[0]['bssid']
1783     params = hs20_ap_params()
1784     hostapd.add_ap(apdev[0], params)
1785
1786     dev[0].hs20_enable()
1787     dev[0].scan_for_bss(bssid, freq="2412")
1788     values = bw_cred(domain="example.com", dl_home=5490, ul_home=58)
1789     id = dev[0].add_cred_values(values)
1790     check_bandwidth_selection(dev[0], "home", False)
1791     dev[0].remove_cred(id)
1792
1793     values = bw_cred(domain="example.com", dl_home=5491, ul_home=58)
1794     id = dev[0].add_cred_values(values)
1795     check_bandwidth_selection(dev[0], "home", True)
1796     dev[0].remove_cred(id)
1797
1798     values = bw_cred(domain="example.com", dl_home=5490, ul_home=59)
1799     id = dev[0].add_cred_values(values)
1800     check_bandwidth_selection(dev[0], "home", True)
1801     dev[0].remove_cred(id)
1802
1803     values = bw_cred(domain="example.com", dl_home=5491, ul_home=59)
1804     id = dev[0].add_cred_values(values)
1805     check_bandwidth_selection(dev[0], "home", True)
1806     check_auto_select(dev[0], bssid)
1807
1808     bssid2 = apdev[1]['bssid']
1809     params = hs20_ap_params(ssid="test-hs20-b")
1810     params['hs20_wan_metrics'] = "01:8000:1000:1:1:3000"
1811     hostapd.add_ap(apdev[1], params)
1812
1813     check_auto_select(dev[0], bssid2)
1814
1815 def test_ap_hs20_min_bandwidth_home_hidden_ssid_in_scan_res(dev, apdev):
1816     """Hotspot 2.0 network selection with min bandwidth (home) while hidden SSID is included in scan results"""
1817     check_eap_capa(dev[0], "MSCHAPV2")
1818     bssid = apdev[0]['bssid']
1819
1820     hapd = hostapd.add_ap(apdev[0], { "ssid": 'secret',
1821                                       "ignore_broadcast_ssid": "1" })
1822     dev[0].scan_for_bss(bssid, freq=2412)
1823     hapd.disable()
1824     hapd_global = hostapd.HostapdGlobal(apdev[0])
1825     hapd_global.flush()
1826     hapd_global.remove(apdev[0]['ifname'])
1827
1828     params = hs20_ap_params()
1829     hostapd.add_ap(apdev[0], params)
1830
1831     dev[0].hs20_enable()
1832     dev[0].scan_for_bss(bssid, freq="2412")
1833     values = bw_cred(domain="example.com", dl_home=5490, ul_home=58)
1834     id = dev[0].add_cred_values(values)
1835     check_bandwidth_selection(dev[0], "home", False)
1836     dev[0].remove_cred(id)
1837
1838     values = bw_cred(domain="example.com", dl_home=5491, ul_home=58)
1839     id = dev[0].add_cred_values(values)
1840     check_bandwidth_selection(dev[0], "home", True)
1841     dev[0].remove_cred(id)
1842
1843     values = bw_cred(domain="example.com", dl_home=5490, ul_home=59)
1844     id = dev[0].add_cred_values(values)
1845     check_bandwidth_selection(dev[0], "home", True)
1846     dev[0].remove_cred(id)
1847
1848     values = bw_cred(domain="example.com", dl_home=5491, ul_home=59)
1849     id = dev[0].add_cred_values(values)
1850     check_bandwidth_selection(dev[0], "home", True)
1851     check_auto_select(dev[0], bssid)
1852
1853     bssid2 = apdev[1]['bssid']
1854     params = hs20_ap_params(ssid="test-hs20-b")
1855     params['hs20_wan_metrics'] = "01:8000:1000:1:1:3000"
1856     hostapd.add_ap(apdev[1], params)
1857
1858     check_auto_select(dev[0], bssid2)
1859
1860     dev[0].flush_scan_cache()
1861
1862 def test_ap_hs20_min_bandwidth_roaming(dev, apdev):
1863     """Hotspot 2.0 network selection with min bandwidth (roaming)"""
1864     check_eap_capa(dev[0], "MSCHAPV2")
1865     bssid = apdev[0]['bssid']
1866     params = hs20_ap_params()
1867     hostapd.add_ap(apdev[0], params)
1868
1869     dev[0].hs20_enable()
1870     dev[0].scan_for_bss(bssid, freq="2412")
1871     values = bw_cred(domain="example.org", dl_roaming=5490, ul_roaming=58)
1872     id = dev[0].add_cred_values(values)
1873     check_bandwidth_selection(dev[0], "roaming", False)
1874     dev[0].remove_cred(id)
1875
1876     values = bw_cred(domain="example.org", dl_roaming=5491, ul_roaming=58)
1877     id = dev[0].add_cred_values(values)
1878     check_bandwidth_selection(dev[0], "roaming", True)
1879     dev[0].remove_cred(id)
1880
1881     values = bw_cred(domain="example.org", dl_roaming=5490, ul_roaming=59)
1882     id = dev[0].add_cred_values(values)
1883     check_bandwidth_selection(dev[0], "roaming", True)
1884     dev[0].remove_cred(id)
1885
1886     values = bw_cred(domain="example.org", dl_roaming=5491, ul_roaming=59)
1887     id = dev[0].add_cred_values(values)
1888     check_bandwidth_selection(dev[0], "roaming", True)
1889     check_auto_select(dev[0], bssid)
1890
1891     bssid2 = apdev[1]['bssid']
1892     params = hs20_ap_params(ssid="test-hs20-b")
1893     params['hs20_wan_metrics'] = "01:8000:1000:1:1:3000"
1894     hostapd.add_ap(apdev[1], params)
1895
1896     check_auto_select(dev[0], bssid2)
1897
1898 def test_ap_hs20_min_bandwidth_and_roaming_partner_preference(dev, apdev):
1899     """Hotspot 2.0 and minimum bandwidth with roaming partner preference"""
1900     check_eap_capa(dev[0], "MSCHAPV2")
1901     bssid = apdev[0]['bssid']
1902     params = hs20_ap_params()
1903     params['domain_name'] = "roaming.example.org"
1904     params['hs20_wan_metrics'] = "01:8000:1000:1:1:3000"
1905     hostapd.add_ap(apdev[0], params)
1906
1907     bssid2 = apdev[1]['bssid']
1908     params = hs20_ap_params(ssid="test-hs20-b")
1909     params['domain_name'] = "roaming.example.net"
1910     hostapd.add_ap(apdev[1], params)
1911
1912     values = default_cred()
1913     values['roaming_partner'] = "roaming.example.net,1,127,*"
1914     id = dev[0].add_cred_values(values)
1915     check_auto_select(dev[0], bssid2)
1916
1917     dev[0].set_cred(id, "min_dl_bandwidth_roaming", "6000")
1918     check_auto_select(dev[0], bssid)
1919
1920     dev[0].set_cred(id, "min_dl_bandwidth_roaming", "10000")
1921     check_auto_select(dev[0], bssid2)
1922
1923 def test_ap_hs20_min_bandwidth_no_wan_metrics(dev, apdev):
1924     """Hotspot 2.0 network selection with min bandwidth but no WAN Metrics"""
1925     bssid = apdev[0]['bssid']
1926     params = hs20_ap_params()
1927     del params['hs20_wan_metrics']
1928     hostapd.add_ap(apdev[0], params)
1929
1930     dev[0].hs20_enable()
1931     dev[0].scan_for_bss(bssid, freq="2412")
1932     values = bw_cred(domain="example.com", dl_home=10000, ul_home=10000,
1933                      dl_roaming=10000, ul_roaming=10000)
1934     dev[0].add_cred_values(values)
1935     check_bandwidth_selection(dev[0], "home", False)
1936
1937 def test_ap_hs20_deauth_req_ess(dev, apdev):
1938     """Hotspot 2.0 connection and deauthentication request for ESS"""
1939     check_eap_capa(dev[0], "MSCHAPV2")
1940     try:
1941         _test_ap_hs20_deauth_req_ess(dev, apdev)
1942     finally:
1943         dev[0].request("SET pmf 0")
1944
1945 def _test_ap_hs20_deauth_req_ess(dev, apdev):
1946     dev[0].request("SET pmf 2")
1947     eap_test(dev[0], apdev[0], "21[3:26]", "TTLS", "user")
1948     dev[0].dump_monitor()
1949     addr = dev[0].p2p_interface_addr()
1950     hapd = hostapd.Hostapd(apdev[0]['ifname'])
1951     hapd.request("HS20_DEAUTH_REQ " + addr + " 1 120 http://example.com/")
1952     ev = dev[0].wait_event(["HS20-DEAUTH-IMMINENT-NOTICE"])
1953     if ev is None:
1954         raise Exception("Timeout on deauth imminent notice")
1955     if "1 120 http://example.com/" not in ev:
1956         raise Exception("Unexpected deauth imminent notice: " + ev)
1957     hapd.request("DEAUTHENTICATE " + addr)
1958     dev[0].wait_disconnected(timeout=10)
1959     if "[TEMP-DISABLED]" not in dev[0].list_networks()[0]['flags']:
1960         raise Exception("Network not marked temporarily disabled")
1961     ev = dev[0].wait_event(["SME: Trying to authenticate",
1962                             "Trying to associate",
1963                             "CTRL-EVENT-CONNECTED"], timeout=5)
1964     if ev is not None:
1965         raise Exception("Unexpected connection attempt")
1966
1967 def test_ap_hs20_deauth_req_bss(dev, apdev):
1968     """Hotspot 2.0 connection and deauthentication request for BSS"""
1969     check_eap_capa(dev[0], "MSCHAPV2")
1970     try:
1971         _test_ap_hs20_deauth_req_bss(dev, apdev)
1972     finally:
1973         dev[0].request("SET pmf 0")
1974
1975 def _test_ap_hs20_deauth_req_bss(dev, apdev):
1976     dev[0].request("SET pmf 2")
1977     eap_test(dev[0], apdev[0], "21[3:26]", "TTLS", "user")
1978     dev[0].dump_monitor()
1979     addr = dev[0].p2p_interface_addr()
1980     hapd = hostapd.Hostapd(apdev[0]['ifname'])
1981     hapd.request("HS20_DEAUTH_REQ " + addr + " 0 120 http://example.com/")
1982     ev = dev[0].wait_event(["HS20-DEAUTH-IMMINENT-NOTICE"])
1983     if ev is None:
1984         raise Exception("Timeout on deauth imminent notice")
1985     if "0 120 http://example.com/" not in ev:
1986         raise Exception("Unexpected deauth imminent notice: " + ev)
1987     hapd.request("DEAUTHENTICATE " + addr + " reason=4")
1988     ev = dev[0].wait_disconnected(timeout=10)
1989     if "reason=4" not in ev:
1990         raise Exception("Unexpected disconnection reason")
1991     if "[TEMP-DISABLED]" not in dev[0].list_networks()[0]['flags']:
1992         raise Exception("Network not marked temporarily disabled")
1993     ev = dev[0].wait_event(["SME: Trying to authenticate",
1994                             "Trying to associate",
1995                             "CTRL-EVENT-CONNECTED"], timeout=5)
1996     if ev is not None:
1997         raise Exception("Unexpected connection attempt")
1998
1999 def test_ap_hs20_deauth_req_from_radius(dev, apdev):
2000     """Hotspot 2.0 connection and deauthentication request from RADIUS"""
2001     check_eap_capa(dev[0], "MSCHAPV2")
2002     try:
2003         _test_ap_hs20_deauth_req_from_radius(dev, apdev)
2004     finally:
2005         dev[0].request("SET pmf 0")
2006
2007 def _test_ap_hs20_deauth_req_from_radius(dev, apdev):
2008     bssid = apdev[0]['bssid']
2009     params = hs20_ap_params()
2010     params['nai_realm'] = [ "0,example.com,21[2:4]" ]
2011     params['hs20_deauth_req_timeout'] = "2"
2012     hostapd.add_ap(apdev[0], params)
2013
2014     dev[0].request("SET pmf 2")
2015     dev[0].hs20_enable()
2016     dev[0].add_cred_values({ 'realm': "example.com",
2017                              'username': "hs20-deauth-test",
2018                              'password': "password" })
2019     interworking_select(dev[0], bssid, freq="2412")
2020     interworking_connect(dev[0], bssid, "TTLS")
2021     ev = dev[0].wait_event(["HS20-DEAUTH-IMMINENT-NOTICE"], timeout=5)
2022     if ev is None:
2023         raise Exception("Timeout on deauth imminent notice")
2024     if " 1 100" not in ev:
2025         raise Exception("Unexpected deauth imminent contents")
2026     dev[0].wait_disconnected(timeout=3)
2027
2028 def test_ap_hs20_remediation_required(dev, apdev):
2029     """Hotspot 2.0 connection and remediation required from RADIUS"""
2030     check_eap_capa(dev[0], "MSCHAPV2")
2031     try:
2032         _test_ap_hs20_remediation_required(dev, apdev)
2033     finally:
2034         dev[0].request("SET pmf 0")
2035
2036 def _test_ap_hs20_remediation_required(dev, apdev):
2037     bssid = apdev[0]['bssid']
2038     params = hs20_ap_params()
2039     params['nai_realm'] = [ "0,example.com,21[2:4]" ]
2040     hostapd.add_ap(apdev[0], params)
2041
2042     dev[0].request("SET pmf 1")
2043     dev[0].hs20_enable()
2044     dev[0].add_cred_values({ 'realm': "example.com",
2045                              'username': "hs20-subrem-test",
2046                              'password': "password" })
2047     interworking_select(dev[0], bssid, freq="2412")
2048     interworking_connect(dev[0], bssid, "TTLS")
2049     ev = dev[0].wait_event(["HS20-SUBSCRIPTION-REMEDIATION"], timeout=5)
2050     if ev is None:
2051         raise Exception("Timeout on subscription remediation notice")
2052     if " 1 https://example.com/" not in ev:
2053         raise Exception("Unexpected subscription remediation event contents")
2054
2055 def test_ap_hs20_remediation_required_ctrl(dev, apdev):
2056     """Hotspot 2.0 connection and subrem from ctrl_iface"""
2057     check_eap_capa(dev[0], "MSCHAPV2")
2058     try:
2059         _test_ap_hs20_remediation_required_ctrl(dev, apdev)
2060     finally:
2061         dev[0].request("SET pmf 0")
2062
2063 def _test_ap_hs20_remediation_required_ctrl(dev, apdev):
2064     bssid = apdev[0]['bssid']
2065     addr = dev[0].own_addr()
2066     params = hs20_ap_params()
2067     params['nai_realm'] = [ "0,example.com,21[2:4]" ]
2068     hapd = hostapd.add_ap(apdev[0], params)
2069
2070     dev[0].request("SET pmf 1")
2071     dev[0].hs20_enable()
2072     dev[0].add_cred_values(default_cred())
2073     interworking_select(dev[0], bssid, freq="2412")
2074     interworking_connect(dev[0], bssid, "TTLS")
2075
2076     hapd.request("HS20_WNM_NOTIF " + addr + " https://example.com/")
2077     ev = dev[0].wait_event(["HS20-SUBSCRIPTION-REMEDIATION"], timeout=5)
2078     if ev is None:
2079         raise Exception("Timeout on subscription remediation notice")
2080     if " 1 https://example.com/" not in ev:
2081         raise Exception("Unexpected subscription remediation event contents")
2082
2083     hapd.request("HS20_WNM_NOTIF " + addr)
2084     ev = dev[0].wait_event(["HS20-SUBSCRIPTION-REMEDIATION"], timeout=5)
2085     if ev is None:
2086         raise Exception("Timeout on subscription remediation notice")
2087     if not ev.endswith("HS20-SUBSCRIPTION-REMEDIATION "):
2088         raise Exception("Unexpected subscription remediation event contents: " + ev)
2089
2090     if "FAIL" not in hapd.request("HS20_WNM_NOTIF "):
2091         raise Exception("Unexpected HS20_WNM_NOTIF success")
2092     if "FAIL" not in hapd.request("HS20_WNM_NOTIF foo"):
2093         raise Exception("Unexpected HS20_WNM_NOTIF success")
2094     if "FAIL" not in hapd.request("HS20_WNM_NOTIF " + addr + " https://12345678923456789842345678456783456712345678923456789842345678456783456712345678923456789842345678456783456712345678923456789842345678456783456712345678923456789842345678456783456712345678923456789842345678456783456712345678923456789842345678456783456712345678927.very.long.example.com/"):
2095         raise Exception("Unexpected HS20_WNM_NOTIF success")
2096
2097 def test_ap_hs20_session_info(dev, apdev):
2098     """Hotspot 2.0 connection and session information from RADIUS"""
2099     check_eap_capa(dev[0], "MSCHAPV2")
2100     try:
2101         _test_ap_hs20_session_info(dev, apdev)
2102     finally:
2103         dev[0].request("SET pmf 0")
2104
2105 def _test_ap_hs20_session_info(dev, apdev):
2106     bssid = apdev[0]['bssid']
2107     params = hs20_ap_params()
2108     params['nai_realm'] = [ "0,example.com,21[2:4]" ]
2109     hostapd.add_ap(apdev[0], params)
2110
2111     dev[0].request("SET pmf 1")
2112     dev[0].hs20_enable()
2113     dev[0].add_cred_values({ 'realm': "example.com",
2114                              'username': "hs20-session-info-test",
2115                              'password': "password" })
2116     interworking_select(dev[0], bssid, freq="2412")
2117     interworking_connect(dev[0], bssid, "TTLS")
2118     ev = dev[0].wait_event(["ESS-DISASSOC-IMMINENT"], timeout=10)
2119     if ev is None:
2120         raise Exception("Timeout on ESS disassociation imminent notice")
2121     if " 1 59904 https://example.com/" not in ev:
2122         raise Exception("Unexpected ESS disassociation imminent event contents")
2123     ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
2124     if ev is None:
2125         raise Exception("Scan not started")
2126     ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=30)
2127     if ev is None:
2128         raise Exception("Scan not completed")
2129
2130 def test_ap_hs20_osen(dev, apdev):
2131     """Hotspot 2.0 OSEN connection"""
2132     params = { 'ssid': "osen",
2133                'osen': "1",
2134                'auth_server_addr': "127.0.0.1",
2135                'auth_server_port': "1812",
2136                'auth_server_shared_secret': "radius" }
2137     hostapd.add_ap(apdev[0], params)
2138
2139     dev[1].connect("osen", key_mgmt="NONE", scan_freq="2412",
2140                    wait_connect=False)
2141     dev[2].connect("osen", key_mgmt="NONE", wep_key0='"hello"',
2142                    scan_freq="2412", wait_connect=False)
2143     dev[0].flush_scan_cache()
2144     dev[0].connect("osen", proto="OSEN", key_mgmt="OSEN", pairwise="CCMP",
2145                    group="GTK_NOT_USED",
2146                    eap="WFA-UNAUTH-TLS", identity="osen@example.com",
2147                    ca_cert="auth_serv/ca.pem",
2148                    scan_freq="2412")
2149     res = dev[0].get_bss(apdev[0]['bssid'])['flags']
2150     if "[OSEN-OSEN-CCMP]" not in res:
2151         raise Exception("OSEN not reported in BSS")
2152     if "[WEP]" in res:
2153         raise Exception("WEP reported in BSS")
2154     res = dev[0].request("SCAN_RESULTS")
2155     if "[OSEN-OSEN-CCMP]" not in res:
2156         raise Exception("OSEN not reported in SCAN_RESULTS")
2157
2158     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
2159     wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
2160     wpas.connect("osen", proto="OSEN", key_mgmt="OSEN", pairwise="CCMP",
2161                  group="GTK_NOT_USED",
2162                  eap="WFA-UNAUTH-TLS", identity="osen@example.com",
2163                  ca_cert="auth_serv/ca.pem",
2164                  scan_freq="2412")
2165     wpas.request("DISCONNECT")
2166
2167 def test_ap_hs20_network_preference(dev, apdev):
2168     """Hotspot 2.0 network selection with preferred home network"""
2169     check_eap_capa(dev[0], "MSCHAPV2")
2170     bssid = apdev[0]['bssid']
2171     params = hs20_ap_params()
2172     hostapd.add_ap(apdev[0], params)
2173
2174     dev[0].hs20_enable()
2175     values = { 'realm': "example.com",
2176                'username': "hs20-test",
2177                'password': "password",
2178                'domain': "example.com" }
2179     dev[0].add_cred_values(values)
2180
2181     id = dev[0].add_network()
2182     dev[0].set_network_quoted(id, "ssid", "home")
2183     dev[0].set_network_quoted(id, "psk", "12345678")
2184     dev[0].set_network(id, "priority", "1")
2185     dev[0].request("ENABLE_NETWORK %s no-connect" % id)
2186
2187     dev[0].scan_for_bss(bssid, freq="2412")
2188     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2189     ev = dev[0].wait_connected(timeout=15)
2190     if bssid not in ev:
2191         raise Exception("Unexpected network selected")
2192
2193     bssid2 = apdev[1]['bssid']
2194     params = hostapd.wpa2_params(ssid="home", passphrase="12345678")
2195     hostapd.add_ap(apdev[1], params)
2196
2197     dev[0].scan_for_bss(bssid2, freq="2412")
2198     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2199     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2200                             "INTERWORKING-ALREADY-CONNECTED" ], timeout=15)
2201     if ev is None:
2202         raise Exception("Connection timed out")
2203     if "INTERWORKING-ALREADY-CONNECTED" in ev:
2204         raise Exception("No roam to higher priority network")
2205     if bssid2 not in ev:
2206         raise Exception("Unexpected network selected")
2207
2208 def test_ap_hs20_network_preference2(dev, apdev):
2209     """Hotspot 2.0 network selection with preferred credential"""
2210     check_eap_capa(dev[0], "MSCHAPV2")
2211     bssid2 = apdev[1]['bssid']
2212     params = hostapd.wpa2_params(ssid="home", passphrase="12345678")
2213     hostapd.add_ap(apdev[1], params)
2214
2215     dev[0].hs20_enable()
2216     values = { 'realm': "example.com",
2217                'username': "hs20-test",
2218                'password': "password",
2219                'domain': "example.com",
2220                'priority': "1" }
2221     dev[0].add_cred_values(values)
2222
2223     id = dev[0].add_network()
2224     dev[0].set_network_quoted(id, "ssid", "home")
2225     dev[0].set_network_quoted(id, "psk", "12345678")
2226     dev[0].request("ENABLE_NETWORK %s no-connect" % id)
2227
2228     dev[0].scan_for_bss(bssid2, freq="2412")
2229     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2230     ev = dev[0].wait_connected(timeout=15)
2231     if bssid2 not in ev:
2232         raise Exception("Unexpected network selected")
2233
2234     bssid = apdev[0]['bssid']
2235     params = hs20_ap_params()
2236     hostapd.add_ap(apdev[0], params)
2237
2238     dev[0].scan_for_bss(bssid, freq="2412")
2239     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2240     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2241                             "INTERWORKING-ALREADY-CONNECTED" ], timeout=15)
2242     if ev is None:
2243         raise Exception("Connection timed out")
2244     if "INTERWORKING-ALREADY-CONNECTED" in ev:
2245         raise Exception("No roam to higher priority network")
2246     if bssid not in ev:
2247         raise Exception("Unexpected network selected")
2248
2249 def test_ap_hs20_network_preference3(dev, apdev):
2250     """Hotspot 2.0 network selection with two credential (one preferred)"""
2251     check_eap_capa(dev[0], "MSCHAPV2")
2252     bssid = apdev[0]['bssid']
2253     params = hs20_ap_params()
2254     hostapd.add_ap(apdev[0], params)
2255
2256     bssid2 = apdev[1]['bssid']
2257     params = hs20_ap_params(ssid="test-hs20b")
2258     params['nai_realm'] = "0,example.org,13[5:6],21[2:4][5:7]"
2259     hostapd.add_ap(apdev[1], params)
2260
2261     dev[0].hs20_enable()
2262     values = { 'realm': "example.com",
2263                'username': "hs20-test",
2264                'password': "password",
2265                'priority': "1" }
2266     dev[0].add_cred_values(values)
2267     values = { 'realm': "example.org",
2268                'username': "hs20-test",
2269                'password': "password" }
2270     id = dev[0].add_cred_values(values)
2271
2272     dev[0].scan_for_bss(bssid, freq="2412")
2273     dev[0].scan_for_bss(bssid2, freq="2412")
2274     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2275     ev = dev[0].wait_connected(timeout=15)
2276     if bssid not in ev:
2277         raise Exception("Unexpected network selected")
2278
2279     dev[0].set_cred(id, "priority", "2")
2280     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2281     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2282                             "INTERWORKING-ALREADY-CONNECTED" ], timeout=15)
2283     if ev is None:
2284         raise Exception("Connection timed out")
2285     if "INTERWORKING-ALREADY-CONNECTED" in ev:
2286         raise Exception("No roam to higher priority network")
2287     if bssid2 not in ev:
2288         raise Exception("Unexpected network selected")
2289
2290 def test_ap_hs20_network_preference4(dev, apdev):
2291     """Hotspot 2.0 network selection with username vs. SIM credential"""
2292     check_eap_capa(dev[0], "MSCHAPV2")
2293     bssid = apdev[0]['bssid']
2294     params = hs20_ap_params()
2295     hostapd.add_ap(apdev[0], params)
2296
2297     bssid2 = apdev[1]['bssid']
2298     params = hs20_ap_params(ssid="test-hs20b")
2299     params['hessid'] = bssid2
2300     params['anqp_3gpp_cell_net'] = "555,444"
2301     params['domain_name'] = "wlan.mnc444.mcc555.3gppnetwork.org"
2302     hostapd.add_ap(apdev[1], params)
2303
2304     dev[0].hs20_enable()
2305     values = { 'realm': "example.com",
2306                'username': "hs20-test",
2307                'password': "password",
2308                'priority': "1" }
2309     dev[0].add_cred_values(values)
2310     values = { 'imsi': "555444-333222111",
2311                'eap': "SIM",
2312                'milenage': "5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123" }
2313     id = dev[0].add_cred_values(values)
2314
2315     dev[0].scan_for_bss(bssid, freq="2412")
2316     dev[0].scan_for_bss(bssid2, freq="2412")
2317     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2318     ev = dev[0].wait_connected(timeout=15)
2319     if bssid not in ev:
2320         raise Exception("Unexpected network selected")
2321
2322     dev[0].set_cred(id, "priority", "2")
2323     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2324     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED",
2325                             "INTERWORKING-ALREADY-CONNECTED" ], timeout=15)
2326     if ev is None:
2327         raise Exception("Connection timed out")
2328     if "INTERWORKING-ALREADY-CONNECTED" in ev:
2329         raise Exception("No roam to higher priority network")
2330     if bssid2 not in ev:
2331         raise Exception("Unexpected network selected")
2332
2333 def test_ap_hs20_interworking_select_blocking_scan(dev, apdev):
2334     """Ongoing INTERWORKING_SELECT blocking SCAN"""
2335     check_eap_capa(dev[0], "MSCHAPV2")
2336     bssid = apdev[0]['bssid']
2337     params = hs20_ap_params()
2338     hostapd.add_ap(apdev[0], params)
2339
2340     dev[0].hs20_enable()
2341     values = { 'realm': "example.com",
2342                'username': "hs20-test",
2343                'password': "password",
2344                'domain': "example.com" }
2345     dev[0].add_cred_values(values)
2346
2347     dev[0].scan_for_bss(bssid, freq="2412")
2348     dev[0].request("INTERWORKING_SELECT auto freq=2412")
2349     if "FAIL-BUSY" not in dev[0].request("SCAN"):
2350         raise Exception("Unexpected SCAN command result")
2351     dev[0].wait_connected(timeout=15)
2352
2353 def test_ap_hs20_fetch_osu(dev, apdev):
2354     """Hotspot 2.0 OSU provider and icon fetch"""
2355     bssid = apdev[0]['bssid']
2356     params = hs20_ap_params()
2357     params['hs20_icon'] = "128:80:zxx:image/png:w1fi_logo:w1fi_logo.png"
2358     params['osu_ssid'] = '"HS 2.0 OSU open"'
2359     params['osu_method_list'] = "1"
2360     params['osu_friendly_name'] = [ "eng:Test OSU", "fin:Testi-OSU" ]
2361     params['osu_icon'] = "w1fi_logo"
2362     params['osu_service_desc'] = [ "eng:Example services", "fin:Esimerkkipalveluja" ]
2363     params['osu_server_uri'] = "https://example.com/osu/"
2364     hostapd.add_ap(apdev[0], params)
2365
2366     bssid2 = apdev[1]['bssid']
2367     params = hs20_ap_params(ssid="test-hs20b")
2368     params['hessid'] = bssid2
2369     params['hs20_icon'] = "128:80:zxx:image/png:w1fi_logo:w1fi_logo.png"
2370     params['osu_ssid'] = '"HS 2.0 OSU OSEN"'
2371     params['osu_method_list'] = "0"
2372     params['osu_nai'] = "osen@example.com"
2373     params['osu_friendly_name'] = [ "eng:Test2 OSU", "fin:Testi2-OSU" ]
2374     params['osu_icon'] = "w1fi_logo"
2375     params['osu_service_desc'] = [ "eng:Example services2", "fin:Esimerkkipalveluja2" ]
2376     params['osu_server_uri'] = "https://example.org/osu/"
2377     hostapd.add_ap(apdev[1], params)
2378
2379     with open("w1fi_logo.png", "r") as f:
2380         orig_logo = f.read()
2381     dev[0].hs20_enable()
2382     dir = "/tmp/osu-fetch"
2383     if os.path.isdir(dir):
2384        files = [ f for f in os.listdir(dir) if f.startswith("osu-") ]
2385        for f in files:
2386            os.remove(dir + "/" + f)
2387     else:
2388         try:
2389             os.makedirs(dir)
2390         except:
2391             pass
2392     try:
2393         dev[1].scan_for_bss(bssid, freq="2412")
2394         dev[2].scan_for_bss(bssid, freq="2412")
2395         dev[0].request("SET osu_dir " + dir)
2396         dev[0].request("FETCH_OSU")
2397         if "FAIL" not in dev[1].request("HS20_ICON_REQUEST foo w1fi_logo"):
2398             raise Exception("Invalid HS20_ICON_REQUEST accepted")
2399         if "OK" not in dev[1].request("HS20_ICON_REQUEST " + bssid + " w1fi_logo"):
2400             raise Exception("HS20_ICON_REQUEST failed")
2401         if "OK" not in dev[2].request("REQ_HS20_ICON " + bssid + " w1fi_logo"):
2402             raise Exception("REQ_HS20_ICON failed")
2403         icons = 0
2404         while True:
2405             ev = dev[0].wait_event(["OSU provider fetch completed",
2406                                     "RX-HS20-ANQP-ICON"], timeout=15)
2407             if ev is None:
2408                 raise Exception("Timeout on OSU fetch")
2409             if "OSU provider fetch completed" in ev:
2410                 break
2411             if "RX-HS20-ANQP-ICON" in ev:
2412                 with open(ev.split(' ')[1], "r") as f:
2413                     logo = f.read()
2414                     if logo == orig_logo:
2415                         icons += 1
2416
2417         with open(dir + "/osu-providers.txt", "r") as f:
2418             prov = f.read()
2419             logger.debug("osu-providers.txt: " + prov)
2420         if "OSU-PROVIDER " + bssid not in prov:
2421             raise Exception("Missing OSU_PROVIDER(1)")
2422         if "OSU-PROVIDER " + bssid2 not in prov:
2423             raise Exception("Missing OSU_PROVIDER(2)")
2424     finally:
2425         files = [ f for f in os.listdir(dir) if f.startswith("osu-") ]
2426         for f in files:
2427             os.remove(dir + "/" + f)
2428         os.rmdir(dir)
2429
2430     if icons != 2:
2431         raise Exception("Unexpected number of icons fetched")
2432
2433     ev = dev[1].wait_event(["GAS-QUERY-START"], timeout=5)
2434     if ev is None:
2435         raise Exception("Timeout on GAS-QUERY-DONE")
2436     ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=5)
2437     if ev is None:
2438         raise Exception("Timeout on GAS-QUERY-DONE")
2439     if "freq=2412 status_code=0 result=SUCCESS" not in ev:
2440         raise Exception("Unexpected GAS-QUERY-DONE: " + ev)
2441     ev = dev[1].wait_event(["RX-HS20-ANQP"], timeout=15)
2442     if ev is None:
2443         raise Exception("Timeout on icon fetch")
2444     if "Icon Binary File" not in ev:
2445         raise Exception("Unexpected ANQP element")
2446
2447     ev = dev[2].wait_event(["RX-HS20-ICON"], timeout=5)
2448     if ev is None:
2449         raise Exception("Timeout on RX-HS20-ICON")
2450     event_icon_len = ev.split(' ')[3]
2451     if " w1fi_logo " not in ev:
2452         raise Exception("RX-HS20-ICON did not have the expected file name")
2453     if bssid not in ev:
2454         raise Exception("RX-HS20-ICON did not have the expected BSSID")
2455     if "FAIL" in dev[2].request("GET_HS20_ICON " + bssid + " w1fi_logo 0 10"):
2456         raise Exception("GET_HS20_ICON 0..10 failed")
2457     if "FAIL" in dev[2].request("GET_HS20_ICON " + bssid + " w1fi_logo 5 10"):
2458         raise Exception("GET_HS20_ICON 5..15 failed")
2459     if "FAIL" not in  dev[2].request("GET_HS20_ICON " + bssid + " w1fi_logo 100000 10"):
2460         raise Exception("Unexpected success of GET_HS20_ICON with too large offset")
2461     icon = ""
2462     pos = 0
2463     while True:
2464         if pos > 100000:
2465             raise Exception("Unexpectedly long icon")
2466         res = dev[2].request("GET_HS20_ICON " + bssid + " w1fi_logo %d 1000" % pos)
2467         if res.startswith("FAIL"):
2468             break
2469         icon += base64.b64decode(res)
2470         pos += 1000
2471     hex = binascii.hexlify(icon)
2472     if not hex.startswith("0009696d6167652f706e677d1d"):
2473         raise Exception("Unexpected beacon binary header: " + hex)
2474     with open('w1fi_logo.png', 'r') as f:
2475         data = f.read()
2476         if icon[13:] != data:
2477             raise Exception("Unexpected icon data")
2478     if len(icon) != int(event_icon_len):
2479         raise Exception("Unexpected RX-HS20-ICON event length: " + event_icon_len)
2480
2481     for i in range(3):
2482         if "OK" not in dev[i].request("REQ_HS20_ICON " + bssid + " w1fi_logo"):
2483             raise Exception("REQ_HS20_ICON failed [2]")
2484     for i in range(3):
2485         ev = dev[i].wait_event(["RX-HS20-ICON"], timeout=5)
2486         if ev is None:
2487             raise Exception("Timeout on RX-HS20-ICON [2]")
2488
2489     if "FAIL" not in dev[2].request("DEL_HS20_ICON foo w1fi_logo"):
2490         raise Exception("Invalid DEL_HS20_ICON accepted")
2491     if "OK" not in dev[2].request("DEL_HS20_ICON " + bssid + " w1fi_logo"):
2492         raise Exception("DEL_HS20_ICON failed")
2493     if "OK" not in dev[1].request("DEL_HS20_ICON " + bssid):
2494         raise Exception("DEL_HS20_ICON failed")
2495     if "OK" not in dev[0].request("DEL_HS20_ICON "):
2496         raise Exception("DEL_HS20_ICON failed")
2497     for i in range(3):
2498         if "FAIL" not in dev[i].request("DEL_HS20_ICON "):
2499             raise Exception("DEL_HS20_ICON accepted when no icons left")
2500
2501 def get_icon(dev, bssid, iconname):
2502     icon = ""
2503     pos = 0
2504     while True:
2505         if pos > 100000:
2506             raise Exception("Unexpectedly long icon")
2507         res = dev.request("GET_HS20_ICON " + bssid + " " + iconname + " %d 3000" % pos)
2508         if res.startswith("FAIL"):
2509             break
2510         icon += base64.b64decode(res)
2511         pos += 3000
2512     if len(icon) < 13:
2513         raise Exception("Too short GET_HS20_ICON response")
2514     return icon[0:13], icon[13:]
2515
2516 def test_ap_hs20_req_hs20_icon(dev, apdev):
2517     """Hotspot 2.0 OSU provider and multi-icon fetch with REQ_HS20_ICON"""
2518     bssid = apdev[0]['bssid']
2519     params = hs20_ap_params()
2520     params['hs20_icon'] = [ "128:80:zxx:image/png:w1fi_logo:w1fi_logo.png",
2521                             "128:80:zxx:image/png:test_logo:auth_serv/sha512-server.pem" ]
2522     params['osu_ssid'] = '"HS 2.0 OSU open"'
2523     params['osu_method_list'] = "1"
2524     params['osu_friendly_name'] = [ "eng:Test OSU", "fin:Testi-OSU" ]
2525     params['osu_icon'] = [ "w1fi_logo", "w1fi_logo2" ]
2526     params['osu_service_desc'] = [ "eng:Example services", "fin:Esimerkkipalveluja" ]
2527     params['osu_server_uri'] = "https://example.com/osu/"
2528     hostapd.add_ap(apdev[0], params)
2529
2530     dev[0].scan_for_bss(bssid, freq="2412")
2531
2532     # First, fetch two icons from the AP to wpa_supplicant
2533
2534     if "OK" not in dev[0].request("REQ_HS20_ICON " + bssid + " w1fi_logo"):
2535         raise Exception("REQ_HS20_ICON failed")
2536     ev = dev[0].wait_event(["RX-HS20-ICON"], timeout=5)
2537     if ev is None:
2538         raise Exception("Timeout on RX-HS20-ICON (1)")
2539
2540     if "OK" not in dev[0].request("REQ_HS20_ICON " + bssid + " test_logo"):
2541         raise Exception("REQ_HS20_ICON failed")
2542     ev = dev[0].wait_event(["RX-HS20-ICON"], timeout=5)
2543     if ev is None:
2544         raise Exception("Timeout on RX-HS20-ICON (2)")
2545
2546     # Then, fetch the icons from wpa_supplicant for validation
2547
2548     hdr, data1 = get_icon(dev[0], bssid, "w1fi_logo")
2549     hdr, data2 = get_icon(dev[0], bssid, "test_logo")
2550
2551     with open('w1fi_logo.png', 'r') as f:
2552         data = f.read()
2553         if data1 != data:
2554             raise Exception("Unexpected icon data (1)")
2555
2556     with open('auth_serv/sha512-server.pem', 'r') as f:
2557         data = f.read()
2558         if data2 != data:
2559             raise Exception("Unexpected icon data (2)")
2560
2561     # Finally, delete the icons from wpa_supplicant
2562
2563     if "OK" not in dev[0].request("DEL_HS20_ICON " + bssid + " w1fi_logo"):
2564         raise Exception("DEL_HS20_ICON failed")
2565     if "OK" not in dev[0].request("DEL_HS20_ICON " + bssid + " test_logo"):
2566         raise Exception("DEL_HS20_ICON failed")
2567
2568 def test_ap_hs20_req_hs20_icon_parallel(dev, apdev):
2569     """Hotspot 2.0 OSU provider and multi-icon parallel fetch with REQ_HS20_ICON"""
2570     bssid = apdev[0]['bssid']
2571     params = hs20_ap_params()
2572     params['hs20_icon'] = [ "128:80:zxx:image/png:w1fi_logo:w1fi_logo.png",
2573                             "128:80:zxx:image/png:test_logo:auth_serv/sha512-server.pem" ]
2574     params['osu_ssid'] = '"HS 2.0 OSU open"'
2575     params['osu_method_list'] = "1"
2576     params['osu_friendly_name'] = [ "eng:Test OSU", "fin:Testi-OSU" ]
2577     params['osu_icon'] = [ "w1fi_logo", "w1fi_logo2" ]
2578     params['osu_service_desc'] = [ "eng:Example services", "fin:Esimerkkipalveluja" ]
2579     params['osu_server_uri'] = "https://example.com/osu/"
2580     hostapd.add_ap(apdev[0], params)
2581
2582     dev[0].scan_for_bss(bssid, freq="2412")
2583
2584     # First, fetch two icons from the AP to wpa_supplicant
2585
2586     if "OK" not in dev[0].request("REQ_HS20_ICON " + bssid + " w1fi_logo"):
2587         raise Exception("REQ_HS20_ICON failed")
2588
2589     if "OK" not in dev[0].request("REQ_HS20_ICON " + bssid + " test_logo"):
2590         raise Exception("REQ_HS20_ICON failed")
2591     ev = dev[0].wait_event(["RX-HS20-ICON"], timeout=5)
2592     if ev is None:
2593         raise Exception("Timeout on RX-HS20-ICON (1)")
2594     ev = dev[0].wait_event(["RX-HS20-ICON"], timeout=5)
2595     if ev is None:
2596         raise Exception("Timeout on RX-HS20-ICON (2)")
2597
2598     # Then, fetch the icons from wpa_supplicant for validation
2599
2600     hdr, data1 = get_icon(dev[0], bssid, "w1fi_logo")
2601     hdr, data2 = get_icon(dev[0], bssid, "test_logo")
2602
2603     with open('w1fi_logo.png', 'r') as f:
2604         data = f.read()
2605         if data1 != data:
2606             raise Exception("Unexpected icon data (1)")
2607
2608     with open('auth_serv/sha512-server.pem', 'r') as f:
2609         data = f.read()
2610         if data2 != data:
2611             raise Exception("Unexpected icon data (2)")
2612
2613     # Finally, delete the icons from wpa_supplicant
2614
2615     if "OK" not in dev[0].request("DEL_HS20_ICON " + bssid + " w1fi_logo"):
2616         raise Exception("DEL_HS20_ICON failed")
2617     if "OK" not in dev[0].request("DEL_HS20_ICON " + bssid + " test_logo"):
2618         raise Exception("DEL_HS20_ICON failed")
2619
2620 def test_ap_hs20_fetch_osu_stop(dev, apdev):
2621     """Hotspot 2.0 OSU provider fetch stopped"""
2622     bssid = apdev[0]['bssid']
2623     params = hs20_ap_params()
2624     params['hs20_icon'] = "128:80:zxx:image/png:w1fi_logo:w1fi_logo.png"
2625     params['osu_ssid'] = '"HS 2.0 OSU open"'
2626     params['osu_method_list'] = "1"
2627     params['osu_friendly_name'] = [ "eng:Test OSU", "fin:Testi-OSU" ]
2628     params['osu_icon'] = "w1fi_logo"
2629     params['osu_service_desc'] = [ "eng:Example services", "fin:Esimerkkipalveluja" ]
2630     params['osu_server_uri'] = "https://example.com/osu/"
2631     hapd = hostapd.add_ap(apdev[0], params)
2632
2633     dev[0].hs20_enable()
2634     dir = "/tmp/osu-fetch"
2635     if os.path.isdir(dir):
2636        files = [ f for f in os.listdir(dir) if f.startswith("osu-") ]
2637        for f in files:
2638            os.remove(dir + "/" + f)
2639     else:
2640         try:
2641             os.makedirs(dir)
2642         except:
2643             pass
2644     try:
2645         dev[0].request("SET osu_dir " + dir)
2646         dev[0].request("SCAN freq=2412-2462")
2647         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=10)
2648         if ev is None:
2649             raise Exception("Scan did not start")
2650         if "FAIL" not in dev[0].request("FETCH_OSU"):
2651             raise Exception("FETCH_OSU accepted while scanning")
2652         ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
2653         if ev is None:
2654             raise Exception("Scan timed out")
2655         hapd.set("ext_mgmt_frame_handling", "1")
2656         dev[0].request("FETCH_ANQP")
2657         if "FAIL" not in dev[0].request("FETCH_OSU"):
2658             raise Exception("FETCH_OSU accepted while in FETCH_ANQP")
2659         dev[0].request("STOP_FETCH_ANQP")
2660         dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
2661         dev[0].dump_monitor()
2662         hapd.dump_monitor()
2663         dev[0].request("INTERWORKING_SELECT freq=2412")
2664         for i in range(5):
2665             msg = hapd.mgmt_rx()
2666             if msg['subtype'] == 13:
2667                 break
2668         if "FAIL" not in dev[0].request("FETCH_OSU"):
2669             raise Exception("FETCH_OSU accepted while in INTERWORKING_SELECT")
2670         ev = dev[0].wait_event(["INTERWORKING-AP", "INTERWORKING-NO-MATCH"],
2671                                timeout=15)
2672         if ev is None:
2673             raise Exception("Network selection timed out")
2674
2675         dev[0].dump_monitor()
2676         if "OK" not in dev[0].request("FETCH_OSU"):
2677             raise Exception("FETCH_OSU failed")
2678         dev[0].request("CANCEL_FETCH_OSU")
2679
2680         for i in range(15):
2681             time.sleep(0.5)
2682             if dev[0].get_driver_status_field("scan_state") == "SCAN_COMPLETED":
2683                 break
2684
2685         dev[0].dump_monitor()
2686         if "OK" not in dev[0].request("FETCH_OSU"):
2687             raise Exception("FETCH_OSU failed")
2688         if "FAIL" not in dev[0].request("FETCH_OSU"):
2689             raise Exception("FETCH_OSU accepted while in FETCH_OSU")
2690         ev = dev[0].wait_event(["GAS-QUERY-START"], 10)
2691         if ev is None:
2692             raise Exception("GAS timed out")
2693         if "FAIL" not in dev[0].request("FETCH_OSU"):
2694             raise Exception("FETCH_OSU accepted while in FETCH_OSU")
2695         dev[0].request("CANCEL_FETCH_OSU")
2696         ev = dev[0].wait_event(["GAS-QUERY-DONE"], 10)
2697         if ev is None:
2698             raise Exception("GAS event timed out after CANCEL_FETCH_OSU")
2699     finally:
2700         files = [ f for f in os.listdir(dir) if f.startswith("osu-") ]
2701         for f in files:
2702             os.remove(dir + "/" + f)
2703         os.rmdir(dir)
2704
2705 def test_ap_hs20_ft(dev, apdev):
2706     """Hotspot 2.0 connection with FT"""
2707     check_eap_capa(dev[0], "MSCHAPV2")
2708     bssid = apdev[0]['bssid']
2709     params = hs20_ap_params()
2710     params['wpa_key_mgmt'] = "FT-EAP"
2711     params['nas_identifier'] = "nas1.w1.fi"
2712     params['r1_key_holder'] = "000102030405"
2713     params["mobility_domain"] = "a1b2"
2714     params["reassociation_deadline"] = "1000"
2715     hostapd.add_ap(apdev[0], params)
2716
2717     dev[0].hs20_enable()
2718     id = dev[0].add_cred_values({ 'realm': "example.com",
2719                                   'username': "hs20-test",
2720                                   'password': "password",
2721                                   'ca_cert': "auth_serv/ca.pem",
2722                                   'domain': "example.com",
2723                                   'update_identifier': "1234" })
2724     interworking_select(dev[0], bssid, "home", freq="2412")
2725     interworking_connect(dev[0], bssid, "TTLS")
2726
2727 def test_ap_hs20_remediation_sql(dev, apdev, params):
2728     """Hotspot 2.0 connection and remediation required using SQLite for user DB"""
2729     check_eap_capa(dev[0], "MSCHAPV2")
2730     try:
2731         import sqlite3
2732     except ImportError:
2733         raise HwsimSkip("No sqlite3 module available")
2734     dbfile = os.path.join(params['logdir'], "eap-user.db")
2735     try:
2736         os.remove(dbfile)
2737     except:
2738         pass
2739     con = sqlite3.connect(dbfile)
2740     with con:
2741         cur = con.cursor()
2742         cur.execute("CREATE TABLE users(identity TEXT PRIMARY KEY, methods TEXT, password TEXT, remediation TEXT, phase2 INTEGER)")
2743         cur.execute("CREATE TABLE wildcards(identity TEXT PRIMARY KEY, methods TEXT)")
2744         cur.execute("INSERT INTO users(identity,methods,password,phase2,remediation) VALUES ('user-mschapv2','TTLS-MSCHAPV2','password',1,'user')")
2745         cur.execute("INSERT INTO wildcards(identity,methods) VALUES ('','TTLS,TLS')")
2746         cur.execute("CREATE TABLE authlog(timestamp TEXT, session TEXT, nas_ip TEXT, username TEXT, note TEXT)")
2747
2748     try:
2749         params = { "ssid": "as", "beacon_int": "2000",
2750                    "radius_server_clients": "auth_serv/radius_clients.conf",
2751                    "radius_server_auth_port": '18128',
2752                    "eap_server": "1",
2753                    "eap_user_file": "sqlite:" + dbfile,
2754                    "ca_cert": "auth_serv/ca.pem",
2755                    "server_cert": "auth_serv/server.pem",
2756                    "private_key": "auth_serv/server.key",
2757                    "subscr_remediation_url": "https://example.org/",
2758                    "subscr_remediation_method": "1" }
2759         hostapd.add_ap(apdev[1], params)
2760
2761         bssid = apdev[0]['bssid']
2762         params = hs20_ap_params()
2763         params['auth_server_port'] = "18128"
2764         hostapd.add_ap(apdev[0], params)
2765
2766         dev[0].request("SET pmf 1")
2767         dev[0].hs20_enable()
2768         id = dev[0].add_cred_values({ 'realm': "example.com",
2769                                       'username': "user-mschapv2",
2770                                       'password': "password",
2771                                       'ca_cert': "auth_serv/ca.pem" })
2772         interworking_select(dev[0], bssid, freq="2412")
2773         interworking_connect(dev[0], bssid, "TTLS")
2774         ev = dev[0].wait_event(["HS20-SUBSCRIPTION-REMEDIATION"], timeout=5)
2775         if ev is None:
2776             raise Exception("Timeout on subscription remediation notice")
2777         if " 1 https://example.org/" not in ev:
2778             raise Exception("Unexpected subscription remediation event contents")
2779
2780         with con:
2781             cur = con.cursor()
2782             cur.execute("SELECT * from authlog")
2783             rows = cur.fetchall()
2784             if len(rows) < 1:
2785                 raise Exception("No authlog entries")
2786
2787     finally:
2788         os.remove(dbfile)
2789         dev[0].request("SET pmf 0")
2790
2791 def test_ap_hs20_external_selection(dev, apdev):
2792     """Hotspot 2.0 connection using external network selection and creation"""
2793     check_eap_capa(dev[0], "MSCHAPV2")
2794     bssid = apdev[0]['bssid']
2795     params = hs20_ap_params()
2796     params['hessid'] = bssid
2797     params['disable_dgaf'] = '1'
2798     hostapd.add_ap(apdev[0], params)
2799
2800     dev[0].hs20_enable()
2801     dev[0].connect("test-hs20", proto="RSN", key_mgmt="WPA-EAP", eap="TTLS",
2802                    identity="hs20-test", password="password",
2803                    ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2804                    scan_freq="2412", update_identifier="54321")
2805     if dev[0].get_status_field("hs20") != "2":
2806         raise Exception("Unexpected hs20 indication")
2807
2808 def test_ap_hs20_random_mac_addr(dev, apdev):
2809     """Hotspot 2.0 connection with random MAC address"""
2810     check_eap_capa(dev[0], "MSCHAPV2")
2811     bssid = apdev[0]['bssid']
2812     params = hs20_ap_params()
2813     params['hessid'] = bssid
2814     params['disable_dgaf'] = '1'
2815     hapd = hostapd.add_ap(apdev[0], params)
2816
2817     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
2818     wpas.interface_add("wlan5")
2819     addr = wpas.p2p_interface_addr()
2820     wpas.request("SET mac_addr 1")
2821     wpas.request("SET preassoc_mac_addr 1")
2822     wpas.request("SET rand_addr_lifetime 60")
2823     wpas.hs20_enable()
2824     wpas.flush_scan_cache()
2825     id = wpas.add_cred_values({ 'realm': "example.com",
2826                                   'username': "hs20-test",
2827                                   'password': "password",
2828                                   'ca_cert': "auth_serv/ca.pem",
2829                                   'domain': "example.com",
2830                                   'update_identifier': "1234" })
2831     interworking_select(wpas, bssid, "home", freq="2412")
2832     interworking_connect(wpas, bssid, "TTLS")
2833     addr1 = wpas.get_driver_status_field("addr")
2834     if addr == addr1:
2835         raise Exception("Did not use random MAC address")
2836
2837     sta = hapd.get_sta(addr)
2838     if sta['addr'] != "FAIL":
2839         raise Exception("Unexpected STA association with permanent address")
2840     sta = hapd.get_sta(addr1)
2841     if sta['addr'] != addr1:
2842         raise Exception("STA association with random address not found")
2843
2844 def test_ap_hs20_multi_network_and_cred_removal(dev, apdev):
2845     """Multiple networks and cred removal"""
2846     check_eap_capa(dev[0], "MSCHAPV2")
2847     bssid = apdev[0]['bssid']
2848     params = hs20_ap_params()
2849     params['nai_realm'] = [ "0,example.com,25[3:26]"]
2850     hapd = hostapd.add_ap(apdev[0], params)
2851
2852     dev[0].add_network()
2853     dev[0].hs20_enable()
2854     id = dev[0].add_cred_values({ 'realm': "example.com",
2855                                   'username': "user",
2856                                   'password': "password" })
2857     interworking_select(dev[0], bssid, freq="2412")
2858     interworking_connect(dev[0], bssid, "PEAP")
2859     dev[0].add_network()
2860
2861     dev[0].request("DISCONNECT")
2862     dev[0].wait_disconnected(timeout=10)
2863
2864     hapd.disable()
2865     hapd.set("ssid", "another ssid")
2866     hapd.enable()
2867
2868     interworking_select(dev[0], bssid, freq="2412")
2869     interworking_connect(dev[0], bssid, "PEAP")
2870     dev[0].add_network()
2871     if len(dev[0].list_networks()) != 5:
2872         raise Exception("Unexpected number of networks prior to remove_crec")
2873
2874     dev[0].dump_monitor()
2875     dev[0].remove_cred(id)
2876     if len(dev[0].list_networks()) != 3:
2877         raise Exception("Unexpected number of networks after to remove_crec")
2878     dev[0].wait_disconnected(timeout=10)
2879
2880 def test_ap_hs20_interworking_add_network(dev, apdev):
2881     """Hotspot 2.0 connection using INTERWORKING_ADD_NETWORK"""
2882     check_eap_capa(dev[0], "MSCHAPV2")
2883     bssid = apdev[0]['bssid']
2884     params = hs20_ap_params()
2885     params['nai_realm'] = [ "0,example.com,21[3:26][6:7][99:99]" ]
2886     hostapd.add_ap(apdev[0], params)
2887
2888     dev[0].hs20_enable()
2889     dev[0].add_cred_values(default_cred(user="user"))
2890     interworking_select(dev[0], bssid, freq=2412)
2891     id = dev[0].interworking_add_network(bssid)
2892     dev[0].select_network(id, freq=2412)
2893     dev[0].wait_connected()
2894
2895 def _test_ap_hs20_proxyarp(dev, apdev):
2896     bssid = apdev[0]['bssid']
2897     params = hs20_ap_params()
2898     params['hessid'] = bssid
2899     params['disable_dgaf'] = '0'
2900     params['proxy_arp'] = '1'
2901     hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
2902     if "OK" in hapd.request("ENABLE"):
2903         raise Exception("Incomplete hostapd configuration was accepted")
2904     hapd.set("ap_isolate", "1")
2905     if "OK" in hapd.request("ENABLE"):
2906         raise Exception("Incomplete hostapd configuration was accepted")
2907     hapd.set('bridge', 'ap-br0')
2908     hapd.dump_monitor()
2909     try:
2910         hapd.enable()
2911     except:
2912         # For now, do not report failures due to missing kernel support
2913         raise HwsimSkip("Could not start hostapd - assume proxyarp not supported in kernel version")
2914     ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=10)
2915     if ev is None:
2916         raise Exception("AP startup timed out")
2917     if "AP-ENABLED" not in ev:
2918         raise Exception("AP startup failed")
2919
2920     dev[0].hs20_enable()
2921     subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
2922     subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
2923
2924     id = dev[0].add_cred_values({ 'realm': "example.com",
2925                                   'username': "hs20-test",
2926                                   'password': "password",
2927                                   'ca_cert': "auth_serv/ca.pem",
2928                                   'domain': "example.com",
2929                                   'update_identifier': "1234" })
2930     interworking_select(dev[0], bssid, "home", freq="2412")
2931     interworking_connect(dev[0], bssid, "TTLS")
2932
2933     dev[1].connect("test-hs20", key_mgmt="WPA-EAP", eap="TTLS",
2934                    identity="hs20-test", password="password",
2935                    ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
2936                    scan_freq="2412")
2937     time.sleep(0.1)
2938
2939     addr0 = dev[0].p2p_interface_addr()
2940     addr1 = dev[1].p2p_interface_addr()
2941
2942     src_ll_opt0 = "\x01\x01" + binascii.unhexlify(addr0.replace(':',''))
2943     src_ll_opt1 = "\x01\x01" + binascii.unhexlify(addr1.replace(':',''))
2944
2945     pkt = build_ns(src_ll=addr0, ip_src="aaaa:bbbb:cccc::2",
2946                    ip_dst="ff02::1:ff00:2", target="aaaa:bbbb:cccc::2",
2947                    opt=src_ll_opt0)
2948     if "OK" not in dev[0].request("DATA_TEST_FRAME " + binascii.hexlify(pkt)):
2949         raise Exception("DATA_TEST_FRAME failed")
2950
2951     pkt = build_ns(src_ll=addr1, ip_src="aaaa:bbbb:dddd::2",
2952                    ip_dst="ff02::1:ff00:2", target="aaaa:bbbb:dddd::2",
2953                    opt=src_ll_opt1)
2954     if "OK" not in dev[1].request("DATA_TEST_FRAME " + binascii.hexlify(pkt)):
2955         raise Exception("DATA_TEST_FRAME failed")
2956
2957     pkt = build_ns(src_ll=addr1, ip_src="aaaa:bbbb:eeee::2",
2958                    ip_dst="ff02::1:ff00:2", target="aaaa:bbbb:eeee::2",
2959                    opt=src_ll_opt1)
2960     if "OK" not in dev[1].request("DATA_TEST_FRAME " + binascii.hexlify(pkt)):
2961         raise Exception("DATA_TEST_FRAME failed")
2962
2963     matches = get_permanent_neighbors("ap-br0")
2964     logger.info("After connect: " + str(matches))
2965     if len(matches) != 3:
2966         raise Exception("Unexpected number of neighbor entries after connect")
2967     if 'aaaa:bbbb:cccc::2 dev ap-br0 lladdr 02:00:00:00:00:00 PERMANENT' not in matches:
2968         raise Exception("dev0 addr missing")
2969     if 'aaaa:bbbb:dddd::2 dev ap-br0 lladdr 02:00:00:00:01:00 PERMANENT' not in matches:
2970         raise Exception("dev1 addr(1) missing")
2971     if 'aaaa:bbbb:eeee::2 dev ap-br0 lladdr 02:00:00:00:01:00 PERMANENT' not in matches:
2972         raise Exception("dev1 addr(2) missing")
2973     dev[0].request("DISCONNECT")
2974     dev[1].request("DISCONNECT")
2975     time.sleep(0.5)
2976     matches = get_permanent_neighbors("ap-br0")
2977     logger.info("After disconnect: " + str(matches))
2978     if len(matches) > 0:
2979         raise Exception("Unexpected neighbor entries after disconnect")
2980
2981 def test_ap_hs20_hidden_ssid_in_scan_res(dev, apdev):
2982     """Hotspot 2.0 connection with hidden SSId in scan results"""
2983     check_eap_capa(dev[0], "MSCHAPV2")
2984     bssid = apdev[0]['bssid']
2985
2986     hapd = hostapd.add_ap(apdev[0], { "ssid": 'secret',
2987                                       "ignore_broadcast_ssid": "1" })
2988     dev[0].scan_for_bss(bssid, freq=2412)
2989     hapd.disable()
2990     hapd_global = hostapd.HostapdGlobal(apdev[0])
2991     hapd_global.flush()
2992     hapd_global.remove(apdev[0]['ifname'])
2993
2994     params = hs20_ap_params()
2995     params['hessid'] = bssid
2996     hapd = hostapd.add_ap(apdev[0], params)
2997
2998     dev[0].hs20_enable()
2999     id = dev[0].add_cred_values({ 'realm': "example.com",
3000                                   'username': "hs20-test",
3001                                   'password': "password",
3002                                   'ca_cert': "auth_serv/ca.pem",
3003                                   'domain': "example.com" })
3004     interworking_select(dev[0], bssid, "home", freq="2412")
3005     interworking_connect(dev[0], bssid, "TTLS")
3006
3007     # clear BSS table to avoid issues in following test cases
3008     dev[0].request("DISCONNECT")
3009     dev[0].wait_disconnected()
3010     hapd.disable()
3011     dev[0].flush_scan_cache()
3012     dev[0].flush_scan_cache()
3013
3014 def test_ap_hs20_proxyarp(dev, apdev):
3015     """Hotspot 2.0 and ProxyARP"""
3016     check_eap_capa(dev[0], "MSCHAPV2")
3017     try:
3018         _test_ap_hs20_proxyarp(dev, apdev)
3019     finally:
3020         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
3021                         stderr=open('/dev/null', 'w'))
3022         subprocess.call(['brctl', 'delbr', 'ap-br0'],
3023                         stderr=open('/dev/null', 'w'))
3024
3025 def _test_ap_hs20_proxyarp_dgaf(dev, apdev, disabled):
3026     bssid = apdev[0]['bssid']
3027     params = hs20_ap_params()
3028     params['hessid'] = bssid
3029     params['disable_dgaf'] = '1' if disabled else '0'
3030     params['proxy_arp'] = '1'
3031     params['na_mcast_to_ucast'] = '1'
3032     params['ap_isolate'] = '1'
3033     params['bridge'] = 'ap-br0'
3034     hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
3035     try:
3036         hapd.enable()
3037     except:
3038         # For now, do not report failures due to missing kernel support
3039         raise HwsimSkip("Could not start hostapd - assume proxyarp not supported in kernel version")
3040     ev = hapd.wait_event(["AP-ENABLED"], timeout=10)
3041     if ev is None:
3042         raise Exception("AP startup timed out")
3043
3044     dev[0].hs20_enable()
3045     subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
3046     subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
3047
3048     id = dev[0].add_cred_values({ 'realm': "example.com",
3049                                   'username': "hs20-test",
3050                                   'password': "password",
3051                                   'ca_cert': "auth_serv/ca.pem",
3052                                   'domain': "example.com",
3053                                   'update_identifier': "1234" })
3054     interworking_select(dev[0], bssid, "home", freq="2412")
3055     interworking_connect(dev[0], bssid, "TTLS")
3056
3057     dev[1].connect("test-hs20", key_mgmt="WPA-EAP", eap="TTLS",
3058                    identity="hs20-test", password="password",
3059                    ca_cert="auth_serv/ca.pem", phase2="auth=MSCHAPV2",
3060                    scan_freq="2412")
3061     time.sleep(0.1)
3062
3063     addr0 = dev[0].p2p_interface_addr()
3064
3065     src_ll_opt0 = "\x01\x01" + binascii.unhexlify(addr0.replace(':',''))
3066
3067     pkt = build_ns(src_ll=addr0, ip_src="aaaa:bbbb:cccc::2",
3068                    ip_dst="ff02::1:ff00:2", target="aaaa:bbbb:cccc::2",
3069                    opt=src_ll_opt0)
3070     if "OK" not in dev[0].request("DATA_TEST_FRAME " + binascii.hexlify(pkt)):
3071         raise Exception("DATA_TEST_FRAME failed")
3072
3073     pkt = build_ra(src_ll=apdev[0]['bssid'], ip_src="aaaa:bbbb:cccc::33",
3074                    ip_dst="ff01::1")
3075     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3076         raise Exception("DATA_TEST_FRAME failed")
3077
3078     pkt = build_na(src_ll=apdev[0]['bssid'], ip_src="aaaa:bbbb:cccc::44",
3079                    ip_dst="ff01::1", target="aaaa:bbbb:cccc::55")
3080     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3081         raise Exception("DATA_TEST_FRAME failed")
3082
3083     pkt = build_dhcp_ack(dst_ll="ff:ff:ff:ff:ff:ff", src_ll=bssid,
3084                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3085                          yiaddr="192.168.1.123", chaddr=addr0)
3086     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3087         raise Exception("DATA_TEST_FRAME failed")
3088     # another copy for additional code coverage
3089     pkt = build_dhcp_ack(dst_ll=addr0, src_ll=bssid,
3090                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3091                          yiaddr="192.168.1.123", chaddr=addr0)
3092     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3093         raise Exception("DATA_TEST_FRAME failed")
3094
3095     matches = get_permanent_neighbors("ap-br0")
3096     logger.info("After connect: " + str(matches))
3097     if len(matches) != 2:
3098         raise Exception("Unexpected number of neighbor entries after connect")
3099     if 'aaaa:bbbb:cccc::2 dev ap-br0 lladdr 02:00:00:00:00:00 PERMANENT' not in matches:
3100         raise Exception("dev0 addr missing")
3101     if '192.168.1.123 dev ap-br0 lladdr 02:00:00:00:00:00 PERMANENT' not in matches:
3102         raise Exception("dev0 IPv4 addr missing")
3103     dev[0].request("DISCONNECT")
3104     dev[1].request("DISCONNECT")
3105     time.sleep(0.5)
3106     matches = get_permanent_neighbors("ap-br0")
3107     logger.info("After disconnect: " + str(matches))
3108     if len(matches) > 0:
3109         raise Exception("Unexpected neighbor entries after disconnect")
3110
3111 def test_ap_hs20_proxyarp_disable_dgaf(dev, apdev):
3112     """Hotspot 2.0 and ProxyARP with DGAF disabled"""
3113     check_eap_capa(dev[0], "MSCHAPV2")
3114     try:
3115         _test_ap_hs20_proxyarp_dgaf(dev, apdev, True)
3116     finally:
3117         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
3118                         stderr=open('/dev/null', 'w'))
3119         subprocess.call(['brctl', 'delbr', 'ap-br0'],
3120                         stderr=open('/dev/null', 'w'))
3121
3122 def test_ap_hs20_proxyarp_enable_dgaf(dev, apdev):
3123     """Hotspot 2.0 and ProxyARP with DGAF enabled"""
3124     check_eap_capa(dev[0], "MSCHAPV2")
3125     try:
3126         _test_ap_hs20_proxyarp_dgaf(dev, apdev, False)
3127     finally:
3128         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
3129                         stderr=open('/dev/null', 'w'))
3130         subprocess.call(['brctl', 'delbr', 'ap-br0'],
3131                         stderr=open('/dev/null', 'w'))
3132
3133 def ip_checksum(buf):
3134     sum = 0
3135     if len(buf) & 0x01:
3136         buf += '\0x00'
3137     for i in range(0, len(buf), 2):
3138         val, = struct.unpack('H', buf[i:i+2])
3139         sum += val
3140     while (sum >> 16):
3141         sum = (sum & 0xffff) + (sum >> 16)
3142     return struct.pack('H', ~sum & 0xffff)
3143
3144 def ipv6_solicited_node_mcaddr(target):
3145     prefix = socket.inet_pton(socket.AF_INET6, "ff02::1:ff00:0")
3146     mask = socket.inet_pton(socket.AF_INET6, "::ff:ffff")
3147     _target = socket.inet_pton(socket.AF_INET6, target)
3148     p = struct.unpack('4I', prefix)
3149     m = struct.unpack('4I', mask)
3150     t = struct.unpack('4I', _target)
3151     res = (p[0] | (t[0] & m[0]),
3152            p[1] | (t[1] & m[1]),
3153            p[2] | (t[2] & m[2]),
3154            p[3] | (t[3] & m[3]))
3155     return socket.inet_ntop(socket.AF_INET6, struct.pack('4I', *res))
3156
3157 def build_icmpv6(ipv6_addrs, type, code, payload):
3158     start = struct.pack("BB", type, code)
3159     end = payload
3160     icmp = start + '\x00\x00' + end
3161     pseudo = ipv6_addrs + struct.pack(">LBBBB", len(icmp), 0, 0, 0, 58)
3162     csum = ip_checksum(pseudo + icmp)
3163     return start + csum + end
3164
3165 def build_ra(src_ll, ip_src, ip_dst, cur_hop_limit=0, router_lifetime=0,
3166              reachable_time=0, retrans_timer=0, opt=None):
3167     link_mc = binascii.unhexlify("3333ff000002")
3168     _src_ll = binascii.unhexlify(src_ll.replace(':',''))
3169     proto = '\x86\xdd'
3170     ehdr = link_mc + _src_ll + proto
3171     _ip_src = socket.inet_pton(socket.AF_INET6, ip_src)
3172     _ip_dst = socket.inet_pton(socket.AF_INET6, ip_dst)
3173
3174     adv = struct.pack('>BBHLL', cur_hop_limit, 0, router_lifetime,
3175                       reachable_time, retrans_timer)
3176     if opt:
3177         payload = adv + opt
3178     else:
3179         payload = adv
3180     icmp = build_icmpv6(_ip_src + _ip_dst, 134, 0, payload)
3181
3182     ipv6 = struct.pack('>BBBBHBB', 0x60, 0, 0, 0, len(icmp), 58, 255)
3183     ipv6 += _ip_src + _ip_dst
3184
3185     return ehdr + ipv6 + icmp
3186
3187 def build_ns(src_ll, ip_src, ip_dst, target, opt=None):
3188     link_mc = binascii.unhexlify("3333ff000002")
3189     _src_ll = binascii.unhexlify(src_ll.replace(':',''))
3190     proto = '\x86\xdd'
3191     ehdr = link_mc + _src_ll + proto
3192     _ip_src = socket.inet_pton(socket.AF_INET6, ip_src)
3193     if ip_dst is None:
3194         ip_dst = ipv6_solicited_node_mcaddr(target)
3195     _ip_dst = socket.inet_pton(socket.AF_INET6, ip_dst)
3196
3197     reserved = '\x00\x00\x00\x00'
3198     _target = socket.inet_pton(socket.AF_INET6, target)
3199     if opt:
3200         payload = reserved + _target + opt
3201     else:
3202         payload = reserved + _target
3203     icmp = build_icmpv6(_ip_src + _ip_dst, 135, 0, payload)
3204
3205     ipv6 = struct.pack('>BBBBHBB', 0x60, 0, 0, 0, len(icmp), 58, 255)
3206     ipv6 += _ip_src + _ip_dst
3207
3208     return ehdr + ipv6 + icmp
3209
3210 def send_ns(dev, src_ll=None, target=None, ip_src=None, ip_dst=None, opt=None,
3211             hapd_bssid=None):
3212     if hapd_bssid:
3213         if src_ll is None:
3214             src_ll = hapd_bssid
3215         cmd = "DATA_TEST_FRAME ifname=ap-br0 "
3216     else:
3217         if src_ll is None:
3218             src_ll = dev.p2p_interface_addr()
3219         cmd = "DATA_TEST_FRAME "
3220
3221     if opt is None:
3222         opt = "\x01\x01" + binascii.unhexlify(src_ll.replace(':',''))
3223
3224     pkt = build_ns(src_ll=src_ll, ip_src=ip_src, ip_dst=ip_dst, target=target,
3225                    opt=opt)
3226     if "OK" not in dev.request(cmd + binascii.hexlify(pkt)):
3227         raise Exception("DATA_TEST_FRAME failed")
3228
3229 def build_na(src_ll, ip_src, ip_dst, target, opt=None, flags=0):
3230     link_mc = binascii.unhexlify("3333ff000002")
3231     _src_ll = binascii.unhexlify(src_ll.replace(':',''))
3232     proto = '\x86\xdd'
3233     ehdr = link_mc + _src_ll + proto
3234     _ip_src = socket.inet_pton(socket.AF_INET6, ip_src)
3235     _ip_dst = socket.inet_pton(socket.AF_INET6, ip_dst)
3236
3237     _target = socket.inet_pton(socket.AF_INET6, target)
3238     if opt:
3239         payload = struct.pack('>Bxxx', flags) + _target + opt
3240     else:
3241         payload = struct.pack('>Bxxx', flags) + _target
3242     icmp = build_icmpv6(_ip_src + _ip_dst, 136, 0, payload)
3243
3244     ipv6 = struct.pack('>BBBBHBB', 0x60, 0, 0, 0, len(icmp), 58, 255)
3245     ipv6 += _ip_src + _ip_dst
3246
3247     return ehdr + ipv6 + icmp
3248
3249 def send_na(dev, src_ll=None, target=None, ip_src=None, ip_dst=None, opt=None,
3250             hapd_bssid=None):
3251     if hapd_bssid:
3252         if src_ll is None:
3253             src_ll = hapd_bssid
3254         cmd = "DATA_TEST_FRAME ifname=ap-br0 "
3255     else:
3256         if src_ll is None:
3257             src_ll = dev.p2p_interface_addr()
3258         cmd = "DATA_TEST_FRAME "
3259
3260     pkt = build_na(src_ll=src_ll, ip_src=ip_src, ip_dst=ip_dst, target=target,
3261                    opt=opt)
3262     if "OK" not in dev.request(cmd + binascii.hexlify(pkt)):
3263         raise Exception("DATA_TEST_FRAME failed")
3264
3265 def build_dhcp_ack(dst_ll, src_ll, ip_src, ip_dst, yiaddr, chaddr,
3266                    subnet_mask="255.255.255.0", truncated_opt=False,
3267                    wrong_magic=False, force_tot_len=None, no_dhcp=False):
3268     _dst_ll = binascii.unhexlify(dst_ll.replace(':',''))
3269     _src_ll = binascii.unhexlify(src_ll.replace(':',''))
3270     proto = '\x08\x00'
3271     ehdr = _dst_ll + _src_ll + proto
3272     _ip_src = socket.inet_pton(socket.AF_INET, ip_src)
3273     _ip_dst = socket.inet_pton(socket.AF_INET, ip_dst)
3274     _subnet_mask = socket.inet_pton(socket.AF_INET, subnet_mask)
3275
3276     _ciaddr = '\x00\x00\x00\x00'
3277     _yiaddr = socket.inet_pton(socket.AF_INET, yiaddr)
3278     _siaddr = '\x00\x00\x00\x00'
3279     _giaddr = '\x00\x00\x00\x00'
3280     _chaddr = binascii.unhexlify(chaddr.replace(':','') + "00000000000000000000")
3281     payload = struct.pack('>BBBBL3BB', 2, 1, 6, 0, 12345, 0, 0, 0, 0)
3282     payload += _ciaddr + _yiaddr + _siaddr + _giaddr + _chaddr + 192*'\x00'
3283     # magic
3284     if wrong_magic:
3285         payload += '\x63\x82\x53\x00'
3286     else:
3287         payload += '\x63\x82\x53\x63'
3288     if truncated_opt:
3289         payload += '\x22\xff\x00'
3290     # Option: DHCP Message Type = ACK
3291     payload += '\x35\x01\x05'
3292     # Pad Option
3293     payload += '\x00'
3294     # Option: Subnet Mask
3295     payload += '\x01\x04' + _subnet_mask
3296     # Option: Time Offset
3297     payload += struct.pack('>BBL', 2, 4, 0)
3298     # End Option
3299     payload += '\xff'
3300     # Pad Option
3301     payload += '\x00\x00\x00\x00'
3302
3303     if no_dhcp:
3304         payload = struct.pack('>BBBBL3BB', 2, 1, 6, 0, 12345, 0, 0, 0, 0)
3305         payload += _ciaddr + _yiaddr + _siaddr + _giaddr + _chaddr + 192*'\x00'
3306
3307     udp = struct.pack('>HHHH', 67, 68, 8 + len(payload), 0) + payload
3308
3309     if force_tot_len:
3310         tot_len = force_tot_len
3311     else:
3312         tot_len = 20 + len(udp)
3313     start = struct.pack('>BBHHBBBB', 0x45, 0, tot_len, 0, 0, 0, 128, 17)
3314     ipv4 = start + '\x00\x00' + _ip_src + _ip_dst
3315     csum = ip_checksum(ipv4)
3316     ipv4 = start + csum + _ip_src + _ip_dst
3317
3318     return ehdr + ipv4 + udp
3319
3320 def build_arp(dst_ll, src_ll, opcode, sender_mac, sender_ip,
3321               target_mac, target_ip):
3322     _dst_ll = binascii.unhexlify(dst_ll.replace(':',''))
3323     _src_ll = binascii.unhexlify(src_ll.replace(':',''))
3324     proto = '\x08\x06'
3325     ehdr = _dst_ll + _src_ll + proto
3326
3327     _sender_mac = binascii.unhexlify(sender_mac.replace(':',''))
3328     _sender_ip = socket.inet_pton(socket.AF_INET, sender_ip)
3329     _target_mac = binascii.unhexlify(target_mac.replace(':',''))
3330     _target_ip = socket.inet_pton(socket.AF_INET, target_ip)
3331
3332     arp = struct.pack('>HHBBH', 1, 0x0800, 6, 4, opcode)
3333     arp += _sender_mac + _sender_ip
3334     arp += _target_mac + _target_ip
3335
3336     return ehdr + arp
3337
3338 def send_arp(dev, dst_ll="ff:ff:ff:ff:ff:ff", src_ll=None, opcode=1,
3339              sender_mac=None, sender_ip="0.0.0.0",
3340              target_mac="00:00:00:00:00:00", target_ip="0.0.0.0",
3341              hapd_bssid=None):
3342     if hapd_bssid:
3343         if src_ll is None:
3344             src_ll = hapd_bssid
3345         if sender_mac is None:
3346             sender_mac = hapd_bssid
3347         cmd = "DATA_TEST_FRAME ifname=ap-br0 "
3348     else:
3349         if src_ll is None:
3350             src_ll = dev.p2p_interface_addr()
3351         if sender_mac is None:
3352             sender_mac = dev.p2p_interface_addr()
3353         cmd = "DATA_TEST_FRAME "
3354
3355     pkt = build_arp(dst_ll=dst_ll, src_ll=src_ll, opcode=opcode,
3356                     sender_mac=sender_mac, sender_ip=sender_ip,
3357                     target_mac=target_mac, target_ip=target_ip)
3358     if "OK" not in dev.request(cmd + binascii.hexlify(pkt)):
3359         raise Exception("DATA_TEST_FRAME failed")
3360
3361 def get_permanent_neighbors(ifname):
3362     cmd = subprocess.Popen(['ip', 'nei'], stdout=subprocess.PIPE)
3363     res = cmd.stdout.read()
3364     cmd.stdout.close()
3365     return [ line for line in res.splitlines() if "PERMANENT" in line and ifname in line ]
3366
3367 def get_bridge_macs(ifname):
3368     cmd = subprocess.Popen(['brctl', 'showmacs', ifname],
3369                            stdout=subprocess.PIPE)
3370     res = cmd.stdout.read()
3371     cmd.stdout.close()
3372     return res
3373
3374 def tshark_get_arp(cap, filter):
3375     res = run_tshark(cap, filter,
3376                      [ "eth.dst", "eth.src",
3377                        "arp.src.hw_mac", "arp.src.proto_ipv4",
3378                        "arp.dst.hw_mac", "arp.dst.proto_ipv4" ],
3379                      wait=False)
3380     frames = []
3381     for l in res.splitlines():
3382         frames.append(l.split('\t'))
3383     return frames
3384
3385 def tshark_get_ns(cap):
3386     res = run_tshark(cap, "icmpv6.type == 135",
3387                      [ "eth.dst", "eth.src",
3388                        "ipv6.src", "ipv6.dst",
3389                        "icmpv6.nd.ns.target_address",
3390                        "icmpv6.opt.linkaddr" ],
3391                      wait=False)
3392     frames = []
3393     for l in res.splitlines():
3394         frames.append(l.split('\t'))
3395     return frames
3396
3397 def tshark_get_na(cap):
3398     res = run_tshark(cap, "icmpv6.type == 136",
3399                      [ "eth.dst", "eth.src",
3400                        "ipv6.src", "ipv6.dst",
3401                        "icmpv6.nd.na.target_address",
3402                        "icmpv6.opt.linkaddr" ],
3403                      wait=False)
3404     frames = []
3405     for l in res.splitlines():
3406         frames.append(l.split('\t'))
3407     return frames
3408
3409 def _test_proxyarp_open(dev, apdev, params, ebtables=False):
3410     prefix = "proxyarp_open"
3411     if ebtables:
3412         prefix += "_ebtables"
3413     cap_br = os.path.join(params['logdir'], prefix + ".ap-br0.pcap")
3414     cap_dev0 = os.path.join(params['logdir'],
3415                             prefix + ".%s.pcap" % dev[0].ifname)
3416     cap_dev1 = os.path.join(params['logdir'],
3417                             prefix + ".%s.pcap" % dev[1].ifname)
3418     cap_dev2 = os.path.join(params['logdir'],
3419                             prefix + ".%s.pcap" % dev[2].ifname)
3420
3421     bssid = apdev[0]['bssid']
3422     params = { 'ssid': 'open' }
3423     params['proxy_arp'] = '1'
3424     hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
3425     hapd.set("ap_isolate", "1")
3426     hapd.set('bridge', 'ap-br0')
3427     hapd.dump_monitor()
3428     try:
3429         hapd.enable()
3430     except:
3431         # For now, do not report failures due to missing kernel support
3432         raise HwsimSkip("Could not start hostapd - assume proxyarp not supported in kernel version")
3433     ev = hapd.wait_event(["AP-ENABLED", "AP-DISABLED"], timeout=10)
3434     if ev is None:
3435         raise Exception("AP startup timed out")
3436     if "AP-ENABLED" not in ev:
3437         raise Exception("AP startup failed")
3438
3439     params2 = { 'ssid': 'another' }
3440     hapd2 = hostapd.add_ap(apdev[1], params2, no_enable=True)
3441     hapd2.set('bridge', 'ap-br0')
3442     hapd2.enable()
3443
3444     subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
3445     subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
3446
3447     if ebtables:
3448         for chain in [ 'FORWARD', 'OUTPUT' ]:
3449             subprocess.call(['ebtables', '-A', chain, '-p', 'ARP',
3450                              '-d', 'Broadcast', '-o', apdev[0]['ifname'],
3451                              '-j', 'DROP'])
3452             subprocess.call(['ebtables', '-A', chain, '-d', 'Multicast',
3453                              '-p', 'IPv6', '--ip6-protocol', 'ipv6-icmp',
3454                              '--ip6-icmp-type', 'neighbor-solicitation',
3455                              '-o', apdev[0]['ifname'], '-j', 'DROP'])
3456             subprocess.call(['ebtables', '-A', chain, '-d', 'Multicast',
3457                              '-p', 'IPv6', '--ip6-protocol', 'ipv6-icmp',
3458                              '--ip6-icmp-type', 'neighbor-advertisement',
3459                              '-o', apdev[0]['ifname'], '-j', 'DROP'])
3460             subprocess.call(['ebtables', '-A', chain,
3461                              '-p', 'IPv6', '--ip6-protocol', 'ipv6-icmp',
3462                              '--ip6-icmp-type', 'router-solicitation',
3463                              '-o', apdev[0]['ifname'], '-j', 'DROP'])
3464             # Multicast Listener Report Message
3465             subprocess.call(['ebtables', '-A', chain, '-d', 'Multicast',
3466                              '-p', 'IPv6', '--ip6-protocol', 'ipv6-icmp',
3467                              '--ip6-icmp-type', '143',
3468                              '-o', apdev[0]['ifname'], '-j', 'DROP'])
3469
3470     time.sleep(0.5)
3471     cmd = {}
3472     cmd[0] = subprocess.Popen(['tcpdump', '-p', '-U', '-i', 'ap-br0',
3473                                '-w', cap_br, '-s', '2000'],
3474                               stderr=open('/dev/null', 'w'))
3475     cmd[1] = subprocess.Popen(['tcpdump', '-p', '-U', '-i', dev[0].ifname,
3476                                '-w', cap_dev0, '-s', '2000'],
3477                               stderr=open('/dev/null', 'w'))
3478     cmd[2] = subprocess.Popen(['tcpdump', '-p', '-U', '-i', dev[1].ifname,
3479                                '-w', cap_dev1, '-s', '2000'],
3480                               stderr=open('/dev/null', 'w'))
3481     cmd[3] = subprocess.Popen(['tcpdump', '-p', '-U', '-i', dev[2].ifname,
3482                                '-w', cap_dev2, '-s', '2000'],
3483                               stderr=open('/dev/null', 'w'))
3484
3485     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
3486     dev[1].connect("open", key_mgmt="NONE", scan_freq="2412")
3487     dev[2].connect("another", key_mgmt="NONE", scan_freq="2412")
3488     time.sleep(0.1)
3489
3490     brcmd = subprocess.Popen(['brctl', 'show'], stdout=subprocess.PIPE)
3491     res = brcmd.stdout.read()
3492     brcmd.stdout.close()
3493     logger.info("Bridge setup: " + res)
3494
3495     brcmd = subprocess.Popen(['brctl', 'showstp', 'ap-br0'],
3496                              stdout=subprocess.PIPE)
3497     res = brcmd.stdout.read()
3498     brcmd.stdout.close()
3499     logger.info("Bridge showstp: " + res)
3500
3501     addr0 = dev[0].p2p_interface_addr()
3502     addr1 = dev[1].p2p_interface_addr()
3503     addr2 = dev[2].p2p_interface_addr()
3504
3505     src_ll_opt0 = "\x01\x01" + binascii.unhexlify(addr0.replace(':',''))
3506     src_ll_opt1 = "\x01\x01" + binascii.unhexlify(addr1.replace(':',''))
3507
3508     # DAD NS
3509     send_ns(dev[0], ip_src="::", target="aaaa:bbbb:cccc::2")
3510
3511     send_ns(dev[0], ip_src="aaaa:bbbb:cccc::2", target="aaaa:bbbb:cccc::2")
3512     # test frame without source link-layer address option
3513     send_ns(dev[0], ip_src="aaaa:bbbb:cccc::2", target="aaaa:bbbb:cccc::2",
3514             opt='')
3515     # test frame with bogus option
3516     send_ns(dev[0], ip_src="aaaa:bbbb:cccc::2", target="aaaa:bbbb:cccc::2",
3517             opt="\x70\x01\x01\x02\x03\x04\x05\x05")
3518     # test frame with truncated source link-layer address option
3519     send_ns(dev[0], ip_src="aaaa:bbbb:cccc::2", target="aaaa:bbbb:cccc::2",
3520             opt="\x01\x01\x01\x02\x03\x04")
3521     # test frame with foreign source link-layer address option
3522     send_ns(dev[0], ip_src="aaaa:bbbb:cccc::2", target="aaaa:bbbb:cccc::2",
3523             opt="\x01\x01\x01\x02\x03\x04\x05\x06")
3524
3525     send_ns(dev[1], ip_src="aaaa:bbbb:dddd::2", target="aaaa:bbbb:dddd::2")
3526
3527     send_ns(dev[1], ip_src="aaaa:bbbb:eeee::2", target="aaaa:bbbb:eeee::2")
3528     # another copy for additional code coverage
3529     send_ns(dev[1], ip_src="aaaa:bbbb:eeee::2", target="aaaa:bbbb:eeee::2")
3530
3531     pkt = build_dhcp_ack(dst_ll="ff:ff:ff:ff:ff:ff", src_ll=bssid,
3532                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3533                          yiaddr="192.168.1.124", chaddr=addr0)
3534     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3535         raise Exception("DATA_TEST_FRAME failed")
3536     # Change address and verify unicast
3537     pkt = build_dhcp_ack(dst_ll=addr0, src_ll=bssid,
3538                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3539                          yiaddr="192.168.1.123", chaddr=addr0)
3540     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3541         raise Exception("DATA_TEST_FRAME failed")
3542
3543     # Not-associated client MAC address
3544     pkt = build_dhcp_ack(dst_ll="ff:ff:ff:ff:ff:ff", src_ll=bssid,
3545                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3546                          yiaddr="192.168.1.125", chaddr="22:33:44:55:66:77")
3547     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3548         raise Exception("DATA_TEST_FRAME failed")
3549
3550     # No IP address
3551     pkt = build_dhcp_ack(dst_ll=addr1, src_ll=bssid,
3552                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3553                          yiaddr="0.0.0.0", chaddr=addr1)
3554     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3555         raise Exception("DATA_TEST_FRAME failed")
3556
3557     # Zero subnet mask
3558     pkt = build_dhcp_ack(dst_ll=addr1, src_ll=bssid,
3559                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3560                          yiaddr="192.168.1.126", chaddr=addr1,
3561                          subnet_mask="0.0.0.0")
3562     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3563         raise Exception("DATA_TEST_FRAME failed")
3564
3565     # Truncated option
3566     pkt = build_dhcp_ack(dst_ll=addr1, src_ll=bssid,
3567                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3568                          yiaddr="192.168.1.127", chaddr=addr1,
3569                          truncated_opt=True)
3570     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3571         raise Exception("DATA_TEST_FRAME failed")
3572
3573     # Wrong magic
3574     pkt = build_dhcp_ack(dst_ll=addr1, src_ll=bssid,
3575                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3576                          yiaddr="192.168.1.128", chaddr=addr1,
3577                          wrong_magic=True)
3578     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3579         raise Exception("DATA_TEST_FRAME failed")
3580
3581     # Wrong IPv4 total length
3582     pkt = build_dhcp_ack(dst_ll=addr1, src_ll=bssid,
3583                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3584                          yiaddr="192.168.1.129", chaddr=addr1,
3585                          force_tot_len=1000)
3586     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3587         raise Exception("DATA_TEST_FRAME failed")
3588
3589     # BOOTP
3590     pkt = build_dhcp_ack(dst_ll=addr1, src_ll=bssid,
3591                          ip_src="192.168.1.1", ip_dst="255.255.255.255",
3592                          yiaddr="192.168.1.129", chaddr=addr1,
3593                          no_dhcp=True)
3594     if "OK" not in hapd.request("DATA_TEST_FRAME ifname=ap-br0 " + binascii.hexlify(pkt)):
3595         raise Exception("DATA_TEST_FRAME failed")
3596
3597     macs = get_bridge_macs("ap-br0")
3598     logger.info("After connect (showmacs): " + str(macs))
3599
3600     matches = get_permanent_neighbors("ap-br0")
3601     logger.info("After connect: " + str(matches))
3602     if len(matches) != 4:
3603         raise Exception("Unexpected number of neighbor entries after connect")
3604     if 'aaaa:bbbb:cccc::2 dev ap-br0 lladdr 02:00:00:00:00:00 PERMANENT' not in matches:
3605         raise Exception("dev0 addr missing")
3606     if 'aaaa:bbbb:dddd::2 dev ap-br0 lladdr 02:00:00:00:01:00 PERMANENT' not in matches:
3607         raise Exception("dev1 addr(1) missing")
3608     if 'aaaa:bbbb:eeee::2 dev ap-br0 lladdr 02:00:00:00:01:00 PERMANENT' not in matches:
3609         raise Exception("dev1 addr(2) missing")
3610     if '192.168.1.123 dev ap-br0 lladdr 02:00:00:00:00:00 PERMANENT' not in matches:
3611         raise Exception("dev0 IPv4 addr missing")
3612
3613     targets = [ "192.168.1.123", "192.168.1.124", "192.168.1.125",
3614                 "192.168.1.126" ]
3615     for target in targets:
3616         send_arp(dev[1], sender_ip="192.168.1.100", target_ip=target)
3617
3618     for target in targets:
3619         send_arp(hapd, hapd_bssid=bssid, sender_ip="192.168.1.101",
3620                  target_ip=target)
3621
3622     for target in targets:
3623         send_arp(dev[2], sender_ip="192.168.1.103", target_ip=target)
3624
3625     # ARP Probe from wireless STA
3626     send_arp(dev[1], target_ip="192.168.1.127")
3627     # ARP Announcement from wireless STA
3628     send_arp(dev[1], sender_ip="192.168.1.127", target_ip="192.168.1.127")
3629     send_arp(dev[1], sender_ip="192.168.1.127", target_ip="192.168.1.127",
3630              opcode=2)
3631
3632     macs = get_bridge_macs("ap-br0")
3633     logger.info("After ARP Probe + Announcement (showmacs): " + str(macs))
3634
3635     matches = get_permanent_neighbors("ap-br0")
3636     logger.info("After ARP Probe + Announcement: " + str(matches))
3637
3638     # ARP Request for the newly introduced IP address from wireless STA
3639     send_arp(dev[0], sender_ip="192.168.1.123", target_ip="192.168.1.127")
3640
3641     # ARP Request for the newly introduced IP address from bridge
3642     send_arp(hapd, hapd_bssid=bssid, sender_ip="192.168.1.102",
3643              target_ip="192.168.1.127")
3644     send_arp(dev[2], sender_ip="192.168.1.103", target_ip="192.168.1.127")
3645
3646     # ARP Probe from bridge
3647     send_arp(hapd, hapd_bssid=bssid, target_ip="192.168.1.130")
3648     send_arp(dev[2], target_ip="192.168.1.131")
3649     # ARP Announcement from bridge (not to be learned by AP for proxyarp)
3650     send_arp(hapd, hapd_bssid=bssid, sender_ip="192.168.1.130",
3651              target_ip="192.168.1.130")
3652     send_arp(hapd, hapd_bssid=bssid, sender_ip="192.168.1.130",
3653              target_ip="192.168.1.130", opcode=2)
3654     send_arp(dev[2], sender_ip="192.168.1.131", target_ip="192.168.1.131")
3655     send_arp(dev[2], sender_ip="192.168.1.131", target_ip="192.168.1.131",
3656              opcode=2)
3657
3658     macs = get_bridge_macs("ap-br0")
3659     logger.info("After ARP Probe + Announcement (showmacs): " + str(macs))
3660
3661     matches = get_permanent_neighbors("ap-br0")
3662     logger.info("After ARP Probe + Announcement: " + str(matches))
3663
3664     # ARP Request for the newly introduced IP address from wireless STA
3665     send_arp(dev[0], sender_ip="192.168.1.123", target_ip="192.168.1.130")
3666     # ARP Response from bridge (AP does not proxy for non-wireless devices)
3667     send_arp(hapd, hapd_bssid=bssid, dst_ll=addr0, sender_ip="192.168.1.130",
3668              target_ip="192.168.1.123", opcode=2)
3669
3670     # ARP Request for the newly introduced IP address from wireless STA
3671     send_arp(dev[0], sender_ip="192.168.1.123", target_ip="192.168.1.131")
3672     # ARP Response from bridge (AP does not proxy for non-wireless devices)
3673     send_arp(dev[2], dst_ll=addr0, sender_ip="192.168.1.131",
3674              target_ip="192.168.1.123", opcode=2)
3675
3676     # ARP Request for the newly introduced IP address from bridge
3677     send_arp(hapd, hapd_bssid=bssid, sender_ip="192.168.1.102",
3678              target_ip="192.168.1.130")
3679     send_arp(dev[2], sender_ip="192.168.1.104", target_ip="192.168.1.131")
3680
3681     # ARP Probe from wireless STA (duplicate address; learned through DHCP)
3682     send_arp(dev[1], target_ip="192.168.1.123")
3683     # ARP Probe from wireless STA (duplicate address; learned through ARP)
3684     send_arp(dev[0], target_ip="192.168.1.127")
3685
3686     # Gratuitous ARP Reply for another STA's IP address
3687     send_arp(dev[0], opcode=2, sender_mac=addr0, sender_ip="192.168.1.127",
3688              target_mac=addr1, target_ip="192.168.1.127")
3689     send_arp(dev[1], opcode=2, sender_mac=addr1, sender_ip="192.168.1.123",
3690              target_mac=addr0, target_ip="192.168.1.123")
3691     # ARP Request to verify previous mapping
3692     send_arp(dev[1], sender_ip="192.168.1.127", target_ip="192.168.1.123")
3693     send_arp(dev[0], sender_ip="192.168.1.123", target_ip="192.168.1.127")
3694
3695     time.sleep(0.1)
3696
3697     send_ns(dev[0], target="aaaa:bbbb:dddd::2", ip_src="aaaa:bbbb:cccc::2")
3698     time.sleep(0.1)
3699     send_ns(dev[1], target="aaaa:bbbb:cccc::2", ip_src="aaaa:bbbb:dddd::2")
3700     time.sleep(0.1)
3701     send_ns(hapd, hapd_bssid=bssid, target="aaaa:bbbb:dddd::2",
3702             ip_src="aaaa:bbbb:ffff::2")
3703     time.sleep(0.1)
3704     send_ns(dev[2], target="aaaa:bbbb:cccc::2", ip_src="aaaa:bbbb:ff00::2")
3705     time.sleep(0.1)
3706     send_ns(dev[2], target="aaaa:bbbb:dddd::2", ip_src="aaaa:bbbb:ff00::2")
3707     time.sleep(0.1)
3708     send_ns(dev[2], target="aaaa:bbbb:eeee::2", ip_src="aaaa:bbbb:ff00::2")
3709     time.sleep(0.1)
3710
3711     # Try to probe for an already assigned address
3712     send_ns(dev[1], target="aaaa:bbbb:cccc::2", ip_src="::")
3713     time.sleep(0.1)
3714     send_ns(hapd, hapd_bssid=bssid, target="aaaa:bbbb:cccc::2", ip_src="::")
3715     time.sleep(0.1)
3716     send_ns(dev[2], target="aaaa:bbbb:cccc::2", ip_src="::")
3717     time.sleep(0.1)
3718
3719     # Unsolicited NA
3720     send_na(dev[1], target="aaaa:bbbb:cccc:aeae::3",
3721             ip_src="aaaa:bbbb:cccc:aeae::3", ip_dst="ff02::1")
3722     send_na(hapd, hapd_bssid=bssid, target="aaaa:bbbb:cccc:aeae::4",
3723             ip_src="aaaa:bbbb:cccc:aeae::4", ip_dst="ff02::1")
3724     send_na(dev[2], target="aaaa:bbbb:cccc:aeae::5",
3725             ip_src="aaaa:bbbb:cccc:aeae::5", ip_dst="ff02::1")
3726
3727     try:
3728         hwsim_utils.test_connectivity_iface(dev[0], hapd, "ap-br0")
3729     except Exception, e:
3730         logger.info("test_connectibity_iface failed: " + str(e))
3731         raise HwsimSkip("Assume kernel did not have the required patches for proxyarp")
3732     hwsim_utils.test_connectivity_iface(dev[1], hapd, "ap-br0")
3733     hwsim_utils.test_connectivity(dev[0], dev[1])
3734
3735     dev[0].request("DISCONNECT")
3736     dev[1].request("DISCONNECT")
3737     time.sleep(0.5)
3738     for i in range(len(cmd)):
3739         cmd[i].terminate()
3740     macs = get_bridge_macs("ap-br0")
3741     logger.info("After disconnect (showmacs): " + str(macs))
3742     matches = get_permanent_neighbors("ap-br0")
3743     logger.info("After disconnect: " + str(matches))
3744     if len(matches) > 0:
3745         raise Exception("Unexpected neighbor entries after disconnect")
3746     if ebtables:
3747         cmd = subprocess.Popen(['ebtables', '-L', '--Lc'],
3748                                stdout=subprocess.PIPE)
3749         res = cmd.stdout.read()
3750         cmd.stdout.close()
3751         logger.info("ebtables results:\n" + res)
3752
3753     # Verify that expected ARP messages were seen and no unexpected
3754     # ARP messages were seen.
3755
3756     arp_req = tshark_get_arp(cap_dev0, "arp.opcode == 1")
3757     arp_reply = tshark_get_arp(cap_dev0, "arp.opcode == 2")
3758     logger.info("dev0 seen ARP requests:\n" + str(arp_req))
3759     logger.info("dev0 seen ARP replies:\n" + str(arp_reply))
3760
3761     if [ 'ff:ff:ff:ff:ff:ff', addr1,
3762          addr1, '192.168.1.100',
3763          '00:00:00:00:00:00', '192.168.1.123' ] in arp_req:
3764         raise Exception("dev0 saw ARP request from dev1")
3765     if [ 'ff:ff:ff:ff:ff:ff', addr2,
3766          addr2, '192.168.1.103',
3767          '00:00:00:00:00:00', '192.168.1.123' ] in arp_req:
3768         raise Exception("dev0 saw ARP request from dev2")
3769     # TODO: Uncomment once fixed in kernel
3770     #if [ 'ff:ff:ff:ff:ff:ff', bssid,
3771     #     bssid, '192.168.1.101',
3772     #     '00:00:00:00:00:00', '192.168.1.123' ] in arp_req:
3773     #    raise Exception("dev0 saw ARP request from br")
3774
3775     if ebtables:
3776         for req in arp_req:
3777             if req[1] != addr0:
3778                 raise Exception("Unexpected foreign ARP request on dev0")
3779
3780     arp_req = tshark_get_arp(cap_dev1, "arp.opcode == 1")
3781     arp_reply = tshark_get_arp(cap_dev1, "arp.opcode == 2")
3782     logger.info("dev1 seen ARP requests:\n" + str(arp_req))
3783     logger.info("dev1 seen ARP replies:\n" + str(arp_reply))
3784
3785     if [ 'ff:ff:ff:ff:ff:ff', addr2,
3786          addr2, '192.168.1.103',
3787          '00:00:00:00:00:00', '192.168.1.123' ] in arp_req:
3788         raise Exception("dev1 saw ARP request from dev2")
3789     if [addr1, addr0, addr0, '192.168.1.123', addr1, '192.168.1.100'] not in arp_reply:
3790         raise Exception("dev1 did not get ARP response for 192.168.1.123")
3791
3792     if ebtables:
3793         for req in arp_req:
3794             if req[1] != addr1:
3795                 raise Exception("Unexpected foreign ARP request on dev1")
3796
3797     arp_req = tshark_get_arp(cap_dev2, "arp.opcode == 1")
3798     arp_reply = tshark_get_arp(cap_dev2, "arp.opcode == 2")
3799     logger.info("dev2 seen ARP requests:\n" + str(arp_req))
3800     logger.info("dev2 seen ARP replies:\n" + str(arp_reply))
3801
3802     if [ addr2, addr0,
3803          addr0, '192.168.1.123',
3804          addr2, '192.168.1.103' ] not in arp_reply:
3805         raise Exception("dev2 did not get ARP response for 192.168.1.123")
3806
3807     arp_req = tshark_get_arp(cap_br, "arp.opcode == 1")
3808     arp_reply = tshark_get_arp(cap_br, "arp.opcode == 2")
3809     logger.info("br seen ARP requests:\n" + str(arp_req))
3810     logger.info("br seen ARP replies:\n" + str(arp_reply))
3811
3812     # TODO: Uncomment once fixed in kernel
3813     #if [ bssid, addr0,
3814     #     addr0, '192.168.1.123',
3815     #     bssid, '192.168.1.101' ] not in arp_reply:
3816     #    raise Exception("br did not get ARP response for 192.168.1.123")
3817
3818     ns = tshark_get_ns(cap_dev0)
3819     logger.info("dev0 seen NS: " + str(ns))
3820     na = tshark_get_na(cap_dev0)
3821     logger.info("dev0 seen NA: " + str(na))
3822
3823     if [ addr0, addr1, 'aaaa:bbbb:dddd::2', 'aaaa:bbbb:cccc::2',
3824          'aaaa:bbbb:dddd::2', addr1 ] not in na:
3825         raise Exception("dev0 did not get NA for aaaa:bbbb:dddd::2")
3826
3827     if ebtables:
3828         for req in ns:
3829             if req[1] != addr0:
3830                 raise Exception("Unexpected foreign NS on dev0: " + str(req))
3831
3832     ns = tshark_get_ns(cap_dev1)
3833     logger.info("dev1 seen NS: " + str(ns))
3834     na = tshark_get_na(cap_dev1)
3835     logger.info("dev1 seen NA: " + str(na))
3836
3837     if [ addr1, addr0, 'aaaa:bbbb:cccc::2', 'aaaa:bbbb:dddd::2',
3838          'aaaa:bbbb:cccc::2', addr0 ] not in na:
3839         raise Exception("dev1 did not get NA for aaaa:bbbb:cccc::2")
3840
3841     if ebtables:
3842         for req in ns:
3843             if req[1] != addr1:
3844                 raise Exception("Unexpected foreign NS on dev1: " + str(req))
3845
3846     ns = tshark_get_ns(cap_dev2)
3847     logger.info("dev2 seen NS: " + str(ns))
3848     na = tshark_get_na(cap_dev2)
3849     logger.info("dev2 seen NA: " + str(na))
3850
3851     # FIX: enable once kernel implementation for proxyarp IPv6 is fixed
3852     #if [ addr2, addr0, 'aaaa:bbbb:cccc::2', 'aaaa:bbbb:ff00::2',
3853     #     'aaaa:bbbb:cccc::2', addr0 ] not in na:
3854     #    raise Exception("dev2 did not get NA for aaaa:bbbb:cccc::2")
3855     #if [ addr2, addr1, 'aaaa:bbbb:dddd::2', 'aaaa:bbbb:ff00::2',
3856     #     'aaaa:bbbb:dddd::2', addr1 ] not in na:
3857     #    raise Exception("dev2 did not get NA for aaaa:bbbb:dddd::2")
3858     #if [ addr2, addr1, 'aaaa:bbbb:eeee::2', 'aaaa:bbbb:ff00::2',
3859     #     'aaaa:bbbb:eeee::2', addr1 ] not in na:
3860     #    raise Exception("dev2 did not get NA for aaaa:bbbb:eeee::2")
3861
3862 def test_proxyarp_open(dev, apdev, params):
3863     """ProxyARP with open network"""
3864     try:
3865         _test_proxyarp_open(dev, apdev, params)
3866     finally:
3867         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
3868                         stderr=open('/dev/null', 'w'))
3869         subprocess.call(['brctl', 'delbr', 'ap-br0'],
3870                         stderr=open('/dev/null', 'w'))
3871
3872 def test_proxyarp_open_ebtables(dev, apdev, params):
3873     """ProxyARP with open network"""
3874     try:
3875         _test_proxyarp_open(dev, apdev, params, ebtables=True)
3876     finally:
3877         try:
3878             subprocess.call(['ebtables', '-F', 'FORWARD'])
3879             subprocess.call(['ebtables', '-F', 'OUTPUT'])
3880         except:
3881             pass
3882         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
3883                         stderr=open('/dev/null', 'w'))
3884         subprocess.call(['brctl', 'delbr', 'ap-br0'],
3885                         stderr=open('/dev/null', 'w'))
3886
3887 def test_ap_hs20_connect_deinit(dev, apdev):
3888     """Hotspot 2.0 connection interrupted with deinit"""
3889     check_eap_capa(dev[0], "MSCHAPV2")
3890     bssid = apdev[0]['bssid']
3891     params = hs20_ap_params()
3892     params['hessid'] = bssid
3893     hapd = hostapd.add_ap(apdev[0], params)
3894
3895     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
3896     wpas.interface_add("wlan5", drv_params="")
3897     wpas.hs20_enable()
3898     wpas.flush_scan_cache()
3899     wpas.add_cred_values({ 'realm': "example.com",
3900                            'username': "hs20-test",
3901                            'password': "password",
3902                            'ca_cert': "auth_serv/ca.pem",
3903                            'domain': "example.com" })
3904
3905     wpas.scan_for_bss(bssid, freq=2412)
3906     hapd.disable()
3907
3908     wpas.request("INTERWORKING_SELECT freq=2412")
3909
3910     id = wpas.request("RADIO_WORK add block-work")
3911     ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
3912     if ev is None:
3913         raise Exception("Timeout while waiting radio work to start")
3914     ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
3915     if ev is None:
3916         raise Exception("Timeout while waiting radio work to start (2)")
3917
3918     # Remove the interface while the gas-query radio work is still pending and
3919     # GAS query has not yet been started.
3920     wpas.interface_remove("wlan5")
3921
3922 def test_ap_hs20_anqp_format_errors(dev, apdev):
3923     """Interworking network selection and ANQP format errors"""
3924     bssid = apdev[0]['bssid']
3925     params = hs20_ap_params()
3926     params['hessid'] = bssid
3927     hapd = hostapd.add_ap(apdev[0], params)
3928
3929     dev[0].hs20_enable()
3930     values = { 'realm': "example.com",
3931                'ca_cert': "auth_serv/ca.pem",
3932                'username': "hs20-test",
3933                'password': "password",
3934                'domain': "example.com" }
3935     id = dev[0].add_cred_values(values)
3936
3937     dev[0].scan_for_bss(bssid, freq="2412")
3938
3939     tests = [ "00", "ffff", "010011223344", "020008000005112233445500",
3940               "01000400000000", "01000000000000",
3941               "01000300000200", "0100040000ff0000", "01000300000100",
3942               "01000300000001",
3943               "01000600000056112233",
3944               "01000900000002050001000111",
3945               "01000600000001000000", "01000600000001ff0000",
3946               "01000600000001020001",
3947               "010008000000010400010001", "0100080000000104000100ff",
3948               "010011000000010d00050200020100030005000600",
3949               "0000" ]
3950     for t in tests:
3951         hapd.set("anqp_elem", "263:" + t)
3952         dev[0].request("INTERWORKING_SELECT freq=2412")
3953         ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=5)
3954         if ev is None:
3955             raise Exception("Network selection timed out")
3956         dev[0].dump_monitor()
3957
3958     dev[0].remove_cred(id)
3959     id = dev[0].add_cred_values({ 'imsi': "555444-333222111", 'eap': "AKA",
3960                                   'milenage': "5122250214c33e723a5dd523fc145fc0:981d464c7c52eb6e5036234984ad0bcf:000000000123"})
3961
3962     tests = [ "00", "0100", "0001", "00ff", "000200ff", "0003000101",
3963               "00020100" ]
3964     for t in tests:
3965         hapd.set("anqp_elem", "264:" + t)
3966         dev[0].request("INTERWORKING_SELECT freq=2412")
3967         ev = dev[0].wait_event(["INTERWORKING-NO-MATCH"], timeout=5)
3968         if ev is None:
3969             raise Exception("Network selection timed out")
3970         dev[0].dump_monitor()
3971
3972 def test_ap_hs20_cred_with_nai_realm(dev, apdev):
3973     """Hotspot 2.0 network selection and cred_with_nai_realm cred->realm"""
3974     bssid = apdev[0]['bssid']
3975     params = hs20_ap_params()
3976     params['hessid'] = bssid
3977     hostapd.add_ap(apdev[0], params)
3978
3979     dev[0].hs20_enable()
3980
3981     id = dev[0].add_cred_values({ 'realm': "example.com",
3982                                   'username': "test",
3983                                   'password': "secret",
3984                                   'domain': "example.com",
3985                                   'eap': 'TTLS' })
3986     interworking_select(dev[0], bssid, "home", freq=2412)
3987     dev[0].remove_cred(id)
3988
3989     id = dev[0].add_cred_values({ 'realm': "foo.com",
3990                                   'username': "test",
3991                                   'password': "secret",
3992                                   'domain': "example.com",
3993                                   'roaming_consortium': "112234",
3994                                   'eap': 'TTLS' })
3995     interworking_select(dev[0], bssid, "home", freq=2412, no_match=True)
3996     dev[0].remove_cred(id)
3997
3998 def test_ap_hs20_cred_and_no_roaming_consortium(dev, apdev):
3999     """Hotspot 2.0 network selection and no roaming consortium"""
4000     bssid = apdev[0]['bssid']
4001     params = hs20_ap_params()
4002     params['hessid'] = bssid
4003     del params['roaming_consortium']
4004     hostapd.add_ap(apdev[0], params)
4005
4006     dev[0].hs20_enable()
4007
4008     id = dev[0].add_cred_values({ 'realm': "example.com",
4009                                   'username': "test",
4010                                   'password': "secret",
4011                                   'domain': "example.com",
4012                                   'roaming_consortium': "112234",
4013                                   'eap': 'TTLS' })
4014     interworking_select(dev[0], bssid, "home", freq=2412, no_match=True)
4015
4016 def test_ap_hs20_interworking_oom(dev, apdev):
4017     """Hotspot 2.0 network selection and OOM"""
4018     bssid = apdev[0]['bssid']
4019     params = hs20_ap_params()
4020     params['hessid'] = bssid
4021     params['nai_realm'] = [ "0,no.match.here;example.com;no.match.here.either,21[2:1][5:7]",
4022                             "0,example.com,13[5:6],21[2:4][5:7]",
4023                             "0,another.example.com" ]
4024     hostapd.add_ap(apdev[0], params)
4025
4026     dev[0].hs20_enable()
4027
4028     id = dev[0].add_cred_values({ 'realm': "example.com",
4029                                   'username': "test",
4030                                   'password': "secret",
4031                                   'domain': "example.com",
4032                                   'eap': 'TTLS' })
4033
4034     dev[0].scan_for_bss(bssid, freq="2412")
4035
4036     funcs = [ "wpabuf_alloc;interworking_anqp_send_req",
4037               "anqp_build_req;interworking_anqp_send_req",
4038               "gas_query_req;interworking_anqp_send_req",
4039               "dup_binstr;nai_realm_parse_realm",
4040               "=nai_realm_parse_realm",
4041               "=nai_realm_parse",
4042               "=nai_realm_match" ]
4043     for func in funcs:
4044         with alloc_fail(dev[0], 1, func):
4045             dev[0].request("INTERWORKING_SELECT auto freq=2412")
4046             ev = dev[0].wait_event(["Starting ANQP"], timeout=5)
4047             if ev is None:
4048                 raise Exception("ANQP did not start")
4049             wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")