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