2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011-2013, 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 "wpa_supplicant_i.h"
20 #include "offchannel.h"
21 #include "gas_query.h"
24 /** GAS query timeout in seconds */
25 #define GAS_QUERY_TIMEOUT_PERIOD 2
26 /** Retry period for GAS query requests in milliseconds */
27 #define GAS_SERVICE_RETRY_PERIOD_MS 500
31 * struct gas_query_pending - Pending GAS query
33 struct gas_query_pending {
35 struct gas_query *gas;
39 unsigned int wait_comeback:1;
40 unsigned int offchannel_tx_started:1;
44 struct wpabuf *adv_proto;
46 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
47 enum gas_query_result result,
48 const struct wpabuf *adv_proto,
49 const struct wpabuf *resp, u16 status_code);
54 * struct gas_query - Internal GAS query data
57 struct wpa_supplicant *wpa_s;
58 struct dl_list pending; /* struct gas_query_pending */
59 struct gas_query_pending *current;
60 struct wpa_radio_work *work;
64 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
65 static void gas_query_timeout(void *eloop_data, void *user_ctx);
66 static void gas_service_timeout(void *eloop_data, void *user_ctx);
70 * gas_query_init - Initialize GAS query component
71 * @wpa_s: Pointer to wpa_supplicant data
72 * Returns: Pointer to GAS query data or %NULL on failure
74 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
76 struct gas_query *gas;
78 gas = os_zalloc(sizeof(*gas));
83 dl_list_init(&gas->pending);
89 static const char * gas_result_txt(enum gas_query_result result)
92 case GAS_QUERY_SUCCESS:
94 case GAS_QUERY_FAILURE:
96 case GAS_QUERY_TIMEOUT:
98 case GAS_QUERY_PEER_ERROR:
100 case GAS_QUERY_INTERNAL_ERROR:
101 return "INTERNAL_ERROR";
102 case GAS_QUERY_CANCELLED:
104 case GAS_QUERY_DELETED_AT_DEINIT:
105 return "DELETED_AT_DEINIT";
112 static void gas_query_free(struct gas_query_pending *query, int del_list)
114 struct gas_query *gas = query->gas;
117 dl_list_del(&query->list);
119 if (gas->work && gas->work->ctx == query) {
120 radio_work_done(gas->work);
124 wpabuf_free(query->req);
125 wpabuf_free(query->adv_proto);
126 wpabuf_free(query->resp);
131 static void gas_query_done(struct gas_query *gas,
132 struct gas_query_pending *query,
133 enum gas_query_result result)
135 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
136 " dialog_token=%u freq=%d status_code=%u result=%s",
137 MAC2STR(query->addr), query->dialog_token, query->freq,
138 query->status_code, gas_result_txt(result));
139 if (gas->current == query)
141 if (query->offchannel_tx_started)
142 offchannel_send_action_done(gas->wpa_s);
143 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
144 eloop_cancel_timeout(gas_query_timeout, gas, query);
145 eloop_cancel_timeout(gas_service_timeout, gas, query);
146 dl_list_del(&query->list);
147 query->cb(query->ctx, query->addr, query->dialog_token, result,
148 query->adv_proto, query->resp, query->status_code);
149 gas_query_free(query, 0);
154 * gas_query_deinit - Deinitialize GAS query component
155 * @gas: GAS query data from gas_query_init()
157 void gas_query_deinit(struct gas_query *gas)
159 struct gas_query_pending *query, *next;
164 dl_list_for_each_safe(query, next, &gas->pending,
165 struct gas_query_pending, list)
166 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
172 static struct gas_query_pending *
173 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
175 struct gas_query_pending *q;
176 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
177 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
178 q->dialog_token == dialog_token)
185 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
188 if (wpabuf_resize(&query->resp, len) < 0) {
189 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
192 wpabuf_put_data(query->resp, data, len);
197 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
198 unsigned int freq, const u8 *dst,
199 const u8 *src, const u8 *bssid,
200 const u8 *data, size_t data_len,
201 enum offchannel_send_action_result result)
203 struct gas_query_pending *query;
204 struct gas_query *gas = wpa_s->gas;
206 if (gas->current == NULL) {
207 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
208 MACSTR " result=%d - no query in progress",
209 freq, MAC2STR(dst), result);
213 query = gas->current;
215 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
216 " result=%d query=%p dialog_token=%u",
217 freq, MAC2STR(dst), result, query, query->dialog_token);
218 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
219 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
223 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
224 eloop_cancel_timeout(gas_query_timeout, gas, query);
225 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
226 gas_query_timeout, gas, query);
228 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
229 eloop_cancel_timeout(gas_query_timeout, gas, query);
230 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
235 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
239 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
240 "freq=%d", MAC2STR(query->addr),
241 (unsigned int) wpabuf_len(req), query->freq);
242 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
243 gas->wpa_s->own_addr, query->addr,
244 wpabuf_head(req), wpabuf_len(req), 1000,
245 gas_query_tx_status, 0);
247 query->offchannel_tx_started = 1;
252 static void gas_query_tx_comeback_req(struct gas_query *gas,
253 struct gas_query_pending *query)
257 req = gas_build_comeback_req(query->dialog_token);
259 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
263 if (gas_query_tx(gas, query, req) < 0) {
264 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
265 MACSTR, MAC2STR(query->addr));
266 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
273 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
275 struct gas_query *gas = eloop_data;
276 struct gas_query_pending *query = user_ctx;
278 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
279 MAC2STR(query->addr));
280 gas_query_tx_comeback_req(gas, query);
284 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
285 struct gas_query_pending *query,
288 unsigned int secs, usecs;
290 secs = (comeback_delay * 1024) / 1000000;
291 usecs = comeback_delay * 1024 - secs * 1000000;
292 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
293 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
294 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
295 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
300 static void gas_query_rx_initial(struct gas_query *gas,
301 struct gas_query_pending *query,
302 const u8 *adv_proto, const u8 *resp,
303 size_t len, u16 comeback_delay)
305 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
306 MACSTR " (dialog_token=%u comeback_delay=%u)",
307 MAC2STR(query->addr), query->dialog_token, comeback_delay);
309 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
310 if (query->adv_proto == NULL) {
311 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
315 if (comeback_delay) {
316 query->wait_comeback = 1;
317 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
321 /* Query was completed without comeback mechanism */
322 if (gas_query_append(query, resp, len) < 0) {
323 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
327 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
331 static void gas_query_rx_comeback(struct gas_query *gas,
332 struct gas_query_pending *query,
333 const u8 *adv_proto, const u8 *resp,
334 size_t len, u8 frag_id, u8 more_frags,
337 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
338 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
339 "comeback_delay=%u)",
340 MAC2STR(query->addr), query->dialog_token, frag_id,
341 more_frags, comeback_delay);
343 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
344 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
345 wpabuf_len(query->adv_proto)) != 0) {
346 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
347 "between initial and comeback response from "
348 MACSTR, MAC2STR(query->addr));
349 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
353 if (comeback_delay) {
355 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
356 "with non-zero frag_id and comeback_delay "
357 "from " MACSTR, MAC2STR(query->addr));
358 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
361 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
365 if (frag_id != query->next_frag_id) {
366 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
367 "from " MACSTR, MAC2STR(query->addr));
368 if (frag_id + 1 == query->next_frag_id) {
369 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
370 "retry of previous fragment");
373 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
376 query->next_frag_id++;
378 if (gas_query_append(query, resp, len) < 0) {
379 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
384 gas_query_tx_comeback_req(gas, query);
388 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
393 * gas_query_rx - Indicate reception of a Public Action frame
394 * @gas: GAS query data from gas_query_init()
395 * @da: Destination MAC address of the Action frame
396 * @sa: Source MAC address of the Action frame
397 * @bssid: BSSID of the Action frame
398 * @data: Payload of the Action frame
399 * @len: Length of @data
400 * @freq: Frequency (in MHz) on which the frame was received
401 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
403 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
404 const u8 *bssid, const u8 *data, size_t len, int freq)
406 struct gas_query_pending *query;
407 u8 action, dialog_token, frag_id = 0, more_frags = 0;
408 u16 comeback_delay, resp_len;
409 const u8 *pos, *adv_proto;
411 if (gas == NULL || len < 4)
416 dialog_token = *pos++;
418 if (action != WLAN_PA_GAS_INITIAL_RESP &&
419 action != WLAN_PA_GAS_COMEBACK_RESP)
420 return -1; /* Not a GAS response */
422 query = gas_query_get_pending(gas, sa, dialog_token);
424 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
425 " dialog token %u", MAC2STR(sa), dialog_token);
429 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
430 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
431 MACSTR " dialog token %u when waiting for comeback "
432 "response", MAC2STR(sa), dialog_token);
436 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
437 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
438 MACSTR " dialog token %u when waiting for initial "
439 "response", MAC2STR(sa), dialog_token);
443 query->status_code = WPA_GET_LE16(pos);
446 if (query->status_code != WLAN_STATUS_SUCCESS) {
447 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
448 "%u failed - status code %u",
449 MAC2STR(sa), dialog_token, query->status_code);
450 gas_query_done(gas, query, GAS_QUERY_FAILURE);
454 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
455 if (pos + 1 > data + len)
457 frag_id = *pos & 0x7f;
458 more_frags = (*pos & 0x80) >> 7;
463 if (pos + 2 > data + len)
465 comeback_delay = WPA_GET_LE16(pos);
468 /* Advertisement Protocol element */
469 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
470 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
471 "Protocol element in the response from " MACSTR,
476 if (*pos != WLAN_EID_ADV_PROTO) {
477 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
478 "Protocol element ID %u in response from " MACSTR,
486 /* Query Response Length */
487 if (pos + 2 > data + len) {
488 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
491 resp_len = WPA_GET_LE16(pos);
494 if (pos + resp_len > data + len) {
495 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
496 "response from " MACSTR, MAC2STR(sa));
500 if (pos + resp_len < data + len) {
501 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
502 "after Query Response from " MACSTR,
503 (unsigned int) (data + len - pos - resp_len),
507 if (action == WLAN_PA_GAS_COMEBACK_RESP)
508 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
509 frag_id, more_frags, comeback_delay);
511 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
518 static void gas_query_timeout(void *eloop_data, void *user_ctx)
520 struct gas_query *gas = eloop_data;
521 struct gas_query_pending *query = user_ctx;
523 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
525 MAC2STR(query->addr), query->dialog_token);
526 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
530 static void gas_service_timeout(void *eloop_data, void *user_ctx)
532 struct gas_query *gas = eloop_data;
533 struct wpa_supplicant *wpa_s = gas->wpa_s;
534 struct gas_query_pending *query = user_ctx;
537 conn = wpas_wpa_is_in_progress(wpa_s, 1);
539 wpa_printf(MSG_DEBUG, "GAS: Delaying GAS query Tx while connection operation is in progress");
540 eloop_register_timeout(
541 GAS_SERVICE_RETRY_PERIOD_MS / 1000,
542 (GAS_SERVICE_RETRY_PERIOD_MS % 1000) * 1000,
543 gas_service_timeout, gas, query);
547 if (gas_query_tx(gas, query, query->req) < 0) {
548 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
549 MACSTR, MAC2STR(query->addr));
550 gas_query_free(query, 1);
553 gas->current = query;
555 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
556 query->dialog_token);
557 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
558 gas_query_timeout, gas, query);
562 static int gas_query_dialog_token_available(struct gas_query *gas,
563 const u8 *dst, u8 dialog_token)
565 struct gas_query_pending *q;
566 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
567 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
568 dialog_token == q->dialog_token)
576 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
578 struct gas_query_pending *query = work->ctx;
581 gas_query_free(query, 1);
585 query->gas->work = work;
586 gas_service_timeout(query->gas, query);
591 * gas_query_req - Request a GAS query
592 * @gas: GAS query data from gas_query_init()
593 * @dst: Destination MAC address for the query
594 * @freq: Frequency (in MHz) for the channel on which to send the query
595 * @req: GAS query payload (to be freed by gas_query module in case of success
597 * @cb: Callback function for reporting GAS query result and response
598 * @ctx: Context pointer to use with the @cb call
599 * Returns: dialog token (>= 0) on success or -1 on failure
601 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
603 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
604 enum gas_query_result result,
605 const struct wpabuf *adv_proto,
606 const struct wpabuf *resp, u16 status_code),
609 struct gas_query_pending *query;
611 static int next_start = 0;
613 if (wpabuf_len(req) < 3)
616 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
617 if (gas_query_dialog_token_available(
618 gas, dst, (next_start + dialog_token) % 256))
621 if (dialog_token == 256)
622 return -1; /* Too many pending queries */
623 dialog_token = (next_start + dialog_token) % 256;
624 next_start = (dialog_token + 1) % 256;
626 query = os_zalloc(sizeof(*query));
631 os_memcpy(query->addr, dst, ETH_ALEN);
632 query->dialog_token = dialog_token;
637 dl_list_add(&gas->pending, &query->list);
639 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
641 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
642 " dialog_token=%u freq=%d",
643 MAC2STR(query->addr), query->dialog_token, query->freq);
645 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
647 gas_query_free(query, 1);
656 * gas_query_cancel - Cancel a pending GAS query
657 * @gas: GAS query data from gas_query_init()
658 * @dst: Destination MAC address for the query
659 * @dialog_token: Dialog token from gas_query_req()
661 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
663 struct gas_query_pending *query;
665 query = gas_query_get_pending(gas, dst, dialog_token);
667 gas_query_done(gas, query, GAS_QUERY_CANCELLED);