]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_cfg80211.py
tests: Add wait_connected() and wait_disconnected() helpers
[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 import hwsim_utils
16 from nl80211 import *
17
18 def nl80211_command(dev, cmd, attr):
19 res = dev.request("VENDOR ffffffff {} {}".format(nl80211_cmd[cmd],
20 binascii.hexlify(attr)))
21 if "FAIL" in res:
22 raise Exception("nl80211 command failed")
23 return binascii.unhexlify(res)
24
25 def test_cfg80211_disassociate(dev, apdev):
26 """cfg80211 disassociation command"""
27 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
28 dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
29 ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
30 if ev is None:
31 raise Exception("No connection event received from hostapd")
32
33 ifindex = int(dev[0].get_driver_status_field("ifindex"))
34 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
35 attrs += build_nl80211_attr_u16('REASON_CODE', 1)
36 attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
37 nl80211_command(dev[0], 'DISASSOCIATE', attrs)
38
39 ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
40 if ev is None:
41 raise Exception("No disconnection event received from hostapd")
42
43 def nl80211_frame(dev, ifindex, frame, freq=None, duration=None, offchannel_tx_ok=False):
44 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
45 if freq is not None:
46 attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
47 if duration is not None:
48 attrs += build_nl80211_attr_u32('DURATION', duration)
49 if offchannel_tx_ok:
50 attrs += build_nl80211_attr_flag('OFFCHANNEL_TX_OK')
51 attrs += build_nl80211_attr('FRAME', frame)
52 return parse_nl80211_attrs(nl80211_command(dev, 'FRAME', attrs))
53
54 def nl80211_frame_wait_cancel(dev, ifindex, cookie):
55 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
56 attrs += build_nl80211_attr('COOKIE', cookie)
57 return nl80211_command(dev, 'FRAME_WAIT_CANCEL', attrs)
58
59 def nl80211_remain_on_channel(dev, ifindex, freq, duration):
60 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
61 attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
62 attrs += build_nl80211_attr_u32('DURATION', duration)
63 return nl80211_command(dev, 'REMAIN_ON_CHANNEL', attrs)
64
65 def test_cfg80211_tx_frame(dev, apdev, params):
66 """cfg80211 offchannel TX frame command"""
67 ifindex = int(dev[0].get_driver_status_field("ifindex"))
68
69 frame = binascii.unhexlify("d000000002000000010002000000000002000000010000000409506f9a090001dd5e506f9a0902020025080401001f0502006414060500585804510b0906000200000000000b1000585804510b0102030405060708090a0b0d1d000200000000000108000000000000000000101100084465766963652041110500585804510bdd190050f204104a0001101012000200011049000600372a000120")
70
71 dev[0].request("P2P_GROUP_ADD freq=2412")
72 res = nl80211_frame(dev[0], ifindex, frame, freq=2422, duration=500,
73 offchannel_tx_ok=True)
74 time.sleep(0.1)
75
76 # note: Uncommenting this seems to remove the incorrect channel issue
77 #nl80211_frame_wait_cancel(dev[0], ifindex, res[nl80211_attr['COOKIE']])
78
79 # note: this Action frame ends up getting sent incorrectly on 2422 MHz
80 nl80211_frame(dev[0], ifindex, frame, freq=2412)
81 time.sleep(1.5)
82 # note: also the Deauthenticate frame sent by the GO going down ends up
83 # being transmitted incorrectly on 2422 MHz.
84
85 try:
86 arg = [ "tshark",
87 "-r", os.path.join(params['logdir'], "hwsim0.pcapng"),
88 "-Y", "wlan.fc.type_subtype == 13",
89 "-Tfields", "-e", "radiotap.channel.freq" ]
90 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
91 stderr=open('/dev/null', 'w'))
92 except Exception, e:
93 logger.info("Could run run tshark check: " + str(e))
94 cmd = None
95 pass
96
97 if cmd:
98 freq = cmd.stdout.read().splitlines()
99 if len(freq) != 2:
100 raise Exception("Unexpected number of Action frames (%d)" % len(freq))
101 if freq[0] != "2422":
102 raise Exception("First Action frame on unexpected channel: %s MHz" % freq[0])
103 if freq[1] != "2412":
104 raise Exception("Second Action frame on unexpected channel: %s MHz" % freq[1])
105
106 def test_cfg80211_wep_key_idx_change(dev, apdev):
107 """WEP Shared Key authentication and key index change without deauth"""
108 hapd = hostapd.add_ap(apdev[0]['ifname'],
109 { "ssid": "wep-shared-key",
110 "wep_key0": '"hello12345678"',
111 "wep_key1": '"other12345678"',
112 "auth_algs": "2" })
113 id = dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
114 wep_key0='"hello12345678"',
115 wep_key1='"other12345678"',
116 wep_tx_keyidx="0",
117 scan_freq="2412")
118 hwsim_utils.test_connectivity(dev[0], hapd)
119
120 dev[0].set_network(id, "wep_tx_keyidx", "1")
121
122 # clear cfg80211 auth state to allow new auth without deauth frame
123 ifindex = int(dev[0].get_driver_status_field("ifindex"))
124 attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
125 attrs += build_nl80211_attr_u16('REASON_CODE', 1)
126 attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
127 attrs += build_nl80211_attr_flag('LOCAL_STATE_CHANGE')
128 nl80211_command(dev[0], 'DEAUTHENTICATE', attrs)
129 dev[0].wait_disconnected(timeout=5, error="Local-deauth timed out")
130
131 # the previous command results in deauth event followed by auto-reconnect
132 dev[0].wait_connected(timeout=10, error="Reassociation timed out")
133 hwsim_utils.test_connectivity(dev[0], hapd)