tests: wpa_supplicant mesh scan results and 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 from wpasupplicant import WpaSupplicant
15 from utils import HwsimSkip, alloc_fail, wait_fail_trigger
16 from tshark import run_tshark
17
18 def check_mesh_support(dev, secure=False):
19     if "MESH" not in dev.get_capability("modes"):
20         raise HwsimSkip("Driver does not support mesh")
21     if secure and "SAE" not in dev.get_capability("auth_alg"):
22         raise HwsimSkip("SAE not supported")
23
24 def check_mesh_scan(dev, params, other_started=False, beacon_int=0):
25     if not other_started:
26         dev.dump_monitor()
27     id = dev.request("SCAN " + params)
28     if "FAIL" in id:
29         raise Exception("Failed to start scan")
30     id = int(id)
31
32     if other_started:
33         ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
34         if ev is None:
35             raise Exception("Other scan did not start")
36         if "id=" + str(id) in ev:
37             raise Exception("Own scan id unexpectedly included in start event")
38
39         ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
40         if ev is None:
41             raise Exception("Other scan did not complete")
42         if "id=" + str(id) in ev:
43             raise Exception(
44                 "Own scan id unexpectedly included in completed event")
45
46     ev = dev.wait_event(["CTRL-EVENT-SCAN-STARTED"])
47     if ev is None:
48         raise Exception("Scan did not start")
49     if "id=" + str(id) not in ev:
50         raise Exception("Scan id not included in start event")
51
52     ev = dev.wait_event(["CTRL-EVENT-SCAN-RESULTS"])
53     if ev is None:
54         raise Exception("Scan did not complete")
55     if "id=" + str(id) not in ev:
56         raise Exception("Scan id not included in completed event")
57
58     res = dev.request("SCAN_RESULTS")
59
60     if res.find("[MESH]") < 0:
61         raise Exception("Scan did not contain a MESH network")
62
63     bssid = res.splitlines()[1].split(' ')[0]
64     bss = dev.get_bss(bssid)
65     if bss is None:
66         raise Exception("Could not get BSS entry for mesh")
67     if 'mesh_capability' not in bss:
68         raise Exception("mesh_capability missing from BSS entry")
69     if beacon_int:
70         if 'beacon_int' not in bss:
71             raise Exception("beacon_int missing from BSS entry")
72         if str(beacon_int) != bss['beacon_int']:
73             raise Exception("Unexpected beacon_int in BSS entry: " + bss['beacon_int'])
74
75 def check_mesh_group_added(dev):
76     ev = dev.wait_event(["MESH-GROUP-STARTED"])
77     if ev is None:
78         raise Exception("Test exception: Couldn't join mesh")
79
80
81 def check_mesh_group_removed(dev):
82     ev = dev.wait_event(["MESH-GROUP-REMOVED"])
83     if ev is None:
84         raise Exception("Test exception: Couldn't leave mesh")
85
86
87 def check_mesh_peer_connected(dev, timeout=10):
88     ev = dev.wait_event(["MESH-PEER-CONNECTED"], timeout=timeout)
89     if ev is None:
90         raise Exception("Test exception: Remote peer did not connect.")
91
92
93 def check_mesh_peer_disconnected(dev):
94     ev = dev.wait_event(["MESH-PEER-DISCONNECTED"])
95     if ev is None:
96         raise Exception("Test exception: Peer disconnect event not detected.")
97
98
99 def test_wpas_add_set_remove_support(dev):
100     """wpa_supplicant MESH add/set/remove network support"""
101     check_mesh_support(dev[0])
102     id = dev[0].add_network()
103     dev[0].set_network(id, "mode", "5")
104     dev[0].remove_network(id)
105
106 def add_open_mesh_network(dev, freq="2412", start=True, beacon_int=0,
107                           basic_rates=None, chwidth=0):
108     id = dev.add_network()
109     dev.set_network(id, "mode", "5")
110     dev.set_network_quoted(id, "ssid", "wpas-mesh-open")
111     dev.set_network(id, "key_mgmt", "NONE")
112     dev.set_network(id, "frequency", freq)
113     if chwidth > 0:
114         dev.set_network(id, "max_oper_chwidth", str(chwidth))
115     if beacon_int:
116         dev.set_network(id, "beacon_int", str(beacon_int))
117     if basic_rates:
118         dev.set_network(id, "mesh_basic_rates", basic_rates)
119     if start:
120         dev.mesh_group_add(id)
121     return id
122
123 def test_wpas_mesh_group_added(dev):
124     """wpa_supplicant MESH group add"""
125     check_mesh_support(dev[0])
126     add_open_mesh_network(dev[0])
127
128     # Check for MESH-GROUP-STARTED event
129     check_mesh_group_added(dev[0])
130
131
132 def test_wpas_mesh_group_remove(dev):
133     """wpa_supplicant MESH group remove"""
134     check_mesh_support(dev[0])
135     add_open_mesh_network(dev[0])
136     # Check for MESH-GROUP-STARTED event
137     check_mesh_group_added(dev[0])
138     dev[0].mesh_group_remove()
139     # Check for MESH-GROUP-REMOVED event
140     check_mesh_group_removed(dev[0])
141     dev[0].mesh_group_remove()
142
143 def test_wpas_mesh_peer_connected(dev):
144     """wpa_supplicant MESH peer connected"""
145     check_mesh_support(dev[0])
146     add_open_mesh_network(dev[0], beacon_int=160)
147     add_open_mesh_network(dev[1], beacon_int=160)
148
149     # Check for mesh joined
150     check_mesh_group_added(dev[0])
151     check_mesh_group_added(dev[1])
152
153     # Check for peer connected
154     check_mesh_peer_connected(dev[0])
155     check_mesh_peer_connected(dev[1])
156
157
158 def test_wpas_mesh_peer_disconnected(dev):
159     """wpa_supplicant MESH peer disconnected"""
160     check_mesh_support(dev[0])
161     add_open_mesh_network(dev[0])
162     add_open_mesh_network(dev[1])
163
164     # Check for mesh joined
165     check_mesh_group_added(dev[0])
166     check_mesh_group_added(dev[1])
167
168     # Check for peer connected
169     check_mesh_peer_connected(dev[0])
170     check_mesh_peer_connected(dev[1])
171
172     # Remove group on dev 1
173     dev[1].mesh_group_remove()
174     # Device 0 should get a disconnection event
175     check_mesh_peer_disconnected(dev[0])
176
177
178 def test_wpas_mesh_mode_scan(dev):
179     """wpa_supplicant MESH scan support"""
180     check_mesh_support(dev[0])
181     add_open_mesh_network(dev[0])
182     add_open_mesh_network(dev[1], beacon_int=175)
183
184     # Check for mesh joined
185     check_mesh_group_added(dev[0])
186     check_mesh_group_added(dev[1])
187
188     # Check for Mesh scan
189     check_mesh_scan(dev[0], "use_id=1", beacon_int=175)
190
191 def test_wpas_mesh_open(dev, apdev):
192     """wpa_supplicant open MESH network connectivity"""
193     check_mesh_support(dev[0])
194     add_open_mesh_network(dev[0], freq="2462", basic_rates="60 120 240")
195     add_open_mesh_network(dev[1], freq="2462", basic_rates="60 120 240")
196
197     # Check for mesh joined
198     check_mesh_group_added(dev[0])
199     check_mesh_group_added(dev[1])
200
201     # Check for peer connected
202     check_mesh_peer_connected(dev[0])
203     check_mesh_peer_connected(dev[1])
204
205     # Test connectivity 0->1 and 1->0
206     hwsim_utils.test_connectivity(dev[0], dev[1])
207
208 def test_wpas_mesh_open_no_auto(dev, apdev):
209     """wpa_supplicant open MESH network connectivity"""
210     check_mesh_support(dev[0])
211     id = add_open_mesh_network(dev[0], start=False)
212     dev[0].set_network(id, "dot11MeshMaxRetries", "16")
213     dev[0].set_network(id, "dot11MeshRetryTimeout", "255")
214     dev[0].mesh_group_add(id)
215
216     id = add_open_mesh_network(dev[1], start=False)
217     dev[1].set_network(id, "no_auto_peer", "1")
218     dev[1].mesh_group_add(id)
219
220     # Check for mesh joined
221     check_mesh_group_added(dev[0])
222     check_mesh_group_added(dev[1])
223
224     # Check for peer connected
225     check_mesh_peer_connected(dev[0], timeout=30)
226     check_mesh_peer_connected(dev[1])
227
228     # Test connectivity 0->1 and 1->0
229     hwsim_utils.test_connectivity(dev[0], dev[1])
230
231 def add_mesh_secure_net(dev, psk=True):
232     id = dev.add_network()
233     dev.set_network(id, "mode", "5")
234     dev.set_network_quoted(id, "ssid", "wpas-mesh-sec")
235     dev.set_network(id, "key_mgmt", "SAE")
236     dev.set_network(id, "frequency", "2412")
237     if psk:
238         dev.set_network_quoted(id, "psk", "thisismypassphrase!")
239     return id
240
241 def test_wpas_mesh_secure(dev, apdev):
242     """wpa_supplicant secure MESH network connectivity"""
243     check_mesh_support(dev[0], secure=True)
244     dev[0].request("SET sae_groups ")
245     id = add_mesh_secure_net(dev[0])
246     dev[0].mesh_group_add(id)
247
248     dev[1].request("SET sae_groups ")
249     id = add_mesh_secure_net(dev[1])
250     dev[1].mesh_group_add(id)
251
252     # Check for mesh joined
253     check_mesh_group_added(dev[0])
254     check_mesh_group_added(dev[1])
255
256     # Check for peer connected
257     check_mesh_peer_connected(dev[0])
258     check_mesh_peer_connected(dev[1])
259
260     # Test connectivity 0->1 and 1->0
261     hwsim_utils.test_connectivity(dev[0], dev[1])
262
263 def test_wpas_mesh_secure_sae_group_mismatch(dev, apdev):
264     """wpa_supplicant secure MESH and SAE group mismatch"""
265     check_mesh_support(dev[0], secure=True)
266     addr0 = dev[0].p2p_interface_addr()
267     addr1 = dev[1].p2p_interface_addr()
268     addr2 = dev[2].p2p_interface_addr()
269
270     dev[0].request("SET sae_groups 19 25")
271     id = add_mesh_secure_net(dev[0])
272     dev[0].mesh_group_add(id)
273
274     dev[1].request("SET sae_groups 19")
275     id = add_mesh_secure_net(dev[1])
276     dev[1].mesh_group_add(id)
277
278     dev[2].request("SET sae_groups 26")
279     id = add_mesh_secure_net(dev[2])
280     dev[2].mesh_group_add(id)
281
282     check_mesh_group_added(dev[0])
283     check_mesh_group_added(dev[1])
284     check_mesh_group_added(dev[2])
285
286     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"])
287     if ev is None:
288         raise Exception("Remote peer did not connect")
289     if addr1 not in ev:
290         raise Exception("Unexpected peer connected: " + ev)
291
292     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"])
293     if ev is None:
294         raise Exception("Remote peer did not connect")
295     if addr0 not in ev:
296         raise Exception("Unexpected peer connected: " + ev)
297
298     ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
299     if ev is not None:
300         raise Exception("Unexpected peer connection at dev[2]: " + ev)
301
302     ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
303     if ev is not None:
304         raise Exception("Unexpected peer connection: " + ev)
305
306     ev = dev[1].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
307     if ev is not None:
308         raise Exception("Unexpected peer connection: " + ev)
309
310     dev[0].request("SET sae_groups ")
311     dev[1].request("SET sae_groups ")
312     dev[2].request("SET sae_groups ")
313
314 def test_wpas_mesh_secure_sae_missing_password(dev, apdev):
315     """wpa_supplicant secure MESH and missing SAE password"""
316     check_mesh_support(dev[0], secure=True)
317     id = add_mesh_secure_net(dev[0], psk=False)
318     dev[0].set_network(id, "psk", "8f20b381f9b84371d61b5080ad85cac3c61ab3ca9525be5b2d0f4da3d979187a")
319     dev[0].mesh_group_add(id)
320     ev = dev[0].wait_event(["MESH-GROUP-STARTED", "Could not join mesh"],
321                            timeout=5)
322     if ev is None:
323         raise Exception("Timeout on mesh start event")
324     if "MESH-GROUP-STARTED" in ev:
325         raise Exception("Unexpected mesh group start")
326     ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.1)
327     if ev is not None:
328         raise Exception("Unexpected mesh group start")
329
330 def test_wpas_mesh_secure_no_auto(dev, apdev):
331     """wpa_supplicant secure MESH network connectivity"""
332     check_mesh_support(dev[0], secure=True)
333     dev[0].request("SET sae_groups 19")
334     id = add_mesh_secure_net(dev[0])
335     dev[0].mesh_group_add(id)
336
337     dev[1].request("SET sae_groups 19")
338     id = add_mesh_secure_net(dev[1])
339     dev[1].set_network(id, "no_auto_peer", "1")
340     dev[1].mesh_group_add(id)
341
342     # Check for mesh joined
343     check_mesh_group_added(dev[0])
344     check_mesh_group_added(dev[1])
345
346     # Check for peer connected
347     check_mesh_peer_connected(dev[0], timeout=30)
348     check_mesh_peer_connected(dev[1])
349
350     # Test connectivity 0->1 and 1->0
351     hwsim_utils.test_connectivity(dev[0], dev[1])
352
353     dev[0].request("SET sae_groups ")
354     dev[1].request("SET sae_groups ")
355
356 def test_wpas_mesh_secure_dropped_frame(dev, apdev):
357     """Secure mesh network connectivity when the first plink Open is dropped"""
358     check_mesh_support(dev[0], secure=True)
359
360     dev[0].request("SET ext_mgmt_frame_handling 1")
361     dev[0].request("SET sae_groups ")
362     id = add_mesh_secure_net(dev[0])
363     dev[0].mesh_group_add(id)
364
365     dev[1].request("SET sae_groups ")
366     id = add_mesh_secure_net(dev[1])
367     dev[1].mesh_group_add(id)
368
369     # Check for mesh joined
370     check_mesh_group_added(dev[0])
371     check_mesh_group_added(dev[1])
372
373     # Drop the first Action frame (plink Open) to test unexpected order of
374     # Confirm/Open messages.
375     count = 0
376     while True:
377         count += 1
378         if count > 10:
379             raise Exception("Did not see Action frames")
380         rx_msg = dev[0].mgmt_rx()
381         if rx_msg is None:
382             raise Exception("MGMT-RX timeout")
383         if rx_msg['subtype'] == 13:
384             logger.info("Drop the first Action frame")
385             break
386         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'))):
387             raise Exception("MGMT_RX_PROCESS failed")
388
389     dev[0].request("SET ext_mgmt_frame_handling 0")
390
391     # Check for peer connected
392     check_mesh_peer_connected(dev[0])
393     check_mesh_peer_connected(dev[1])
394
395     # Test connectivity 0->1 and 1->0
396     hwsim_utils.test_connectivity(dev[0], dev[1])
397
398 def test_wpas_mesh_ctrl(dev):
399     """wpa_supplicant ctrl_iface mesh command error cases"""
400     check_mesh_support(dev[0])
401     if "FAIL" not in dev[0].request("MESH_GROUP_ADD 123"):
402         raise Exception("Unexpected MESH_GROUP_ADD success")
403     id = dev[0].add_network()
404     if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
405         raise Exception("Unexpected MESH_GROUP_ADD success")
406     dev[0].set_network(id, "mode", "5")
407     dev[0].set_network(id, "key_mgmt", "WPA-PSK")
408     if "FAIL" not in dev[0].request("MESH_GROUP_ADD %d" % id):
409         raise Exception("Unexpected MESH_GROUP_ADD success")
410
411     if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE foo"):
412         raise Exception("Unexpected MESH_GROUP_REMOVE success")
413
414 def test_wpas_mesh_dynamic_interface(dev):
415     """wpa_supplicant mesh with dynamic interface"""
416     check_mesh_support(dev[0])
417     mesh0 = None
418     mesh1 = None
419     try:
420         mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
421         if "FAIL" in mesh0:
422             raise Exception("MESH_INTERFACE_ADD failed")
423         mesh1 = dev[1].request("MESH_INTERFACE_ADD")
424         if "FAIL" in mesh1:
425             raise Exception("MESH_INTERFACE_ADD failed")
426
427         wpas0 = WpaSupplicant(ifname=mesh0)
428         wpas1 = WpaSupplicant(ifname=mesh1)
429         logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
430         logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
431
432         add_open_mesh_network(wpas0)
433         add_open_mesh_network(wpas1)
434         check_mesh_group_added(wpas0)
435         check_mesh_group_added(wpas1)
436         check_mesh_peer_connected(wpas0)
437         check_mesh_peer_connected(wpas1)
438         hwsim_utils.test_connectivity(wpas0, wpas1)
439
440         # Must not allow MESH_GROUP_REMOVE on dynamic interface
441         if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh0):
442             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
443         if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh1):
444             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
445
446         # Must not allow MESH_GROUP_REMOVE on another radio interface
447         if "FAIL" not in wpas0.request("MESH_GROUP_REMOVE " + mesh1):
448             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
449         if "FAIL" not in wpas1.request("MESH_GROUP_REMOVE " + mesh0):
450             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
451
452         wpas0.remove_ifname()
453         wpas1.remove_ifname()
454
455         if "OK" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
456             raise Exception("MESH_GROUP_REMOVE failed")
457         if "OK" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
458             raise Exception("MESH_GROUP_REMOVE failed")
459
460         if "FAIL" not in dev[0].request("MESH_GROUP_REMOVE " + mesh0):
461             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
462         if "FAIL" not in dev[1].request("MESH_GROUP_REMOVE " + mesh1):
463             raise Exception("Invalid MESH_GROUP_REMOVE accepted")
464
465         logger.info("Make sure another dynamic group can be added")
466         mesh0 = dev[0].request("MESH_INTERFACE_ADD ifname=mesh0")
467         if "FAIL" in mesh0:
468             raise Exception("MESH_INTERFACE_ADD failed")
469         mesh1 = dev[1].request("MESH_INTERFACE_ADD")
470         if "FAIL" in mesh1:
471             raise Exception("MESH_INTERFACE_ADD failed")
472
473         wpas0 = WpaSupplicant(ifname=mesh0)
474         wpas1 = WpaSupplicant(ifname=mesh1)
475         logger.info(mesh0 + " address " + wpas0.get_status_field("address"))
476         logger.info(mesh1 + " address " + wpas1.get_status_field("address"))
477
478         add_open_mesh_network(wpas0)
479         add_open_mesh_network(wpas1)
480         check_mesh_group_added(wpas0)
481         check_mesh_group_added(wpas1)
482         check_mesh_peer_connected(wpas0)
483         check_mesh_peer_connected(wpas1)
484         hwsim_utils.test_connectivity(wpas0, wpas1)
485     finally:
486         if mesh0:
487             dev[0].request("MESH_GROUP_REMOVE " + mesh0)
488         if mesh1:
489             dev[1].request("MESH_GROUP_REMOVE " + mesh1)
490
491 def test_wpas_mesh_max_peering(dev, apdev):
492     """Mesh max peering limit"""
493     check_mesh_support(dev[0])
494     try:
495         dev[0].request("SET max_peer_links 1")
496
497         # first, connect dev[0] and dev[1]
498         add_open_mesh_network(dev[0])
499         add_open_mesh_network(dev[1])
500         for i in range(2):
501             ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
502             if ev is None:
503                 raise Exception("dev%d did not connect with any peer" % i)
504
505         # add dev[2] which will try to connect with both dev[0] and dev[1],
506         # but can complete connection only with dev[1]
507         add_open_mesh_network(dev[2])
508         for i in range(1, 3):
509             ev = dev[i].wait_event(["MESH-PEER-CONNECTED"])
510             if ev is None:
511                 raise Exception("dev%d did not connect the second peer" % i)
512
513         ev = dev[0].wait_event(["MESH-PEER-CONNECTED"], timeout=1)
514         if ev is not None:
515             raise Exception("dev0 connection beyond max peering limit")
516
517         ev = dev[2].wait_event(["MESH-PEER-CONNECTED"], timeout=0.1)
518         if ev is not None:
519             raise Exception("dev2 reported unexpected peering: " + ev)
520
521         for i in range(3):
522             dev[i].mesh_group_remove()
523             check_mesh_group_removed(dev[i])
524     finally:
525         dev[0].request("SET max_peer_links 99")
526
527 def test_wpas_mesh_open_5ghz(dev, apdev):
528     """wpa_supplicant open MESH network on 5 GHz band"""
529     try:
530         _test_wpas_mesh_open_5ghz(dev, apdev)
531     finally:
532         subprocess.call(['iw', 'reg', 'set', '00'])
533         dev[0].flush_scan_cache()
534         dev[1].flush_scan_cache()
535
536 def _test_wpas_mesh_open_5ghz(dev, apdev):
537     check_mesh_support(dev[0])
538     subprocess.call(['iw', 'reg', 'set', 'US'])
539     for i in range(2):
540         for j in range(5):
541             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
542             if ev is None:
543                 raise Exception("No regdom change event")
544             if "alpha2=US" in ev:
545                 break
546         add_open_mesh_network(dev[i], freq="5180")
547
548     # Check for mesh joined
549     check_mesh_group_added(dev[0])
550     check_mesh_group_added(dev[1])
551
552     # Check for peer connected
553     check_mesh_peer_connected(dev[0])
554     check_mesh_peer_connected(dev[1])
555
556     # Test connectivity 0->1 and 1->0
557     hwsim_utils.test_connectivity(dev[0], dev[1])
558
559 def test_wpas_mesh_open_vht_80p80(dev, apdev):
560     """wpa_supplicant open MESH network on VHT 80+80 MHz channel"""
561     try:
562         _test_wpas_mesh_open_vht_80p80(dev, apdev)
563     finally:
564         subprocess.call(['iw', 'reg', 'set', '00'])
565         dev[0].flush_scan_cache()
566         dev[1].flush_scan_cache()
567
568 def _test_wpas_mesh_open_vht_80p80(dev, apdev):
569     check_mesh_support(dev[0])
570     subprocess.call(['iw', 'reg', 'set', 'US'])
571     for i in range(2):
572         for j in range(5):
573             ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
574             if ev is None:
575                 raise Exception("No regdom change event")
576             if "alpha2=US" in ev:
577                 break
578         add_open_mesh_network(dev[i], freq="5180", chwidth=3)
579
580     # Check for mesh joined
581     check_mesh_group_added(dev[0])
582     check_mesh_group_added(dev[1])
583
584     # Check for peer connected
585     check_mesh_peer_connected(dev[0])
586     check_mesh_peer_connected(dev[1])
587
588     # Test connectivity 0->1 and 1->0
589     hwsim_utils.test_connectivity(dev[0], dev[1])
590
591     sig = dev[0].request("SIGNAL_POLL").splitlines()
592     if "WIDTH=80+80 MHz" not in sig:
593         raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
594     if "CENTER_FRQ1=5210" not in sig:
595         raise Exception("Unexpected SIGNAL_POLL value(3): " + str(sig))
596     if "CENTER_FRQ2=5775" not in sig:
597         raise Exception("Unexpected SIGNAL_POLL value(4): " + str(sig))
598
599     sig = dev[1].request("SIGNAL_POLL").splitlines()
600     if "WIDTH=80+80 MHz" not in sig:
601         raise Exception("Unexpected SIGNAL_POLL value(2b): " + str(sig))
602     if "CENTER_FRQ1=5210" not in sig:
603         raise Exception("Unexpected SIGNAL_POLL value(3b): " + str(sig))
604     if "CENTER_FRQ2=5775" not in sig:
605         raise Exception("Unexpected SIGNAL_POLL value(4b): " + str(sig))
606
607 def test_wpas_mesh_password_mismatch(dev, apdev):
608     """Mesh network and one device with mismatching password"""
609     check_mesh_support(dev[0], secure=True)
610     dev[0].request("SET sae_groups ")
611     id = add_mesh_secure_net(dev[0])
612     dev[0].mesh_group_add(id)
613
614     dev[1].request("SET sae_groups ")
615     id = add_mesh_secure_net(dev[1])
616     dev[1].mesh_group_add(id)
617
618     dev[2].request("SET sae_groups ")
619     id = add_mesh_secure_net(dev[2])
620     dev[2].set_network_quoted(id, "psk", "wrong password")
621     dev[2].mesh_group_add(id)
622
623     # The two peers with matching password need to be able to connect
624     check_mesh_group_added(dev[0])
625     check_mesh_group_added(dev[1])
626     check_mesh_peer_connected(dev[0])
627     check_mesh_peer_connected(dev[1])
628
629     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
630     if ev is None:
631         raise Exception("dev2 did not report auth failure (1)")
632     ev = dev[2].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
633     if ev is None:
634         raise Exception("dev2 did not report auth failure (2)")
635
636     count = 0
637     ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
638     if ev is None:
639         logger.info("dev0 did not report auth failure")
640     else:
641         if "addr=" + dev[2].own_addr() not in ev:
642             raise Exception("Unexpected peer address in dev0 event: " + ev)
643         count += 1
644
645     ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=1)
646     if ev is None:
647         logger.info("dev1 did not report auth failure")
648     else:
649         if "addr=" + dev[2].own_addr() not in ev:
650             raise Exception("Unexpected peer address in dev1 event: " + ev)
651         count += 1
652
653     hwsim_utils.test_connectivity(dev[0], dev[1])
654
655     for i in range(2):
656         try:
657             hwsim_utils.test_connectivity(dev[i], dev[2], timeout=1)
658             raise Exception("Data connectivity test passed unexpectedly")
659         except Exception, e:
660             if "data delivery failed" not in str(e):
661                 raise
662
663     if count == 0:
664         raise Exception("Neither dev0 nor dev1 reported auth failure")
665
666 def test_wpas_mesh_password_mismatch_retry(dev, apdev, params):
667     """Mesh password mismatch and retry [long]"""
668     if not params['long']:
669         raise HwsimSkip("Skip test case with long duration due to --long not specified")
670     check_mesh_support(dev[0], secure=True)
671     dev[0].request("SET sae_groups ")
672     id = add_mesh_secure_net(dev[0])
673     dev[0].mesh_group_add(id)
674
675     dev[1].request("SET sae_groups ")
676     id = add_mesh_secure_net(dev[1])
677     dev[1].set_network_quoted(id, "psk", "wrong password")
678     dev[1].mesh_group_add(id)
679
680     # Check for mesh joined
681     check_mesh_group_added(dev[0])
682     check_mesh_group_added(dev[1])
683
684     for i in range(4):
685         ev = dev[0].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
686         if ev is None:
687             raise Exception("dev0 did not report auth failure (%d)" % i)
688         ev = dev[1].wait_event(["MESH-SAE-AUTH-FAILURE"], timeout=20)
689         if ev is None:
690             raise Exception("dev1 did not report auth failure (%d)" % i)
691
692     ev = dev[0].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
693     if ev is None:
694         raise Exception("dev0 did not report auth blocked")
695     ev = dev[1].wait_event(["MESH-SAE-AUTH-BLOCKED"], timeout=10)
696     if ev is None:
697         raise Exception("dev1 did not report auth blocked")
698
699 def test_mesh_wpa_auth_init_oom(dev, apdev):
700     """Secure mesh network setup failing due to wpa_init() OOM"""
701     check_mesh_support(dev[0], secure=True)
702     dev[0].request("SET sae_groups ")
703     with alloc_fail(dev[0], 1, "wpa_init"):
704         id = add_mesh_secure_net(dev[0])
705         dev[0].mesh_group_add(id)
706         ev = dev[0].wait_event(["MESH-GROUP-STARTED"], timeout=0.2)
707         if ev is not None:
708             raise Exception("Unexpected mesh group start during OOM")
709
710 def test_wpas_mesh_reconnect(dev, apdev):
711     """Secure mesh network plink counting during reconnection"""
712     check_mesh_support(dev[0])
713     try:
714         _test_wpas_mesh_reconnect(dev)
715     finally:
716         dev[0].request("SET max_peer_links 99")
717
718 def _test_wpas_mesh_reconnect(dev):
719     dev[0].request("SET max_peer_links 2")
720     dev[0].request("SET sae_groups ")
721     id = add_mesh_secure_net(dev[0])
722     dev[0].set_network(id, "beacon_int", "100")
723     dev[0].mesh_group_add(id)
724     dev[1].request("SET sae_groups ")
725     id = add_mesh_secure_net(dev[1])
726     dev[1].mesh_group_add(id)
727     check_mesh_group_added(dev[0])
728     check_mesh_group_added(dev[1])
729     check_mesh_peer_connected(dev[0])
730     check_mesh_peer_connected(dev[1])
731
732     for i in range(3):
733         # Drop incoming management frames to avoid handling link close
734         dev[0].request("SET ext_mgmt_frame_handling 1")
735         dev[1].mesh_group_remove()
736         check_mesh_group_removed(dev[1])
737         dev[1].request("FLUSH")
738         dev[0].request("SET ext_mgmt_frame_handling 0")
739         id = add_mesh_secure_net(dev[1])
740         dev[1].mesh_group_add(id)
741         check_mesh_group_added(dev[1])
742         check_mesh_peer_connected(dev[1])
743         dev[0].dump_monitor()
744         dev[1].dump_monitor()
745
746 def test_wpas_mesh_gate_forwarding(dev, apdev, p):
747     """Mesh forwards traffic to unknown sta to mesh gates"""
748     addr0 = dev[0].own_addr()
749     addr1 = dev[1].own_addr()
750     addr2 = dev[2].own_addr()
751     external_sta = '02:11:22:33:44:55'
752
753     # start 3 node connected mesh
754     check_mesh_support(dev[0])
755     for i in range(3):
756         add_open_mesh_network(dev[i])
757         check_mesh_group_added(dev[i])
758     for i in range(3):
759         check_mesh_peer_connected(dev[i])
760
761     hwsim_utils.test_connectivity(dev[0], dev[1])
762     hwsim_utils.test_connectivity(dev[1], dev[2])
763     hwsim_utils.test_connectivity(dev[0], dev[2])
764
765     # dev0 and dev1 are mesh gates
766     subprocess.call(['iw', 'dev', dev[0].ifname, 'set', 'mesh_param',
767                      'mesh_gate_announcements=1'])
768     subprocess.call(['iw', 'dev', dev[1].ifname, 'set', 'mesh_param',
769                      'mesh_gate_announcements=1'])
770
771     # wait for gate announcement frames
772     time.sleep(1)
773
774     # data frame from dev2 -> external sta should be sent to both gates
775     dev[2].request("DATA_TEST_CONFIG 1")
776     dev[2].request("DATA_TEST_TX {} {} 0".format(external_sta, addr2))
777     dev[2].request("DATA_TEST_CONFIG 0")
778
779     capfile = os.path.join(p['logdir'], "hwsim0.pcapng")
780     filt = "wlan.sa==%s && wlan_mgt.fixed.mesh_addr5==%s" % (addr2,
781                                                              external_sta)
782     for i in range(15):
783         da = run_tshark(capfile, filt, [ "wlan.da" ])
784         if addr0 in da and addr1 in da:
785             logger.debug("Frames seen in tshark iteration %d" % i)
786             break
787         time.sleep(0.3)
788
789     if addr0 not in da:
790         raise Exception("Frame to gate %s not observed" % addr0)
791     if addr1 not in da:
792         raise Exception("Frame to gate %s not observed" % addr1)
793
794 def test_wpas_mesh_pmksa_caching(dev, apdev):
795     """Secure mesh network and PMKSA caching"""
796     check_mesh_support(dev[0], secure=True)
797     dev[0].request("SET sae_groups ")
798     id = add_mesh_secure_net(dev[0])
799     dev[0].mesh_group_add(id)
800
801     dev[1].request("SET sae_groups ")
802     id = add_mesh_secure_net(dev[1])
803     dev[1].mesh_group_add(id)
804
805     # Check for mesh joined
806     check_mesh_group_added(dev[0])
807     check_mesh_group_added(dev[1])
808
809     # Check for peer connected
810     check_mesh_peer_connected(dev[0])
811     check_mesh_peer_connected(dev[1])
812
813     addr0 = dev[0].own_addr()
814     addr1 = dev[1].own_addr()
815     pmksa0 = dev[0].get_pmksa(addr1)
816     pmksa1 = dev[1].get_pmksa(addr0)
817     if pmksa0 is None or pmksa1 is None:
818         raise Exception("No PMKSA cache entry created")
819     if pmksa0['pmkid'] != pmksa1['pmkid']:
820         raise Exception("PMKID mismatch in PMKSA cache entries")
821
822     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
823         raise Exception("Failed to remove peer")
824     pmksa0b = dev[0].get_pmksa(addr1)
825     if pmksa0b is None:
826         raise Exception("PMKSA cache entry not maintained")
827     time.sleep(0.1)
828
829     if "FAIL" not in dev[0].request("MESH_PEER_ADD " + addr1):
830         raise Exception("MESH_PEER_ADD unexpectedly succeeded in no_auto_peer=0 case")
831
832 def test_wpas_mesh_pmksa_caching2(dev, apdev):
833     """Secure mesh network and PMKSA caching with no_auto_peer=1"""
834     check_mesh_support(dev[0], secure=True)
835     addr0 = dev[0].own_addr()
836     addr1 = dev[1].own_addr()
837     dev[0].request("SET sae_groups ")
838     id = add_mesh_secure_net(dev[0])
839     dev[0].set_network(id, "no_auto_peer", "1")
840     dev[0].mesh_group_add(id)
841
842     dev[1].request("SET sae_groups ")
843     id = add_mesh_secure_net(dev[1])
844     dev[1].set_network(id, "no_auto_peer", "1")
845     dev[1].mesh_group_add(id)
846
847     # Check for mesh joined
848     check_mesh_group_added(dev[0])
849     check_mesh_group_added(dev[1])
850
851     # Check for peer connected
852     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
853     if ev is None:
854         raise Exception("Missing no-initiate message")
855     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
856         raise Exception("MESH_PEER_ADD failed")
857     check_mesh_peer_connected(dev[0])
858     check_mesh_peer_connected(dev[1])
859
860     pmksa0 = dev[0].get_pmksa(addr1)
861     pmksa1 = dev[1].get_pmksa(addr0)
862     if pmksa0 is None or pmksa1 is None:
863         raise Exception("No PMKSA cache entry created")
864     if pmksa0['pmkid'] != pmksa1['pmkid']:
865         raise Exception("PMKID mismatch in PMKSA cache entries")
866
867     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
868         raise Exception("Failed to remove peer")
869     pmksa0b = dev[0].get_pmksa(addr1)
870     if pmksa0b is None:
871         raise Exception("PMKSA cache entry not maintained")
872
873     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
874     if ev is None:
875         raise Exception("Missing no-initiate message (2)")
876     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
877         raise Exception("MESH_PEER_ADD failed (2)")
878     check_mesh_peer_connected(dev[0])
879     check_mesh_peer_connected(dev[1])
880
881     pmksa0c = dev[0].get_pmksa(addr1)
882     pmksa1c = dev[1].get_pmksa(addr0)
883     if pmksa0c is None or pmksa1c is None:
884         raise Exception("No PMKSA cache entry created (2)")
885     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
886         raise Exception("PMKID mismatch in PMKSA cache entries")
887     if pmksa0['pmkid'] != pmksa0c['pmkid']:
888         raise Exception("PMKID changed")
889
890     hwsim_utils.test_connectivity(dev[0], dev[1])
891
892 def test_wpas_mesh_pmksa_caching_no_match(dev, apdev):
893     """Secure mesh network and PMKSA caching with no PMKID match"""
894     check_mesh_support(dev[0], secure=True)
895     addr0 = dev[0].own_addr()
896     addr1 = dev[1].own_addr()
897     dev[0].request("SET sae_groups ")
898     id = add_mesh_secure_net(dev[0])
899     dev[0].set_network(id, "no_auto_peer", "1")
900     dev[0].mesh_group_add(id)
901
902     dev[1].request("SET sae_groups ")
903     id = add_mesh_secure_net(dev[1])
904     dev[1].set_network(id, "no_auto_peer", "1")
905     dev[1].mesh_group_add(id)
906
907     # Check for mesh joined
908     check_mesh_group_added(dev[0])
909     check_mesh_group_added(dev[1])
910
911     # Check for peer connected
912     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
913     if ev is None:
914         raise Exception("Missing no-initiate message")
915     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
916         raise Exception("MESH_PEER_ADD failed")
917     check_mesh_peer_connected(dev[0])
918     check_mesh_peer_connected(dev[1])
919
920     pmksa0 = dev[0].get_pmksa(addr1)
921     pmksa1 = dev[1].get_pmksa(addr0)
922     if pmksa0 is None or pmksa1 is None:
923         raise Exception("No PMKSA cache entry created")
924     if pmksa0['pmkid'] != pmksa1['pmkid']:
925         raise Exception("PMKID mismatch in PMKSA cache entries")
926
927     if "OK" not in dev[0].request("MESH_PEER_REMOVE " + addr1):
928         raise Exception("Failed to remove peer")
929
930     if "OK" not in dev[1].request("PMKSA_FLUSH"):
931         raise Exception("Failed to flush PMKSA cache")
932
933     ev = dev[0].wait_event(["will not initiate new peer link"], timeout=10)
934     if ev is None:
935         raise Exception("Missing no-initiate message (2)")
936     if "OK" not in dev[0].request("MESH_PEER_ADD " + addr1):
937         raise Exception("MESH_PEER_ADD failed (2)")
938     check_mesh_peer_connected(dev[0])
939     check_mesh_peer_connected(dev[1])
940
941     pmksa0c = dev[0].get_pmksa(addr1)
942     pmksa1c = dev[1].get_pmksa(addr0)
943     if pmksa0c is None or pmksa1c is None:
944         raise Exception("No PMKSA cache entry created (2)")
945     if pmksa0c['pmkid'] != pmksa1c['pmkid']:
946         raise Exception("PMKID mismatch in PMKSA cache entries")
947     if pmksa0['pmkid'] == pmksa0c['pmkid']:
948         raise Exception("PMKID did not change")
949
950     hwsim_utils.test_connectivity(dev[0], dev[1])
951
952 def test_mesh_oom(dev, apdev):
953     """Mesh network setup failing due to OOM"""
954     check_mesh_support(dev[0], secure=True)
955     dev[0].request("SET sae_groups ")
956
957     with alloc_fail(dev[0], 1, "mesh_config_create"):
958         add_open_mesh_network(dev[0])
959         ev = dev[0].wait_event(["Failed to init mesh"])
960         if ev is None:
961             raise Exception("Init failure not reported")
962
963     for i in range(1, 65):
964         with alloc_fail(dev[0], i, "wpa_supplicant_mesh_init"):
965             add_open_mesh_network(dev[0])
966             wait_fail_trigger(dev[0], "GET_ALLOC_FAIL")
967             ev = dev[0].wait_event(["Failed to init mesh",
968                                     "MESH-GROUP-STARTED"])
969             if ev is None:
970                 raise Exception("Init failure not reported")
971
972 def test_mesh_add_interface_oom(dev):
973     """wpa_supplicant mesh with dynamic interface addition failing"""
974     check_mesh_support(dev[0])
975     for i in range(1, 3):
976         mesh = None
977         try:
978             with alloc_fail(dev[0], i, "wpas_mesh_add_interface"):
979                 mesh = dev[0].request("MESH_INTERFACE_ADD").strip()
980         finally:
981             if mesh and mesh != "FAIL":
982                 dev[0].request("MESH_GROUP_REMOVE " + mesh)
983
984 def test_mesh_scan_oom(dev):
985     """wpa_supplicant mesh scan results and OOM"""
986     check_mesh_support(dev[0])
987     add_open_mesh_network(dev[0])
988     check_mesh_group_added(dev[0])
989     for i in range(5):
990         dev[1].scan(freq="2412")
991         res = dev[1].request("SCAN_RESULTS")
992         if "[MESH]" in res:
993             break
994     for r in res.splitlines():
995         if "[MESH]" in r:
996             break
997     bssid = r.split('\t')[0]
998
999     bss = dev[1].get_bss(bssid)
1000     if bss is None:
1001         raise Exception("Could not get BSS entry for mesh")
1002
1003     for i in range(1, 3):
1004         with alloc_fail(dev[1], i, "mesh_attr_text"):
1005             bss = dev[1].get_bss(bssid)
1006             if bss is not None:
1007                 raise Exception("Unexpected BSS result during OOM")