1# Test various AP mode parameters
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
7from remotehost import remote_compatible
8import logging
9logger = logging.getLogger()
10import os
11import struct
12import subprocess
13import time
14
15import hwsim_utils
16import hostapd
17from tshark import run_tshark
18from utils import *
19
20@remote_compatible
21def test_ap_fragmentation_rts_set_high(dev, apdev):
22    """WPA2-PSK AP with fragmentation and RTS thresholds larger than frame length"""
23    ssid = "test-wpa2-psk"
24    passphrase = 'qwertyuiop'
25    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
26    params['rts_threshold'] = "1000"
27    params['fragm_threshold'] = "2000"
28    hapd = hostapd.add_ap(apdev[0], params)
29    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
30    hwsim_utils.test_connectivity(dev[0], hapd)
31    dev[0].request("DISCONNECT")
32    hapd.disable()
33    hapd.set('fragm_threshold', '-1')
34    hapd.set('rts_threshold', '-1')
35    hapd.enable()
36
37@remote_compatible
38def test_ap_fragmentation_open(dev, apdev):
39    """Open AP with fragmentation threshold"""
40    ssid = "fragmentation"
41    params = {}
42    params['ssid'] = ssid
43    params['fragm_threshold'] = "1000"
44    hapd = hostapd.add_ap(apdev[0], params)
45    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
46    hwsim_utils.test_connectivity(dev[0], hapd)
47    dev[0].request("DISCONNECT")
48    hapd.disable()
49    hapd.set('fragm_threshold', '-1')
50    hapd.enable()
51
52@remote_compatible
53def test_ap_fragmentation_wpa2(dev, apdev):
54    """WPA2-PSK AP with fragmentation threshold"""
55    ssid = "test-wpa2-psk"
56    passphrase = 'qwertyuiop'
57    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
58    params['fragm_threshold'] = "1000"
59    hapd = hostapd.add_ap(apdev[0], params)
60    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
61    hwsim_utils.test_connectivity(dev[0], hapd)
62    dev[0].request("DISCONNECT")
63    hapd.disable()
64    hapd.set('fragm_threshold', '-1')
65    hapd.enable()
66
67def test_ap_vendor_elements(dev, apdev):
68    """WPA2-PSK AP with vendor elements added"""
69    bssid = apdev[0]['bssid']
70    ssid = "test-wpa2-psk"
71    passphrase = 'qwertyuiop'
72    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
73    params['vendor_elements'] = "dd0411223301"
74    params['assocresp_elements'] = "dd0411223302"
75    hapd = hostapd.add_ap(apdev[0], params)
76    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
77    bss = dev[0].get_bss(bssid)
78    if "dd0411223301" not in bss['ie']:
79        raise Exception("Vendor element not shown in scan results")
80
81    hapd.set('vendor_elements', 'dd051122330203dd0400137400dd04001374ff')
82    if "OK" not in hapd.request("UPDATE_BEACON"):
83        raise Exception("UPDATE_BEACON failed")
84    dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
85    bss = dev[1].get_bss(bssid)
86    if "dd0411223301" in bss['ie']:
87        raise Exception("Old vendor element still in scan results")
88    if "dd051122330203" not in bss['ie']:
89        raise Exception("New vendor element not shown in scan results")
90
91def test_ap_element_parse(dev, apdev):
92    """Information element parsing - extra coverage"""
93    bssid = apdev[0]['bssid']
94    ssid = "test-wpa2-psk"
95    params = {'ssid': ssid,
96              'vendor_elements': "380501020304059e009e009e009e009e009e00"}
97    hapd = hostapd.add_ap(apdev[0], params)
98    dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
99    bss = dev[0].get_bss(bssid)
100    if "38050102030405" not in bss['ie']:
101        raise Exception("Timeout element not shown in scan results")
102
103@remote_compatible
104def test_ap_element_parse_oom(dev, apdev):
105    """Information element parsing OOM"""
106    bssid = apdev[0]['bssid']
107    ssid = "test-wpa2-psk"
108    params = {'ssid': ssid,
109              'vendor_elements': "dd0d506f9a0a00000600411c440028"}
110    hapd = hostapd.add_ap(apdev[0], params)
111    dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
112    with alloc_fail(dev[0], 1, "wpabuf_alloc;ieee802_11_vendor_ie_concat"):
113        bss = dev[0].get_bss(bssid)
114        logger.info(str(bss))
115
116def test_ap_country(dev, apdev):
117    """WPA2-PSK AP setting country code and using 5 GHz band"""
118    try:
119        hapd = None
120        bssid = apdev[0]['bssid']
121        ssid = "test-wpa2-psk"
122        passphrase = 'qwertyuiop'
123        params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
124        params['country_code'] = 'FI'
125        params['ieee80211d'] = '1'
126        params['hw_mode'] = 'a'
127        params['channel'] = '36'
128        hapd = hostapd.add_ap(apdev[0], params)
129        dev[0].connect(ssid, psk=passphrase, scan_freq="5180")
130        hwsim_utils.test_connectivity(dev[0], hapd)
131    finally:
132        if hapd:
133            hapd.request("DISABLE")
134        dev[0].disconnect_and_stop_scan()
135        hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
136        dev[0].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=0.5)
137        dev[0].flush_scan_cache()
138
139def test_ap_acl_accept(dev, apdev):
140    """MAC ACL accept list"""
141    ssid = "acl"
142    params = {}
143    filename = hostapd.acl_file(dev, apdev, 'hostapd.macaddr')
144    hostapd.send_file(apdev[0], filename, filename)
145    params['ssid'] = ssid
146    params['accept_mac_file'] = filename
147    hapd = hostapd.add_ap(apdev[0], params)
148    dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
149    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
150    dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
151    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
152    dev[0].request("REMOVE_NETWORK all")
153    dev[1].request("REMOVE_NETWORK all")
154    hapd.request("SET macaddr_acl 1")
155    dev[1].dump_monitor()
156    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
157    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
158    ev = dev[1].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
159    if ev is not None:
160        raise Exception("Unexpected association")
161    if filename.startswith('/tmp/'):
162        os.unlink(filename)
163
164def test_ap_acl_deny(dev, apdev):
165    """MAC ACL deny list"""
166    ssid = "acl"
167    params = {}
168    filename = hostapd.acl_file(dev, apdev, 'hostapd.macaddr')
169    hostapd.send_file(apdev[0], filename, filename)
170    params['ssid'] = ssid
171    params['deny_mac_file'] = filename
172    hapd = hostapd.add_ap(apdev[0], params)
173    dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", passive=True)
174    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
175    dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
176    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
177    ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
178    if ev is not None:
179        raise Exception("Unexpected association")
180    if filename.startswith('/tmp/'):
181        os.unlink(filename)
182
183def test_ap_acl_mgmt(dev, apdev):
184    """MAC ACL accept/deny management"""
185    ssid = "acl"
186    params = {}
187    filename = hostapd.acl_file(dev, apdev, 'hostapd.macaddr')
188    hostapd.send_file(apdev[0], filename, filename)
189    params['ssid'] = ssid
190    params['deny_mac_file'] = filename
191    hapd = hostapd.add_ap(apdev[0], params)
192
193    accept = hapd.request("ACCEPT_ACL SHOW").splitlines()
194    logger.info("accept: " + str(accept))
195    deny = hapd.request("DENY_ACL SHOW").splitlines()
196    logger.info("deny: " + str(deny))
197    if len(accept) != 0:
198        raise Exception("Unexpected number of accept entries")
199    if len(deny) != 3:
200        raise Exception("Unexpected number of deny entries")
201    if "01:01:01:01:01:01 VLAN_ID=0" not in deny:
202        raise Exception("Missing deny entry")
203
204    if "OK" not in hapd.request("ACCEPT_ACL DEL_MAC 22:33:44:55:66:77"):
205        raise Exception("DEL_MAC with empty list failed")
206    if "FAIL" not in hapd.request("ACCEPT_ACL ADD_MAC 22:33:44:55:66"):
207        raise Exception("ADD_MAC with invalid MAC address accepted")
208    hapd.request("ACCEPT_ACL ADD_MAC 22:33:44:55:66:77")
209    if "FAIL" not in hapd.request("ACCEPT_ACL DEL_MAC 22:33:44:55:66"):
210        raise Exception("DEL_MAC with invalid MAC address accepted")
211    hapd.request("DENY_ACL ADD_MAC 22:33:44:55:66:88 VLAN_ID=2")
212
213    accept = hapd.request("ACCEPT_ACL SHOW").splitlines()
214    logger.info("accept: " + str(accept))
215    deny = hapd.request("DENY_ACL SHOW").splitlines()
216    logger.info("deny: " + str(deny))
217    if len(accept) != 1:
218        raise Exception("Unexpected number of accept entries (2)")
219    if len(deny) != 4:
220        raise Exception("Unexpected number of deny entries (2)")
221    if "01:01:01:01:01:01 VLAN_ID=0" not in deny:
222        raise Exception("Missing deny entry (2)")
223    if "22:33:44:55:66:88 VLAN_ID=2" not in deny:
224        raise Exception("Missing deny entry (2)")
225    if "22:33:44:55:66:77 VLAN_ID=0" not in accept:
226        raise Exception("Missing accept entry (2)")
227
228    hapd.request("ACCEPT_ACL DEL_MAC 22:33:44:55:66:77")
229    hapd.request("DENY_ACL DEL_MAC 22:33:44:55:66:88")
230
231    accept = hapd.request("ACCEPT_ACL SHOW").splitlines()
232    logger.info("accept: " + str(accept))
233    deny = hapd.request("DENY_ACL SHOW").splitlines()
234    logger.info("deny: " + str(deny))
235    if len(accept) != 0:
236        raise Exception("Unexpected number of accept entries (3)")
237    if len(deny) != 3:
238        raise Exception("Unexpected number of deny entries (3)")
239    if "01:01:01:01:01:01 VLAN_ID=0" not in deny:
240        raise Exception("Missing deny entry (3)")
241
242    hapd.request("ACCEPT_ACL CLEAR")
243    hapd.request("DENY_ACL CLEAR")
244
245    accept = hapd.request("ACCEPT_ACL SHOW").splitlines()
246    logger.info("accept: " + str(accept))
247    deny = hapd.request("DENY_ACL SHOW").splitlines()
248    logger.info("deny: " + str(deny))
249    if len(accept) != 0:
250        raise Exception("Unexpected number of accept entries (4)")
251    if len(deny) != 0:
252        raise Exception("Unexpected number of deny entries (4)")
253
254    dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
255    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
256    dev[0].dump_monitor()
257    hapd.request("DENY_ACL ADD_MAC " + dev[0].own_addr())
258    dev[0].wait_disconnected()
259    dev[0].request("DISCONNECT")
260    if filename.startswith('/tmp/'):
261        os.unlink(filename)
262
263def test_ap_acl_accept_changes(dev, apdev):
264    """MAC ACL accept list changes"""
265    ssid = "acl"
266    params = {}
267    params['ssid'] = ssid
268    params['macaddr_acl'] = "1"
269    hapd = hostapd.add_ap(apdev[0], params)
270    hapd.request("ACCEPT_ACL ADD_MAC " + dev[0].own_addr())
271    hapd.request("ACCEPT_ACL ADD_MAC " + dev[1].own_addr())
272    dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412")
273    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
274    dev[1].scan_for_bss(apdev[0]['bssid'], freq="2412")
275    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
276    hapd.request("ACCEPT_ACL DEL_MAC " + dev[0].own_addr())
277    dev[0].wait_disconnected()
278    dev[0].request("DISCONNECT")
279    ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
280    if ev is not None:
281        raise Exception("Unexpected disconnection")
282    hapd.request("ACCEPT_ACL CLEAR")
283    dev[1].wait_disconnected()
284    dev[1].request("DISCONNECT")
285
286@remote_compatible
287def test_ap_wds_sta(dev, apdev):
288    """WPA2-PSK AP with STA using 4addr mode"""
289    ssid = "test-wpa2-psk"
290    passphrase = 'qwertyuiop'
291    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
292    params['wds_sta'] = "1"
293    params['wds_bridge'] = "wds-br0"
294    hapd = hostapd.add_ap(apdev[0], params)
295
296    try:
297        dev[0].cmd_execute(['brctl', 'addbr', 'wds-br0'])
298        dev[0].cmd_execute(['brctl', 'setfd', 'wds-br0', '0'])
299        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'up'])
300        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'on'])
301        dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
302        ev = hapd.wait_event(["WDS-STA-INTERFACE-ADDED"], timeout=10)
303        if ev is None:
304            raise Exception("No WDS-STA-INTERFACE-ADDED event seen")
305        if "sta_addr=" + dev[0].own_addr() not in ev:
306            raise Exception("No sta_addr match in " + ev)
307        if "ifname=" + hapd.ifname + ".sta" not in ev:
308            raise Exception("No ifname match in " + ev)
309        sta = hapd.get_sta(dev[0].own_addr())
310        if "wds_sta_ifname" not in sta:
311            raise Exception("Missing wds_sta_ifname in STA data")
312        if "ifname=" + sta['wds_sta_ifname'] not in ev:
313            raise Exception("wds_sta_ifname %s not in event: %s" %
314                            (sta['wds_sta_ifname'], ev))
315        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
316                                            max_tries=15)
317        dev[0].request("REATTACH")
318        dev[0].wait_connected()
319        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
320                                            max_tries=15)
321        dev[0].request("SET reassoc_same_bss_optim 1")
322        dev[0].request("REATTACH")
323        dev[0].wait_connected()
324        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
325                                            max_tries=5, timeout=1)
326    finally:
327        dev[0].request("SET reassoc_same_bss_optim 0")
328        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'off'])
329        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'down'])
330        dev[0].cmd_execute(['brctl', 'delbr', 'wds-br0'])
331
332def test_ap_wds_sta_eap(dev, apdev):
333    """WPA2-EAP AP with STA using 4addr mode"""
334    ssid = "test-wpa2-eap"
335    params = hostapd.wpa2_eap_params(ssid=ssid)
336    params['wds_sta'] = "1"
337    params['wds_bridge'] = "wds-br0"
338    hapd = hostapd.add_ap(apdev[0], params)
339
340    try:
341        dev[0].cmd_execute(['brctl', 'addbr', 'wds-br0'])
342        dev[0].cmd_execute(['brctl', 'setfd', 'wds-br0', '0'])
343        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'up'])
344        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'on'])
345        dev[0].connect(ssid, key_mgmt="WPA-EAP", eap="GPSK",
346                       identity="gpsk user",
347                       password="abcdefghijklmnop0123456789abcdef",
348                       scan_freq="2412")
349        ev = hapd.wait_event(["WDS-STA-INTERFACE-ADDED"], timeout=10)
350        if ev is None:
351            raise Exception("No WDS-STA-INTERFACE-ADDED event seen")
352        if "sta_addr=" + dev[0].own_addr() not in ev:
353            raise Exception("No sta_addr match in " + ev)
354        if "ifname=" + hapd.ifname + ".sta" not in ev:
355            raise Exception("No ifname match in " + ev)
356        sta = hapd.get_sta(dev[0].own_addr())
357        if "wds_sta_ifname" not in sta:
358            raise Exception("Missing wds_sta_ifname in STA data")
359        if "ifname=" + sta['wds_sta_ifname'] not in ev:
360            raise Exception("wds_sta_ifname %s not in event: %s" %
361                            (sta['wds_sta_ifname'], ev))
362        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
363                                            max_tries=15)
364    finally:
365        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'off'])
366        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'down'])
367        dev[0].cmd_execute(['brctl', 'delbr', 'wds-br0'])
368
369def test_ap_wds_sta_open(dev, apdev):
370    """Open AP with STA using 4addr mode"""
371    ssid = "test-wds-open"
372    params = {}
373    params['ssid'] = ssid
374    params['wds_sta'] = "1"
375    params['wds_bridge'] = "wds-br0"
376    hapd = hostapd.add_ap(apdev[0], params)
377
378    try:
379        dev[0].cmd_execute(['brctl', 'addbr', 'wds-br0'])
380        dev[0].cmd_execute(['brctl', 'setfd', 'wds-br0', '0'])
381        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'up'])
382        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'on'])
383        dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
384        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
385                                            max_tries=15)
386        dev[0].request("REATTACH")
387        dev[0].wait_connected()
388        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
389                                            max_tries=15)
390        dev[0].request("SET reassoc_same_bss_optim 1")
391        dev[0].request("REATTACH")
392        dev[0].wait_connected()
393        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
394                                            max_tries=5, timeout=1)
395    finally:
396        dev[0].request("SET reassoc_same_bss_optim 0")
397        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'off'])
398        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'down'])
399        dev[0].cmd_execute(['brctl', 'delbr', 'wds-br0'])
400
401def test_ap_wds_sta_wep(dev, apdev):
402    """WEP AP with STA using 4addr mode"""
403    check_wep_capa(dev[0])
404    ssid = "test-wds-wep"
405    params = {}
406    params['ssid'] = ssid
407    params["ieee80211n"] = "0"
408    params['wep_key0'] = '"hello"'
409    params['wds_sta'] = "1"
410    params['wds_bridge'] = "wds-br0"
411    hapd = hostapd.add_ap(apdev[0], params)
412
413    try:
414        dev[0].cmd_execute(['brctl', 'addbr', 'wds-br0'])
415        dev[0].cmd_execute(['brctl', 'setfd', 'wds-br0', '0'])
416        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'up'])
417        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'on'])
418        dev[0].connect(ssid, key_mgmt="NONE", wep_key0='"hello"',
419                       scan_freq="2412")
420        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
421                                            max_tries=15)
422        dev[0].request("REATTACH")
423        dev[0].wait_connected()
424        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
425                                            max_tries=15)
426        dev[0].request("SET reassoc_same_bss_optim 1")
427        dev[0].request("REATTACH")
428        dev[0].wait_connected()
429        hwsim_utils.test_connectivity_iface(dev[0], hapd, "wds-br0",
430                                            max_tries=5, timeout=1)
431    finally:
432        dev[0].request("SET reassoc_same_bss_optim 0")
433        dev[0].cmd_execute(['iw', dev[0].ifname, 'set', '4addr', 'off'])
434        dev[0].cmd_execute(['ip', 'link', 'set', 'dev', 'wds-br0', 'down'])
435        dev[0].cmd_execute(['brctl', 'delbr', 'wds-br0'])
436
437@remote_compatible
438def test_ap_inactivity_poll(dev, apdev):
439    """AP using inactivity poll"""
440    ssid = "test-wpa2-psk"
441    passphrase = 'qwertyuiop'
442    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
443    params['ap_max_inactivity'] = "1"
444    hapd = hostapd.add_ap(apdev[0], params)
445    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
446    hapd.set("ext_mgmt_frame_handling", "1")
447    dev[0].request("DISCONNECT")
448    ev = hapd.wait_event(["MGMT-RX"], timeout=5)
449    if ev is None:
450        raise Exception("MGMT RX wait timed out for Deauth")
451    hapd.set("ext_mgmt_frame_handling", "0")
452    ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=30)
453    if ev is None:
454        raise Exception("STA disconnection on inactivity was not reported")
455
456@remote_compatible
457def test_ap_inactivity_disconnect(dev, apdev):
458    """AP using inactivity disconnect"""
459    ssid = "test-wpa2-psk"
460    passphrase = 'qwertyuiop'
461    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
462    params['ap_max_inactivity'] = "1"
463    params['skip_inactivity_poll'] = "1"
464    hapd = hostapd.add_ap(apdev[0], params)
465    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
466    hapd.set("ext_mgmt_frame_handling", "1")
467    dev[0].request("DISCONNECT")
468    ev = hapd.wait_event(["MGMT-RX"], timeout=5)
469    if ev is None:
470        raise Exception("MGMT RX wait timed out for Deauth")
471    hapd.set("ext_mgmt_frame_handling", "0")
472    ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=30)
473    if ev is None:
474        raise Exception("STA disconnection on inactivity was not reported")
475
476@remote_compatible
477def test_ap_basic_rates(dev, apdev):
478    """Open AP with lots of basic rates"""
479    ssid = "basic rates"
480    params = {}
481    params['ssid'] = ssid
482    params['basic_rates'] = "10 20 55 110 60 90 120 180 240 360 480 540"
483    hostapd.add_ap(apdev[0], params)
484    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
485
486@remote_compatible
487def test_ap_short_preamble(dev, apdev):
488    """Open AP with short preamble"""
489    ssid = "short preamble"
490    params = {}
491    params['ssid'] = ssid
492    params['preamble'] = "1"
493    hostapd.add_ap(apdev[0], params)
494    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
495
496def test_ap_spectrum_management_required(dev, apdev):
497    """Open AP with spectrum management required"""
498    ssid = "spectrum mgmt"
499    params = {}
500    params['ssid'] = ssid
501    params["country_code"] = "JP"
502    params["hw_mode"] = "a"
503    params["channel"] = "36"
504    params["ieee80211d"] = "1"
505    params["local_pwr_constraint"] = "3"
506    params['spectrum_mgmt_required'] = "1"
507    try:
508        hapd = None
509        hapd = hostapd.add_ap(apdev[0], params)
510        dev[0].connect(ssid, key_mgmt="NONE", scan_freq="5180")
511        dev[0].wait_regdom(country_ie=True)
512    finally:
513        if hapd:
514            hapd.request("DISABLE")
515        dev[0].disconnect_and_stop_scan()
516        hostapd.cmd_execute(apdev[0], ['iw', 'reg', 'set', '00'])
517        dev[0].wait_event(["CTRL-EVENT-REGDOM-CHANGE"], timeout=0.5)
518        dev[0].flush_scan_cache()
519
520@remote_compatible
521def test_ap_max_listen_interval(dev, apdev):
522    """Open AP with maximum listen interval limit"""
523    ssid = "listen"
524    params = {}
525    params['ssid'] = ssid
526    params['max_listen_interval'] = "1"
527    hostapd.add_ap(apdev[0], params)
528    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
529    ev = dev[0].wait_event(["CTRL-EVENT-ASSOC-REJECT"])
530    if ev is None:
531        raise Exception("Association rejection not reported")
532    if "status_code=51" not in ev:
533        raise Exception("Unexpected ASSOC-REJECT reason")
534
535@remote_compatible
536def test_ap_max_num_sta(dev, apdev):
537    """Open AP with maximum STA count"""
538    ssid = "max"
539    params = {}
540    params['ssid'] = ssid
541    params['max_num_sta'] = "1"
542    hostapd.add_ap(apdev[0], params)
543    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
544    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412", wait_connect=False)
545    ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
546    if ev is not None:
547        raise Exception("Unexpected association")
548
549def test_ap_max_num_sta_no_probe_resp(dev, apdev, params):
550    """Maximum STA count and limit on Probe Response frames"""
551    logdir = params['logdir']
552    dev[0].flush_scan_cache()
553    ssid = "max"
554    params = {}
555    params['ssid'] = ssid
556    params['beacon_int'] = "2000"
557    params['max_num_sta'] = "1"
558    params['no_probe_resp_if_max_sta'] = "1"
559    hostapd.add_ap(apdev[0], params)
560    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
561    dev[0].scan(freq=2412, type="ONLY")
562    dev[0].scan(freq=2412, type="ONLY")
563    seen = dev[0].get_bss(apdev[0]['bssid']) != None
564    dev[1].scan(freq=2412, type="ONLY")
565    if seen:
566        out = run_tshark(os.path.join(logdir, "hwsim0.pcapng"),
567                         "wlan.fc.type_subtype == 5", ["wlan.da"])
568        if out:
569            if dev[0].own_addr() not in out:
570                # Discovery happened through Beacon frame reception. That's not
571                # an error case.
572                seen = False
573            if dev[1].own_addr() not in out:
574                raise Exception("No Probe Response frames to dev[1] seen")
575        if seen:
576            raise Exception("AP found unexpectedly")
577
578@remote_compatible
579def test_ap_tx_queue_params(dev, apdev):
580    """Open AP with TX queue params set"""
581    ssid = "tx"
582    params = {}
583    params['ssid'] = ssid
584    params['tx_queue_data2_aifs'] = "4"
585    params['tx_queue_data2_cwmin'] = "7"
586    params['tx_queue_data2_cwmax'] = "1023"
587    params['tx_queue_data2_burst'] = "4.2"
588    params['tx_queue_data1_aifs'] = "4"
589    params['tx_queue_data1_cwmin'] = "7"
590    params['tx_queue_data1_cwmax'] = "1023"
591    params['tx_queue_data1_burst'] = "2"
592    hapd = hostapd.add_ap(apdev[0], params)
593    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
594    hwsim_utils.test_connectivity(dev[0], hapd)
595
596def test_ap_tx_queue_params_invalid(dev, apdev):
597    """Invalid TX queue params set (cwmin/cwmax)"""
598    ssid = "tx"
599    params = {}
600    params['ssid'] = ssid
601    params['tx_queue_data2_aifs'] = "4"
602    params['tx_queue_data2_cwmin'] = "7"
603    params['tx_queue_data2_cwmax'] = "1023"
604    params['tx_queue_data2_burst'] = "4.2"
605    params['wmm_ac_bk_cwmin'] = "4"
606    params['wmm_ac_bk_cwmax'] = "10"
607    params['wmm_ac_bk_aifs'] = "7"
608    params['wmm_ac_bk_txop_limit'] = "0"
609    params['wmm_ac_bk_acm'] = "0"
610
611    hapd = hostapd.add_ap(apdev[0], params)
612
613    # Valid WMM change
614    hapd.set("wmm_ac_be_cwmin", "3")
615
616    # "Invalid TX queue cwMin/cwMax values. cwMin(7) greater than cwMax(3)"
617    if "FAIL" not in hapd.request('SET tx_queue_data2_cwmax 3'):
618        raise Exception("TX cwMax < cwMin accepted")
619    # "Invalid WMM AC cwMin/cwMax values. cwMin(4) greater than cwMax(3)"
620    if "FAIL" not in hapd.request('SET wmm_ac_bk_cwmax 3'):
621        raise Exception("AC cwMax < cwMin accepted")
622
623    hapd.request("SET tx_queue_data2_cwmax 1023")
624    hapd.set("wmm_ac_bk_cwmax", "10")
625    # Invalid IEs to cause WMM parameter update failing
626    hapd.set("vendor_elements", "dd04112233")
627    hapd.set("wmm_ac_be_cwmin", "3")
628    # Valid IEs to cause WMM parameter update succeeding
629    hapd.set("vendor_elements", "dd0411223344")
630    hapd.set("wmm_ac_be_cwmin", "3")
631
632    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
633
634def test_ap_beacon_rate_legacy(dev, apdev):
635    """Open AP with Beacon frame TX rate 5.5 Mbps"""
636    hapd = hostapd.add_ap(apdev[0], {'ssid': 'beacon-rate'})
637    res = hapd.get_driver_status_field('capa.flags')
638    if (int(res, 0) & 0x0000080000000000) == 0:
639        raise HwsimSkip("Setting Beacon frame TX rate not supported")
640    hapd.disable()
641    hapd.set('beacon_rate', '55')
642    hapd.enable()
643    dev[0].connect('beacon-rate', key_mgmt="NONE", scan_freq="2412")
644    time.sleep(0.5)
645
646def test_ap_beacon_rate_legacy2(dev, apdev):
647    """Open AP with Beacon frame TX rate 12 Mbps in VHT BSS"""
648    hapd = hostapd.add_ap(apdev[0], {'ssid': 'beacon-rate'})
649    res = hapd.get_driver_status_field('capa.flags')
650    if (int(res, 0) & 0x0000080000000000) == 0:
651        raise HwsimSkip("Setting Beacon frame TX rate not supported")
652    hapd.disable()
653    hapd.set('beacon_rate', '120')
654    hapd.set("country_code", "DE")
655    hapd.set("hw_mode", "a")
656    hapd.set("channel", "36")
657    hapd.set("ieee80211n", "1")
658    hapd.set("ieee80211ac", "1")
659    hapd.set("ht_capab", "[HT40+]")
660    hapd.set("vht_capab", "")
661    hapd.set("vht_oper_chwidth", "0")
662    hapd.set("vht_oper_centr_freq_seg0_idx", "0")
663    try:
664        hapd.enable()
665        dev[0].scan_for_bss(hapd.own_addr(), freq="5180")
666        dev[0].connect('beacon-rate', key_mgmt="NONE", scan_freq="5180")
667        time.sleep(0.5)
668    finally:
669        dev[0].request("DISCONNECT")
670        hapd.request("DISABLE")
671        subprocess.call(['iw', 'reg', 'set', '00'])
672        dev[0].flush_scan_cache()
673
674def test_ap_beacon_rate_ht(dev, apdev):
675    """Open AP with Beacon frame TX rate HT-MCS 0"""
676    hapd = hostapd.add_ap(apdev[0], {'ssid': 'beacon-rate'})
677    res = hapd.get_driver_status_field('capa.flags')
678    if (int(res, 0) & 0x0000100000000000) == 0:
679        raise HwsimSkip("Setting Beacon frame TX rate not supported")
680    hapd.disable()
681    hapd.set('beacon_rate', 'ht:0')
682    hapd.enable()
683    dev[0].connect('beacon-rate', key_mgmt="NONE", scan_freq="2412")
684    time.sleep(0.5)
685
686def test_ap_beacon_rate_ht2(dev, apdev):
687    """Open AP with Beacon frame TX rate HT-MCS 1 in VHT BSS"""
688    hapd = hostapd.add_ap(apdev[0], {'ssid': 'beacon-rate'})
689    res = hapd.get_driver_status_field('capa.flags')
690    if (int(res, 0) & 0x0000100000000000) == 0:
691        raise HwsimSkip("Setting Beacon frame TX rate not supported")
692    hapd.disable()
693    hapd.set('beacon_rate', 'ht:1')
694    hapd.set("country_code", "DE")
695    hapd.set("hw_mode", "a")
696    hapd.set("channel", "36")
697    hapd.set("ieee80211n", "1")
698    hapd.set("ieee80211ac", "1")
699    hapd.set("ht_capab", "[HT40+]")
700    hapd.set("vht_capab", "")
701    hapd.set("vht_oper_chwidth", "0")
702    hapd.set("vht_oper_centr_freq_seg0_idx", "0")
703    try:
704        hapd.enable()
705        dev[0].scan_for_bss(hapd.own_addr(), freq="5180")
706        dev[0].connect('beacon-rate', key_mgmt="NONE", scan_freq="5180")
707        time.sleep(0.5)
708    finally:
709        dev[0].request("DISCONNECT")
710        hapd.request("DISABLE")
711        subprocess.call(['iw', 'reg', 'set', '00'])
712        dev[0].flush_scan_cache()
713
714def test_ap_beacon_rate_vht(dev, apdev):
715    """Open AP with Beacon frame TX rate VHT-MCS 0"""
716    hapd = hostapd.add_ap(apdev[0], {'ssid': 'beacon-rate'})
717    res = hapd.get_driver_status_field('capa.flags')
718    if (int(res, 0) & 0x0000200000000000) == 0:
719        raise HwsimSkip("Setting Beacon frame TX rate not supported")
720    hapd.disable()
721    hapd.set('beacon_rate', 'vht:0')
722    hapd.set("country_code", "DE")
723    hapd.set("hw_mode", "a")
724    hapd.set("channel", "36")
725    hapd.set("ieee80211n", "1")
726    hapd.set("ieee80211ac", "1")
727    hapd.set("ht_capab", "[HT40+]")
728    hapd.set("vht_capab", "")
729    hapd.set("vht_oper_chwidth", "0")
730    hapd.set("vht_oper_centr_freq_seg0_idx", "0")
731    try:
732        hapd.enable()
733        dev[0].scan_for_bss(hapd.own_addr(), freq="5180")
734        dev[0].connect('beacon-rate', key_mgmt="NONE", scan_freq="5180")
735        time.sleep(0.5)
736    finally:
737        dev[0].request("DISCONNECT")
738        hapd.request("DISABLE")
739        subprocess.call(['iw', 'reg', 'set', '00'])
740        dev[0].flush_scan_cache()
741
742def test_ap_wep_to_wpa(dev, apdev):
743    """WEP to WPA2-PSK configuration change in hostapd"""
744    check_wep_capa(dev[0])
745    hapd = hostapd.add_ap(apdev[0],
746                          {"ssid": "wep-to-wpa",
747                           "wep_key0": '"hello"'})
748    dev[0].flush_scan_cache()
749    dev[0].connect("wep-to-wpa", key_mgmt="NONE", wep_key0='"hello"',
750                   scan_freq="2412")
751    hwsim_utils.test_connectivity(dev[0], hapd)
752    dev[0].request("DISCONNECT")
753    dev[0].wait_disconnected()
754
755    hapd.disable()
756    hapd.set("wep_key0", "")
757    hapd.set("wpa_passphrase", "12345678")
758    hapd.set("wpa", "2")
759    hapd.set("wpa_key_mgmt", "WPA-PSK")
760    hapd.set("rsn_pairwise", "CCMP")
761    hapd.enable()
762
763    dev[0].connect("wep-to-wpa", psk="12345678", scan_freq="2412")
764    hwsim_utils.test_connectivity(dev[0], hapd)
765
766def test_ap_missing_psk(dev, apdev):
767    """WPA2-PSK AP and no PSK configured"""
768    ssid = "test-wpa2-psk"
769    params = hostapd.wpa2_params(ssid=ssid)
770    try:
771        # "WPA-PSK enabled, but PSK or passphrase is not configured."
772        hostapd.add_ap(apdev[0], params)
773        raise Exception("AP setup succeeded unexpectedly")
774    except Exception as e:
775        if "Failed to enable hostapd" in str(e):
776            pass
777        else:
778            raise
779
780def test_ap_eapol_version(dev, apdev):
781    """hostapd eapol_version configuration"""
782    passphrase = "asdfghjkl"
783    params = hostapd.wpa2_params(ssid="test1", passphrase=passphrase)
784    hapd = hostapd.add_ap(apdev[0], params)
785    params = hostapd.wpa2_params(ssid="test2", passphrase=passphrase)
786    params['eapol_version'] = '1'
787    hapd2 = hostapd.add_ap(apdev[1], params)
788
789    hapd.request("SET ext_eapol_frame_io 1")
790    dev[0].connect("test1", psk=passphrase, scan_freq="2412",
791                   wait_connect=False)
792    ev1 = hapd.wait_event(["EAPOL-TX"], timeout=15)
793    if ev1 is None:
794        raise Exception("Timeout on EAPOL-TX from hostapd")
795    hapd.request("SET ext_eapol_frame_io 0")
796
797    hapd2.request("SET ext_eapol_frame_io 1")
798    dev[1].connect("test2", psk=passphrase, scan_freq="2412",
799                   wait_connect=False)
800    ev2 = hapd2.wait_event(["EAPOL-TX"], timeout=15)
801    if ev2 is None:
802        raise Exception("Timeout on EAPOL-TX from hostapd")
803    hapd2.request("SET ext_eapol_frame_io 0")
804
805    dev[0].wait_connected()
806    dev[1].wait_connected()
807
808    ver1 = ev1.split(' ')[2][0:2]
809    ver2 = ev2.split(' ')[2][0:2]
810    if ver1 != "02":
811        raise Exception("Unexpected default eapol_version: " + ver1)
812    if ver2 != "01":
813        raise Exception("eapol_version did not match configuration: " + ver2)
814
815def test_ap_dtim_period(dev, apdev):
816    """DTIM period configuration"""
817    ssid = "dtim-period"
818    params = {'ssid': ssid, 'dtim_period': "10"}
819    hapd = hostapd.add_ap(apdev[0], params)
820    bssid = hapd.own_addr()
821    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
822    for i in range(10):
823        dev[0].scan(freq="2412")
824        bss = dev[0].get_bss(bssid)
825        if 'beacon_ie' in bss:
826            break
827        time.sleep(0.2)
828    if 'beacon_ie' not in bss:
829        raise Exception("Did not find Beacon IEs")
830
831    ie = parse_ie(bss['beacon_ie'])
832    if 5 not in ie:
833        raise Exception("TIM element missing")
834    count, period = struct.unpack('BB', ie[5][0:2])
835    logger.info("DTIM count %d  DTIM period %d" % (count, period))
836    if period != 10:
837        raise Exception("Unexpected DTIM period: %d" % period)
838    if count >= period:
839        raise Exception("Unexpected DTIM count: %d" % count)
840
841def test_ap_no_probe_resp(dev, apdev):
842    """AP with Probe Response frame sending from hostapd disabled"""
843    ssid = "no-probe-resp"
844    params = {'ssid': ssid, 'send_probe_response': "0"}
845    hapd = hostapd.add_ap(apdev[0], params)
846    bssid = hapd.own_addr()
847    dev[0].scan_for_bss(bssid, freq="2412", passive=True)
848    dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
849    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
850    bss = dev[0].get_bss(bssid)
851    if 'ie' in bss and 'beacon_ie' in bss and \
852       len(bss['ie']) != len(bss['beacon_ie']):
853        raise Exception("Probe Response frames seen")
854
855def test_ap_long_preamble(dev, apdev):
856    """AP with long preamble"""
857    ssid = "long-preamble"
858    params = {'ssid': ssid, 'preamble': "0",
859              'hw_mode': 'b', 'ieee80211n': '0',
860              'supported_rates': '10', 'basic_rates': '10'}
861    hapd = hostapd.add_ap(apdev[0], params)
862    bssid = hapd.own_addr()
863    dev[0].scan_for_bss(bssid, freq="2412")
864    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
865    hwsim_utils.test_connectivity(dev[0], hapd)
866
867def test_ap_wmm_uapsd(dev, apdev):
868    """AP with U-APSD advertisement"""
869    ssid = "uapsd"
870    params = {'ssid': ssid, 'uapsd_advertisement_enabled': "1"}
871    hapd = hostapd.add_ap(apdev[0], params)
872    bssid = hapd.own_addr()
873    dev[0].scan_for_bss(bssid, freq="2412")
874    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
875    hwsim_utils.test_connectivity(dev[0], hapd)
876
877def test_ap_wowlan_triggers(dev, apdev):
878    """AP with wowlan_triggers"""
879    ssid = "wowlan"
880    params = {'ssid': ssid, 'wowlan_triggers': "any"}
881    hapd = hostapd.add_ap(apdev[0], params)
882    bssid = hapd.own_addr()
883    dev[0].scan_for_bss(bssid, freq="2412")
884    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
885    hwsim_utils.test_connectivity(dev[0], hapd)
886
887def test_ap_notify_mgmt_frames(dev, apdev):
888    """hostapd notify_mgmt_frames configuration enabled"""
889    ssid = "mgmt_frames"
890    params = {'ssid': ssid, 'notify_mgmt_frames': "1"}
891    hapd = hostapd.add_ap(apdev[0], params)
892    bssid = hapd.own_addr()
893    dev[0].scan_for_bss(bssid, freq="2412")
894    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
895    ev = hapd.wait_event(["AP-MGMT-FRAME-RECEIVED"], timeout=5)
896    if ev is None:
897        raise Exception("AP-MGMT-FRAME-RECEIVED wait timed out")
898    if "buf=b0" not in ev:
899        raise Exception("Expected auth request in AP-MGMT-FRAME-RECEIVED")
900
901def test_ap_notify_mgmt_frames_disabled(dev, apdev):
902    """hostapd notify_mgmt_frames configuration disabled"""
903    ssid = "mgmt_frames"
904    params = {'ssid': ssid, 'notify_mgmt_frames': "0"}
905    hapd = hostapd.add_ap(apdev[0], params)
906    bssid = hapd.own_addr()
907    dev[0].scan_for_bss(bssid, freq="2412")
908    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
909    ev = hapd.wait_event(["AP-MGMT-FRAME-RECEIVED"], timeout=0.1)
910    if ev is not None:
911        raise Exception("Unexpected AP-MGMT-FRAME-RECEIVED")
912
913def test_ap_airtime_policy_static(dev, apdev):
914    """Airtime policy - static"""
915    ssid = "test-wpa2-psk"
916    passphrase = 'qwertyuiop'
917    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
918    params['airtime_mode'] = "1"
919    params['airtime_update_interval'] = "200"
920    params['airtime_sta_weight'] = dev[0].own_addr() + " 512"
921    hapd = hostapd.add_ap(apdev[0], params)
922    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
923    dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
924    time.sleep(1)
925
926def test_ap_airtime_policy_per_bss_dynamic(dev, apdev):
927    """Airtime policy - per-BSS dynamic"""
928    ssid = "test-wpa2-psk"
929    passphrase = 'qwertyuiop'
930    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
931    params['airtime_mode'] = "2"
932    params['airtime_update_interval'] = "200"
933    params['airtime_bss_weight'] = "2"
934    hapd = hostapd.add_ap(apdev[0], params)
935    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
936    dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
937    time.sleep(1)
938
939def test_ap_airtime_policy_per_bss_limit(dev, apdev):
940    """Airtime policy - per-BSS limit"""
941    ssid = "test-wpa2-psk"
942    passphrase = 'qwertyuiop'
943    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
944    params['airtime_mode'] = "3"
945    params['airtime_update_interval'] = "200"
946    params['airtime_bss_weight'] = "2"
947    params['airtime_bss_limit'] = "1"
948    hapd = hostapd.add_ap(apdev[0], params)
949    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
950    dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
951    time.sleep(1)
952    hapd.set("force_backlog_bytes", "1")
953    time.sleep(1)
954
955def test_ap_airtime_policy_per_bss_limit_invalid(dev, apdev):
956    """Airtime policy - per-BSS limit (invalid)"""
957    ssid = "test-wpa2-psk"
958    passphrase = 'qwertyuiop'
959    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
960    params['airtime_mode'] = "3"
961    params['airtime_update_interval'] = "0"
962    params['airtime_bss_weight'] = "2"
963    params['airtime_bss_limit'] = "1"
964    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
965    if "FAIL" not in hapd.request("ENABLE"):
966        raise Exception("Invalid airtime policy configuration accepted")
967    hapd.set("airtime_update_interval", "200")
968    hapd.enable()
969    hapd.set("airtime_update_interval", "0")
970    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
971    dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
972    time.sleep(1)
973