tests: Mesh and local SAE failures
[mech_eap.git] / tests / hwsim / test_wpas_mesh.py
1 # wpa_supplicant mesh mode tests
2 # Copyright (c) 2014, cozybit Inc.
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 os
10 import subprocess
11 import time
12
13 import hwsim_utils
14 from wpasupplicant import WpaSupplicant
15 from utils import HwsimSkip, alloc_fail, fail_test, wait_fail_trigger
16 from tshark import run_tshark
17
18 def check_mesh_support(dev, secure=False):
19     if "MESH" not in dev.get_capability("modes"):
20         raise HwsimSkip("Driver does not support mesh")
21     if secure and "SAE" not in dev.get_capability("auth_alg"):
22         raise HwsimSkip("SAE not supported")
23
24 def check_mesh_scan(dev, params, other_started=False, beacon_int=0):
25     if not other_started:
26         dev.dump_monitor()
27     id = dev.request("SCAN " + params)
28     if "FAIL" in id:
29         raise Exception("Failed to start scan")
30     id = int(id)
31
32     if other_started:
33         ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
34         if ev is None:
35             raise Exception("Other scan did not start")
36         if "id=" + str(id) in ev:
37             raise Exception("Own scan id unexpectedly included in start event")
38
39         ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
40         if ev is None:
41             raise Exception("Other scan did not complete")
42         if "id=" + str(id) in ev:
43             raise Exception(
44                 "Own scan id unexpectedly included in completed event")
45
46     ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
47     if ev is None:
48         raise Exception("Scan did not start")
49     if "id=" + str(id) not in ev:
50         raise Exception("Scan id not included in start event")
51
52     ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
53     if ev is None:
54         raise Exception("Scan did not complete")
55     if "id=" + str(id) not in ev:
56         raise Exception("Scan id not included in completed event")
57
58     res = dev.request("SCAN_RESULTS")
59
60     if res.find("[MESH]") < 0:
61         raise Exception("Scan did not contain a MESH network")
62
63     bssid = res.splitlines()[1].split(' ')[0]
64     bss = dev.get_bss(bssid)
65     if bss is None:
66         raise Exception("Could not get BSS entry for mesh")
67     if 'mesh_capability' not in bss:
68         raise Exception("mesh_capability missing from BSS entry")
69     if beacon_int:
70         if 'beacon_int' not in bss:
71             raise Exception("beacon_int missing from BSS entry")
72         if str(beacon_int) != bss['beacon_int']:
73             raise Exception("Unexpected beacon_int in BSS entry: " + bss['beacon_int'])
74
75 def check_mesh_group_added(dev):
76     ev = dev.wait_event(["MESH-GROUP-STARTED"])
77     if ev is None:
78         raise Exception("Test exception: Couldn't join mesh")
79
80
81 def check_mesh_group_removed(dev):
82     ev = dev.wait_event(["MESH-GROUP-REMOVED"])
83     if ev is None:
84         raise Exception("Test exception: Couldn't leave mesh")
85
86
87 def check_mesh_peer_connected(dev, timeout=10):
88     ev = dev.wait_event(["MESH-PEER-CONNECTED"], timeout=timeout)
89     if ev is None:
90         raise Exception("Test exception: Remote peer did not connect.")
91
92
93 def check_mesh_peer_disconnected(dev):
94     ev = dev.wait_event(["MESH-PEER-DISCONNECTED"])
95     if ev is None:
96         raise Exception("Test exception: Peer disconnect event not detected.")
97
98
99 def test_wpas_add_set_remove_support(dev):
100     """wpa_supplicant MESH add/set/remove network support"""
101     check_mesh_support(dev[0])
102     id = dev[0].add_network()
103     dev[0].set_network(id, "mode", "5")
104     dev[0].remove_network(id)
105
106 def add_open_mesh_network(dev, freq="2412", start=True, beacon_int=0,
107                           basic_rates=None, chwidth=0):
108     id = dev.add_network()
109     dev.set_network(id, "mode", "5")
110     dev.set_network_quoted(id, "ssid", "wpas-mesh-open")
111     dev.set_network(id, "key_mgmt", "NONE")
112     dev.set_network(id, "frequency", freq)
113     if chwidth > 0:
114         dev.set_network(id, "max_oper_chwidth", str(chwidth))
115     if beacon_int:
116         dev.set_network(id, "beacon_int", str(beacon_int))
117     if basic_rates:
118         dev.set_network(id, "mesh_basic_rates", basic_rates)
119     if start:
120         dev.mesh_group_add(id)
121     return id
122
123 def test_wpas_mesh_group_added(dev):
124     """wpa_supplicant MESH group add"""
125     check_mesh_support(dev[0])
126     add_open_mesh_network(dev[0])
127
128     # Check for MESH-GROUP-STARTED event
129     check_mesh_group_added(dev[0])
130
131
132 def test_wpas_mesh_group_remove(dev):
133     """wpa_supplicant MESH group remove"""
134     check_mesh_support(dev[0])
135     add_open_mesh_network(dev[0])
136     # Check for MESH-GROUP-STARTED event
137     check_mesh_group_added(dev[0])
138     dev[0].mesh_group_remove()
139     # Check for MESH-GROUP-REMOVED event
140     check_mesh_group_removed(dev[0])
141     dev[0].mesh_group_remove()
142
143 def test_wpas_mesh_peer_connected(dev):
144     """wpa_supplicant MESH peer connected"""
145     check_mesh_support(dev[0])
146     add_open_mesh_network(dev[0], beacon_int=160)
147     add_open_mesh_network(dev[1], beacon_int=160)
148
149     # Check for mesh joined
150     check_mesh_group_added(dev[0])
151     check_mesh_group_added(dev[1])
152
153     # Check for peer connected
154     check_mesh_peer_connected(dev[0])
155     check_mesh_peer_connected(dev[1])
156
157
158 def test_wpas_mesh_peer_disconnected(dev):
159     """wpa_supplicant MESH peer disconnected"""
160     check_mesh_support(dev[0])
161     add_open_mesh_network(dev[0])
162     add_open_mesh_network(dev[1])
163
164     # Check for mesh joined
165     check_mesh_group_added(dev[0])
166     check_mesh_group_added(dev[1])
167
168     # Check for peer connected
169     check_mesh_peer_connected(dev[0])
170     check_mesh_peer_connected(dev[1])
171
172     # Remove group on dev 1
173     dev[1].mesh_group_remove()
174     # Device 0 should get a disconnection event
175     check_mesh_peer_disconnected(dev[0])
176
177
178 def test_wpas_mesh_mode_scan(dev):
179     """wpa_supplicant MESH scan support"""
180     check_mesh_support(dev[0])
181     add_open_mesh_network(dev[0])
182     add_open_mesh_network(dev[1], beacon_int=175)
183
184     # Check for mesh joined
185     check_mesh_group_added(dev[0])
186     check_mesh_group_added(dev[1])
187
188     # Check for Mesh scan
189     check_mesh_scan(dev[0], "use_id=1", beacon_int=175)
190
191 def test_wpas_mesh_open(dev, apdev):
192     """wpa_supplicant open MESH network connectivity"""
193     check_mesh_support(dev[0])
194     add_open_mesh_network(dev[0], freq="2462", basic_rates="60 120 240")
195     add_open_mesh_network(dev[1], freq="2462", basic_rates="60 120 240")
196
197     # Check for mesh joined
198     check_mesh_group_added(dev[0])
199     check_mesh_group_added(dev[1])
200
201     # Check for peer connected
202     check_mesh_peer_connected(dev[0])
203     check_mesh_peer_connected(dev[1])
204
205     # Test connectivity 0->1 and 1->0
206     hwsim_utils.test_connectivity(dev[0], dev[1])
207
208 def test_wpas_mesh_open_no_auto(dev, apdev):
209     """wpa_supplicant open MESH network connectivity"""
210     check_mesh_support(dev[0])
211     id = add_open_mesh_network(dev[0], start=False)
212     dev[0].set_network(id, "dot11MeshMaxRetries", "16")
213     dev[0].set_network(id, "dot11MeshRetryTimeout", "255")
214     dev[0].mesh_group_add(id)
215
216     id = add_open_mesh_network(dev[1], start=False)
217     dev[1].set_network(id, "no_auto_peer", "1")
218     dev[1].mesh_group_add(id)
219
220     # Check for mesh joined
221     check_mesh_group_added(dev[0])
222     check_mesh_group_added(dev[1])
223
224     # Check for peer connected
225     check_mesh_peer_connected(dev[0], timeout=30)
226     check_mesh_peer_connected(dev[1])
227
228     # Test connectivity 0->1 and 1->0
229     hwsim_utils.test_connectivity(dev[0], dev[1])
230
231 def add_mesh_secure_net(dev, psk=True):
232     id = dev.add_network()
233     dev.set_network(id, "mode", "5")
234     dev.set_network_quoted(id, "ssid", "wpas-mesh-sec")
235     dev.set_network(id, "key_mgmt", "SAE")
236     dev.set_network(id, "frequency", "2412")
237     if psk:
238         dev.set_network_quoted(id, "psk", "thisismypassphrase!")
239     return id
240
241 def test_wpas_mesh_secure(dev, apdev):
242     """wpa_supplicant secure MESH network connectivity"""
243     check_mesh_support(dev[0], secure=True)
244     dev[0].request("SET sae_groups ")
245     id = add_mesh_secure_net(dev[0])
246     dev[0].mesh_group_add(id)
247
248     dev[1].request("SET sae_groups ")
249     id = add_mesh_secure_net(dev[1])
250     dev[1].mesh_group_add(id)
251
252     # Check for mesh joined
253     check_mesh_group_added(dev[0])
254     check_mesh_group_added(dev[1])
255
256     # Check for peer connected
257     check_mesh_peer_connected(dev[0])
258     check_mesh_peer_connected(dev[1])
259
260     # Test connectivity 0->1 and 1->0
261     hwsim_utils.test_connectivity(dev[0], dev[1])
262
263 def test_wpas_mesh_secure_sae_group_mismatch(dev, apdev):
264     """wpa_supplicant secure MESH and SAE group mismatch"""
265     check_mesh_support(dev[0], secure=True)
266     addr0 = dev[0].p2p_interface_addr()
267     addr1 = dev[1].p2p_interface_addr()
268     addr2 = dev[2].p2p_interface_addr()
269
270     dev[0].request("SET sae_groups 19 25")
271     id = add_mesh_secure_net(dev[0])
272     dev[0].mesh_group_add(id)
273
274     dev[1].request("SET sae_groups 19")
275     id = add_mesh_secure_net(dev[1])
276     dev[1].mesh_group_add(id)
277
278     dev[2].request("SET sae_groups 26")
279     id = add_mesh_secure_net(dev[2])
280     dev[2].mesh_group_add(id)
281
282     check_mesh_group_added(dev[0])
283     check_mesh_group_added(dev[1])
284     check_mesh_group_added(dev[2])
285
286     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"])
287     if ev is None:
288         raise Exception("Remote peer did not connect")
289     if addr1 not in ev:
290         raise Exception("Unexpected peer connected: " + ev)
291
292     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"])
293     if ev is None:
294         raise Exception("Remote peer did not connect")
295     if addr0 not in ev:
296         raise Exception("Unexpected peer connected: " + ev)
297
298     ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
299     if ev is not None:
300         raise Exception("Unexpected peer connection at dev[2]: " + ev)
301
302     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
303     if ev is not None:
304         raise Exception("Unexpected peer connection: " + ev)
305
306     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
307     if ev is not None:
308         raise Exception("Unexpected peer connection: " + ev)
309
310     dev[0].request("SET sae_groups ")
311     dev[1].request("SET sae_groups ")
312     dev[2].request("SET sae_groups ")
313
314 def test_wpas_mesh_secure_sae_group_negotiation(dev, apdev):
315     """wpa_supplicant secure MESH and SAE group negotiation"""
316     check_mesh_support(dev[0], secure=True)
317     addr0 = dev[0].own_addr()
318     addr1 = dev[1].own_addr()
319
320     #dev[0].request("SET sae_groups 21 20 25 26")
321     dev[0].request("SET sae_groups 25")
322     id = add_mesh_secure_net(dev[0])
323     dev[0].mesh_group_add(id)
324
325     dev[1].request("SET sae_groups 19 25")
326     id = add_mesh_secure_net(dev[1])
327     dev[1].mesh_group_add(id)
328
329     check_mesh_group_added(dev[0])
330     check_mesh_group_added(dev[1])
331
332     check_mesh_peer_connected(dev[0])
333     check_mesh_peer_connected(dev[1])
334
335     dev[0].request("SET sae_groups ")
336     dev[1].request("SET sae_groups ")
337
338 def test_wpas_mesh_secure_sae_missing_password(dev, apdev):
339     """wpa_supplicant secure MESH and missing SAE password"""
340     check_mesh_support(dev[0], secure=True)
341     id = add_mesh_secure_net(dev[0], psk=False)
342     dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
343     dev[0].mesh_group_add(id)
344     ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
345                            timeout=5)
346     if ev is None:
347         raise Exception("Timeout on mesh start event")
348     if "MESH-GROUP-STARTED" in ev:
349         raise Exception("Unexpected mesh group start")
350     ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
351     if ev is not None:
352         raise Exception("Unexpected mesh group start")
353
354 def test_wpas_mesh_secure_no_auto(dev, apdev):
355     """wpa_supplicant secure MESH network connectivity"""
356     check_mesh_support(dev[0], secure=True)
357     dev[0].request("SET sae_groups 19")
358     id = add_mesh_secure_net(dev[0])
359     dev[0].mesh_group_add(id)
360
361     dev[1].request("SET sae_groups 19")
362     id = add_mesh_secure_net(dev[1])
363     dev[1].set_network(id, "no_auto_peer", "1")
364     dev[1].mesh_group_add(id)
365
366     # Check for mesh joined
367     check_mesh_group_added(dev[0])
368     check_mesh_group_added(dev[1])
369
370     # Check for peer connected
371     check_mesh_peer_connected(dev[0], timeout=30)
372     check_mesh_peer_connected(dev[1])
373
374     # Test connectivity 0->1 and 1->0
375     hwsim_utils.test_connectivity(dev[0], dev[1])
376
377     dev[0].request("SET sae_groups ")
378     dev[1].request("SET sae_groups ")
379
380 def test_wpas_mesh_secure_dropped_frame(dev, apdev):
381     """Secure mesh network connectivity when the first plink Open is dropped"""
382     check_mesh_support(dev[0], secure=True)
383
384     dev[0].request("SET ext_mgmt_frame_handling 1")
385     dev[0].request("SET sae_groups ")
386     id = add_mesh_secure_net(dev[0])
387     dev[0].mesh_group_add(id)
388
389     dev[1].request("SET sae_groups ")
390     id = add_mesh_secure_net(dev[1])
391     dev[1].mesh_group_add(id)
392
393     # Check for mesh joined
394     check_mesh_group_added(dev[0])
395     check_mesh_group_added(dev[1])
396
397     # Drop the first Action frame (plink Open) to test unexpected order of
398     # Confirm/Open messages.
399     count = 0
400     while True:
401         count += 1
402         if count > 10:
403             raise Exception("Did not see Action frames")
404         rx_msg = dev[0].mgmt_rx()
405         if rx_msg is None:
406             raise Exception("MGMT-RX timeout")
407         if rx_msg['subtype'] == 13:
408             logger.info("Drop the first Action frame")
409             break
410         if "OK" not in dev[0].request("MGMT_RX_PROCESS freq={} datarate={} ssi_signal={} frame={}".format(rx_msg['freq'], rx_msg['datarate'], rx_msg['ssi_signal'], rx_msg['frame'].encode('hex'))):
411             raise Exception("MGMT_RX_PROCESS failed")
412
413     dev[0].request("SET ext_mgmt_frame_handling 0")
414
415     # Check for peer connected
416     check_mesh_peer_connected(dev[0])
417     check_mesh_peer_connected(dev[1])
418
419     # Test connectivity 0->1 and 1->0
420     hwsim_utils.test_connectivity(dev[0], dev[1])
421
422 def test_wpas_mesh_ctrl(dev):
423     """wpa_supplicant ctrl_iface mesh command error cases"""
424     check_mesh_support(dev[0])
425     if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
426         raise Exception("Unexpected MESH_GROUP_ADD success")
427     id = dev[0].add_network()
428     if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
429         raise Exception("Unexpected MESH_GROUP_ADD success")
430     dev[0].set_network(id, "mode", "5")
431     dev[0].set_network(id, "key_mgmt", "WPA-PSK")
432     if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
433         raise Exception("Unexpected MESH_GROUP_ADD success")
434
435     if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
436         raise Exception("Unexpected MESH_GROUP_REMOVE success")
437
438 def test_wpas_mesh_dynamic_interface(dev):
439     """wpa_supplicant mesh with dynamic interface"""
440     check_mesh_support(dev[0])
441     mesh0 = None
442     mesh1 = None
443     try:
444         mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
445         if "FAIL" in mesh0:
446             raise Exception("MESH_INTERFACE_ADD failed")
447         mesh1 = dev[1].request("MESH_INTERFACE_ADD")
448         if "FAIL" in mesh1:
449             raise Exception("MESH_INTERFACE_ADD failed")
450
451         wpas0 = WpaSupplicant(ifname=mesh0)
452         wpas1 = WpaSupplicant(ifname=mesh1)
453         logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
454         logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
455
456         add_open_mesh_network(wpas0)
457         add_open_mesh_network(wpas1)
458         check_mesh_group_added(wpas0)
459         check_mesh_group_added(wpas1)
460         check_mesh_peer_connected(wpas0)
461         check_mesh_peer_connected(wpas1)
462         hwsim_utils.test_connectivity(wpas0, wpas1)
463
464         # Must not allow MESH_GROUP_REMOVE on dynamic interface
465         if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
466             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
467         if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
468             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
469
470         # Must not allow MESH_GROUP_REMOVE on another radio interface
471         if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
472             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
473         if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
474             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
475
476         wpas0.remove_ifname()
477         wpas1.remove_ifname()
478
479         if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
480             raise Exception("MESH_GROUP_REMOVE failed")
481         if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
482             raise Exception("MESH_GROUP_REMOVE failed")
483
484         if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
485             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
486         if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
487             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
488
489         logger.info("Make sure another dynamic group can be added")
490         mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
491         if "FAIL" in mesh0:
492             raise Exception("MESH_INTERFACE_ADD failed")
493         mesh1 = dev[1].request("MESH_INTERFACE_ADD")
494         if "FAIL" in mesh1:
495             raise Exception("MESH_INTERFACE_ADD failed")
496
497         wpas0 = WpaSupplicant(ifname=mesh0)
498         wpas1 = WpaSupplicant(ifname=mesh1)
499         logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
500         logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
501
502         add_open_mesh_network(wpas0)
503         add_open_mesh_network(wpas1)
504         check_mesh_group_added(wpas0)
505         check_mesh_group_added(wpas1)
506         check_mesh_peer_connected(wpas0)
507         check_mesh_peer_connected(wpas1)
508         hwsim_utils.test_connectivity(wpas0, wpas1)
509     finally:
510         if mesh0:
511             dev[0].request("MESH_GROUP_REMOVE " + mesh0)
512         if mesh1:
513             dev[1].request("MESH_GROUP_REMOVE " + mesh1)
514
515 def test_wpas_mesh_max_peering(dev, apdev):
516     """Mesh max peering limit"""
517     check_mesh_support(dev[0])
518     try:
519         dev[0].request("SET max_peer_links 1")
520
521         # first, connect dev[0] and dev[1]
522         add_open_mesh_network(dev[0])
523         add_open_mesh_network(dev[1])
524         for i in range(2):
525             ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
526             if ev is None:
527                 raise Exception("dev%d did not connect with any peer" % i)
528
529         # add dev[2] which will try to connect with both dev[0] and dev[1],
530         # but can complete connection only with dev[1]
531         add_open_mesh_network(dev[2])
532         for i in range(1, 3):
533             ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
534             if ev is None:
535                 raise Exception("dev%d did not connect the second peer" % i)
536
537         ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
538         if ev is not None:
539             raise Exception("dev0 connection beyond max peering limit")
540
541         ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
542         if ev is not None:
543             raise Exception("dev2 reported unexpected peering: " + ev)
544
545         for i in range(3):
546             dev[i].mesh_group_remove()
547             check_mesh_group_removed(dev[i])
548     finally:
549         dev[0].request("SET max_peer_links 99")
550
551 def test_wpas_mesh_open_5ghz(dev, apdev):
552     """wpa_supplicant open MESH network on 5 GHz band"""
553     try:
554         _test_wpas_mesh_open_5ghz(dev, apdev)
555     finally:
556         subprocess.call(['iw', 'reg', 'set', '00'])
557         dev[0].flush_scan_cache()
558         dev[1].flush_scan_cache()
559
560 def _test_wpas_mesh_open_5ghz(dev, apdev):
561     check_mesh_support(dev[0])
562     subprocess.call(['iw', 'reg', 'set', 'US'])
563     for i in range(2):
564         for j in range(5):
565             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
566             if ev is None:
567                 raise Exception("No regdom change event")
568             if "alpha2=US" in ev:
569                 break
570         add_open_mesh_network(dev[i], freq="5180")
571
572     # Check for mesh joined
573     check_mesh_group_added(dev[0])
574     check_mesh_group_added(dev[1])
575
576     # Check for peer connected
577     check_mesh_peer_connected(dev[0])
578     check_mesh_peer_connected(dev[1])
579
580     # Test connectivity 0->1 and 1->0
581     hwsim_utils.test_connectivity(dev[0], dev[1])
582
583 def test_wpas_mesh_open_vht_80p80(dev, apdev):
584     """wpa_supplicant open MESH network on VHT 80+80 MHz channel"""
585     try:
586         _test_wpas_mesh_open_vht_80p80(dev, apdev)
587     finally:
588         subprocess.call(['iw', 'reg', 'set', '00'])
589         dev[0].flush_scan_cache()
590         dev[1].flush_scan_cache()
591
592 def _test_wpas_mesh_open_vht_80p80(dev, apdev):
593     check_mesh_support(dev[0])
594     subprocess.call(['iw', 'reg', 'set', 'US'])
595     for i in range(2):
596         for j in range(5):
597             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
598             if ev is None:
599                 raise Exception("No regdom change event")
600             if "alpha2=US" in ev:
601                 break
602         add_open_mesh_network(dev[i], freq="5180", chwidth=3)
603
604     # Check for mesh joined
605     check_mesh_group_added(dev[0])
606     check_mesh_group_added(dev[1])
607
608     # Check for peer connected
609     check_mesh_peer_connected(dev[0])
610     check_mesh_peer_connected(dev[1])
611
612     # Test connectivity 0->1 and 1->0
613     hwsim_utils.test_connectivity(dev[0], dev[1])
614
615     sig = dev[0].request("SIGNAL_POLL").splitlines()
616     if "WIDTH=80+80 MHz" not in sig:
617         raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
618     if "CENTER_FRQ1=5210" not in sig:
619         raise Exception("Unexpected SIGNAL_POLL value(3): " + str(sig))
620     if "CENTER_FRQ2=5775" not in sig:
621         raise Exception("Unexpected SIGNAL_POLL value(4): " + str(sig))
622
623     sig = dev[1].request("SIGNAL_POLL").splitlines()
624     if "WIDTH=80+80 MHz" not in sig:
625         raise Exception("Unexpected SIGNAL_POLL value(2b): " + str(sig))
626     if "CENTER_FRQ1=5210" not in sig:
627         raise Exception("Unexpected SIGNAL_POLL value(3b): " + str(sig))
628     if "CENTER_FRQ2=5775" not in sig:
629         raise Exception("Unexpected SIGNAL_POLL value(4b): " + str(sig))
630
631 def test_wpas_mesh_password_mismatch(dev, apdev):
632     """Mesh network and one device with mismatching password"""
633     check_mesh_support(dev[0], secure=True)
634     dev[0].request("SET sae_groups ")
635     id = add_mesh_secure_net(dev[0])
636     dev[0].mesh_group_add(id)
637
638     dev[1].request("SET sae_groups ")
639     id = add_mesh_secure_net(dev[1])
640     dev[1].mesh_group_add(id)
641
642     dev[2].request("SET sae_groups ")
643     id = add_mesh_secure_net(dev[2])
644     dev[2].set_network_quoted(id, "psk", "wrong password")
645     dev[2].mesh_group_add(id)
646
647     # The two peers with matching password need to be able to connect
648     check_mesh_group_added(dev[0])
649     check_mesh_group_added(dev[1])
650     check_mesh_peer_connected(dev[0])
651     check_mesh_peer_connected(dev[1])
652
653     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
654     if ev is None:
655         raise Exception("dev2 did not report auth failure (1)")
656     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
657     if ev is None:
658         raise Exception("dev2 did not report auth failure (2)")
659
660     count = 0
661     ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
662     if ev is None:
663         logger.info("dev0 did not report auth failure")
664     else:
665         if "addr=" + dev[2].own_addr() not in ev:
666             raise Exception("Unexpected peer address in dev0 event: " + ev)
667         count += 1
668
669     ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
670     if ev is None:
671         logger.info("dev1 did not report auth failure")
672     else:
673         if "addr=" + dev[2].own_addr() not in ev:
674             raise Exception("Unexpected peer address in dev1 event: " + ev)
675         count += 1
676
677     hwsim_utils.test_connectivity(dev[0], dev[1])
678
679     for i in range(2):
680         try:
681             hwsim_utils.test_connectivity(dev[i], dev[2], timeout=1)
682             raise Exception("Data connectivity test passed unexpectedly")
683         except Exception, e:
684             if "data delivery failed" not in str(e):
685                 raise
686
687     if count == 0:
688         raise Exception("Neither dev0 nor dev1 reported auth failure")
689
690 def test_wpas_mesh_password_mismatch_retry(dev, apdev, params):
691     """Mesh password mismatch and retry [long]"""
692     if not params['long']:
693         raise HwsimSkip("Skip test case with long duration due to --long not specified")
694     check_mesh_support(dev[0], secure=True)
695     dev[0].request("SET sae_groups ")
696     id = add_mesh_secure_net(dev[0])
697     dev[0].mesh_group_add(id)
698
699     dev[1].request("SET sae_groups ")
700     id = add_mesh_secure_net(dev[1])
701     dev[1].set_network_quoted(id, "psk", "wrong password")
702     dev[1].mesh_group_add(id)
703
704     # Check for mesh joined
705     check_mesh_group_added(dev[0])
706     check_mesh_group_added(dev[1])
707
708     for i in range(4):
709         ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
710         if ev is None:
711             raise Exception("dev0 did not report auth failure (%d)" % i)
712         ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
713         if ev is None:
714             raise Exception("dev1 did not report auth failure (%d)" % i)
715
716     ev = dev[0].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
717     if ev is None:
718         raise Exception("dev0 did not report auth blocked")
719     ev = dev[1].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
720     if ev is None:
721         raise Exception("dev1 did not report auth blocked")
722
723 def test_mesh_wpa_auth_init_oom(dev, apdev):
724     """Secure mesh network setup failing due to wpa_init() OOM"""
725     check_mesh_support(dev[0], secure=True)
726     dev[0].request("SET sae_groups ")
727     with alloc_fail(dev[0], 1, "wpa_init"):
728         id = add_mesh_secure_net(dev[0])
729         dev[0].mesh_group_add(id)
730         ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.2)
731         if ev is not None:
732             raise Exception("Unexpected mesh group start during OOM")
733
734 def test_mesh_wpa_init_fail(dev, apdev):
735     """Secure mesh network setup local failure"""
736     check_mesh_support(dev[0], secure=True)
737     dev[0].request("SET sae_groups ")
738
739     with fail_test(dev[0], 1, "os_get_random;=__mesh_rsn_auth_init"):
740         id = add_mesh_secure_net(dev[0])
741         dev[0].mesh_group_add(id)
742         wait_fail_trigger(dev[0], "GET_FAIL")
743
744     with alloc_fail(dev[0], 1, "mesh_rsn_auth_init"):
745         id = add_mesh_secure_net(dev[0])
746         dev[0].mesh_group_add(id)
747         wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
748
749 def test_wpas_mesh_reconnect(dev, apdev):
750     """Secure mesh network plink counting during reconnection"""
751     check_mesh_support(dev[0])
752     try:
753         _test_wpas_mesh_reconnect(dev)
754     finally:
755         dev[0].request("SET max_peer_links 99")
756
757 def _test_wpas_mesh_reconnect(dev):
758     dev[0].request("SET max_peer_links 2")
759     dev[0].request("SET sae_groups ")
760     id = add_mesh_secure_net(dev[0])
761     dev[0].set_network(id, "beacon_int", "100")
762     dev[0].mesh_group_add(id)
763     dev[1].request("SET sae_groups ")
764     id = add_mesh_secure_net(dev[1])
765     dev[1].mesh_group_add(id)
766     check_mesh_group_added(dev[0])
767     check_mesh_group_added(dev[1])
768     check_mesh_peer_connected(dev[0])
769     check_mesh_peer_connected(dev[1])
770
771     for i in range(3):
772         # Drop incoming management frames to avoid handling link close
773         dev[0].request("SET ext_mgmt_frame_handling 1")
774         dev[1].mesh_group_remove()
775         check_mesh_group_removed(dev[1])
776         dev[1].request("FLUSH")
777         dev[0].request("SET ext_mgmt_frame_handling 0")
778         id = add_mesh_secure_net(dev[1])
779         dev[1].mesh_group_add(id)
780         check_mesh_group_added(dev[1])
781         check_mesh_peer_connected(dev[1])
782         dev[0].dump_monitor()
783         dev[1].dump_monitor()
784
785 def test_wpas_mesh_gate_forwarding(dev, apdev, p):
786     """Mesh forwards traffic to unknown sta to mesh gates"""
787     addr0 = dev[0].own_addr()
788     addr1 = dev[1].own_addr()
789     addr2 = dev[2].own_addr()
790     external_sta = '02:11:22:33:44:55'
791
792     # start 3 node connected mesh
793     check_mesh_support(dev[0])
794     for i in range(3):
795         add_open_mesh_network(dev[i])
796         check_mesh_group_added(dev[i])
797     for i in range(3):
798         check_mesh_peer_connected(dev[i])
799
800     hwsim_utils.test_connectivity(dev[0], dev[1])
801     hwsim_utils.test_connectivity(dev[1], dev[2])
802     hwsim_utils.test_connectivity(dev[0], dev[2])
803
804     # dev0 and dev1 are mesh gates
805     subprocess.call(['iw', 'dev', dev[0].ifname, 'set', 'mesh_param',
806                      'mesh_gate_announcements=1'])
807     subprocess.call(['iw', 'dev', dev[1].ifname, 'set', 'mesh_param',
808                      'mesh_gate_announcements=1'])
809
810     # wait for gate announcement frames
811     time.sleep(1)
812
813     # data frame from dev2 -> external sta should be sent to both gates
814     dev[2].request("DATA_TEST_CONFIG 1")
815     dev[2].request("DATA_TEST_TX {} {} 0".format(external_sta, addr2))
816     dev[2].request("DATA_TEST_CONFIG 0")
817
818     capfile = os.path.join(p['logdir'], "hwsim0.pcapng")
819     filt = "wlan.sa==%s && wlan_mgt.fixed.mesh_addr5==%s" % (addr2,
820                                                              external_sta)
821     for i in range(15):
822         da = run_tshark(capfile, filt, [ "wlan.da" ])
823         if addr0 in da and addr1 in da:
824             logger.debug("Frames seen in tshark iteration %d" % i)
825             break
826         time.sleep(0.3)
827
828     if addr0 not in da:
829         raise Exception("Frame to gate %s not observed" % addr0)
830     if addr1 not in da:
831         raise Exception("Frame to gate %s not observed" % addr1)
832
833 def test_wpas_mesh_pmksa_caching(dev, apdev):
834     """Secure mesh network and PMKSA caching"""
835     check_mesh_support(dev[0], secure=True)
836     dev[0].request("SET sae_groups ")
837     id = add_mesh_secure_net(dev[0])
838     dev[0].mesh_group_add(id)
839
840     dev[1].request("SET sae_groups ")
841     id = add_mesh_secure_net(dev[1])
842     dev[1].mesh_group_add(id)
843
844     # Check for mesh joined
845     check_mesh_group_added(dev[0])
846     check_mesh_group_added(dev[1])
847
848     # Check for peer connected
849     check_mesh_peer_connected(dev[0])
850     check_mesh_peer_connected(dev[1])
851
852     addr0 = dev[0].own_addr()
853     addr1 = dev[1].own_addr()
854     pmksa0 = dev[0].get_pmksa(addr1)
855     pmksa1 = dev[1].get_pmksa(addr0)
856     if pmksa0 is None or pmksa1 is None:
857         raise Exception("No PMKSA cache entry created")
858     if pmksa0['pmkid'] != pmksa1['pmkid']:
859         raise Exception("PMKID mismatch in PMKSA cache entries")
860
861     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
862         raise Exception("Failed to remove peer")
863     pmksa0b = dev[0].get_pmksa(addr1)
864     if pmksa0b is None:
865         raise Exception("PMKSA cache entry not maintained")
866     time.sleep(0.1)
867
868     if "FAIL" not in dev[0].request("MESH_PEER_ADD " + addr1):
869         raise Exception("MESH_PEER_ADD unexpectedly succeeded in no_auto_peer=0 case")
870
871 def test_wpas_mesh_pmksa_caching2(dev, apdev):
872     """Secure mesh network and PMKSA caching with no_auto_peer=1"""
873     check_mesh_support(dev[0], secure=True)
874     addr0 = dev[0].own_addr()
875     addr1 = dev[1].own_addr()
876     dev[0].request("SET sae_groups ")
877     id = add_mesh_secure_net(dev[0])
878     dev[0].set_network(id, "no_auto_peer", "1")
879     dev[0].mesh_group_add(id)
880
881     dev[1].request("SET sae_groups ")
882     id = add_mesh_secure_net(dev[1])
883     dev[1].set_network(id, "no_auto_peer", "1")
884     dev[1].mesh_group_add(id)
885
886     # Check for mesh joined
887     check_mesh_group_added(dev[0])
888     check_mesh_group_added(dev[1])
889
890     # Check for peer connected
891     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
892     if ev is None:
893         raise Exception("Missing no-initiate message")
894     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
895         raise Exception("MESH_PEER_ADD failed")
896     check_mesh_peer_connected(dev[0])
897     check_mesh_peer_connected(dev[1])
898
899     pmksa0 = dev[0].get_pmksa(addr1)
900     pmksa1 = dev[1].get_pmksa(addr0)
901     if pmksa0 is None or pmksa1 is None:
902         raise Exception("No PMKSA cache entry created")
903     if pmksa0['pmkid'] != pmksa1['pmkid']:
904         raise Exception("PMKID mismatch in PMKSA cache entries")
905
906     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
907         raise Exception("Failed to remove peer")
908     pmksa0b = dev[0].get_pmksa(addr1)
909     if pmksa0b is None:
910         raise Exception("PMKSA cache entry not maintained")
911
912     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
913     if ev is None:
914         raise Exception("Missing no-initiate message (2)")
915     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
916         raise Exception("MESH_PEER_ADD failed (2)")
917     check_mesh_peer_connected(dev[0])
918     check_mesh_peer_connected(dev[1])
919
920     pmksa0c = dev[0].get_pmksa(addr1)
921     pmksa1c = dev[1].get_pmksa(addr0)
922     if pmksa0c is None or pmksa1c is None:
923         raise Exception("No PMKSA cache entry created (2)")
924     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
925         raise Exception("PMKID mismatch in PMKSA cache entries")
926     if pmksa0['pmkid'] != pmksa0c['pmkid']:
927         raise Exception("PMKID changed")
928
929     hwsim_utils.test_connectivity(dev[0], dev[1])
930
931 def test_wpas_mesh_pmksa_caching_no_match(dev, apdev):
932     """Secure mesh network and PMKSA caching with no PMKID match"""
933     check_mesh_support(dev[0], secure=True)
934     addr0 = dev[0].own_addr()
935     addr1 = dev[1].own_addr()
936     dev[0].request("SET sae_groups ")
937     id = add_mesh_secure_net(dev[0])
938     dev[0].set_network(id, "no_auto_peer", "1")
939     dev[0].mesh_group_add(id)
940
941     dev[1].request("SET sae_groups ")
942     id = add_mesh_secure_net(dev[1])
943     dev[1].set_network(id, "no_auto_peer", "1")
944     dev[1].mesh_group_add(id)
945
946     # Check for mesh joined
947     check_mesh_group_added(dev[0])
948     check_mesh_group_added(dev[1])
949
950     # Check for peer connected
951     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
952     if ev is None:
953         raise Exception("Missing no-initiate message")
954     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
955         raise Exception("MESH_PEER_ADD failed")
956     check_mesh_peer_connected(dev[0])
957     check_mesh_peer_connected(dev[1])
958
959     pmksa0 = dev[0].get_pmksa(addr1)
960     pmksa1 = dev[1].get_pmksa(addr0)
961     if pmksa0 is None or pmksa1 is None:
962         raise Exception("No PMKSA cache entry created")
963     if pmksa0['pmkid'] != pmksa1['pmkid']:
964         raise Exception("PMKID mismatch in PMKSA cache entries")
965
966     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
967         raise Exception("Failed to remove peer")
968
969     if "OK" not in dev[1].request("PMKSA_FLUSH"):
970         raise Exception("Failed to flush PMKSA cache")
971
972     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
973     if ev is None:
974         raise Exception("Missing no-initiate message (2)")
975     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
976         raise Exception("MESH_PEER_ADD failed (2)")
977     check_mesh_peer_connected(dev[0])
978     check_mesh_peer_connected(dev[1])
979
980     pmksa0c = dev[0].get_pmksa(addr1)
981     pmksa1c = dev[1].get_pmksa(addr0)
982     if pmksa0c is None or pmksa1c is None:
983         raise Exception("No PMKSA cache entry created (2)")
984     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
985         raise Exception("PMKID mismatch in PMKSA cache entries")
986     if pmksa0['pmkid'] == pmksa0c['pmkid']:
987         raise Exception("PMKID did not change")
988
989     hwsim_utils.test_connectivity(dev[0], dev[1])
990
991 def test_mesh_oom(dev, apdev):
992     """Mesh network setup failing due to OOM"""
993     check_mesh_support(dev[0], secure=True)
994     dev[0].request("SET sae_groups ")
995
996     with alloc_fail(dev[0], 1, "mesh_config_create"):
997         add_open_mesh_network(dev[0])
998         ev = dev[0].wait_event(["Failed to init mesh"])
999         if ev is None:
1000             raise Exception("Init failure not reported")
1001
1002     for i in range(1, 65):
1003         with alloc_fail(dev[0], i, "wpa_supplicant_mesh_init"):
1004             add_open_mesh_network(dev[0])
1005             wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
1006             ev = dev[0].wait_event(["Failed to init mesh",
1007                                     "MESH-GROUP-STARTED"])
1008             if ev is None:
1009                 raise Exception("Init failure not reported")
1010
1011 def test_mesh_add_interface_oom(dev):
1012     """wpa_supplicant mesh with dynamic interface addition failing"""
1013     check_mesh_support(dev[0])
1014     for i in range(1, 3):
1015         mesh = None
1016         try:
1017             with alloc_fail(dev[0], i, "wpas_mesh_add_interface"):
1018                 mesh = dev[0].request("MESH_INTERFACE_ADD").strip()
1019         finally:
1020             if mesh and mesh != "FAIL":
1021                 dev[0].request("MESH_GROUP_REMOVE " + mesh)
1022
1023 def test_mesh_scan_oom(dev):
1024     """wpa_supplicant mesh scan results and OOM"""
1025     check_mesh_support(dev[0])
1026     add_open_mesh_network(dev[0])
1027     check_mesh_group_added(dev[0])
1028     for i in range(5):
1029         dev[1].scan(freq="2412")
1030         res = dev[1].request("SCAN_RESULTS")
1031         if "[MESH]" in res:
1032             break
1033     for r in res.splitlines():
1034         if "[MESH]" in r:
1035             break
1036     bssid = r.split('\t')[0]
1037
1038     bss = dev[1].get_bss(bssid)
1039     if bss is None:
1040         raise Exception("Could not get BSS entry for mesh")
1041
1042     for i in range(1, 3):
1043         with alloc_fail(dev[1], i, "mesh_attr_text"):
1044             bss = dev[1].get_bss(bssid)
1045             if bss is not None:
1046                 raise Exception("Unexpected BSS result during OOM")
1047
1048 def test_mesh_sae_groups_invalid(dev, apdev):
1049     """Mesh with invalid SAE group configuration"""
1050     check_mesh_support(dev[0], secure=True)
1051
1052     dev[0].request("SET sae_groups 25")
1053     id = add_mesh_secure_net(dev[0])
1054     dev[0].mesh_group_add(id)
1055
1056     dev[1].request("SET sae_groups 123 122 121")
1057     id = add_mesh_secure_net(dev[1])
1058     dev[1].mesh_group_add(id)
1059
1060     check_mesh_group_added(dev[0])
1061     check_mesh_group_added(dev[1])
1062
1063     ev = dev[0].wait_event(["new peer notification"], timeout=10)
1064     if ev is None:
1065         raise Exception("dev[0] did not see peer")
1066     ev = dev[1].wait_event(["new peer notification"], timeout=10)
1067     if ev is None:
1068         raise Exception("dev[1] did not see peer")
1069
1070     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
1071     if ev is not None:
1072         raise Exception("Unexpected connection(0)")
1073
1074     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.01)
1075     if ev is not None:
1076         raise Exception("Unexpected connection(1)")
1077
1078     dev[0].request("SET sae_groups ")
1079     dev[1].request("SET sae_groups ")
1080
1081 def test_mesh_sae_failure(dev, apdev):
1082     """Mesh and local SAE failures"""
1083     check_mesh_support(dev[0], secure=True)
1084
1085     dev[0].request("SET sae_groups ")
1086     dev[1].request("SET sae_groups ")
1087
1088     funcs = [ (1, "=mesh_rsn_auth_sae_sta", True),
1089               (1, "mesh_rsn_build_sae_commit;mesh_rsn_auth_sae_sta", False),
1090               (1, "auth_sae_init_committed;mesh_rsn_auth_sae_sta", True),
1091               (1, "=mesh_rsn_protect_frame", True),
1092               (2, "=mesh_rsn_protect_frame", True),
1093               (1, "aes_siv_encrypt;mesh_rsn_protect_frame", True),
1094               (1, "=mesh_rsn_process_ampe", True),
1095               (1, "aes_siv_decrypt;mesh_rsn_process_ampe", True) ]
1096     for count, func, success in funcs:
1097         id = add_mesh_secure_net(dev[0])
1098         dev[0].mesh_group_add(id)
1099
1100         with alloc_fail(dev[1], count, func):
1101             id = add_mesh_secure_net(dev[1])
1102             dev[1].mesh_group_add(id)
1103             check_mesh_group_added(dev[0])
1104             check_mesh_group_added(dev[1])
1105             if success:
1106                 # retry is expected to work
1107                 check_mesh_peer_connected(dev[0])
1108                 check_mesh_peer_connected(dev[1])
1109             else:
1110                 wait_fail_trigger(dev[1], "GET_ALLOC_FAIL")
1111         dev[0].mesh_group_remove()
1112         dev[1].mesh_group_remove()
1113         check_mesh_group_removed(dev[0])
1114         check_mesh_group_removed(dev[1])