2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
5 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
14 #include "utils/eloop.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/gas.h"
17 #include "common/wpa_ctrl.h"
18 #include "rsn_supp/wpa.h"
19 #include "wpa_supplicant_i.h"
21 #include "offchannel.h"
22 #include "gas_query.h"
25 /** GAS query timeout in seconds */
26 #define GAS_QUERY_TIMEOUT_PERIOD 2
30 * struct gas_query_pending - Pending GAS query
32 struct gas_query_pending
{
34 struct gas_query
*gas
;
38 unsigned int wait_comeback
:1;
39 unsigned int offchannel_tx_started
:1;
43 struct wpabuf
*adv_proto
;
45 void (*cb
)(void *ctx
, const u8
*dst
, u8 dialog_token
,
46 enum gas_query_result result
,
47 const struct wpabuf
*adv_proto
,
48 const struct wpabuf
*resp
, u16 status_code
);
53 * struct gas_query - Internal GAS query data
56 struct wpa_supplicant
*wpa_s
;
57 struct dl_list pending
; /* struct gas_query_pending */
58 struct gas_query_pending
*current
;
59 struct wpa_radio_work
*work
;
63 static void gas_query_tx_comeback_timeout(void *eloop_data
, void *user_ctx
);
64 static void gas_query_timeout(void *eloop_data
, void *user_ctx
);
68 * gas_query_init - Initialize GAS query component
69 * @wpa_s: Pointer to wpa_supplicant data
70 * Returns: Pointer to GAS query data or %NULL on failure
72 struct gas_query
* gas_query_init(struct wpa_supplicant
*wpa_s
)
74 struct gas_query
*gas
;
76 gas
= os_zalloc(sizeof(*gas
));
81 dl_list_init(&gas
->pending
);
87 static const char * gas_result_txt(enum gas_query_result result
)
90 case GAS_QUERY_SUCCESS
:
92 case GAS_QUERY_FAILURE
:
94 case GAS_QUERY_TIMEOUT
:
96 case GAS_QUERY_PEER_ERROR
:
98 case GAS_QUERY_INTERNAL_ERROR
:
99 return "INTERNAL_ERROR";
100 case GAS_QUERY_CANCELLED
:
102 case GAS_QUERY_DELETED_AT_DEINIT
:
103 return "DELETED_AT_DEINIT";
110 static void gas_query_free(struct gas_query_pending
*query
, int del_list
)
112 struct gas_query
*gas
= query
->gas
;
115 dl_list_del(&query
->list
);
117 if (gas
->work
&& gas
->work
->ctx
== query
) {
118 radio_work_done(gas
->work
);
122 wpabuf_free(query
->req
);
123 wpabuf_free(query
->adv_proto
);
124 wpabuf_free(query
->resp
);
129 static void gas_query_done(struct gas_query
*gas
,
130 struct gas_query_pending
*query
,
131 enum gas_query_result result
)
133 wpa_msg(gas
->wpa_s
, MSG_INFO
, GAS_QUERY_DONE
"addr=" MACSTR
134 " dialog_token=%u freq=%d status_code=%u result=%s",
135 MAC2STR(query
->addr
), query
->dialog_token
, query
->freq
,
136 query
->status_code
, gas_result_txt(result
));
137 if (gas
->current
== query
)
139 if (query
->offchannel_tx_started
)
140 offchannel_send_action_done(gas
->wpa_s
);
141 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
142 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
143 dl_list_del(&query
->list
);
144 query
->cb(query
->ctx
, query
->addr
, query
->dialog_token
, result
,
145 query
->adv_proto
, query
->resp
, query
->status_code
);
146 gas_query_free(query
, 0);
151 * gas_query_deinit - Deinitialize GAS query component
152 * @gas: GAS query data from gas_query_init()
154 void gas_query_deinit(struct gas_query
*gas
)
156 struct gas_query_pending
*query
, *next
;
161 dl_list_for_each_safe(query
, next
, &gas
->pending
,
162 struct gas_query_pending
, list
)
163 gas_query_done(gas
, query
, GAS_QUERY_DELETED_AT_DEINIT
);
169 static struct gas_query_pending
*
170 gas_query_get_pending(struct gas_query
*gas
, const u8
*addr
, u8 dialog_token
)
172 struct gas_query_pending
*q
;
173 dl_list_for_each(q
, &gas
->pending
, struct gas_query_pending
, list
) {
174 if (os_memcmp(q
->addr
, addr
, ETH_ALEN
) == 0 &&
175 q
->dialog_token
== dialog_token
)
182 static int gas_query_append(struct gas_query_pending
*query
, const u8
*data
,
185 if (wpabuf_resize(&query
->resp
, len
) < 0) {
186 wpa_printf(MSG_DEBUG
, "GAS: No memory to store the response");
189 wpabuf_put_data(query
->resp
, data
, len
);
194 static void gas_query_tx_status(struct wpa_supplicant
*wpa_s
,
195 unsigned int freq
, const u8
*dst
,
196 const u8
*src
, const u8
*bssid
,
197 const u8
*data
, size_t data_len
,
198 enum offchannel_send_action_result result
)
200 struct gas_query_pending
*query
;
201 struct gas_query
*gas
= wpa_s
->gas
;
203 if (gas
->current
== NULL
) {
204 wpa_printf(MSG_DEBUG
, "GAS: Unexpected TX status: freq=%u dst="
205 MACSTR
" result=%d - no query in progress",
206 freq
, MAC2STR(dst
), result
);
210 query
= gas
->current
;
212 wpa_printf(MSG_DEBUG
, "GAS: TX status: freq=%u dst=" MACSTR
213 " result=%d query=%p dialog_token=%u",
214 freq
, MAC2STR(dst
), result
, query
, query
->dialog_token
);
215 if (os_memcmp(dst
, query
->addr
, ETH_ALEN
) != 0) {
216 wpa_printf(MSG_DEBUG
, "GAS: TX status for unexpected destination");
220 if (result
== OFFCHANNEL_SEND_ACTION_SUCCESS
) {
221 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
222 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD
, 0,
223 gas_query_timeout
, gas
, query
);
225 if (result
== OFFCHANNEL_SEND_ACTION_FAILED
) {
226 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
227 eloop_register_timeout(0, 0, gas_query_timeout
, gas
, query
);
232 static int pmf_in_use(struct wpa_supplicant
*wpa_s
, const u8
*addr
)
234 if (wpa_s
->current_ssid
== NULL
||
235 wpa_s
->wpa_state
< WPA_4WAY_HANDSHAKE
||
236 os_memcmp(addr
, wpa_s
->bssid
, ETH_ALEN
) != 0)
238 return wpa_sm_pmf_enabled(wpa_s
->wpa
);
242 static int gas_query_tx(struct gas_query
*gas
, struct gas_query_pending
*query
,
245 int res
, prot
= pmf_in_use(gas
->wpa_s
, query
->addr
);
247 wpa_printf(MSG_DEBUG
, "GAS: Send action frame to " MACSTR
" len=%u "
248 "freq=%d prot=%d", MAC2STR(query
->addr
),
249 (unsigned int) wpabuf_len(req
), query
->freq
, prot
);
251 u8
*categ
= wpabuf_mhead_u8(req
);
252 *categ
= WLAN_ACTION_PROTECTED_DUAL
;
254 res
= offchannel_send_action(gas
->wpa_s
, query
->freq
, query
->addr
,
255 gas
->wpa_s
->own_addr
, query
->addr
,
256 wpabuf_head(req
), wpabuf_len(req
), 1000,
257 gas_query_tx_status
, 0);
259 query
->offchannel_tx_started
= 1;
264 static void gas_query_tx_comeback_req(struct gas_query
*gas
,
265 struct gas_query_pending
*query
)
269 req
= gas_build_comeback_req(query
->dialog_token
);
271 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
275 if (gas_query_tx(gas
, query
, req
) < 0) {
276 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Action frame to "
277 MACSTR
, MAC2STR(query
->addr
));
278 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
285 static void gas_query_tx_comeback_timeout(void *eloop_data
, void *user_ctx
)
287 struct gas_query
*gas
= eloop_data
;
288 struct gas_query_pending
*query
= user_ctx
;
290 wpa_printf(MSG_DEBUG
, "GAS: Comeback timeout for request to " MACSTR
,
291 MAC2STR(query
->addr
));
292 gas_query_tx_comeback_req(gas
, query
);
296 static void gas_query_tx_comeback_req_delay(struct gas_query
*gas
,
297 struct gas_query_pending
*query
,
300 unsigned int secs
, usecs
;
302 secs
= (comeback_delay
* 1024) / 1000000;
303 usecs
= comeback_delay
* 1024 - secs
* 1000000;
304 wpa_printf(MSG_DEBUG
, "GAS: Send comeback request to " MACSTR
305 " in %u secs %u usecs", MAC2STR(query
->addr
), secs
, usecs
);
306 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
307 eloop_register_timeout(secs
, usecs
, gas_query_tx_comeback_timeout
,
312 static void gas_query_rx_initial(struct gas_query
*gas
,
313 struct gas_query_pending
*query
,
314 const u8
*adv_proto
, const u8
*resp
,
315 size_t len
, u16 comeback_delay
)
317 wpa_printf(MSG_DEBUG
, "GAS: Received initial response from "
318 MACSTR
" (dialog_token=%u comeback_delay=%u)",
319 MAC2STR(query
->addr
), query
->dialog_token
, comeback_delay
);
321 query
->adv_proto
= wpabuf_alloc_copy(adv_proto
, 2 + adv_proto
[1]);
322 if (query
->adv_proto
== NULL
) {
323 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
327 if (comeback_delay
) {
328 query
->wait_comeback
= 1;
329 gas_query_tx_comeback_req_delay(gas
, query
, comeback_delay
);
333 /* Query was completed without comeback mechanism */
334 if (gas_query_append(query
, resp
, len
) < 0) {
335 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
339 gas_query_done(gas
, query
, GAS_QUERY_SUCCESS
);
343 static void gas_query_rx_comeback(struct gas_query
*gas
,
344 struct gas_query_pending
*query
,
345 const u8
*adv_proto
, const u8
*resp
,
346 size_t len
, u8 frag_id
, u8 more_frags
,
349 wpa_printf(MSG_DEBUG
, "GAS: Received comeback response from "
350 MACSTR
" (dialog_token=%u frag_id=%u more_frags=%u "
351 "comeback_delay=%u)",
352 MAC2STR(query
->addr
), query
->dialog_token
, frag_id
,
353 more_frags
, comeback_delay
);
355 if ((size_t) 2 + adv_proto
[1] != wpabuf_len(query
->adv_proto
) ||
356 os_memcmp(adv_proto
, wpabuf_head(query
->adv_proto
),
357 wpabuf_len(query
->adv_proto
)) != 0) {
358 wpa_printf(MSG_DEBUG
, "GAS: Advertisement Protocol changed "
359 "between initial and comeback response from "
360 MACSTR
, MAC2STR(query
->addr
));
361 gas_query_done(gas
, query
, GAS_QUERY_PEER_ERROR
);
365 if (comeback_delay
) {
367 wpa_printf(MSG_DEBUG
, "GAS: Invalid comeback response "
368 "with non-zero frag_id and comeback_delay "
369 "from " MACSTR
, MAC2STR(query
->addr
));
370 gas_query_done(gas
, query
, GAS_QUERY_PEER_ERROR
);
373 gas_query_tx_comeback_req_delay(gas
, query
, comeback_delay
);
377 if (frag_id
!= query
->next_frag_id
) {
378 wpa_printf(MSG_DEBUG
, "GAS: Unexpected frag_id in response "
379 "from " MACSTR
, MAC2STR(query
->addr
));
380 if (frag_id
+ 1 == query
->next_frag_id
) {
381 wpa_printf(MSG_DEBUG
, "GAS: Drop frame as possible "
382 "retry of previous fragment");
385 gas_query_done(gas
, query
, GAS_QUERY_PEER_ERROR
);
388 query
->next_frag_id
++;
390 if (gas_query_append(query
, resp
, len
) < 0) {
391 gas_query_done(gas
, query
, GAS_QUERY_INTERNAL_ERROR
);
396 gas_query_tx_comeback_req(gas
, query
);
400 gas_query_done(gas
, query
, GAS_QUERY_SUCCESS
);
405 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
406 * @gas: GAS query data from gas_query_init()
407 * @da: Destination MAC address of the Action frame
408 * @sa: Source MAC address of the Action frame
409 * @bssid: BSSID of the Action frame
410 * @categ: Category of the Action frame
411 * @data: Payload of the Action frame
412 * @len: Length of @data
413 * @freq: Frequency (in MHz) on which the frame was received
414 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
416 int gas_query_rx(struct gas_query
*gas
, const u8
*da
, const u8
*sa
,
417 const u8
*bssid
, u8 categ
, const u8
*data
, size_t len
,
420 struct gas_query_pending
*query
;
421 u8 action
, dialog_token
, frag_id
= 0, more_frags
= 0;
422 u16 comeback_delay
, resp_len
;
423 const u8
*pos
, *adv_proto
;
426 if (gas
== NULL
|| len
< 4)
429 prot
= categ
== WLAN_ACTION_PROTECTED_DUAL
;
430 pmf
= pmf_in_use(gas
->wpa_s
, bssid
);
432 wpa_printf(MSG_DEBUG
, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
436 wpa_printf(MSG_DEBUG
, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
442 dialog_token
= *pos
++;
444 if (action
!= WLAN_PA_GAS_INITIAL_RESP
&&
445 action
!= WLAN_PA_GAS_COMEBACK_RESP
)
446 return -1; /* Not a GAS response */
448 query
= gas_query_get_pending(gas
, sa
, dialog_token
);
450 wpa_printf(MSG_DEBUG
, "GAS: No pending query found for " MACSTR
451 " dialog token %u", MAC2STR(sa
), dialog_token
);
455 if (query
->wait_comeback
&& action
== WLAN_PA_GAS_INITIAL_RESP
) {
456 wpa_printf(MSG_DEBUG
, "GAS: Unexpected initial response from "
457 MACSTR
" dialog token %u when waiting for comeback "
458 "response", MAC2STR(sa
), dialog_token
);
462 if (!query
->wait_comeback
&& action
== WLAN_PA_GAS_COMEBACK_RESP
) {
463 wpa_printf(MSG_DEBUG
, "GAS: Unexpected comeback response from "
464 MACSTR
" dialog token %u when waiting for initial "
465 "response", MAC2STR(sa
), dialog_token
);
469 query
->status_code
= WPA_GET_LE16(pos
);
472 if (query
->status_code
!= WLAN_STATUS_SUCCESS
) {
473 wpa_printf(MSG_DEBUG
, "GAS: Query to " MACSTR
" dialog token "
474 "%u failed - status code %u",
475 MAC2STR(sa
), dialog_token
, query
->status_code
);
476 gas_query_done(gas
, query
, GAS_QUERY_FAILURE
);
480 if (action
== WLAN_PA_GAS_COMEBACK_RESP
) {
481 if (pos
+ 1 > data
+ len
)
483 frag_id
= *pos
& 0x7f;
484 more_frags
= (*pos
& 0x80) >> 7;
489 if (pos
+ 2 > data
+ len
)
491 comeback_delay
= WPA_GET_LE16(pos
);
494 /* Advertisement Protocol element */
495 if (pos
+ 2 > data
+ len
|| pos
+ 2 + pos
[1] > data
+ len
) {
496 wpa_printf(MSG_DEBUG
, "GAS: No room for Advertisement "
497 "Protocol element in the response from " MACSTR
,
502 if (*pos
!= WLAN_EID_ADV_PROTO
) {
503 wpa_printf(MSG_DEBUG
, "GAS: Unexpected Advertisement "
504 "Protocol element ID %u in response from " MACSTR
,
512 /* Query Response Length */
513 if (pos
+ 2 > data
+ len
) {
514 wpa_printf(MSG_DEBUG
, "GAS: No room for GAS Response Length");
517 resp_len
= WPA_GET_LE16(pos
);
520 if (pos
+ resp_len
> data
+ len
) {
521 wpa_printf(MSG_DEBUG
, "GAS: Truncated Query Response in "
522 "response from " MACSTR
, MAC2STR(sa
));
526 if (pos
+ resp_len
< data
+ len
) {
527 wpa_printf(MSG_DEBUG
, "GAS: Ignore %u octets of extra data "
528 "after Query Response from " MACSTR
,
529 (unsigned int) (data
+ len
- pos
- resp_len
),
533 if (action
== WLAN_PA_GAS_COMEBACK_RESP
)
534 gas_query_rx_comeback(gas
, query
, adv_proto
, pos
, resp_len
,
535 frag_id
, more_frags
, comeback_delay
);
537 gas_query_rx_initial(gas
, query
, adv_proto
, pos
, resp_len
,
544 static void gas_query_timeout(void *eloop_data
, void *user_ctx
)
546 struct gas_query
*gas
= eloop_data
;
547 struct gas_query_pending
*query
= user_ctx
;
549 wpa_printf(MSG_DEBUG
, "GAS: No response received for query to " MACSTR
551 MAC2STR(query
->addr
), query
->dialog_token
);
552 gas_query_done(gas
, query
, GAS_QUERY_TIMEOUT
);
556 static int gas_query_dialog_token_available(struct gas_query
*gas
,
557 const u8
*dst
, u8 dialog_token
)
559 struct gas_query_pending
*q
;
560 dl_list_for_each(q
, &gas
->pending
, struct gas_query_pending
, list
) {
561 if (os_memcmp(dst
, q
->addr
, ETH_ALEN
) == 0 &&
562 dialog_token
== q
->dialog_token
)
570 static void gas_query_start_cb(struct wpa_radio_work
*work
, int deinit
)
572 struct gas_query_pending
*query
= work
->ctx
;
573 struct gas_query
*gas
= query
->gas
;
578 gas_query_done(gas
, query
, GAS_QUERY_DELETED_AT_DEINIT
);
582 gas_query_free(query
, 1);
588 if (gas_query_tx(gas
, query
, query
->req
) < 0) {
589 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Action frame to "
590 MACSTR
, MAC2STR(query
->addr
));
591 gas_query_free(query
, 1);
594 gas
->current
= query
;
596 wpa_printf(MSG_DEBUG
, "GAS: Starting query timeout for dialog token %u",
597 query
->dialog_token
);
598 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD
, 0,
599 gas_query_timeout
, gas
, query
);
605 * gas_query_req - Request a GAS query
606 * @gas: GAS query data from gas_query_init()
607 * @dst: Destination MAC address for the query
608 * @freq: Frequency (in MHz) for the channel on which to send the query
609 * @req: GAS query payload (to be freed by gas_query module in case of success
611 * @cb: Callback function for reporting GAS query result and response
612 * @ctx: Context pointer to use with the @cb call
613 * Returns: dialog token (>= 0) on success or -1 on failure
615 int gas_query_req(struct gas_query
*gas
, const u8
*dst
, int freq
,
617 void (*cb
)(void *ctx
, const u8
*dst
, u8 dialog_token
,
618 enum gas_query_result result
,
619 const struct wpabuf
*adv_proto
,
620 const struct wpabuf
*resp
, u16 status_code
),
623 struct gas_query_pending
*query
;
625 static int next_start
= 0;
627 if (wpabuf_len(req
) < 3)
630 for (dialog_token
= 0; dialog_token
< 256; dialog_token
++) {
631 if (gas_query_dialog_token_available(
632 gas
, dst
, (next_start
+ dialog_token
) % 256))
635 if (dialog_token
== 256)
636 return -1; /* Too many pending queries */
637 dialog_token
= (next_start
+ dialog_token
) % 256;
638 next_start
= (dialog_token
+ 1) % 256;
640 query
= os_zalloc(sizeof(*query
));
645 os_memcpy(query
->addr
, dst
, ETH_ALEN
);
646 query
->dialog_token
= dialog_token
;
651 dl_list_add(&gas
->pending
, &query
->list
);
653 *(wpabuf_mhead_u8(req
) + 2) = dialog_token
;
655 wpa_msg(gas
->wpa_s
, MSG_INFO
, GAS_QUERY_START
"addr=" MACSTR
656 " dialog_token=%u freq=%d",
657 MAC2STR(query
->addr
), query
->dialog_token
, query
->freq
);
659 if (radio_add_work(gas
->wpa_s
, freq
, "gas-query", 0, gas_query_start_cb
,
661 gas_query_free(query
, 1);
670 * gas_query_cancel - Cancel a pending GAS query
671 * @gas: GAS query data from gas_query_init()
672 * @dst: Destination MAC address for the query
673 * @dialog_token: Dialog token from gas_query_req()
675 void gas_query_cancel(struct gas_query
*gas
, const u8
*dst
, u8 dialog_token
)
677 struct gas_query_pending
*query
;
679 query
= gas_query_get_pending(gas
, dst
, dialog_token
);
681 gas_query_done(gas
, query
, GAS_QUERY_CANCELLED
);