tests: Secure mesh network and PMKSA caching failing due to OOM
[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     with alloc_fail(dev[0], 1, "mesh_rsn_auth_init"):
797         id = add_mesh_secure_net(dev[0])
798         dev[0].mesh_group_add(id)
799         wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
800
801 def test_wpas_mesh_reconnect(dev, apdev):
802     """Secure mesh network plink counting during reconnection"""
803     check_mesh_support(dev[0])
804     try:
805         _test_wpas_mesh_reconnect(dev)
806     finally:
807         dev[0].request("SET max_peer_links 99")
808
809 def _test_wpas_mesh_reconnect(dev):
810     dev[0].request("SET max_peer_links 2")
811     dev[0].request("SET sae_groups ")
812     id = add_mesh_secure_net(dev[0])
813     dev[0].set_network(id, "beacon_int", "100")
814     dev[0].mesh_group_add(id)
815     dev[1].request("SET sae_groups ")
816     id = add_mesh_secure_net(dev[1])
817     dev[1].mesh_group_add(id)
818     check_mesh_group_added(dev[0])
819     check_mesh_group_added(dev[1])
820     check_mesh_peer_connected(dev[0])
821     check_mesh_peer_connected(dev[1])
822
823     for i in range(3):
824         # Drop incoming management frames to avoid handling link close
825         dev[0].request("SET ext_mgmt_frame_handling 1")
826         dev[1].mesh_group_remove()
827         check_mesh_group_removed(dev[1])
828         dev[1].request("FLUSH")
829         dev[0].request("SET ext_mgmt_frame_handling 0")
830         id = add_mesh_secure_net(dev[1])
831         dev[1].mesh_group_add(id)
832         check_mesh_group_added(dev[1])
833         check_mesh_peer_connected(dev[1])
834         dev[0].dump_monitor()
835         dev[1].dump_monitor()
836
837 def test_wpas_mesh_gate_forwarding(dev, apdev, p):
838     """Mesh forwards traffic to unknown sta to mesh gates"""
839     addr0 = dev[0].own_addr()
840     addr1 = dev[1].own_addr()
841     addr2 = dev[2].own_addr()
842     external_sta = '02:11:22:33:44:55'
843
844     # start 3 node connected mesh
845     check_mesh_support(dev[0])
846     for i in range(3):
847         add_open_mesh_network(dev[i])
848         check_mesh_group_added(dev[i])
849     for i in range(3):
850         check_mesh_peer_connected(dev[i])
851
852     hwsim_utils.test_connectivity(dev[0], dev[1])
853     hwsim_utils.test_connectivity(dev[1], dev[2])
854     hwsim_utils.test_connectivity(dev[0], dev[2])
855
856     # dev0 and dev1 are mesh gates
857     subprocess.call(['iw', 'dev', dev[0].ifname, 'set', 'mesh_param',
858                      'mesh_gate_announcements=1'])
859     subprocess.call(['iw', 'dev', dev[1].ifname, 'set', 'mesh_param',
860                      'mesh_gate_announcements=1'])
861
862     # wait for gate announcement frames
863     time.sleep(1)
864
865     # data frame from dev2 -> external sta should be sent to both gates
866     dev[2].request("DATA_TEST_CONFIG 1")
867     dev[2].request("DATA_TEST_TX {} {} 0".format(external_sta, addr2))
868     dev[2].request("DATA_TEST_CONFIG 0")
869
870     capfile = os.path.join(p['logdir'], "hwsim0.pcapng")
871     filt = "wlan.sa==%s && wlan_mgt.fixed.mesh_addr5==%s" % (addr2,
872                                                              external_sta)
873     for i in range(15):
874         da = run_tshark(capfile, filt, [ "wlan.da" ])
875         if addr0 in da and addr1 in da:
876             logger.debug("Frames seen in tshark iteration %d" % i)
877             break
878         time.sleep(0.3)
879
880     if addr0 not in da:
881         raise Exception("Frame to gate %s not observed" % addr0)
882     if addr1 not in da:
883         raise Exception("Frame to gate %s not observed" % addr1)
884
885 def test_wpas_mesh_pmksa_caching(dev, apdev):
886     """Secure mesh network and PMKSA caching"""
887     check_mesh_support(dev[0], secure=True)
888     dev[0].request("SET sae_groups ")
889     id = add_mesh_secure_net(dev[0])
890     dev[0].mesh_group_add(id)
891
892     dev[1].request("SET sae_groups ")
893     id = add_mesh_secure_net(dev[1])
894     dev[1].mesh_group_add(id)
895
896     # Check for mesh joined
897     check_mesh_group_added(dev[0])
898     check_mesh_group_added(dev[1])
899
900     # Check for peer connected
901     check_mesh_peer_connected(dev[0])
902     check_mesh_peer_connected(dev[1])
903
904     addr0 = dev[0].own_addr()
905     addr1 = dev[1].own_addr()
906     pmksa0 = dev[0].get_pmksa(addr1)
907     pmksa1 = dev[1].get_pmksa(addr0)
908     if pmksa0 is None or pmksa1 is None:
909         raise Exception("No PMKSA cache entry created")
910     if pmksa0['pmkid'] != pmksa1['pmkid']:
911         raise Exception("PMKID mismatch in PMKSA cache entries")
912
913     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
914         raise Exception("Failed to remove peer")
915     pmksa0b = dev[0].get_pmksa(addr1)
916     if pmksa0b is None:
917         raise Exception("PMKSA cache entry not maintained")
918     time.sleep(0.1)
919
920     if "FAIL" not in dev[0].request("MESH_PEER_ADD " + addr1):
921         raise Exception("MESH_PEER_ADD unexpectedly succeeded in no_auto_peer=0 case")
922
923 def test_wpas_mesh_pmksa_caching2(dev, apdev):
924     """Secure mesh network and PMKSA caching with no_auto_peer=1"""
925     check_mesh_support(dev[0], secure=True)
926     addr0 = dev[0].own_addr()
927     addr1 = dev[1].own_addr()
928     dev[0].request("SET sae_groups ")
929     id = add_mesh_secure_net(dev[0])
930     dev[0].set_network(id, "no_auto_peer", "1")
931     dev[0].mesh_group_add(id)
932
933     dev[1].request("SET sae_groups ")
934     id = add_mesh_secure_net(dev[1])
935     dev[1].set_network(id, "no_auto_peer", "1")
936     dev[1].mesh_group_add(id)
937
938     # Check for mesh joined
939     check_mesh_group_added(dev[0])
940     check_mesh_group_added(dev[1])
941
942     # Check for peer connected
943     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
944     if ev is None:
945         raise Exception("Missing no-initiate message")
946     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
947         raise Exception("MESH_PEER_ADD failed")
948     check_mesh_peer_connected(dev[0])
949     check_mesh_peer_connected(dev[1])
950
951     pmksa0 = dev[0].get_pmksa(addr1)
952     pmksa1 = dev[1].get_pmksa(addr0)
953     if pmksa0 is None or pmksa1 is None:
954         raise Exception("No PMKSA cache entry created")
955     if pmksa0['pmkid'] != pmksa1['pmkid']:
956         raise Exception("PMKID mismatch in PMKSA cache entries")
957
958     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
959         raise Exception("Failed to remove peer")
960     pmksa0b = dev[0].get_pmksa(addr1)
961     if pmksa0b is None:
962         raise Exception("PMKSA cache entry not maintained")
963
964     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
965     if ev is None:
966         raise Exception("Missing no-initiate message (2)")
967     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
968         raise Exception("MESH_PEER_ADD failed (2)")
969     check_mesh_peer_connected(dev[0])
970     check_mesh_peer_connected(dev[1])
971
972     pmksa0c = dev[0].get_pmksa(addr1)
973     pmksa1c = dev[1].get_pmksa(addr0)
974     if pmksa0c is None or pmksa1c is None:
975         raise Exception("No PMKSA cache entry created (2)")
976     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
977         raise Exception("PMKID mismatch in PMKSA cache entries")
978     if pmksa0['pmkid'] != pmksa0c['pmkid']:
979         raise Exception("PMKID changed")
980
981     hwsim_utils.test_connectivity(dev[0], dev[1])
982
983 def test_wpas_mesh_pmksa_caching_no_match(dev, apdev):
984     """Secure mesh network and PMKSA caching with no PMKID match"""
985     check_mesh_support(dev[0], secure=True)
986     addr0 = dev[0].own_addr()
987     addr1 = dev[1].own_addr()
988     dev[0].request("SET sae_groups ")
989     id = add_mesh_secure_net(dev[0])
990     dev[0].set_network(id, "no_auto_peer", "1")
991     dev[0].mesh_group_add(id)
992
993     dev[1].request("SET sae_groups ")
994     id = add_mesh_secure_net(dev[1])
995     dev[1].set_network(id, "no_auto_peer", "1")
996     dev[1].mesh_group_add(id)
997
998     # Check for mesh joined
999     check_mesh_group_added(dev[0])
1000     check_mesh_group_added(dev[1])
1001
1002     # Check for peer connected
1003     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
1004     if ev is None:
1005         raise Exception("Missing no-initiate message")
1006     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
1007         raise Exception("MESH_PEER_ADD failed")
1008     check_mesh_peer_connected(dev[0])
1009     check_mesh_peer_connected(dev[1])
1010
1011     pmksa0 = dev[0].get_pmksa(addr1)
1012     pmksa1 = dev[1].get_pmksa(addr0)
1013     if pmksa0 is None or pmksa1 is None:
1014         raise Exception("No PMKSA cache entry created")
1015     if pmksa0['pmkid'] != pmksa1['pmkid']:
1016         raise Exception("PMKID mismatch in PMKSA cache entries")
1017
1018     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
1019         raise Exception("Failed to remove peer")
1020
1021     if "OK" not in dev[1].request("PMKSA_FLUSH"):
1022         raise Exception("Failed to flush PMKSA cache")
1023
1024     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
1025     if ev is None:
1026         raise Exception("Missing no-initiate message (2)")
1027     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
1028         raise Exception("MESH_PEER_ADD failed (2)")
1029     check_mesh_peer_connected(dev[0])
1030     check_mesh_peer_connected(dev[1])
1031
1032     pmksa0c = dev[0].get_pmksa(addr1)
1033     pmksa1c = dev[1].get_pmksa(addr0)
1034     if pmksa0c is None or pmksa1c is None:
1035         raise Exception("No PMKSA cache entry created (2)")
1036     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
1037         raise Exception("PMKID mismatch in PMKSA cache entries")
1038     if pmksa0['pmkid'] == pmksa0c['pmkid']:
1039         raise Exception("PMKID did not change")
1040
1041     hwsim_utils.test_connectivity(dev[0], dev[1])
1042
1043 def test_mesh_pmksa_caching_oom(dev, apdev):
1044     """Secure mesh network and PMKSA caching failing due to OOM"""
1045     check_mesh_support(dev[0], secure=True)
1046     addr0 = dev[0].own_addr()
1047     addr1 = dev[1].own_addr()
1048     dev[0].request("SET sae_groups ")
1049     id = add_mesh_secure_net(dev[0])
1050     dev[0].set_network(id, "no_auto_peer", "1")
1051     dev[0].mesh_group_add(id)
1052
1053     dev[1].request("SET sae_groups ")
1054     id = add_mesh_secure_net(dev[1])
1055     dev[1].set_network(id, "no_auto_peer", "1")
1056     dev[1].mesh_group_add(id)
1057
1058     # Check for mesh joined
1059     check_mesh_group_added(dev[0])
1060     check_mesh_group_added(dev[1])
1061
1062     # Check for peer connected
1063     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
1064     if ev is None:
1065         raise Exception("Missing no-initiate message")
1066     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
1067         raise Exception("MESH_PEER_ADD failed")
1068     check_mesh_peer_connected(dev[0])
1069     check_mesh_peer_connected(dev[1])
1070
1071     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
1072         raise Exception("Failed to remove peer")
1073     pmksa0b = dev[0].get_pmksa(addr1)
1074     if pmksa0b is None:
1075         raise Exception("PMKSA cache entry not maintained")
1076
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 (2)")
1080
1081     with alloc_fail(dev[0], 1, "wpa_auth_sta_init;mesh_rsn_auth_sae_sta"):
1082         if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
1083             raise Exception("MESH_PEER_ADD failed (2)")
1084         wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
1085
1086 def test_mesh_oom(dev, apdev):
1087     """Mesh network setup failing due to OOM"""
1088     check_mesh_support(dev[0], secure=True)
1089     dev[0].request("SET sae_groups ")
1090
1091     with alloc_fail(dev[0], 1, "mesh_config_create"):
1092         add_open_mesh_network(dev[0])
1093         ev = dev[0].wait_event(["Failed to init mesh"])
1094         if ev is None:
1095             raise Exception("Init failure not reported")
1096
1097     with alloc_fail(dev[0], 4, "=wpa_supplicant_mesh_init"):
1098         add_open_mesh_network(dev[0], basic_rates="60 120 240")
1099         ev = dev[0].wait_event(["Failed to init mesh"])
1100         if ev is None:
1101             raise Exception("Init failure not reported")
1102
1103     for i in range(1, 66):
1104         dev[0].dump_monitor()
1105         logger.info("Test instance %d" % i)
1106         try:
1107             with alloc_fail(dev[0], i, "wpa_supplicant_mesh_init"):
1108                 add_open_mesh_network(dev[0])
1109                 wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
1110                 ev = dev[0].wait_event(["Failed to init mesh",
1111                                         "MESH-GROUP-STARTED"])
1112                 if ev is None:
1113                     raise Exception("Init failure not reported")
1114         except Exception, e:
1115             if i < 15:
1116                 raise
1117             logger.info("Ignore no-oom for i=%d" % i)
1118
1119     with alloc_fail(dev[0], 5, "=wpa_supplicant_mesh_init"):
1120         id = add_mesh_secure_net(dev[0])
1121         dev[0].mesh_group_add(id)
1122         ev = dev[0].wait_event(["Failed to init mesh"])
1123         if ev is None:
1124             raise Exception("Init failure not reported")
1125
1126 def test_mesh_add_interface_oom(dev):
1127     """wpa_supplicant mesh with dynamic interface addition failing"""
1128     check_mesh_support(dev[0])
1129     for i in range(1, 3):
1130         mesh = None
1131         try:
1132             with alloc_fail(dev[0], i, "wpas_mesh_add_interface"):
1133                 mesh = dev[0].request("MESH_INTERFACE_ADD").strip()
1134         finally:
1135             if mesh and mesh != "FAIL":
1136                 dev[0].request("MESH_GROUP_REMOVE " + mesh)
1137
1138 def test_mesh_scan_oom(dev):
1139     """wpa_supplicant mesh scan results and OOM"""
1140     check_mesh_support(dev[0])
1141     add_open_mesh_network(dev[0])
1142     check_mesh_group_added(dev[0])
1143     for i in range(5):
1144         dev[1].scan(freq="2412")
1145         res = dev[1].request("SCAN_RESULTS")
1146         if "[MESH]" in res:
1147             break
1148     for r in res.splitlines():
1149         if "[MESH]" in r:
1150             break
1151     bssid = r.split('\t')[0]
1152
1153     bss = dev[1].get_bss(bssid)
1154     if bss is None:
1155         raise Exception("Could not get BSS entry for mesh")
1156
1157     for i in range(1, 3):
1158         with alloc_fail(dev[1], i, "mesh_attr_text"):
1159             bss = dev[1].get_bss(bssid)
1160             if bss is not None:
1161                 raise Exception("Unexpected BSS result during OOM")
1162
1163 def test_mesh_drv_fail(dev, apdev):
1164     """Mesh network setup failing due to driver command failure"""
1165     check_mesh_support(dev[0], secure=True)
1166     dev[0].request("SET sae_groups ")
1167
1168     with fail_test(dev[0], 1, "nl80211_join_mesh"):
1169         add_open_mesh_network(dev[0])
1170         ev = dev[0].wait_event(["mesh join error"])
1171         if ev is None:
1172             raise Exception("Join failure not reported")
1173
1174     dev[0].dump_monitor()
1175     with fail_test(dev[0], 1, "wpa_driver_nl80211_if_add"):
1176         if "FAIL" not in dev[0].request("MESH_INTERFACE_ADD").strip():
1177             raise Exception("Interface added unexpectedly")
1178
1179     dev[0].dump_monitor()
1180     with fail_test(dev[0], 1, "wpa_driver_nl80211_init_mesh"):
1181         add_open_mesh_network(dev[0])
1182         ev = dev[0].wait_event(["Could not join mesh"])
1183         if ev is None:
1184             raise Exception("Join failure not reported")
1185
1186 def test_mesh_sae_groups_invalid(dev, apdev):
1187     """Mesh with invalid SAE group configuration"""
1188     check_mesh_support(dev[0], secure=True)
1189
1190     dev[0].request("SET sae_groups 25")
1191     id = add_mesh_secure_net(dev[0])
1192     dev[0].mesh_group_add(id)
1193
1194     dev[1].request("SET sae_groups 123 122 121")
1195     id = add_mesh_secure_net(dev[1])
1196     dev[1].mesh_group_add(id)
1197
1198     check_mesh_group_added(dev[0])
1199     check_mesh_group_added(dev[1])
1200
1201     ev = dev[0].wait_event(["new peer notification"], timeout=10)
1202     if ev is None:
1203         raise Exception("dev[0] did not see peer")
1204     ev = dev[1].wait_event(["new peer notification"], timeout=10)
1205     if ev is None:
1206         raise Exception("dev[1] did not see peer")
1207
1208     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
1209     if ev is not None:
1210         raise Exception("Unexpected connection(0)")
1211
1212     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.01)
1213     if ev is not None:
1214         raise Exception("Unexpected connection(1)")
1215
1216     dev[0].request("SET sae_groups ")
1217     dev[1].request("SET sae_groups ")
1218
1219 def test_mesh_sae_failure(dev, apdev):
1220     """Mesh and local SAE failures"""
1221     check_mesh_support(dev[0], secure=True)
1222
1223     dev[0].request("SET sae_groups ")
1224     dev[1].request("SET sae_groups ")
1225
1226     funcs = [ (1, "=mesh_rsn_auth_sae_sta", True),
1227               (1, "mesh_rsn_build_sae_commit;mesh_rsn_auth_sae_sta", False),
1228               (1, "auth_sae_init_committed;mesh_rsn_auth_sae_sta", True),
1229               (1, "=mesh_rsn_protect_frame", True),
1230               (2, "=mesh_rsn_protect_frame", True),
1231               (1, "aes_siv_encrypt;mesh_rsn_protect_frame", True),
1232               (1, "=mesh_rsn_process_ampe", True),
1233               (1, "aes_siv_decrypt;mesh_rsn_process_ampe", True) ]
1234     for count, func, success in funcs:
1235         id = add_mesh_secure_net(dev[0])
1236         dev[0].mesh_group_add(id)
1237
1238         with alloc_fail(dev[1], count, func):
1239             id = add_mesh_secure_net(dev[1])
1240             dev[1].mesh_group_add(id)
1241             check_mesh_group_added(dev[0])
1242             check_mesh_group_added(dev[1])
1243             if success:
1244                 # retry is expected to work
1245                 check_mesh_peer_connected(dev[0])
1246                 check_mesh_peer_connected(dev[1])
1247             else:
1248                 wait_fail_trigger(dev[1], "GET_ALLOC_FAIL")
1249         dev[0].mesh_group_remove()
1250         dev[1].mesh_group_remove()
1251         check_mesh_group_removed(dev[0])
1252         check_mesh_group_removed(dev[1])
1253
1254 def test_mesh_failure(dev, apdev):
1255     """Mesh and local failures"""
1256     check_mesh_support(dev[0])
1257
1258     funcs = [ (1, "ap_sta_add;mesh_mpm_add_peer", True),
1259               (1, "wpabuf_alloc;mesh_mpm_send_plink_action", True) ]
1260     for count, func, success in funcs:
1261         add_open_mesh_network(dev[0])
1262
1263         with alloc_fail(dev[1], count, func):
1264             add_open_mesh_network(dev[1])
1265             check_mesh_group_added(dev[0])
1266             check_mesh_group_added(dev[1])
1267             if success:
1268                 # retry is expected to work
1269                 check_mesh_peer_connected(dev[0])
1270                 check_mesh_peer_connected(dev[1])
1271             else:
1272                 wait_fail_trigger(dev[1], "GET_ALLOC_FAIL")
1273         dev[0].mesh_group_remove()
1274         dev[1].mesh_group_remove()
1275         check_mesh_group_removed(dev[0])
1276         check_mesh_group_removed(dev[1])
1277
1278     funcs = [ (1, "mesh_mpm_init_link", True) ]
1279     for count, func, success in funcs:
1280         add_open_mesh_network(dev[0])
1281
1282         with fail_test(dev[1], count, func):
1283             add_open_mesh_network(dev[1])
1284             check_mesh_group_added(dev[0])
1285             check_mesh_group_added(dev[1])
1286             if success:
1287                 # retry is expected to work
1288                 check_mesh_peer_connected(dev[0])
1289                 check_mesh_peer_connected(dev[1])
1290             else:
1291                 wait_fail_trigger(dev[1], "GET_FAIL")
1292         dev[0].mesh_group_remove()
1293         dev[1].mesh_group_remove()
1294         check_mesh_group_removed(dev[0])
1295         check_mesh_group_removed(dev[1])
1296
1297 def test_mesh_invalid_frequency(dev, apdev):
1298     """Mesh and invalid frequency configuration"""
1299     check_mesh_support(dev[0])
1300     add_open_mesh_network(dev[0], freq=None)
1301     ev = dev[0].wait_event(["MESH-GROUP-STARTED",
1302                             "Could not join mesh"])
1303     if ev is None or "Could not join mesh" not in ev:
1304         raise Exception("Mesh join failure not reported")
1305     dev[0].request("REMOVE_NETWORK all")
1306
1307     add_open_mesh_network(dev[0], freq="2413")
1308     ev = dev[0].wait_event(["MESH-GROUP-STARTED",
1309                             "Could not join mesh"])
1310     if ev is None or "Could not join mesh" not in ev:
1311         raise Exception("Mesh join failure not reported")
1312
1313 def test_mesh_default_beacon_int(dev, apdev):
1314     """Mesh and default beacon interval"""
1315     check_mesh_support(dev[0])
1316     try:
1317         dev[0].request("SET beacon_int 200")
1318         add_open_mesh_network(dev[0])
1319         check_mesh_group_added(dev[0])
1320     finally:
1321         dev[0].request("SET beacon_int 0")
1322
1323 def test_mesh_scan_parse_error(dev, apdev):
1324     """Mesh scan element parse error"""
1325     check_mesh_support(dev[0])
1326     params = { "ssid": "open",
1327                "beacon_int": "2000" }
1328     hapd = hostapd.add_ap(apdev[0], params)
1329     bssid = apdev[0]['bssid']
1330     hapd.set('vendor_elements', 'dd0201')
1331     for i in range(10):
1332         dev[0].scan(freq=2412)
1333         if bssid in dev[0].request("SCAN_RESULTS"):
1334             break
1335     # This will fail in IE parsing due to the truncated IE in the Probe
1336     # Response frame.
1337     bss = dev[0].request("BSS " + bssid)