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
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 *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_result result,
52 const struct wpabuf *adv_proto,
53 const struct wpabuf *resp, u16 status_code);
58 * struct gas_query - Internal GAS query data
61 struct wpa_supplicant *wpa_s;
62 struct dl_list pending; /* struct gas_query_pending */
63 struct gas_query_pending *current;
64 struct wpa_radio_work *work;
68 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
69 static void gas_query_timeout(void *eloop_data, void *user_ctx);
70 static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
71 static void gas_query_tx_initial_req(struct gas_query *gas,
72 struct gas_query_pending *query);
73 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
76 static int ms_from_time(struct os_reltime *last)
78 struct os_reltime now, res;
81 os_reltime_sub(&now, last, &res);
82 return res.sec * 1000 + res.usec / 1000;
87 * gas_query_init - Initialize GAS query component
88 * @wpa_s: Pointer to wpa_supplicant data
89 * Returns: Pointer to GAS query data or %NULL on failure
91 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
93 struct gas_query *gas;
95 gas = os_zalloc(sizeof(*gas));
100 dl_list_init(&gas->pending);
106 static const char * gas_result_txt(enum gas_query_result result)
109 case GAS_QUERY_SUCCESS:
111 case GAS_QUERY_FAILURE:
113 case GAS_QUERY_TIMEOUT:
115 case GAS_QUERY_PEER_ERROR:
117 case GAS_QUERY_INTERNAL_ERROR:
118 return "INTERNAL_ERROR";
119 case GAS_QUERY_CANCELLED:
121 case GAS_QUERY_DELETED_AT_DEINIT:
122 return "DELETED_AT_DEINIT";
129 static void gas_query_free(struct gas_query_pending *query, int del_list)
131 struct gas_query *gas = query->gas;
134 dl_list_del(&query->list);
136 if (gas->work && gas->work->ctx == query) {
137 radio_work_done(gas->work);
141 wpabuf_free(query->req);
142 wpabuf_free(query->adv_proto);
143 wpabuf_free(query->resp);
148 static void gas_query_done(struct gas_query *gas,
149 struct gas_query_pending *query,
150 enum gas_query_result result)
152 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
153 " dialog_token=%u freq=%d status_code=%u result=%s",
154 MAC2STR(query->addr), query->dialog_token, query->freq,
155 query->status_code, gas_result_txt(result));
156 if (gas->current == query)
158 if (query->offchannel_tx_started)
159 offchannel_send_action_done(gas->wpa_s);
160 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
161 eloop_cancel_timeout(gas_query_timeout, gas, query);
162 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
163 dl_list_del(&query->list);
164 query->cb(query->ctx, query->addr, query->dialog_token, result,
165 query->adv_proto, query->resp, query->status_code);
166 gas_query_free(query, 0);
171 * gas_query_deinit - Deinitialize GAS query component
172 * @gas: GAS query data from gas_query_init()
174 void gas_query_deinit(struct gas_query *gas)
176 struct gas_query_pending *query, *next;
181 dl_list_for_each_safe(query, next, &gas->pending,
182 struct gas_query_pending, list)
183 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
189 static struct gas_query_pending *
190 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
192 struct gas_query_pending *q;
193 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
194 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
195 q->dialog_token == dialog_token)
202 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
205 if (wpabuf_resize(&query->resp, len) < 0) {
206 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
209 wpabuf_put_data(query->resp, data, len);
214 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
215 unsigned int freq, const u8 *dst,
216 const u8 *src, const u8 *bssid,
217 const u8 *data, size_t data_len,
218 enum offchannel_send_action_result result)
220 struct gas_query_pending *query;
221 struct gas_query *gas = wpa_s->gas;
224 if (gas->current == NULL) {
225 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
226 MACSTR " result=%d - no query in progress",
227 freq, MAC2STR(dst), result);
231 query = gas->current;
233 dur = ms_from_time(&query->last_oper);
234 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
235 " result=%d query=%p dialog_token=%u dur=%d ms",
236 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
237 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
238 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
241 os_get_reltime(&query->last_oper);
243 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
244 eloop_cancel_timeout(gas_query_timeout, gas, query);
245 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
246 gas_query_timeout, gas, query);
247 if (query->wait_comeback && !query->retry) {
248 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
250 eloop_register_timeout(
251 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
252 gas_query_rx_comeback_timeout, gas, query);
255 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
256 eloop_cancel_timeout(gas_query_timeout, gas, query);
257 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
262 static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
264 if (wpa_s->current_ssid == NULL ||
265 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
266 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
268 return wpa_sm_pmf_enabled(wpa_s->wpa);
272 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
273 struct wpabuf *req, unsigned int wait_time)
275 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
277 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
278 "freq=%d prot=%d", MAC2STR(query->addr),
279 (unsigned int) wpabuf_len(req), query->freq, prot);
281 u8 *categ = wpabuf_mhead_u8(req);
282 *categ = WLAN_ACTION_PROTECTED_DUAL;
284 os_get_reltime(&query->last_oper);
285 if (gas->wpa_s->max_remain_on_chan &&
286 wait_time > gas->wpa_s->max_remain_on_chan)
287 wait_time = gas->wpa_s->max_remain_on_chan;
288 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
289 gas->wpa_s->own_addr, query->addr,
290 wpabuf_head(req), wpabuf_len(req),
291 wait_time, gas_query_tx_status, 0);
293 query->offchannel_tx_started = 1;
298 static void gas_query_tx_comeback_req(struct gas_query *gas,
299 struct gas_query_pending *query)
302 unsigned int wait_time;
304 req = gas_build_comeback_req(query->dialog_token);
306 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
310 wait_time = (query->retry || !query->offchannel_tx_started) ?
311 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
313 if (gas_query_tx(gas, query, req, wait_time) < 0) {
314 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
315 MACSTR, MAC2STR(query->addr));
316 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
323 static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
325 struct gas_query *gas = eloop_data;
326 struct gas_query_pending *query = user_ctx;
329 wpa_printf(MSG_DEBUG,
330 "GAS: No response to comeback request received (retry=%u)",
332 if (gas->current != query || query->retry)
334 dialog_token = gas_query_new_dialog_token(gas, query->addr);
335 if (dialog_token < 0)
337 wpa_printf(MSG_DEBUG,
338 "GAS: Retry GAS query due to comeback response timeout");
340 query->dialog_token = dialog_token;
341 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
342 query->wait_comeback = 0;
343 query->next_frag_id = 0;
344 wpabuf_free(query->adv_proto);
345 query->adv_proto = NULL;
346 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
347 eloop_cancel_timeout(gas_query_timeout, gas, query);
348 gas_query_tx_initial_req(gas, query);
352 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
354 struct gas_query *gas = eloop_data;
355 struct gas_query_pending *query = user_ctx;
357 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
358 MAC2STR(query->addr));
359 gas_query_tx_comeback_req(gas, query);
363 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
364 struct gas_query_pending *query,
367 unsigned int secs, usecs;
369 if (comeback_delay > 1 && query->offchannel_tx_started) {
370 offchannel_send_action_done(gas->wpa_s);
371 query->offchannel_tx_started = 0;
374 secs = (comeback_delay * 1024) / 1000000;
375 usecs = comeback_delay * 1024 - secs * 1000000;
376 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
377 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
378 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
379 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
384 static void gas_query_rx_initial(struct gas_query *gas,
385 struct gas_query_pending *query,
386 const u8 *adv_proto, const u8 *resp,
387 size_t len, u16 comeback_delay)
389 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
390 MACSTR " (dialog_token=%u comeback_delay=%u)",
391 MAC2STR(query->addr), query->dialog_token, comeback_delay);
393 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
394 if (query->adv_proto == NULL) {
395 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
399 if (comeback_delay) {
400 query->wait_comeback = 1;
401 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
405 /* Query was completed without comeback mechanism */
406 if (gas_query_append(query, resp, len) < 0) {
407 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
411 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
415 static void gas_query_rx_comeback(struct gas_query *gas,
416 struct gas_query_pending *query,
417 const u8 *adv_proto, const u8 *resp,
418 size_t len, u8 frag_id, u8 more_frags,
421 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
422 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
423 "comeback_delay=%u)",
424 MAC2STR(query->addr), query->dialog_token, frag_id,
425 more_frags, comeback_delay);
426 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
428 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
429 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
430 wpabuf_len(query->adv_proto)) != 0) {
431 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
432 "between initial and comeback response from "
433 MACSTR, MAC2STR(query->addr));
434 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
438 if (comeback_delay) {
440 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
441 "with non-zero frag_id and comeback_delay "
442 "from " MACSTR, MAC2STR(query->addr));
443 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
446 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
450 if (frag_id != query->next_frag_id) {
451 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
452 "from " MACSTR, MAC2STR(query->addr));
453 if (frag_id + 1 == query->next_frag_id) {
454 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
455 "retry of previous fragment");
458 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
461 query->next_frag_id++;
463 if (gas_query_append(query, resp, len) < 0) {
464 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
469 gas_query_tx_comeback_req(gas, query);
473 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
478 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
479 * @gas: GAS query data from gas_query_init()
480 * @da: Destination MAC address of the Action frame
481 * @sa: Source MAC address of the Action frame
482 * @bssid: BSSID of the Action frame
483 * @categ: Category of the Action frame
484 * @data: Payload of the Action frame
485 * @len: Length of @data
486 * @freq: Frequency (in MHz) on which the frame was received
487 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
489 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
490 const u8 *bssid, u8 categ, const u8 *data, size_t len,
493 struct gas_query_pending *query;
494 u8 action, dialog_token, frag_id = 0, more_frags = 0;
495 u16 comeback_delay, resp_len;
496 const u8 *pos, *adv_proto;
500 if (gas == NULL || len < 4)
503 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
504 pmf = pmf_in_use(gas->wpa_s, sa);
506 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
510 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
516 dialog_token = *pos++;
518 if (action != WLAN_PA_GAS_INITIAL_RESP &&
519 action != WLAN_PA_GAS_COMEBACK_RESP)
520 return -1; /* Not a GAS response */
522 query = gas_query_get_pending(gas, sa, dialog_token);
524 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
525 " dialog token %u", MAC2STR(sa), dialog_token);
529 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
530 ms_from_time(&query->last_oper), MAC2STR(sa));
532 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
533 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
534 MACSTR " dialog token %u when waiting for comeback "
535 "response", MAC2STR(sa), dialog_token);
539 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
540 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
541 MACSTR " dialog token %u when waiting for initial "
542 "response", MAC2STR(sa), dialog_token);
546 query->status_code = WPA_GET_LE16(pos);
549 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
550 action == WLAN_PA_GAS_COMEBACK_RESP) {
551 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
552 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
553 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
554 "%u failed - status code %u",
555 MAC2STR(sa), dialog_token, query->status_code);
556 gas_query_done(gas, query, GAS_QUERY_FAILURE);
560 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
561 if (pos + 1 > data + len)
563 frag_id = *pos & 0x7f;
564 more_frags = (*pos & 0x80) >> 7;
569 if (pos + 2 > data + len)
571 comeback_delay = WPA_GET_LE16(pos);
574 /* Advertisement Protocol element */
575 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
576 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
577 "Protocol element in the response from " MACSTR,
582 if (*pos != WLAN_EID_ADV_PROTO) {
583 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
584 "Protocol element ID %u in response from " MACSTR,
592 /* Query Response Length */
593 if (pos + 2 > data + len) {
594 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
597 resp_len = WPA_GET_LE16(pos);
600 left = data + len - pos;
601 if (resp_len > left) {
602 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
603 "response from " MACSTR, MAC2STR(sa));
607 if (resp_len < left) {
608 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
609 "after Query Response from " MACSTR,
610 left - resp_len, MAC2STR(sa));
613 if (action == WLAN_PA_GAS_COMEBACK_RESP)
614 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
615 frag_id, more_frags, comeback_delay);
617 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
624 static void gas_query_timeout(void *eloop_data, void *user_ctx)
626 struct gas_query *gas = eloop_data;
627 struct gas_query_pending *query = user_ctx;
629 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
631 MAC2STR(query->addr), query->dialog_token);
632 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
636 static int gas_query_dialog_token_available(struct gas_query *gas,
637 const u8 *dst, u8 dialog_token)
639 struct gas_query_pending *q;
640 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
641 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
642 dialog_token == q->dialog_token)
650 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
652 struct gas_query_pending *query = work->ctx;
653 struct gas_query *gas = query->gas;
654 struct wpa_supplicant *wpa_s = gas->wpa_s;
659 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
663 gas_query_free(query, 1);
667 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
668 wpa_msg(wpa_s, MSG_INFO,
669 "Failed to assign random MAC address for GAS");
670 gas_query_free(query, 1);
671 radio_work_done(work);
676 gas_query_tx_initial_req(gas, query);
680 static void gas_query_tx_initial_req(struct gas_query *gas,
681 struct gas_query_pending *query)
683 if (gas_query_tx(gas, query, query->req,
684 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
685 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
686 MACSTR, MAC2STR(query->addr));
687 gas_query_free(query, 1);
690 gas->current = query;
692 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
693 query->dialog_token);
694 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
695 gas_query_timeout, gas, query);
699 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
701 static int next_start = 0;
704 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
705 if (gas_query_dialog_token_available(
706 gas, dst, (next_start + dialog_token) % 256))
709 if (dialog_token == 256)
710 return -1; /* Too many pending queries */
711 dialog_token = (next_start + dialog_token) % 256;
712 next_start = (dialog_token + 1) % 256;
718 * gas_query_req - Request a GAS query
719 * @gas: GAS query data from gas_query_init()
720 * @dst: Destination MAC address for the query
721 * @freq: Frequency (in MHz) for the channel on which to send the query
722 * @req: GAS query payload (to be freed by gas_query module in case of success
724 * @cb: Callback function for reporting GAS query result and response
725 * @ctx: Context pointer to use with the @cb call
726 * Returns: dialog token (>= 0) on success or -1 on failure
728 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
730 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
731 enum gas_query_result result,
732 const struct wpabuf *adv_proto,
733 const struct wpabuf *resp, u16 status_code),
736 struct gas_query_pending *query;
739 if (wpabuf_len(req) < 3)
742 dialog_token = gas_query_new_dialog_token(gas, dst);
743 if (dialog_token < 0)
746 query = os_zalloc(sizeof(*query));
751 os_memcpy(query->addr, dst, ETH_ALEN);
752 query->dialog_token = dialog_token;
757 dl_list_add(&gas->pending, &query->list);
759 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
761 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
762 " dialog_token=%u freq=%d",
763 MAC2STR(query->addr), query->dialog_token, query->freq);
765 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
767 gas_query_free(query, 1);
776 * gas_query_cancel - Cancel a pending GAS query
777 * @gas: GAS query data from gas_query_init()
778 * @dst: Destination MAC address for the query
779 * @dialog_token: Dialog token from gas_query_req()
781 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
783 struct gas_query_pending *query;
785 query = gas_query_get_pending(gas, dst, dialog_token);
787 gas_query_done(gas, query, GAS_QUERY_CANCELLED);