2 * Generic advertisement service (GAS) query (hostapd)
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011-2017, 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 "utils/list.h"
16 #include "common/ieee802_11_defs.h"
17 #include "common/gas.h"
18 #include "common/wpa_ctrl.h"
21 #include "ap_drv_ops.h"
22 #include "gas_query_ap.h"
25 /** GAS query timeout in seconds */
26 #define GAS_QUERY_TIMEOUT_PERIOD 2
28 /* GAS query wait-time / duration in ms */
29 #define GAS_QUERY_WAIT_TIME_INITIAL 1000
30 #define GAS_QUERY_WAIT_TIME_COMEBACK 150
33 * struct gas_query_pending - Pending GAS query
35 struct gas_query_pending
{
37 struct gas_query_ap
*gas
;
41 unsigned int wait_comeback
:1;
42 unsigned int offchannel_tx_started
:1;
47 struct wpabuf
*adv_proto
;
49 struct os_reltime last_oper
;
50 void (*cb
)(void *ctx
, const u8
*dst
, u8 dialog_token
,
51 enum gas_query_ap_result result
,
52 const struct wpabuf
*adv_proto
,
53 const struct wpabuf
*resp
, u16 status_code
);
59 * struct gas_query_ap - Internal GAS query data
62 struct hostapd_data
*hapd
;
64 struct dl_list pending
; /* struct gas_query_pending */
65 struct gas_query_pending
*current
;
69 static void gas_query_tx_comeback_timeout(void *eloop_data
, void *user_ctx
);
70 static void gas_query_timeout(void *eloop_data
, void *user_ctx
);
71 static void gas_query_rx_comeback_timeout(void *eloop_data
, void *user_ctx
);
72 static void gas_query_tx_initial_req(struct gas_query_ap
*gas
,
73 struct gas_query_pending
*query
);
74 static int gas_query_new_dialog_token(struct gas_query_ap
*gas
, const u8
*dst
);
77 static int ms_from_time(struct os_reltime
*last
)
79 struct os_reltime now
, res
;
82 os_reltime_sub(&now
, last
, &res
);
83 return res
.sec
* 1000 + res
.usec
/ 1000;
88 * gas_query_ap_init - Initialize GAS query component
89 * @hapd: Pointer to hostapd data
90 * Returns: Pointer to GAS query data or %NULL on failure
92 struct gas_query_ap
* gas_query_ap_init(struct hostapd_data
*hapd
,
95 struct gas_query_ap
*gas
;
97 gas
= os_zalloc(sizeof(*gas
));
102 gas
->msg_ctx
= msg_ctx
;
103 dl_list_init(&gas
->pending
);
109 static const char * gas_result_txt(enum gas_query_ap_result result
)
112 case GAS_QUERY_AP_SUCCESS
:
114 case GAS_QUERY_AP_FAILURE
:
116 case GAS_QUERY_AP_TIMEOUT
:
118 case GAS_QUERY_AP_PEER_ERROR
:
120 case GAS_QUERY_AP_INTERNAL_ERROR
:
121 return "INTERNAL_ERROR";
122 case GAS_QUERY_AP_DELETED_AT_DEINIT
:
123 return "DELETED_AT_DEINIT";
130 static void gas_query_free(struct gas_query_pending
*query
, int del_list
)
133 dl_list_del(&query
->list
);
135 wpabuf_free(query
->req
);
136 wpabuf_free(query
->adv_proto
);
137 wpabuf_free(query
->resp
);
142 static void gas_query_done(struct gas_query_ap
*gas
,
143 struct gas_query_pending
*query
,
144 enum gas_query_ap_result result
)
146 wpa_msg(gas
->msg_ctx
, MSG_INFO
, GAS_QUERY_DONE
"addr=" MACSTR
147 " dialog_token=%u freq=%d status_code=%u result=%s",
148 MAC2STR(query
->addr
), query
->dialog_token
, query
->freq
,
149 query
->status_code
, gas_result_txt(result
));
150 if (gas
->current
== query
)
152 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
153 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
154 eloop_cancel_timeout(gas_query_rx_comeback_timeout
, gas
, query
);
155 dl_list_del(&query
->list
);
156 query
->cb(query
->ctx
, query
->addr
, query
->dialog_token
, result
,
157 query
->adv_proto
, query
->resp
, query
->status_code
);
158 gas_query_free(query
, 0);
163 * gas_query_ap_deinit - Deinitialize GAS query component
164 * @gas: GAS query data from gas_query_init()
166 void gas_query_ap_deinit(struct gas_query_ap
*gas
)
168 struct gas_query_pending
*query
, *next
;
173 dl_list_for_each_safe(query
, next
, &gas
->pending
,
174 struct gas_query_pending
, list
)
175 gas_query_done(gas
, query
, GAS_QUERY_AP_DELETED_AT_DEINIT
);
181 static struct gas_query_pending
*
182 gas_query_get_pending(struct gas_query_ap
*gas
, const u8
*addr
, u8 dialog_token
)
184 struct gas_query_pending
*q
;
185 dl_list_for_each(q
, &gas
->pending
, struct gas_query_pending
, list
) {
186 if (os_memcmp(q
->addr
, addr
, ETH_ALEN
) == 0 &&
187 q
->dialog_token
== dialog_token
)
194 static int gas_query_append(struct gas_query_pending
*query
, const u8
*data
,
197 if (wpabuf_resize(&query
->resp
, len
) < 0) {
198 wpa_printf(MSG_DEBUG
, "GAS: No memory to store the response");
201 wpabuf_put_data(query
->resp
, data
, len
);
206 void gas_query_ap_tx_status(struct gas_query_ap
*gas
, const u8
*dst
,
207 const u8
*data
, size_t data_len
, int ok
)
209 struct gas_query_pending
*query
;
212 if (!gas
|| !gas
->current
) {
213 wpa_printf(MSG_DEBUG
, "GAS: Unexpected TX status: dst=" MACSTR
214 " ok=%d - no query in progress", MAC2STR(dst
), ok
);
218 query
= gas
->current
;
220 dur
= ms_from_time(&query
->last_oper
);
221 wpa_printf(MSG_DEBUG
, "GAS: TX status: dst=" MACSTR
222 " ok=%d query=%p dialog_token=%u dur=%d ms",
223 MAC2STR(dst
), ok
, query
, query
->dialog_token
, dur
);
224 if (os_memcmp(dst
, query
->addr
, ETH_ALEN
) != 0) {
225 wpa_printf(MSG_DEBUG
, "GAS: TX status for unexpected destination");
228 os_get_reltime(&query
->last_oper
);
230 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
232 wpa_printf(MSG_DEBUG
, "GAS: No ACK to GAS request");
233 eloop_register_timeout(0, 250000, gas_query_timeout
,
236 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD
, 0,
237 gas_query_timeout
, gas
, query
);
239 if (query
->wait_comeback
&& !query
->retry
) {
240 eloop_cancel_timeout(gas_query_rx_comeback_timeout
,
242 eloop_register_timeout(
243 0, (GAS_QUERY_WAIT_TIME_COMEBACK
+ 10) * 1000,
244 gas_query_rx_comeback_timeout
, gas
, query
);
249 static int pmf_in_use(struct hostapd_data
*hapd
, const u8
*addr
)
251 struct sta_info
*sta
;
253 sta
= ap_get_sta(hapd
, addr
);
254 return sta
&& (sta
->flags
& WLAN_STA_MFP
);
258 static int gas_query_tx(struct gas_query_ap
*gas
,
259 struct gas_query_pending
*query
,
260 struct wpabuf
*req
, unsigned int wait_time
)
262 int res
, prot
= pmf_in_use(gas
->hapd
, query
->addr
);
264 wpa_printf(MSG_DEBUG
, "GAS: Send action frame to " MACSTR
" len=%u "
265 "freq=%d prot=%d using src addr " MACSTR
,
266 MAC2STR(query
->addr
), (unsigned int) wpabuf_len(req
),
267 query
->freq
, prot
, MAC2STR(query
->sa
));
269 u8
*categ
= wpabuf_mhead_u8(req
);
270 *categ
= WLAN_ACTION_PROTECTED_DUAL
;
272 os_get_reltime(&query
->last_oper
);
273 res
= hostapd_drv_send_action(gas
->hapd
, query
->freq
, wait_time
,
274 query
->addr
, wpabuf_head(req
),
280 static void gas_query_tx_comeback_req(struct gas_query_ap
*gas
,
281 struct gas_query_pending
*query
)
284 unsigned int wait_time
;
286 req
= gas_build_comeback_req(query
->dialog_token
);
288 gas_query_done(gas
, query
, GAS_QUERY_AP_INTERNAL_ERROR
);
292 wait_time
= (query
->retry
|| !query
->offchannel_tx_started
) ?
293 GAS_QUERY_WAIT_TIME_INITIAL
: GAS_QUERY_WAIT_TIME_COMEBACK
;
295 if (gas_query_tx(gas
, query
, req
, wait_time
) < 0) {
296 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Action frame to "
297 MACSTR
, MAC2STR(query
->addr
));
298 gas_query_done(gas
, query
, GAS_QUERY_AP_INTERNAL_ERROR
);
305 static void gas_query_rx_comeback_timeout(void *eloop_data
, void *user_ctx
)
307 struct gas_query_ap
*gas
= eloop_data
;
308 struct gas_query_pending
*query
= user_ctx
;
311 wpa_printf(MSG_DEBUG
,
312 "GAS: No response to comeback request received (retry=%u)",
314 if (gas
->current
!= query
|| query
->retry
)
316 dialog_token
= gas_query_new_dialog_token(gas
, query
->addr
);
317 if (dialog_token
< 0)
319 wpa_printf(MSG_DEBUG
,
320 "GAS: Retry GAS query due to comeback response timeout");
322 query
->dialog_token
= dialog_token
;
323 *(wpabuf_mhead_u8(query
->req
) + 2) = dialog_token
;
324 query
->wait_comeback
= 0;
325 query
->next_frag_id
= 0;
326 wpabuf_free(query
->adv_proto
);
327 query
->adv_proto
= NULL
;
328 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
329 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
330 gas_query_tx_initial_req(gas
, query
);
334 static void gas_query_tx_comeback_timeout(void *eloop_data
, void *user_ctx
)
336 struct gas_query_ap
*gas
= eloop_data
;
337 struct gas_query_pending
*query
= user_ctx
;
339 wpa_printf(MSG_DEBUG
, "GAS: Comeback timeout for request to " MACSTR
,
340 MAC2STR(query
->addr
));
341 gas_query_tx_comeback_req(gas
, query
);
345 static void gas_query_tx_comeback_req_delay(struct gas_query_ap
*gas
,
346 struct gas_query_pending
*query
,
349 unsigned int secs
, usecs
;
351 secs
= (comeback_delay
* 1024) / 1000000;
352 usecs
= comeback_delay
* 1024 - secs
* 1000000;
353 wpa_printf(MSG_DEBUG
, "GAS: Send comeback request to " MACSTR
354 " in %u secs %u usecs", MAC2STR(query
->addr
), secs
, usecs
);
355 eloop_cancel_timeout(gas_query_tx_comeback_timeout
, gas
, query
);
356 eloop_register_timeout(secs
, usecs
, gas_query_tx_comeback_timeout
,
361 static void gas_query_rx_initial(struct gas_query_ap
*gas
,
362 struct gas_query_pending
*query
,
363 const u8
*adv_proto
, const u8
*resp
,
364 size_t len
, u16 comeback_delay
)
366 wpa_printf(MSG_DEBUG
, "GAS: Received initial response from "
367 MACSTR
" (dialog_token=%u comeback_delay=%u)",
368 MAC2STR(query
->addr
), query
->dialog_token
, comeback_delay
);
370 query
->adv_proto
= wpabuf_alloc_copy(adv_proto
, 2 + adv_proto
[1]);
371 if (query
->adv_proto
== NULL
) {
372 gas_query_done(gas
, query
, GAS_QUERY_AP_INTERNAL_ERROR
);
376 if (comeback_delay
) {
377 eloop_cancel_timeout(gas_query_timeout
, gas
, query
);
378 query
->wait_comeback
= 1;
379 gas_query_tx_comeback_req_delay(gas
, query
, comeback_delay
);
383 /* Query was completed without comeback mechanism */
384 if (gas_query_append(query
, resp
, len
) < 0) {
385 gas_query_done(gas
, query
, GAS_QUERY_AP_INTERNAL_ERROR
);
389 gas_query_done(gas
, query
, GAS_QUERY_AP_SUCCESS
);
393 static void gas_query_rx_comeback(struct gas_query_ap
*gas
,
394 struct gas_query_pending
*query
,
395 const u8
*adv_proto
, const u8
*resp
,
396 size_t len
, u8 frag_id
, u8 more_frags
,
399 wpa_printf(MSG_DEBUG
, "GAS: Received comeback response from "
400 MACSTR
" (dialog_token=%u frag_id=%u more_frags=%u "
401 "comeback_delay=%u)",
402 MAC2STR(query
->addr
), query
->dialog_token
, frag_id
,
403 more_frags
, comeback_delay
);
404 eloop_cancel_timeout(gas_query_rx_comeback_timeout
, gas
, query
);
406 if ((size_t) 2 + adv_proto
[1] != wpabuf_len(query
->adv_proto
) ||
407 os_memcmp(adv_proto
, wpabuf_head(query
->adv_proto
),
408 wpabuf_len(query
->adv_proto
)) != 0) {
409 wpa_printf(MSG_DEBUG
, "GAS: Advertisement Protocol changed "
410 "between initial and comeback response from "
411 MACSTR
, MAC2STR(query
->addr
));
412 gas_query_done(gas
, query
, GAS_QUERY_AP_PEER_ERROR
);
416 if (comeback_delay
) {
418 wpa_printf(MSG_DEBUG
, "GAS: Invalid comeback response "
419 "with non-zero frag_id and comeback_delay "
420 "from " MACSTR
, MAC2STR(query
->addr
));
421 gas_query_done(gas
, query
, GAS_QUERY_AP_PEER_ERROR
);
424 gas_query_tx_comeback_req_delay(gas
, query
, comeback_delay
);
428 if (frag_id
!= query
->next_frag_id
) {
429 wpa_printf(MSG_DEBUG
, "GAS: Unexpected frag_id in response "
430 "from " MACSTR
, MAC2STR(query
->addr
));
431 if (frag_id
+ 1 == query
->next_frag_id
) {
432 wpa_printf(MSG_DEBUG
, "GAS: Drop frame as possible "
433 "retry of previous fragment");
436 gas_query_done(gas
, query
, GAS_QUERY_AP_PEER_ERROR
);
439 query
->next_frag_id
++;
441 if (gas_query_append(query
, resp
, len
) < 0) {
442 gas_query_done(gas
, query
, GAS_QUERY_AP_INTERNAL_ERROR
);
447 gas_query_tx_comeback_req(gas
, query
);
451 gas_query_done(gas
, query
, GAS_QUERY_AP_SUCCESS
);
456 * gas_query_ap_rx - Indicate reception of a Public Action or Protected Dual
458 * @gas: GAS query data from gas_query_init()
459 * @sa: Source MAC address of the Action frame
460 * @categ: Category of the Action frame
461 * @data: Payload of the Action frame
462 * @len: Length of @data
463 * @freq: Frequency (in MHz) on which the frame was received
464 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
466 int gas_query_ap_rx(struct gas_query_ap
*gas
, const u8
*sa
, u8 categ
,
467 const u8
*data
, size_t len
, int freq
)
469 struct gas_query_pending
*query
;
470 u8 action
, dialog_token
, frag_id
= 0, more_frags
= 0;
471 u16 comeback_delay
, resp_len
;
472 const u8
*pos
, *adv_proto
;
481 dialog_token
= *pos
++;
483 if (action
!= WLAN_PA_GAS_INITIAL_RESP
&&
484 action
!= WLAN_PA_GAS_COMEBACK_RESP
)
485 return -1; /* Not a GAS response */
487 prot
= categ
== WLAN_ACTION_PROTECTED_DUAL
;
488 pmf
= pmf_in_use(gas
->hapd
, sa
);
490 wpa_printf(MSG_DEBUG
, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
494 wpa_printf(MSG_DEBUG
, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
498 query
= gas_query_get_pending(gas
, sa
, dialog_token
);
500 wpa_printf(MSG_DEBUG
, "GAS: No pending query found for " MACSTR
501 " dialog token %u", MAC2STR(sa
), dialog_token
);
505 wpa_printf(MSG_DEBUG
, "GAS: Response in %d ms from " MACSTR
,
506 ms_from_time(&query
->last_oper
), MAC2STR(sa
));
508 if (query
->wait_comeback
&& action
== WLAN_PA_GAS_INITIAL_RESP
) {
509 wpa_printf(MSG_DEBUG
, "GAS: Unexpected initial response from "
510 MACSTR
" dialog token %u when waiting for comeback "
511 "response", MAC2STR(sa
), dialog_token
);
515 if (!query
->wait_comeback
&& action
== WLAN_PA_GAS_COMEBACK_RESP
) {
516 wpa_printf(MSG_DEBUG
, "GAS: Unexpected comeback response from "
517 MACSTR
" dialog token %u when waiting for initial "
518 "response", MAC2STR(sa
), dialog_token
);
522 query
->status_code
= WPA_GET_LE16(pos
);
525 if (query
->status_code
== WLAN_STATUS_QUERY_RESP_OUTSTANDING
&&
526 action
== WLAN_PA_GAS_COMEBACK_RESP
) {
527 wpa_printf(MSG_DEBUG
, "GAS: Allow non-zero status for outstanding comeback response");
528 } else if (query
->status_code
!= WLAN_STATUS_SUCCESS
) {
529 wpa_printf(MSG_DEBUG
, "GAS: Query to " MACSTR
" dialog token "
530 "%u failed - status code %u",
531 MAC2STR(sa
), dialog_token
, query
->status_code
);
532 gas_query_done(gas
, query
, GAS_QUERY_AP_FAILURE
);
536 if (action
== WLAN_PA_GAS_COMEBACK_RESP
) {
537 if (pos
+ 1 > data
+ len
)
539 frag_id
= *pos
& 0x7f;
540 more_frags
= (*pos
& 0x80) >> 7;
545 if (pos
+ 2 > data
+ len
)
547 comeback_delay
= WPA_GET_LE16(pos
);
550 /* Advertisement Protocol element */
551 if (pos
+ 2 > data
+ len
|| pos
+ 2 + pos
[1] > data
+ len
) {
552 wpa_printf(MSG_DEBUG
, "GAS: No room for Advertisement "
553 "Protocol element in the response from " MACSTR
,
558 if (*pos
!= WLAN_EID_ADV_PROTO
) {
559 wpa_printf(MSG_DEBUG
, "GAS: Unexpected Advertisement "
560 "Protocol element ID %u in response from " MACSTR
,
568 /* Query Response Length */
569 if (pos
+ 2 > data
+ len
) {
570 wpa_printf(MSG_DEBUG
, "GAS: No room for GAS Response Length");
573 resp_len
= WPA_GET_LE16(pos
);
576 left
= data
+ len
- pos
;
577 if (resp_len
> left
) {
578 wpa_printf(MSG_DEBUG
, "GAS: Truncated Query Response in "
579 "response from " MACSTR
, MAC2STR(sa
));
583 if (resp_len
< left
) {
584 wpa_printf(MSG_DEBUG
, "GAS: Ignore %u octets of extra data "
585 "after Query Response from " MACSTR
,
586 left
- resp_len
, MAC2STR(sa
));
589 if (action
== WLAN_PA_GAS_COMEBACK_RESP
)
590 gas_query_rx_comeback(gas
, query
, adv_proto
, pos
, resp_len
,
591 frag_id
, more_frags
, comeback_delay
);
593 gas_query_rx_initial(gas
, query
, adv_proto
, pos
, resp_len
,
600 static void gas_query_timeout(void *eloop_data
, void *user_ctx
)
602 struct gas_query_ap
*gas
= eloop_data
;
603 struct gas_query_pending
*query
= user_ctx
;
605 wpa_printf(MSG_DEBUG
, "GAS: No response received for query to " MACSTR
607 MAC2STR(query
->addr
), query
->dialog_token
);
608 gas_query_done(gas
, query
, GAS_QUERY_AP_TIMEOUT
);
612 static int gas_query_dialog_token_available(struct gas_query_ap
*gas
,
613 const u8
*dst
, u8 dialog_token
)
615 struct gas_query_pending
*q
;
616 dl_list_for_each(q
, &gas
->pending
, struct gas_query_pending
, list
) {
617 if (os_memcmp(dst
, q
->addr
, ETH_ALEN
) == 0 &&
618 dialog_token
== q
->dialog_token
)
626 static void gas_query_tx_initial_req(struct gas_query_ap
*gas
,
627 struct gas_query_pending
*query
)
629 if (gas_query_tx(gas
, query
, query
->req
,
630 GAS_QUERY_WAIT_TIME_INITIAL
) < 0) {
631 wpa_printf(MSG_DEBUG
, "GAS: Failed to send Action frame to "
632 MACSTR
, MAC2STR(query
->addr
));
633 gas_query_done(gas
, query
, GAS_QUERY_AP_INTERNAL_ERROR
);
636 gas
->current
= query
;
638 wpa_printf(MSG_DEBUG
, "GAS: Starting query timeout for dialog token %u",
639 query
->dialog_token
);
640 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD
, 0,
641 gas_query_timeout
, gas
, query
);
645 static int gas_query_new_dialog_token(struct gas_query_ap
*gas
, const u8
*dst
)
647 static int next_start
= 0;
650 for (dialog_token
= 0; dialog_token
< 256; dialog_token
++) {
651 if (gas_query_dialog_token_available(
652 gas
, dst
, (next_start
+ dialog_token
) % 256))
655 if (dialog_token
== 256)
656 return -1; /* Too many pending queries */
657 dialog_token
= (next_start
+ dialog_token
) % 256;
658 next_start
= (dialog_token
+ 1) % 256;
664 * gas_query_ap_req - Request a GAS query
665 * @gas: GAS query data from gas_query_init()
666 * @dst: Destination MAC address for the query
667 * @freq: Frequency (in MHz) for the channel on which to send the query
668 * @req: GAS query payload (to be freed by gas_query module in case of success
670 * @cb: Callback function for reporting GAS query result and response
671 * @ctx: Context pointer to use with the @cb call
672 * Returns: dialog token (>= 0) on success or -1 on failure
674 int gas_query_ap_req(struct gas_query_ap
*gas
, const u8
*dst
, int freq
,
676 void (*cb
)(void *ctx
, const u8
*dst
, u8 dialog_token
,
677 enum gas_query_ap_result result
,
678 const struct wpabuf
*adv_proto
,
679 const struct wpabuf
*resp
, u16 status_code
),
682 struct gas_query_pending
*query
;
685 if (!gas
|| wpabuf_len(req
) < 3)
688 dialog_token
= gas_query_new_dialog_token(gas
, dst
);
689 if (dialog_token
< 0)
692 query
= os_zalloc(sizeof(*query
));
697 os_memcpy(query
->addr
, dst
, ETH_ALEN
);
698 query
->dialog_token
= dialog_token
;
703 dl_list_add(&gas
->pending
, &query
->list
);
705 *(wpabuf_mhead_u8(req
) + 2) = dialog_token
;
707 wpa_msg(gas
->msg_ctx
, MSG_INFO
, GAS_QUERY_START
"addr=" MACSTR
708 " dialog_token=%u freq=%d",
709 MAC2STR(query
->addr
), query
->dialog_token
, query
->freq
);
711 gas_query_tx_initial_req(gas
, query
);