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