2 # Copyright (c) 2013, Qualcomm Atheros, Inc.
3 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
5 # This software may be distributed under the terms of the BSD license.
6 # See README for more details.
11 logger = logging.getLogger()
16 from wpasupplicant import WpaSupplicant
17 from utils import alloc_fail, skip_with_fips
20 params = hostapd.wpa2_params(ssid="test-gas")
21 params['wpa_key_mgmt'] = "WPA-EAP"
22 params['ieee80211w'] = "1"
23 params['ieee8021x'] = "1"
24 params['auth_server_addr'] = "127.0.0.1"
25 params['auth_server_port'] = "1812"
26 params['auth_server_shared_secret'] = "radius"
27 params['interworking'] = "1"
28 params['access_network_type'] = "14"
29 params['internet'] = "1"
33 params['venue_group'] = "7"
34 params['venue_type'] = "1"
35 params['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
36 params['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
38 params['domain_name'] = "example.com,another.example.com"
39 params['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
40 "0,another.example.com" ]
41 params['anqp_3gpp_cell_net'] = "244,91"
42 params['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
43 params['ipaddr_type_availability'] = "14"
45 params['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
46 params['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
47 params['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
48 params['hs20_operating_class'] = "5173"
52 params = hs20_ap_params()
53 params['hessid'] = ap['bssid']
54 hostapd.add_ap(ap['ifname'], params)
55 return hostapd.Hostapd(ap['ifname'])
57 def get_gas_response(dev, bssid, info, allow_fetch_failure=False,
59 exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
60 res = re.split(exp, info)
62 raise Exception("Could not parse GAS-RESPONSE-INFO")
64 raise Exception("Unexpected BSSID in response")
68 raise Exception("GAS query failed")
71 raise Exception("GAS query reported invalid response length")
72 if int(resp_len) > 2000:
73 raise Exception("Unexpected long GAS response")
76 if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " 123456"):
77 raise Exception("Invalid dialog token accepted")
78 if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 10000,10001"):
79 raise Exception("Invalid range accepted")
80 if "FAIL-Invalid range" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0,10000"):
81 raise Exception("Invalid range accepted")
82 if "FAIL" not in dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 0"):
83 raise Exception("Invalid GAS_RESPONSE_GET accepted")
85 res1_2 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 1,2")
86 res5_3 = dev.request("GAS_RESPONSE_GET " + bssid + " " + token + " 5,3")
88 resp = dev.request("GAS_RESPONSE_GET " + bssid + " " + token)
90 if allow_fetch_failure:
91 logger.debug("GAS response was not available anymore")
93 raise Exception("Could not fetch GAS response")
94 if len(resp) != int(resp_len) * 2:
95 raise Exception("Unexpected GAS response length")
96 logger.debug("GAS response: " + resp)
98 if resp[2:6] != res1_2:
99 raise Exception("Unexpected response substring res1_2: " + res1_2)
100 if resp[10:16] != res5_3:
101 raise Exception("Unexpected response substring res5_3: " + res5_3)
103 def test_gas_generic(dev, apdev):
104 """Generic GAS query"""
105 bssid = apdev[0]['bssid']
106 params = hs20_ap_params()
107 params['hessid'] = bssid
108 hostapd.add_ap(apdev[0]['ifname'], params)
112 "00:11:22:33:44:55 ",
113 "00:11:22:33:44:55 ",
114 "00:11:22:33:44:55 1",
115 "00:11:22:33:44:55 1 1234",
116 "00:11:22:33:44:55 qq",
117 "00:11:22:33:44:55 qq 1234",
118 "00:11:22:33:44:55 00 1",
119 "00:11:22:33:44:55 00 123",
120 "00:11:22:33:44:55 00 ",
121 "00:11:22:33:44:55 00 qq" ]
123 if "FAIL" not in dev[0].request("GAS_REQUEST " + cmd):
124 raise Exception("Invalid GAS_REQUEST accepted: " + cmd)
126 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
127 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
129 raise Exception("GAS query request rejected")
130 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
132 raise Exception("GAS query timed out")
133 get_gas_response(dev[0], bssid, ev, extra_test=True)
135 if "FAIL" not in dev[0].request("GAS_RESPONSE_GET ff"):
136 raise Exception("Invalid GAS_RESPONSE_GET accepted")
138 def test_gas_concurrent_scan(dev, apdev):
139 """Generic GAS queries with concurrent scan operation"""
140 bssid = apdev[0]['bssid']
141 params = hs20_ap_params()
142 params['hessid'] = bssid
143 hostapd.add_ap(apdev[0]['ifname'], params)
145 # get BSS entry available to allow GAS query
146 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
148 logger.info("Request concurrent operations")
149 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
151 raise Exception("GAS query request rejected")
152 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000801")
154 raise Exception("GAS query request rejected")
155 dev[0].scan(no_wait=True)
156 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000201")
158 raise Exception("GAS query request rejected")
159 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000501")
161 raise Exception("GAS query request rejected")
164 for i in range(0, 5):
165 ev = dev[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
168 raise Exception("Operation timed out")
169 if "GAS-RESPONSE-INFO" in ev:
170 responses = responses + 1
171 get_gas_response(dev[0], bssid, ev, allow_fetch_failure=True)
174 raise Exception("Unexpected number of GAS responses")
176 def test_gas_concurrent_connect(dev, apdev):
177 """Generic GAS queries with concurrent connection operation"""
178 skip_with_fips(dev[0])
179 bssid = apdev[0]['bssid']
180 params = hs20_ap_params()
181 params['hessid'] = bssid
182 hostapd.add_ap(apdev[0]['ifname'], params)
184 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
186 logger.debug("Start concurrent connect and GAS request")
187 dev[0].connect("test-gas", key_mgmt="WPA-EAP", eap="TTLS",
188 identity="DOMAIN\mschapv2 user", anonymous_identity="ttls",
189 password="password", phase2="auth=MSCHAPV2",
190 ca_cert="auth_serv/ca.pem", wait_connect=False,
192 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
194 raise Exception("GAS query request rejected")
196 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
199 raise Exception("Operation timed out")
200 if "CTRL-EVENT-CONNECTED" not in ev:
201 raise Exception("Unexpected operation order")
203 ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
206 raise Exception("Operation timed out")
207 if "GAS-RESPONSE-INFO" not in ev:
208 raise Exception("Unexpected operation order")
209 get_gas_response(dev[0], bssid, ev)
211 dev[0].request("DISCONNECT")
212 dev[0].wait_disconnected(timeout=5)
214 logger.debug("Wait six seconds for expiration of connect-without-scan")
216 dev[0].dump_monitor()
218 logger.debug("Start concurrent GAS request and connect")
219 req = dev[0].request("GAS_REQUEST " + bssid + " 00 000102000101")
221 raise Exception("GAS query request rejected")
222 dev[0].request("RECONNECT")
224 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
226 raise Exception("Operation timed out")
227 get_gas_response(dev[0], bssid, ev)
229 ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=20)
231 raise Exception("No new scan results reported")
233 ev = dev[0].wait_connected(timeout=20, error="Operation tiemd out")
234 if "CTRL-EVENT-CONNECTED" not in ev:
235 raise Exception("Unexpected operation order")
237 def test_gas_fragment(dev, apdev):
238 """GAS fragmentation"""
239 hapd = start_ap(apdev[0])
240 hapd.set("gas_frag_limit", "50")
242 dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
243 dev[0].request("FETCH_ANQP")
244 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=1)
246 raise Exception("No GAS-QUERY-DONE event")
247 if "result=SUCCESS" not in ev:
248 raise Exception("Unexpected GAS result: " + ev)
249 for i in range(0, 13):
250 ev = dev[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout=5)
252 raise Exception("Operation timed out")
253 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=1)
255 raise Exception("No ANQP-QUERY-DONE event")
256 if "result=SUCCESS" not in ev:
257 raise Exception("Unexpected ANQP result: " + ev)
259 def test_gas_comeback_delay(dev, apdev):
260 """GAS comeback delay"""
261 hapd = start_ap(apdev[0])
262 hapd.set("gas_comeback_delay", "500")
264 dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
265 dev[0].request("FETCH_ANQP")
266 if "FAIL-BUSY" not in dev[0].request("SCAN"):
267 raise Exception("SCAN accepted during FETCH_ANQP")
268 for i in range(0, 6):
269 ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
271 raise Exception("Operation timed out")
273 def test_gas_stop_fetch_anqp(dev, apdev):
274 """Stop FETCH_ANQP operation"""
275 hapd = start_ap(apdev[0])
277 dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
278 hapd.set("ext_mgmt_frame_handling", "1")
279 dev[0].request("FETCH_ANQP")
280 dev[0].request("STOP_FETCH_ANQP")
281 hapd.set("ext_mgmt_frame_handling", "0")
282 ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
284 raise Exception("GAS-QUERY-DONE timed out")
286 raise Exception("Unexpected ANQP response received")
288 def test_gas_anqp_get(dev, apdev):
289 """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
290 hapd = start_ap(apdev[0])
291 bssid = apdev[0]['bssid']
293 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
294 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
295 raise Exception("ANQP_GET command failed")
297 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
299 raise Exception("GAS query start timed out")
301 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
303 raise Exception("GAS query timed out")
305 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
306 if ev is None or "Venue Name" not in ev:
307 raise Exception("Did not receive Venue Name")
309 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
310 if ev is None or "Domain Name list" not in ev:
311 raise Exception("Did not receive Domain Name list")
313 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
314 if ev is None or "Operator Friendly Name" not in ev:
315 raise Exception("Did not receive Operator Friendly Name")
317 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
318 if ev is None or "WAN Metrics" not in ev:
319 raise Exception("Did not receive WAN Metrics")
321 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
323 raise Exception("ANQP-QUERY-DONE event not seen")
324 if "result=SUCCESS" not in ev:
325 raise Exception("Unexpected result: " + ev)
327 if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
328 raise Exception("ANQP_GET command failed")
330 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
331 if ev is None or "Operator Friendly Name" not in ev:
332 raise Exception("Did not receive Operator Friendly Name")
334 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
335 if ev is None or "WAN Metrics" not in ev:
336 raise Exception("Did not receive WAN Metrics")
340 "00:11:22:33:44:55 258,hs20:-1",
341 "00:11:22:33:44:55 258,hs20:0",
342 "00:11:22:33:44:55 258,hs20:32",
343 "00:11:22:33:44:55 hs20:-1",
344 "00:11:22:33:44:55 hs20:0",
345 "00:11:22:33:44:55 hs20:32",
347 "00:11:22:33:44:55 ",
348 "00:11:22:33:44:55 0" ]
350 if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
351 raise Exception("Invalid ANQP_GET accepted")
355 "00:11:22:33:44:55 -1",
356 "00:11:22:33:44:55 0",
357 "00:11:22:33:44:55 32",
359 "00:11:22:33:44:55 ",
360 "00:11:22:33:44:55 0" ]
362 if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
363 raise Exception("Invalid HS20_ANQP_GET accepted")
365 def expect_gas_result(dev, result, status=None):
366 ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
368 raise Exception("GAS query timed out")
369 if "result=" + result not in ev:
370 raise Exception("Unexpected GAS query result")
371 if status and "status_code=" + str(status) + ' ' not in ev:
372 raise Exception("Unexpected GAS status code")
374 def anqp_get(dev, bssid, id):
375 if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
376 raise Exception("ANQP_GET command failed")
377 ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
379 raise Exception("GAS query start timed out")
381 def test_gas_timeout(dev, apdev):
383 hapd = start_ap(apdev[0])
384 bssid = apdev[0]['bssid']
386 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
387 hapd.set("ext_mgmt_frame_handling", "1")
389 anqp_get(dev[0], bssid, 263)
391 ev = hapd.wait_event(["MGMT-RX"], timeout=5)
393 raise Exception("MGMT RX wait timed out")
395 expect_gas_result(dev[0], "TIMEOUT")
397 MGMT_SUBTYPE_ACTION = 13
398 ACTION_CATEG_PUBLIC = 4
400 GAS_INITIAL_REQUEST = 10
401 GAS_INITIAL_RESPONSE = 11
402 GAS_COMEBACK_REQUEST = 12
403 GAS_COMEBACK_RESPONSE = 13
404 GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
405 GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
407 def anqp_adv_proto():
408 return struct.pack('BBBB', 108, 2, 127, 0)
410 def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
411 return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
412 dialog_token, status_code, comeback_delay) + anqp_adv_proto()
414 def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
418 adv = struct.pack('BBBB', 108, 2, 127, 1)
420 adv = anqp_adv_proto()
421 return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
422 dialog_token, status_code, id, comeback_delay) + adv
428 query = hapd.mgmt_rx()
430 raise Exception("Action frame not received")
431 if query['subtype'] != MGMT_SUBTYPE_ACTION:
433 payload = query['payload']
436 (category, action) = struct.unpack('BB', payload[0:2])
437 if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
440 raise Exception("No Action frame received")
442 def parse_gas(payload):
444 (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
445 if category != ACTION_CATEG_PUBLIC:
447 if action not in GAS_ACTIONS:
450 gas['action'] = action
453 if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
456 gas['dialog_token'] = dialog_token
458 if action == GAS_INITIAL_RESPONSE:
461 (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
462 gas['status_code'] = status_code
463 gas['comeback_delay'] = comeback_delay
465 if action == GAS_COMEBACK_RESPONSE:
468 (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
469 gas['status_code'] = status_code
471 gas['comeback_delay'] = comeback_delay
475 def action_response(req):
477 resp['fc'] = req['fc']
478 resp['da'] = req['sa']
479 resp['sa'] = req['da']
480 resp['bssid'] = req['bssid']
483 def send_gas_resp(hapd, resp):
485 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
487 raise Exception("Missing TX status for GAS response")
489 raise Exception("GAS response not acknowledged")
491 def test_gas_invalid_response_type(dev, apdev):
492 """GAS invalid response type"""
493 hapd = start_ap(apdev[0])
494 bssid = apdev[0]['bssid']
496 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
497 hapd.set("ext_mgmt_frame_handling", "1")
499 anqp_get(dev[0], bssid, 263)
502 gas = parse_gas(query['payload'])
504 resp = action_response(query)
505 # GAS Comeback Response instead of GAS Initial Response
506 resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
507 send_gas_resp(hapd, resp)
509 # station drops the invalid frame, so this needs to result in GAS timeout
510 expect_gas_result(dev[0], "TIMEOUT")
512 def test_gas_failure_status_code(dev, apdev):
513 """GAS failure status code"""
514 hapd = start_ap(apdev[0])
515 bssid = apdev[0]['bssid']
517 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
518 hapd.set("ext_mgmt_frame_handling", "1")
520 anqp_get(dev[0], bssid, 263)
523 gas = parse_gas(query['payload'])
525 resp = action_response(query)
526 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
527 send_gas_resp(hapd, resp)
529 expect_gas_result(dev[0], "FAILURE")
531 def test_gas_malformed(dev, apdev):
532 """GAS malformed response frames"""
533 hapd = start_ap(apdev[0])
534 bssid = apdev[0]['bssid']
536 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
537 hapd.set("ext_mgmt_frame_handling", "1")
539 anqp_get(dev[0], bssid, 263)
542 gas = parse_gas(query['payload'])
544 resp = action_response(query)
546 resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
547 GAS_COMEBACK_RESPONSE,
548 gas['dialog_token'], 0)
551 resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
552 GAS_COMEBACK_RESPONSE,
553 gas['dialog_token'], 0, 0)
556 hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
557 gas['dialog_token'], 0, 0)
558 resp['payload'] = hdr + struct.pack('B', 108)
560 resp['payload'] = hdr + struct.pack('BB', 108, 0)
562 resp['payload'] = hdr + struct.pack('BB', 108, 1)
564 resp['payload'] = hdr + struct.pack('BB', 108, 255)
566 resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
568 resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
570 resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
573 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
576 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
579 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
582 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
585 # Station drops invalid frames, but the last of the responses is valid from
586 # GAS view point even though it has an extra octet in the end and the ANQP
587 # part of the response is not valid. This is reported as successfully
588 # completed GAS exchange.
589 expect_gas_result(dev[0], "SUCCESS")
591 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
593 raise Exception("ANQP-QUERY-DONE not reported")
594 if "result=INVALID_FRAME" not in ev:
595 raise Exception("Unexpected result: " + ev)
597 def init_gas(hapd, bssid, dev):
598 anqp_get(dev, bssid, 263)
600 gas = parse_gas(query['payload'])
601 dialog_token = gas['dialog_token']
603 resp = action_response(query)
604 resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
605 send_gas_resp(hapd, resp)
608 gas = parse_gas(query['payload'])
609 if gas['action'] != GAS_COMEBACK_REQUEST:
610 raise Exception("Unexpected request action")
611 if gas['dialog_token'] != dialog_token:
612 raise Exception("Unexpected dialog token change")
613 return query, dialog_token
615 def test_gas_malformed_comeback_resp(dev, apdev):
616 """GAS malformed comeback response frames"""
617 hapd = start_ap(apdev[0])
618 bssid = apdev[0]['bssid']
620 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
621 hapd.set("ext_mgmt_frame_handling", "1")
623 logger.debug("Non-zero status code in comeback response")
624 query, dialog_token = init_gas(hapd, bssid, dev[0])
625 resp = action_response(query)
626 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
627 send_gas_resp(hapd, resp)
628 expect_gas_result(dev[0], "FAILURE", status=2)
630 logger.debug("Different advertisement protocol in comeback response")
631 query, dialog_token = init_gas(hapd, bssid, dev[0])
632 resp = action_response(query)
633 resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
634 send_gas_resp(hapd, resp)
635 expect_gas_result(dev[0], "PEER_ERROR")
637 logger.debug("Non-zero frag id and comeback delay in comeback response")
638 query, dialog_token = init_gas(hapd, bssid, dev[0])
639 resp = action_response(query)
640 resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
641 send_gas_resp(hapd, resp)
642 expect_gas_result(dev[0], "PEER_ERROR")
644 logger.debug("Unexpected frag id in comeback response")
645 query, dialog_token = init_gas(hapd, bssid, dev[0])
646 resp = action_response(query)
647 resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
648 send_gas_resp(hapd, resp)
649 expect_gas_result(dev[0], "PEER_ERROR")
651 logger.debug("Empty fragment and replay in comeback response")
652 query, dialog_token = init_gas(hapd, bssid, dev[0])
653 resp = action_response(query)
654 resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
655 send_gas_resp(hapd, resp)
657 gas = parse_gas(query['payload'])
658 if gas['action'] != GAS_COMEBACK_REQUEST:
659 raise Exception("Unexpected request action")
660 if gas['dialog_token'] != dialog_token:
661 raise Exception("Unexpected dialog token change")
662 resp = action_response(query)
663 resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
664 send_gas_resp(hapd, resp)
665 resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
666 send_gas_resp(hapd, resp)
667 expect_gas_result(dev[0], "SUCCESS")
669 logger.debug("Unexpected initial response when waiting for comeback response")
670 query, dialog_token = init_gas(hapd, bssid, dev[0])
671 resp = action_response(query)
672 resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
673 send_gas_resp(hapd, resp)
674 ev = hapd.wait_event(["MGMT-RX"], timeout=1)
676 raise Exception("Unexpected management frame")
677 expect_gas_result(dev[0], "TIMEOUT")
679 logger.debug("Too short comeback response")
680 query, dialog_token = init_gas(hapd, bssid, dev[0])
681 resp = action_response(query)
682 resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
683 GAS_COMEBACK_RESPONSE, dialog_token, 0)
684 send_gas_resp(hapd, resp)
685 ev = hapd.wait_event(["MGMT-RX"], timeout=1)
687 raise Exception("Unexpected management frame")
688 expect_gas_result(dev[0], "TIMEOUT")
690 logger.debug("Too short comeback response(2)")
691 query, dialog_token = init_gas(hapd, bssid, dev[0])
692 resp = action_response(query)
693 resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
694 GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
696 send_gas_resp(hapd, resp)
697 ev = hapd.wait_event(["MGMT-RX"], timeout=1)
699 raise Exception("Unexpected management frame")
700 expect_gas_result(dev[0], "TIMEOUT")
702 logger.debug("Maximum comeback response fragment claiming more fragments")
703 query, dialog_token = init_gas(hapd, bssid, dev[0])
704 resp = action_response(query)
705 resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
706 send_gas_resp(hapd, resp)
707 for i in range(1, 129):
709 gas = parse_gas(query['payload'])
710 if gas['action'] != GAS_COMEBACK_REQUEST:
711 raise Exception("Unexpected request action")
712 if gas['dialog_token'] != dialog_token:
713 raise Exception("Unexpected dialog token change")
714 resp = action_response(query)
715 resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
716 send_gas_resp(hapd, resp)
717 expect_gas_result(dev[0], "PEER_ERROR")
719 def test_gas_comeback_resp_additional_delay(dev, apdev):
720 """GAS comeback response requesting additional delay"""
721 hapd = start_ap(apdev[0])
722 bssid = apdev[0]['bssid']
724 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
725 hapd.set("ext_mgmt_frame_handling", "1")
727 query, dialog_token = init_gas(hapd, bssid, dev[0])
728 for i in range(0, 2):
729 resp = action_response(query)
730 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
731 send_gas_resp(hapd, resp)
733 gas = parse_gas(query['payload'])
734 if gas['action'] != GAS_COMEBACK_REQUEST:
735 raise Exception("Unexpected request action")
736 if gas['dialog_token'] != dialog_token:
737 raise Exception("Unexpected dialog token change")
738 resp = action_response(query)
739 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
740 send_gas_resp(hapd, resp)
741 expect_gas_result(dev[0], "SUCCESS")
743 def test_gas_unknown_adv_proto(dev, apdev):
744 """Unknown advertisement protocol id"""
745 bssid = apdev[0]['bssid']
746 params = hs20_ap_params()
747 params['hessid'] = bssid
748 hostapd.add_ap(apdev[0]['ifname'], params)
750 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
751 req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
753 raise Exception("GAS query request rejected")
754 expect_gas_result(dev[0], "FAILURE", "59")
755 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
757 raise Exception("GAS query timed out")
758 exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
759 res = re.split(exp, ev)
761 raise Exception("Could not parse GAS-RESPONSE-INFO")
763 raise Exception("Unexpected BSSID in response")
766 raise Exception("Unexpected GAS-RESPONSE-INFO status")
768 def test_gas_max_pending(dev, apdev):
769 """GAS and maximum pending query limit"""
770 hapd = start_ap(apdev[0])
771 hapd.set("gas_frag_limit", "50")
772 bssid = apdev[0]['bssid']
774 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
775 wpas.interface_add("wlan5")
776 if "OK" not in wpas.request("P2P_SET listen_channel 1"):
777 raise Exception("Failed to set listen channel")
778 if "OK" not in wpas.p2p_listen():
779 raise Exception("Failed to start listen state")
780 if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
781 raise Exception("Failed to enable external management frame handling")
783 anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
784 gas = struct.pack('<H', len(anqp_query)) + anqp_query
786 for dialog_token in range(1, 10):
787 msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
788 dialog_token) + anqp_adv_proto() + gas
789 req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
790 if "OK" not in wpas.request(req):
791 raise Exception("Could not send management frame")
792 resp = wpas.mgmt_rx()
794 raise Exception("MGMT-RX timeout")
795 if 'payload' not in resp:
796 raise Exception("Missing payload")
797 gresp = parse_gas(resp['payload'])
798 if gresp['dialog_token'] != dialog_token:
799 raise Exception("Dialog token mismatch")
800 status_code = gresp['status_code']
801 if dialog_token < 9 and status_code != 0:
802 raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
803 if dialog_token > 8 and status_code == 0:
804 raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
806 def test_gas_no_pending(dev, apdev):
807 """GAS and no pending query for comeback request"""
808 hapd = start_ap(apdev[0])
809 bssid = apdev[0]['bssid']
811 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
812 wpas.interface_add("wlan5")
813 if "OK" not in wpas.request("P2P_SET listen_channel 1"):
814 raise Exception("Failed to set listen channel")
815 if "OK" not in wpas.p2p_listen():
816 raise Exception("Failed to start listen state")
817 if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
818 raise Exception("Failed to enable external management frame handling")
820 msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
821 req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
822 if "OK" not in wpas.request(req):
823 raise Exception("Could not send management frame")
824 resp = wpas.mgmt_rx()
826 raise Exception("MGMT-RX timeout")
827 if 'payload' not in resp:
828 raise Exception("Missing payload")
829 gresp = parse_gas(resp['payload'])
830 status_code = gresp['status_code']
831 if status_code != 60:
832 raise Exception("Unexpected status code {} (expected 60)".format(status_code))
834 def test_gas_missing_payload(dev, apdev):
835 """No action code in the query frame"""
836 bssid = apdev[0]['bssid']
837 params = hs20_ap_params()
838 params['hessid'] = bssid
839 hostapd.add_ap(apdev[0]['ifname'], params)
841 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
843 cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
844 if "FAIL" in dev[0].request(cmd):
845 raise Exception("Could not send test Action frame")
846 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
848 raise Exception("Timeout on MGMT-TX-STATUS")
849 if "result=SUCCESS" not in ev:
850 raise Exception("AP did not ack Action frame")
852 cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
853 if "FAIL" in dev[0].request(cmd):
854 raise Exception("Could not send test Action frame")
855 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
857 raise Exception("Timeout on MGMT-TX-STATUS")
858 if "result=SUCCESS" not in ev:
859 raise Exception("AP did not ack Action frame")
861 def test_gas_query_deinit(dev, apdev):
862 """Pending GAS/ANQP query during deinit"""
863 hapd = start_ap(apdev[0])
864 bssid = apdev[0]['bssid']
866 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
867 wpas.interface_add("wlan5")
869 wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
870 id = wpas.request("RADIO_WORK add block-work")
871 if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
872 raise Exception("ANQP_GET command failed")
874 ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
876 raise Exception("Timeout while waiting radio work to start")
877 ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
879 raise Exception("Timeout while waiting radio work to start (2)")
881 # Remove the interface while the gas-query radio work is still pending and
882 # GAS query has not yet been started.
883 wpas.interface_remove("wlan5")
885 def test_gas_anqp_oom_wpas(dev, apdev):
886 """GAS/ANQP query and OOM in wpa_supplicant"""
887 hapd = start_ap(apdev[0])
888 bssid = apdev[0]['bssid']
890 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
892 with alloc_fail(dev[0], 1, "gas_build_req"):
893 if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
894 raise Exception("Unexpected ANQP_GET command success (OOM)")
896 def test_gas_anqp_oom_hapd(dev, apdev):
897 """GAS/ANQP query and OOM in hostapd"""
898 hapd = start_ap(apdev[0])
899 bssid = apdev[0]['bssid']
901 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
903 with alloc_fail(hapd, 1, "gas_build_resp"):
904 # This query will time out due to the AP not sending a response (OOM).
905 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
906 raise Exception("ANQP_GET command failed")
908 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
910 raise Exception("GAS query start timed out")
912 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
914 raise Exception("GAS query timed out")
915 if "result=TIMEOUT" not in ev:
916 raise Exception("Unexpected result: " + ev)
918 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
920 raise Exception("ANQP-QUERY-DONE event not seen")
921 if "result=FAILURE" not in ev:
922 raise Exception("Unexpected result: " + ev)
924 with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
925 hapd.set("gas_frag_limit", "50")
927 # This query will time out due to the AP not sending a response (OOM).
928 print dev[0].request("FETCH_ANQP")
929 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
931 raise Exception("GAS query start timed out")
933 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
935 raise Exception("GAS query timed out")
936 if "result=TIMEOUT" not in ev:
937 raise Exception("Unexpected result: " + ev)
939 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
941 raise Exception("ANQP-QUERY-DONE event not seen")
942 if "result=FAILURE" not in ev:
943 raise Exception("Unexpected result: " + ev)