]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_gas.py
tests: Mark 525 tests as remote compatible
[thirdparty/hostap.git] / tests / hwsim / test_gas.py
1 # GAS tests
2 # Copyright (c) 2013, Qualcomm Atheros, Inc.
3 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
4 #
5 # This software may be distributed under the terms of the BSD license.
6 # See README for more details.
7
8 from remotehost import remote_compatible
9 import time
10 import binascii
11 import logging
12 logger = logging.getLogger()
13 import os
14 import re
15 import struct
16
17 import hostapd
18 from wpasupplicant import WpaSupplicant
19 from tshark import run_tshark
20 from utils import alloc_fail, wait_fail_trigger, skip_with_fips
21 from hwsim import HWSimRadio
22
23 def hs20_ap_params():
24 params = hostapd.wpa2_params(ssid="test-gas")
25 params['wpa_key_mgmt'] = "WPA-EAP"
26 params['ieee80211w'] = "1"
27 params['ieee8021x'] = "1"
28 params['auth_server_addr'] = "127.0.0.1"
29 params['auth_server_port'] = "1812"
30 params['auth_server_shared_secret'] = "radius"
31 params['interworking'] = "1"
32 params['access_network_type'] = "14"
33 params['internet'] = "1"
34 params['asra'] = "0"
35 params['esr'] = "0"
36 params['uesa'] = "0"
37 params['venue_group'] = "7"
38 params['venue_type'] = "1"
39 params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
40 params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
41 "fedcba" ]
42 params['domain_name'] = "example.com,another.example.com"
43 params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
44 "0,another.example.com" ]
45 params['anqp_3gpp_cell_net'] = "244,91"
46 params['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
47 params['ipaddr_type_availability'] = "14"
48 params['hs20'] = "1"
49 params['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
50 params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
51 params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
52 params['hs20_operating_class'] = "5173"
53 return params
54
55 def start_ap(ap):
56 params = hs20_ap_params()
57 params['hessid'] = ap['bssid']
58 return hostapd.add_ap(ap, params)
59
60 def get_gas_response(dev, bssid, info, allow_fetch_failure=False,
61 extra_test=False):
62 exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
63 res = re.split(exp, info)
64 if len(res) < 6:
65 raise Exception("Could not parse GAS-RESPONSE-INFO")
66 if res[2] != bssid:
67 raise Exception("Unexpected BSSID in response")
68 token = res[3]
69 status = res[4]
70 if status != "0":
71 raise Exception("GAS query failed")
72 resp_len = res[5]
73 if resp_len == "-1":
74 raise Exception("GAS query reported invalid response length")
75 if int(resp_len) > 2000:
76 raise Exception("Unexpected long GAS response")
77
78 if extra_test:
79 if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " 123456"):
80 raise Exception("Invalid dialog token accepted")
81 if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 10000,10001"):
82 raise Exception("Invalid range accepted")
83 if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0,10000"):
84 raise Exception("Invalid range accepted")
85 if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0"):
86 raise Exception("Invalid GAS_RESPONSE_GET accepted")
87
88 res1_2 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 1,2")
89 res5_3 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 5,3")
90
91 resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
92 if "FAIL" in resp:
93 if allow_fetch_failure:
94 logger.debug("GAS response was not available anymore")
95 return
96 raise Exception("Could not fetch GAS response")
97 if len(resp) != int(resp_len) * 2:
98 raise Exception("Unexpected GAS response length")
99 logger.debug("GAS response: " + resp)
100 if extra_test:
101 if resp[2:6] != res1_2:
102 raise Exception("Unexpected response substring res1_2: " + res1_2)
103 if resp[10:16] != res5_3:
104 raise Exception("Unexpected response substring res5_3: " + res5_3)
105
106 def test_gas_generic(dev, apdev):
107 """Generic GAS query"""
108 bssid = apdev[0]['bssid']
109 params = hs20_ap_params()
110 params['hessid'] = bssid
111 hostapd.add_ap(apdev[0], params)
112
113 cmds = [ "foo",
114 "00:11:22:33:44:55",
115 "00:11:22:33:44:55 ",
116 "00:11:22:33:44:55 ",
117 "00:11:22:33:44:55 1",
118 "00:11:22:33:44:55 1 1234",
119 "00:11:22:33:44:55 qq",
120 "00:11:22:33:44:55 qq 1234",
121 "00:11:22:33:44:55 00 1",
122 "00:11:22:33:44:55 00 123",
123 "00:11:22:33:44:55 00 ",
124 "00:11:22:33:44:55 00 qq" ]
125 for cmd in cmds:
126 if "FAIL" not in dev[0].request("GAS_REQUEST " + cmd):
127 raise Exception("Invalid GAS_REQUEST accepted: " + cmd)
128
129 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
130 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
131 if "FAIL" in req:
132 raise Exception("GAS query request rejected")
133 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
134 if ev is None:
135 raise Exception("GAS query timed out")
136 get_gas_response(dev[0], bssid, ev, extra_test=True)
137
138 if "FAIL" not in dev[0].request("GAS_RESPONSE_GET ff"):
139 raise Exception("Invalid GAS_RESPONSE_GET accepted")
140
141 def test_gas_concurrent_scan(dev, apdev):
142 """Generic GAS queries with concurrent scan operation"""
143 bssid = apdev[0]['bssid']
144 params = hs20_ap_params()
145 params['hessid'] = bssid
146 hostapd.add_ap(apdev[0], params)
147
148 # get BSS entry available to allow GAS query
149 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
150
151 logger.info("Request concurrent operations")
152 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
153 if "FAIL" in req:
154 raise Exception("GAS query request rejected")
155 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
156 if "FAIL" in req:
157 raise Exception("GAS query request rejected")
158 dev[0].scan(no_wait=True)
159 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
160 if "FAIL" in req:
161 raise Exception("GAS query request rejected")
162 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
163 if "FAIL" in req:
164 raise Exception("GAS query request rejected")
165
166 responses = 0
167 for i in range(0, 5):
168 ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
169 timeout=10)
170 if ev is None:
171 raise Exception("Operation timed out")
172 if "GAS-RESPONSE-INFO" in ev:
173 responses = responses + 1
174 get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
175
176 if responses != 4:
177 raise Exception("Unexpected number of GAS responses")
178
179 def test_gas_concurrent_connect(dev, apdev):
180 """Generic GAS queries with concurrent connection operation"""
181 skip_with_fips(dev[0])
182 bssid = apdev[0]['bssid']
183 params = hs20_ap_params()
184 params['hessid'] = bssid
185 hostapd.add_ap(apdev[0], params)
186
187 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
188
189 logger.debug("Start concurrent connect and GAS request")
190 dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
191 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
192 password="password", phase2="auth=MSCHAPV2",
193 ca_cert="auth_serv/ca.pem", wait_connect=False,
194 scan_freq="2412")
195 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
196 if "FAIL" in req:
197 raise Exception("GAS query request rejected")
198
199 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
200 timeout=20)
201 if ev is None:
202 raise Exception("Operation timed out")
203 if "CTRL-EVENT-CONNECTED" not in ev:
204 raise Exception("Unexpected operation order")
205
206 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
207 timeout=20)
208 if ev is None:
209 raise Exception("Operation timed out")
210 if "GAS-RESPONSE-INFO" not in ev:
211 raise Exception("Unexpected operation order")
212 get_gas_response(dev[0], bssid, ev)
213
214 dev[0].request("DISCONNECT")
215 dev[0].wait_disconnected(timeout=5)
216
217 logger.debug("Wait six seconds for expiration of connect-without-scan")
218 time.sleep(6)
219 dev[0].dump_monitor()
220
221 logger.debug("Start concurrent GAS request and connect")
222 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
223 if "FAIL" in req:
224 raise Exception("GAS query request rejected")
225 dev[0].request("RECONNECT")
226
227 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
228 if ev is None:
229 raise Exception("Operation timed out")
230 get_gas_response(dev[0], bssid, ev)
231
232 ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
233 if ev is None:
234 raise Exception("No new scan results reported")
235
236 ev = dev[0].wait_connected(timeout=20, error="Operation tiemd out")
237 if "CTRL-EVENT-CONNECTED" not in ev:
238 raise Exception("Unexpected operation order")
239
240 def gas_fragment_and_comeback(dev, apdev, frag_limit=0, comeback_delay=0):
241 hapd = start_ap(apdev)
242 if frag_limit:
243 hapd.set("gas_frag_limit", str(frag_limit))
244 if comeback_delay:
245 hapd.set("gas_comeback_delay", str(comeback_delay))
246
247 dev.scan_for_bss(apdev['bssid'], freq="2412", force_scan=True)
248 dev.request("FETCH_ANQP")
249 ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=5)
250 if ev is None:
251 raise Exception("No GAS-QUERY-DONE event")
252 if "result=SUCCESS" not in ev:
253 raise Exception("Unexpected GAS result: " + ev)
254 for i in range(0, 13):
255 ev = dev.wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
256 if ev is None:
257 raise Exception("Operation timed out")
258 ev = dev.wait_event(["ANQP-QUERY-DONE"], timeout=1)
259 if ev is None:
260 raise Exception("No ANQP-QUERY-DONE event")
261 if "result=SUCCESS" not in ev:
262 raise Exception("Unexpected ANQP result: " + ev)
263
264 def test_gas_fragment(dev, apdev):
265 """GAS fragmentation"""
266 gas_fragment_and_comeback(dev[0], apdev[0], frag_limit=50)
267
268 def test_gas_fragment_mcc(dev, apdev):
269 """GAS fragmentation with mac80211_hwsim MCC enabled"""
270 with HWSimRadio(n_channels=2) as (radio, iface):
271 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
272 wpas.interface_add(iface)
273 gas_fragment_and_comeback(wpas, apdev[0], frag_limit=50)
274
275 def test_gas_fragment_with_comeback_delay(dev, apdev):
276 """GAS fragmentation and comeback delay"""
277 gas_fragment_and_comeback(dev[0], apdev[0], frag_limit=50,
278 comeback_delay=500)
279
280 def test_gas_fragment_with_comeback_delay_mcc(dev, apdev):
281 """GAS fragmentation and comeback delay with mac80211_hwsim MCC enabled"""
282 with HWSimRadio(n_channels=2) as (radio, iface):
283 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
284 wpas.interface_add(iface)
285 gas_fragment_and_comeback(wpas, apdev[0], frag_limit=50,
286 comeback_delay=500)
287
288 def test_gas_comeback_delay(dev, apdev):
289 """GAS comeback delay"""
290 hapd = start_ap(apdev[0])
291 hapd.set("gas_comeback_delay", "500")
292
293 dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
294 dev[0].request("FETCH_ANQP")
295 if "FAIL-BUSY" not in dev[0].request("SCAN"):
296 raise Exception("SCAN accepted during FETCH_ANQP")
297 for i in range(0, 6):
298 ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
299 if ev is None:
300 raise Exception("Operation timed out")
301
302 @remote_compatible
303 def test_gas_stop_fetch_anqp(dev, apdev):
304 """Stop FETCH_ANQP operation"""
305 hapd = start_ap(apdev[0])
306
307 dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
308 hapd.set("ext_mgmt_frame_handling", "1")
309 dev[0].request("FETCH_ANQP")
310 dev[0].request("STOP_FETCH_ANQP")
311 hapd.set("ext_mgmt_frame_handling", "0")
312 ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
313 if ev is None:
314 raise Exception("GAS-QUERY-DONE timed out")
315 if "RX-ANQP" in ev:
316 raise Exception("Unexpected ANQP response received")
317
318 def test_gas_anqp_get(dev, apdev):
319 """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
320 hapd = start_ap(apdev[0])
321 bssid = apdev[0]['bssid']
322
323 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
324 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
325 raise Exception("ANQP_GET command failed")
326
327 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
328 if ev is None:
329 raise Exception("GAS query start timed out")
330
331 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
332 if ev is None:
333 raise Exception("GAS query timed out")
334
335 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
336 if ev is None or "Venue Name" not in ev:
337 raise Exception("Did not receive Venue Name")
338
339 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
340 if ev is None or "Domain Name list" not in ev:
341 raise Exception("Did not receive Domain Name list")
342
343 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
344 if ev is None or "Operator Friendly Name" not in ev:
345 raise Exception("Did not receive Operator Friendly Name")
346
347 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
348 if ev is None or "WAN Metrics" not in ev:
349 raise Exception("Did not receive WAN Metrics")
350
351 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
352 if ev is None:
353 raise Exception("ANQP-QUERY-DONE event not seen")
354 if "result=SUCCESS" not in ev:
355 raise Exception("Unexpected result: " + ev)
356
357 if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
358 raise Exception("ANQP_GET command failed")
359
360 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
361 if ev is None or "Operator Friendly Name" not in ev:
362 raise Exception("Did not receive Operator Friendly Name")
363
364 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
365 if ev is None or "WAN Metrics" not in ev:
366 raise Exception("Did not receive WAN Metrics")
367
368 cmds = [ "",
369 "foo",
370 "00:11:22:33:44:55 258,hs20:-1",
371 "00:11:22:33:44:55 258,hs20:0",
372 "00:11:22:33:44:55 258,hs20:32",
373 "00:11:22:33:44:55 hs20:-1",
374 "00:11:22:33:44:55 hs20:0",
375 "00:11:22:33:44:55 hs20:32",
376 "00:11:22:33:44:55",
377 "00:11:22:33:44:55 ",
378 "00:11:22:33:44:55 0" ]
379 for cmd in cmds:
380 if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
381 raise Exception("Invalid ANQP_GET accepted")
382
383 cmds = [ "",
384 "foo",
385 "00:11:22:33:44:55 -1",
386 "00:11:22:33:44:55 0",
387 "00:11:22:33:44:55 32",
388 "00:11:22:33:44:55",
389 "00:11:22:33:44:55 ",
390 "00:11:22:33:44:55 0" ]
391 for cmd in cmds:
392 if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
393 raise Exception("Invalid HS20_ANQP_GET accepted")
394
395 def expect_gas_result(dev, result, status=None):
396 ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
397 if ev is None:
398 raise Exception("GAS query timed out")
399 if "result=" + result not in ev:
400 raise Exception("Unexpected GAS query result")
401 if status and "status_code=" + str(status) + ' ' not in ev:
402 raise Exception("Unexpected GAS status code")
403
404 def anqp_get(dev, bssid, id):
405 if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
406 raise Exception("ANQP_GET command failed")
407 ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
408 if ev is None:
409 raise Exception("GAS query start timed out")
410
411 def test_gas_timeout(dev, apdev):
412 """GAS timeout"""
413 hapd = start_ap(apdev[0])
414 bssid = apdev[0]['bssid']
415
416 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
417 hapd.set("ext_mgmt_frame_handling", "1")
418
419 anqp_get(dev[0], bssid, 263)
420
421 ev = hapd.wait_event(["MGMT-RX"], timeout=5)
422 if ev is None:
423 raise Exception("MGMT RX wait timed out")
424
425 expect_gas_result(dev[0], "TIMEOUT")
426
427 MGMT_SUBTYPE_ACTION = 13
428 ACTION_CATEG_PUBLIC = 4
429
430 GAS_INITIAL_REQUEST = 10
431 GAS_INITIAL_RESPONSE = 11
432 GAS_COMEBACK_REQUEST = 12
433 GAS_COMEBACK_RESPONSE = 13
434 GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
435 GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
436
437 def anqp_adv_proto():
438 return struct.pack('BBBB', 108, 2, 127, 0)
439
440 def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
441 return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
442 dialog_token, status_code, comeback_delay) + anqp_adv_proto()
443
444 def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
445 if more:
446 id |= 0x80
447 if bogus_adv_proto:
448 adv = struct.pack('BBBB', 108, 2, 127, 1)
449 else:
450 adv = anqp_adv_proto()
451 return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
452 dialog_token, status_code, id, comeback_delay) + adv
453
454 def gas_rx(hapd):
455 count = 0
456 while count < 30:
457 count = count + 1
458 query = hapd.mgmt_rx()
459 if query is None:
460 raise Exception("Action frame not received")
461 if query['subtype'] != MGMT_SUBTYPE_ACTION:
462 continue
463 payload = query['payload']
464 if len(payload) < 2:
465 continue
466 (category, action) = struct.unpack('BB', payload[0:2])
467 if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
468 continue
469 return query
470 raise Exception("No Action frame received")
471
472 def parse_gas(payload):
473 pos = payload
474 (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
475 if category != ACTION_CATEG_PUBLIC:
476 return None
477 if action not in GAS_ACTIONS:
478 return None
479 gas = {}
480 gas['action'] = action
481 pos = pos[3:]
482
483 if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
484 return None
485
486 gas['dialog_token'] = dialog_token
487
488 if action == GAS_INITIAL_RESPONSE:
489 if len(pos) < 4:
490 return None
491 (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
492 gas['status_code'] = status_code
493 gas['comeback_delay'] = comeback_delay
494
495 if action == GAS_COMEBACK_RESPONSE:
496 if len(pos) < 5:
497 return None
498 (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
499 gas['status_code'] = status_code
500 gas['frag'] = frag
501 gas['comeback_delay'] = comeback_delay
502
503 return gas
504
505 def action_response(req):
506 resp = {}
507 resp['fc'] = req['fc']
508 resp['da'] = req['sa']
509 resp['sa'] = req['da']
510 resp['bssid'] = req['bssid']
511 return resp
512
513 def send_gas_resp(hapd, resp):
514 hapd.mgmt_tx(resp)
515 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
516 if ev is None:
517 raise Exception("Missing TX status for GAS response")
518 if "ok=1" not in ev:
519 raise Exception("GAS response not acknowledged")
520
521 def test_gas_invalid_response_type(dev, apdev):
522 """GAS invalid response type"""
523 hapd = start_ap(apdev[0])
524 bssid = apdev[0]['bssid']
525
526 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
527 hapd.set("ext_mgmt_frame_handling", "1")
528
529 anqp_get(dev[0], bssid, 263)
530
531 query = gas_rx(hapd)
532 gas = parse_gas(query['payload'])
533
534 resp = action_response(query)
535 # GAS Comeback Response instead of GAS Initial Response
536 resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
537 send_gas_resp(hapd, resp)
538
539 # station drops the invalid frame, so this needs to result in GAS timeout
540 expect_gas_result(dev[0], "TIMEOUT")
541
542 def test_gas_failure_status_code(dev, apdev):
543 """GAS failure status code"""
544 hapd = start_ap(apdev[0])
545 bssid = apdev[0]['bssid']
546
547 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
548 hapd.set("ext_mgmt_frame_handling", "1")
549
550 anqp_get(dev[0], bssid, 263)
551
552 query = gas_rx(hapd)
553 gas = parse_gas(query['payload'])
554
555 resp = action_response(query)
556 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
557 send_gas_resp(hapd, resp)
558
559 expect_gas_result(dev[0], "FAILURE")
560
561 def test_gas_malformed(dev, apdev):
562 """GAS malformed response frames"""
563 hapd = start_ap(apdev[0])
564 bssid = apdev[0]['bssid']
565
566 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
567 hapd.set("ext_mgmt_frame_handling", "1")
568
569 anqp_get(dev[0], bssid, 263)
570
571 query = gas_rx(hapd)
572 gas = parse_gas(query['payload'])
573
574 resp = action_response(query)
575
576 resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
577 GAS_COMEBACK_RESPONSE,
578 gas['dialog_token'], 0)
579 hapd.mgmt_tx(resp)
580
581 resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
582 GAS_COMEBACK_RESPONSE,
583 gas['dialog_token'], 0, 0)
584 hapd.mgmt_tx(resp)
585
586 hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
587 gas['dialog_token'], 0, 0)
588 resp['payload'] = hdr + struct.pack('B', 108)
589 hapd.mgmt_tx(resp)
590 resp['payload'] = hdr + struct.pack('BB', 108, 0)
591 hapd.mgmt_tx(resp)
592 resp['payload'] = hdr + struct.pack('BB', 108, 1)
593 hapd.mgmt_tx(resp)
594 resp['payload'] = hdr + struct.pack('BB', 108, 255)
595 hapd.mgmt_tx(resp)
596 resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
597 hapd.mgmt_tx(resp)
598 resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
599 hapd.mgmt_tx(resp)
600 resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
601 hapd.mgmt_tx(resp)
602
603 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
604 hapd.mgmt_tx(resp)
605
606 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
607 hapd.mgmt_tx(resp)
608
609 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
610 hapd.mgmt_tx(resp)
611
612 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
613 hapd.mgmt_tx(resp)
614
615 # Station drops invalid frames, but the last of the responses is valid from
616 # GAS view point even though it has an extra octet in the end and the ANQP
617 # part of the response is not valid. This is reported as successfully
618 # completed GAS exchange.
619 expect_gas_result(dev[0], "SUCCESS")
620
621 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
622 if ev is None:
623 raise Exception("ANQP-QUERY-DONE not reported")
624 if "result=INVALID_FRAME" not in ev:
625 raise Exception("Unexpected result: " + ev)
626
627 def init_gas(hapd, bssid, dev):
628 anqp_get(dev, bssid, 263)
629 query = gas_rx(hapd)
630 gas = parse_gas(query['payload'])
631 dialog_token = gas['dialog_token']
632
633 resp = action_response(query)
634 resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
635 send_gas_resp(hapd, resp)
636
637 query = gas_rx(hapd)
638 gas = parse_gas(query['payload'])
639 if gas['action'] != GAS_COMEBACK_REQUEST:
640 raise Exception("Unexpected request action")
641 if gas['dialog_token'] != dialog_token:
642 raise Exception("Unexpected dialog token change")
643 return query, dialog_token
644
645 def allow_gas_initial_req(hapd, dialog_token):
646 msg = hapd.mgmt_rx(timeout=1)
647 if msg is not None:
648 gas = parse_gas(msg['payload'])
649 if gas['action'] != GAS_INITIAL_REQUEST or dialog_token == gas['dialog_token']:
650 raise Exception("Unexpected management frame")
651
652 def test_gas_malformed_comeback_resp(dev, apdev):
653 """GAS malformed comeback response frames"""
654 hapd = start_ap(apdev[0])
655 bssid = apdev[0]['bssid']
656
657 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
658 hapd.set("ext_mgmt_frame_handling", "1")
659
660 logger.debug("Non-zero status code in comeback response")
661 query, dialog_token = init_gas(hapd, bssid, dev[0])
662 resp = action_response(query)
663 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
664 send_gas_resp(hapd, resp)
665 expect_gas_result(dev[0], "FAILURE", status=2)
666
667 logger.debug("Different advertisement protocol in comeback response")
668 query, dialog_token = init_gas(hapd, bssid, dev[0])
669 resp = action_response(query)
670 resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
671 send_gas_resp(hapd, resp)
672 expect_gas_result(dev[0], "PEER_ERROR")
673
674 logger.debug("Non-zero frag id and comeback delay in comeback response")
675 query, dialog_token = init_gas(hapd, bssid, dev[0])
676 resp = action_response(query)
677 resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
678 send_gas_resp(hapd, resp)
679 expect_gas_result(dev[0], "PEER_ERROR")
680
681 logger.debug("Unexpected frag id in comeback response")
682 query, dialog_token = init_gas(hapd, bssid, dev[0])
683 resp = action_response(query)
684 resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
685 send_gas_resp(hapd, resp)
686 expect_gas_result(dev[0], "PEER_ERROR")
687
688 logger.debug("Empty fragment and replay in comeback response")
689 query, dialog_token = init_gas(hapd, bssid, dev[0])
690 resp = action_response(query)
691 resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
692 send_gas_resp(hapd, resp)
693 query = gas_rx(hapd)
694 gas = parse_gas(query['payload'])
695 if gas['action'] != GAS_COMEBACK_REQUEST:
696 raise Exception("Unexpected request action")
697 if gas['dialog_token'] != dialog_token:
698 raise Exception("Unexpected dialog token change")
699 resp = action_response(query)
700 resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
701 send_gas_resp(hapd, resp)
702 resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
703 send_gas_resp(hapd, resp)
704 expect_gas_result(dev[0], "SUCCESS")
705
706 logger.debug("Unexpected initial response when waiting for comeback response")
707 query, dialog_token = init_gas(hapd, bssid, dev[0])
708 resp = action_response(query)
709 resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
710 send_gas_resp(hapd, resp)
711 allow_gas_initial_req(hapd, dialog_token)
712 expect_gas_result(dev[0], "TIMEOUT")
713
714 logger.debug("Too short comeback response")
715 query, dialog_token = init_gas(hapd, bssid, dev[0])
716 resp = action_response(query)
717 resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
718 GAS_COMEBACK_RESPONSE, dialog_token, 0)
719 send_gas_resp(hapd, resp)
720 allow_gas_initial_req(hapd, dialog_token)
721 expect_gas_result(dev[0], "TIMEOUT")
722
723 logger.debug("Too short comeback response(2)")
724 query, dialog_token = init_gas(hapd, bssid, dev[0])
725 resp = action_response(query)
726 resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
727 GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
728 0)
729 send_gas_resp(hapd, resp)
730 allow_gas_initial_req(hapd, dialog_token)
731 expect_gas_result(dev[0], "TIMEOUT")
732
733 logger.debug("Maximum comeback response fragment claiming more fragments")
734 query, dialog_token = init_gas(hapd, bssid, dev[0])
735 resp = action_response(query)
736 resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
737 send_gas_resp(hapd, resp)
738 for i in range(1, 129):
739 query = gas_rx(hapd)
740 gas = parse_gas(query['payload'])
741 if gas['action'] != GAS_COMEBACK_REQUEST:
742 raise Exception("Unexpected request action")
743 if gas['dialog_token'] != dialog_token:
744 raise Exception("Unexpected dialog token change")
745 resp = action_response(query)
746 resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
747 send_gas_resp(hapd, resp)
748 expect_gas_result(dev[0], "PEER_ERROR")
749
750 def test_gas_comeback_resp_additional_delay(dev, apdev):
751 """GAS comeback response requesting additional delay"""
752 hapd = start_ap(apdev[0])
753 bssid = apdev[0]['bssid']
754
755 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
756 hapd.set("ext_mgmt_frame_handling", "1")
757
758 query, dialog_token = init_gas(hapd, bssid, dev[0])
759 for i in range(0, 2):
760 resp = action_response(query)
761 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
762 send_gas_resp(hapd, resp)
763 query = gas_rx(hapd)
764 gas = parse_gas(query['payload'])
765 if gas['action'] != GAS_COMEBACK_REQUEST:
766 raise Exception("Unexpected request action")
767 if gas['dialog_token'] != dialog_token:
768 raise Exception("Unexpected dialog token change")
769 resp = action_response(query)
770 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
771 send_gas_resp(hapd, resp)
772 expect_gas_result(dev[0], "SUCCESS")
773
774 def test_gas_unknown_adv_proto(dev, apdev):
775 """Unknown advertisement protocol id"""
776 bssid = apdev[0]['bssid']
777 params = hs20_ap_params()
778 params['hessid'] = bssid
779 hostapd.add_ap(apdev[0], params)
780
781 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
782 req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
783 if "FAIL" in req:
784 raise Exception("GAS query request rejected")
785 expect_gas_result(dev[0], "FAILURE", "59")
786 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
787 if ev is None:
788 raise Exception("GAS query timed out")
789 exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
790 res = re.split(exp, ev)
791 if len(res) < 6:
792 raise Exception("Could not parse GAS-RESPONSE-INFO")
793 if res[2] != bssid:
794 raise Exception("Unexpected BSSID in response")
795 status = res[4]
796 if status != "59":
797 raise Exception("Unexpected GAS-RESPONSE-INFO status")
798
799 def test_gas_max_pending(dev, apdev):
800 """GAS and maximum pending query limit"""
801 hapd = start_ap(apdev[0])
802 hapd.set("gas_frag_limit", "50")
803 bssid = apdev[0]['bssid']
804
805 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
806 wpas.interface_add("wlan5")
807 if "OK" not in wpas.request("P2P_SET listen_channel 1"):
808 raise Exception("Failed to set listen channel")
809 if "OK" not in wpas.p2p_listen():
810 raise Exception("Failed to start listen state")
811 if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
812 raise Exception("Failed to enable external management frame handling")
813
814 anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
815 gas = struct.pack('<H', len(anqp_query)) + anqp_query
816
817 for dialog_token in range(1, 10):
818 msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
819 dialog_token) + anqp_adv_proto() + gas
820 req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
821 if "OK" not in wpas.request(req):
822 raise Exception("Could not send management frame")
823 resp = wpas.mgmt_rx()
824 if resp is None:
825 raise Exception("MGMT-RX timeout")
826 if 'payload' not in resp:
827 raise Exception("Missing payload")
828 gresp = parse_gas(resp['payload'])
829 if gresp['dialog_token'] != dialog_token:
830 raise Exception("Dialog token mismatch")
831 status_code = gresp['status_code']
832 if dialog_token < 9 and status_code != 0:
833 raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
834 if dialog_token > 8 and status_code == 0:
835 raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
836
837 def test_gas_no_pending(dev, apdev):
838 """GAS and no pending query for comeback request"""
839 hapd = start_ap(apdev[0])
840 bssid = apdev[0]['bssid']
841
842 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
843 wpas.interface_add("wlan5")
844 if "OK" not in wpas.request("P2P_SET listen_channel 1"):
845 raise Exception("Failed to set listen channel")
846 if "OK" not in wpas.p2p_listen():
847 raise Exception("Failed to start listen state")
848 if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
849 raise Exception("Failed to enable external management frame handling")
850
851 msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
852 req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
853 if "OK" not in wpas.request(req):
854 raise Exception("Could not send management frame")
855 resp = wpas.mgmt_rx()
856 if resp is None:
857 raise Exception("MGMT-RX timeout")
858 if 'payload' not in resp:
859 raise Exception("Missing payload")
860 gresp = parse_gas(resp['payload'])
861 status_code = gresp['status_code']
862 if status_code != 60:
863 raise Exception("Unexpected status code {} (expected 60)".format(status_code))
864
865 def test_gas_delete_at_deinit(dev, apdev):
866 """GAS query deleted at deinit"""
867 hapd = start_ap(apdev[0])
868 hapd.set("gas_comeback_delay", "1000")
869 bssid = apdev[0]['bssid']
870
871 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
872 wpas.interface_add("wlan5")
873 wpas.scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
874 wpas.request("ANQP_GET " + bssid + " 258")
875
876 wpas.global_request("INTERFACE_REMOVE " + wpas.ifname)
877 ev = wpas.wait_event(["GAS-QUERY-DONE"], timeout=2)
878 del wpas
879 if ev is None:
880 raise Exception("GAS-QUERY-DONE not seen")
881 if "result=DELETED_AT_DEINIT" not in ev:
882 raise Exception("Unexpected result code: " + ev)
883
884 def test_gas_missing_payload(dev, apdev):
885 """No action code in the query frame"""
886 bssid = apdev[0]['bssid']
887 params = hs20_ap_params()
888 params['hessid'] = bssid
889 hostapd.add_ap(apdev[0], params)
890
891 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
892
893 cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
894 if "FAIL" in dev[0].request(cmd):
895 raise Exception("Could not send test Action frame")
896 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
897 if ev is None:
898 raise Exception("Timeout on MGMT-TX-STATUS")
899 if "result=SUCCESS" not in ev:
900 raise Exception("AP did not ack Action frame")
901
902 cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
903 if "FAIL" in dev[0].request(cmd):
904 raise Exception("Could not send test Action frame")
905 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
906 if ev is None:
907 raise Exception("Timeout on MGMT-TX-STATUS")
908 if "result=SUCCESS" not in ev:
909 raise Exception("AP did not ack Action frame")
910
911 def test_gas_query_deinit(dev, apdev):
912 """Pending GAS/ANQP query during deinit"""
913 hapd = start_ap(apdev[0])
914 bssid = apdev[0]['bssid']
915
916 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
917 wpas.interface_add("wlan5")
918
919 wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
920 id = wpas.request("RADIO_WORK add block-work")
921 if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
922 raise Exception("ANQP_GET command failed")
923
924 ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
925 if ev is None:
926 raise Exception("Timeout while waiting radio work to start")
927 ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
928 if ev is None:
929 raise Exception("Timeout while waiting radio work to start (2)")
930
931 # Remove the interface while the gas-query radio work is still pending and
932 # GAS query has not yet been started.
933 wpas.interface_remove("wlan5")
934
935 @remote_compatible
936 def test_gas_anqp_oom_wpas(dev, apdev):
937 """GAS/ANQP query and OOM in wpa_supplicant"""
938 hapd = start_ap(apdev[0])
939 bssid = apdev[0]['bssid']
940
941 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
942
943 with alloc_fail(dev[0], 1, "wpa_bss_anqp_alloc"):
944 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
945 raise Exception("ANQP_GET command failed")
946 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
947 if ev is None:
948 raise Exception("ANQP query did not complete")
949
950 with alloc_fail(dev[0], 1, "gas_build_req"):
951 if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
952 raise Exception("Unexpected ANQP_GET command success (OOM)")
953
954 def test_gas_anqp_oom_hapd(dev, apdev):
955 """GAS/ANQP query and OOM in hostapd"""
956 hapd = start_ap(apdev[0])
957 bssid = apdev[0]['bssid']
958
959 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
960
961 with alloc_fail(hapd, 1, "gas_build_resp"):
962 # This query will time out due to the AP not sending a response (OOM).
963 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
964 raise Exception("ANQP_GET command failed")
965
966 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
967 if ev is None:
968 raise Exception("GAS query start timed out")
969
970 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
971 if ev is None:
972 raise Exception("GAS query timed out")
973 if "result=TIMEOUT" not in ev:
974 raise Exception("Unexpected result: " + ev)
975
976 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
977 if ev is None:
978 raise Exception("ANQP-QUERY-DONE event not seen")
979 if "result=FAILURE" not in ev:
980 raise Exception("Unexpected result: " + ev)
981
982 with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
983 hapd.set("gas_frag_limit", "50")
984
985 # The first attempt of this query will time out due to the AP not
986 # sending a response (OOM), but the retry succeeds.
987 dev[0].request("FETCH_ANQP")
988 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
989 if ev is None:
990 raise Exception("GAS query start timed out")
991
992 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
993 if ev is None:
994 raise Exception("GAS query timed out")
995 if "result=SUCCESS" not in ev:
996 raise Exception("Unexpected result: " + ev)
997
998 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
999 if ev is None:
1000 raise Exception("ANQP-QUERY-DONE event not seen")
1001 if "result=SUCCESS" not in ev:
1002 raise Exception("Unexpected result: " + ev)
1003
1004 def test_gas_anqp_extra_elements(dev, apdev):
1005 """GAS/ANQP and extra ANQP elements"""
1006 geo_loc = "001052834d12efd2b08b9b4bf1cc2c00004104050000000000060100"
1007 civic_loc = "0000f9555302f50102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5"
1008 held_uri = "https://held.example.com/location"
1009 held = struct.pack('BBB', 0, 1 + len(held_uri), 1) + held_uri
1010 supl_fqdn = "supl.example.com"
1011 supl = struct.pack('BBB', 0, 1 + len(supl_fqdn), 1) + supl_fqdn
1012 public_id = binascii.hexlify(held + supl)
1013 params = { "ssid": "gas/anqp",
1014 "interworking": "1",
1015 "anqp_elem": [ "265:" + geo_loc,
1016 "266:" + civic_loc,
1017 "262:1122334455",
1018 "267:" + public_id,
1019 "275:01020304",
1020 "60000:01",
1021 "299:0102" ] }
1022 hapd = hostapd.add_ap(apdev[0], params)
1023 bssid = apdev[0]['bssid']
1024
1025 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
1026 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 265,266"):
1027 raise Exception("ANQP_GET command failed")
1028
1029 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
1030 if ev is None:
1031 raise Exception("GAS query timed out")
1032
1033 bss = dev[0].get_bss(bssid)
1034
1035 if 'anqp[265]' not in bss:
1036 raise Exception("AP Geospatial Location ANQP-element not seen")
1037 if bss['anqp[265]'] != geo_loc:
1038 raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
1039
1040 if 'anqp[266]' not in bss:
1041 raise Exception("AP Civic Location ANQP-element not seen")
1042 if bss['anqp[266]'] != civic_loc:
1043 raise Exception("Unexpected AP Civic Location ANQP-element value: " + bss['anqp[266]'])
1044
1045 dev[1].scan_for_bss(bssid, freq="2412", force_scan=True)
1046 if "OK" not in dev[1].request("ANQP_GET " + bssid + " 257,258,259,260,261,262,263,264,265,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"):
1047 raise Exception("ANQP_GET command failed")
1048
1049 ev = dev[1].wait_event(["GAS-QUERY-DONE"], timeout=10)
1050 if ev is None:
1051 raise Exception("GAS query timed out")
1052
1053 bss = dev[1].get_bss(bssid)
1054
1055 if 'anqp[265]' not in bss:
1056 raise Exception("AP Geospatial Location ANQP-element not seen")
1057 if bss['anqp[265]'] != geo_loc:
1058 raise Exception("Unexpected AP Geospatial Location ANQP-element value: " + bss['anqp[265]'])
1059
1060 if 'anqp[266]' in bss:
1061 raise Exception("AP Civic Location ANQP-element unexpectedly seen")
1062
1063 if 'anqp[267]' not in bss:
1064 raise Exception("AP Location Public Identifier ANQP-element not seen")
1065 if bss['anqp[267]'] != public_id:
1066 raise Exception("Unexpected AP Location Public Identifier ANQP-element value: " + bss['anqp[267]'])
1067
1068 if 'anqp[275]' not in bss:
1069 raise Exception("ANQP-element Info ID 275 not seen")
1070 if bss['anqp[275]'] != "01020304":
1071 raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
1072
1073 if 'anqp[299]' not in bss:
1074 raise Exception("ANQP-element Info ID 299 not seen")
1075 if bss['anqp[299]'] != "0102":
1076 raise Exception("Unexpected AP ANQP-element Info ID 299 value: " + bss['anqp[299]'])
1077
1078 if 'anqp_ip_addr_type_availability' not in bss:
1079 raise Exception("ANQP-element Info ID 292 not seen")
1080 if bss['anqp_ip_addr_type_availability'] != "1122334455":
1081 raise Exception("Unexpected AP ANQP-element Info ID 262 value: " + bss['anqp_ip_addr_type_availability'])
1082
1083 def test_gas_anqp_address3_not_assoc(dev, apdev, params):
1084 """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when not associated"""
1085 try:
1086 _test_gas_anqp_address3_not_assoc(dev, apdev, params)
1087 finally:
1088 dev[0].request("SET gas_address3 0")
1089
1090 def _test_gas_anqp_address3_not_assoc(dev, apdev, params):
1091 hapd = start_ap(apdev[0])
1092 bssid = apdev[0]['bssid']
1093
1094 if "OK" not in dev[0].request("SET gas_address3 1"):
1095 raise Exception("Failed to set gas_address3")
1096
1097 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
1098 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
1099 raise Exception("ANQP_GET command failed")
1100
1101 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
1102 if ev is None:
1103 raise Exception("GAS query start timed out")
1104
1105 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
1106 if ev is None:
1107 raise Exception("GAS query timed out")
1108
1109 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
1110 if ev is None or "Venue Name" not in ev:
1111 raise Exception("Did not receive Venue Name")
1112
1113 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
1114 if ev is None:
1115 raise Exception("ANQP-QUERY-DONE event not seen")
1116 if "result=SUCCESS" not in ev:
1117 raise Exception("Unexpected result: " + ev)
1118
1119 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
1120 "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
1121 display=["wlan.bssid"])
1122 res = out.splitlines()
1123 if len(res) != 2:
1124 raise Exception("Unexpected number of GAS frames")
1125 if res[0] != 'ff:ff:ff:ff:ff:ff':
1126 raise Exception("GAS request used unexpected Address3 field value: " + res[0])
1127 if res[1] != 'ff:ff:ff:ff:ff:ff':
1128 raise Exception("GAS response used unexpected Address3 field value: " + res[1])
1129
1130 def test_gas_anqp_address3_assoc(dev, apdev, params):
1131 """GAS/ANQP query using IEEE 802.11 compliant Address 3 value when associated"""
1132 try:
1133 _test_gas_anqp_address3_assoc(dev, apdev, params)
1134 finally:
1135 dev[0].request("SET gas_address3 0")
1136
1137 def _test_gas_anqp_address3_assoc(dev, apdev, params):
1138 hapd = start_ap(apdev[0])
1139 bssid = apdev[0]['bssid']
1140
1141 if "OK" not in dev[0].request("SET gas_address3 1"):
1142 raise Exception("Failed to set gas_address3")
1143
1144 dev[0].scan_for_bss(bssid, freq="2412")
1145 dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
1146 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
1147 password="password", phase2="auth=MSCHAPV2",
1148 ca_cert="auth_serv/ca.pem", scan_freq="2412")
1149
1150 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
1151 raise Exception("ANQP_GET command failed")
1152
1153 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
1154 if ev is None:
1155 raise Exception("GAS query start timed out")
1156
1157 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
1158 if ev is None:
1159 raise Exception("GAS query timed out")
1160
1161 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
1162 if ev is None or "Venue Name" not in ev:
1163 raise Exception("Did not receive Venue Name")
1164
1165 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
1166 if ev is None:
1167 raise Exception("ANQP-QUERY-DONE event not seen")
1168 if "result=SUCCESS" not in ev:
1169 raise Exception("Unexpected result: " + ev)
1170
1171 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
1172 "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
1173 display=["wlan.bssid"])
1174 res = out.splitlines()
1175 if len(res) != 2:
1176 raise Exception("Unexpected number of GAS frames")
1177 if res[0] != bssid:
1178 raise Exception("GAS request used unexpected Address3 field value: " + res[0])
1179 if res[1] != bssid:
1180 raise Exception("GAS response used unexpected Address3 field value: " + res[1])
1181
1182 def test_gas_anqp_address3_ap_forced(dev, apdev, params):
1183 """GAS/ANQP query using IEEE 802.11 compliant Address 3 value on AP"""
1184 hapd = start_ap(apdev[0])
1185 bssid = apdev[0]['bssid']
1186 hapd.set("gas_address3", "1")
1187
1188 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
1189 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
1190 raise Exception("ANQP_GET command failed")
1191
1192 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
1193 if ev is None:
1194 raise Exception("GAS query start timed out")
1195
1196 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
1197 if ev is None:
1198 raise Exception("GAS query timed out")
1199
1200 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
1201 if ev is None or "Venue Name" not in ev:
1202 raise Exception("Did not receive Venue Name")
1203
1204 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
1205 if ev is None:
1206 raise Exception("ANQP-QUERY-DONE event not seen")
1207 if "result=SUCCESS" not in ev:
1208 raise Exception("Unexpected result: " + ev)
1209
1210 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
1211 "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
1212 display=["wlan.bssid"])
1213 res = out.splitlines()
1214 if len(res) != 2:
1215 raise Exception("Unexpected number of GAS frames")
1216 if res[0] != bssid:
1217 raise Exception("GAS request used unexpected Address3 field value: " + res[0])
1218 if res[1] != 'ff:ff:ff:ff:ff:ff':
1219 raise Exception("GAS response used unexpected Address3 field value: " + res[1])
1220
1221 def test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
1222 """GAS/ANQP query using IEEE 802.11 non-compliant Address 3 (AP)"""
1223 try:
1224 _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params)
1225 finally:
1226 dev[0].request("SET gas_address3 0")
1227
1228 def _test_gas_anqp_address3_ap_non_compliant(dev, apdev, params):
1229 hapd = start_ap(apdev[0])
1230 bssid = apdev[0]['bssid']
1231 hapd.set("gas_address3", "2")
1232
1233 if "OK" not in dev[0].request("SET gas_address3 1"):
1234 raise Exception("Failed to set gas_address3")
1235
1236 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
1237 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
1238 raise Exception("ANQP_GET command failed")
1239
1240 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
1241 if ev is None:
1242 raise Exception("GAS query start timed out")
1243
1244 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
1245 if ev is None:
1246 raise Exception("GAS query timed out")
1247
1248 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
1249 if ev is None or "Venue Name" not in ev:
1250 raise Exception("Did not receive Venue Name")
1251
1252 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
1253 if ev is None:
1254 raise Exception("ANQP-QUERY-DONE event not seen")
1255 if "result=SUCCESS" not in ev:
1256 raise Exception("Unexpected result: " + ev)
1257
1258 out = run_tshark(os.path.join(params['logdir'], "hwsim0.pcapng"),
1259 "wlan_mgt.fixed.category_code == 4 && (wlan_mgt.fixed.publicact == 0x0a || wlan_mgt.fixed.publicact == 0x0b)",
1260 display=["wlan.bssid"])
1261 res = out.splitlines()
1262 if len(res) != 2:
1263 raise Exception("Unexpected number of GAS frames")
1264 if res[0] != 'ff:ff:ff:ff:ff:ff':
1265 raise Exception("GAS request used unexpected Address3 field value: " + res[0])
1266 if res[1] != bssid:
1267 raise Exception("GAS response used unexpected Address3 field value: " + res[1])
1268
1269 def test_gas_prot_vs_not_prot(dev, apdev, params):
1270 """GAS/ANQP query protected vs. not protected"""
1271 hapd = start_ap(apdev[0])
1272 bssid = apdev[0]['bssid']
1273
1274 dev[0].scan_for_bss(bssid, freq="2412")
1275 dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
1276 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
1277 password="password", phase2="auth=MSCHAPV2",
1278 ca_cert="auth_serv/ca.pem", scan_freq="2412",
1279 ieee80211w="2")
1280
1281 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
1282 raise Exception("ANQP_GET command failed")
1283
1284 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
1285 if ev is None:
1286 raise Exception("No GAS-QUERY-DONE event")
1287 if "result=SUCCESS" not in ev:
1288 raise Exception("Unexpected GAS result: " + ev)
1289
1290 # GAS: Drop unexpected unprotected GAS frame when PMF is enabled
1291 dev[0].request("SET ext_mgmt_frame_handling 1")
1292 res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
1293 dev[0].request("SET ext_mgmt_frame_handling 0")
1294 if "OK" not in res:
1295 raise Exception("MGMT_RX_PROCESS failed")
1296
1297 dev[0].request("DISCONNECT")
1298 dev[0].wait_disconnected()
1299
1300 # GAS: No pending query found for 02:00:00:00:03:00 dialog token 0
1301 dev[0].request("SET ext_mgmt_frame_handling 1")
1302 res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000040b00000005006c027f000000")
1303 dev[0].request("SET ext_mgmt_frame_handling 0")
1304 if "OK" not in res:
1305 raise Exception("MGMT_RX_PROCESS failed")
1306
1307 # GAS: Drop unexpected protected GAS frame when PMF is disabled
1308 dev[0].request("SET ext_mgmt_frame_handling 1")
1309 res = dev[0].request("MGMT_RX_PROCESS freq=2412 datarate=0 ssi_signal=-30 frame=d0003a010200000000000200000003000200000003001000090b00000005006c027f000000")
1310 dev[0].request("SET ext_mgmt_frame_handling 0")
1311 if "OK" not in res:
1312 raise Exception("MGMT_RX_PROCESS failed")
1313
1314 def test_gas_failures(dev, apdev):
1315 """GAS failure cases"""
1316 hapd = start_ap(apdev[0])
1317 hapd.set("gas_comeback_delay", "5")
1318 bssid = apdev[0]['bssid']
1319
1320 hapd2 = start_ap(apdev[1])
1321 bssid2 = apdev[1]['bssid']
1322
1323 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
1324 dev[0].scan_for_bss(bssid2, freq="2412")
1325
1326 tests = [ (bssid, "gas_build_req;gas_query_tx_comeback_req"),
1327 (bssid, "gas_query_tx;gas_query_tx_comeback_req"),
1328 (bssid, "gas_query_append;gas_query_rx_comeback"),
1329 (bssid2, "gas_query_append;gas_query_rx_initial"),
1330 (bssid2, "wpabuf_alloc_copy;gas_query_rx_initial"),
1331 (bssid, "gas_query_tx;gas_query_tx_initial_req") ]
1332 for addr,func in tests:
1333 with alloc_fail(dev[0], 1, func):
1334 dev[0].request("ANQP_GET " + addr + " 258")
1335 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=5)
1336 if ev is None:
1337 raise Exception("No GAS-QUERY-DONE seen")
1338 if "result=INTERNAL_ERROR" not in ev:
1339 raise Exception("Unexpected result code: " + ev)
1340 dev[0].dump_monitor()
1341
1342 tests = [ "=gas_query_req", "radio_add_work;gas_query_req" ]
1343 for func in tests:
1344 with alloc_fail(dev[0], 1, func):
1345 if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
1346 raise Exception("ANQP_GET succeeded unexpectedly during OOM")
1347 dev[0].dump_monitor()
1348
1349 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
1350 wpas.interface_add("wlan5")
1351 wpas.scan_for_bss(bssid2, freq="2412")
1352 wpas.request("SET preassoc_mac_addr 1111")
1353 wpas.request("ANQP_GET " + bssid2 + " 258")
1354 ev = wpas.wait_event(["Failed to assign random MAC address for GAS"],
1355 timeout=5)
1356 wpas.request("SET preassoc_mac_addr 0")
1357 if ev is None:
1358 raise Exception("No random MAC address error seen")