]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_gas.py
2 # Copyright (c) 2013, Qualcomm Atheros, Inc.
3 # Copyright (c) 2013-2014, 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()
18 params
= hostapd
.wpa2_params(ssid
="test-gas")
19 params
['wpa_key_mgmt'] = "WPA-EAP"
20 params
['ieee80211w'] = "1"
21 params
['ieee8021x'] = "1"
22 params
['auth_server_addr'] = "127.0.0.1"
23 params
['auth_server_port'] = "1812"
24 params
['auth_server_shared_secret'] = "radius"
25 params
['interworking'] = "1"
26 params
['access_network_type'] = "14"
27 params
['internet'] = "1"
31 params
['venue_group'] = "7"
32 params
['venue_type'] = "1"
33 params
['venue_name'] = [ "eng:Example venue", "fin:Esimerkkipaikka" ]
34 params
['roaming_consortium'] = [ "112233", "1020304050", "010203040506",
36 params
['domain_name'] = "example.com,another.example.com"
37 params
['nai_realm'] = [ "0,example.com,13[5:6],21[2:4][5:7]",
38 "0,another.example.com" ]
39 params
['anqp_3gpp_cell_net'] = "244,91"
40 params
['network_auth_type'] = "02http://www.example.com/redirect/me/here/"
41 params
['ipaddr_type_availability'] = "14"
43 params
['hs20_oper_friendly_name'] = [ "eng:Example operator", "fin:Esimerkkioperaattori" ]
44 params
['hs20_wan_metrics'] = "01:8000:1000:80:240:3000"
45 params
['hs20_conn_capab'] = [ "1:0:2", "6:22:1", "17:5060:0" ]
46 params
['hs20_operating_class'] = "5173"
50 params
= hs20_ap_params()
51 params
['hessid'] = ap
['bssid']
52 hostapd
.add_ap(ap
['ifname'], params
)
53 return hostapd
.Hostapd(ap
['ifname'])
55 def get_gas_response(dev
, bssid
, info
, allow_fetch_failure
=False):
56 exp
= r
'<.>(GAS-RESPONSE-INFO) addr=([0-9a-f:]*) dialog_token=([0-9]*) status_code=([0-9]*) resp_len=([\-0-9]*)'
57 res
= re
.split(exp
, info
)
59 raise Exception("Could not parse GAS-RESPONSE-INFO")
61 raise Exception("Unexpected BSSID in response")
65 raise Exception("GAS query failed")
68 raise Exception("GAS query reported invalid response length")
69 if int(resp_len
) > 2000:
70 raise Exception("Unexpected long GAS response")
72 resp
= dev
.request("GAS_RESPONSE_GET " + bssid
+ " " + token
)
74 if allow_fetch_failure
:
75 logger
.debug("GAS response was not available anymore")
77 raise Exception("Could not fetch GAS response")
78 if len(resp
) != int(resp_len
) * 2:
79 raise Exception("Unexpected GAS response length")
80 logger
.debug("GAS response: " + resp
)
82 def test_gas_generic(dev
, apdev
):
83 """Generic GAS query"""
84 bssid
= apdev
[0]['bssid']
85 params
= hs20_ap_params()
86 params
['hessid'] = bssid
87 hostapd
.add_ap(apdev
[0]['ifname'], params
)
89 dev
[0].scan(freq
="2412")
90 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000101")
92 raise Exception("GAS query request rejected")
93 ev
= dev
[0].wait_event(["GAS-RESPONSE-INFO"], timeout
=10)
95 raise Exception("GAS query timed out")
96 get_gas_response(dev
[0], bssid
, ev
)
98 def test_gas_concurrent_scan(dev
, apdev
):
99 """Generic GAS queries with concurrent scan operation"""
100 bssid
= apdev
[0]['bssid']
101 params
= hs20_ap_params()
102 params
['hessid'] = bssid
103 hostapd
.add_ap(apdev
[0]['ifname'], params
)
105 # get BSS entry available to allow GAS query
106 dev
[0].scan(freq
="2412")
108 logger
.info("Request concurrent operations")
109 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000101")
111 raise Exception("GAS query request rejected")
112 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000801")
114 raise Exception("GAS query request rejected")
115 dev
[0].scan(no_wait
=True)
116 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000201")
118 raise Exception("GAS query request rejected")
119 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000501")
121 raise Exception("GAS query request rejected")
124 for i
in range(0, 5):
125 ev
= dev
[0].wait_event(["GAS-RESPONSE-INFO", "CTRL-EVENT-SCAN-RESULTS"],
128 raise Exception("Operation timed out")
129 if "GAS-RESPONSE-INFO" in ev
:
130 responses
= responses
+ 1
131 get_gas_response(dev
[0], bssid
, ev
, allow_fetch_failure
=True)
134 raise Exception("Unexpected number of GAS responses")
136 def test_gas_concurrent_connect(dev
, apdev
):
137 """Generic GAS queries with concurrent connection operation"""
138 bssid
= apdev
[0]['bssid']
139 params
= hs20_ap_params()
140 params
['hessid'] = bssid
141 hostapd
.add_ap(apdev
[0]['ifname'], params
)
143 dev
[0].scan(freq
="2412")
145 logger
.debug("Start concurrent connect and GAS request")
146 dev
[0].connect("test-gas", key_mgmt
="WPA-EAP", eap
="TTLS",
147 identity
="DOMAIN\mschapv2 user", anonymous_identity
="ttls",
148 password
="password", phase2
="auth=MSCHAPV2",
149 ca_cert
="auth_serv/ca.pem", wait_connect
=False,
151 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000101")
153 raise Exception("GAS query request rejected")
155 ev
= dev
[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
158 raise Exception("Operation timed out")
159 if "CTRL-EVENT-CONNECTED" not in ev
:
160 raise Exception("Unexpected operation order")
162 ev
= dev
[0].wait_event(["CTRL-EVENT-CONNECTED", "GAS-RESPONSE-INFO"],
165 raise Exception("Operation timed out")
166 if "GAS-RESPONSE-INFO" not in ev
:
167 raise Exception("Unexpected operation order")
168 get_gas_response(dev
[0], bssid
, ev
)
170 dev
[0].request("DISCONNECT")
171 ev
= dev
[0].wait_event(["CTRL-EVENT-DISCONNECTED"], timeout
=5)
173 raise Exception("Disconnection timed out")
175 logger
.debug("Wait six seconds for expiration of connect-without-scan")
177 dev
[0].dump_monitor()
179 logger
.debug("Start concurrent GAS request and connect")
180 req
= dev
[0].request("GAS_REQUEST " + bssid
+ " 00 000102000101")
182 raise Exception("GAS query request rejected")
183 dev
[0].request("RECONNECT")
185 ev
= dev
[0].wait_event(["GAS-RESPONSE-INFO"], timeout
=10)
187 raise Exception("Operation timed out")
188 get_gas_response(dev
[0], bssid
, ev
)
190 ev
= dev
[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], timeout
=20)
192 raise Exception("No new scan results reported")
194 ev
= dev
[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout
=20)
196 raise Exception("Operation timed out")
197 if "CTRL-EVENT-CONNECTED" not in ev
:
198 raise Exception("Unexpected operation order")
200 def test_gas_fragment(dev
, apdev
):
201 """GAS fragmentation"""
202 hapd
= start_ap(apdev
[0])
203 hapd
.set("gas_frag_limit", "50")
205 dev
[0].scan(freq
="2412")
206 dev
[0].request("FETCH_ANQP")
207 for i
in range(0, 13):
208 ev
= dev
[0].wait_event(["RX-ANQP", "RX-HS20-ANQP"], timeout
=5)
210 raise Exception("Operation timed out")
212 def test_gas_comeback_delay(dev
, apdev
):
213 """GAS fragmentation"""
214 hapd
= start_ap(apdev
[0])
215 hapd
.set("gas_comeback_delay", "500")
217 dev
[0].scan(freq
="2412")
218 dev
[0].request("FETCH_ANQP")
219 for i
in range(0, 6):
220 ev
= dev
[0].wait_event(["RX-ANQP"], timeout
=5)
222 raise Exception("Operation timed out")
224 def expect_gas_result(dev
, result
):
225 ev
= dev
.wait_event(["GAS-QUERY-DONE"], timeout
=10)
227 raise Exception("GAS query timed out")
228 if "result=" + result
not in ev
:
229 raise Exception("Unexpected GAS query result")
231 def anqp_get(dev
, bssid
, id):
232 dev
.request("ANQP_GET " + bssid
+ " " + str(id))
233 ev
= dev
.wait_event(["GAS-QUERY-START"], timeout
=5)
235 raise Exception("GAS query start timed out")
237 def test_gas_timeout(dev
, apdev
):
239 hapd
= start_ap(apdev
[0])
240 bssid
= apdev
[0]['bssid']
242 dev
[0].scan(freq
="2412")
243 hapd
.set("ext_mgmt_frame_handling", "1")
245 anqp_get(dev
[0], bssid
, 263)
247 ev
= hapd
.wait_event(["MGMT-RX"], timeout
=5)
249 raise Exception("MGMT RX wait timed out")
251 expect_gas_result(dev
[0], "TIMEOUT")
253 MGMT_SUBTYPE_ACTION
= 13
254 ACTION_CATEG_PUBLIC
= 4
256 GAS_INITIAL_REQUEST
= 10
257 GAS_INITIAL_RESPONSE
= 11
258 GAS_COMEBACK_REQUEST
= 12
259 GAS_COMEBACK_RESPONSE
= 13
260 GAS_ACTIONS
= [ GAS_INITIAL_REQUEST
, GAS_INITIAL_RESPONSE
,
261 GAS_COMEBACK_REQUEST
, GAS_COMEBACK_RESPONSE
]
263 def anqp_adv_proto():
264 return struct
.pack('BBBB', 108, 2, 127, 0)
266 def anqp_initial_resp(dialog_token
, status_code
):
267 return struct
.pack('<BBBHH', ACTION_CATEG_PUBLIC
, GAS_INITIAL_RESPONSE
,
268 dialog_token
, status_code
, 0) + anqp_adv_proto()
270 def anqp_comeback_resp(dialog_token
):
271 return struct
.pack('<BBBHBH', ACTION_CATEG_PUBLIC
, GAS_COMEBACK_RESPONSE
,
272 dialog_token
, 0, 0, 0) + anqp_adv_proto()
278 query
= hapd
.mgmt_rx()
280 raise Exception("Action frame not received")
281 if query
['subtype'] != MGMT_SUBTYPE_ACTION
:
283 payload
= query
['payload']
286 (category
, action
) = struct
.unpack('BB', payload
[0:2])
287 if category
!= ACTION_CATEG_PUBLIC
or action
not in GAS_ACTIONS
:
290 raise Exception("No Action frame received")
292 def parse_gas(payload
):
294 (category
, action
, dialog_token
) = struct
.unpack('BBB', pos
[0:3])
295 if category
!= ACTION_CATEG_PUBLIC
:
297 if action
not in GAS_ACTIONS
:
300 gas
['action'] = action
306 gas
['dialog_token'] = dialog_token
309 def action_response(req
):
311 resp
['fc'] = req
['fc']
312 resp
['da'] = req
['sa']
313 resp
['sa'] = req
['da']
314 resp
['bssid'] = req
['bssid']
317 def test_gas_invalid_response_type(dev
, apdev
):
318 """GAS invalid response type"""
319 hapd
= start_ap(apdev
[0])
320 bssid
= apdev
[0]['bssid']
322 dev
[0].scan(freq
="2412")
323 hapd
.set("ext_mgmt_frame_handling", "1")
325 anqp_get(dev
[0], bssid
, 263)
328 gas
= parse_gas(query
['payload'])
330 resp
= action_response(query
)
331 # GAS Comeback Response instead of GAS Initial Response
332 resp
['payload'] = anqp_comeback_resp(gas
['dialog_token']) + struct
.pack('<H', 0)
334 ev
= hapd
.wait_event(["MGMT-TX-STATUS"], timeout
=5)
336 raise Exception("Missing TX status for GAS response")
338 raise Exception("GAS response not acknowledged")
340 # station drops the invalid frame, so this needs to result in GAS timeout
341 expect_gas_result(dev
[0], "TIMEOUT")
343 def test_gas_failure_status_code(dev
, apdev
):
344 """GAS failure status code"""
345 hapd
= start_ap(apdev
[0])
346 bssid
= apdev
[0]['bssid']
348 dev
[0].scan(freq
="2412")
349 hapd
.set("ext_mgmt_frame_handling", "1")
351 anqp_get(dev
[0], bssid
, 263)
354 gas
= parse_gas(query
['payload'])
356 resp
= action_response(query
)
357 resp
['payload'] = anqp_initial_resp(gas
['dialog_token'], 61) + struct
.pack('<H', 0)
359 ev
= hapd
.wait_event(["MGMT-TX-STATUS"], timeout
=5)
361 raise Exception("Missing TX status for GAS response")
363 raise Exception("GAS response not acknowledged")
365 expect_gas_result(dev
[0], "FAILURE")
367 def test_gas_malformed(dev
, apdev
):
368 """GAS malformed response frames"""
369 hapd
= start_ap(apdev
[0])
370 bssid
= apdev
[0]['bssid']
372 dev
[0].scan(freq
="2412")
373 hapd
.set("ext_mgmt_frame_handling", "1")
375 anqp_get(dev
[0], bssid
, 263)
378 gas
= parse_gas(query
['payload'])
380 resp
= action_response(query
)
382 resp
['payload'] = struct
.pack('<BBBH', ACTION_CATEG_PUBLIC
,
383 GAS_COMEBACK_RESPONSE
,
384 gas
['dialog_token'], 0)
387 resp
['payload'] = struct
.pack('<BBBHB', ACTION_CATEG_PUBLIC
,
388 GAS_COMEBACK_RESPONSE
,
389 gas
['dialog_token'], 0, 0)
392 hdr
= struct
.pack('<BBBHH', ACTION_CATEG_PUBLIC
, GAS_INITIAL_RESPONSE
,
393 gas
['dialog_token'], 0, 0)
394 resp
['payload'] = hdr
+ struct
.pack('B', 108)
396 resp
['payload'] = hdr
+ struct
.pack('BB', 108, 0)
398 resp
['payload'] = hdr
+ struct
.pack('BB', 108, 1)
400 resp
['payload'] = hdr
+ struct
.pack('BB', 108, 255)
402 resp
['payload'] = hdr
+ struct
.pack('BBB', 108, 1, 127)
404 resp
['payload'] = hdr
+ struct
.pack('BBB', 108, 2, 127)
406 resp
['payload'] = hdr
+ struct
.pack('BBBB', 0, 2, 127, 0)
409 resp
['payload'] = anqp_initial_resp(gas
['dialog_token'], 0) + struct
.pack('<H', 1)
412 resp
['payload'] = anqp_initial_resp(gas
['dialog_token'], 0) + struct
.pack('<HB', 2, 0)
415 resp
['payload'] = anqp_initial_resp(gas
['dialog_token'], 0) + struct
.pack('<H', 65535)
418 resp
['payload'] = anqp_initial_resp(gas
['dialog_token'], 0) + struct
.pack('<HBB', 1, 0, 0)
421 # Station drops invalid frames, but the last of the responses is valid from
422 # GAS view point even though it has an extra octet in the end and the ANQP
423 # part of the response is not valid. This is reported as successfulyl
424 # completed GAS exchange.
425 expect_gas_result(dev
[0], "SUCCESS")