2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
7 from remotehost import remote_compatible
9 logger = logging.getLogger()
15 from utils import alloc_fail
17 def connect_ibss_cmd(dev, id, freq=2412):
19 dev.select_network(id, freq=str(freq))
21 def wait_ibss_connection(dev):
22 logger.info(dev.ifname + " waiting for IBSS start/join to complete")
23 ev = dev.wait_connected(timeout=20,
24 error="Connection to the IBSS timed out")
25 exp = r'<.>(CTRL-EVENT-CONNECTED) - Connection to ([0-9a-f:]*) completed.*'
31 def wait_4way_handshake(dev1, dev2):
32 logger.info(dev1.ifname + " waiting for 4-way handshake completion with " + dev2.ifname + " " + dev2.p2p_interface_addr())
33 ev = dev1.wait_event(["IBSS-RSN-COMPLETED " + dev2.p2p_interface_addr()],
36 raise Exception("4-way handshake in IBSS timed out")
38 def wait_4way_handshake2(dev1, dev2, dev3):
39 logger.info(dev1.ifname + " waiting for 4-way handshake completion with " + dev2.ifname + " " + dev2.p2p_interface_addr() + " and " + dev3.p2p_interface_addr())
40 ev = dev1.wait_event(["IBSS-RSN-COMPLETED " + dev2.p2p_interface_addr(),
41 "IBSS-RSN-COMPLETED " + dev3.p2p_interface_addr()],
44 raise Exception("4-way handshake in IBSS timed out")
45 ev = dev1.wait_event(["IBSS-RSN-COMPLETED " + dev2.p2p_interface_addr(),
46 "IBSS-RSN-COMPLETED " + dev3.p2p_interface_addr()],
49 raise Exception("4-way handshake in IBSS timed out")
51 def add_ibss(dev, ssid, psk=None, proto=None, key_mgmt=None, pairwise=None,
52 group=None, beacon_int=None, bssid=None, scan_freq=None,
53 wep_key0=None, freq=2412, chwidth=0):
54 id = dev.add_network()
55 dev.set_network(id, "mode", "1")
56 dev.set_network(id, "frequency", str(freq))
58 dev.set_network(id, "max_oper_chwidth", str(chwidth))
60 dev.set_network(id, "scan_freq", str(scan_freq))
61 dev.set_network_quoted(id, "ssid", ssid)
63 dev.set_network_quoted(id, "psk", psk)
65 dev.set_network(id, "proto", proto)
67 dev.set_network(id, "key_mgmt", key_mgmt)
69 dev.set_network(id, "pairwise", pairwise)
71 dev.set_network(id, "group", group)
73 dev.set_network(id, "beacon_int", beacon_int)
75 dev.set_network(id, "bssid", bssid)
77 dev.set_network(id, "wep_key0", wep_key0)
78 dev.request("ENABLE_NETWORK " + str(id) + " no-connect")
81 def add_ibss_rsn(dev, ssid):
82 return add_ibss(dev, ssid, "12345678", "RSN", "WPA-PSK", "CCMP", "CCMP")
84 def add_ibss_rsn_tkip(dev, ssid):
85 return add_ibss(dev, ssid, "12345678", "RSN", "WPA-PSK", "TKIP", "TKIP")
87 def add_ibss_wpa_none(dev, ssid):
88 return add_ibss(dev, ssid, "12345678", "WPA", "WPA-NONE", "TKIP", "TKIP")
90 def add_ibss_wpa_none_ccmp(dev, ssid):
91 return add_ibss(dev, ssid, "12345678", "WPA", "WPA-NONE", "CCMP", "CCMP")
93 def test_ibss_rsn(dev):
97 logger.info("Start IBSS on the first STA")
98 id = add_ibss_rsn(dev[0], ssid)
99 connect_ibss_cmd(dev[0], id)
100 bssid0 = wait_ibss_connection(dev[0])
102 logger.info("Join two STAs to the IBSS")
104 id = add_ibss_rsn(dev[1], ssid)
105 connect_ibss_cmd(dev[1], id)
106 bssid1 = wait_ibss_connection(dev[1])
108 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
109 # try to merge with a scan
111 wait_4way_handshake(dev[0], dev[1])
112 wait_4way_handshake(dev[1], dev[0])
114 id = add_ibss_rsn(dev[2], ssid)
115 connect_ibss_cmd(dev[2], id)
116 bssid2 = wait_ibss_connection(dev[2])
118 logger.info("STA0 BSSID " + bssid0 + " differs from STA2 BSSID " + bssid2)
119 # try to merge with a scan
121 wait_4way_handshake(dev[0], dev[2])
122 wait_4way_handshake2(dev[2], dev[0], dev[1])
124 # Allow some time for all peers to complete key setup
126 hwsim_utils.test_connectivity(dev[0], dev[1])
127 hwsim_utils.test_connectivity(dev[0], dev[2])
128 hwsim_utils.test_connectivity(dev[1], dev[2])
130 dev[1].request("REMOVE_NETWORK all")
132 id = add_ibss_rsn(dev[1], ssid)
133 connect_ibss_cmd(dev[1], id)
134 bssid1 = wait_ibss_connection(dev[1])
136 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
137 # try to merge with a scan
139 wait_4way_handshake(dev[0], dev[1])
140 wait_4way_handshake(dev[1], dev[0])
142 hwsim_utils.test_connectivity(dev[0], dev[1])
144 if "OK" not in dev[0].request("IBSS_RSN " + dev[1].p2p_interface_addr()):
145 raise Exception("IBSS_RSN command failed")
147 def test_ibss_wpa_none(dev):
151 logger.info("Start IBSS on the first STA")
152 id = add_ibss_wpa_none(dev[0], ssid)
153 connect_ibss_cmd(dev[0], id)
154 bssid0 = wait_ibss_connection(dev[0])
156 # This is a bit ugly, but no one really cares about WPA-None, so there may
157 # not be enough justification to clean this up.. For now, wpa_supplicant
158 # will show two connection events with mac80211_hwsim where the first one
159 # comes with all zeros address.
160 if bssid0 == "00:00:00:00:00:00":
161 logger.info("Waiting for real BSSID on the first STA")
162 bssid0 = wait_ibss_connection(dev[0])
164 logger.info("Join two STAs to the IBSS")
166 id = add_ibss_wpa_none(dev[1], ssid)
167 connect_ibss_cmd(dev[1], id)
168 id = add_ibss_wpa_none(dev[2], ssid)
169 connect_ibss_cmd(dev[2], id)
171 bssid1 = wait_ibss_connection(dev[1])
173 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
174 bssid1 = wait_ibss_connection(dev[1])
176 bssid2 = wait_ibss_connection(dev[2])
178 logger.info("STA0 BSSID " + bssid0 + " differs from STA2 BSSID " + bssid2)
179 bssid2 = wait_ibss_connection(dev[2])
181 logger.info("bssid0=%s bssid1=%s bssid2=%s" % (bssid0, bssid1, bssid2))
183 bss = dev[0].get_bss(bssid0)
185 bss = dev[1].get_bss(bssid1)
187 raise Exception("Could not find BSS entry for IBSS")
188 if 'flags' not in bss:
189 raise Exception("Could not get BSS flags from BSS table")
190 if "[WPA-None-TKIP]" not in bss['flags']:
191 raise Exception("Unexpected BSS flags: " + bss['flags'])
193 # Allow some time for all peers to complete key setup
196 # This is supposed to work, but looks like WPA-None does not work with
197 # mac80211 currently..
199 hwsim_utils.test_connectivity(dev[0], dev[1])
201 logger.info("Ignoring known connectivity failure: " + str(e))
203 hwsim_utils.test_connectivity(dev[0], dev[2])
205 logger.info("Ignoring known connectivity failure: " + str(e))
207 hwsim_utils.test_connectivity(dev[1], dev[2])
209 logger.info("Ignoring known connectivity failure: " + str(e))
211 def test_ibss_wpa_none_ccmp(dev):
212 """IBSS WPA-None/CCMP"""
215 logger.info("Start IBSS on the first STA")
216 id = add_ibss_wpa_none(dev[0], ssid)
217 connect_ibss_cmd(dev[0], id)
218 bssid0 = wait_ibss_connection(dev[0])
220 # This is a bit ugly, but no one really cares about WPA-None, so there may
221 # not be enough justification to clean this up.. For now, wpa_supplicant
222 # will show two connection events with mac80211_hwsim where the first one
223 # comes with all zeros address.
224 if bssid0 == "00:00:00:00:00:00":
225 logger.info("Waiting for real BSSID on the first STA")
226 bssid0 = wait_ibss_connection(dev[0])
229 logger.info("Join a STA to the IBSS")
230 id = add_ibss_wpa_none(dev[1], ssid)
231 connect_ibss_cmd(dev[1], id)
233 bssid1 = wait_ibss_connection(dev[1])
235 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
236 bssid1 = wait_ibss_connection(dev[1])
238 logger.info("bssid0=%s bssid1=%s" % (bssid0, bssid1))
240 # Allow some time for all peers to complete key setup
243 # This is supposed to work, but looks like WPA-None does not work with
244 # mac80211 currently..
246 hwsim_utils.test_connectivity(dev[0], dev[1])
248 logger.info("Ignoring known connectivity failure: " + str(e))
250 def test_ibss_open(dev):
251 """IBSS open (no security)"""
253 id = add_ibss(dev[0], ssid, key_mgmt="NONE", beacon_int="150")
254 connect_ibss_cmd(dev[0], id)
255 bssid0 = wait_ibss_connection(dev[0])
257 id = add_ibss(dev[1], ssid, key_mgmt="NONE", beacon_int="200")
258 connect_ibss_cmd(dev[1], id)
259 bssid1 = wait_ibss_connection(dev[1])
261 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
263 res = dev[0].request("SCAN_RESULTS")
264 if "[IBSS]" not in res:
265 res = dev[1].request("SCAN_RESULTS")
266 if "[IBSS]" not in res:
267 raise Exception("IBSS flag missing from scan results: " + res)
268 bss = dev[0].get_bss(bssid0)
270 bss = dev[1].get_bss(bssid1)
272 raise Exception("Could not find BSS entry for IBSS")
273 if 'flags' not in bss:
274 raise Exception("Could not get BSS flags from BSS table")
275 if "[IBSS]" not in bss['flags']:
276 raise Exception("Unexpected BSS flags: " + bss['flags'])
278 freq0 = dev[0].get_status_field("freq")
279 freq1 = dev[1].get_status_field("freq")
280 if freq0 != "2412" or freq1 != "2412":
281 raise Exception("IBSS operating frequency not reported correctly (%s %s)" % (freq0, freq1))
283 def test_ibss_open_fixed_bssid(dev):
284 """IBSS open (no security) and fixed BSSID"""
286 bssid="02:11:22:33:44:55"
288 dev[0].request("AP_SCAN 2")
289 add_ibss(dev[0], ssid, key_mgmt="NONE", bssid=bssid, beacon_int="150")
290 dev[0].request("REASSOCIATE")
292 dev[1].request("AP_SCAN 2")
293 add_ibss(dev[1], ssid, key_mgmt="NONE", bssid=bssid, beacon_int="200")
294 dev[1].request("REASSOCIATE")
296 bssid0 = wait_ibss_connection(dev[0])
297 bssid1 = wait_ibss_connection(dev[1])
299 raise Exception("STA0 BSSID " + bssid0 + " differs from fixed BSSID " + bssid)
301 raise Exception("STA0 BSSID " + bssid0 + " differs from fixed BSSID " + bssid)
303 dev[0].request("AP_SCAN 1")
304 dev[1].request("AP_SCAN 1")
306 def test_ibss_open_retry(dev):
307 """IBSS open (no security) with cfg80211 retry workaround"""
308 subprocess.check_call(['iw', 'dev', dev[0].ifname, 'set', 'type', 'adhoc'])
309 subprocess.check_call(['iw', 'dev', dev[0].ifname, 'ibss', 'join',
310 'ibss-test', '2412', 'HT20', 'fixed-freq',
311 '02:22:33:44:55:66'])
314 dev[0].request("AP_SCAN 2")
315 id = add_ibss(dev[0], ssid, key_mgmt="NONE", beacon_int="150",
316 bssid="02:33:44:55:66:77", scan_freq=2412)
317 #connect_ibss_cmd(dev[0], id)
318 dev[0].request("REASSOCIATE")
319 bssid0 = wait_ibss_connection(dev[0])
321 subprocess.check_call(['iw', 'dev', dev[0].ifname, 'ibss', 'leave'])
323 dev[0].request("DISCONNECT")
325 dev[0].request("AP_SCAN 1")
327 def test_ibss_rsn_tkip(dev):
328 """IBSS RSN with TKIP as the cipher"""
331 id = add_ibss_rsn_tkip(dev[0], ssid)
332 connect_ibss_cmd(dev[0], id)
333 bssid0 = wait_ibss_connection(dev[0])
335 id = add_ibss_rsn_tkip(dev[1], ssid)
336 connect_ibss_cmd(dev[1], id)
337 bssid1 = wait_ibss_connection(dev[1])
339 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
340 # try to merge with a scan
342 wait_4way_handshake(dev[0], dev[1])
343 wait_4way_handshake(dev[1], dev[0])
345 def test_ibss_wep(dev):
349 id = add_ibss(dev[0], ssid, key_mgmt="NONE", wep_key0='"hello"')
350 connect_ibss_cmd(dev[0], id)
351 bssid0 = wait_ibss_connection(dev[0])
353 id = add_ibss(dev[1], ssid, key_mgmt="NONE", wep_key0='"hello"')
354 connect_ibss_cmd(dev[1], id)
355 bssid1 = wait_ibss_connection(dev[1])
358 def test_ibss_rsn_error_case(dev):
359 """IBSS RSN regression test for IBSS_RSN prior IBSS setup"""
360 if "FAIL" not in dev[0].request("IBSS_RSN 02:03:04:05:06:07"):
361 raise Exception("Unexpected IBSS_RSN result")
363 def test_ibss_5ghz(dev):
364 """IBSS on 5 GHz band"""
368 subprocess.call(['iw', 'reg', 'set', '00'])
369 dev[0].flush_scan_cache()
370 dev[1].flush_scan_cache()
372 def _test_ibss_5ghz(dev):
373 subprocess.call(['iw', 'reg', 'set', 'US'])
376 ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
378 raise Exception("No regdom change event")
379 if "alpha2=US" in ev:
381 dev[i].dump_monitor()
384 id = add_ibss(dev[0], ssid, key_mgmt="NONE", beacon_int="150", freq=5180)
385 connect_ibss_cmd(dev[0], id, freq=5180)
386 bssid0 = wait_ibss_connection(dev[0])
388 dev[1].scan_for_bss(bssid0, freq=5180)
389 id = add_ibss(dev[1], ssid, key_mgmt="NONE", beacon_int="200", freq=5180)
390 connect_ibss_cmd(dev[1], id, freq=5180)
391 bssid1 = wait_ibss_connection(dev[1])
393 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
395 dev[0].request("DISCONNECT")
396 dev[1].request("DISCONNECT")
397 dev[0].dump_monitor()
398 dev[1].dump_monitor()
400 def test_ibss_vht_80p80(dev):
401 """IBSS on VHT 80+80 MHz channel"""
403 _test_ibss_vht_80p80(dev)
405 subprocess.call(['iw', 'reg', 'set', '00'])
406 dev[0].flush_scan_cache()
407 dev[1].flush_scan_cache()
409 def _test_ibss_vht_80p80(dev):
410 subprocess.call(['iw', 'reg', 'set', 'US'])
413 ev = dev[i].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=5)
415 raise Exception("No regdom change event")
416 if "alpha2=US" in ev:
418 dev[i].dump_monitor()
421 id = add_ibss(dev[0], ssid, key_mgmt="NONE", freq=5180, chwidth=3)
422 connect_ibss_cmd(dev[0], id, freq=5180)
423 bssid0 = wait_ibss_connection(dev[0])
424 sig = dev[0].request("SIGNAL_POLL").splitlines()
425 if "FREQUENCY=5180" not in sig:
426 raise Exception("Unexpected SIGNAL_POLL value(1): " + str(sig))
427 if "WIDTH=80+80 MHz" not in sig:
428 raise Exception("Unexpected SIGNAL_POLL value(2): " + str(sig))
429 if "CENTER_FRQ1=5210" not in sig:
430 raise Exception("Unexpected SIGNAL_POLL value(3): " + str(sig))
431 if "CENTER_FRQ2=5775" not in sig:
432 raise Exception("Unexpected SIGNAL_POLL value(4): " + str(sig))
434 dev[1].scan_for_bss(bssid0, freq=5180)
435 id = add_ibss(dev[1], ssid, key_mgmt="NONE", freq=5180, chwidth=3)
436 connect_ibss_cmd(dev[1], id, freq=5180)
437 bssid1 = wait_ibss_connection(dev[1])
439 logger.info("STA0 BSSID " + bssid0 + " differs from STA1 BSSID " + bssid1)
441 sig = dev[1].request("SIGNAL_POLL").splitlines()
442 if "FREQUENCY=5180" not in sig:
443 raise Exception("Unexpected SIGNAL_POLL value(1b): " + str(sig))
444 logger.info("STA1 SIGNAL_POLL: " + str(sig))
445 # For now, don't report errors on joining STA failing to get 80+80 MHZ
446 # since mac80211 missed functionality for that to work.
448 dev[0].request("DISCONNECT")
449 dev[1].request("DISCONNECT")
450 dev[0].dump_monitor()
451 dev[1].dump_monitor()
453 def test_ibss_rsn_oom(dev):
454 """IBSS RSN OOM during wpa_init"""
455 with alloc_fail(dev[0], 1, "wpa_init"):
457 id = add_ibss_rsn(dev[0], ssid)
458 connect_ibss_cmd(dev[0], id)
459 bssid0 = wait_ibss_connection(dev[0])