1# hostapd control interface
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
7import logging
8logger = logging.getLogger()
9import os
10from remotehost import remote_compatible
11import hostapd
12import hwsim_utils
13from utils import *
14
15@remote_compatible
16def test_hapd_ctrl_status(dev, apdev):
17    """hostapd ctrl_iface STATUS commands"""
18    ssid = "hapd-ctrl"
19    bssid = apdev[0]['bssid']
20    params = hostapd.wpa2_params(ssid=ssid, passphrase="12345678")
21    hapd = hostapd.add_ap(apdev[0], params)
22    status = hapd.get_status()
23    logger.info("STATUS: " + str(status))
24    driver = hapd.get_driver_status()
25    logger.info("STATUS-DRIVER: " + str(driver))
26
27    if status['bss[0]'] != apdev[0]['ifname']:
28        raise Exception("Unexpected bss[0]")
29    if status['ssid[0]'] != ssid:
30        raise Exception("Unexpected ssid[0]")
31    if status['bssid[0]'] != bssid:
32        raise Exception("Unexpected bssid[0]")
33    if status['freq'] != "2412":
34        raise Exception("Unexpected freq")
35    if status['beacon_int'] != "100":
36        raise Exception("Unexpected beacon_int")
37    if status['dtim_period'] != "2":
38        raise Exception("Unexpected dtim_period")
39    if "max_txpower" not in status:
40        raise Exception("Missing max_txpower")
41    if "ht_caps_info" not in status:
42        raise Exception("Missing ht_caps_info")
43
44    if driver['beacon_set'] != "1":
45        raise Exception("Unexpected beacon_set")
46    if driver['addr'] != bssid:
47        raise Exception("Unexpected addr")
48
49@remote_compatible
50def test_hapd_ctrl_p2p_manager(dev, apdev):
51    """hostapd as P2P Device manager"""
52    ssid = "hapd-p2p-mgr"
53    passphrase = "12345678"
54    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
55    params['manage_p2p'] = '1'
56    params['allow_cross_connection'] = '0'
57    hapd = hostapd.add_ap(apdev[0], params)
58    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
59    addr = dev[0].own_addr()
60    if "OK" not in hapd.request("DEAUTHENTICATE " + addr + " p2p=2"):
61        raise Exception("DEAUTHENTICATE command failed")
62    dev[0].wait_disconnected(timeout=5)
63    dev[0].wait_connected(timeout=10, error="Re-connection timed out")
64
65    if "OK" not in hapd.request("DISASSOCIATE " + addr + " p2p=2"):
66        raise Exception("DISASSOCIATE command failed")
67    dev[0].wait_disconnected(timeout=5)
68    dev[0].wait_connected(timeout=10, error="Re-connection timed out")
69
70@remote_compatible
71def test_hapd_ctrl_sta(dev, apdev):
72    """hostapd and STA ctrl_iface commands"""
73    try:
74        run_hapd_ctrl_sta(dev, apdev)
75    finally:
76        dev[0].request("VENDOR_ELEM_REMOVE 13 *")
77
78def run_hapd_ctrl_sta(dev, apdev):
79    ssid = "hapd-ctrl-sta"
80    passphrase = "12345678"
81    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
82    hapd = hostapd.add_ap(apdev[0], params)
83    hglobal = hostapd.HostapdGlobal(apdev[0])
84    dev[0].request("VENDOR_ELEM_ADD 13 2102ff02")
85    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
86    addr = dev[0].own_addr()
87    ev = hapd.wait_event(["AP-STA-CONNECTED"], timeout=2)
88    if ev is None:
89        raise Exception("No hostapd per-interface event reported")
90    ev2 = hglobal.wait_event(["AP-STA-CONNECTED"], timeout=2)
91    if ev2 is None:
92        raise Exception("No hostapd global event reported")
93    if not ev2.startswith("IFNAME=" + apdev[0]['ifname'] + " <"):
94        raise Exception("Unexpected global event prefix: " + ev2)
95    if ev not in ev2:
96        raise Exception("Event mismatch (%s,%s)" % (ev, ev2))
97    if "FAIL" in hapd.request("STA " + addr):
98        raise Exception("Unexpected STA failure")
99    if "FAIL" not in hapd.request("STA " + addr + " eapol"):
100        raise Exception("Unexpected STA-eapol success")
101    if "FAIL" not in hapd.request("STA " + addr + " foo"):
102        raise Exception("Unexpected STA-foo success")
103    if "FAIL" not in hapd.request("STA 00:11:22:33:44"):
104        raise Exception("Unexpected STA success")
105    if "FAIL" not in hapd.request("STA 00:11:22:33:44:55"):
106        raise Exception("Unexpected STA success")
107
108    if len(hapd.request("STA-NEXT " + addr).splitlines()) > 0:
109        raise Exception("Unexpected STA-NEXT result")
110    if "FAIL" not in hapd.request("STA-NEXT 00:11:22:33:44"):
111        raise Exception("Unexpected STA-NEXT success")
112
113    sta = hapd.get_sta(addr)
114    logger.info("STA: " + str(sta))
115    if "ext_capab" not in sta:
116        raise Exception("Missing ext_capab in STA output")
117    if 'ht_caps_info' not in sta:
118        raise Exception("Missing ht_caps_info in STA output")
119    if 'min_txpower' not in sta:
120        raise Exception("Missing min_txpower in STA output")
121    if 'max_txpower' not in sta:
122        raise Exception("Missing min_txpower in STA output")
123    if sta['min_txpower'] != '-1':
124        raise Exception("Unxpected min_txpower value: " + sta['min_txpower'])
125    if sta['max_txpower'] != '2':
126        raise Exception("Unxpected max_txpower value: " + sta['max_txpower'])
127
128@remote_compatible
129def test_hapd_ctrl_disconnect(dev, apdev):
130    """hostapd and disconnection ctrl_iface commands"""
131    ssid = "hapd-ctrl"
132    passphrase = "12345678"
133    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
134    hapd = hostapd.add_ap(apdev[0], params)
135    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
136    addr = dev[0].p2p_dev_addr()
137
138    if "FAIL" not in hapd.request("DEAUTHENTICATE 00:11:22:33:44"):
139        raise Exception("Unexpected DEAUTHENTICATE success")
140
141    if "OK" not in hapd.request("DEAUTHENTICATE ff:ff:ff:ff:ff:ff"):
142        raise Exception("Unexpected DEAUTHENTICATE failure")
143    dev[0].wait_disconnected(timeout=5)
144    dev[0].wait_connected(timeout=10, error="Re-connection timed out")
145
146    if "FAIL" not in hapd.request("DISASSOCIATE 00:11:22:33:44"):
147        raise Exception("Unexpected DISASSOCIATE success")
148
149    if "OK" not in hapd.request("DISASSOCIATE ff:ff:ff:ff:ff:ff"):
150        raise Exception("Unexpected DISASSOCIATE failure")
151    dev[0].wait_disconnected(timeout=5)
152    dev[0].wait_connected(timeout=10, error="Re-connection timed out")
153
154@remote_compatible
155def test_hapd_ctrl_chan_switch(dev, apdev):
156    """hostapd and CHAN_SWITCH ctrl_iface command"""
157    ssid = "hapd-ctrl"
158    params = {"ssid": ssid}
159    hapd = hostapd.add_ap(apdev[0], params)
160    if "FAIL" not in hapd.request("CHAN_SWITCH "):
161        raise Exception("Unexpected CHAN_SWITCH success")
162    if "FAIL" not in hapd.request("CHAN_SWITCH qwerty 2422"):
163        raise Exception("Unexpected CHAN_SWITCH success")
164    if "FAIL" not in hapd.request("CHAN_SWITCH 5 qwerty"):
165        raise Exception("Unexpected CHAN_SWITCH success")
166    if "FAIL" not in hapd.request("CHAN_SWITCH 0 2432 center_freq1=123 center_freq2=234 bandwidth=1000 sec_channel_offset=20 ht vht"):
167        raise Exception("Unexpected CHAN_SWITCH success")
168
169@remote_compatible
170def test_hapd_ctrl_level(dev, apdev):
171    """hostapd and LEVEL ctrl_iface command"""
172    ssid = "hapd-ctrl"
173    params = {"ssid": ssid}
174    hapd = hostapd.add_ap(apdev[0], params)
175    if "FAIL" not in hapd.request("LEVEL 0"):
176        raise Exception("Unexpected LEVEL success on non-monitor interface")
177
178@remote_compatible
179def test_hapd_ctrl_new_sta(dev, apdev):
180    """hostapd and NEW_STA ctrl_iface command"""
181    ssid = "hapd-ctrl"
182    params = {"ssid": ssid}
183    hapd = hostapd.add_ap(apdev[0], params)
184    if "FAIL" not in hapd.request("NEW_STA 00:11:22:33:44"):
185        raise Exception("Unexpected NEW_STA success")
186    if "OK" not in hapd.request("NEW_STA 00:11:22:33:44:55"):
187        raise Exception("Unexpected NEW_STA failure")
188    if "AUTHORIZED" not in hapd.request("STA 00:11:22:33:44:55"):
189        raise Exception("Unexpected NEW_STA STA status")
190    if "OK" not in hapd.request("NEW_STA 00:11:22:33:44:55"):
191        raise Exception("Unexpected NEW_STA failure")
192    with alloc_fail(hapd, 1, "ap_sta_add;hostapd_ctrl_iface_new_sta"):
193        if "FAIL" not in hapd.request("NEW_STA 00:11:22:33:44:66"):
194            raise Exception("Unexpected NEW_STA success during OOM")
195
196@remote_compatible
197def test_hapd_ctrl_get(dev, apdev):
198    """hostapd and GET ctrl_iface command"""
199    ssid = "hapd-ctrl"
200    params = {"ssid": ssid}
201    hapd = hostapd.add_ap(apdev[0], params)
202    if "FAIL" not in hapd.request("GET foo"):
203        raise Exception("Unexpected GET success")
204    if "FAIL" in hapd.request("GET version"):
205        raise Exception("Unexpected GET version failure")
206
207@remote_compatible
208def test_hapd_ctrl_unknown(dev, apdev):
209    """hostapd and unknown ctrl_iface command"""
210    ssid = "hapd-ctrl"
211    params = {"ssid": ssid}
212    hapd = hostapd.add_ap(apdev[0], params)
213    if "UNKNOWN COMMAND" not in hapd.request("FOO"):
214        raise Exception("Unexpected response")
215
216@remote_compatible
217def test_hapd_ctrl_hs20_wnm_notif(dev, apdev):
218    """hostapd and HS20_WNM_NOTIF ctrl_iface command"""
219    ssid = "hapd-ctrl"
220    params = {"ssid": ssid}
221    hapd = hostapd.add_ap(apdev[0], params)
222    if "FAIL" not in hapd.request("HS20_WNM_NOTIF 00:11:22:33:44 http://example.com/"):
223        raise Exception("Unexpected HS20_WNM_NOTIF success")
224    if "FAIL" not in hapd.request("HS20_WNM_NOTIF 00:11:22:33:44:55http://example.com/"):
225        raise Exception("Unexpected HS20_WNM_NOTIF success")
226
227@remote_compatible
228def test_hapd_ctrl_hs20_deauth_req(dev, apdev):
229    """hostapd and HS20_DEAUTH_REQ ctrl_iface command"""
230    ssid = "hapd-ctrl"
231    params = {"ssid": ssid}
232    hapd = hostapd.add_ap(apdev[0], params)
233    if "FAIL" not in hapd.request("HS20_DEAUTH_REQ 00:11:22:33:44 1 120 http://example.com/"):
234        raise Exception("Unexpected HS20_DEAUTH_REQ success")
235    if "FAIL" not in hapd.request("HS20_DEAUTH_REQ 00:11:22:33:44:55"):
236        raise Exception("Unexpected HS20_DEAUTH_REQ success")
237    if "FAIL" not in hapd.request("HS20_DEAUTH_REQ 00:11:22:33:44:55 1"):
238        raise Exception("Unexpected HS20_DEAUTH_REQ success")
239
240@remote_compatible
241def test_hapd_ctrl_disassoc_imminent(dev, apdev):
242    """hostapd and DISASSOC_IMMINENT ctrl_iface command"""
243    ssid = "hapd-ctrl"
244    params = {"ssid": ssid}
245    hapd = hostapd.add_ap(apdev[0], params)
246    if "FAIL" not in hapd.request("DISASSOC_IMMINENT 00:11:22:33:44"):
247        raise Exception("Unexpected DISASSOC_IMMINENT success")
248    if "FAIL" not in hapd.request("DISASSOC_IMMINENT 00:11:22:33:44:55"):
249        raise Exception("Unexpected DISASSOC_IMMINENT success")
250    if "FAIL" not in hapd.request("DISASSOC_IMMINENT 00:11:22:33:44:55 2"):
251        raise Exception("Unexpected DISASSOC_IMMINENT success")
252    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
253    addr = dev[0].p2p_interface_addr()
254    if "OK" not in hapd.request("DISASSOC_IMMINENT " + addr + " 2"):
255        raise Exception("Unexpected DISASSOC_IMMINENT failure")
256    ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
257    if ev is None:
258        raise Exception("Scan timed out")
259
260@remote_compatible
261def test_hapd_ctrl_ess_disassoc(dev, apdev):
262    """hostapd and ESS_DISASSOC ctrl_iface command"""
263    ssid = "hapd-ctrl"
264    params = {"ssid": ssid}
265    hapd = hostapd.add_ap(apdev[0], params)
266    if "FAIL" not in hapd.request("ESS_DISASSOC 00:11:22:33:44"):
267        raise Exception("Unexpected ESS_DISASSOCT success")
268    if "FAIL" not in hapd.request("ESS_DISASSOC 00:11:22:33:44:55"):
269        raise Exception("Unexpected ESS_DISASSOC success")
270    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
271    addr = dev[0].p2p_interface_addr()
272    if "FAIL" not in hapd.request("ESS_DISASSOC " + addr):
273        raise Exception("Unexpected ESS_DISASSOC success")
274    if "FAIL" not in hapd.request("ESS_DISASSOC " + addr + " -1"):
275        raise Exception("Unexpected ESS_DISASSOC success")
276    if "FAIL" not in hapd.request("ESS_DISASSOC " + addr + " 1"):
277        raise Exception("Unexpected ESS_DISASSOC success")
278    if "OK" not in hapd.request("ESS_DISASSOC " + addr + " 20 http://example.com/"):
279        raise Exception("Unexpected ESS_DISASSOC failure")
280    ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
281    if ev is None:
282        raise Exception("Scan timed out")
283
284def test_hapd_ctrl_set_deny_mac_file(dev, apdev):
285    """hostapd and SET deny_mac_file ctrl_iface command"""
286    ssid = "hapd-ctrl"
287    filename = hostapd.acl_file(dev, apdev, 'hostapd.macaddr')
288    params = {"ssid": ssid}
289    hapd = hostapd.add_ap(apdev[0], params)
290    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
291    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
292    hapd.send_file(filename, filename)
293    if "OK" not in hapd.request("SET deny_mac_file " + filename):
294        raise Exception("Unexpected SET failure")
295    dev[0].wait_disconnected(timeout=15)
296    ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"], 1)
297    if ev is not None:
298        raise Exception("Unexpected disconnection")
299    if filename.startswith('/tmp/'):
300        os.unlink(filename)
301
302def test_hapd_ctrl_set_accept_mac_file(dev, apdev):
303    """hostapd and SET accept_mac_file ctrl_iface command"""
304    ssid = "hapd-ctrl"
305    filename = hostapd.acl_file(dev, apdev, 'hostapd.macaddr')
306    params = {"ssid": ssid}
307    hapd = hostapd.add_ap(apdev[0], params)
308    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
309    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
310    hapd.send_file(filename, filename)
311    hapd.request("SET macaddr_acl 1")
312    if "OK" not in hapd.request("SET accept_mac_file " + filename):
313        raise Exception("Unexpected SET failure")
314    dev[1].wait_disconnected(timeout=15)
315    ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], 1)
316    if ev is not None:
317        raise Exception("Unexpected disconnection")
318    if filename.startswith('/tmp/'):
319        os.unlink(filename)
320
321def test_hapd_ctrl_set_accept_mac_file_vlan(dev, apdev):
322    """hostapd and SET accept_mac_file ctrl_iface command (VLAN ID)"""
323    ssid = "hapd-ctrl"
324    filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
325    params = {"ssid": ssid}
326    hapd = hostapd.add_ap(apdev[0], params)
327    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
328    dev[1].connect(ssid, key_mgmt="NONE", scan_freq="2412")
329    hapd.send_file(filename, filename)
330    hapd.request("SET macaddr_acl 1")
331    if "OK" not in hapd.request("SET accept_mac_file " + filename):
332        raise Exception("Unexpected SET failure")
333    dev[1].wait_disconnected(timeout=15)
334    dev[0].wait_disconnected(timeout=15)
335    if filename.startswith('/tmp/'):
336        os.unlink(filename)
337
338@remote_compatible
339def test_hapd_ctrl_set_error_cases(dev, apdev):
340    """hostapd and SET error cases"""
341    ssid = "hapd-ctrl"
342    params = {"ssid": ssid}
343    hapd = hostapd.add_ap(apdev[0], params)
344    errors = ["wpa_key_mgmt FOO",
345              "wpa_key_mgmt WPA-PSK   \t  FOO",
346              "wpa_key_mgmt    \t  ",
347              "wpa_pairwise FOO",
348              "wpa_pairwise   \t   ",
349              'wep_key0 "',
350              'wep_key0 "abcde',
351              "wep_key0 1",
352              "wep_key0 12q3456789",
353              "wep_key_len_broadcast 20",
354              "wep_rekey_period -1",
355              "wep_default_key 4",
356              "r0kh 02:00:00:00:03:0q nas1.w1.fi 100102030405060708090a0b0c0d0e0f100102030405060708090a0b0c0d0e0f",
357              "r0kh 02:00:00:00:03:00 12345678901234567890123456789012345678901234567890.nas1.w1.fi 100102030405060708090a0b0c0d0e0f100102030405060708090a0b0c0d0e0f",
358              "r0kh 02:00:00:00:03:00 nas1.w1.fi 100q02030405060708090a0b0c0d0e0f100q02030405060708090a0b0c0d0e0f",
359              "r1kh 02:00:00:00:04:q0 00:01:02:03:04:06 200102030405060708090a0b0c0d0e0f200102030405060708090a0b0c0d0e0f",
360              "r1kh 02:00:00:00:04:00 00:01:02:03:04:q6 200102030405060708090a0b0c0d0e0f200102030405060708090a0b0c0d0e0f",
361              "r1kh 02:00:00:00:04:00 00:01:02:03:04:06 2q0102030405060708090a0b0c0d0e0f2q0102030405060708090a0b0c0d0e0f",
362              "roaming_consortium 1",
363              "roaming_consortium 12",
364              "roaming_consortium 112233445566778899aabbccddeeff00",
365              'venue_name P"engExample venue"',
366              'venue_name P"engExample venue',
367              "venue_name engExample venue",
368              "venue_name e:Example venue",
369              "venue_name eng1:Example venue",
370              "venue_name eng:Example venue 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
371              "anqp_3gpp_cell_net abc",
372              "anqp_3gpp_cell_net ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;",
373              "anqp_3gpp_cell_net 244",
374              "anqp_3gpp_cell_net 24,123",
375              "anqp_3gpp_cell_net 244,1",
376              "anqp_3gpp_cell_net 244,1234",
377              "nai_realm 0",
378              "nai_realm 0,1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.nas1.w1.fi",
379              "nai_realm 0,example.org,1,2,3,4,5,6,7,8",
380              "nai_realm 0,example.org,1[1:1][2:2][3:3][4:4][5:5]",
381              "nai_realm 0,example.org,1[1]",
382              "nai_realm 0,example.org,1[1:1",
383              "nai_realm 0,a.example.org;b.example.org;c.example.org;d.example.org;e.example.org;f.example.org;g.example.org;h.example.org;i.example.org;j.example.org;k.example.org",
384              "qos_map_set 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60",
385              "qos_map_set 53,2,22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,255,300",
386              "qos_map_set 53,2,22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,255,-1",
387              "qos_map_set 53,2,22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,255,255,1",
388              "qos_map_set 1",
389              "qos_map_set 1,2",
390              "hs20_conn_capab 1",
391              "hs20_conn_capab 6:22",
392              "hs20_wan_metrics 0q:8000:1000:80:240:3000",
393              "hs20_wan_metrics 01",
394              "hs20_wan_metrics 01:8000",
395              "hs20_wan_metrics 01:8000:1000",
396              "hs20_wan_metrics 01:8000:1000:80",
397              "hs20_wan_metrics 01:8000:1000:80:240",
398              "hs20_oper_friendly_name eng1:Example",
399              "hs20_icon 32",
400              "hs20_icon 32:32",
401              "hs20_icon 32:32:eng",
402              "hs20_icon 32:32:eng:image/png",
403              "hs20_icon 32:32:eng:image/png:icon32",
404              "hs20_icon 32:32:eng:image/png:123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890:/tmp/icon32.png",
405              "hs20_icon 32:32:eng:image/png:name:/tmp/123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.png",
406              "osu_ssid ",
407              "osu_ssid P",
408              'osu_ssid P"abc',
409              'osu_ssid "1234567890123456789012345678901234567890"',
410              "osu_friendly_name eng:Example",
411              "osu_nai anonymous@example.com",
412              "osu_nai2 anonymous@example.com",
413              "osu_method_list 1 0",
414              "osu_icon foo",
415              "osu_service_desc eng:Example services",
416              "ssid 1234567890123456789012345678901234567890",
417              "pac_opaque_encr_key 123456",
418              "eap_fast_a_id 12345",
419              "eap_fast_a_id 12345q",
420              "own_ip_addr foo",
421              "auth_server_addr foo2",
422              "auth_server_shared_secret ",
423              "acct_server_addr foo3",
424              "acct_server_shared_secret ",
425              "radius_auth_req_attr 123::",
426              "radius_acct_req_attr 123::",
427              "radius_das_client 192.168.1.123",
428              "radius_das_client 192.168.1.1a foo",
429              "auth_algs 0",
430              "max_num_sta -1",
431              "max_num_sta 1000000",
432              "wpa_passphrase 1234567",
433              "wpa_passphrase 1234567890123456789012345678901234567890123456789012345678901234",
434              "wpa_psk 1234567890123456789012345678901234567890123456789012345678901234a",
435              "wpa_psk 12345678901234567890123456789012345678901234567890123456789012",
436              "wpa_psk_radius 123",
437              "wpa_pairwise NONE",
438              "wpa_pairwise WEP40",
439              "wpa_pairwise WEP104",
440              "rsn_pairwise NONE",
441              "rsn_pairwise WEP40",
442              "rsn_pairwise WEP104",
443              "mobility_domain 01",
444              "r1_key_holder 0011223344",
445              "ctrl_interface_group nosuchgrouphere",
446              "hw_mode foo",
447              "wps_rf_bands foo",
448              "beacon_int 0",
449              "beacon_int 65536",
450              "acs_num_scans 0",
451              "acs_num_scans 101",
452              "rts_threshold -2",
453              "rts_threshold 65536",
454              "fragm_threshold -2",
455              "fragm_threshold 2347",
456              "send_probe_response -1",
457              "send_probe_response 2",
458              "vlan_naming -1",
459              "vlan_naming 10000000",
460              "group_mgmt_cipher FOO",
461              "assoc_sa_query_max_timeout 0",
462              "assoc_sa_query_retry_timeout 0",
463              "wps_state -1",
464              "wps_state 3",
465              "uuid FOO",
466              "device_name 1234567890123456789012345678901234567890",
467              "manufacturer 1234567890123456789012345678901234567890123456789012345678901234567890",
468              "model_name 1234567890123456789012345678901234567890",
469              "model_number 1234567890123456789012345678901234567890",
470              "serial_number 1234567890123456789012345678901234567890",
471              "device_type FOO",
472              "os_version 1",
473              "ap_settings /tmp/does/not/exist/ap-settings.foo",
474              "wps_nfc_dev_pw_id 4",
475              "wps_nfc_dev_pw_id 100000",
476              "time_zone A",
477              "access_network_type -1",
478              "access_network_type 16",
479              "hessid 00:11:22:33:44",
480              "network_auth_type 0q",
481              "ipaddr_type_availability 1q",
482              "hs20_operating_class 0",
483              "hs20_operating_class 0q",
484              "bss_load_test ",
485              "bss_load_test 12",
486              "bss_load_test 12:80",
487              "vendor_elements 0",
488              "vendor_elements 0q",
489              "assocresp_elements 0",
490              "assocresp_elements 0q",
491              "local_pwr_constraint -1",
492              "local_pwr_constraint 256",
493              "wmm_ac_bk_cwmin -1",
494              "wmm_ac_be_cwmin 16",
495              "wmm_ac_vi_cwmax -1",
496              "wmm_ac_vo_cwmax 16",
497              "wmm_ac_foo_cwmax 6",
498              "wmm_ac_bk_aifs 0",
499              "wmm_ac_bk_aifs 256",
500              "wmm_ac_bk_txop_limit -1",
501              "wmm_ac_bk_txop_limit 65536",
502              "wmm_ac_bk_acm -1",
503              "wmm_ac_bk_acm 2",
504              "wmm_ac_bk_foo 2",
505              "tx_queue_foo_aifs 3",
506              "tx_queue_data3_cwmin 4",
507              "tx_queue_data3_cwmax 4",
508              "tx_queue_data3_aifs -4",
509              "tx_queue_data3_foo 1"]
510    for e in errors:
511        if "FAIL" not in hapd.request("SET " + e):
512            raise Exception("Unexpected SET success: '%s'" % e)
513
514    if "OK" not in hapd.request("SET osu_server_uri https://example.com/"):
515        raise Exception("Unexpected SET osu_server_uri failure")
516    if "OK" not in hapd.request("SET osu_friendly_name eng:Example"):
517        raise Exception("Unexpected SET osu_friendly_name failure")
518
519    errors = ["osu_friendly_name eng1:Example",
520              "osu_service_desc eng1:Example services"]
521    for e in errors:
522        if "FAIL" not in hapd.request("SET " + e):
523            raise Exception("Unexpected SET success: '%s'" % e)
524
525    no_err = ["wps_nfc_dh_pubkey 0",
526              "wps_nfc_dh_privkey 0q",
527              "wps_nfc_dev_pw 012",
528              "manage_p2p 0",
529              "disassoc_low_ack 0",
530              "network_auth_type 01",
531              "tdls_prohibit 0",
532              "tdls_prohibit_chan_switch 0"]
533    for e in no_err:
534        if "OK" not in hapd.request("SET " + e):
535            raise Exception("Unexpected SET failure: '%s'" % e)
536
537@remote_compatible
538def test_hapd_ctrl_global(dev, apdev):
539    """hostapd and GET ctrl_iface command"""
540    ssid = "hapd-ctrl"
541    params = {"ssid": ssid}
542    ifname = apdev[0]['ifname']
543    hapd = hostapd.add_ap(apdev[0], params)
544    hapd_global = hostapd.HostapdGlobal(apdev[0])
545    res = hapd_global.request("IFNAME=" + ifname + " PING")
546    if "PONG" not in res:
547            raise Exception("Could not ping hostapd interface " + ifname + " via global control interface")
548    res = hapd_global.request("IFNAME=" + ifname + " GET version")
549    if "FAIL" in res:
550           raise Exception("Could not get hostapd version for " + ifname + " via global control interface")
551    res = hapd_global.request("IFNAME=no-such-ifname GET version")
552    if "FAIL-NO-IFNAME-MATCH" not in res:
553           raise Exception("Invalid ifname not reported")
554    res = hapd_global.request("INTERFACES")
555    if "FAIL" in res:
556        raise Exception("INTERFACES command failed")
557    if apdev[0]['ifname'] not in res.splitlines():
558        raise Exception("AP interface missing from INTERFACES")
559    res = hapd_global.request("INTERFACES ctrl")
560    if "FAIL" in res:
561        raise Exception("INTERFACES ctrl command failed")
562    if apdev[0]['ifname'] + " ctrl_iface=" not in res:
563        raise Exception("AP interface missing from INTERFACES ctrl")
564
565    if "FAIL" not in hapd_global.request("DETACH"):
566        raise Exception("DETACH succeeded unexpectedly")
567
568def dup_network(hapd_global, src, dst, param):
569    res = hapd_global.request("DUP_NETWORK %s %s %s" % (src, dst, param))
570    if "OK" not in res:
571        raise Exception("Could not dup %s param from %s to %s" % (param, src,
572                                                                  dst))
573
574def test_hapd_dup_network_global_wpa2(dev, apdev):
575    """hostapd and DUP_NETWORK command (WPA2)"""
576    passphrase = "12345678"
577    src_ssid = "hapd-ctrl-src"
578    dst_ssid = "hapd-ctrl-dst"
579
580    src_params = hostapd.wpa2_params(ssid=src_ssid, passphrase=passphrase)
581    src_ifname = apdev[0]['ifname']
582    src_hapd = hostapd.add_ap(apdev[0], src_params)
583
584    dst_params = {"ssid": dst_ssid}
585    dst_ifname = apdev[1]['ifname']
586    dst_hapd = hostapd.add_ap(apdev[1], dst_params, no_enable=True)
587
588    hapd_global = hostapd.HostapdGlobal()
589
590    for param in ["wpa", "wpa_passphrase", "wpa_key_mgmt", "rsn_pairwise"]:
591        dup_network(hapd_global, src_ifname, dst_ifname, param)
592
593    dst_hapd.enable()
594
595    dev[0].connect(dst_ssid, psk=passphrase, proto="RSN", pairwise="CCMP",
596                   scan_freq="2412")
597    addr = dev[0].own_addr()
598    if "FAIL" in dst_hapd.request("STA " + addr):
599            raise Exception("Could not connect using duplicated wpa params")
600
601    tests = ["a",
602             "no-such-ifname no-such-ifname",
603             src_ifname + " no-such-ifname",
604             src_ifname + " no-such-ifname no-such-param",
605             src_ifname + " " + dst_ifname + " no-such-param"]
606    for t in tests:
607        if "FAIL" not in hapd_global.request("DUP_NETWORK " + t):
608            raise Exception("Invalid DUP_NETWORK accepted: " + t)
609    with alloc_fail(src_hapd, 1, "hostapd_ctrl_iface_dup_param"):
610        if "FAIL" not in hapd_global.request("DUP_NETWORK %s %s wpa" % (src_ifname, dst_ifname)):
611            raise Exception("DUP_NETWORK accepted during OOM")
612
613def test_hapd_dup_network_global_wpa(dev, apdev):
614    """hostapd and DUP_NETWORK command (WPA)"""
615    skip_with_fips(dev[0])
616    skip_without_tkip(dev[0])
617    psk = '602e323e077bc63bd80307ef4745b754b0ae0a925c2638ecd13a794b9527b9e6'
618    src_ssid = "hapd-ctrl-src"
619    dst_ssid = "hapd-ctrl-dst"
620
621    src_params = hostapd.wpa_params(ssid=src_ssid)
622    src_params['wpa_psk'] = psk
623    src_ifname = apdev[0]['ifname']
624    src_hapd = hostapd.add_ap(apdev[0], src_params)
625
626    dst_params = {"ssid": dst_ssid}
627    dst_ifname = apdev[1]['ifname']
628    dst_hapd = hostapd.add_ap(apdev[1], dst_params, no_enable=True)
629
630    hapd_global = hostapd.HostapdGlobal()
631
632    for param in ["wpa", "wpa_psk", "wpa_key_mgmt", "wpa_pairwise"]:
633        dup_network(hapd_global, src_ifname, dst_ifname, param)
634
635    dst_hapd.enable()
636
637    dev[0].connect(dst_ssid, raw_psk=psk, proto="WPA", pairwise="TKIP",
638                   scan_freq="2412")
639    addr = dev[0].own_addr()
640    if "FAIL" in dst_hapd.request("STA " + addr):
641            raise Exception("Could not connect using duplicated wpa params")
642
643@remote_compatible
644def test_hapd_ctrl_log_level(dev, apdev):
645    """hostapd ctrl_iface LOG_LEVEL"""
646    hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
647    level = hapd.request("LOG_LEVEL")
648    if "Current level: MSGDUMP" not in level:
649        raise Exception("Unexpected debug level(1): " + level)
650    if "Timestamp: 1" not in level:
651        raise Exception("Unexpected timestamp(1): " + level)
652
653    if "OK" not in hapd.request("LOG_LEVEL  MSGDUMP  0"):
654        raise Exception("LOG_LEVEL failed")
655    level = hapd.request("LOG_LEVEL")
656    if "Current level: MSGDUMP" not in level:
657        raise Exception("Unexpected debug level(2): " + level)
658    if "Timestamp: 0" not in level:
659        raise Exception("Unexpected timestamp(2): " + level)
660
661    if "OK" not in hapd.request("LOG_LEVEL  MSGDUMP  1"):
662        raise Exception("LOG_LEVEL failed")
663    level = hapd.request("LOG_LEVEL")
664    if "Current level: MSGDUMP" not in level:
665        raise Exception("Unexpected debug level(3): " + level)
666    if "Timestamp: 1" not in level:
667        raise Exception("Unexpected timestamp(3): " + level)
668
669    if "FAIL" not in hapd.request("LOG_LEVEL FOO"):
670        raise Exception("Invalid LOG_LEVEL accepted")
671
672    for lev in ["EXCESSIVE", "MSGDUMP", "DEBUG", "INFO", "WARNING", "ERROR"]:
673        if "OK" not in hapd.request("LOG_LEVEL " + lev):
674            raise Exception("LOG_LEVEL failed for " + lev)
675        level = hapd.request("LOG_LEVEL")
676        if "Current level: " + lev not in level:
677            raise Exception("Unexpected debug level: " + level)
678
679    if "OK" not in hapd.request("LOG_LEVEL  MSGDUMP  1"):
680        raise Exception("LOG_LEVEL failed")
681    level = hapd.request("LOG_LEVEL")
682    if "Current level: MSGDUMP" not in level:
683        raise Exception("Unexpected debug level(3): " + level)
684    if "Timestamp: 1" not in level:
685        raise Exception("Unexpected timestamp(3): " + level)
686
687@remote_compatible
688def test_hapd_ctrl_disconnect_no_tx(dev, apdev):
689    """hostapd disconnecting STA without transmitting Deauth/Disassoc"""
690    ssid = "hapd-test"
691    passphrase = "12345678"
692    params = hostapd.wpa2_params(ssid=ssid, passphrase=passphrase)
693    hapd = hostapd.add_ap(apdev[0], params)
694    bssid = apdev[0]['bssid']
695    dev[0].connect(ssid, psk=passphrase, scan_freq="2412")
696    addr0 = dev[0].own_addr()
697    dev[1].connect(ssid, psk=passphrase, scan_freq="2412")
698    addr1 = dev[1].own_addr()
699
700    # Disconnect the STA without sending out Deauthentication frame
701    if "OK" not in hapd.request("DEAUTHENTICATE " + addr0 + " tx=0"):
702        raise Exception("DEAUTHENTICATE command failed")
703    # Force disconnection due to AP receiving a frame from not-asssociated STA
704    dev[0].request("DATA_TEST_CONFIG 1")
705    dev[0].request("DATA_TEST_TX " + bssid + " " + addr0)
706    ev = dev[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
707    dev[0].request("DATA_TEST_CONFIG 0")
708    if ev is None:
709        raise Exception("Disconnection event not seen after TX attempt")
710    if "reason=7" not in ev:
711        raise Exception("Unexpected disconnection reason: " + ev)
712
713    # Disconnect the STA without sending out Disassociation frame
714    if "OK" not in hapd.request("DISASSOCIATE " + addr1 + " tx=0"):
715        raise Exception("DISASSOCIATE command failed")
716    # Force disconnection due to AP receiving a frame from not-asssociated STA
717    dev[1].request("DATA_TEST_CONFIG 1")
718    dev[1].request("DATA_TEST_TX " + bssid + " " + addr1)
719    ev = dev[1].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=5)
720    dev[1].request("DATA_TEST_CONFIG 0")
721    if ev is None:
722        raise Exception("Disconnection event not seen after TX attempt")
723    if "reason=7" not in ev:
724        raise Exception("Unexpected disconnection reason: " + ev)
725
726def test_hapd_ctrl_mib(dev, apdev):
727    """hostapd and MIB ctrl_iface command with open network"""
728    ssid = "hapd-ctrl"
729    params = {"ssid": ssid}
730    hapd = hostapd.add_ap(apdev[0], params)
731
732    mib = hapd.request("MIB")
733    if len(mib) != 0:
734        raise Exception("Unexpected MIB response: " + mib)
735
736    mib = hapd.request("MIB radius_server")
737    if len(mib) != 0:
738        raise Exception("Unexpected 'MIB radius_server' response: " + mib)
739
740    if "FAIL" not in hapd.request("MIB foo"):
741        raise Exception("'MIB foo' succeeded")
742
743    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
744
745    mib = hapd.request("MIB")
746    if "FAIL" in mib:
747        raise Exception("Unexpected MIB response: " + mib)
748
749    mib = hapd.request("MIB radius_server")
750    if len(mib) != 0:
751        raise Exception("Unexpected 'MIB radius_server' response: " + mib)
752
753    if "FAIL" not in hapd.request("MIB foo"):
754        raise Exception("'MIB foo' succeeded")
755
756def test_hapd_ctrl_not_yet_fully_enabled(dev, apdev):
757    """hostapd and ctrl_iface commands when BSS not yet fully enabled"""
758    ssid = "hapd-ctrl"
759    params = {"ssid": ssid}
760    hapd = hostapd.add_ap(apdev[0], params, no_enable=True)
761
762    if not hapd.ping():
763        raise Exception("PING failed")
764    if "FAIL" in hapd.request("MIB"):
765        raise Exception("MIB failed")
766    if len(hapd.request("MIB radius_server")) != 0:
767        raise Exception("Unexpected 'MIB radius_server' response")
768    if "state=UNINITIALIZED" not in hapd.request("STATUS"):
769        raise Exception("Unexpected STATUS response")
770    if "FAIL" not in hapd.request("STATUS-DRIVER"):
771        raise Exception("Unexpected response to STATUS-DRIVER")
772    if len(hapd.request("STA-FIRST")) != 0:
773        raise Exception("Unexpected response to STA-FIRST")
774    if "FAIL" not in hapd.request("STA ff:ff:ff:ff:ff:ff"):
775        raise Exception("Unexpected response to STA")
776    cmds = ["NEW_STA 02:ff:ff:ff:ff:ff",
777            "DEAUTHENTICATE 02:ff:ff:ff:ff:ff",
778            "DEAUTHENTICATE 02:ff:ff:ff:ff:ff test=0",
779            "DEAUTHENTICATE 02:ff:ff:ff:ff:ff p2p=0",
780            "DEAUTHENTICATE 02:ff:ff:ff:ff:ff tx=0",
781            "DISASSOCIATE 02:ff:ff:ff:ff:ff",
782            "DISASSOCIATE 02:ff:ff:ff:ff:ff test=0",
783            "DISASSOCIATE 02:ff:ff:ff:ff:ff p2p=0",
784            "DISASSOCIATE 02:ff:ff:ff:ff:ff tx=0",
785            "SA_QUERY 02:ff:ff:ff:ff:ff",
786            "WPS_PIN any 12345670",
787            "WPS_PBC",
788            "WPS_CANCEL",
789            "WPS_AP_PIN random",
790            "WPS_AP_PIN disable",
791            "WPS_CHECK_PIN 123456789",
792            "WPS_GET_STATUS",
793            "WPS_NFC_TAG_READ 00",
794            "WPS_NFC_CONFIG_TOKEN NDEF",
795            "WPS_NFC_TOKEN WPS",
796            "NFC_GET_HANDOVER_SEL NDEF WPS-CR",
797            "NFC_REPORT_HANDOVER RESP WPS 00 00",
798            "SET_QOS_MAP_SET 22,6,8,15,0,7,255,255,16,31,32,39,255,255,40,47,48,55",
799            "SEND_QOS_MAP_CONF 02:ff:ff:ff:ff:ff",
800            "HS20_WNM_NOTIF 02:ff:ff:ff:ff:ff https://example.com/",
801            "HS20_DEAUTH_REQ 02:ff:ff:ff:ff:ff 1 120 https://example.com/",
802            "DISASSOC_IMMINENT 02:ff:ff:ff:ff:ff 10",
803            "ESS_DISASSOC 02:ff:ff:ff:ff:ff 10 https://example.com/",
804            "BSS_TM_REQ 02:ff:ff:ff:ff:ff",
805            "GET_CONFIG",
806            "RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1",
807            "CHAN_SWITCH 5 5200 ht sec_channel_offset=-1 bandwidth=40",
808            "TRACK_STA_LIST",
809            "PMKSA",
810            "PMKSA_FLUSH",
811            "SET_NEIGHBOR 00:11:22:33:44:55 ssid=\"test1\"",
812            "REMOVE_NEIGHBOR 00:11:22:33:44:55 ssid=\"test1\"",
813            "REQ_LCI 00:11:22:33:44:55",
814            "REQ_RANGE 00:11:22:33:44:55",
815            "DRIVER_FLAGS",
816            "STOP_AP"]
817    for cmd in cmds:
818        hapd.request(cmd)
819
820def test_hapd_ctrl_set(dev, apdev):
821    """hostapd and SET ctrl_iface command"""
822    ssid = "hapd-ctrl"
823    params = {"ssid": ssid}
824    hapd = hostapd.add_ap(apdev[0], params)
825    tests = ["foo",
826             "wps_version_number 300",
827             "gas_frag_limit 0",
828             "mbo_assoc_disallow 0"]
829    for t in tests:
830        if "FAIL" not in hapd.request("SET " + t):
831            raise Exception("Invalid SET command accepted: " + t)
832
833def test_hapd_ctrl_radar(dev, apdev):
834    """hostapd and RADAR ctrl_iface command"""
835    ssid = "hapd-ctrl"
836    params = {"ssid": ssid}
837    hapd = hostapd.add_ap(apdev[0], params)
838
839    tests = ["foo", "foo bar"]
840    for t in tests:
841        if "FAIL" not in hapd.request("RADAR " + t):
842            raise Exception("Invalid RADAR command accepted: " + t)
843
844    tests = ["DETECTED freq=2412 chan_offset=12 cf1=1234 cf2=2345",
845             "CAC-FINISHED freq=2412",
846             "CAC-ABORTED freq=2412",
847             "NOP-FINISHED freq=2412"]
848    for t in tests:
849        hapd.request("RADAR " + t)
850
851def test_hapd_ctrl_ext_io_errors(dev, apdev):
852    """hostapd and external I/O errors"""
853    ssid = "hapd-ctrl"
854    params = {"ssid": ssid}
855    hapd = hostapd.add_ap(apdev[0], params)
856    tests = ["MGMT_TX 1",
857             "MGMT_TX 1q",
858             "MGMT_RX_PROCESS freq=2412",
859             "MGMT_TX_STATUS_PROCESS style=1 ok=0 buf=12345678",
860             "EAPOL_RX foo",
861             "EAPOL_RX 00:11:22:33:44:55 1",
862             "EAPOL_RX 00:11:22:33:44:55 1q"]
863    for t in tests:
864        if "FAIL" not in hapd.request(t):
865            raise Exception("Invalid command accepted: " + t)
866    with alloc_fail(hapd, 1, "=hostapd_ctrl_iface_mgmt_tx"):
867        if "FAIL" not in hapd.request("MGMT_TX 12"):
868            raise Exception("MGMT_TX accepted during OOM")
869    with alloc_fail(hapd, 1, "=hostapd_ctrl_iface_eapol_rx"):
870        if "FAIL" not in hapd.request("EAPOL_RX 00:11:22:33:44:55 11"):
871            raise Exception("EAPOL_RX accepted during OOM")
872
873    hapd.set("ext_mgmt_frame_handling", "1")
874    tests = ["MGMT_RX_PROCESS freq=2412",
875             "MGMT_RX_PROCESS freq=2412 ssi_signal=0",
876             "MGMT_RX_PROCESS freq=2412 frame=1",
877             "MGMT_RX_PROCESS freq=2412 frame=1q",
878             "MGMT_TX_STATUS_PROCESS style=1 ok=0",
879             "MGMT_TX_STATUS_PROCESS style=1 ok=0 buf=1234567",
880             "MGMT_TX_STATUS_PROCESS style=1 ok=0 buf=1234567q"]
881    for t in tests:
882        if "FAIL" not in hapd.request(t):
883            raise Exception("Invalid command accepted: " + t)
884    with alloc_fail(hapd, 1, "=hostapd_ctrl_iface_mgmt_rx_process"):
885        if "FAIL" not in hapd.request("MGMT_RX_PROCESS freq=2412 frame=11"):
886            raise Exception("MGMT_RX_PROCESS accepted during OOM")
887    hapd.set("ext_mgmt_frame_handling", "0")
888
889    if "OK" not in hapd.request("DATA_TEST_CONFIG 1"):
890        raise Exception("Failed to enable l2_test")
891    if "OK" not in hapd.request("DATA_TEST_CONFIG 1"):
892        raise Exception("Failed to enable l2_test(2)")
893    tests = ["DATA_TEST_TX foo",
894             "DATA_TEST_TX 00:11:22:33:44:55 foo",
895             "DATA_TEST_TX 00:11:22:33:44:55 00:11:22:33:44:55 -1",
896             "DATA_TEST_TX 00:11:22:33:44:55 00:11:22:33:44:55 256"]
897    for t in tests:
898        if "FAIL" not in hapd.request(t):
899            raise Exception("Invalid command accepted: " + t)
900    if "OK" not in hapd.request("DATA_TEST_CONFIG 0"):
901        raise Exception("Failed to disable l2_test")
902    tests = ["DATA_TEST_TX 00:11:22:33:44:55 00:11:22:33:44:55 0",
903             "DATA_TEST_FRAME ifname=foo",
904             "DATA_TEST_FRAME 1",
905             "DATA_TEST_FRAME 11",
906             "DATA_TEST_FRAME 112233445566778899aabbccddeefq"]
907    for t in tests:
908        if "FAIL" not in hapd.request(t):
909            raise Exception("Invalid command accepted: " + t)
910    with alloc_fail(hapd, 1, "=hostapd_ctrl_iface_data_test_frame"):
911        if "FAIL" not in hapd.request("DATA_TEST_FRAME 112233445566778899aabbccddeeff"):
912            raise Exception("DATA_TEST_FRAME accepted during OOM")
913
914def test_hapd_ctrl_vendor_test(dev, apdev):
915    """hostapd and VENDOR test command"""
916    ssid = "hapd-ctrl"
917    params = {"ssid": ssid}
918    hapd = hostapd.add_ap(apdev[0], params)
919
920    OUI_QCA = 0x001374
921    QCA_NL80211_VENDOR_SUBCMD_TEST = 1
922    QCA_WLAN_VENDOR_ATTR_TEST = 8
923    attr = struct.pack("@HHI", 4 + 4, QCA_WLAN_VENDOR_ATTR_TEST, 123)
924    cmd = "VENDOR %x %d %s" % (OUI_QCA, QCA_NL80211_VENDOR_SUBCMD_TEST, binascii.hexlify(attr).decode())
925
926    res = hapd.request(cmd)
927    if "FAIL" in res:
928        raise Exception("VENDOR command failed")
929    val, = struct.unpack("@I", binascii.unhexlify(res))
930    if val != 125:
931        raise Exception("Incorrect response value")
932
933    res = hapd.request(cmd + " nested=1")
934    if "FAIL" in res:
935        raise Exception("VENDOR command failed")
936    val, = struct.unpack("@I", binascii.unhexlify(res))
937    if val != 125:
938        raise Exception("Incorrect response value")
939
940    res = hapd.request(cmd + " nested=0")
941    if "FAIL" not in res:
942        raise Exception("VENDOR command with invalid (not nested) data accepted")
943
944def test_hapd_ctrl_vendor_errors(dev, apdev):
945    """hostapd and VENDOR errors"""
946    ssid = "hapd-ctrl"
947    params = {"ssid": ssid}
948    hapd = hostapd.add_ap(apdev[0], params)
949    tests = ["q",
950             "10q",
951             "10 10q",
952             "10 10 123q",
953             "10 10"]
954    for t in tests:
955        if "FAIL" not in hapd.request("VENDOR " + t):
956            raise Exception("Invalid VENDOR command accepted: " + t)
957    with alloc_fail(hapd, 1, "=hostapd_ctrl_iface_vendor"):
958        if "FAIL" not in hapd.request("VENDOR 10 10 10"):
959            raise Exception("VENDOR accepted during OOM")
960    with alloc_fail(hapd, 1, "wpabuf_alloc;hostapd_ctrl_iface_vendor"):
961        if "FAIL" not in hapd.request("VENDOR 10 10"):
962            raise Exception("VENDOR accepted during OOM")
963
964def test_hapd_ctrl_eapol_reauth_errors(dev, apdev):
965    """hostapd and EAPOL_REAUTH errors"""
966    ssid = "hapd-ctrl"
967    params = {"ssid": ssid}
968    hapd = hostapd.add_ap(apdev[0], params)
969    tests = ["foo",
970             "11:22:33:44:55:66"]
971    for t in tests:
972        if "FAIL" not in hapd.request("EAPOL_REAUTH " + t):
973            raise Exception("Invalid EAPOL_REAUTH command accepted: " + t)
974
975def test_hapd_ctrl_eapol_relog(dev, apdev):
976    """hostapd and RELOG"""
977    ssid = "hapd-ctrl"
978    params = {"ssid": ssid}
979    hapd = hostapd.add_ap(apdev[0], params)
980    if "OK" not in hapd.request("RELOG"):
981        raise Exception("RELOG failed")
982
983def test_hapd_ctrl_poll_sta_errors(dev, apdev):
984    """hostapd and POLL_STA errors"""
985    ssid = "hapd-ctrl"
986    params = {"ssid": ssid}
987    hapd = hostapd.add_ap(apdev[0], params)
988    tests = ["foo",
989             "11:22:33:44:55:66"]
990    for t in tests:
991        if "FAIL" not in hapd.request("POLL_STA " + t):
992            raise Exception("Invalid POLL_STA command accepted: " + t)
993
994def test_hapd_ctrl_update_beacon(dev, apdev):
995    """hostapd and UPDATE_BEACON"""
996    ssid = "hapd-ctrl"
997    params = {"ssid": ssid}
998    hapd = hostapd.add_ap(apdev[0], params)
999    if "OK" not in hapd.request("UPDATE_BEACON"):
1000        raise Exception("UPDATE_BEACON failed")
1001    with fail_test(hapd, 1, "ieee802_11_set_beacon"):
1002        if "FAIL" not in hapd.request("UPDATE_BEACON"):
1003            raise Exception("UPDATE_BEACON succeeded unexpectedly")
1004    dev[0].connect(ssid, key_mgmt="NONE", scan_freq="2412")
1005    dev[0].request("DISCONNECT")
1006    if "OK" not in hapd.request("UPDATE_BEACON"):
1007        raise Exception("UPDATE_BEACON failed")
1008    hapd.disable()
1009    if "FAIL" not in hapd.request("UPDATE_BEACON"):
1010        raise Exception("UPDATE_BEACON did not indicate failure when disabled")
1011
1012def test_hapd_ctrl_test_fail(dev, apdev):
1013    """hostapd and TEST_ALLOC_FAIL/TEST_FAIL"""
1014    ssid = "hapd-ctrl"
1015    params = {"ssid": ssid}
1016    hapd = hostapd.add_ap(apdev[0], params)
1017    if "OK" not in hapd.request("TEST_ALLOC_FAIL 1:unknownfunc"):
1018            raise HwsimSkip("TEST_ALLOC_FAIL not supported")
1019    if "OK" not in hapd.request("TEST_ALLOC_FAIL "):
1020        raise Exception("TEST_ALLOC_FAIL clearing failed")
1021    if "OK" not in hapd.request("TEST_FAIL "):
1022        raise Exception("TEST_FAIL clearing failed")
1023
1024def test_hapd_ctrl_setband(dev, apdev):
1025    """hostapd and setband"""
1026    ssid = "hapd-ctrl"
1027    params = {"ssid": ssid}
1028    hapd = hostapd.add_ap(apdev[0], params)
1029    # The actual setband driver operations are not supported without vendor
1030    # commands, so only check minimal parsing items here.
1031    if "FAIL" not in hapd.request("SET setband foo"):
1032        raise Exception("Invalid setband value accepted")
1033    vals = ["5G", "6G", "2G", "2G,6G", "2G,5G,6G", "AUTO"]
1034    for val in vals:
1035        if "OK" not in hapd.request("SET setband " + val):
1036            raise Exception("SET setband %s failed" % val)
1037
1038def test_hapd_ctrl_get_capability(dev, apdev):
1039    """hostapd GET_CAPABILITY"""
1040    ssid = "hapd-ctrl"
1041    params = {"ssid": ssid}
1042    hapd = hostapd.add_ap(apdev[0], params)
1043    if "FAIL" not in hapd.request("GET_CAPABILITY "):
1044        raise Exception("Invalid GET_CAPABILITY accepted")
1045    res = hapd.request("GET_CAPABILITY dpp")
1046    logger.info("DPP capability: " + res)
1047
1048def test_hapd_ctrl_pmksa_add_failures(dev, apdev):
1049    """hostapd PMKSA_ADD failures"""
1050    ssid = "hapd-ctrl"
1051    params = {"ssid": ssid}
1052    hapd = hostapd.add_ap(apdev[0], params)
1053    tests = ["q",
1054             "22:22:22:22:22:22",
1055             "22:22:22:22:22:22 q",
1056             "22:22:22:22:22:22 " + 16*'00',
1057             "22:22:22:22:22:22 " + 16*"00" + " " + 10*"00",
1058             "22:22:22:22:22:22 " + 16*"00" + " q",
1059             "22:22:22:22:22:22 " + 16*"00" + " " + 200*"00",
1060             "22:22:22:22:22:22 " + 16*"00" + " " + 32*"00" + " 12345",
1061             "22:22:22:22:22:22 " + 16*"00" + " " + 32*"00" + " 12345 1",
1062             ""]
1063    for t in tests:
1064        if "FAIL" not in hapd.request("PMKSA_ADD " + t):
1065            raise Exception("Invalid PMKSA_ADD accepted: " + t)
1066
1067def test_hapd_ctrl_attach_errors(dev, apdev):
1068    """hostapd ATTACH errors"""
1069    params = {"ssid": "hapd-ctrl"}
1070    hapd = hostapd.add_ap(apdev[0], params)
1071    hglobal = hostapd.HostapdGlobal(apdev[0])
1072    with alloc_fail(hapd, 1, "ctrl_iface_attach"):
1073        if "FAIL" not in hapd.request("ATTACH foo"):
1074            raise Exception("Invalid ATTACH accepted")
1075    with alloc_fail(hapd, 1, "ctrl_iface_attach"):
1076        if "FAIL" not in hglobal.request("ATTACH foo"):
1077            raise Exception("Invalid ATTACH accepted")
1078