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