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;
46 struct wpabuf *adv_proto;
48 struct os_reltime last_oper;
49 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
50 enum gas_query_result result,
51 const struct wpabuf *adv_proto,
52 const struct wpabuf *resp, u16 status_code);
57 * struct gas_query - Internal GAS query data
60 struct wpa_supplicant *wpa_s;
61 struct dl_list pending; /* struct gas_query_pending */
62 struct gas_query_pending *current;
63 struct wpa_radio_work *work;
67 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
68 static void gas_query_timeout(void *eloop_data, void *user_ctx);
71 static int ms_from_time(struct os_reltime *last)
73 struct os_reltime now, res;
76 os_reltime_sub(&now, last, &res);
77 return res.sec * 1000 + res.usec / 1000;
82 * gas_query_init - Initialize GAS query component
83 * @wpa_s: Pointer to wpa_supplicant data
84 * Returns: Pointer to GAS query data or %NULL on failure
86 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
88 struct gas_query *gas;
90 gas = os_zalloc(sizeof(*gas));
95 dl_list_init(&gas->pending);
101 static const char * gas_result_txt(enum gas_query_result result)
104 case GAS_QUERY_SUCCESS:
106 case GAS_QUERY_FAILURE:
108 case GAS_QUERY_TIMEOUT:
110 case GAS_QUERY_PEER_ERROR:
112 case GAS_QUERY_INTERNAL_ERROR:
113 return "INTERNAL_ERROR";
114 case GAS_QUERY_CANCELLED:
116 case GAS_QUERY_DELETED_AT_DEINIT:
117 return "DELETED_AT_DEINIT";
124 static void gas_query_free(struct gas_query_pending *query, int del_list)
126 struct gas_query *gas = query->gas;
129 dl_list_del(&query->list);
131 if (gas->work && gas->work->ctx == query) {
132 radio_work_done(gas->work);
136 wpabuf_free(query->req);
137 wpabuf_free(query->adv_proto);
138 wpabuf_free(query->resp);
143 static void gas_query_done(struct gas_query *gas,
144 struct gas_query_pending *query,
145 enum gas_query_result result)
147 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
148 " dialog_token=%u freq=%d status_code=%u result=%s",
149 MAC2STR(query->addr), query->dialog_token, query->freq,
150 query->status_code, gas_result_txt(result));
151 if (gas->current == query)
153 if (query->offchannel_tx_started)
154 offchannel_send_action_done(gas->wpa_s);
155 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
156 eloop_cancel_timeout(gas_query_timeout, gas, query);
157 dl_list_del(&query->list);
158 query->cb(query->ctx, query->addr, query->dialog_token, result,
159 query->adv_proto, query->resp, query->status_code);
160 gas_query_free(query, 0);
165 * gas_query_deinit - Deinitialize GAS query component
166 * @gas: GAS query data from gas_query_init()
168 void gas_query_deinit(struct gas_query *gas)
170 struct gas_query_pending *query, *next;
175 dl_list_for_each_safe(query, next, &gas->pending,
176 struct gas_query_pending, list)
177 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
183 static struct gas_query_pending *
184 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
186 struct gas_query_pending *q;
187 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
188 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
189 q->dialog_token == dialog_token)
196 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
199 if (wpabuf_resize(&query->resp, len) < 0) {
200 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
203 wpabuf_put_data(query->resp, data, len);
208 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
209 unsigned int freq, const u8 *dst,
210 const u8 *src, const u8 *bssid,
211 const u8 *data, size_t data_len,
212 enum offchannel_send_action_result result)
214 struct gas_query_pending *query;
215 struct gas_query *gas = wpa_s->gas;
218 if (gas->current == NULL) {
219 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
220 MACSTR " result=%d - no query in progress",
221 freq, MAC2STR(dst), result);
225 query = gas->current;
227 dur = ms_from_time(&query->last_oper);
228 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
229 " result=%d query=%p dialog_token=%u dur=%d ms",
230 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
231 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
232 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
235 os_get_reltime(&query->last_oper);
237 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
238 eloop_cancel_timeout(gas_query_timeout, gas, query);
239 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
240 gas_query_timeout, gas, query);
242 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
243 eloop_cancel_timeout(gas_query_timeout, gas, query);
244 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
249 static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
251 if (wpa_s->current_ssid == NULL ||
252 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
253 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
255 return wpa_sm_pmf_enabled(wpa_s->wpa);
259 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
260 struct wpabuf *req, unsigned int wait_time)
262 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
264 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
265 "freq=%d prot=%d", MAC2STR(query->addr),
266 (unsigned int) wpabuf_len(req), query->freq, prot);
268 u8 *categ = wpabuf_mhead_u8(req);
269 *categ = WLAN_ACTION_PROTECTED_DUAL;
271 os_get_reltime(&query->last_oper);
272 if (gas->wpa_s->max_remain_on_chan &&
273 wait_time > gas->wpa_s->max_remain_on_chan)
274 wait_time = gas->wpa_s->max_remain_on_chan;
275 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
276 gas->wpa_s->own_addr, query->addr,
277 wpabuf_head(req), wpabuf_len(req),
278 wait_time, gas_query_tx_status, 0);
280 query->offchannel_tx_started = 1;
285 static void gas_query_tx_comeback_req(struct gas_query *gas,
286 struct gas_query_pending *query)
289 unsigned int wait_time;
291 req = gas_build_comeback_req(query->dialog_token);
293 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
297 wait_time = !query->offchannel_tx_started ?
298 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
300 if (gas_query_tx(gas, query, req, wait_time) < 0) {
301 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
302 MACSTR, MAC2STR(query->addr));
303 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
310 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
312 struct gas_query *gas = eloop_data;
313 struct gas_query_pending *query = user_ctx;
315 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
316 MAC2STR(query->addr));
317 gas_query_tx_comeback_req(gas, query);
321 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
322 struct gas_query_pending *query,
325 unsigned int secs, usecs;
327 if (query->offchannel_tx_started) {
328 offchannel_send_action_done(gas->wpa_s);
329 query->offchannel_tx_started = 0;
332 secs = (comeback_delay * 1024) / 1000000;
333 usecs = comeback_delay * 1024 - secs * 1000000;
334 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
335 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
336 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
337 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
342 static void gas_query_rx_initial(struct gas_query *gas,
343 struct gas_query_pending *query,
344 const u8 *adv_proto, const u8 *resp,
345 size_t len, u16 comeback_delay)
347 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
348 MACSTR " (dialog_token=%u comeback_delay=%u)",
349 MAC2STR(query->addr), query->dialog_token, comeback_delay);
351 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
352 if (query->adv_proto == NULL) {
353 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
357 if (comeback_delay) {
358 query->wait_comeback = 1;
359 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
363 /* Query was completed without comeback mechanism */
364 if (gas_query_append(query, resp, len) < 0) {
365 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
369 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
373 static void gas_query_rx_comeback(struct gas_query *gas,
374 struct gas_query_pending *query,
375 const u8 *adv_proto, const u8 *resp,
376 size_t len, u8 frag_id, u8 more_frags,
379 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
380 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
381 "comeback_delay=%u)",
382 MAC2STR(query->addr), query->dialog_token, frag_id,
383 more_frags, comeback_delay);
385 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
386 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
387 wpabuf_len(query->adv_proto)) != 0) {
388 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
389 "between initial and comeback response from "
390 MACSTR, MAC2STR(query->addr));
391 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
395 if (comeback_delay) {
397 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
398 "with non-zero frag_id and comeback_delay "
399 "from " MACSTR, MAC2STR(query->addr));
400 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
403 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
407 if (frag_id != query->next_frag_id) {
408 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
409 "from " MACSTR, MAC2STR(query->addr));
410 if (frag_id + 1 == query->next_frag_id) {
411 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
412 "retry of previous fragment");
415 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
418 query->next_frag_id++;
420 if (gas_query_append(query, resp, len) < 0) {
421 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
426 gas_query_tx_comeback_req(gas, query);
430 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
435 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
436 * @gas: GAS query data from gas_query_init()
437 * @da: Destination MAC address of the Action frame
438 * @sa: Source MAC address of the Action frame
439 * @bssid: BSSID of the Action frame
440 * @categ: Category of the Action frame
441 * @data: Payload of the Action frame
442 * @len: Length of @data
443 * @freq: Frequency (in MHz) on which the frame was received
444 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
446 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
447 const u8 *bssid, u8 categ, const u8 *data, size_t len,
450 struct gas_query_pending *query;
451 u8 action, dialog_token, frag_id = 0, more_frags = 0;
452 u16 comeback_delay, resp_len;
453 const u8 *pos, *adv_proto;
457 if (gas == NULL || len < 4)
460 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
461 pmf = pmf_in_use(gas->wpa_s, bssid);
463 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
467 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
473 dialog_token = *pos++;
475 if (action != WLAN_PA_GAS_INITIAL_RESP &&
476 action != WLAN_PA_GAS_COMEBACK_RESP)
477 return -1; /* Not a GAS response */
479 query = gas_query_get_pending(gas, sa, dialog_token);
481 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
482 " dialog token %u", MAC2STR(sa), dialog_token);
486 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
487 ms_from_time(&query->last_oper), MAC2STR(sa));
489 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
490 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
491 MACSTR " dialog token %u when waiting for comeback "
492 "response", MAC2STR(sa), dialog_token);
496 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
497 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
498 MACSTR " dialog token %u when waiting for initial "
499 "response", MAC2STR(sa), dialog_token);
503 query->status_code = WPA_GET_LE16(pos);
506 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
507 action == WLAN_PA_GAS_COMEBACK_RESP) {
508 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
509 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
510 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
511 "%u failed - status code %u",
512 MAC2STR(sa), dialog_token, query->status_code);
513 gas_query_done(gas, query, GAS_QUERY_FAILURE);
517 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
518 if (pos + 1 > data + len)
520 frag_id = *pos & 0x7f;
521 more_frags = (*pos & 0x80) >> 7;
526 if (pos + 2 > data + len)
528 comeback_delay = WPA_GET_LE16(pos);
531 /* Advertisement Protocol element */
532 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
533 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
534 "Protocol element in the response from " MACSTR,
539 if (*pos != WLAN_EID_ADV_PROTO) {
540 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
541 "Protocol element ID %u in response from " MACSTR,
549 /* Query Response Length */
550 if (pos + 2 > data + len) {
551 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
554 resp_len = WPA_GET_LE16(pos);
557 left = data + len - pos;
558 if (resp_len > left) {
559 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
560 "response from " MACSTR, MAC2STR(sa));
564 if (resp_len < left) {
565 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
566 "after Query Response from " MACSTR,
567 left - resp_len, MAC2STR(sa));
570 if (action == WLAN_PA_GAS_COMEBACK_RESP)
571 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
572 frag_id, more_frags, comeback_delay);
574 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
581 static void gas_query_timeout(void *eloop_data, void *user_ctx)
583 struct gas_query *gas = eloop_data;
584 struct gas_query_pending *query = user_ctx;
586 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
588 MAC2STR(query->addr), query->dialog_token);
589 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
593 static int gas_query_dialog_token_available(struct gas_query *gas,
594 const u8 *dst, u8 dialog_token)
596 struct gas_query_pending *q;
597 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
598 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
599 dialog_token == q->dialog_token)
607 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
609 struct gas_query_pending *query = work->ctx;
610 struct gas_query *gas = query->gas;
611 struct wpa_supplicant *wpa_s = gas->wpa_s;
616 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
620 gas_query_free(query, 1);
624 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
625 wpa_msg(wpa_s, MSG_INFO,
626 "Failed to assign random MAC address for GAS");
627 gas_query_free(query, 1);
628 radio_work_done(work);
634 if (gas_query_tx(gas, query, query->req,
635 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
636 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
637 MACSTR, MAC2STR(query->addr));
638 gas_query_free(query, 1);
641 gas->current = query;
643 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
644 query->dialog_token);
645 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
646 gas_query_timeout, gas, query);
652 * gas_query_req - Request a GAS query
653 * @gas: GAS query data from gas_query_init()
654 * @dst: Destination MAC address for the query
655 * @freq: Frequency (in MHz) for the channel on which to send the query
656 * @req: GAS query payload (to be freed by gas_query module in case of success
658 * @cb: Callback function for reporting GAS query result and response
659 * @ctx: Context pointer to use with the @cb call
660 * Returns: dialog token (>= 0) on success or -1 on failure
662 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
664 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
665 enum gas_query_result result,
666 const struct wpabuf *adv_proto,
667 const struct wpabuf *resp, u16 status_code),
670 struct gas_query_pending *query;
672 static int next_start = 0;
674 if (wpabuf_len(req) < 3)
677 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
678 if (gas_query_dialog_token_available(
679 gas, dst, (next_start + dialog_token) % 256))
682 if (dialog_token == 256)
683 return -1; /* Too many pending queries */
684 dialog_token = (next_start + dialog_token) % 256;
685 next_start = (dialog_token + 1) % 256;
687 query = os_zalloc(sizeof(*query));
692 os_memcpy(query->addr, dst, ETH_ALEN);
693 query->dialog_token = dialog_token;
698 dl_list_add(&gas->pending, &query->list);
700 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
702 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
703 " dialog_token=%u freq=%d",
704 MAC2STR(query->addr), query->dialog_token, query->freq);
706 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
708 gas_query_free(query, 1);
717 * gas_query_cancel - Cancel a pending GAS query
718 * @gas: GAS query data from gas_query_init()
719 * @dst: Destination MAC address for the query
720 * @dialog_token: Dialog token from gas_query_req()
722 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
724 struct gas_query_pending *query;
726 query = gas_query_get_pending(gas, dst, dialog_token);
728 gas_query_done(gas, query, GAS_QUERY_CANCELLED);