aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hwsim/test_ap_track.py
blob: 64ca8432ebb407b9e05c2fe9761581f064be243b (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Test cases for hostapd tracking unconnected stations
# Copyright (c) 2015, Jouni Malinen <j@w1.fi>
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

import logging
logger = logging.getLogger()
import subprocess
import time

import hostapd
from wpasupplicant import WpaSupplicant

def test_ap_track_sta(dev, apdev):
    """Dualband AP tracking unconnected stations"""
    try:
        _test_ap_track_sta(dev, apdev)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def _test_ap_track_sta(dev, apdev):
    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "track_sta_max_num": "2" }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "a",
               "channel": "40",
               "track_sta_max_num": "100",
               "track_sta_max_age": "1" }
    hapd2 = hostapd.add_ap(apdev[1], params)
    bssid2 = apdev[1]['bssid']

    for i in range(2):
        dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
        dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
        dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
        dev[2].scan_for_bss(bssid2, freq=5200, force_scan=True)

    addr0 = dev[0].own_addr()
    addr1 = dev[1].own_addr()
    addr2 = dev[2].own_addr()

    track = hapd.request("TRACK_STA_LIST")
    if addr0 not in track or addr1 not in track:
        raise Exception("Station missing from 2.4 GHz tracking")
    if addr2 in track:
        raise Exception("Unexpected station included in 2.4 GHz tracking")
    
    track = hapd2.request("TRACK_STA_LIST")
    if addr0 not in track or addr2 not in track:
        raise Exception("Station missing from 5 GHz tracking")
    if addr1 in track:
        raise Exception("Unexpected station included in 5 GHz tracking")

    # Test expiration
    time.sleep(1.1)
    track = hapd.request("TRACK_STA_LIST")
    if addr0 not in track or addr1 not in track:
        raise Exception("Station missing from 2.4 GHz tracking (expiration)")
    track = hapd2.request("TRACK_STA_LIST")
    if addr0 in track or addr2 in track:
        raise Exception("Station not expired from 5 GHz tracking")

    # Test maximum list length
    dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
    dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
    dev[2].scan_for_bss(bssid, freq=2437, force_scan=True)
    track = hapd.request("TRACK_STA_LIST")
    if len(track.splitlines()) != 2:
        raise Exception("Unexpected number of entries: %d" % len(track.splitlines()))
    if addr1 not in track or addr2 not in track:
        raise Exception("Station missing from 2.4 GHz tracking (max limit)")

def test_ap_track_sta_no_probe_resp(dev, apdev):
    """Dualband AP not replying to probes from dualband STA on 2.4 GHz"""
    try:
        _test_ap_track_sta_no_probe_resp(dev, apdev)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def _test_ap_track_sta_no_probe_resp(dev, apdev):
    dev[0].flush_scan_cache()

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "beacon_int": "10000",
               "no_probe_resp_if_seen_on": apdev[1]['ifname'] }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "a",
               "channel": "40",
               "track_sta_max_num": "100" }
    hapd2 = hostapd.add_ap(apdev[1], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
    dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
    dev[0].scan(freq=2437, type="ONLY")
    dev[0].scan(freq=2437, type="ONLY")

    if dev[0].get_bss(bssid):
        raise Exception("2.4 GHz AP found unexpectedly")

def test_ap_track_sta_no_auth(dev, apdev):
    """Dualband AP rejecting authentication from dualband STA on 2.4 GHz"""
    try:
        _test_ap_track_sta_no_auth(dev, apdev)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def _test_ap_track_sta_no_auth(dev, apdev):
    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "track_sta_max_num": "100",
               "no_auth_if_seen_on": apdev[1]['ifname'] }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "a",
               "channel": "40",
               "track_sta_max_num": "100" }
    hapd2 = hostapd.add_ap(apdev[1], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
    dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
    dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)

    dev[1].connect("track", key_mgmt="NONE", scan_freq="2437")

    dev[0].connect("track", key_mgmt="NONE", scan_freq="2437",
                   freq_list="2437", wait_connect=False)
    dev[1].request("DISCONNECT")
    ev = dev[0].wait_event([ "CTRL-EVENT-CONNECTED",
                             "CTRL-EVENT-AUTH-REJECT" ], timeout=10)
    if ev is None:
        raise Exception("Unknown connection result")
    if "CTRL-EVENT-CONNECTED" in ev:
        raise Exception("Unexpected connection")
    if "status_code=82" not in ev:
        raise Exception("Unexpected rejection reason: " + ev)
    if "ie=34" not in ev:
        raise Exception("No Neighbor Report element: " + ev)
    dev[0].request("DISCONNECT")

def test_ap_track_sta_no_auth_passive(dev, apdev):
    """AP rejecting authentication from dualband STA on 2.4 GHz (passive)"""
    try:
        _test_ap_track_sta_no_auth_passive(dev, apdev)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def _test_ap_track_sta_no_auth_passive(dev, apdev):
    dev[0].flush_scan_cache()

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "no_auth_if_seen_on": apdev[1]['ifname'] }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "a",
               "channel": "40",
               "interworking": "1",
               "venue_name": "eng:Venue",
               "track_sta_max_num": "100" }
    hapd2 = hostapd.add_ap(apdev[1], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
    for i in range(10):
        dev[0].request("SCAN freq=5200 passive=1")
        ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=5)
        if ev is None:
            raise Exception("Scan did not complete")
        if dev[0].get_bss(bssid2):
            break
        if i == 9:
            raise Exception("AP not found with passive scans")

    if "OK" not in dev[0].request("ANQP_GET " + bssid2 + " 258"):
        raise Exception("ANQP_GET command failed")
    ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
    if ev is None or "Venue Name" not in ev:
        raise Exception("Did not receive Venue Name")

    dev[0].connect("track", key_mgmt="NONE", scan_freq="2437",
                   freq_list="2437", wait_connect=False)
    ev = dev[0].wait_event([ "CTRL-EVENT-CONNECTED",
                             "CTRL-EVENT-AUTH-REJECT" ], timeout=10)
    if ev is None:
        raise Exception("Unknown connection result")
    if "CTRL-EVENT-CONNECTED" in ev:
        raise Exception("Unexpected connection")
    if "status_code=82" not in ev:
        raise Exception("Unexpected rejection reason: " + ev)
    dev[0].request("DISCONNECT")

def test_ap_track_sta_force_5ghz(dev, apdev):
    """Dualband AP forcing dualband STA to connect on 5 GHz"""
    try:
        _test_ap_track_sta_force_5ghz(dev, apdev)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def _test_ap_track_sta_force_5ghz(dev, apdev):
    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "no_probe_resp_if_seen_on": apdev[1]['ifname'],
               "no_auth_if_seen_on": apdev[1]['ifname'] }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "a",
               "channel": "40",
               "track_sta_max_num": "100" }
    hapd2 = hostapd.add_ap(apdev[1], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
    dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)

    dev[0].connect("track", key_mgmt="NONE", scan_freq="2437 5200")
    freq = dev[0].get_status_field('freq')
    if freq != '5200':
        raise Exception("Unexpected operating channel")
    dev[0].request("DISCONNECT")

def test_ap_track_sta_force_2ghz(dev, apdev):
    """Dualband AP forcing dualband STA to connect on 2.4 GHz"""
    try:
        _test_ap_track_sta_force_2ghz(dev, apdev)
    finally:
        subprocess.call(['iw', 'reg', 'set', '00'])

def _test_ap_track_sta_force_2ghz(dev, apdev):
    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "track_sta_max_num": "100" }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "a",
               "channel": "40",
               "no_probe_resp_if_seen_on": apdev[0]['ifname'],
               "no_auth_if_seen_on": apdev[0]['ifname'] }
    hapd2 = hostapd.add_ap(apdev[1], params)
    bssid2 = apdev[1]['bssid']

    dev[0].scan_for_bss(bssid2, freq=5200, force_scan=True)
    dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)

    dev[0].connect("track", key_mgmt="NONE", scan_freq="2437 5200")
    freq = dev[0].get_status_field('freq')
    if freq != '2437':
        raise Exception("Unexpected operating channel")
    dev[0].request("DISCONNECT")

def test_ap_track_taxonomy(dev, apdev):
    """AP tracking STA taxonomy"""
    try:
        _test_ap_track_taxonomy(dev, apdev)
    finally:
        dev[1].request("SET p2p_disabled 0")

def _test_ap_track_taxonomy(dev, apdev):
    params = { "ssid": "track",
               "country_code": "US",
               "hw_mode": "g",
               "channel": "6",
               "track_sta_max_num": "2" }
    hapd = hostapd.add_ap(apdev[0], params)
    bssid = apdev[0]['bssid']

    dev[0].scan_for_bss(bssid, freq=2437, force_scan=True)
    addr0 = dev[0].own_addr()
    dev[0].connect("track", key_mgmt="NONE", scan_freq="2437")

    dev[1].request("SET p2p_disabled 1")
    dev[1].scan_for_bss(bssid, freq=2437, force_scan=True)
    addr1 = dev[1].own_addr()
    dev[1].connect("track", key_mgmt="NONE", scan_freq="2437")

    wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
    wpas.interface_add("wlan5")
    wpas.request("SET model_name track test")
    wpas.scan_for_bss(bssid, freq=2437, force_scan=True)
    addr = wpas.own_addr()
    wpas.connect("track", key_mgmt="NONE", scan_freq="2437")

    if "FAIL" not in hapd.request("SIGNATURE abc"):
        raise Exception("SIGNATURE failure not reported (1)")
    if "FAIL" not in hapd.request("SIGNATURE 22:33:44:55:66:77"):
        raise Exception("SIGNATURE failure not reported (2)")

    res = hapd.request("SIGNATURE " + addr0)
    logger.info("sta0: " + res)
    if not res.startswith("wifi4|probe:"):
        raise Exception("Unexpected SIGNATURE prefix")
    if "|assoc:" not in res:
        raise Exception("Missing assoc info in SIGNATURE")
    if "wps:track_test" in res:
        raise Exception("Unexpected WPS model name")

    res = hapd.request("SIGNATURE " + addr1)
    logger.info("sta1: " + res)
    if not res.startswith("wifi4|probe:"):
        raise Exception("Unexpected SIGNATURE prefix")
    if "|assoc:" not in res:
        raise Exception("Missing assoc info in SIGNATURE")
    if "wps:" in res:
        raise Exception("Unexpected WPS info");
    if ",221(0050f2,4)," in res:
        raise Exception("Unexpected WPS IE info");
    if ",221(506f9a,9)," in res:
        raise Exception("Unexpected P2P IE info");

    res = hapd.request("SIGNATURE " + addr)
    logger.info("sta: " + res)
    if not res.startswith("wifi4|probe:"):
        raise Exception("Unexpected SIGNATURE prefix")
    if "|assoc:" not in res:
        raise Exception("Missing assoc info in SIGNATURE")
    if "wps:track_test" not in res:
        raise Exception("Missing WPS model name")
    if ",221(0050f2,4)," not in res:
        raise Exception("Missing WPS IE info");
    if ",221(506f9a,9)," not in res:
        raise Exception("Missing P2P IE info");

    addr2 = dev[2].own_addr()
    res = hapd.request("SIGNATURE " + addr2)
    if "FAIL" not in res:
        raise Exception("Unexpected SIGNATURE success for sta2 (1)")

    for i in range(10):
        dev[2].request("SCAN freq=2437 passive=1")
        ev = dev[2].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=10)
        if ev is None:
            raise Exception("Scan did not complete")
        if dev[2].get_bss(bssid):
            break

    res = hapd.request("SIGNATURE " + addr2)
    if "FAIL" not in res:
        raise Exception("Unexpected SIGNATURE success for sta2 (2)")

    dev[2].connect("track", key_mgmt="NONE", scan_freq="2437")

    res = hapd.request("SIGNATURE " + addr2)
    if "FAIL" not in res and len(res) > 0:
        raise Exception("Unexpected SIGNATURE success for sta2 (3)")

    dev[2].scan_for_bss(bssid, freq=2437, force_scan=True)

    res = hapd.request("SIGNATURE " + addr2)
    logger.info("sta2: " + res)
    if not res.startswith("wifi4|probe:"):
        raise Exception("Unexpected SIGNATURE prefix")
    if "|assoc:" not in res:
        raise Exception("Missing assoc info in SIGNATURE")