2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
13 #include "utils/eloop.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/gas.h"
16 #include "wpa_supplicant_i.h"
18 #include "offchannel.h"
19 #include "gas_query.h"
22 /** GAS query timeout in seconds */
23 #define GAS_QUERY_TIMEOUT_PERIOD 2
24 /** Retry period for GAS query requests in milliseconds */
25 #define GAS_SERVICE_RETRY_PERIOD_MS 500
29 * struct gas_query_pending - Pending GAS query
31 struct gas_query_pending {
36 unsigned int wait_comeback:1;
37 unsigned int offchannel_tx_started:1;
41 struct wpabuf *adv_proto;
43 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
44 enum gas_query_result result,
45 const struct wpabuf *adv_proto,
46 const struct wpabuf *resp, u16 status_code);
51 * struct gas_query - Internal GAS query data
54 struct wpa_supplicant *wpa_s;
55 struct dl_list pending; /* struct gas_query_pending */
56 struct gas_query_pending *current;
60 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
61 static void gas_query_timeout(void *eloop_data, void *user_ctx);
62 static void gas_service_timeout(void *eloop_data, void *user_ctx);
66 * gas_query_init - Initialize GAS query component
67 * @wpa_s: Pointer to wpa_supplicant data
68 * Returns: Pointer to GAS query data or %NULL on failure
70 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
72 struct gas_query *gas;
74 gas = os_zalloc(sizeof(*gas));
79 dl_list_init(&gas->pending);
85 static void gas_query_done(struct gas_query *gas,
86 struct gas_query_pending *query,
87 enum gas_query_result result)
89 if (gas->current == query)
91 if (query->offchannel_tx_started)
92 offchannel_send_action_done(gas->wpa_s);
93 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
94 eloop_cancel_timeout(gas_query_timeout, gas, query);
95 eloop_cancel_timeout(gas_service_timeout, gas, query);
96 dl_list_del(&query->list);
97 query->cb(query->ctx, query->addr, query->dialog_token, result,
98 query->adv_proto, query->resp, query->status_code);
99 wpabuf_free(query->req);
100 wpabuf_free(query->adv_proto);
101 wpabuf_free(query->resp);
107 * gas_query_deinit - Deinitialize GAS query component
108 * @gas: GAS query data from gas_query_init()
110 void gas_query_deinit(struct gas_query *gas)
112 struct gas_query_pending *query, *next;
117 dl_list_for_each_safe(query, next, &gas->pending,
118 struct gas_query_pending, list)
119 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
125 static struct gas_query_pending *
126 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
128 struct gas_query_pending *q;
129 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
130 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
131 q->dialog_token == dialog_token)
138 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
141 if (wpabuf_resize(&query->resp, len) < 0) {
142 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
145 wpabuf_put_data(query->resp, data, len);
150 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
151 unsigned int freq, const u8 *dst,
152 const u8 *src, const u8 *bssid,
153 const u8 *data, size_t data_len,
154 enum offchannel_send_action_result result)
156 struct gas_query_pending *q, *query = NULL;
157 struct gas_query *gas = wpa_s->gas;
159 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
160 if (os_memcmp(q->addr, dst, ETH_ALEN) == 0) {
166 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
167 " result=%d query=%p",
168 freq, MAC2STR(dst), result, query);
172 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
173 eloop_cancel_timeout(gas_query_timeout, gas, query);
174 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
175 gas_query_timeout, gas, query);
177 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
178 eloop_cancel_timeout(gas_query_timeout, gas, query);
179 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
184 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
188 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
189 "freq=%d", MAC2STR(query->addr),
190 (unsigned int) wpabuf_len(req), query->freq);
191 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
192 gas->wpa_s->own_addr, query->addr,
193 wpabuf_head(req), wpabuf_len(req), 1000,
194 gas_query_tx_status, 0);
196 query->offchannel_tx_started = 1;
201 static void gas_query_tx_comeback_req(struct gas_query *gas,
202 struct gas_query_pending *query)
206 req = gas_build_comeback_req(query->dialog_token);
208 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
212 if (gas_query_tx(gas, query, req) < 0) {
213 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
214 MACSTR, MAC2STR(query->addr));
215 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
222 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
224 struct gas_query *gas = eloop_data;
225 struct gas_query_pending *query = user_ctx;
227 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
228 MAC2STR(query->addr));
229 gas_query_tx_comeback_req(gas, query);
233 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
234 struct gas_query_pending *query,
237 unsigned int secs, usecs;
239 secs = (comeback_delay * 1024) / 1000000;
240 usecs = comeback_delay * 1024 - secs * 1000000;
241 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
242 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
243 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
244 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
249 static void gas_query_rx_initial(struct gas_query *gas,
250 struct gas_query_pending *query,
251 const u8 *adv_proto, const u8 *resp,
252 size_t len, u16 comeback_delay)
254 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
255 MACSTR " (dialog_token=%u comeback_delay=%u)",
256 MAC2STR(query->addr), query->dialog_token, comeback_delay);
258 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
259 if (query->adv_proto == NULL) {
260 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
264 if (comeback_delay) {
265 query->wait_comeback = 1;
266 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
270 /* Query was completed without comeback mechanism */
271 if (gas_query_append(query, resp, len) < 0) {
272 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
276 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
280 static void gas_query_rx_comeback(struct gas_query *gas,
281 struct gas_query_pending *query,
282 const u8 *adv_proto, const u8 *resp,
283 size_t len, u8 frag_id, u8 more_frags,
286 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
287 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
288 "comeback_delay=%u)",
289 MAC2STR(query->addr), query->dialog_token, frag_id,
290 more_frags, comeback_delay);
292 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
293 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
294 wpabuf_len(query->adv_proto)) != 0) {
295 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
296 "between initial and comeback response from "
297 MACSTR, MAC2STR(query->addr));
298 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
302 if (comeback_delay) {
304 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
305 "with non-zero frag_id and comeback_delay "
306 "from " MACSTR, MAC2STR(query->addr));
307 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
310 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
314 if (frag_id != query->next_frag_id) {
315 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
316 "from " MACSTR, MAC2STR(query->addr));
317 if (frag_id + 1 == query->next_frag_id) {
318 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
319 "retry of previous fragment");
322 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
325 query->next_frag_id++;
327 if (gas_query_append(query, resp, len) < 0) {
328 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
333 gas_query_tx_comeback_req(gas, query);
337 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
342 * gas_query_rx - Indicate reception of a Public Action frame
343 * @gas: GAS query data from gas_query_init()
344 * @da: Destination MAC address of the Action frame
345 * @sa: Source MAC address of the Action frame
346 * @bssid: BSSID of the Action frame
347 * @data: Payload of the Action frame
348 * @len: Length of @data
349 * @freq: Frequency (in MHz) on which the frame was received
350 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
352 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
353 const u8 *bssid, const u8 *data, size_t len, int freq)
355 struct gas_query_pending *query;
356 u8 action, dialog_token, frag_id = 0, more_frags = 0;
357 u16 comeback_delay, resp_len;
358 const u8 *pos, *adv_proto;
360 if (gas == NULL || len < 4)
365 dialog_token = *pos++;
367 if (action != WLAN_PA_GAS_INITIAL_RESP &&
368 action != WLAN_PA_GAS_COMEBACK_RESP)
369 return -1; /* Not a GAS response */
371 query = gas_query_get_pending(gas, sa, dialog_token);
373 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
374 " dialog token %u", MAC2STR(sa), dialog_token);
378 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
379 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
380 MACSTR " dialog token %u when waiting for comeback "
381 "response", MAC2STR(sa), dialog_token);
385 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
386 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
387 MACSTR " dialog token %u when waiting for initial "
388 "response", MAC2STR(sa), dialog_token);
392 query->status_code = WPA_GET_LE16(pos);
395 if (query->status_code != WLAN_STATUS_SUCCESS) {
396 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
397 "%u failed - status code %u",
398 MAC2STR(sa), dialog_token, query->status_code);
399 gas_query_done(gas, query, GAS_QUERY_FAILURE);
403 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
404 if (pos + 1 > data + len)
406 frag_id = *pos & 0x7f;
407 more_frags = (*pos & 0x80) >> 7;
412 if (pos + 2 > data + len)
414 comeback_delay = WPA_GET_LE16(pos);
417 /* Advertisement Protocol element */
418 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
419 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
420 "Protocol element in the response from " MACSTR,
425 if (*pos != WLAN_EID_ADV_PROTO) {
426 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
427 "Protocol element ID %u in response from " MACSTR,
435 /* Query Response Length */
436 if (pos + 2 > data + len) {
437 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
440 resp_len = WPA_GET_LE16(pos);
443 if (pos + resp_len > data + len) {
444 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
445 "response from " MACSTR, MAC2STR(sa));
449 if (pos + resp_len < data + len) {
450 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
451 "after Query Response from " MACSTR,
452 (unsigned int) (data + len - pos - resp_len),
456 if (action == WLAN_PA_GAS_COMEBACK_RESP)
457 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
458 frag_id, more_frags, comeback_delay);
460 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
467 static void gas_query_timeout(void *eloop_data, void *user_ctx)
469 struct gas_query *gas = eloop_data;
470 struct gas_query_pending *query = user_ctx;
472 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
473 MAC2STR(query->addr));
474 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
478 static void gas_service_timeout(void *eloop_data, void *user_ctx)
480 struct gas_query *gas = eloop_data;
481 struct wpa_supplicant *wpa_s = gas->wpa_s;
482 struct gas_query_pending *query = user_ctx;
485 conn = wpas_wpa_is_in_progress(wpa_s, 1);
486 if (conn || wpa_s->scanning || gas->current) {
487 wpa_printf(MSG_DEBUG, "GAS: Delaying GAS query Tx while another operation is in progress:%s%s%s",
488 conn ? " connection" : "",
489 wpa_s->scanning ? " scanning" : "",
490 gas->current ? " gas_query" : "");
491 eloop_register_timeout(
492 GAS_SERVICE_RETRY_PERIOD_MS / 1000,
493 (GAS_SERVICE_RETRY_PERIOD_MS % 1000) * 1000,
494 gas_service_timeout, gas, query);
498 if (gas_query_tx(gas, query, query->req) < 0) {
499 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
500 MACSTR, MAC2STR(query->addr));
501 dl_list_del(&query->list);
502 wpabuf_free(query->req);
506 gas->current = query;
508 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
509 gas_query_timeout, gas, query);
513 static int gas_query_dialog_token_available(struct gas_query *gas,
514 const u8 *dst, u8 dialog_token)
516 struct gas_query_pending *q;
517 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
518 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
519 dialog_token == q->dialog_token)
528 * gas_query_req - Request a GAS query
529 * @gas: GAS query data from gas_query_init()
530 * @dst: Destination MAC address for the query
531 * @freq: Frequency (in MHz) for the channel on which to send the query
532 * @req: GAS query payload (to be freed by gas_query module in case of success
534 * @cb: Callback function for reporting GAS query result and response
535 * @ctx: Context pointer to use with the @cb call
536 * Returns: dialog token (>= 0) on success or -1 on failure
538 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
540 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
541 enum gas_query_result result,
542 const struct wpabuf *adv_proto,
543 const struct wpabuf *resp, u16 status_code),
546 struct gas_query_pending *query;
548 static int next_start = 0;
550 if (wpabuf_len(req) < 3)
553 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
554 if (gas_query_dialog_token_available(
555 gas, dst, (next_start + dialog_token) % 256))
558 if (dialog_token == 256)
559 return -1; /* Too many pending queries */
560 dialog_token = (next_start + dialog_token) % 256;
561 next_start = (dialog_token + 1) % 256;
563 query = os_zalloc(sizeof(*query));
567 os_memcpy(query->addr, dst, ETH_ALEN);
568 query->dialog_token = dialog_token;
573 dl_list_add(&gas->pending, &query->list);
575 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
577 wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
578 " dialog_token %u", MAC2STR(dst), dialog_token);
580 eloop_register_timeout(0, 0, gas_service_timeout, gas, query);
587 * gas_query_cancel - Cancel a pending GAS query
588 * @gas: GAS query data from gas_query_init()
589 * @dst: Destination MAC address for the query
590 * @dialog_token: Dialog token from gas_query_req()
592 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
594 struct gas_query_pending *query;
596 query = gas_query_get_pending(gas, dst, dialog_token);
598 gas_query_done(gas, query, GAS_QUERY_CANCELLED);
603 int gas_query_in_progress(struct gas_query *gas)
605 return gas->current != NULL;