2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011, Qualcomm Atheros
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
19 #include "utils/eloop.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/gas.h"
22 #include "wpa_supplicant_i.h"
24 #include "gas_query.h"
27 #define GAS_QUERY_TIMEOUT 5
30 struct gas_query_pending
{
38 struct wpabuf
*adv_proto
;
40 void (*cb
)(void *ctx
, const u8
*dst
, u8 dialog_token
,
41 enum gas_query_result result
,
42 const struct wpabuf
*adv_proto
,
43 const struct wpabuf
*resp
, u16 status_code
);
48 struct wpa_supplicant
*wpa_s
;
49 struct dl_list pending
; /* struct gas_query_pending */
53 static void gas_query_tx_comeback_timeout(void *eloop_data
, void *user_ctx
);
54 static void gas_query_timeout(void *eloop_data
, void *user_ctx
);
57 struct gas_query
* gas_query_init(struct wpa_supplicant
*wpa_s
)
59 struct gas_query
*gas
;
61 gas
= os_zalloc(sizeof(*gas
));
66 dl_list_init(&gas
->pending
);
72 static void gas_query_done(struct gas_query
*gas
,
73 struct gas_query_pending
*query
,
74 enum gas_query_result result
)
76 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
77 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
78 dl_list_del(&query
->list
);
79 query
->cb(query
->ctx
, query
->addr
, query
->dialog_token
, result
,
80 query
->adv_proto
, query
->resp
, query
->status_code
);
81 wpabuf_free(query
->adv_proto
);
82 wpabuf_free(query
->resp
);
87 void gas_query_deinit(struct gas_query
*gas
)
89 struct gas_query_pending
*query
, *next
;
94 dl_list_for_each_safe(query
, next
, &gas
->pending
,
95 struct gas_query_pending
, list
)
96 gas_query_done(gas
, query
, GAS_QUERY_DELETED_AT_DEINIT
);
102 static struct gas_query_pending
*
103 gas_query_get_pending(struct gas_query
*gas
, const u8
*addr
, u8 dialog_token
)
105 struct gas_query_pending
*q
;
106 dl_list_for_each(q
, &gas
->pending
, struct gas_query_pending
, list
) {
107 if (os_memcmp(q
->addr
, addr
, ETH_ALEN
) == 0 &&
108 q
->dialog_token
== dialog_token
)
115 static int gas_query_append(struct gas_query_pending
*query
, const u8
*data
,
118 if (wpabuf_resize(&query
->resp
, len
) < 0) {
119 wpa_printf(MSG_DEBUG
, "GAS: No memory to store the response");
122 wpabuf_put_data(query
->resp
, data
, len
);
127 static int gas_query_tx(struct gas_query
*gas
, struct gas_query_pending
*query
,
130 wpa_printf(MSG_DEBUG
, "GAS: Send action frame to " MACSTR
" len=%u "
131 "freq=%d", MAC2STR(query
->addr
),
132 (unsigned int) wpabuf_len(req
), query
->freq
);
133 return wpa_drv_send_action(gas
->wpa_s
, query
->freq
, 0, query
->addr
,
134 gas
->wpa_s
->own_addr
, query
->addr
,
135 wpabuf_head(req
), wpabuf_len(req
));
139 static void gas_query_tx_comeback_req(struct gas_query
*gas
,
140 struct gas_query_pending
*query
)
144 req
= gas_build_comeback_req(query
->dialog_token
);
146 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
150 if (gas_query_tx(gas
, query
, req
) < 0) {
151 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Action frame to "
152 MACSTR
, MAC2STR(query
->addr
));
153 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
160 static void gas_query_tx_comeback_timeout(void *eloop_data
, void *user_ctx
)
162 struct gas_query
*gas
= eloop_data
;
163 struct gas_query_pending
*query
= user_ctx
;
165 wpa_printf(MSG_DEBUG
, "GAS: Comeback timeout for request to " MACSTR
,
166 MAC2STR(query
->addr
));
167 gas_query_tx_comeback_req(gas
, query
);
171 static void gas_query_tx_comeback_req_delay(struct gas_query
*gas
,
172 struct gas_query_pending
*query
,
175 unsigned int secs
, usecs
;
177 secs
= (comeback_delay
* 1024) / 1000000;
178 usecs
= comeback_delay
* 1024 - secs
* 1000000;
179 wpa_printf(MSG_DEBUG
, "GAS: Send comeback request to " MACSTR
180 " in %u secs %u usecs", MAC2STR(query
->addr
), secs
, usecs
);
181 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
182 eloop_register_timeout(secs
, usecs
, gas_query_tx_comeback_timeout
,
187 static void gas_query_rx_initial(struct gas_query
*gas
,
188 struct gas_query_pending
*query
,
189 const u8
*adv_proto
, const u8
*resp
,
190 size_t len
, u16 comeback_delay
)
192 wpa_printf(MSG_DEBUG
, "GAS: Received initial response from "
193 MACSTR
" (dialog_token=%u comeback_delay=%u)",
194 MAC2STR(query
->addr
), query
->dialog_token
, comeback_delay
);
196 query
->adv_proto
= wpabuf_alloc_copy(adv_proto
, 2 + adv_proto
[1]);
197 if (query
->adv_proto
== NULL
) {
198 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
202 if (comeback_delay
) {
203 query
->wait_comeback
= 1;
204 gas_query_tx_comeback_req_delay(gas
, query
, comeback_delay
);
208 /* Query was completed without comeback mechanism */
209 if (gas_query_append(query
, resp
, len
) < 0) {
210 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
214 gas_query_done(gas
, query
, GAS_QUERY_SUCCESS
);
218 static void gas_query_rx_comeback(struct gas_query
*gas
,
219 struct gas_query_pending
*query
,
220 const u8
*adv_proto
, const u8
*resp
,
221 size_t len
, u8 frag_id
, u8 more_frags
,
224 wpa_printf(MSG_DEBUG
, "GAS: Received comeback response from "
225 MACSTR
" (dialog_token=%u frag_id=%u more_frags=%u "
226 "comeback_delay=%u)",
227 MAC2STR(query
->addr
), query
->dialog_token
, frag_id
,
228 more_frags
, comeback_delay
);
230 if ((size_t) 2 + adv_proto
[1] != wpabuf_len(query
->adv_proto
) ||
231 os_memcmp(adv_proto
, wpabuf_head(query
->adv_proto
),
232 wpabuf_len(query
->adv_proto
)) != 0) {
233 wpa_printf(MSG_DEBUG
, "GAS: Advertisement Protocol changed "
234 "between initial and comeback response from "
235 MACSTR
, MAC2STR(query
->addr
));
236 gas_query_done(gas
, query
, GAS_QUERY_PEER_ERROR
);
240 if (comeback_delay
) {
242 wpa_printf(MSG_DEBUG
, "GAS: Invalid comeback response "
243 "with non-zero frag_id and comeback_delay "
244 "from " MACSTR
, MAC2STR(query
->addr
));
245 gas_query_done(gas
, query
, GAS_QUERY_PEER_ERROR
);
248 gas_query_tx_comeback_req_delay(gas
, query
, comeback_delay
);
252 if (frag_id
!= query
->next_frag_id
) {
253 wpa_printf(MSG_DEBUG
, "GAS: Unexpected frag_id in response "
254 "from " MACSTR
, MAC2STR(query
->addr
));
255 gas_query_done(gas
, query
, GAS_QUERY_PEER_ERROR
);
258 query
->next_frag_id
++;
260 if (gas_query_append(query
, resp
, len
) < 0) {
261 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
266 gas_query_tx_comeback_req(gas
, query
);
270 gas_query_done(gas
, query
, GAS_QUERY_SUCCESS
);
274 int gas_query_rx(struct gas_query
*gas
, const u8
*da
, const u8
*sa
,
275 const u8
*bssid
, const u8
*data
, size_t len
, int freq
)
277 struct gas_query_pending
*query
;
278 u8 action
, dialog_token
, frag_id
= 0, more_frags
= 0;
279 u16 comeback_delay
, resp_len
;
280 const u8
*pos
, *adv_proto
;
282 if (gas
== NULL
|| len
< 4)
287 dialog_token
= *pos
++;
289 if (action
!= WLAN_PA_GAS_INITIAL_RESP
&&
290 action
!= WLAN_PA_GAS_COMEBACK_RESP
)
291 return -1; /* Not a GAS response */
293 query
= gas_query_get_pending(gas
, sa
, dialog_token
);
295 wpa_printf(MSG_DEBUG
, "GAS: No pending query found for " MACSTR
296 " dialog token %u", MAC2STR(sa
), dialog_token
);
300 if (query
->wait_comeback
&& action
== WLAN_PA_GAS_INITIAL_RESP
) {
301 wpa_printf(MSG_DEBUG
, "GAS: Unexpected initial response from "
302 MACSTR
" dialog token %u when waiting for comeback "
303 "response", MAC2STR(sa
), dialog_token
);
307 if (!query
->wait_comeback
&& action
== WLAN_PA_GAS_COMEBACK_RESP
) {
308 wpa_printf(MSG_DEBUG
, "GAS: Unexpected comeback response from "
309 MACSTR
" dialog token %u when waiting for initial "
310 "response", MAC2STR(sa
), dialog_token
);
314 query
->status_code
= WPA_GET_LE16(pos
);
317 if (query
->status_code
!= WLAN_STATUS_SUCCESS
) {
318 wpa_printf(MSG_DEBUG
, "GAS: Query to " MACSTR
" dialog token "
319 "%u failed - status code %u",
320 MAC2STR(sa
), dialog_token
, query
->status_code
);
321 gas_query_done(gas
, query
, GAS_QUERY_FAILURE
);
325 if (action
== WLAN_PA_GAS_COMEBACK_RESP
) {
326 if (pos
+ 1 > data
+ len
)
328 frag_id
= *pos
& 0x7f;
329 more_frags
= (*pos
& 0x80) >> 7;
334 if (pos
+ 2 > data
+ len
)
336 comeback_delay
= WPA_GET_LE16(pos
);
339 /* Advertisement Protocol element */
340 if (pos
+ 2 > data
+ len
|| pos
+ 2 + pos
[1] > data
+ len
) {
341 wpa_printf(MSG_DEBUG
, "GAS: No room for Advertisement "
342 "Protocol element in the response from " MACSTR
,
347 if (*pos
!= WLAN_EID_ADV_PROTO
) {
348 wpa_printf(MSG_DEBUG
, "GAS: Unexpected Advertisement "
349 "Protocol element ID %u in response from " MACSTR
,
357 /* Query Response Length */
358 if (pos
+ 2 > data
+ len
) {
359 wpa_printf(MSG_DEBUG
, "GAS: No room for GAS Response Length");
362 resp_len
= WPA_GET_LE16(pos
);
365 if (pos
+ resp_len
> data
+ len
) {
366 wpa_printf(MSG_DEBUG
, "GAS: Truncated Query Response in "
367 "response from " MACSTR
, MAC2STR(sa
));
371 if (pos
+ resp_len
< data
+ len
) {
372 wpa_printf(MSG_DEBUG
, "GAS: Ignore %u octets of extra data "
373 "after Query Response from " MACSTR
,
374 (unsigned int) (data
+ len
- pos
- resp_len
),
378 if (action
== WLAN_PA_GAS_COMEBACK_RESP
)
379 gas_query_rx_comeback(gas
, query
, adv_proto
, pos
, resp_len
,
380 frag_id
, more_frags
, comeback_delay
);
382 gas_query_rx_initial(gas
, query
, adv_proto
, pos
, resp_len
,
389 static void gas_query_timeout(void *eloop_data
, void *user_ctx
)
391 struct gas_query
*gas
= eloop_data
;
392 struct gas_query_pending
*query
= user_ctx
;
394 wpa_printf(MSG_DEBUG
, "GAS: No response received for query to " MACSTR
,
395 MAC2STR(query
->addr
));
396 gas_query_done(gas
, query
, GAS_QUERY_TIMEOUT
);
400 static int gas_query_dialog_token_available(struct gas_query
*gas
,
401 const u8
*dst
, u8 dialog_token
)
403 struct gas_query_pending
*q
;
404 dl_list_for_each(q
, &gas
->pending
, struct gas_query_pending
, list
) {
405 if (os_memcmp(dst
, q
->addr
, ETH_ALEN
) == 0 &&
406 dialog_token
== q
->dialog_token
)
414 int gas_query_req(struct gas_query
*gas
, const u8
*dst
, int freq
,
416 void (*cb
)(void *ctx
, const u8
*dst
, u8 dialog_token
,
417 enum gas_query_result result
,
418 const struct wpabuf
*adv_proto
,
419 const struct wpabuf
*resp
, u16 status_code
),
422 struct gas_query_pending
*query
;
425 if (wpabuf_len(req
) < 3)
428 for (dialog_token
= 0; dialog_token
< 256; dialog_token
++) {
429 if (gas_query_dialog_token_available(gas
, dst
, dialog_token
))
432 if (dialog_token
== 256)
433 return -1; /* Too many pending queries */
435 query
= os_zalloc(sizeof(*query
));
439 os_memcpy(query
->addr
, dst
, ETH_ALEN
);
440 query
->dialog_token
= dialog_token
;
444 dl_list_add(&gas
->pending
, &query
->list
);
446 *(wpabuf_mhead_u8(req
) + 2) = dialog_token
;
448 wpa_printf(MSG_DEBUG
, "GAS: Starting request for " MACSTR
449 " dialog_token %u", MAC2STR(dst
), dialog_token
);
450 if (gas_query_tx(gas
, query
, req
) < 0) {
451 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Action frame to "
452 MACSTR
, MAC2STR(query
->addr
));
457 eloop_register_timeout(GAS_QUERY_TIMEOUT
, 0, gas_query_timeout
,
464 void gas_query_cancel(struct gas_query
*gas
, const u8
*dst
, u8 dialog_token
)
466 struct gas_query_pending
*query
;
468 query
= gas_query_get_pending(gas
, dst
, dialog_token
);
470 gas_query_done(gas
, query
, GAS_QUERY_CANCELLED
);