]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_cfg80211.py
tests: cfg80211 offchannel TX vs. operating channel
[thirdparty/hostap.git] / tests / hwsim / test_cfg80211.py
1 # cfg80211 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 binascii
10 import os
11 import subprocess
12 import time
13
14 import hostapd
15 from nl80211 import *
16
17 def nl80211_command(dev, cmd, attr):
18 res = dev.request("VENDOR ffffffff {} {}".format(nl80211_cmd[cmd],
19 binascii.hexlify(attr)))
20 if "FAIL" in res:
21 raise Exception("nl80211 command failed")
22 return binascii.unhexlify(res)
23
24 def test_cfg80211_disassociate(dev, apdev):
25 """cfg80211 disassociation command"""
26 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
27 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
28 ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
29 if ev is None:
30 raise Exception("No connection event received from hostapd")
31
32 ifindex = int(dev[0].get_driver_status_field("ifindex"))
33 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
34 attrs += build_nl80211_attr_u16('REASON_CODE', 1)
35 attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
36 nl80211_command(dev[0], 'DISASSOCIATE', attrs)
37
38 ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
39 if ev is None:
40 raise Exception("No disconnection event received from hostapd")
41
42 def nl80211_frame(dev, ifindex, frame, freq=None, duration=None, offchannel_tx_ok=False):
43 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
44 if freq is not None:
45 attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
46 if duration is not None:
47 attrs += build_nl80211_attr_u32('DURATION', duration)
48 if offchannel_tx_ok:
49 attrs += build_nl80211_attr_flag('OFFCHANNEL_TX_OK')
50 attrs += build_nl80211_attr('FRAME', frame)
51 return parse_nl80211_attrs(nl80211_command(dev, 'FRAME', attrs))
52
53 def nl80211_frame_wait_cancel(dev, ifindex, cookie):
54 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
55 attrs += build_nl80211_attr('COOKIE', cookie)
56 return nl80211_command(dev, 'FRAME_WAIT_CANCEL', attrs)
57
58 def nl80211_remain_on_channel(dev, ifindex, freq, duration):
59 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
60 attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
61 attrs += build_nl80211_attr_u32('DURATION', duration)
62 return nl80211_command(dev, 'REMAIN_ON_CHANNEL', attrs)
63
64 def test_cfg80211_tx_frame(dev, apdev, params):
65 """cfg80211 offchannel TX frame command"""
66 ifindex = int(dev[0].get_driver_status_field("ifindex"))
67
68 frame = binascii.unhexlify("d000000002000000010002000000000002000000010000000409506f9a090001dd5e506f9a0902020025080401001f0502006414060500585804510b0906000200000000000b1000585804510b0102030405060708090a0b0d1d000200000000000108000000000000000000101100084465766963652041110500585804510bdd190050f204104a0001101012000200011049000600372a000120")
69
70 dev[0].request("P2P_GROUP_ADD freq=2412")
71 res = nl80211_frame(dev[0], ifindex, frame, freq=2422, duration=500,
72 offchannel_tx_ok=True)
73 time.sleep(0.1)
74
75 # note: Uncommenting this seems to remove the incorrect channel issue
76 #nl80211_frame_wait_cancel(dev[0], ifindex, res[nl80211_attr['COOKIE']])
77
78 # note: this Action frame ends up getting sent incorrectly on 2422 MHz
79 nl80211_frame(dev[0], ifindex, frame, freq=2412)
80 time.sleep(1.5)
81 # note: also the Deauthenticate frame sent by the GO going down ends up
82 # being transmitted incorrectly on 2422 MHz.
83
84 try:
85 arg = [ "tshark",
86 "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
87 "-R", "wlan.fc.type_subtype == 13",
88 "-Tfields", "-e", "radiotap.channel.freq" ]
89 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
90 stderr=open('/dev/null', 'w'))
91 except Exception, e:
92 logger.info("Could run run tshark check: " + str(e))
93 cmd = None
94 pass
95
96 if cmd:
97 freq = cmd.stdout.read().splitlines()
98 if len(freq) != 2:
99 raise Exception("Unexpected number of Action frames (%d)" % len(freq))
100 if freq[0] != "2422":
101 raise Exception("First Action frame on unexpected channel: %s MHz" % freq[0])
102 if freq[1] != "2412":
103 raise Exception("Second Action frame on unexpected channel: %s MHz" % freq[1])