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 fragmentation"""
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 for i in range(0, 6):
267 ev = dev[0].wait_event(["RX-ANQP"], timeout=5)
269 raise Exception("Operation timed out")
271 def test_gas_stop_fetch_anqp(dev, apdev):
272 """Stop FETCH_ANQP operation"""
273 hapd = start_ap(apdev[0])
275 dev[0].scan_for_bss(apdev[0]['bssid'], freq="2412", force_scan=True)
276 hapd.set("ext_mgmt_frame_handling", "1")
277 dev[0].request("FETCH_ANQP")
278 dev[0].request("STOP_FETCH_ANQP")
279 hapd.set("ext_mgmt_frame_handling", "0")
280 ev = dev[0].wait_event(["RX-ANQP", "GAS-QUERY-DONE"], timeout=10)
282 raise Exception("GAS-QUERY-DONE timed out")
284 raise Exception("Unexpected ANQP response received")
286 def test_gas_anqp_get(dev, apdev):
287 """GAS/ANQP query for both IEEE 802.11 and Hotspot 2.0 elements"""
288 hapd = start_ap(apdev[0])
289 bssid = apdev[0]['bssid']
291 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
292 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258,268,hs20:3,hs20:4"):
293 raise Exception("ANQP_GET command failed")
295 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
297 raise Exception("GAS query start timed out")
299 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
301 raise Exception("GAS query timed out")
303 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
304 if ev is None or "Venue Name" not in ev:
305 raise Exception("Did not receive Venue Name")
307 ev = dev[0].wait_event(["RX-ANQP"], timeout=1)
308 if ev is None or "Domain Name list" not in ev:
309 raise Exception("Did not receive Domain Name list")
311 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
312 if ev is None or "Operator Friendly Name" not in ev:
313 raise Exception("Did not receive Operator Friendly Name")
315 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
316 if ev is None or "WAN Metrics" not in ev:
317 raise Exception("Did not receive WAN Metrics")
319 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
321 raise Exception("ANQP-QUERY-DONE event not seen")
322 if "result=SUCCESS" not in ev:
323 raise Exception("Unexpected result: " + ev)
325 if "OK" not in dev[0].request("HS20_ANQP_GET " + bssid + " 3,4"):
326 raise Exception("ANQP_GET command failed")
328 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
329 if ev is None or "Operator Friendly Name" not in ev:
330 raise Exception("Did not receive Operator Friendly Name")
332 ev = dev[0].wait_event(["RX-HS20-ANQP"], timeout=1)
333 if ev is None or "WAN Metrics" not in ev:
334 raise Exception("Did not receive WAN Metrics")
338 "00:11:22:33:44:55 258,hs20:-1",
339 "00:11:22:33:44:55 258,hs20:0",
340 "00:11:22:33:44:55 258,hs20:32",
341 "00:11:22:33:44:55 hs20:-1",
342 "00:11:22:33:44:55 hs20:0",
343 "00:11:22:33:44:55 hs20:32",
345 "00:11:22:33:44:55 ",
346 "00:11:22:33:44:55 0" ]
348 if "FAIL" not in dev[0].request("ANQP_GET " + cmd):
349 raise Exception("Invalid ANQP_GET accepted")
353 "00:11:22:33:44:55 -1",
354 "00:11:22:33:44:55 0",
355 "00:11:22:33:44:55 32",
357 "00:11:22:33:44:55 ",
358 "00:11:22:33:44:55 0" ]
360 if "FAIL" not in dev[0].request("HS20_ANQP_GET " + cmd):
361 raise Exception("Invalid HS20_ANQP_GET accepted")
363 def expect_gas_result(dev, result, status=None):
364 ev = dev.wait_event(["GAS-QUERY-DONE"], timeout=10)
366 raise Exception("GAS query timed out")
367 if "result=" + result not in ev:
368 raise Exception("Unexpected GAS query result")
369 if status and "status_code=" + str(status) + ' ' not in ev:
370 raise Exception("Unexpected GAS status code")
372 def anqp_get(dev, bssid, id):
373 if "OK" not in dev.request("ANQP_GET " + bssid + " " + str(id)):
374 raise Exception("ANQP_GET command failed")
375 ev = dev.wait_event(["GAS-QUERY-START"], timeout=5)
377 raise Exception("GAS query start timed out")
379 def test_gas_timeout(dev, apdev):
381 hapd = start_ap(apdev[0])
382 bssid = apdev[0]['bssid']
384 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
385 hapd.set("ext_mgmt_frame_handling", "1")
387 anqp_get(dev[0], bssid, 263)
389 ev = hapd.wait_event(["MGMT-RX"], timeout=5)
391 raise Exception("MGMT RX wait timed out")
393 expect_gas_result(dev[0], "TIMEOUT")
395 MGMT_SUBTYPE_ACTION = 13
396 ACTION_CATEG_PUBLIC = 4
398 GAS_INITIAL_REQUEST = 10
399 GAS_INITIAL_RESPONSE = 11
400 GAS_COMEBACK_REQUEST = 12
401 GAS_COMEBACK_RESPONSE = 13
402 GAS_ACTIONS = [ GAS_INITIAL_REQUEST, GAS_INITIAL_RESPONSE,
403 GAS_COMEBACK_REQUEST, GAS_COMEBACK_RESPONSE ]
405 def anqp_adv_proto():
406 return struct.pack('BBBB', 108, 2, 127, 0)
408 def anqp_initial_resp(dialog_token, status_code, comeback_delay=0):
409 return struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
410 dialog_token, status_code, comeback_delay) + anqp_adv_proto()
412 def anqp_comeback_resp(dialog_token, status_code=0, id=0, more=False, comeback_delay=0, bogus_adv_proto=False):
416 adv = struct.pack('BBBB', 108, 2, 127, 1)
418 adv = anqp_adv_proto()
419 return struct.pack('<BBBHBH', ACTION_CATEG_PUBLIC, GAS_COMEBACK_RESPONSE,
420 dialog_token, status_code, id, comeback_delay) + adv
426 query = hapd.mgmt_rx()
428 raise Exception("Action frame not received")
429 if query['subtype'] != MGMT_SUBTYPE_ACTION:
431 payload = query['payload']
434 (category, action) = struct.unpack('BB', payload[0:2])
435 if category != ACTION_CATEG_PUBLIC or action not in GAS_ACTIONS:
438 raise Exception("No Action frame received")
440 def parse_gas(payload):
442 (category, action, dialog_token) = struct.unpack('BBB', pos[0:3])
443 if category != ACTION_CATEG_PUBLIC:
445 if action not in GAS_ACTIONS:
448 gas['action'] = action
451 if len(pos) < 1 and action != GAS_COMEBACK_REQUEST:
454 gas['dialog_token'] = dialog_token
456 if action == GAS_INITIAL_RESPONSE:
459 (status_code, comeback_delay) = struct.unpack('<HH', pos[0:4])
460 gas['status_code'] = status_code
461 gas['comeback_delay'] = comeback_delay
463 if action == GAS_COMEBACK_RESPONSE:
466 (status_code, frag, comeback_delay) = struct.unpack('<HBH', pos[0:5])
467 gas['status_code'] = status_code
469 gas['comeback_delay'] = comeback_delay
473 def action_response(req):
475 resp['fc'] = req['fc']
476 resp['da'] = req['sa']
477 resp['sa'] = req['da']
478 resp['bssid'] = req['bssid']
481 def send_gas_resp(hapd, resp):
483 ev = hapd.wait_event(["MGMT-TX-STATUS"], timeout=5)
485 raise Exception("Missing TX status for GAS response")
487 raise Exception("GAS response not acknowledged")
489 def test_gas_invalid_response_type(dev, apdev):
490 """GAS invalid response type"""
491 hapd = start_ap(apdev[0])
492 bssid = apdev[0]['bssid']
494 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
495 hapd.set("ext_mgmt_frame_handling", "1")
497 anqp_get(dev[0], bssid, 263)
500 gas = parse_gas(query['payload'])
502 resp = action_response(query)
503 # GAS Comeback Response instead of GAS Initial Response
504 resp['payload'] = anqp_comeback_resp(gas['dialog_token']) + struct.pack('<H', 0)
505 send_gas_resp(hapd, resp)
507 # station drops the invalid frame, so this needs to result in GAS timeout
508 expect_gas_result(dev[0], "TIMEOUT")
510 def test_gas_failure_status_code(dev, apdev):
511 """GAS failure status code"""
512 hapd = start_ap(apdev[0])
513 bssid = apdev[0]['bssid']
515 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
516 hapd.set("ext_mgmt_frame_handling", "1")
518 anqp_get(dev[0], bssid, 263)
521 gas = parse_gas(query['payload'])
523 resp = action_response(query)
524 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 61) + struct.pack('<H', 0)
525 send_gas_resp(hapd, resp)
527 expect_gas_result(dev[0], "FAILURE")
529 def test_gas_malformed(dev, apdev):
530 """GAS malformed response frames"""
531 hapd = start_ap(apdev[0])
532 bssid = apdev[0]['bssid']
534 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
535 hapd.set("ext_mgmt_frame_handling", "1")
537 anqp_get(dev[0], bssid, 263)
540 gas = parse_gas(query['payload'])
542 resp = action_response(query)
544 resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
545 GAS_COMEBACK_RESPONSE,
546 gas['dialog_token'], 0)
549 resp['payload'] = struct.pack('<BBBHB', ACTION_CATEG_PUBLIC,
550 GAS_COMEBACK_RESPONSE,
551 gas['dialog_token'], 0, 0)
554 hdr = struct.pack('<BBBHH', ACTION_CATEG_PUBLIC, GAS_INITIAL_RESPONSE,
555 gas['dialog_token'], 0, 0)
556 resp['payload'] = hdr + struct.pack('B', 108)
558 resp['payload'] = hdr + struct.pack('BB', 108, 0)
560 resp['payload'] = hdr + struct.pack('BB', 108, 1)
562 resp['payload'] = hdr + struct.pack('BB', 108, 255)
564 resp['payload'] = hdr + struct.pack('BBB', 108, 1, 127)
566 resp['payload'] = hdr + struct.pack('BBB', 108, 2, 127)
568 resp['payload'] = hdr + struct.pack('BBBB', 0, 2, 127, 0)
571 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 1)
574 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HB', 2, 0)
577 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<H', 65535)
580 resp['payload'] = anqp_initial_resp(gas['dialog_token'], 0) + struct.pack('<HBB', 1, 0, 0)
583 # Station drops invalid frames, but the last of the responses is valid from
584 # GAS view point even though it has an extra octet in the end and the ANQP
585 # part of the response is not valid. This is reported as successfully
586 # completed GAS exchange.
587 expect_gas_result(dev[0], "SUCCESS")
589 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=5)
591 raise Exception("ANQP-QUERY-DONE not reported")
592 if "result=INVALID_FRAME" not in ev:
593 raise Exception("Unexpected result: " + ev)
595 def init_gas(hapd, bssid, dev):
596 anqp_get(dev, bssid, 263)
598 gas = parse_gas(query['payload'])
599 dialog_token = gas['dialog_token']
601 resp = action_response(query)
602 resp['payload'] = anqp_initial_resp(dialog_token, 0, comeback_delay=1) + struct.pack('<H', 0)
603 send_gas_resp(hapd, resp)
606 gas = parse_gas(query['payload'])
607 if gas['action'] != GAS_COMEBACK_REQUEST:
608 raise Exception("Unexpected request action")
609 if gas['dialog_token'] != dialog_token:
610 raise Exception("Unexpected dialog token change")
611 return query, dialog_token
613 def test_gas_malformed_comeback_resp(dev, apdev):
614 """GAS malformed comeback response frames"""
615 hapd = start_ap(apdev[0])
616 bssid = apdev[0]['bssid']
618 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
619 hapd.set("ext_mgmt_frame_handling", "1")
621 logger.debug("Non-zero status code in comeback response")
622 query, dialog_token = init_gas(hapd, bssid, dev[0])
623 resp = action_response(query)
624 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=2) + struct.pack('<H', 0)
625 send_gas_resp(hapd, resp)
626 expect_gas_result(dev[0], "FAILURE", status=2)
628 logger.debug("Different advertisement protocol in comeback response")
629 query, dialog_token = init_gas(hapd, bssid, dev[0])
630 resp = action_response(query)
631 resp['payload'] = anqp_comeback_resp(dialog_token, bogus_adv_proto=True) + struct.pack('<H', 0)
632 send_gas_resp(hapd, resp)
633 expect_gas_result(dev[0], "PEER_ERROR")
635 logger.debug("Non-zero frag id and comeback delay in comeback response")
636 query, dialog_token = init_gas(hapd, bssid, dev[0])
637 resp = action_response(query)
638 resp['payload'] = anqp_comeback_resp(dialog_token, id=1, comeback_delay=1) + struct.pack('<H', 0)
639 send_gas_resp(hapd, resp)
640 expect_gas_result(dev[0], "PEER_ERROR")
642 logger.debug("Unexpected frag id in comeback response")
643 query, dialog_token = init_gas(hapd, bssid, dev[0])
644 resp = action_response(query)
645 resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
646 send_gas_resp(hapd, resp)
647 expect_gas_result(dev[0], "PEER_ERROR")
649 logger.debug("Empty fragment and replay in comeback response")
650 query, dialog_token = init_gas(hapd, bssid, dev[0])
651 resp = action_response(query)
652 resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
653 send_gas_resp(hapd, resp)
655 gas = parse_gas(query['payload'])
656 if gas['action'] != GAS_COMEBACK_REQUEST:
657 raise Exception("Unexpected request action")
658 if gas['dialog_token'] != dialog_token:
659 raise Exception("Unexpected dialog token change")
660 resp = action_response(query)
661 resp['payload'] = anqp_comeback_resp(dialog_token) + struct.pack('<H', 0)
662 send_gas_resp(hapd, resp)
663 resp['payload'] = anqp_comeback_resp(dialog_token, id=1) + struct.pack('<H', 0)
664 send_gas_resp(hapd, resp)
665 expect_gas_result(dev[0], "SUCCESS")
667 logger.debug("Unexpected initial response when waiting for comeback response")
668 query, dialog_token = init_gas(hapd, bssid, dev[0])
669 resp = action_response(query)
670 resp['payload'] = anqp_initial_resp(dialog_token, 0) + struct.pack('<H', 0)
671 send_gas_resp(hapd, resp)
672 ev = hapd.wait_event(["MGMT-RX"], timeout=1)
674 raise Exception("Unexpected management frame")
675 expect_gas_result(dev[0], "TIMEOUT")
677 logger.debug("Too short comeback response")
678 query, dialog_token = init_gas(hapd, bssid, dev[0])
679 resp = action_response(query)
680 resp['payload'] = struct.pack('<BBBH', ACTION_CATEG_PUBLIC,
681 GAS_COMEBACK_RESPONSE, dialog_token, 0)
682 send_gas_resp(hapd, resp)
683 ev = hapd.wait_event(["MGMT-RX"], timeout=1)
685 raise Exception("Unexpected management frame")
686 expect_gas_result(dev[0], "TIMEOUT")
688 logger.debug("Too short comeback response(2)")
689 query, dialog_token = init_gas(hapd, bssid, dev[0])
690 resp = action_response(query)
691 resp['payload'] = struct.pack('<BBBHBB', ACTION_CATEG_PUBLIC,
692 GAS_COMEBACK_RESPONSE, dialog_token, 0, 0x80,
694 send_gas_resp(hapd, resp)
695 ev = hapd.wait_event(["MGMT-RX"], timeout=1)
697 raise Exception("Unexpected management frame")
698 expect_gas_result(dev[0], "TIMEOUT")
700 logger.debug("Maximum comeback response fragment claiming more fragments")
701 query, dialog_token = init_gas(hapd, bssid, dev[0])
702 resp = action_response(query)
703 resp['payload'] = anqp_comeback_resp(dialog_token, more=True) + struct.pack('<H', 0)
704 send_gas_resp(hapd, resp)
705 for i in range(1, 129):
707 gas = parse_gas(query['payload'])
708 if gas['action'] != GAS_COMEBACK_REQUEST:
709 raise Exception("Unexpected request action")
710 if gas['dialog_token'] != dialog_token:
711 raise Exception("Unexpected dialog token change")
712 resp = action_response(query)
713 resp['payload'] = anqp_comeback_resp(dialog_token, id=i, more=True) + struct.pack('<H', 0)
714 send_gas_resp(hapd, resp)
715 expect_gas_result(dev[0], "PEER_ERROR")
717 def test_gas_comeback_resp_additional_delay(dev, apdev):
718 """GAS comeback response requesting additional delay"""
719 hapd = start_ap(apdev[0])
720 bssid = apdev[0]['bssid']
722 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
723 hapd.set("ext_mgmt_frame_handling", "1")
725 query, dialog_token = init_gas(hapd, bssid, dev[0])
726 for i in range(0, 2):
727 resp = action_response(query)
728 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=95, comeback_delay=50) + struct.pack('<H', 0)
729 send_gas_resp(hapd, resp)
731 gas = parse_gas(query['payload'])
732 if gas['action'] != GAS_COMEBACK_REQUEST:
733 raise Exception("Unexpected request action")
734 if gas['dialog_token'] != dialog_token:
735 raise Exception("Unexpected dialog token change")
736 resp = action_response(query)
737 resp['payload'] = anqp_comeback_resp(dialog_token, status_code=0) + struct.pack('<H', 0)
738 send_gas_resp(hapd, resp)
739 expect_gas_result(dev[0], "SUCCESS")
741 def test_gas_unknown_adv_proto(dev, apdev):
742 """Unknown advertisement protocol id"""
743 bssid = apdev[0]['bssid']
744 params = hs20_ap_params()
745 params['hessid'] = bssid
746 hostapd.add_ap(apdev[0]['ifname'], params)
748 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
749 req = dev[0].request("GAS_REQUEST " + bssid + " 42 000102000101")
751 raise Exception("GAS query request rejected")
752 expect_gas_result(dev[0], "FAILURE", "59")
753 ev = dev[0].wait_event(["GAS-RESPONSE-INFO"], timeout=10)
755 raise Exception("GAS query timed out")
756 exp = r'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
757 res = re.split(exp, ev)
759 raise Exception("Could not parse GAS-RESPONSE-INFO")
761 raise Exception("Unexpected BSSID in response")
764 raise Exception("Unexpected GAS-RESPONSE-INFO status")
766 def test_gas_max_pending(dev, apdev):
767 """GAS and maximum pending query limit"""
768 hapd = start_ap(apdev[0])
769 hapd.set("gas_frag_limit", "50")
770 bssid = apdev[0]['bssid']
772 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
773 wpas.interface_add("wlan5")
774 if "OK" not in wpas.request("P2P_SET listen_channel 1"):
775 raise Exception("Failed to set listen channel")
776 if "OK" not in wpas.p2p_listen():
777 raise Exception("Failed to start listen state")
778 if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
779 raise Exception("Failed to enable external management frame handling")
781 anqp_query = struct.pack('<HHHHHHHHHH', 256, 16, 257, 258, 260, 261, 262, 263, 264, 268)
782 gas = struct.pack('<H', len(anqp_query)) + anqp_query
784 for dialog_token in range(1, 10):
785 msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_INITIAL_REQUEST,
786 dialog_token) + anqp_adv_proto() + gas
787 req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
788 if "OK" not in wpas.request(req):
789 raise Exception("Could not send management frame")
790 resp = wpas.mgmt_rx()
792 raise Exception("MGMT-RX timeout")
793 if 'payload' not in resp:
794 raise Exception("Missing payload")
795 gresp = parse_gas(resp['payload'])
796 if gresp['dialog_token'] != dialog_token:
797 raise Exception("Dialog token mismatch")
798 status_code = gresp['status_code']
799 if dialog_token < 9 and status_code != 0:
800 raise Exception("Unexpected failure status code {} for dialog token {}".format(status_code, dialog_token))
801 if dialog_token > 8 and status_code == 0:
802 raise Exception("Unexpected success status code {} for dialog token {}".format(status_code, dialog_token))
804 def test_gas_no_pending(dev, apdev):
805 """GAS and no pending query for comeback request"""
806 hapd = start_ap(apdev[0])
807 bssid = apdev[0]['bssid']
809 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
810 wpas.interface_add("wlan5")
811 if "OK" not in wpas.request("P2P_SET listen_channel 1"):
812 raise Exception("Failed to set listen channel")
813 if "OK" not in wpas.p2p_listen():
814 raise Exception("Failed to start listen state")
815 if "FAIL" in wpas.request("SET ext_mgmt_frame_handling 1"):
816 raise Exception("Failed to enable external management frame handling")
818 msg = struct.pack('<BBB', ACTION_CATEG_PUBLIC, GAS_COMEBACK_REQUEST, 1)
819 req = "MGMT_TX {} {} freq=2412 wait_time=10 action={}".format(bssid, bssid, binascii.hexlify(msg))
820 if "OK" not in wpas.request(req):
821 raise Exception("Could not send management frame")
822 resp = wpas.mgmt_rx()
824 raise Exception("MGMT-RX timeout")
825 if 'payload' not in resp:
826 raise Exception("Missing payload")
827 gresp = parse_gas(resp['payload'])
828 status_code = gresp['status_code']
829 if status_code != 60:
830 raise Exception("Unexpected status code {} (expected 60)".format(status_code))
832 def test_gas_missing_payload(dev, apdev):
833 """No action code in the query frame"""
834 bssid = apdev[0]['bssid']
835 params = hs20_ap_params()
836 params['hessid'] = bssid
837 hostapd.add_ap(apdev[0]['ifname'], params)
839 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
841 cmd = "MGMT_TX {} {} freq=2412 action=040A".format(bssid, bssid)
842 if "FAIL" in dev[0].request(cmd):
843 raise Exception("Could not send test Action frame")
844 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
846 raise Exception("Timeout on MGMT-TX-STATUS")
847 if "result=SUCCESS" not in ev:
848 raise Exception("AP did not ack Action frame")
850 cmd = "MGMT_TX {} {} freq=2412 action=04".format(bssid, bssid)
851 if "FAIL" in dev[0].request(cmd):
852 raise Exception("Could not send test Action frame")
853 ev = dev[0].wait_event(["MGMT-TX-STATUS"], timeout=10)
855 raise Exception("Timeout on MGMT-TX-STATUS")
856 if "result=SUCCESS" not in ev:
857 raise Exception("AP did not ack Action frame")
859 def test_gas_query_deinit(dev, apdev):
860 """Pending GAS/ANQP query during deinit"""
861 hapd = start_ap(apdev[0])
862 bssid = apdev[0]['bssid']
864 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
865 wpas.interface_add("wlan5")
867 wpas.scan_for_bss(bssid, freq="2412", force_scan=True)
868 id = wpas.request("RADIO_WORK add block-work")
869 if "OK" not in wpas.request("ANQP_GET " + bssid + " 258"):
870 raise Exception("ANQP_GET command failed")
872 ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
874 raise Exception("Timeout while waiting radio work to start")
875 ev = wpas.wait_event(["GAS-QUERY-START", "EXT-RADIO-WORK-START"], timeout=5)
877 raise Exception("Timeout while waiting radio work to start (2)")
879 # Remove the interface while the gas-query radio work is still pending and
880 # GAS query has not yet been started.
881 wpas.interface_remove("wlan5")
883 def test_gas_anqp_oom_wpas(dev, apdev):
884 """GAS/ANQP query and OOM in wpa_supplicant"""
885 hapd = start_ap(apdev[0])
886 bssid = apdev[0]['bssid']
888 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
890 with alloc_fail(dev[0], 1, "gas_build_req"):
891 if "FAIL" not in dev[0].request("ANQP_GET " + bssid + " 258"):
892 raise Exception("Unexpected ANQP_GET command success (OOM)")
894 def test_gas_anqp_oom_hapd(dev, apdev):
895 """GAS/ANQP query and OOM in hostapd"""
896 hapd = start_ap(apdev[0])
897 bssid = apdev[0]['bssid']
899 dev[0].scan_for_bss(bssid, freq="2412", force_scan=True)
901 with alloc_fail(hapd, 1, "gas_build_resp"):
902 # This query will time out due to the AP not sending a response (OOM).
903 if "OK" not in dev[0].request("ANQP_GET " + bssid + " 258"):
904 raise Exception("ANQP_GET command failed")
906 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
908 raise Exception("GAS query start timed out")
910 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
912 raise Exception("GAS query timed out")
913 if "result=TIMEOUT" not in ev:
914 raise Exception("Unexpected result: " + ev)
916 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
918 raise Exception("ANQP-QUERY-DONE event not seen")
919 if "result=FAILURE" not in ev:
920 raise Exception("Unexpected result: " + ev)
922 with alloc_fail(hapd, 1, "gas_anqp_build_comeback_resp"):
923 hapd.set("gas_frag_limit", "50")
925 # This query will time out due to the AP not sending a response (OOM).
926 print dev[0].request("FETCH_ANQP")
927 ev = dev[0].wait_event(["GAS-QUERY-START"], timeout=5)
929 raise Exception("GAS query start timed out")
931 ev = dev[0].wait_event(["GAS-QUERY-DONE"], timeout=10)
933 raise Exception("GAS query timed out")
934 if "result=TIMEOUT" not in ev:
935 raise Exception("Unexpected result: " + ev)
937 ev = dev[0].wait_event(["ANQP-QUERY-DONE"], timeout=10)
939 raise Exception("ANQP-QUERY-DONE event not seen")
940 if "result=FAILURE" not in ev:
941 raise Exception("Unexpected result: " + ev)