tests: Create new radios for MCC tests
[mech_eap.git] / tests / hwsim / test_p2p_channel.py
1 # P2P channel selection test cases
2 # Copyright (c) 2014, Jouni Malinen <j@w1.fi>
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 hostapd
14 import hwsim_utils
15 from wpasupplicant import WpaSupplicant
16 from hwsim import HWSimRadio
17 from test_p2p_grpform import go_neg_pin_authorized
18 from test_p2p_grpform import check_grpform_results
19 from test_p2p_grpform import remove_group
20 from test_p2p_grpform import go_neg_pbc
21 from test_p2p_autogo import autogo
22
23 def set_country(country):
24     subprocess.call(['sudo', 'iw', 'reg', 'set', country])
25     time.sleep(0.1)
26
27 def test_p2p_channel_5ghz(dev):
28     """P2P group formation with 5 GHz preference"""
29     try:
30         set_country("US")
31         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
32                                                r_dev=dev[1], r_intent=0,
33                                                test_data=False)
34         check_grpform_results(i_res, r_res)
35         freq = int(i_res['freq'])
36         if freq < 5000:
37             raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
38         remove_group(dev[0], dev[1])
39     finally:
40         set_country("00")
41
42 def test_p2p_channel_5ghz_no_vht(dev):
43     """P2P group formation with 5 GHz preference when VHT channels are disallowed"""
44     try:
45         set_country("US")
46         dev[0].request("P2P_SET disallow_freq 5180-5240")
47         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
48                                                r_dev=dev[1], r_intent=0,
49                                                test_data=False)
50         check_grpform_results(i_res, r_res)
51         freq = int(i_res['freq'])
52         if freq < 5000:
53             raise Exception("Unexpected channel %d MHz - did not follow 5 GHz preference" % freq)
54         remove_group(dev[0], dev[1])
55     finally:
56         set_country("00")
57         dev[0].request("P2P_SET disallow_freq ")
58
59 def test_p2p_channel_random_social(dev):
60     """P2P group formation with 5 GHz preference but all 5 GHz channels disabled"""
61     try:
62         set_country("US")
63         dev[0].request("SET p2p_oper_channel 11")
64         dev[0].request("P2P_SET disallow_freq 5000-6000,2462")
65         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
66                                                r_dev=dev[1], r_intent=0,
67                                                test_data=False)
68         check_grpform_results(i_res, r_res)
69         freq = int(i_res['freq'])
70         if freq not in [ 2412, 2437, 2462 ]:
71             raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
72         remove_group(dev[0], dev[1])
73     finally:
74         set_country("00")
75         dev[0].request("P2P_SET disallow_freq ")
76
77 def test_p2p_channel_random(dev):
78     """P2P group formation with 5 GHz preference but all 5 GHz channels and all social channels disabled"""
79     try:
80         set_country("US")
81         dev[0].request("SET p2p_oper_channel 11")
82         dev[0].request("P2P_SET disallow_freq 5000-6000,2412,2437,2462")
83         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
84                                                r_dev=dev[1], r_intent=0,
85                                                test_data=False)
86         check_grpform_results(i_res, r_res)
87         freq = int(i_res['freq'])
88         if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
89             raise Exception("Unexpected channel %d MHz" % freq)
90         remove_group(dev[0], dev[1])
91     finally:
92         set_country("00")
93         dev[0].request("P2P_SET disallow_freq ")
94
95 def test_p2p_channel_random_social_with_op_class_change(dev, apdev, params):
96     """P2P group formation using random social channel with oper class change needed"""
97     try:
98         set_country("US")
99         logger.info("Start group on 5 GHz")
100         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
101                                                r_dev=dev[1], r_intent=0,
102                                                test_data=False)
103         check_grpform_results(i_res, r_res)
104         freq = int(i_res['freq'])
105         if freq < 5000:
106             raise Exception("Unexpected channel %d MHz - did not pick 5 GHz preference" % freq)
107         remove_group(dev[0], dev[1])
108
109         logger.info("Disable 5 GHz and try to re-start group based on 5 GHz preference")
110         dev[0].request("SET p2p_oper_reg_class 115")
111         dev[0].request("SET p2p_oper_channel 36")
112         dev[0].request("P2P_SET disallow_freq 5000-6000")
113         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
114                                                r_dev=dev[1], r_intent=0,
115                                                test_data=False)
116         check_grpform_results(i_res, r_res)
117         freq = int(i_res['freq'])
118         if freq not in [ 2412, 2437, 2462 ]:
119             raise Exception("Unexpected channel %d MHz - did not pick random social channel" % freq)
120         remove_group(dev[0], dev[1])
121
122         try:
123             arg = [ "tshark",
124                     "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
125                     "-Y", "wifi_p2p.public_action.subtype == 0",
126                     "-V" ]
127             cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
128                                    stderr=open('/dev/null', 'w'))
129         except Exception, e:
130             logger.info("Could run run tshark check: " + str(e))
131             cmd = None
132             pass
133
134         if cmd:
135             last = None
136             for l in cmd.stdout.read().splitlines():
137                 if "Operating Channel:" not in l:
138                     continue
139                 last = l
140             if last is None:
141                 raise Exception("Could not find GO Negotiation Request")
142             if "Operating Class 81" not in last:
143                 raise Exception("Unexpected operating class: " + last.strip())
144     finally:
145         set_country("00")
146         dev[0].request("P2P_SET disallow_freq ")
147         dev[0].request("SET p2p_oper_reg_class 81")
148         dev[0].request("SET p2p_oper_channel 11")
149
150 def test_p2p_channel_avoid(dev):
151     """P2P and avoid frequencies driver event"""
152     try:
153         set_country("US")
154         if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES 5000-6000,2412,2437,2462"):
155             raise Exception("Could not simulate driver event")
156         ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
157         if ev is None:
158             raise Exception("No CTRL-EVENT-AVOID-FREQ event")
159         [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
160                                                r_dev=dev[1], r_intent=0,
161                                                test_data=False)
162         check_grpform_results(i_res, r_res)
163         freq = int(i_res['freq'])
164         if freq > 2500 or freq in [ 2412, 2437, 2462 ]:
165             raise Exception("Unexpected channel %d MHz" % freq)
166
167         if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES"):
168             raise Exception("Could not simulate driver event(2)")
169         ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
170         if ev is None:
171             raise Exception("No CTRL-EVENT-AVOID-FREQ event")
172         ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=1)
173         if ev is not None:
174             raise Exception("Unexpected P2P-REMOVE-AND-REFORM-GROUP event")
175
176         if "OK" not in dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES " + str(freq)):
177             raise Exception("Could not simulate driver event(3)")
178         ev = dev[0].wait_event(["CTRL-EVENT-AVOID-FREQ"], timeout=10)
179         if ev is None:
180             raise Exception("No CTRL-EVENT-AVOID-FREQ event")
181         ev = dev[0].wait_event(["P2P-REMOVE-AND-REFORM-GROUP"], timeout=10)
182         if ev is None:
183             raise Exception("No P2P-REMOVE-AND-REFORM-GROUP event")
184     finally:
185         set_country("00")
186         dev[0].request("DRIVER_EVENT AVOID_FREQUENCIES")
187
188 def test_autogo_following_bss(dev, apdev):
189     """P2P autonomous GO operate on the same channel as station interface"""
190     if dev[0].get_mcc() > 1:
191         logger.info("test mode: MCC")
192
193     dev[0].request("SET p2p_no_group_iface 0")
194
195     channels = { 3 : "2422", 5 : "2432", 9 : "2452" }
196     for key in channels:
197         hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid" : 'ap-test',
198                                                     "channel" : str(key) })
199         dev[0].connect("ap-test", key_mgmt="NONE",
200                        scan_freq=str(channels[key]))
201         res_go = autogo(dev[0])
202         if res_go['freq'] != channels[key]:
203             raise Exception("Group operation channel is not the same as on connected station interface")
204         hwsim_utils.test_connectivity(dev[0], hapd)
205         dev[0].remove_group(res_go['ifname'])
206
207 def test_go_neg_with_bss_connected(dev, apdev):
208     """P2P channel selection: GO negotiation when station interface is connected"""
209
210     dev[0].request("SET p2p_no_group_iface 0")
211
212     hapd = hostapd.add_ap(apdev[0]['ifname'],
213                           { "ssid": 'bss-2.4ghz', "channel": '5' })
214     dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2432")
215     #dev[0] as GO
216     [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=10, r_dev=dev[1],
217                                 r_intent=1)
218     check_grpform_results(i_res, r_res)
219     if i_res['role'] != "GO":
220        raise Exception("GO not selected according to go_intent")
221     if i_res['freq'] != "2432":
222        raise Exception("Group formed on a different frequency than BSS")
223     hwsim_utils.test_connectivity(dev[0], hapd)
224     dev[0].remove_group(i_res['ifname'])
225
226     if dev[0].get_mcc() > 1:
227         logger.info("Skip as-client case due to MCC being enabled")
228         return;
229
230     #dev[0] as client
231     [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=1, r_dev=dev[1],
232                                   r_intent=10)
233     check_grpform_results(i_res2, r_res2)
234     if i_res2['role'] != "client":
235        raise Exception("GO not selected according to go_intent")
236     if i_res2['freq'] != "2432":
237        raise Exception("Group formed on a different frequency than BSS")
238     hwsim_utils.test_connectivity(dev[0], hapd)
239
240 def test_autogo_with_bss_on_disallowed_chan(dev, apdev):
241     """P2P channel selection: Autonomous GO with BSS on a disallowed channel"""
242
243     with HWSimRadio(n_channels=2) as (radio, iface):
244         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
245         wpas.interface_add(iface)
246
247         wpas.request("SET p2p_no_group_iface 0")
248
249         if wpas.get_mcc() < 2:
250            raise Exception("New radio does not support MCC")
251
252         try:
253             hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
254                                                         "channel": '1' })
255             wpas.request("P2P_SET disallow_freq 2412")
256             wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
257             res = autogo(wpas)
258             if res['freq'] == "2412":
259                raise Exception("GO set on a disallowed channel")
260             hwsim_utils.test_connectivity(wpas, hapd)
261         finally:
262             wpas.request("P2P_SET disallow_freq ")
263
264 def test_go_neg_with_bss_on_disallowed_chan(dev, apdev):
265     """P2P channel selection: GO negotiation with station interface on a disallowed channel"""
266
267     with HWSimRadio(n_channels=2) as (radio, iface):
268         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
269         wpas.interface_add(iface)
270
271         wpas.request("SET p2p_no_group_iface 0")
272
273         if wpas.get_mcc() < 2:
274            raise Exception("New radio does not support MCC")
275
276         try:
277             hapd = hostapd.add_ap(apdev[0]['ifname'],
278                                   { "ssid": 'bss-2.4ghz', "channel": '1' })
279             wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
280             wpas.request("P2P_SET disallow_freq 2412")
281
282             #wpas as GO
283             [i_res, r_res] = go_neg_pbc(i_dev=wpas, i_intent=10, r_dev=dev[1],
284                                         r_intent=1)
285             check_grpform_results(i_res, r_res)
286             if i_res['role'] != "GO":
287                raise Exception("GO not selected according to go_intent")
288             if i_res['freq'] == "2412":
289                raise Exception("Group formed on a disallowed channel")
290             hwsim_utils.test_connectivity(wpas, hapd)
291             wpas.remove_group(i_res['ifname'])
292
293             #wpas as client
294             [i_res2, r_res2] = go_neg_pbc(i_dev=wpas, i_intent=1, r_dev=dev[1],
295                                           r_intent=10)
296             check_grpform_results(i_res2, r_res2)
297             if i_res2['role'] != "client":
298                raise Exception("GO not selected according to go_intent")
299             if i_res2['freq'] == "2412":
300                raise Exception("Group formed on a disallowed channel")
301             hwsim_utils.test_connectivity(wpas, hapd)
302         finally:
303             wpas.request("P2P_SET disallow_freq ")
304
305 def test_autogo_force_diff_channel(dev, apdev):
306     """P2P autonomous GO and station interface operate on different channels"""
307     with HWSimRadio(n_channels=2) as (radio, iface):
308         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
309         wpas.interface_add(iface)
310
311         if wpas.get_mcc() < 2:
312            raise Exception("New radio does not support MCC")
313
314         wpas.request("SET p2p_no_group_iface 0")
315
316         hapd = hostapd.add_ap(apdev[0]['ifname'],
317                               {"ssid" : 'ap-test', "channel" : '1'})
318         wpas.connect("ap-test", key_mgmt = "NONE", scan_freq = "2412")
319         channels = { 2 : 2417, 5 : 2432, 9 : 2452 }
320         for key in channels:
321             res_go = autogo(wpas, channels[key])
322             hwsim_utils.test_connectivity(wpas, hapd)
323             if int(res_go['freq']) == 2412:
324                 raise Exception("Group operation channel is: 2412 excepted: " + res_go['freq'])
325             wpas.remove_group(res_go['ifname'])
326
327 def test_go_neg_forced_freq_diff_than_bss_freq(dev, apdev):
328     """P2P channel selection: GO negotiation with forced freq different than station interface"""
329     with HWSimRadio(n_channels=2) as (radio, iface):
330         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
331         wpas.interface_add(iface)
332
333         if wpas.get_mcc() < 2:
334            raise Exception("New radio does not support MCC")
335
336         wpas.request("SET p2p_no_group_iface 0")
337
338         hapd = hostapd.add_ap(apdev[0]['ifname'],
339                               { "country_code": 'US',
340                                 "ssid": 'bss-5ghz', "hw_mode": 'a',
341                                 "channel": '40' })
342         wpas.connect("bss-5ghz", key_mgmt="NONE", scan_freq="5200")
343
344         # GO and peer force the same freq, different than BSS freq,
345         # wpas to become GO
346         [i_res, r_res] = go_neg_pbc(i_dev=dev[1], i_intent=1, i_freq=5180,
347                                     r_dev=wpas, r_intent=14, r_freq=5180)
348         check_grpform_results(i_res, r_res)
349         if i_res['freq'] != "5180":
350            raise Exception("P2P group formed on unexpected frequency: " + i_res['freq'])
351         if r_res['role'] != "GO":
352            raise Exception("GO not selected according to go_intent")
353         hwsim_utils.test_connectivity(wpas, hapd)
354         wpas.remove_group(r_res['ifname'])
355
356         # GO and peer force the same freq, different than BSS freq, wpas to
357         # become client
358         [i_res2, r_res2] = go_neg_pbc(i_dev=dev[1], i_intent=14, i_freq=2422,
359                                       r_dev=wpas, r_intent=1, r_freq=2422)
360         check_grpform_results(i_res2, r_res2)
361         if i_res2['freq'] != "2422":
362            raise Exception("P2P group formed on unexpected frequency: " + i_res2['freq'])
363         if r_res2['role'] != "client":
364            raise Exception("GO not selected according to go_intent")
365         hwsim_utils.test_connectivity(wpas, hapd)
366
367 def test_go_pref_chan_bss_on_diff_chan(dev, apdev):
368     """P2P channel selection: Station on different channel than GO configured pref channel"""
369
370     dev[0].request("SET p2p_no_group_iface 0")
371
372     try:
373         hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
374                                                     "channel": '1' })
375         dev[0].request("SET p2p_pref_chan 81:2")
376         dev[0].connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
377         res = autogo(dev[0])
378         if res['freq'] != "2412":
379            raise Exception("GO channel did not follow BSS")
380         hwsim_utils.test_connectivity(dev[0], hapd)
381     finally:
382         dev[0].request("SET p2p_pref_chan ")
383
384 def test_go_pref_chan_bss_on_disallowed_chan(dev, apdev):
385     """P2P channel selection: Station interface on different channel than GO configured pref channel, and station channel is disallowed"""
386     with HWSimRadio(n_channels=2) as (radio, iface):
387         wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
388         wpas.interface_add(iface)
389
390         if wpas.get_mcc() < 2:
391            raise Exception("New radio does not support MCC")
392
393         wpas.request("SET p2p_no_group_iface 0")
394
395         try:
396             hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": 'bss-2.4ghz',
397                                                         "channel": '1' })
398             wpas.request("P2P_SET disallow_freq 2412")
399             wpas.request("SET p2p_pref_chan 81:2")
400             wpas.connect("bss-2.4ghz", key_mgmt="NONE", scan_freq="2412")
401             res2 = autogo(wpas)
402             if res2['freq'] != "2417":
403                raise Exception("GO channel did not follow pref_chan configuration")
404             hwsim_utils.test_connectivity(wpas, hapd)
405         finally:
406             wpas.request("P2P_SET disallow_freq ")
407             wpas.request("SET p2p_pref_chan ")
408
409 def test_no_go_freq(dev, apdev):
410     """P2P channel selection: no GO freq"""
411     try:
412        dev[0].request("SET p2p_no_go_freq 2412")
413        # dev[0] as client, channel 1 is ok
414        [i_res, r_res] = go_neg_pbc(i_dev=dev[0], i_intent=1,
415                                    r_dev=dev[1], r_intent=14, r_freq=2412)
416        check_grpform_results(i_res, r_res)
417        if i_res['freq'] != "2412":
418           raise Exception("P2P group not formed on forced freq")
419
420        dev[1].remove_group(r_res['ifname'])
421        fail = False
422        # dev[0] as GO, channel 1 is not allowed
423        try:
424           dev[0].request("SET p2p_no_go_freq 2412")
425           [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14,
426                                         r_dev=dev[1], r_intent=1, r_freq=2412)
427           check_grpform_results(i_res2, r_res2)
428           fail = True
429        except:
430            pass
431        if fail:
432            raise Exception("GO set on a disallowed freq")
433     finally:
434        dev[0].request("SET p2p_no_go_freq ")
435
436 def test_go_neg_peers_force_diff_freq(dev, apdev):
437     try:
438        [i_res2, r_res2] = go_neg_pbc(i_dev=dev[0], i_intent=14, i_freq=5180,
439                                      r_dev=dev[1], r_intent=0, r_freq=5200)
440     except Exception, e:
441         return
442     raise Exception("Unexpected group formation success")
443
444 def test_autogo_random_channel(dev, apdev):
445     """P2P channel selection: GO instantiated on random channel 1, 6, 11"""
446     freqs = []
447     go_freqs = ["2412", "2437", "2462"]
448     for i in range(0, 20):
449         result = autogo(dev[0])
450         if result['freq'] not in go_freqs:
451            raise Exception("Unexpected frequency selected: " + result['freq'])
452         if result['freq'] not in freqs:
453             freqs.append(result['freq'])
454         if len(freqs) == 3:
455             break
456         dev[0].remove_group(result['ifname'])
457     if i == 20:
458        raise Exception("GO created 20 times and not all social channels were selected. freqs not selected: " + str(list(set(go_freqs) - set(freqs))))
459
460 def test_p2p_autogo_pref_chan_disallowed(dev, apdev):
461     """P2P channel selection: GO preferred channels are disallowed"""
462     try:
463        dev[0].request("SET p2p_pref_chan 81:1,81:3,81:6,81:9,81:11")
464        dev[0].request("P2P_SET disallow_freq 2412,2422,2437,2452,2462")
465        for i in range(0, 5):
466            res = autogo(dev[0])
467            if res['freq'] in [ "2412", "2422", "2437", "2452", "2462" ]:
468                raise Exception("GO channel is disallowed")
469            dev[0].remove_group(res['ifname'])
470     finally:
471        dev[0].request("P2P_SET disallow_freq ")
472        dev[0].request("SET p2p_pref_chan ")
473
474 def test_p2p_autogo_pref_chan_not_in_regulatory(dev, apdev):
475     """P2P channel selection: GO preferred channel not allowed in the regulatory rules"""
476     try:
477         set_country("US")
478         dev[0].request("SET p2p_pref_chan 124:149")
479         res = autogo(dev[0], persistent=True)
480         if res['freq'] != "5745":
481             raise Exception("Unexpected channel selected: " + res['freq'])
482         dev[0].remove_group(res['ifname'])
483
484         netw = dev[0].list_networks()
485         if len(netw) != 1:
486             raise Exception("Unexpected number of network blocks: " + str(netw))
487         id = netw[0]['id']
488
489         set_country("DE")
490         res = autogo(dev[0], persistent=id)
491         if res['freq'] == "5745":
492             raise Exception("Unexpected channel selected(2): " + res['freq'])
493         dev[0].remove_group(res['ifname'])
494     finally:
495         dev[0].request("SET p2p_pref_chan ")
496         set_country("00")