mesh: Fix error path handling in init OOM cases
[mech_eap.git] / tests / hwsim / test_wpas_mesh.py
1 #!/usr/bin/python
2 #
3 # wpa_supplicant mesh mode tests
4 # Copyright (c) 2014, cozybit Inc.
5 #
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
8
9 import logging
10 logger = logging.getLogger()
11 import os
12 import subprocess
13 import time
14
15 import hwsim_utils
16 from wpasupplicant import WpaSupplicant
17 from utils import HwsimSkip, alloc_fail
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     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_missing_password(dev, apdev):
317     """wpa_supplicant secure MESH and missing SAE password"""
318     check_mesh_support(dev[0], secure=True)
319     id = add_mesh_secure_net(dev[0], psk=False)
320     dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
321     dev[0].mesh_group_add(id)
322     ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
323                            timeout=5)
324     if ev is None:
325         raise Exception("Timeout on mesh start event")
326     if "MESH-GROUP-STARTED" in ev:
327         raise Exception("Unexpected mesh group start")
328     ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
329     if ev is not None:
330         raise Exception("Unexpected mesh group start")
331
332 def test_wpas_mesh_secure_no_auto(dev, apdev):
333     """wpa_supplicant secure MESH network connectivity"""
334     check_mesh_support(dev[0], secure=True)
335     dev[0].request("SET sae_groups 19")
336     id = add_mesh_secure_net(dev[0])
337     dev[0].mesh_group_add(id)
338
339     dev[1].request("SET sae_groups 19")
340     id = add_mesh_secure_net(dev[1])
341     dev[1].set_network(id, "no_auto_peer", "1")
342     dev[1].mesh_group_add(id)
343
344     # Check for mesh joined
345     check_mesh_group_added(dev[0])
346     check_mesh_group_added(dev[1])
347
348     # Check for peer connected
349     check_mesh_peer_connected(dev[0], timeout=30)
350     check_mesh_peer_connected(dev[1])
351
352     # Test connectivity 0->1 and 1->0
353     hwsim_utils.test_connectivity(dev[0], dev[1])
354
355     dev[0].request("SET sae_groups ")
356     dev[1].request("SET sae_groups ")
357
358 def test_wpas_mesh_secure_dropped_frame(dev, apdev):
359     """Secure mesh network connectivity when the first plink Open is dropped"""
360     check_mesh_support(dev[0], secure=True)
361
362     dev[0].request("SET ext_mgmt_frame_handling 1")
363     dev[0].request("SET sae_groups ")
364     id = add_mesh_secure_net(dev[0])
365     dev[0].mesh_group_add(id)
366
367     dev[1].request("SET sae_groups ")
368     id = add_mesh_secure_net(dev[1])
369     dev[1].mesh_group_add(id)
370
371     # Check for mesh joined
372     check_mesh_group_added(dev[0])
373     check_mesh_group_added(dev[1])
374
375     # Drop the first Action frame (plink Open) to test unexpected order of
376     # Confirm/Open messages.
377     count = 0
378     while True:
379         count += 1
380         if count > 10:
381             raise Exception("Did not see Action frames")
382         rx_msg = dev[0].mgmt_rx()
383         if rx_msg is None:
384             raise Exception("MGMT-RX timeout")
385         if rx_msg['subtype'] == 13:
386             logger.info("Drop the first Action frame")
387             break
388         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'))):
389             raise Exception("MGMT_RX_PROCESS failed")
390
391     dev[0].request("SET ext_mgmt_frame_handling 0")
392
393     # Check for peer connected
394     check_mesh_peer_connected(dev[0])
395     check_mesh_peer_connected(dev[1])
396
397     # Test connectivity 0->1 and 1->0
398     hwsim_utils.test_connectivity(dev[0], dev[1])
399
400 def test_wpas_mesh_ctrl(dev):
401     """wpa_supplicant ctrl_iface mesh command error cases"""
402     check_mesh_support(dev[0])
403     if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
404         raise Exception("Unexpected MESH_GROUP_ADD success")
405     id = dev[0].add_network()
406     if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
407         raise Exception("Unexpected MESH_GROUP_ADD success")
408     dev[0].set_network(id, "mode", "5")
409     dev[0].set_network(id, "key_mgmt", "WPA-PSK")
410     if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
411         raise Exception("Unexpected MESH_GROUP_ADD success")
412
413     if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
414         raise Exception("Unexpected MESH_GROUP_REMOVE success")
415
416 def test_wpas_mesh_dynamic_interface(dev):
417     """wpa_supplicant mesh with dynamic interface"""
418     check_mesh_support(dev[0])
419     mesh0 = None
420     mesh1 = None
421     try:
422         mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
423         if "FAIL" in mesh0:
424             raise Exception("MESH_INTERFACE_ADD failed")
425         mesh1 = dev[1].request("MESH_INTERFACE_ADD")
426         if "FAIL" in mesh1:
427             raise Exception("MESH_INTERFACE_ADD failed")
428
429         wpas0 = WpaSupplicant(ifname=mesh0)
430         wpas1 = WpaSupplicant(ifname=mesh1)
431         logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
432         logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
433
434         add_open_mesh_network(wpas0)
435         add_open_mesh_network(wpas1)
436         check_mesh_group_added(wpas0)
437         check_mesh_group_added(wpas1)
438         check_mesh_peer_connected(wpas0)
439         check_mesh_peer_connected(wpas1)
440         hwsim_utils.test_connectivity(wpas0, wpas1)
441
442         # Must not allow MESH_GROUP_REMOVE on dynamic interface
443         if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
444             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
445         if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
446             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
447
448         # Must not allow MESH_GROUP_REMOVE on another radio interface
449         if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
450             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
451         if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
452             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
453
454         wpas0.remove_ifname()
455         wpas1.remove_ifname()
456
457         if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
458             raise Exception("MESH_GROUP_REMOVE failed")
459         if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
460             raise Exception("MESH_GROUP_REMOVE failed")
461
462         if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
463             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
464         if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
465             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
466
467         logger.info("Make sure another dynamic group can be added")
468         mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
469         if "FAIL" in mesh0:
470             raise Exception("MESH_INTERFACE_ADD failed")
471         mesh1 = dev[1].request("MESH_INTERFACE_ADD")
472         if "FAIL" in mesh1:
473             raise Exception("MESH_INTERFACE_ADD failed")
474
475         wpas0 = WpaSupplicant(ifname=mesh0)
476         wpas1 = WpaSupplicant(ifname=mesh1)
477         logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
478         logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
479
480         add_open_mesh_network(wpas0)
481         add_open_mesh_network(wpas1)
482         check_mesh_group_added(wpas0)
483         check_mesh_group_added(wpas1)
484         check_mesh_peer_connected(wpas0)
485         check_mesh_peer_connected(wpas1)
486         hwsim_utils.test_connectivity(wpas0, wpas1)
487     finally:
488         if mesh0:
489             dev[0].request("MESH_GROUP_REMOVE " + mesh0)
490         if mesh1:
491             dev[1].request("MESH_GROUP_REMOVE " + mesh1)
492
493 def test_wpas_mesh_max_peering(dev, apdev):
494     """Mesh max peering limit"""
495     check_mesh_support(dev[0])
496     try:
497         dev[0].request("SET max_peer_links 1")
498
499         # first, connect dev[0] and dev[1]
500         add_open_mesh_network(dev[0])
501         add_open_mesh_network(dev[1])
502         for i in range(2):
503             ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
504             if ev is None:
505                 raise Exception("dev%d did not connect with any peer" % i)
506
507         # add dev[2] which will try to connect with both dev[0] and dev[1],
508         # but can complete connection only with dev[1]
509         add_open_mesh_network(dev[2])
510         for i in range(1, 3):
511             ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
512             if ev is None:
513                 raise Exception("dev%d did not connect the second peer" % i)
514
515         ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
516         if ev is not None:
517             raise Exception("dev0 connection beyond max peering limit")
518
519         ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
520         if ev is not None:
521             raise Exception("dev2 reported unexpected peering: " + ev)
522
523         for i in range(3):
524             dev[i].mesh_group_remove()
525             check_mesh_group_removed(dev[i])
526     finally:
527         dev[0].request("SET max_peer_links 99")
528
529 def test_wpas_mesh_open_5ghz(dev, apdev):
530     """wpa_supplicant open MESH network on 5 GHz band"""
531     try:
532         _test_wpas_mesh_open_5ghz(dev, apdev)
533     finally:
534         subprocess.call(['iw', 'reg', 'set', '00'])
535         dev[0].flush_scan_cache()
536         dev[1].flush_scan_cache()
537
538 def _test_wpas_mesh_open_5ghz(dev, apdev):
539     check_mesh_support(dev[0])
540     subprocess.call(['iw', 'reg', 'set', 'US'])
541     for i in range(2):
542         for j in range(5):
543             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
544             if ev is None:
545                 raise Exception("No regdom change event")
546             if "alpha2=US" in ev:
547                 break
548         add_open_mesh_network(dev[i], freq="5180")
549
550     # Check for mesh joined
551     check_mesh_group_added(dev[0])
552     check_mesh_group_added(dev[1])
553
554     # Check for peer connected
555     check_mesh_peer_connected(dev[0])
556     check_mesh_peer_connected(dev[1])
557
558     # Test connectivity 0->1 and 1->0
559     hwsim_utils.test_connectivity(dev[0], dev[1])
560
561 def test_wpas_mesh_open_vht_80p80(dev, apdev):
562     """wpa_supplicant open MESH network on VHT 80+80 MHz channel"""
563     try:
564         _test_wpas_mesh_open_vht_80p80(dev, apdev)
565     finally:
566         subprocess.call(['iw', 'reg', 'set', '00'])
567         dev[0].flush_scan_cache()
568         dev[1].flush_scan_cache()
569
570 def _test_wpas_mesh_open_vht_80p80(dev, apdev):
571     check_mesh_support(dev[0])
572     subprocess.call(['iw', 'reg', 'set', 'US'])
573     for i in range(2):
574         for j in range(5):
575             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
576             if ev is None:
577                 raise Exception("No regdom change event")
578             if "alpha2=US" in ev:
579                 break
580         add_open_mesh_network(dev[i], freq="5180", chwidth=3)
581
582     # Check for mesh joined
583     check_mesh_group_added(dev[0])
584     check_mesh_group_added(dev[1])
585
586     # Check for peer connected
587     check_mesh_peer_connected(dev[0])
588     check_mesh_peer_connected(dev[1])
589
590     # Test connectivity 0->1 and 1->0
591     hwsim_utils.test_connectivity(dev[0], dev[1])
592
593     sig = dev[0].request("SIGNAL_POLL").splitlines()
594     if "WIDTH=80+80 MHz" not in sig:
595         raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
596     if "CENTER_FRQ1=5210" not in sig:
597         raise Exception("Unexpected SIGNAL_POLL value(3): " + str(sig))
598     if "CENTER_FRQ2=5775" not in sig:
599         raise Exception("Unexpected SIGNAL_POLL value(4): " + str(sig))
600
601     sig = dev[1].request("SIGNAL_POLL").splitlines()
602     if "WIDTH=80+80 MHz" not in sig:
603         raise Exception("Unexpected SIGNAL_POLL value(2b): " + str(sig))
604     if "CENTER_FRQ1=5210" not in sig:
605         raise Exception("Unexpected SIGNAL_POLL value(3b): " + str(sig))
606     if "CENTER_FRQ2=5775" not in sig:
607         raise Exception("Unexpected SIGNAL_POLL value(4b): " + str(sig))
608
609 def test_wpas_mesh_password_mismatch(dev, apdev):
610     """Mesh network and one device with mismatching password"""
611     check_mesh_support(dev[0], secure=True)
612     dev[0].request("SET sae_groups ")
613     id = add_mesh_secure_net(dev[0])
614     dev[0].mesh_group_add(id)
615
616     dev[1].request("SET sae_groups ")
617     id = add_mesh_secure_net(dev[1])
618     dev[1].mesh_group_add(id)
619
620     dev[2].request("SET sae_groups ")
621     id = add_mesh_secure_net(dev[2])
622     dev[2].set_network_quoted(id, "psk", "wrong password")
623     dev[2].mesh_group_add(id)
624
625     # The two peers with matching password need to be able to connect
626     check_mesh_group_added(dev[0])
627     check_mesh_group_added(dev[1])
628     check_mesh_peer_connected(dev[0])
629     check_mesh_peer_connected(dev[1])
630
631     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
632     if ev is None:
633         raise Exception("dev2 did not report auth failure (1)")
634     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
635     if ev is None:
636         raise Exception("dev2 did not report auth failure (2)")
637
638     count = 0
639     ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
640     if ev is None:
641         logger.info("dev0 did not report auth failure")
642     else:
643         if "addr=" + dev[2].own_addr() not in ev:
644             raise Exception("Unexpected peer address in dev0 event: " + ev)
645         count += 1
646
647     ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
648     if ev is None:
649         logger.info("dev1 did not report auth failure")
650     else:
651         if "addr=" + dev[2].own_addr() not in ev:
652             raise Exception("Unexpected peer address in dev1 event: " + ev)
653         count += 1
654
655     hwsim_utils.test_connectivity(dev[0], dev[1])
656
657     for i in range(2):
658         try:
659             hwsim_utils.test_connectivity(dev[i], dev[2], timeout=1)
660             raise Exception("Data connectivity test passed unexpectedly")
661         except Exception, e:
662             if "data delivery failed" not in str(e):
663                 raise
664
665     if count == 0:
666         raise Exception("Neither dev0 nor dev1 reported auth failure")
667
668 def test_wpas_mesh_password_mismatch_retry(dev, apdev, params):
669     """Mesh password mismatch and retry [long]"""
670     if not params['long']:
671         raise HwsimSkip("Skip test case with long duration due to --long not specified")
672     check_mesh_support(dev[0], secure=True)
673     dev[0].request("SET sae_groups ")
674     id = add_mesh_secure_net(dev[0])
675     dev[0].mesh_group_add(id)
676
677     dev[1].request("SET sae_groups ")
678     id = add_mesh_secure_net(dev[1])
679     dev[1].set_network_quoted(id, "psk", "wrong password")
680     dev[1].mesh_group_add(id)
681
682     # Check for mesh joined
683     check_mesh_group_added(dev[0])
684     check_mesh_group_added(dev[1])
685
686     for i in range(4):
687         ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
688         if ev is None:
689             raise Exception("dev0 did not report auth failure (%d)" % i)
690         ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
691         if ev is None:
692             raise Exception("dev1 did not report auth failure (%d)" % i)
693
694     ev = dev[0].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
695     if ev is None:
696         raise Exception("dev0 did not report auth blocked")
697     ev = dev[1].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
698     if ev is None:
699         raise Exception("dev1 did not report auth blocked")
700
701 def test_mesh_wpa_auth_init_oom(dev, apdev):
702     """Secure mesh network setup failing due to wpa_init() OOM"""
703     check_mesh_support(dev[0], secure=True)
704     dev[0].request("SET sae_groups ")
705     with alloc_fail(dev[0], 1, "wpa_init"):
706         id = add_mesh_secure_net(dev[0])
707         dev[0].mesh_group_add(id)
708         ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.2)
709         if ev is not None:
710             raise Exception("Unexpected mesh group start during OOM")
711
712 def test_wpas_mesh_reconnect(dev, apdev):
713     """Secure mesh network plink counting during reconnection"""
714     check_mesh_support(dev[0])
715     try:
716         _test_wpas_mesh_reconnect(dev)
717     finally:
718         dev[0].request("SET max_peer_links 99")
719
720 def _test_wpas_mesh_reconnect(dev):
721     dev[0].request("SET max_peer_links 2")
722     dev[0].request("SET sae_groups ")
723     id = add_mesh_secure_net(dev[0])
724     dev[0].set_network(id, "beacon_int", "100")
725     dev[0].mesh_group_add(id)
726     dev[1].request("SET sae_groups ")
727     id = add_mesh_secure_net(dev[1])
728     dev[1].mesh_group_add(id)
729     check_mesh_group_added(dev[0])
730     check_mesh_group_added(dev[1])
731     check_mesh_peer_connected(dev[0])
732     check_mesh_peer_connected(dev[1])
733
734     for i in range(3):
735         # Drop incoming management frames to avoid handling link close
736         dev[0].request("SET ext_mgmt_frame_handling 1")
737         dev[1].mesh_group_remove()
738         check_mesh_group_removed(dev[1])
739         dev[1].request("FLUSH")
740         dev[0].request("SET ext_mgmt_frame_handling 0")
741         id = add_mesh_secure_net(dev[1])
742         dev[1].mesh_group_add(id)
743         check_mesh_group_added(dev[1])
744         check_mesh_peer_connected(dev[1])
745         dev[0].dump_monitor()
746         dev[1].dump_monitor()
747
748 def test_wpas_mesh_gate_forwarding(dev, apdev, p):
749     """Mesh forwards traffic to unknown sta to mesh gates"""
750     addr0 = dev[0].own_addr()
751     addr1 = dev[1].own_addr()
752     addr2 = dev[2].own_addr()
753     external_sta = '02:11:22:33:44:55'
754
755     # start 3 node connected mesh
756     check_mesh_support(dev[0])
757     for i in range(3):
758         add_open_mesh_network(dev[i])
759         check_mesh_group_added(dev[i])
760     for i in range(3):
761         check_mesh_peer_connected(dev[i])
762
763     hwsim_utils.test_connectivity(dev[0], dev[1])
764     hwsim_utils.test_connectivity(dev[1], dev[2])
765     hwsim_utils.test_connectivity(dev[0], dev[2])
766
767     # dev0 and dev1 are mesh gates
768     subprocess.call(['iw', 'dev', dev[0].ifname, 'set', 'mesh_param',
769                      'mesh_gate_announcements=1'])
770     subprocess.call(['iw', 'dev', dev[1].ifname, 'set', 'mesh_param',
771                      'mesh_gate_announcements=1'])
772
773     # wait for gate announcement frames
774     time.sleep(1)
775
776     # data frame from dev2 -> external sta should be sent to both gates
777     dev[2].request("DATA_TEST_CONFIG 1")
778     dev[2].request("DATA_TEST_TX {} {} 0".format(external_sta, addr2))
779     dev[2].request("DATA_TEST_CONFIG 0")
780
781     capfile = os.path.join(p['logdir'], "hwsim0.pcapng")
782     filt = "wlan.sa==%s && wlan_mgt.fixed.mesh_addr5==%s" % (addr2,
783                                                              external_sta)
784     for i in range(15):
785         da = run_tshark(capfile, filt, [ "wlan.da" ])
786         if addr0 in da and addr1 in da:
787             logger.debug("Frames seen in tshark iteration %d" % i)
788             break
789         time.sleep(0.3)
790
791     if addr0 not in da:
792         raise Exception("Frame to gate %s not observed" % addr0)
793     if addr1 not in da:
794         raise Exception("Frame to gate %s not observed" % addr1)
795
796 def test_wpas_mesh_pmksa_caching(dev, apdev):
797     """Secure mesh network and PMKSA caching"""
798     check_mesh_support(dev[0], secure=True)
799     dev[0].request("SET sae_groups ")
800     id = add_mesh_secure_net(dev[0])
801     dev[0].mesh_group_add(id)
802
803     dev[1].request("SET sae_groups ")
804     id = add_mesh_secure_net(dev[1])
805     dev[1].mesh_group_add(id)
806
807     # Check for mesh joined
808     check_mesh_group_added(dev[0])
809     check_mesh_group_added(dev[1])
810
811     # Check for peer connected
812     check_mesh_peer_connected(dev[0])
813     check_mesh_peer_connected(dev[1])
814
815     addr0 = dev[0].own_addr()
816     addr1 = dev[1].own_addr()
817     pmksa0 = dev[0].get_pmksa(addr1)
818     pmksa1 = dev[1].get_pmksa(addr0)
819     if pmksa0 is None or pmksa1 is None:
820         raise Exception("No PMKSA cache entry created")
821     if pmksa0['pmkid'] != pmksa1['pmkid']:
822         raise Exception("PMKID mismatch in PMKSA cache entries")
823
824     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
825         raise Exception("Failed to remove peer")
826     pmksa0b = dev[0].get_pmksa(addr1)
827     if pmksa0b is None:
828         raise Exception("PMKSA cache entry not maintained")
829     time.sleep(0.1)
830
831     if "FAIL" not in dev[0].request("MESH_PEER_ADD " + addr1):
832         raise Exception("MESH_PEER_ADD unexpectedly succeeded in no_auto_peer=0 case")
833
834 def test_wpas_mesh_pmksa_caching2(dev, apdev):
835     """Secure mesh network and PMKSA caching with no_auto_peer=1"""
836     check_mesh_support(dev[0], secure=True)
837     addr0 = dev[0].own_addr()
838     addr1 = dev[1].own_addr()
839     dev[0].request("SET sae_groups ")
840     id = add_mesh_secure_net(dev[0])
841     dev[0].set_network(id, "no_auto_peer", "1")
842     dev[0].mesh_group_add(id)
843
844     dev[1].request("SET sae_groups ")
845     id = add_mesh_secure_net(dev[1])
846     dev[1].set_network(id, "no_auto_peer", "1")
847     dev[1].mesh_group_add(id)
848
849     # Check for mesh joined
850     check_mesh_group_added(dev[0])
851     check_mesh_group_added(dev[1])
852
853     # Check for peer connected
854     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
855     if ev is None:
856         raise Exception("Missing no-initiate message")
857     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
858         raise Exception("MESH_PEER_ADD failed")
859     check_mesh_peer_connected(dev[0])
860     check_mesh_peer_connected(dev[1])
861
862     pmksa0 = dev[0].get_pmksa(addr1)
863     pmksa1 = dev[1].get_pmksa(addr0)
864     if pmksa0 is None or pmksa1 is None:
865         raise Exception("No PMKSA cache entry created")
866     if pmksa0['pmkid'] != pmksa1['pmkid']:
867         raise Exception("PMKID mismatch in PMKSA cache entries")
868
869     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
870         raise Exception("Failed to remove peer")
871     pmksa0b = dev[0].get_pmksa(addr1)
872     if pmksa0b is None:
873         raise Exception("PMKSA cache entry not maintained")
874
875     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
876     if ev is None:
877         raise Exception("Missing no-initiate message (2)")
878     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
879         raise Exception("MESH_PEER_ADD failed (2)")
880     check_mesh_peer_connected(dev[0])
881     check_mesh_peer_connected(dev[1])
882
883     pmksa0c = dev[0].get_pmksa(addr1)
884     pmksa1c = dev[1].get_pmksa(addr0)
885     if pmksa0c is None or pmksa1c is None:
886         raise Exception("No PMKSA cache entry created (2)")
887     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
888         raise Exception("PMKID mismatch in PMKSA cache entries")
889     if pmksa0['pmkid'] != pmksa0c['pmkid']:
890         raise Exception("PMKID changed")
891
892     hwsim_utils.test_connectivity(dev[0], dev[1])
893
894 def test_wpas_mesh_pmksa_caching_no_match(dev, apdev):
895     """Secure mesh network and PMKSA caching with no PMKID match"""
896     check_mesh_support(dev[0], secure=True)
897     addr0 = dev[0].own_addr()
898     addr1 = dev[1].own_addr()
899     dev[0].request("SET sae_groups ")
900     id = add_mesh_secure_net(dev[0])
901     dev[0].set_network(id, "no_auto_peer", "1")
902     dev[0].mesh_group_add(id)
903
904     dev[1].request("SET sae_groups ")
905     id = add_mesh_secure_net(dev[1])
906     dev[1].set_network(id, "no_auto_peer", "1")
907     dev[1].mesh_group_add(id)
908
909     # Check for mesh joined
910     check_mesh_group_added(dev[0])
911     check_mesh_group_added(dev[1])
912
913     # Check for peer connected
914     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
915     if ev is None:
916         raise Exception("Missing no-initiate message")
917     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
918         raise Exception("MESH_PEER_ADD failed")
919     check_mesh_peer_connected(dev[0])
920     check_mesh_peer_connected(dev[1])
921
922     pmksa0 = dev[0].get_pmksa(addr1)
923     pmksa1 = dev[1].get_pmksa(addr0)
924     if pmksa0 is None or pmksa1 is None:
925         raise Exception("No PMKSA cache entry created")
926     if pmksa0['pmkid'] != pmksa1['pmkid']:
927         raise Exception("PMKID mismatch in PMKSA cache entries")
928
929     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
930         raise Exception("Failed to remove peer")
931
932     if "OK" not in dev[1].request("PMKSA_FLUSH"):
933         raise Exception("Failed to flush PMKSA cache")
934
935     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
936     if ev is None:
937         raise Exception("Missing no-initiate message (2)")
938     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
939         raise Exception("MESH_PEER_ADD failed (2)")
940     check_mesh_peer_connected(dev[0])
941     check_mesh_peer_connected(dev[1])
942
943     pmksa0c = dev[0].get_pmksa(addr1)
944     pmksa1c = dev[1].get_pmksa(addr0)
945     if pmksa0c is None or pmksa1c is None:
946         raise Exception("No PMKSA cache entry created (2)")
947     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
948         raise Exception("PMKID mismatch in PMKSA cache entries")
949     if pmksa0['pmkid'] == pmksa0c['pmkid']:
950         raise Exception("PMKID did not change")
951
952     hwsim_utils.test_connectivity(dev[0], dev[1])