tests: bssid_blacklist and bssid_whitelist
[mech_eap.git] / tests / hwsim / test_ap_open.py
1 # Open mode AP tests
2 # Copyright (c) 2014, Qualcomm Atheros, 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 struct
10 import subprocess
11 import time
12
13 import hostapd
14 import hwsim_utils
15 from utils import alloc_fail
16
17 def test_ap_open(dev, apdev):
18     """AP with open mode (no security) configuration"""
19     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
20     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
21                    bg_scan_period="0")
22     ev = hapd.wait_event([ "AP-STA-CONNECTED" ], timeout=5)
23     if ev is None:
24         raise Exception("No connection event received from hostapd")
25     hwsim_utils.test_connectivity(dev[0], hapd)
26
27     dev[0].request("DISCONNECT")
28     ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=5)
29     if ev is None:
30         raise Exception("No disconnection event received from hostapd")
31
32 def test_ap_open_packet_loss(dev, apdev):
33     """AP with open mode configuration and large packet loss"""
34     params = { "ssid": "open",
35                "ignore_probe_probability": "0.5",
36                "ignore_auth_probability": "0.5",
37                "ignore_assoc_probability": "0.5",
38                "ignore_reassoc_probability": "0.5" }
39     hapd = hostapd.add_ap(apdev[0]['ifname'], params)
40     for i in range(0, 3):
41         dev[i].connect("open", key_mgmt="NONE", scan_freq="2412",
42                        wait_connect=False)
43     for i in range(0, 3):
44         dev[i].wait_connected(timeout=20)
45
46 def test_ap_open_unknown_action(dev, apdev):
47     """AP with open mode configuration and unknown Action frame"""
48     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
49     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
50     bssid = apdev[0]['bssid']
51     cmd = "MGMT_TX {} {} freq=2412 action=765432".format(bssid, bssid)
52     if "FAIL" in dev[0].request(cmd):
53         raise Exception("Could not send test Action frame")
54     ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
55     if ev is None:
56         raise Exception("Timeout on MGMT-TX-STATUS")
57     if "result=SUCCESS" not in ev:
58         raise Exception("AP did not ack Action frame")
59
60 def test_ap_open_reconnect_on_inactivity_disconnect(dev, apdev):
61     """Reconnect to open mode AP after inactivity related disconnection"""
62     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
63     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
64     hapd.request("DEAUTHENTICATE " + dev[0].p2p_interface_addr() + " reason=4")
65     dev[0].wait_disconnected(timeout=5)
66     dev[0].wait_connected(timeout=2, error="Timeout on reconnection")
67
68 def test_ap_open_assoc_timeout(dev, apdev):
69     """AP timing out association"""
70     ssid = "test"
71     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
72     dev[0].scan(freq="2412")
73     hapd.set("ext_mgmt_frame_handling", "1")
74     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
75                    wait_connect=False)
76     for i in range(0, 10):
77         req = hapd.mgmt_rx()
78         if req is None:
79             raise Exception("MGMT RX wait timed out")
80         if req['subtype'] == 11:
81             break
82         req = None
83     if not req:
84         raise Exception("Authentication frame not received")
85
86     resp = {}
87     resp['fc'] = req['fc']
88     resp['da'] = req['sa']
89     resp['sa'] = req['da']
90     resp['bssid'] = req['bssid']
91     resp['payload'] = struct.pack('<HHH', 0, 2, 0)
92     hapd.mgmt_tx(resp)
93
94     assoc = 0
95     for i in range(0, 10):
96         req = hapd.mgmt_rx()
97         if req is None:
98             raise Exception("MGMT RX wait timed out")
99         if req['subtype'] == 0:
100             assoc += 1
101             if assoc == 3:
102                 break
103     if assoc != 3:
104         raise Exception("Association Request frames not received: assoc=%d" % assoc)
105     hapd.set("ext_mgmt_frame_handling", "0")
106     dev[0].wait_connected(timeout=15)
107
108 def test_ap_open_id_str(dev, apdev):
109     """AP with open mode and id_str"""
110     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
111     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412", id_str="foo",
112                    wait_connect=False)
113     ev = dev[0].wait_connected(timeout=10)
114     if "id_str=foo" not in ev:
115         raise Exception("CTRL-EVENT-CONNECT did not have matching id_str: " + ev)
116     if dev[0].get_status_field("id_str") != "foo":
117         raise Exception("id_str mismatch")
118
119 def test_ap_open_select_any(dev, apdev):
120     """AP with open mode and select any network"""
121     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
122     id = dev[0].connect("unknown", key_mgmt="NONE", scan_freq="2412",
123                         only_add_network=True)
124     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
125                    only_add_network=True)
126     dev[0].select_network(id)
127     ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
128     if ev is not None:
129         raise Exception("Unexpected connection")
130
131     dev[0].select_network("any")
132     dev[0].wait_connected(timeout=10)
133
134 def test_ap_open_unexpected_assoc_event(dev, apdev):
135     """AP with open mode and unexpected association event"""
136     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
137     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
138     dev[0].request("DISCONNECT")
139     dev[0].wait_disconnected(timeout=15)
140     dev[0].dump_monitor()
141     # This will be accepted due to matching network
142     subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
143                      apdev[0]['bssid']])
144     dev[0].wait_connected(timeout=15)
145     dev[0].dump_monitor()
146
147     dev[0].request("REMOVE_NETWORK all")
148     dev[0].wait_disconnected(timeout=5)
149     dev[0].dump_monitor()
150     # This will result in disconnection due to no matching network
151     subprocess.call(['iw', 'dev', dev[0].ifname, 'connect', 'open', "2412",
152                      apdev[0]['bssid']])
153     dev[0].wait_disconnected(timeout=15)
154
155 def test_ap_bss_load(dev, apdev):
156     """AP with open mode (no security) configuration"""
157     hapd = hostapd.add_ap(apdev[0]['ifname'],
158                           { "ssid": "open",
159                             "bss_load_update_period": "10" })
160     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412")
161     # this does not really get much useful output with mac80211_hwsim currently,
162     # but run through the channel survey update couple of times
163     for i in range(0, 10):
164         hwsim_utils.test_connectivity(dev[0], hapd)
165         hwsim_utils.test_connectivity(dev[0], hapd)
166         hwsim_utils.test_connectivity(dev[0], hapd)
167         time.sleep(0.15)
168
169 def hapd_out_of_mem(hapd, apdev, count, func):
170     with alloc_fail(hapd, count, func):
171         started = False
172         try:
173             hostapd.add_ap(apdev['ifname'], { "ssid": "open" })
174             started = True
175         except:
176             pass
177         if started:
178             raise Exception("hostapd interface started even with memory allocation failure: " + arg)
179
180 def test_ap_open_out_of_memory(dev, apdev):
181     """hostapd failing to setup interface due to allocation failure"""
182     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
183     hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_alloc_bss_data")
184
185     for i in range(1, 3):
186         hapd_out_of_mem(hapd, apdev[1], i, "hostapd_iface_alloc")
187
188     for i in range(1, 5):
189         hapd_out_of_mem(hapd, apdev[1], i, "hostapd_config_defaults;hostapd_config_alloc")
190
191     hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_config_alloc")
192
193     hapd_out_of_mem(hapd, apdev[1], 1, "hostapd_driver_init")
194
195     for i in range(1, 4):
196         hapd_out_of_mem(hapd, apdev[1], i, "=wpa_driver_nl80211_drv_init")
197
198     # eloop_register_read_sock() call from i802_init()
199     hapd_out_of_mem(hapd, apdev[1], 1, "eloop_sock_table_add_sock;eloop_register_sock;?eloop_register_read_sock;=i802_init")
200
201     # verify that a new interface can still be added when memory allocation does
202     # not fail
203     hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
204
205 def test_bssid_black_white_list(dev, apdev):
206     """BSSID black/white list"""
207     hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
208     hapd2 = hostapd.add_ap(apdev[1]['ifname'], { "ssid": "open" })
209
210     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
211                    bssid_whitelist=apdev[1]['bssid'])
212     dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
213                    bssid_blacklist=apdev[1]['bssid'])
214     dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
215                    bssid_whitelist="00:00:00:00:00:00/00:00:00:00:00:00",
216                    bssid_blacklist=apdev[1]['bssid'])
217     if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
218         raise Exception("dev[0] connected to unexpected AP")
219     if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
220         raise Exception("dev[1] connected to unexpected AP")
221     if dev[2].get_status_field('bssid') != apdev[0]['bssid']:
222         raise Exception("dev[2] connected to unexpected AP")
223     dev[0].request("REMOVE_NETWORK all")
224     dev[1].request("REMOVE_NETWORK all")
225     dev[2].request("REMOVE_NETWORK all")
226
227     dev[2].connect("open", key_mgmt="NONE", scan_freq="2412",
228                    bssid_whitelist="00:00:00:00:00:00", wait_connect=False)
229     dev[0].connect("open", key_mgmt="NONE", scan_freq="2412",
230                    bssid_whitelist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
231     dev[1].connect("open", key_mgmt="NONE", scan_freq="2412",
232                    bssid_blacklist="11:22:33:44:55:66/ff:00:00:00:00:00 " + apdev[1]['bssid'] + " aa:bb:cc:dd:ee:ff")
233     if dev[0].get_status_field('bssid') != apdev[1]['bssid']:
234         raise Exception("dev[0] connected to unexpected AP")
235     if dev[1].get_status_field('bssid') != apdev[0]['bssid']:
236         raise Exception("dev[1] connected to unexpected AP")
237     dev[0].request("REMOVE_NETWORK all")
238     dev[1].request("REMOVE_NETWORK all")
239     ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED"], timeout=0.1)
240     if ev is not None:
241         raise Exception("Unexpected dev[2] connectin")
242     dev[2].request("REMOVE_NETWORK all")