Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / 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 tshark import run_tshark
17 from nl80211 import *
18
19 def nl80211_command(dev, cmd, attr):
20     res = dev.request("VENDOR ffffffff {} {}".format(nl80211_cmd[cmd],
21                                                      binascii.hexlify(attr)))
22     if "FAIL" in res:
23         raise Exception("nl80211 command failed")
24     return binascii.unhexlify(res)
25
26 def test_cfg80211_disassociate(dev, apdev):
27     """cfg80211 disassociation command"""
28     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
29     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
30     ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
31     if ev is None:
32         raise Exception("No connection event received from hostapd")
33
34     ifindex = int(dev[0].get_driver_status_field("ifindex"))
35     attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
36     attrs += build_nl80211_attr_u16('REASON_CODE', 1)
37     attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
38     nl80211_command(dev[0], 'DISASSOCIATE', attrs)
39
40     ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
41     if ev is None:
42         raise Exception("No disconnection event received from hostapd")
43
44 def nl80211_frame(dev, ifindex, frame, freq=None, duration=None, offchannel_tx_ok=False):
45     attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
46     if freq is not None:
47         attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
48     if duration is not None:
49         attrs += build_nl80211_attr_u32('DURATION', duration)
50     if offchannel_tx_ok:
51         attrs += build_nl80211_attr_flag('OFFCHANNEL_TX_OK')
52     attrs += build_nl80211_attr('FRAME', frame)
53     return parse_nl80211_attrs(nl80211_command(dev, 'FRAME', attrs))
54
55 def nl80211_frame_wait_cancel(dev, ifindex, cookie):
56     attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
57     attrs += build_nl80211_attr('COOKIE', cookie)
58     return nl80211_command(dev, 'FRAME_WAIT_CANCEL', attrs)
59
60 def nl80211_remain_on_channel(dev, ifindex, freq, duration):
61     attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
62     attrs += build_nl80211_attr_u32('WIPHY_FREQ', freq)
63     attrs += build_nl80211_attr_u32('DURATION', duration)
64     return nl80211_command(dev, 'REMAIN_ON_CHANNEL', attrs)
65
66 def test_cfg80211_tx_frame(dev, apdev, params):
67     """cfg80211 offchannel TX frame command"""
68     ifindex = int(dev[0].get_driver_status_field("ifindex"))
69
70     frame = binascii.unhexlify("d000000002000000010002000000000002000000010000000409506f9a090001dd5e506f9a0902020025080401001f0502006414060500585804510b0906000200000000000b1000585804510b0102030405060708090a0b0d1d000200000000000108000000000000000000101100084465766963652041110500585804510bdd190050f204104a0001101012000200011049000600372a000120")
71
72     dev[0].request("P2P_GROUP_ADD freq=2412")
73     res = nl80211_frame(dev[0], ifindex, frame, freq=2422, duration=500,
74                         offchannel_tx_ok=True)
75     time.sleep(0.1)
76
77     # note: Uncommenting this seems to remove the incorrect channel issue
78     #nl80211_frame_wait_cancel(dev[0], ifindex, res[nl80211_attr['COOKIE']])
79
80     # note: this Action frame ends up getting sent incorrectly on 2422 MHz
81     nl80211_frame(dev[0], ifindex, frame, freq=2412)
82     time.sleep(1.5)
83     # note: also the Deauthenticate frame sent by the GO going down ends up
84     # being transmitted incorrectly on 2422 MHz.
85
86     out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
87                      "wlan.fc.type_subtype == 13", ["radiotap.channel.freq"])
88     if out is not None:
89         freq = out.splitlines()
90         if len(freq) != 2:
91             raise Exception("Unexpected number of Action frames (%d)" % len(freq))
92         if freq[0] != "2422":
93             raise Exception("First Action frame on unexpected channel: %s MHz" % freq[0])
94         if freq[1] != "2412":
95             raise Exception("Second Action frame on unexpected channel: %s MHz" % freq[1])
96
97 def test_cfg80211_wep_key_idx_change(dev, apdev):
98     """WEP Shared Key authentication and key index change without deauth"""
99     hapd = hostapd.add_ap(apdev[0]['ifname'],
100                           { "ssid": "wep-shared-key",
101                             "wep_key0": '"hello12345678"',
102                             "wep_key1": '"other12345678"',
103                             "auth_algs": "2" })
104     id = dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
105                         wep_key0='"hello12345678"',
106                         wep_key1='"other12345678"',
107                         wep_tx_keyidx="0",
108                         scan_freq="2412")
109     hwsim_utils.test_connectivity(dev[0], hapd)
110
111     dev[0].set_network(id, "wep_tx_keyidx", "1")
112
113     # clear cfg80211 auth state to allow new auth without deauth frame
114     ifindex = int(dev[0].get_driver_status_field("ifindex"))
115     attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
116     attrs += build_nl80211_attr_u16('REASON_CODE', 1)
117     attrs += build_nl80211_attr_mac('MAC', apdev[0]['bssid'])
118     attrs += build_nl80211_attr_flag('LOCAL_STATE_CHANGE')
119     nl80211_command(dev[0], 'DEAUTHENTICATE', attrs)
120     dev[0].wait_disconnected(timeout=5, error="Local-deauth timed out")
121
122     # the previous command results in deauth event followed by auto-reconnect
123     dev[0].wait_connected(timeout=10, error="Reassociation timed out")
124     hwsim_utils.test_connectivity(dev[0], hapd)
125
126 def test_cfg80211_hostapd_ext_sta_remove(dev, apdev):
127     """cfg80211 DEL_STATION issued externally to hostapd"""
128     hapd = hostapd.add_ap(apdev[0]['ifname'],
129                           { "ssid": "open" })
130     id = dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
131
132     ifindex = int(hapd.get_driver_status_field("ifindex"))
133     attrs = build_nl80211_attr_u32('IFINDEX', ifindex)
134     attrs += build_nl80211_attr_u16('REASON_CODE', 1)
135     attrs += build_nl80211_attr_u8('MGMT_SUBTYPE', 12)
136     attrs += build_nl80211_attr_mac('MAC', dev[0].own_addr())
137     nl80211_command(hapd, 'DEL_STATION', attrs)
138
139     # Currently, hostapd ignores the NL80211_CMD_DEL_STATION event if
140     # drv->device_ap_sme == 0 (which is the case with mac80211_hwsim), so no
141     # further action happens here. If that event were to be used to remove the
142     # STA entry from hostapd even in device_ap_sme == 0 case, this test case
143     # could be extended to cover additional operations.