tests: Mesh network setup failing due to driver command failure
[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_mesh_open_vht_160(dev, apdev):
633     """Open mesh network on VHT 160 MHz channel"""
634     try:
635         _test_mesh_open_vht_160(dev, apdev)
636     finally:
637         subprocess.call(['iw', 'reg', 'set', '00'])
638         dev[0].flush_scan_cache()
639         dev[1].flush_scan_cache()
640
641 def _test_mesh_open_vht_160(dev, apdev):
642     check_mesh_support(dev[0])
643     subprocess.call(['iw', 'reg', 'set', 'ZA'])
644     for i in range(2):
645         for j in range(5):
646             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
647             if ev is None:
648                 raise Exception("No regdom change event")
649             if "alpha2=ZA" in ev:
650                 break
651
652         cmd = subprocess.Popen(["iw", "reg", "get"], stdout=subprocess.PIPE)
653         reg = cmd.stdout.read()
654         if "@ 160)" not in reg:
655             raise HwsimSkip("160 MHz channel not supported in regulatory information")
656
657         add_open_mesh_network(dev[i], freq="5520", chwidth=2)
658
659     # Check for mesh joined
660     check_mesh_group_added(dev[0])
661     check_mesh_group_added(dev[1])
662
663     # Check for peer connected
664     check_mesh_peer_connected(dev[0])
665     check_mesh_peer_connected(dev[1])
666
667     # Test connectivity 0->1 and 1->0
668     hwsim_utils.test_connectivity(dev[0], dev[1])
669
670     sig = dev[0].request("SIGNAL_POLL").splitlines()
671     if "WIDTH=160 MHz" not in sig:
672         raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
673     if "FREQUENCY=5520" not in sig:
674         raise Exception("Unexpected SIGNAL_POLL value(3): " + str(sig))
675
676     sig = dev[1].request("SIGNAL_POLL").splitlines()
677     if "WIDTH=160 MHz" not in sig:
678         raise Exception("Unexpected SIGNAL_POLL value(2b): " + str(sig))
679     if "FREQUENCY=5520" not in sig:
680         raise Exception("Unexpected SIGNAL_POLL value(3b): " + str(sig))
681
682 def test_wpas_mesh_password_mismatch(dev, apdev):
683     """Mesh network and one device with mismatching password"""
684     check_mesh_support(dev[0], secure=True)
685     dev[0].request("SET sae_groups ")
686     id = add_mesh_secure_net(dev[0])
687     dev[0].mesh_group_add(id)
688
689     dev[1].request("SET sae_groups ")
690     id = add_mesh_secure_net(dev[1])
691     dev[1].mesh_group_add(id)
692
693     dev[2].request("SET sae_groups ")
694     id = add_mesh_secure_net(dev[2])
695     dev[2].set_network_quoted(id, "psk", "wrong password")
696     dev[2].mesh_group_add(id)
697
698     # The two peers with matching password need to be able to connect
699     check_mesh_group_added(dev[0])
700     check_mesh_group_added(dev[1])
701     check_mesh_peer_connected(dev[0])
702     check_mesh_peer_connected(dev[1])
703
704     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
705     if ev is None:
706         raise Exception("dev2 did not report auth failure (1)")
707     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
708     if ev is None:
709         raise Exception("dev2 did not report auth failure (2)")
710
711     count = 0
712     ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
713     if ev is None:
714         logger.info("dev0 did not report auth failure")
715     else:
716         if "addr=" + dev[2].own_addr() not in ev:
717             raise Exception("Unexpected peer address in dev0 event: " + ev)
718         count += 1
719
720     ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
721     if ev is None:
722         logger.info("dev1 did not report auth failure")
723     else:
724         if "addr=" + dev[2].own_addr() not in ev:
725             raise Exception("Unexpected peer address in dev1 event: " + ev)
726         count += 1
727
728     hwsim_utils.test_connectivity(dev[0], dev[1])
729
730     for i in range(2):
731         try:
732             hwsim_utils.test_connectivity(dev[i], dev[2], timeout=1)
733             raise Exception("Data connectivity test passed unexpectedly")
734         except Exception, e:
735             if "data delivery failed" not in str(e):
736                 raise
737
738     if count == 0:
739         raise Exception("Neither dev0 nor dev1 reported auth failure")
740
741 def test_wpas_mesh_password_mismatch_retry(dev, apdev, params):
742     """Mesh password mismatch and retry [long]"""
743     if not params['long']:
744         raise HwsimSkip("Skip test case with long duration due to --long not specified")
745     check_mesh_support(dev[0], secure=True)
746     dev[0].request("SET sae_groups ")
747     id = add_mesh_secure_net(dev[0])
748     dev[0].mesh_group_add(id)
749
750     dev[1].request("SET sae_groups ")
751     id = add_mesh_secure_net(dev[1])
752     dev[1].set_network_quoted(id, "psk", "wrong password")
753     dev[1].mesh_group_add(id)
754
755     # Check for mesh joined
756     check_mesh_group_added(dev[0])
757     check_mesh_group_added(dev[1])
758
759     for i in range(4):
760         ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
761         if ev is None:
762             raise Exception("dev0 did not report auth failure (%d)" % i)
763         ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
764         if ev is None:
765             raise Exception("dev1 did not report auth failure (%d)" % i)
766
767     ev = dev[0].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
768     if ev is None:
769         raise Exception("dev0 did not report auth blocked")
770     ev = dev[1].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
771     if ev is None:
772         raise Exception("dev1 did not report auth blocked")
773
774 def test_mesh_wpa_auth_init_oom(dev, apdev):
775     """Secure mesh network setup failing due to wpa_init() OOM"""
776     check_mesh_support(dev[0], secure=True)
777     dev[0].request("SET sae_groups ")
778     with alloc_fail(dev[0], 1, "wpa_init"):
779         id = add_mesh_secure_net(dev[0])
780         dev[0].mesh_group_add(id)
781         ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.2)
782         if ev is not None:
783             raise Exception("Unexpected mesh group start during OOM")
784
785 def test_mesh_wpa_init_fail(dev, apdev):
786     """Secure mesh network setup local failure"""
787     check_mesh_support(dev[0], secure=True)
788     dev[0].request("SET sae_groups ")
789
790     with fail_test(dev[0], 1, "os_get_random;=__mesh_rsn_auth_init"):
791         id = add_mesh_secure_net(dev[0])
792         dev[0].mesh_group_add(id)
793         wait_fail_trigger(dev[0], "GET_FAIL")
794
795     with alloc_fail(dev[0], 1, "mesh_rsn_auth_init"):
796         id = add_mesh_secure_net(dev[0])
797         dev[0].mesh_group_add(id)
798         wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
799
800 def test_wpas_mesh_reconnect(dev, apdev):
801     """Secure mesh network plink counting during reconnection"""
802     check_mesh_support(dev[0])
803     try:
804         _test_wpas_mesh_reconnect(dev)
805     finally:
806         dev[0].request("SET max_peer_links 99")
807
808 def _test_wpas_mesh_reconnect(dev):
809     dev[0].request("SET max_peer_links 2")
810     dev[0].request("SET sae_groups ")
811     id = add_mesh_secure_net(dev[0])
812     dev[0].set_network(id, "beacon_int", "100")
813     dev[0].mesh_group_add(id)
814     dev[1].request("SET sae_groups ")
815     id = add_mesh_secure_net(dev[1])
816     dev[1].mesh_group_add(id)
817     check_mesh_group_added(dev[0])
818     check_mesh_group_added(dev[1])
819     check_mesh_peer_connected(dev[0])
820     check_mesh_peer_connected(dev[1])
821
822     for i in range(3):
823         # Drop incoming management frames to avoid handling link close
824         dev[0].request("SET ext_mgmt_frame_handling 1")
825         dev[1].mesh_group_remove()
826         check_mesh_group_removed(dev[1])
827         dev[1].request("FLUSH")
828         dev[0].request("SET ext_mgmt_frame_handling 0")
829         id = add_mesh_secure_net(dev[1])
830         dev[1].mesh_group_add(id)
831         check_mesh_group_added(dev[1])
832         check_mesh_peer_connected(dev[1])
833         dev[0].dump_monitor()
834         dev[1].dump_monitor()
835
836 def test_wpas_mesh_gate_forwarding(dev, apdev, p):
837     """Mesh forwards traffic to unknown sta to mesh gates"""
838     addr0 = dev[0].own_addr()
839     addr1 = dev[1].own_addr()
840     addr2 = dev[2].own_addr()
841     external_sta = '02:11:22:33:44:55'
842
843     # start 3 node connected mesh
844     check_mesh_support(dev[0])
845     for i in range(3):
846         add_open_mesh_network(dev[i])
847         check_mesh_group_added(dev[i])
848     for i in range(3):
849         check_mesh_peer_connected(dev[i])
850
851     hwsim_utils.test_connectivity(dev[0], dev[1])
852     hwsim_utils.test_connectivity(dev[1], dev[2])
853     hwsim_utils.test_connectivity(dev[0], dev[2])
854
855     # dev0 and dev1 are mesh gates
856     subprocess.call(['iw', 'dev', dev[0].ifname, 'set', 'mesh_param',
857                      'mesh_gate_announcements=1'])
858     subprocess.call(['iw', 'dev', dev[1].ifname, 'set', 'mesh_param',
859                      'mesh_gate_announcements=1'])
860
861     # wait for gate announcement frames
862     time.sleep(1)
863
864     # data frame from dev2 -> external sta should be sent to both gates
865     dev[2].request("DATA_TEST_CONFIG 1")
866     dev[2].request("DATA_TEST_TX {} {} 0".format(external_sta, addr2))
867     dev[2].request("DATA_TEST_CONFIG 0")
868
869     capfile = os.path.join(p['logdir'], "hwsim0.pcapng")
870     filt = "wlan.sa==%s && wlan_mgt.fixed.mesh_addr5==%s" % (addr2,
871                                                              external_sta)
872     for i in range(15):
873         da = run_tshark(capfile, filt, [ "wlan.da" ])
874         if addr0 in da and addr1 in da:
875             logger.debug("Frames seen in tshark iteration %d" % i)
876             break
877         time.sleep(0.3)
878
879     if addr0 not in da:
880         raise Exception("Frame to gate %s not observed" % addr0)
881     if addr1 not in da:
882         raise Exception("Frame to gate %s not observed" % addr1)
883
884 def test_wpas_mesh_pmksa_caching(dev, apdev):
885     """Secure mesh network and PMKSA caching"""
886     check_mesh_support(dev[0], secure=True)
887     dev[0].request("SET sae_groups ")
888     id = add_mesh_secure_net(dev[0])
889     dev[0].mesh_group_add(id)
890
891     dev[1].request("SET sae_groups ")
892     id = add_mesh_secure_net(dev[1])
893     dev[1].mesh_group_add(id)
894
895     # Check for mesh joined
896     check_mesh_group_added(dev[0])
897     check_mesh_group_added(dev[1])
898
899     # Check for peer connected
900     check_mesh_peer_connected(dev[0])
901     check_mesh_peer_connected(dev[1])
902
903     addr0 = dev[0].own_addr()
904     addr1 = dev[1].own_addr()
905     pmksa0 = dev[0].get_pmksa(addr1)
906     pmksa1 = dev[1].get_pmksa(addr0)
907     if pmksa0 is None or pmksa1 is None:
908         raise Exception("No PMKSA cache entry created")
909     if pmksa0['pmkid'] != pmksa1['pmkid']:
910         raise Exception("PMKID mismatch in PMKSA cache entries")
911
912     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
913         raise Exception("Failed to remove peer")
914     pmksa0b = dev[0].get_pmksa(addr1)
915     if pmksa0b is None:
916         raise Exception("PMKSA cache entry not maintained")
917     time.sleep(0.1)
918
919     if "FAIL" not in dev[0].request("MESH_PEER_ADD " + addr1):
920         raise Exception("MESH_PEER_ADD unexpectedly succeeded in no_auto_peer=0 case")
921
922 def test_wpas_mesh_pmksa_caching2(dev, apdev):
923     """Secure mesh network and PMKSA caching with no_auto_peer=1"""
924     check_mesh_support(dev[0], secure=True)
925     addr0 = dev[0].own_addr()
926     addr1 = dev[1].own_addr()
927     dev[0].request("SET sae_groups ")
928     id = add_mesh_secure_net(dev[0])
929     dev[0].set_network(id, "no_auto_peer", "1")
930     dev[0].mesh_group_add(id)
931
932     dev[1].request("SET sae_groups ")
933     id = add_mesh_secure_net(dev[1])
934     dev[1].set_network(id, "no_auto_peer", "1")
935     dev[1].mesh_group_add(id)
936
937     # Check for mesh joined
938     check_mesh_group_added(dev[0])
939     check_mesh_group_added(dev[1])
940
941     # Check for peer connected
942     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
943     if ev is None:
944         raise Exception("Missing no-initiate message")
945     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
946         raise Exception("MESH_PEER_ADD failed")
947     check_mesh_peer_connected(dev[0])
948     check_mesh_peer_connected(dev[1])
949
950     pmksa0 = dev[0].get_pmksa(addr1)
951     pmksa1 = dev[1].get_pmksa(addr0)
952     if pmksa0 is None or pmksa1 is None:
953         raise Exception("No PMKSA cache entry created")
954     if pmksa0['pmkid'] != pmksa1['pmkid']:
955         raise Exception("PMKID mismatch in PMKSA cache entries")
956
957     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
958         raise Exception("Failed to remove peer")
959     pmksa0b = dev[0].get_pmksa(addr1)
960     if pmksa0b is None:
961         raise Exception("PMKSA cache entry not maintained")
962
963     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
964     if ev is None:
965         raise Exception("Missing no-initiate message (2)")
966     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
967         raise Exception("MESH_PEER_ADD failed (2)")
968     check_mesh_peer_connected(dev[0])
969     check_mesh_peer_connected(dev[1])
970
971     pmksa0c = dev[0].get_pmksa(addr1)
972     pmksa1c = dev[1].get_pmksa(addr0)
973     if pmksa0c is None or pmksa1c is None:
974         raise Exception("No PMKSA cache entry created (2)")
975     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
976         raise Exception("PMKID mismatch in PMKSA cache entries")
977     if pmksa0['pmkid'] != pmksa0c['pmkid']:
978         raise Exception("PMKID changed")
979
980     hwsim_utils.test_connectivity(dev[0], dev[1])
981
982 def test_wpas_mesh_pmksa_caching_no_match(dev, apdev):
983     """Secure mesh network and PMKSA caching with no PMKID match"""
984     check_mesh_support(dev[0], secure=True)
985     addr0 = dev[0].own_addr()
986     addr1 = dev[1].own_addr()
987     dev[0].request("SET sae_groups ")
988     id = add_mesh_secure_net(dev[0])
989     dev[0].set_network(id, "no_auto_peer", "1")
990     dev[0].mesh_group_add(id)
991
992     dev[1].request("SET sae_groups ")
993     id = add_mesh_secure_net(dev[1])
994     dev[1].set_network(id, "no_auto_peer", "1")
995     dev[1].mesh_group_add(id)
996
997     # Check for mesh joined
998     check_mesh_group_added(dev[0])
999     check_mesh_group_added(dev[1])
1000
1001     # Check for peer connected
1002     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
1003     if ev is None:
1004         raise Exception("Missing no-initiate message")
1005     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
1006         raise Exception("MESH_PEER_ADD failed")
1007     check_mesh_peer_connected(dev[0])
1008     check_mesh_peer_connected(dev[1])
1009
1010     pmksa0 = dev[0].get_pmksa(addr1)
1011     pmksa1 = dev[1].get_pmksa(addr0)
1012     if pmksa0 is None or pmksa1 is None:
1013         raise Exception("No PMKSA cache entry created")
1014     if pmksa0['pmkid'] != pmksa1['pmkid']:
1015         raise Exception("PMKID mismatch in PMKSA cache entries")
1016
1017     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
1018         raise Exception("Failed to remove peer")
1019
1020     if "OK" not in dev[1].request("PMKSA_FLUSH"):
1021         raise Exception("Failed to flush PMKSA cache")
1022
1023     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
1024     if ev is None:
1025         raise Exception("Missing no-initiate message (2)")
1026     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
1027         raise Exception("MESH_PEER_ADD failed (2)")
1028     check_mesh_peer_connected(dev[0])
1029     check_mesh_peer_connected(dev[1])
1030
1031     pmksa0c = dev[0].get_pmksa(addr1)
1032     pmksa1c = dev[1].get_pmksa(addr0)
1033     if pmksa0c is None or pmksa1c is None:
1034         raise Exception("No PMKSA cache entry created (2)")
1035     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
1036         raise Exception("PMKID mismatch in PMKSA cache entries")
1037     if pmksa0['pmkid'] == pmksa0c['pmkid']:
1038         raise Exception("PMKID did not change")
1039
1040     hwsim_utils.test_connectivity(dev[0], dev[1])
1041
1042 def test_mesh_oom(dev, apdev):
1043     """Mesh network setup failing due to OOM"""
1044     check_mesh_support(dev[0], secure=True)
1045     dev[0].request("SET sae_groups ")
1046
1047     with alloc_fail(dev[0], 1, "mesh_config_create"):
1048         add_open_mesh_network(dev[0])
1049         ev = dev[0].wait_event(["Failed to init mesh"])
1050         if ev is None:
1051             raise Exception("Init failure not reported")
1052
1053     with alloc_fail(dev[0], 4, "=wpa_supplicant_mesh_init"):
1054         add_open_mesh_network(dev[0], basic_rates="60 120 240")
1055         ev = dev[0].wait_event(["Failed to init mesh"])
1056         if ev is None:
1057             raise Exception("Init failure not reported")
1058
1059     for i in range(1, 66):
1060         dev[0].dump_monitor()
1061         logger.info("Test instance %d" % i)
1062         try:
1063             with alloc_fail(dev[0], i, "wpa_supplicant_mesh_init"):
1064                 add_open_mesh_network(dev[0])
1065                 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
1066                 ev = dev[0].wait_event(["Failed to init mesh",
1067                                         "MESH-GROUP-STARTED"])
1068                 if ev is None:
1069                     raise Exception("Init failure not reported")
1070         except Exception, e:
1071             if i < 15:
1072                 raise
1073             logger.info("Ignore no-oom for i=%d" % i)
1074
1075     with alloc_fail(dev[0], 5, "=wpa_supplicant_mesh_init"):
1076         id = add_mesh_secure_net(dev[0])
1077         dev[0].mesh_group_add(id)
1078         ev = dev[0].wait_event(["Failed to init mesh"])
1079         if ev is None:
1080             raise Exception("Init failure not reported")
1081
1082 def test_mesh_add_interface_oom(dev):
1083     """wpa_supplicant mesh with dynamic interface addition failing"""
1084     check_mesh_support(dev[0])
1085     for i in range(1, 3):
1086         mesh = None
1087         try:
1088             with alloc_fail(dev[0], i, "wpas_mesh_add_interface"):
1089                 mesh = dev[0].request("MESH_INTERFACE_ADD").strip()
1090         finally:
1091             if mesh and mesh != "FAIL":
1092                 dev[0].request("MESH_GROUP_REMOVE " + mesh)
1093
1094 def test_mesh_scan_oom(dev):
1095     """wpa_supplicant mesh scan results and OOM"""
1096     check_mesh_support(dev[0])
1097     add_open_mesh_network(dev[0])
1098     check_mesh_group_added(dev[0])
1099     for i in range(5):
1100         dev[1].scan(freq="2412")
1101         res = dev[1].request("SCAN_RESULTS")
1102         if "[MESH]" in res:
1103             break
1104     for r in res.splitlines():
1105         if "[MESH]" in r:
1106             break
1107     bssid = r.split('\t')[0]
1108
1109     bss = dev[1].get_bss(bssid)
1110     if bss is None:
1111         raise Exception("Could not get BSS entry for mesh")
1112
1113     for i in range(1, 3):
1114         with alloc_fail(dev[1], i, "mesh_attr_text"):
1115             bss = dev[1].get_bss(bssid)
1116             if bss is not None:
1117                 raise Exception("Unexpected BSS result during OOM")
1118
1119 def test_mesh_drv_fail(dev, apdev):
1120     """Mesh network setup failing due to driver command failure"""
1121     check_mesh_support(dev[0], secure=True)
1122     dev[0].request("SET sae_groups ")
1123
1124     with fail_test(dev[0], 1, "nl80211_join_mesh"):
1125         add_open_mesh_network(dev[0])
1126         ev = dev[0].wait_event(["mesh join error"])
1127         if ev is None:
1128             raise Exception("Join failure not reported")
1129
1130     dev[0].dump_monitor()
1131     with fail_test(dev[0], 1, "wpa_driver_nl80211_if_add"):
1132         if "FAIL" not in dev[0].request("MESH_INTERFACE_ADD").strip():
1133             raise Exception("Interface added unexpectedly")
1134
1135     dev[0].dump_monitor()
1136     with fail_test(dev[0], 1, "wpa_driver_nl80211_init_mesh"):
1137         add_open_mesh_network(dev[0])
1138         ev = dev[0].wait_event(["Could not join mesh"])
1139         if ev is None:
1140             raise Exception("Join failure not reported")
1141
1142 def test_mesh_sae_groups_invalid(dev, apdev):
1143     """Mesh with invalid SAE group configuration"""
1144     check_mesh_support(dev[0], secure=True)
1145
1146     dev[0].request("SET sae_groups 25")
1147     id = add_mesh_secure_net(dev[0])
1148     dev[0].mesh_group_add(id)
1149
1150     dev[1].request("SET sae_groups 123 122 121")
1151     id = add_mesh_secure_net(dev[1])
1152     dev[1].mesh_group_add(id)
1153
1154     check_mesh_group_added(dev[0])
1155     check_mesh_group_added(dev[1])
1156
1157     ev = dev[0].wait_event(["new peer notification"], timeout=10)
1158     if ev is None:
1159         raise Exception("dev[0] did not see peer")
1160     ev = dev[1].wait_event(["new peer notification"], timeout=10)
1161     if ev is None:
1162         raise Exception("dev[1] did not see peer")
1163
1164     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
1165     if ev is not None:
1166         raise Exception("Unexpected connection(0)")
1167
1168     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.01)
1169     if ev is not None:
1170         raise Exception("Unexpected connection(1)")
1171
1172     dev[0].request("SET sae_groups ")
1173     dev[1].request("SET sae_groups ")
1174
1175 def test_mesh_sae_failure(dev, apdev):
1176     """Mesh and local SAE failures"""
1177     check_mesh_support(dev[0], secure=True)
1178
1179     dev[0].request("SET sae_groups ")
1180     dev[1].request("SET sae_groups ")
1181
1182     funcs = [ (1, "=mesh_rsn_auth_sae_sta", True),
1183               (1, "mesh_rsn_build_sae_commit;mesh_rsn_auth_sae_sta", False),
1184               (1, "auth_sae_init_committed;mesh_rsn_auth_sae_sta", True),
1185               (1, "=mesh_rsn_protect_frame", True),
1186               (2, "=mesh_rsn_protect_frame", True),
1187               (1, "aes_siv_encrypt;mesh_rsn_protect_frame", True),
1188               (1, "=mesh_rsn_process_ampe", True),
1189               (1, "aes_siv_decrypt;mesh_rsn_process_ampe", True) ]
1190     for count, func, success in funcs:
1191         id = add_mesh_secure_net(dev[0])
1192         dev[0].mesh_group_add(id)
1193
1194         with alloc_fail(dev[1], count, func):
1195             id = add_mesh_secure_net(dev[1])
1196             dev[1].mesh_group_add(id)
1197             check_mesh_group_added(dev[0])
1198             check_mesh_group_added(dev[1])
1199             if success:
1200                 # retry is expected to work
1201                 check_mesh_peer_connected(dev[0])
1202                 check_mesh_peer_connected(dev[1])
1203             else:
1204                 wait_fail_trigger(dev[1], "GET_ALLOC_FAIL")
1205         dev[0].mesh_group_remove()
1206         dev[1].mesh_group_remove()
1207         check_mesh_group_removed(dev[0])
1208         check_mesh_group_removed(dev[1])
1209
1210 def test_mesh_failure(dev, apdev):
1211     """Mesh and local failures"""
1212     check_mesh_support(dev[0])
1213
1214     funcs = [ (1, "ap_sta_add;mesh_mpm_add_peer", True),
1215               (1, "wpabuf_alloc;mesh_mpm_send_plink_action", True) ]
1216     for count, func, success in funcs:
1217         add_open_mesh_network(dev[0])
1218
1219         with alloc_fail(dev[1], count, func):
1220             add_open_mesh_network(dev[1])
1221             check_mesh_group_added(dev[0])
1222             check_mesh_group_added(dev[1])
1223             if success:
1224                 # retry is expected to work
1225                 check_mesh_peer_connected(dev[0])
1226                 check_mesh_peer_connected(dev[1])
1227             else:
1228                 wait_fail_trigger(dev[1], "GET_ALLOC_FAIL")
1229         dev[0].mesh_group_remove()
1230         dev[1].mesh_group_remove()
1231         check_mesh_group_removed(dev[0])
1232         check_mesh_group_removed(dev[1])
1233
1234     funcs = [ (1, "mesh_mpm_init_link", True) ]
1235     for count, func, success in funcs:
1236         add_open_mesh_network(dev[0])
1237
1238         with fail_test(dev[1], count, func):
1239             add_open_mesh_network(dev[1])
1240             check_mesh_group_added(dev[0])
1241             check_mesh_group_added(dev[1])
1242             if success:
1243                 # retry is expected to work
1244                 check_mesh_peer_connected(dev[0])
1245                 check_mesh_peer_connected(dev[1])
1246             else:
1247                 wait_fail_trigger(dev[1], "GET_FAIL")
1248         dev[0].mesh_group_remove()
1249         dev[1].mesh_group_remove()
1250         check_mesh_group_removed(dev[0])
1251         check_mesh_group_removed(dev[1])
1252
1253 def test_mesh_invalid_frequency(dev, apdev):
1254     """Mesh and invalid frequency configuration"""
1255     check_mesh_support(dev[0])
1256     add_open_mesh_network(dev[0], freq=None)
1257     ev = dev[0].wait_event(["MESH-GROUP-STARTED",
1258                             "Could not join mesh"])
1259     if ev is None or "Could not join mesh" not in ev:
1260         raise Exception("Mesh join failure not reported")
1261     dev[0].request("REMOVE_NETWORK all")
1262
1263     add_open_mesh_network(dev[0], freq="2413")
1264     ev = dev[0].wait_event(["MESH-GROUP-STARTED",
1265                             "Could not join mesh"])
1266     if ev is None or "Could not join mesh" not in ev:
1267         raise Exception("Mesh join failure not reported")
1268
1269 def test_mesh_default_beacon_int(dev, apdev):
1270     """Mesh and default beacon interval"""
1271     check_mesh_support(dev[0])
1272     try:
1273         dev[0].request("SET beacon_int 200")
1274         add_open_mesh_network(dev[0])
1275         check_mesh_group_added(dev[0])
1276     finally:
1277         dev[0].request("SET beacon_int 0")