tests: Verify correct VLAN after RSN pre-authentication
[mech_eap.git] / tests / hwsim / test_pmksa_cache.py
1 # WPA2-Enterprise PMKSA caching tests
2 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import logging
8 logger = logging.getLogger()
9 import subprocess
10 import time
11
12 import hostapd
13 import hwsim_utils
14 from wpasupplicant import WpaSupplicant
15 from utils import alloc_fail
16 from test_ap_eap import eap_connect
17
18 def test_pmksa_cache_on_roam_back(dev, apdev):
19     """PMKSA cache to skip EAP on reassociation back to same AP"""
20     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
21     hostapd.add_ap(apdev[0]['ifname'], params)
22     bssid = apdev[0]['bssid']
23     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
24                    eap="GPSK", identity="gpsk user",
25                    password="abcdefghijklmnop0123456789abcdef",
26                    scan_freq="2412")
27     pmksa = dev[0].get_pmksa(bssid)
28     if pmksa is None:
29         raise Exception("No PMKSA cache entry created")
30     if pmksa['opportunistic'] != '0':
31         raise Exception("Unexpected opportunistic PMKSA cache entry")
32
33     hostapd.add_ap(apdev[1]['ifname'], params)
34     bssid2 = apdev[1]['bssid']
35
36     dev[0].dump_monitor()
37     logger.info("Roam to AP2")
38     # It can take some time for the second AP to become ready to reply to Probe
39     # Request frames especially under heavy CPU load, so allow couple of rounds
40     # of scanning to avoid reporting errors incorrectly just because of scans
41     # not having seen the target AP.
42     for i in range(0, 10):
43         dev[0].scan(freq="2412")
44         if dev[0].get_bss(bssid2) is not None:
45             break
46         logger.info("Scan again to find target AP")
47     dev[0].request("ROAM " + bssid2)
48     ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
49     if ev is None:
50         raise Exception("EAP success timed out")
51     dev[0].wait_connected(timeout=10, error="Roaming timed out")
52     pmksa2 = dev[0].get_pmksa(bssid2)
53     if pmksa2 is None:
54         raise Exception("No PMKSA cache entry found")
55     if pmksa2['opportunistic'] != '0':
56         raise Exception("Unexpected opportunistic PMKSA cache entry")
57
58     dev[0].dump_monitor()
59     logger.info("Roam back to AP1")
60     dev[0].scan(freq="2412")
61     dev[0].request("ROAM " + bssid)
62     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
63                             "CTRL-EVENT-CONNECTED"], timeout=10)
64     if ev is None:
65         raise Exception("Roaming with the AP timed out")
66     if "CTRL-EVENT-EAP-STARTED" in ev:
67         raise Exception("Unexpected EAP exchange")
68     pmksa1b = dev[0].get_pmksa(bssid)
69     if pmksa1b is None:
70         raise Exception("No PMKSA cache entry found")
71     if pmksa['pmkid'] != pmksa1b['pmkid']:
72         raise Exception("Unexpected PMKID change for AP1")
73
74     dev[0].dump_monitor()
75     if "FAIL" in dev[0].request("PMKSA_FLUSH"):
76         raise Exception("PMKSA_FLUSH failed")
77     if dev[0].get_pmksa(bssid) is not None or dev[0].get_pmksa(bssid2) is not None:
78         raise Exception("PMKSA_FLUSH did not remove PMKSA entries")
79     dev[0].wait_disconnected(timeout=5)
80     dev[0].wait_connected(timeout=15, error="Reconnection timed out")
81
82 def test_pmksa_cache_and_reauth(dev, apdev):
83     """PMKSA caching and EAPOL reauthentication"""
84     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
85     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
86     bssid = apdev[0]['bssid']
87     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
88                    eap="GPSK", identity="gpsk user",
89                    password="abcdefghijklmnop0123456789abcdef",
90                    scan_freq="2412")
91
92     hostapd.add_ap(apdev[1]['ifname'], params)
93     bssid2 = apdev[1]['bssid']
94
95     dev[0].dump_monitor()
96     logger.info("Roam to AP2")
97     # It can take some time for the second AP to become ready to reply to Probe
98     # Request frames especially under heavy CPU load, so allow couple of rounds
99     # of scanning to avoid reporting errors incorrectly just because of scans
100     # not having seen the target AP.
101     for i in range(0, 10):
102         dev[0].scan(freq="2412")
103         if dev[0].get_bss(bssid2) is not None:
104             break
105         logger.info("Scan again to find target AP")
106     dev[0].request("ROAM " + bssid2)
107     ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
108     if ev is None:
109         raise Exception("EAP success timed out")
110     dev[0].wait_connected(timeout=10, error="Roaming timed out")
111
112     dev[0].dump_monitor()
113     logger.info("Roam back to AP1")
114     dev[0].scan(freq="2412")
115     dev[0].request("ROAM " + bssid)
116     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
117                             "CTRL-EVENT-CONNECTED"], timeout=10)
118     if ev is None:
119         raise Exception("Roaming with the AP timed out")
120     if "CTRL-EVENT-EAP-STARTED" in ev:
121         raise Exception("Unexpected EAP exchange")
122
123     # Verify EAPOL reauthentication after PMKSA caching
124     hapd.request("EAPOL_REAUTH " + dev[0].own_addr())
125     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED"], timeout=5)
126     if ev is None:
127         raise Exception("EAP authentication did not start")
128     ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=5)
129     if ev is None:
130         raise Exception("EAP authentication did not succeed")
131
132 def test_pmksa_cache_opportunistic_only_on_sta(dev, apdev):
133     """Opportunistic PMKSA caching enabled only on station"""
134     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
135     hostapd.add_ap(apdev[0]['ifname'], params)
136     bssid = apdev[0]['bssid']
137     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
138                    eap="GPSK", identity="gpsk user",
139                    password="abcdefghijklmnop0123456789abcdef", okc=True,
140                    scan_freq="2412")
141     pmksa = dev[0].get_pmksa(bssid)
142     if pmksa is None:
143         raise Exception("No PMKSA cache entry created")
144     if pmksa['opportunistic'] != '0':
145         raise Exception("Unexpected opportunistic PMKSA cache entry")
146
147     hostapd.add_ap(apdev[1]['ifname'], params)
148     bssid2 = apdev[1]['bssid']
149
150     dev[0].dump_monitor()
151     logger.info("Roam to AP2")
152     dev[0].scan(freq="2412")
153     dev[0].request("ROAM " + bssid2)
154     ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
155     if ev is None:
156         raise Exception("EAP success timed out")
157     dev[0].wait_connected(timeout=10, error="Roaming timed out")
158     pmksa2 = dev[0].get_pmksa(bssid2)
159     if pmksa2 is None:
160         raise Exception("No PMKSA cache entry found")
161     if pmksa2['opportunistic'] != '0':
162         raise Exception("Unexpected opportunistic PMKSA cache entry")
163
164     dev[0].dump_monitor()
165     logger.info("Roam back to AP1")
166     dev[0].scan(freq="2412")
167     dev[0].request("ROAM " + bssid)
168     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
169                             "CTRL-EVENT-CONNECTED"], timeout=10)
170     if ev is None:
171         raise Exception("Roaming with the AP timed out")
172     if "CTRL-EVENT-EAP-STARTED" in ev:
173         raise Exception("Unexpected EAP exchange")
174     pmksa1b = dev[0].get_pmksa(bssid)
175     if pmksa1b is None:
176         raise Exception("No PMKSA cache entry found")
177     if pmksa['pmkid'] != pmksa1b['pmkid']:
178         raise Exception("Unexpected PMKID change for AP1")
179
180 def test_pmksa_cache_opportunistic(dev, apdev):
181     """Opportunistic PMKSA caching"""
182     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
183     params['okc'] = "1"
184     hostapd.add_ap(apdev[0]['ifname'], params)
185     bssid = apdev[0]['bssid']
186     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
187                    eap="GPSK", identity="gpsk user",
188                    password="abcdefghijklmnop0123456789abcdef", okc=True,
189                    scan_freq="2412")
190     pmksa = dev[0].get_pmksa(bssid)
191     if pmksa is None:
192         raise Exception("No PMKSA cache entry created")
193     if pmksa['opportunistic'] != '0':
194         raise Exception("Unexpected opportunistic PMKSA cache entry")
195
196     hostapd.add_ap(apdev[1]['ifname'], params)
197     bssid2 = apdev[1]['bssid']
198
199     dev[0].dump_monitor()
200     logger.info("Roam to AP2")
201     dev[0].scan(freq="2412")
202     dev[0].request("ROAM " + bssid2)
203     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
204                             "CTRL-EVENT-CONNECTED"], timeout=10)
205     if ev is None:
206         raise Exception("Roaming with the AP timed out")
207     if "CTRL-EVENT-EAP-STARTED" in ev:
208         raise Exception("Unexpected EAP exchange")
209     pmksa2 = dev[0].get_pmksa(bssid2)
210     if pmksa2 is None:
211         raise Exception("No PMKSA cache entry created")
212
213     dev[0].dump_monitor()
214     logger.info("Roam back to AP1")
215     dev[0].scan(freq="2412")
216     dev[0].request("ROAM " + bssid)
217     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
218                             "CTRL-EVENT-CONNECTED"], timeout=10)
219     if ev is None:
220         raise Exception("Roaming with the AP timed out")
221     if "CTRL-EVENT-EAP-STARTED" in ev:
222         raise Exception("Unexpected EAP exchange")
223
224     pmksa1b = dev[0].get_pmksa(bssid)
225     if pmksa1b is None:
226         raise Exception("No PMKSA cache entry found")
227     if pmksa['pmkid'] != pmksa1b['pmkid']:
228         raise Exception("Unexpected PMKID change for AP1")
229
230 def test_pmksa_cache_opportunistic_connect(dev, apdev):
231     """Opportunistic PMKSA caching with connect API"""
232     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
233     params['okc'] = "1"
234     hostapd.add_ap(apdev[0]['ifname'], params)
235     bssid = apdev[0]['bssid']
236     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
237     wpas.interface_add("wlan5", drv_params="force_connect_cmd=1")
238     wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
239                  eap="GPSK", identity="gpsk user",
240                  password="abcdefghijklmnop0123456789abcdef", okc=True,
241                  scan_freq="2412")
242     pmksa = wpas.get_pmksa(bssid)
243     if pmksa is None:
244         raise Exception("No PMKSA cache entry created")
245     if pmksa['opportunistic'] != '0':
246         raise Exception("Unexpected opportunistic PMKSA cache entry")
247
248     hostapd.add_ap(apdev[1]['ifname'], params)
249     bssid2 = apdev[1]['bssid']
250
251     wpas.dump_monitor()
252     logger.info("Roam to AP2")
253     wpas.scan_for_bss(bssid2, freq="2412", force_scan=True)
254     wpas.request("ROAM " + bssid2)
255     ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
256                             "CTRL-EVENT-CONNECTED"], timeout=10)
257     if ev is None:
258         raise Exception("Roaming with the AP timed out")
259     if "CTRL-EVENT-EAP-STARTED" in ev:
260         raise Exception("Unexpected EAP exchange")
261     pmksa2 = wpas.get_pmksa(bssid2)
262     if pmksa2 is None:
263         raise Exception("No PMKSA cache entry created")
264
265     wpas.dump_monitor()
266     logger.info("Roam back to AP1")
267     wpas.scan(freq="2412")
268     wpas.request("ROAM " + bssid)
269     ev = wpas.wait_event(["CTRL-EVENT-EAP-STARTED",
270                             "CTRL-EVENT-CONNECTED"], timeout=10)
271     if ev is None:
272         raise Exception("Roaming with the AP timed out")
273     if "CTRL-EVENT-EAP-STARTED" in ev:
274         raise Exception("Unexpected EAP exchange")
275
276     pmksa1b = wpas.get_pmksa(bssid)
277     if pmksa1b is None:
278         raise Exception("No PMKSA cache entry found")
279     if pmksa['pmkid'] != pmksa1b['pmkid']:
280         raise Exception("Unexpected PMKID change for AP1")
281
282 def test_pmksa_cache_expiration(dev, apdev):
283     """PMKSA cache entry expiration"""
284     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
285     hostapd.add_ap(apdev[0]['ifname'], params)
286     bssid = apdev[0]['bssid']
287     dev[0].request("SET dot11RSNAConfigPMKLifetime 10")
288     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
289                    eap="GPSK", identity="gpsk user",
290                    password="abcdefghijklmnop0123456789abcdef",
291                    scan_freq="2412")
292     pmksa = dev[0].get_pmksa(bssid)
293     if pmksa is None:
294         raise Exception("No PMKSA cache entry created")
295     logger.info("Wait for PMKSA cache entry to expire")
296     ev = dev[0].wait_event(["WPA: Key negotiation completed",
297                             "CTRL-EVENT-DISCONNECTED"], timeout=15)
298     if ev is None:
299         raise Exception("No EAP reauthentication seen")
300     if "CTRL-EVENT-DISCONNECTED" in ev:
301         raise Exception("Unexpected disconnection")
302     pmksa2 = dev[0].get_pmksa(bssid)
303     if pmksa['pmkid'] == pmksa2['pmkid']:
304         raise Exception("PMKID did not change")
305
306 def test_pmksa_cache_expiration_disconnect(dev, apdev):
307     """PMKSA cache entry expiration (disconnect)"""
308     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
309     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
310     bssid = apdev[0]['bssid']
311     dev[0].request("SET dot11RSNAConfigPMKLifetime 2")
312     dev[0].request("SET dot11RSNAConfigPMKReauthThreshold 100")
313     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
314                    eap="GPSK", identity="gpsk user",
315                    password="abcdefghijklmnop0123456789abcdef",
316                    scan_freq="2412")
317     pmksa = dev[0].get_pmksa(bssid)
318     if pmksa is None:
319         raise Exception("No PMKSA cache entry created")
320     hapd.request("SET auth_server_shared_secret incorrect")
321     logger.info("Wait for PMKSA cache entry to expire")
322     ev = dev[0].wait_event(["WPA: Key negotiation completed",
323                             "CTRL-EVENT-DISCONNECTED"], timeout=15)
324     if ev is None:
325         raise Exception("No EAP reauthentication seen")
326     if "CTRL-EVENT-DISCONNECTED" not in ev:
327         raise Exception("Missing disconnection")
328     hapd.request("SET auth_server_shared_secret radius")
329     ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=15)
330     if ev is None:
331         raise Exception("No EAP reauthentication seen")
332     pmksa2 = dev[0].get_pmksa(bssid)
333     if pmksa['pmkid'] == pmksa2['pmkid']:
334         raise Exception("PMKID did not change")
335
336 def test_pmksa_cache_and_cui(dev, apdev):
337     """PMKSA cache and Chargeable-User-Identity"""
338     params = hostapd.wpa2_eap_params(ssid="cui")
339     params['radius_request_cui'] = '1'
340     params['acct_server_addr'] = "127.0.0.1"
341     params['acct_server_port'] = "1813"
342     params['acct_server_shared_secret'] = "radius"
343     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
344     bssid = apdev[0]['bssid']
345     dev[0].connect("cui", proto="RSN", key_mgmt="WPA-EAP",
346                    eap="GPSK", identity="gpsk-cui",
347                    password="abcdefghijklmnop0123456789abcdef",
348                    scan_freq="2412")
349     pmksa = dev[0].get_pmksa(bssid)
350     if pmksa is None:
351         raise Exception("No PMKSA cache entry created")
352     ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
353     if ev is None:
354         raise Exception("No connection event received from hostapd")
355
356     dev[0].dump_monitor()
357     logger.info("Disconnect and reconnect to the same AP")
358     dev[0].request("DISCONNECT")
359     dev[0].wait_disconnected()
360     dev[0].request("RECONNECT")
361     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
362                             "CTRL-EVENT-CONNECTED"], timeout=10)
363     if ev is None:
364         raise Exception("Reconnect timed out")
365     if "CTRL-EVENT-EAP-STARTED" in ev:
366         raise Exception("Unexpected EAP exchange")
367     pmksa1b = dev[0].get_pmksa(bssid)
368     if pmksa1b is None:
369         raise Exception("No PMKSA cache entry found")
370     if pmksa['pmkid'] != pmksa1b['pmkid']:
371         raise Exception("Unexpected PMKID change for AP1")
372
373     dev[0].request("REAUTHENTICATE")
374     ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
375     if ev is None:
376         raise Exception("EAP success timed out")
377     for i in range(0, 20):
378         state = dev[0].get_status_field("wpa_state")
379         if state == "COMPLETED":
380             break
381         time.sleep(0.1)
382     if state != "COMPLETED":
383         raise Exception("Reauthentication did not complete")
384
385 def test_pmksa_cache_preauth(dev, apdev):
386     """RSN pre-authentication to generate PMKSA cache entry"""
387     try:
388         params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
389         params['bridge'] = 'ap-br0'
390         hostapd.add_ap(apdev[0]['ifname'], params)
391         subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
392         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
393         eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
394                     password_hex="0123456789abcdef0123456789abcdef")
395
396         params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
397         params['bridge'] = 'ap-br0'
398         params['rsn_preauth'] = '1'
399         params['rsn_preauth_interfaces'] = 'ap-br0'
400         hostapd.add_ap(apdev[1]['ifname'], params)
401         bssid1 = apdev[1]['bssid']
402         dev[0].scan(freq="2412")
403         success = False
404         status_seen = False
405         for i in range(0, 50):
406             if not status_seen:
407                 status = dev[0].request("STATUS")
408                 if "Pre-authentication EAPOL state machines:" in status:
409                     status_seen = True
410             time.sleep(0.1)
411             pmksa = dev[0].get_pmksa(bssid1)
412             if pmksa:
413                 success = True
414                 break
415         if not success:
416             raise Exception("No PMKSA cache entry created from pre-authentication")
417         if not status_seen:
418             raise Exception("Pre-authentication EAPOL status was not available")
419
420         dev[0].scan(freq="2412")
421         if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
422             raise Exception("Scan results missing RSN element info")
423         dev[0].request("ROAM " + bssid1)
424         ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
425                                 "CTRL-EVENT-CONNECTED"], timeout=10)
426         if ev is None:
427             raise Exception("Roaming with the AP timed out")
428         if "CTRL-EVENT-EAP-STARTED" in ev:
429             raise Exception("Unexpected EAP exchange")
430         pmksa2 = dev[0].get_pmksa(bssid1)
431         if pmksa2 is None:
432             raise Exception("No PMKSA cache entry")
433         if pmksa['pmkid'] != pmksa2['pmkid']:
434             raise Exception("Unexpected PMKID change")
435
436     finally:
437         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
438         subprocess.call(['brctl', 'delbr', 'ap-br0'])
439
440 def test_pmksa_cache_preauth_vlan_enabled(dev, apdev):
441     """RSN pre-authentication to generate PMKSA cache entry (dynamic_vlan optional but station without VLAN set)"""
442     try:
443         params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
444         params['bridge'] = 'ap-br0'
445         params['dynamic_vlan'] = '1'
446         hostapd.add_ap(apdev[0]['ifname'], params)
447         subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
448         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
449         eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
450                     password_hex="0123456789abcdef0123456789abcdef")
451
452         params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
453         params['bridge'] = 'ap-br0'
454         params['rsn_preauth'] = '1'
455         params['rsn_preauth_interfaces'] = 'ap-br0'
456         params['dynamic_vlan'] = '1'
457         hostapd.add_ap(apdev[1]['ifname'], params)
458         bssid1 = apdev[1]['bssid']
459         dev[0].scan(freq="2412")
460         success = False
461         status_seen = False
462         for i in range(0, 50):
463             if not status_seen:
464                 status = dev[0].request("STATUS")
465                 if "Pre-authentication EAPOL state machines:" in status:
466                     status_seen = True
467             time.sleep(0.1)
468             pmksa = dev[0].get_pmksa(bssid1)
469             if pmksa:
470                 success = True
471                 break
472         if not success:
473             raise Exception("No PMKSA cache entry created from pre-authentication")
474         if not status_seen:
475             raise Exception("Pre-authentication EAPOL status was not available")
476
477         dev[0].scan(freq="2412")
478         if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
479             raise Exception("Scan results missing RSN element info")
480         dev[0].request("ROAM " + bssid1)
481         ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
482                                 "CTRL-EVENT-CONNECTED"], timeout=10)
483         if ev is None:
484             raise Exception("Roaming with the AP timed out")
485         if "CTRL-EVENT-EAP-STARTED" in ev:
486             raise Exception("Unexpected EAP exchange")
487         pmksa2 = dev[0].get_pmksa(bssid1)
488         if pmksa2 is None:
489             raise Exception("No PMKSA cache entry")
490         if pmksa['pmkid'] != pmksa2['pmkid']:
491             raise Exception("Unexpected PMKID change")
492
493     finally:
494         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
495         subprocess.call(['brctl', 'delbr', 'ap-br0'])
496
497 def test_pmksa_cache_preauth_vlan_used(dev, apdev):
498     """RSN pre-authentication to generate PMKSA cache entry (station with VLAN set)"""
499     try:
500         subprocess.call(['brctl', 'addbr', 'brvlan1'])
501         subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
502         params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
503         params['bridge'] = 'ap-br0'
504         params['dynamic_vlan'] = '1'
505         params['vlan_file'] = 'hostapd.wlan3.vlan'
506         hapd = hostapd.add_ap(apdev[0]['ifname'], params)
507         subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
508         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
509         eap_connect(dev[0], apdev[0], "PAX", "vlan1",
510                     password_hex="0123456789abcdef0123456789abcdef")
511
512         # Verify connectivity in the correct VLAN
513         hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
514
515         params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
516         params['bridge'] = 'ap-br0'
517         params['rsn_preauth'] = '1'
518         params['rsn_preauth_interfaces'] = 'brvlan1'
519         params['dynamic_vlan'] = '1'
520         params['vlan_file'] = 'hostapd.wlan4.vlan'
521         hostapd.add_ap(apdev[1]['ifname'], params)
522         bssid1 = apdev[1]['bssid']
523         dev[0].scan(freq="2412")
524         success = False
525         status_seen = False
526         for i in range(0, 50):
527             if not status_seen:
528                 status = dev[0].request("STATUS")
529                 if "Pre-authentication EAPOL state machines:" in status:
530                     status_seen = True
531             time.sleep(0.1)
532             pmksa = dev[0].get_pmksa(bssid1)
533             if pmksa:
534                 success = True
535                 break
536         if not success:
537             raise Exception("No PMKSA cache entry created from pre-authentication")
538         if not status_seen:
539             raise Exception("Pre-authentication EAPOL status was not available")
540
541         dev[0].scan(freq="2412")
542         if "[WPA2-EAP-CCMP-preauth]" not in dev[0].request("SCAN_RESULTS"):
543             raise Exception("Scan results missing RSN element info")
544         dev[0].request("ROAM " + bssid1)
545         ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
546                                 "CTRL-EVENT-CONNECTED"], timeout=10)
547         if ev is None:
548             raise Exception("Roaming with the AP timed out")
549         if "CTRL-EVENT-EAP-STARTED" in ev:
550             raise Exception("Unexpected EAP exchange")
551         pmksa2 = dev[0].get_pmksa(bssid1)
552         if pmksa2 is None:
553             raise Exception("No PMKSA cache entry")
554         if pmksa['pmkid'] != pmksa2['pmkid']:
555             raise Exception("Unexpected PMKID change")
556
557         # Verify connectivity in the correct VLAN
558         hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
559
560         # Disconnect the STA from both APs to avoid forceful ifdown by the
561         # test script on a VLAN that this has an associated STA. That used to
562         # trigger a mac80211 warning.
563         dev[0].request("DISCONNECT")
564         hapd.request("DISABLE")
565
566     finally:
567         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'],
568                         stderr=open('/dev/null', 'w'))
569         subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
570         subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
571                         stderr=open('/dev/null', 'w'))
572         subprocess.call(['ip', 'link', 'set', 'dev', 'wlan4.1', 'down'],
573                         stderr=open('/dev/null', 'w'))
574         subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
575                         stderr=open('/dev/null', 'w'))
576         subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan4.1'],
577                         stderr=open('/dev/null', 'w'))
578         subprocess.call(['brctl', 'delbr', 'ap-br0'],
579                         stderr=open('/dev/null', 'w'))
580         subprocess.call(['brctl', 'delbr', 'brvlan1'])
581
582 def test_pmksa_cache_disabled(dev, apdev):
583     """PMKSA cache disabling on AP"""
584     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
585     params['disable_pmksa_caching'] = '1'
586     hostapd.add_ap(apdev[0]['ifname'], params)
587     bssid = apdev[0]['bssid']
588     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
589                    eap="GPSK", identity="gpsk user",
590                    password="abcdefghijklmnop0123456789abcdef",
591                    scan_freq="2412")
592
593     hostapd.add_ap(apdev[1]['ifname'], params)
594     bssid2 = apdev[1]['bssid']
595
596     dev[0].dump_monitor()
597     logger.info("Roam to AP2")
598     dev[0].scan_for_bss(bssid2, freq="2412")
599     dev[0].request("ROAM " + bssid2)
600     ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
601     if ev is None:
602         raise Exception("EAP success timed out")
603     dev[0].wait_connected(timeout=10, error="Roaming timed out")
604
605     dev[0].dump_monitor()
606     logger.info("Roam back to AP1")
607     dev[0].scan(freq="2412")
608     dev[0].request("ROAM " + bssid)
609     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
610                             "CTRL-EVENT-CONNECTED"], timeout=20)
611     if ev is None:
612         raise Exception("Roaming with the AP timed out")
613     if "CTRL-EVENT-CONNECTED" in ev:
614         raise Exception("EAP exchange missing")
615     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=20)
616     if ev is None:
617         raise Exception("Roaming with the AP timed out")
618
619 def test_pmksa_cache_ap_expiration(dev, apdev):
620     """PMKSA cache entry expiring on AP"""
621     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
622     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
623     bssid = apdev[0]['bssid']
624     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
625                    eap="GPSK", identity="gpsk-user-session-timeout",
626                    password="abcdefghijklmnop0123456789abcdef",
627                    scan_freq="2412")
628     ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
629     if ev is None:
630         raise Exception("No connection event received from hostapd")
631     dev[0].request("DISCONNECT")
632     time.sleep(5)
633     dev[0].dump_monitor()
634     dev[0].request("RECONNECT")
635     ev = dev[0].wait_event(["CTRL-EVENT-EAP-STARTED",
636                             "CTRL-EVENT-CONNECTED"], timeout=20)
637     if ev is None:
638         raise Exception("Roaming with the AP timed out")
639     if "CTRL-EVENT-CONNECTED" in ev:
640         raise Exception("EAP exchange missing")
641     dev[0].wait_connected(timeout=20, error="Reconnect timed out")
642     dev[0].dump_monitor()
643     dev[0].wait_disconnected(timeout=20)
644     dev[0].wait_connected(timeout=20, error="Reassociation timed out")
645
646 def test_pmksa_cache_multiple_sta(dev, apdev):
647     """PMKSA cache with multiple stations"""
648     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
649     hostapd.add_ap(apdev[0]['ifname'], params)
650     bssid = apdev[0]['bssid']
651     dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
652                    eap="GPSK", identity="gpsk-user-session-timeout",
653                    password="abcdefghijklmnop0123456789abcdef",
654                    scan_freq="2412")
655     dev[1].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
656                    eap="GPSK", identity="gpsk user",
657                    password="abcdefghijklmnop0123456789abcdef",
658                    scan_freq="2412")
659     dev[2].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
660                    eap="GPSK", identity="gpsk-user-session-timeout",
661                    password="abcdefghijklmnop0123456789abcdef",
662                    scan_freq="2412")
663
664     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
665     wpas.interface_add("wlan5")
666     wpas.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
667                  eap="GPSK", identity="gpsk user",
668                  password="abcdefghijklmnop0123456789abcdef",
669                  scan_freq="2412")
670
671     hostapd.add_ap(apdev[1]['ifname'], params)
672     bssid2 = apdev[1]['bssid']
673
674     logger.info("Roam to AP2")
675     for sta in [ dev[1], dev[0], dev[2], wpas ]:
676         sta.dump_monitor()
677         sta.scan_for_bss(bssid2, freq="2412")
678         sta.request("ROAM " + bssid2)
679         ev = sta.wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=10)
680         if ev is None:
681             raise Exception("EAP success timed out")
682         sta.wait_connected(timeout=10, error="Roaming timed out")
683
684     logger.info("Roam back to AP1")
685     for sta in [ dev[1], wpas, dev[0], dev[2] ]:
686         sta.dump_monitor()
687         sta.scan(freq="2412")
688         sta.dump_monitor()
689         sta.request("ROAM " + bssid)
690         sta.wait_connected(timeout=10, error="Roaming timed out")
691         sta.dump_monitor()
692
693     time.sleep(4)
694
695     logger.info("Roam back to AP2")
696     for sta in [ dev[1], wpas, dev[0], dev[2] ]:
697         sta.dump_monitor()
698         sta.scan(freq="2412")
699         sta.dump_monitor()
700         sta.request("ROAM " + bssid2)
701         sta.wait_connected(timeout=10, error="Roaming timed out")
702         sta.dump_monitor()
703
704 def test_pmksa_cache_opportunistic_multiple_sta(dev, apdev):
705     """Opportunistic PMKSA caching with multiple stations"""
706     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
707     params['okc'] = "1"
708     hostapd.add_ap(apdev[0]['ifname'], params)
709     bssid = apdev[0]['bssid']
710     wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
711     wpas.interface_add("wlan5")
712     for sta in [ dev[0], dev[1], dev[2], wpas ]:
713         sta.connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
714                     eap="GPSK", identity="gpsk user",
715                     password="abcdefghijklmnop0123456789abcdef", okc=True,
716                     scan_freq="2412")
717
718     hostapd.add_ap(apdev[1]['ifname'], params)
719     bssid2 = apdev[1]['bssid']
720
721     logger.info("Roam to AP2")
722     for sta in [ dev[2], dev[0], wpas, dev[1] ]:
723         sta.dump_monitor()
724         sta.scan_for_bss(bssid2, freq="2412")
725         if "OK" not in sta.request("ROAM " + bssid2):
726             raise Exception("ROAM command failed")
727         ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
728                              "CTRL-EVENT-CONNECTED"], timeout=10)
729         if ev is None:
730             raise Exception("Roaming with the AP timed out")
731         if "CTRL-EVENT-EAP-STARTED" in ev:
732             raise Exception("Unexpected EAP exchange")
733         pmksa2 = sta.get_pmksa(bssid2)
734         if pmksa2 is None:
735             raise Exception("No PMKSA cache entry created")
736
737     logger.info("Roam back to AP1")
738     for sta in [ dev[0], dev[1], dev[2], wpas ]:
739         sta.dump_monitor()
740         sta.scan_for_bss(bssid, freq="2412")
741         sta.request("ROAM " + bssid)
742         ev = sta.wait_event(["CTRL-EVENT-EAP-STARTED",
743                              "CTRL-EVENT-CONNECTED"], timeout=10)
744         if ev is None:
745             raise Exception("Roaming with the AP timed out")
746         if "CTRL-EVENT-EAP-STARTED" in ev:
747             raise Exception("Unexpected EAP exchange")
748
749 def test_pmksa_cache_preauth_oom(dev, apdev):
750     """RSN pre-authentication to generate PMKSA cache entry and OOM"""
751     try:
752         _test_pmksa_cache_preauth_oom(dev, apdev)
753     finally:
754         subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'down'])
755         subprocess.call(['brctl', 'delbr', 'ap-br0'])
756
757 def _test_pmksa_cache_preauth_oom(dev, apdev):
758     params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
759     params['bridge'] = 'ap-br0'
760     hostapd.add_ap(apdev[0]['ifname'], params)
761     subprocess.call(['brctl', 'setfd', 'ap-br0', '0'])
762     subprocess.call(['ip', 'link', 'set', 'dev', 'ap-br0', 'up'])
763     eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
764                 password_hex="0123456789abcdef0123456789abcdef",
765                 bssid=apdev[0]['bssid'])
766
767     params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
768     params['bridge'] = 'ap-br0'
769     params['rsn_preauth'] = '1'
770     params['rsn_preauth_interfaces'] = 'ap-br0'
771     hapd = hostapd.add_ap(apdev[1]['ifname'], params)
772     bssid1 = apdev[1]['bssid']
773
774     tests = [ (1, "rsn_preauth_receive"),
775               (2, "rsn_preauth_receive"),
776               (1, "rsn_preauth_send") ]
777     for test in tests:
778         with alloc_fail(hapd, test[0], test[1]):
779             dev[0].scan_for_bss(bssid1, freq="2412")
780             if "OK" not in dev[0].request("PREAUTH " + bssid1):
781                 raise Exception("PREAUTH failed")
782
783             success = False
784             count = 0
785             for i in range(50):
786                 time.sleep(0.1)
787                 pmksa = dev[0].get_pmksa(bssid1)
788                 if pmksa:
789                     success = True
790                     break
791                 state = hapd.request('GET_ALLOC_FAIL')
792                 if state.startswith('0:'):
793                     count += 1
794                     if count > 2:
795                         break
796             logger.info("PMKSA cache success: " + str(success))
797
798             dev[0].request("PMKSA_FLUSH")
799             dev[0].wait_disconnected()
800             dev[0].wait_connected()
801             dev[0].dump_monitor()
802
803 def test_pmksa_cache_size_limit(dev, apdev):
804     """PMKSA cache size limit in wpa_supplicant"""
805     try:
806         _test_pmksa_cache_size_limit(dev, apdev)
807     finally:
808         try:
809             hapd = hostapd.HostapdGlobal()
810             hapd.flush()
811             hapd.remove(apdev[0]['ifname'])
812         except:
813             pass
814         params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
815         bssid = apdev[0]['bssid']
816         params['bssid'] = bssid
817         hostapd.add_ap(apdev[0]['ifname'], params)
818
819 def _test_pmksa_cache_size_limit(dev, apdev):
820     params = hostapd.wpa2_eap_params(ssid="test-pmksa-cache")
821     id = dev[0].connect("test-pmksa-cache", proto="RSN", key_mgmt="WPA-EAP",
822                         eap="GPSK", identity="gpsk user",
823                         password="abcdefghijklmnop0123456789abcdef",
824                         scan_freq="2412", only_add_network=True)
825     for i in range(33):
826         bssid = apdev[0]['bssid'][0:15] + "%02x" % i
827         logger.info("Iteration with BSSID " + bssid)
828         params['bssid'] = bssid
829         hostapd.add_ap(apdev[0]['ifname'], params)
830         dev[0].request("BSS_FLUSH 0")
831         dev[0].scan_for_bss(bssid, freq=2412, only_new=True)
832         dev[0].select_network(id)
833         dev[0].wait_connected()
834         dev[0].request("DISCONNECT")
835         dev[0].wait_disconnected()
836         dev[0].dump_monitor()
837         entries = len(dev[0].request("PMKSA").splitlines()) - 1
838         if i == 32:
839             if entries != 32:
840                 raise Exception("Unexpected number of PMKSA entries after expected removal of the oldest entry")
841         elif i + 1 != entries:
842             raise Exception("Unexpected number of PMKSA entries")
843
844         hapd = hostapd.HostapdGlobal()
845         hapd.flush()
846         hapd.remove(apdev[0]['ifname'])
847
848 def test_pmksa_cache_preauth_timeout(dev, apdev):
849     """RSN pre-authentication timing out"""
850     try:
851         _test_pmksa_cache_preauth_timeout(dev, apdev)
852     finally:
853         dev[0].request("SET dot11RSNAConfigSATimeout 60")
854
855 def _test_pmksa_cache_preauth_timeout(dev, apdev):
856     dev[0].request("SET dot11RSNAConfigSATimeout 1")
857     params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
858     hostapd.add_ap(apdev[0]['ifname'], params)
859     eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
860                 password_hex="0123456789abcdef0123456789abcdef",
861                 bssid=apdev[0]['bssid'])
862     if "OK" not in dev[0].request("PREAUTH f2:11:22:33:44:55"):
863         raise Exception("PREAUTH failed")
864     ev = dev[0].wait_event(["RSN: pre-authentication with"], timeout=5)
865     if ev is None:
866         raise Exception("No timeout event seen")
867     if "timed out" not in ev:
868         raise Exception("Unexpected event: " + ev)
869
870 def test_pmksa_cache_preauth_wpas_oom(dev, apdev):
871     """RSN pre-authentication OOM in wpa_supplicant"""
872     params = hostapd.wpa2_eap_params(ssid="test-wpa2-eap")
873     hostapd.add_ap(apdev[0]['ifname'], params)
874     eap_connect(dev[0], apdev[0], "PAX", "pax.user@example.com",
875                 password_hex="0123456789abcdef0123456789abcdef",
876                 bssid=apdev[0]['bssid'])
877     for i in range(1, 11):
878         with alloc_fail(dev[0], i, "rsn_preauth_init"):
879             res = dev[0].request("PREAUTH f2:11:22:33:44:55").strip()
880             logger.info("Iteration %d - PREAUTH command results: %s" % (i, res))
881             for j in range(10):
882                 state = dev[0].request('GET_ALLOC_FAIL')
883                 if state.startswith('0:'):
884                     break
885                 time.sleep(0.05)