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 *query;
157 struct gas_query *gas = wpa_s->gas;
159 if (gas->current == NULL) {
160 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
161 MACSTR " result=%d - no query in progress",
162 freq, MAC2STR(dst), result);
166 query = gas->current;
168 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
169 " result=%d query=%p dialog_token=%u",
170 freq, MAC2STR(dst), result, query, query->dialog_token);
171 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
172 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
176 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
177 eloop_cancel_timeout(gas_query_timeout, gas, query);
178 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
179 gas_query_timeout, gas, query);
181 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
182 eloop_cancel_timeout(gas_query_timeout, gas, query);
183 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
188 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
192 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
193 "freq=%d", MAC2STR(query->addr),
194 (unsigned int) wpabuf_len(req), query->freq);
195 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
196 gas->wpa_s->own_addr, query->addr,
197 wpabuf_head(req), wpabuf_len(req), 1000,
198 gas_query_tx_status, 0);
200 query->offchannel_tx_started = 1;
205 static void gas_query_tx_comeback_req(struct gas_query *gas,
206 struct gas_query_pending *query)
210 req = gas_build_comeback_req(query->dialog_token);
212 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
216 if (gas_query_tx(gas, query, req) < 0) {
217 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
218 MACSTR, MAC2STR(query->addr));
219 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
226 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
228 struct gas_query *gas = eloop_data;
229 struct gas_query_pending *query = user_ctx;
231 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
232 MAC2STR(query->addr));
233 gas_query_tx_comeback_req(gas, query);
237 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
238 struct gas_query_pending *query,
241 unsigned int secs, usecs;
243 secs = (comeback_delay * 1024) / 1000000;
244 usecs = comeback_delay * 1024 - secs * 1000000;
245 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
246 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
247 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
248 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
253 static void gas_query_rx_initial(struct gas_query *gas,
254 struct gas_query_pending *query,
255 const u8 *adv_proto, const u8 *resp,
256 size_t len, u16 comeback_delay)
258 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
259 MACSTR " (dialog_token=%u comeback_delay=%u)",
260 MAC2STR(query->addr), query->dialog_token, comeback_delay);
262 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
263 if (query->adv_proto == NULL) {
264 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
268 if (comeback_delay) {
269 query->wait_comeback = 1;
270 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
274 /* Query was completed without comeback mechanism */
275 if (gas_query_append(query, resp, len) < 0) {
276 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
280 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
284 static void gas_query_rx_comeback(struct gas_query *gas,
285 struct gas_query_pending *query,
286 const u8 *adv_proto, const u8 *resp,
287 size_t len, u8 frag_id, u8 more_frags,
290 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
291 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
292 "comeback_delay=%u)",
293 MAC2STR(query->addr), query->dialog_token, frag_id,
294 more_frags, comeback_delay);
296 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
297 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
298 wpabuf_len(query->adv_proto)) != 0) {
299 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
300 "between initial and comeback response from "
301 MACSTR, MAC2STR(query->addr));
302 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
306 if (comeback_delay) {
308 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
309 "with non-zero frag_id and comeback_delay "
310 "from " MACSTR, MAC2STR(query->addr));
311 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
314 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
318 if (frag_id != query->next_frag_id) {
319 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
320 "from " MACSTR, MAC2STR(query->addr));
321 if (frag_id + 1 == query->next_frag_id) {
322 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
323 "retry of previous fragment");
326 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
329 query->next_frag_id++;
331 if (gas_query_append(query, resp, len) < 0) {
332 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
337 gas_query_tx_comeback_req(gas, query);
341 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
346 * gas_query_rx - Indicate reception of a Public Action frame
347 * @gas: GAS query data from gas_query_init()
348 * @da: Destination MAC address of the Action frame
349 * @sa: Source MAC address of the Action frame
350 * @bssid: BSSID of the Action frame
351 * @data: Payload of the Action frame
352 * @len: Length of @data
353 * @freq: Frequency (in MHz) on which the frame was received
354 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
356 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
357 const u8 *bssid, const u8 *data, size_t len, int freq)
359 struct gas_query_pending *query;
360 u8 action, dialog_token, frag_id = 0, more_frags = 0;
361 u16 comeback_delay, resp_len;
362 const u8 *pos, *adv_proto;
364 if (gas == NULL || len < 4)
369 dialog_token = *pos++;
371 if (action != WLAN_PA_GAS_INITIAL_RESP &&
372 action != WLAN_PA_GAS_COMEBACK_RESP)
373 return -1; /* Not a GAS response */
375 query = gas_query_get_pending(gas, sa, dialog_token);
377 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
378 " dialog token %u", MAC2STR(sa), dialog_token);
382 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
383 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
384 MACSTR " dialog token %u when waiting for comeback "
385 "response", MAC2STR(sa), dialog_token);
389 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
390 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
391 MACSTR " dialog token %u when waiting for initial "
392 "response", MAC2STR(sa), dialog_token);
396 query->status_code = WPA_GET_LE16(pos);
399 if (query->status_code != WLAN_STATUS_SUCCESS) {
400 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
401 "%u failed - status code %u",
402 MAC2STR(sa), dialog_token, query->status_code);
403 gas_query_done(gas, query, GAS_QUERY_FAILURE);
407 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
408 if (pos + 1 > data + len)
410 frag_id = *pos & 0x7f;
411 more_frags = (*pos & 0x80) >> 7;
416 if (pos + 2 > data + len)
418 comeback_delay = WPA_GET_LE16(pos);
421 /* Advertisement Protocol element */
422 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
423 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
424 "Protocol element in the response from " MACSTR,
429 if (*pos != WLAN_EID_ADV_PROTO) {
430 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
431 "Protocol element ID %u in response from " MACSTR,
439 /* Query Response Length */
440 if (pos + 2 > data + len) {
441 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
444 resp_len = WPA_GET_LE16(pos);
447 if (pos + resp_len > data + len) {
448 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
449 "response from " MACSTR, MAC2STR(sa));
453 if (pos + resp_len < data + len) {
454 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
455 "after Query Response from " MACSTR,
456 (unsigned int) (data + len - pos - resp_len),
460 if (action == WLAN_PA_GAS_COMEBACK_RESP)
461 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
462 frag_id, more_frags, comeback_delay);
464 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
471 static void gas_query_timeout(void *eloop_data, void *user_ctx)
473 struct gas_query *gas = eloop_data;
474 struct gas_query_pending *query = user_ctx;
476 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
478 MAC2STR(query->addr), query->dialog_token);
479 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
483 static void gas_service_timeout(void *eloop_data, void *user_ctx)
485 struct gas_query *gas = eloop_data;
486 struct wpa_supplicant *wpa_s = gas->wpa_s;
487 struct gas_query_pending *query = user_ctx;
490 conn = wpas_wpa_is_in_progress(wpa_s, 1);
491 if (conn || wpa_s->scanning || gas->current) {
492 wpa_printf(MSG_DEBUG, "GAS: Delaying GAS query Tx while another operation is in progress:%s%s%s",
493 conn ? " connection" : "",
494 wpa_s->scanning ? " scanning" : "",
495 gas->current ? " gas_query" : "");
496 eloop_register_timeout(
497 GAS_SERVICE_RETRY_PERIOD_MS / 1000,
498 (GAS_SERVICE_RETRY_PERIOD_MS % 1000) * 1000,
499 gas_service_timeout, gas, query);
503 if (gas_query_tx(gas, query, query->req) < 0) {
504 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
505 MACSTR, MAC2STR(query->addr));
506 dl_list_del(&query->list);
507 wpabuf_free(query->req);
511 gas->current = query;
513 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
514 query->dialog_token);
515 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
516 gas_query_timeout, gas, query);
520 static int gas_query_dialog_token_available(struct gas_query *gas,
521 const u8 *dst, u8 dialog_token)
523 struct gas_query_pending *q;
524 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
525 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
526 dialog_token == q->dialog_token)
535 * gas_query_req - Request a GAS query
536 * @gas: GAS query data from gas_query_init()
537 * @dst: Destination MAC address for the query
538 * @freq: Frequency (in MHz) for the channel on which to send the query
539 * @req: GAS query payload (to be freed by gas_query module in case of success
541 * @cb: Callback function for reporting GAS query result and response
542 * @ctx: Context pointer to use with the @cb call
543 * Returns: dialog token (>= 0) on success or -1 on failure
545 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
547 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
548 enum gas_query_result result,
549 const struct wpabuf *adv_proto,
550 const struct wpabuf *resp, u16 status_code),
553 struct gas_query_pending *query;
555 static int next_start = 0;
557 if (wpabuf_len(req) < 3)
560 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
561 if (gas_query_dialog_token_available(
562 gas, dst, (next_start + dialog_token) % 256))
565 if (dialog_token == 256)
566 return -1; /* Too many pending queries */
567 dialog_token = (next_start + dialog_token) % 256;
568 next_start = (dialog_token + 1) % 256;
570 query = os_zalloc(sizeof(*query));
574 os_memcpy(query->addr, dst, ETH_ALEN);
575 query->dialog_token = dialog_token;
580 dl_list_add(&gas->pending, &query->list);
582 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
584 wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
585 " dialog_token %u", MAC2STR(dst), dialog_token);
587 eloop_register_timeout(0, 0, gas_service_timeout, gas, query);
594 * gas_query_cancel - Cancel a pending GAS query
595 * @gas: GAS query data from gas_query_init()
596 * @dst: Destination MAC address for the query
597 * @dialog_token: Dialog token from gas_query_req()
599 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
601 struct gas_query_pending *query;
603 query = gas_query_get_pending(gas, dst, dialog_token);
605 gas_query_done(gas, query, GAS_QUERY_CANCELLED);
610 int gas_query_in_progress(struct gas_query *gas)
612 return gas->current != NULL;