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
30 * struct gas_query_pending - Pending GAS query
32 struct gas_query_pending {
34 struct gas_query *gas;
38 unsigned int wait_comeback:1;
39 unsigned int offchannel_tx_started:1;
43 struct wpabuf *adv_proto;
45 struct os_reltime last_oper;
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);
68 static int ms_from_time(struct os_reltime *last)
70 struct os_reltime now, res;
73 os_reltime_sub(&now, last, &res);
74 return res.sec * 1000 + res.usec / 1000;
79 * gas_query_init - Initialize GAS query component
80 * @wpa_s: Pointer to wpa_supplicant data
81 * Returns: Pointer to GAS query data or %NULL on failure
83 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
85 struct gas_query *gas;
87 gas = os_zalloc(sizeof(*gas));
92 dl_list_init(&gas->pending);
98 static const char * gas_result_txt(enum gas_query_result result)
101 case GAS_QUERY_SUCCESS:
103 case GAS_QUERY_FAILURE:
105 case GAS_QUERY_TIMEOUT:
107 case GAS_QUERY_PEER_ERROR:
109 case GAS_QUERY_INTERNAL_ERROR:
110 return "INTERNAL_ERROR";
111 case GAS_QUERY_CANCELLED:
113 case GAS_QUERY_DELETED_AT_DEINIT:
114 return "DELETED_AT_DEINIT";
121 static void gas_query_free(struct gas_query_pending *query, int del_list)
123 struct gas_query *gas = query->gas;
126 dl_list_del(&query->list);
128 if (gas->work && gas->work->ctx == query) {
129 radio_work_done(gas->work);
133 wpabuf_free(query->req);
134 wpabuf_free(query->adv_proto);
135 wpabuf_free(query->resp);
140 static void gas_query_done(struct gas_query *gas,
141 struct gas_query_pending *query,
142 enum gas_query_result result)
144 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
145 " dialog_token=%u freq=%d status_code=%u result=%s",
146 MAC2STR(query->addr), query->dialog_token, query->freq,
147 query->status_code, gas_result_txt(result));
148 if (gas->current == query)
150 if (query->offchannel_tx_started)
151 offchannel_send_action_done(gas->wpa_s);
152 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
153 eloop_cancel_timeout(gas_query_timeout, gas, query);
154 dl_list_del(&query->list);
155 query->cb(query->ctx, query->addr, query->dialog_token, result,
156 query->adv_proto, query->resp, query->status_code);
157 gas_query_free(query, 0);
162 * gas_query_deinit - Deinitialize GAS query component
163 * @gas: GAS query data from gas_query_init()
165 void gas_query_deinit(struct gas_query *gas)
167 struct gas_query_pending *query, *next;
172 dl_list_for_each_safe(query, next, &gas->pending,
173 struct gas_query_pending, list)
174 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
180 static struct gas_query_pending *
181 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
183 struct gas_query_pending *q;
184 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
185 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
186 q->dialog_token == dialog_token)
193 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
196 if (wpabuf_resize(&query->resp, len) < 0) {
197 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
200 wpabuf_put_data(query->resp, data, len);
205 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
206 unsigned int freq, const u8 *dst,
207 const u8 *src, const u8 *bssid,
208 const u8 *data, size_t data_len,
209 enum offchannel_send_action_result result)
211 struct gas_query_pending *query;
212 struct gas_query *gas = wpa_s->gas;
215 if (gas->current == NULL) {
216 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
217 MACSTR " result=%d - no query in progress",
218 freq, MAC2STR(dst), result);
222 query = gas->current;
224 dur = ms_from_time(&query->last_oper);
225 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
226 " result=%d query=%p dialog_token=%u dur=%d ms",
227 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
228 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
229 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
232 os_get_reltime(&query->last_oper);
234 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
235 eloop_cancel_timeout(gas_query_timeout, gas, query);
236 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
237 gas_query_timeout, gas, query);
239 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
240 eloop_cancel_timeout(gas_query_timeout, gas, query);
241 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
246 static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
248 if (wpa_s->current_ssid == NULL ||
249 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
250 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
252 return wpa_sm_pmf_enabled(wpa_s->wpa);
256 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
259 unsigned int wait_time;
260 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
262 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
263 "freq=%d prot=%d", MAC2STR(query->addr),
264 (unsigned int) wpabuf_len(req), query->freq, prot);
266 u8 *categ = wpabuf_mhead_u8(req);
267 *categ = WLAN_ACTION_PROTECTED_DUAL;
269 os_get_reltime(&query->last_oper);
271 if (gas->wpa_s->max_remain_on_chan &&
272 wait_time > gas->wpa_s->max_remain_on_chan)
273 wait_time = gas->wpa_s->max_remain_on_chan;
274 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
275 gas->wpa_s->own_addr, query->addr,
276 wpabuf_head(req), wpabuf_len(req),
277 wait_time, gas_query_tx_status, 0);
279 query->offchannel_tx_started = 1;
284 static void gas_query_tx_comeback_req(struct gas_query *gas,
285 struct gas_query_pending *query)
289 req = gas_build_comeback_req(query->dialog_token);
291 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
295 if (gas_query_tx(gas, query, req) < 0) {
296 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
297 MACSTR, MAC2STR(query->addr));
298 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
305 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
307 struct gas_query *gas = eloop_data;
308 struct gas_query_pending *query = user_ctx;
310 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
311 MAC2STR(query->addr));
312 gas_query_tx_comeback_req(gas, query);
316 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
317 struct gas_query_pending *query,
320 unsigned int secs, usecs;
322 if (query->offchannel_tx_started) {
323 offchannel_send_action_done(gas->wpa_s);
324 query->offchannel_tx_started = 0;
327 secs = (comeback_delay * 1024) / 1000000;
328 usecs = comeback_delay * 1024 - secs * 1000000;
329 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
330 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
331 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
332 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
337 static void gas_query_rx_initial(struct gas_query *gas,
338 struct gas_query_pending *query,
339 const u8 *adv_proto, const u8 *resp,
340 size_t len, u16 comeback_delay)
342 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
343 MACSTR " (dialog_token=%u comeback_delay=%u)",
344 MAC2STR(query->addr), query->dialog_token, comeback_delay);
346 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
347 if (query->adv_proto == NULL) {
348 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
352 if (comeback_delay) {
353 query->wait_comeback = 1;
354 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
358 /* Query was completed without comeback mechanism */
359 if (gas_query_append(query, resp, len) < 0) {
360 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
364 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
368 static void gas_query_rx_comeback(struct gas_query *gas,
369 struct gas_query_pending *query,
370 const u8 *adv_proto, const u8 *resp,
371 size_t len, u8 frag_id, u8 more_frags,
374 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
375 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
376 "comeback_delay=%u)",
377 MAC2STR(query->addr), query->dialog_token, frag_id,
378 more_frags, comeback_delay);
380 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
381 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
382 wpabuf_len(query->adv_proto)) != 0) {
383 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
384 "between initial and comeback response from "
385 MACSTR, MAC2STR(query->addr));
386 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
390 if (comeback_delay) {
392 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
393 "with non-zero frag_id and comeback_delay "
394 "from " MACSTR, MAC2STR(query->addr));
395 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
398 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
402 if (frag_id != query->next_frag_id) {
403 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
404 "from " MACSTR, MAC2STR(query->addr));
405 if (frag_id + 1 == query->next_frag_id) {
406 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
407 "retry of previous fragment");
410 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
413 query->next_frag_id++;
415 if (gas_query_append(query, resp, len) < 0) {
416 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
421 gas_query_tx_comeback_req(gas, query);
425 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
430 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
431 * @gas: GAS query data from gas_query_init()
432 * @da: Destination MAC address of the Action frame
433 * @sa: Source MAC address of the Action frame
434 * @bssid: BSSID of the Action frame
435 * @categ: Category of the Action frame
436 * @data: Payload of the Action frame
437 * @len: Length of @data
438 * @freq: Frequency (in MHz) on which the frame was received
439 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
441 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
442 const u8 *bssid, u8 categ, const u8 *data, size_t len,
445 struct gas_query_pending *query;
446 u8 action, dialog_token, frag_id = 0, more_frags = 0;
447 u16 comeback_delay, resp_len;
448 const u8 *pos, *adv_proto;
452 if (gas == NULL || len < 4)
455 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
456 pmf = pmf_in_use(gas->wpa_s, bssid);
458 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
462 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
468 dialog_token = *pos++;
470 if (action != WLAN_PA_GAS_INITIAL_RESP &&
471 action != WLAN_PA_GAS_COMEBACK_RESP)
472 return -1; /* Not a GAS response */
474 query = gas_query_get_pending(gas, sa, dialog_token);
476 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
477 " dialog token %u", MAC2STR(sa), dialog_token);
481 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
482 ms_from_time(&query->last_oper), MAC2STR(sa));
484 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
485 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
486 MACSTR " dialog token %u when waiting for comeback "
487 "response", MAC2STR(sa), dialog_token);
491 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
492 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
493 MACSTR " dialog token %u when waiting for initial "
494 "response", MAC2STR(sa), dialog_token);
498 query->status_code = WPA_GET_LE16(pos);
501 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
502 action == WLAN_PA_GAS_COMEBACK_RESP) {
503 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
504 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
505 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
506 "%u failed - status code %u",
507 MAC2STR(sa), dialog_token, query->status_code);
508 gas_query_done(gas, query, GAS_QUERY_FAILURE);
512 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
513 if (pos + 1 > data + len)
515 frag_id = *pos & 0x7f;
516 more_frags = (*pos & 0x80) >> 7;
521 if (pos + 2 > data + len)
523 comeback_delay = WPA_GET_LE16(pos);
526 /* Advertisement Protocol element */
527 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
528 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
529 "Protocol element in the response from " MACSTR,
534 if (*pos != WLAN_EID_ADV_PROTO) {
535 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
536 "Protocol element ID %u in response from " MACSTR,
544 /* Query Response Length */
545 if (pos + 2 > data + len) {
546 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
549 resp_len = WPA_GET_LE16(pos);
552 left = data + len - pos;
553 if (resp_len > left) {
554 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
555 "response from " MACSTR, MAC2STR(sa));
559 if (resp_len < left) {
560 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
561 "after Query Response from " MACSTR,
562 left - resp_len, MAC2STR(sa));
565 if (action == WLAN_PA_GAS_COMEBACK_RESP)
566 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
567 frag_id, more_frags, comeback_delay);
569 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
576 static void gas_query_timeout(void *eloop_data, void *user_ctx)
578 struct gas_query *gas = eloop_data;
579 struct gas_query_pending *query = user_ctx;
581 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
583 MAC2STR(query->addr), query->dialog_token);
584 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
588 static int gas_query_dialog_token_available(struct gas_query *gas,
589 const u8 *dst, u8 dialog_token)
591 struct gas_query_pending *q;
592 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
593 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
594 dialog_token == q->dialog_token)
602 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
604 struct gas_query_pending *query = work->ctx;
605 struct gas_query *gas = query->gas;
606 struct wpa_supplicant *wpa_s = gas->wpa_s;
611 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
615 gas_query_free(query, 1);
619 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
620 wpa_msg(wpa_s, MSG_INFO,
621 "Failed to assign random MAC address for GAS");
622 gas_query_free(query, 1);
623 radio_work_done(work);
629 if (gas_query_tx(gas, query, query->req) < 0) {
630 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
631 MACSTR, MAC2STR(query->addr));
632 gas_query_free(query, 1);
635 gas->current = query;
637 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
638 query->dialog_token);
639 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
640 gas_query_timeout, gas, query);
646 * gas_query_req - Request a GAS query
647 * @gas: GAS query data from gas_query_init()
648 * @dst: Destination MAC address for the query
649 * @freq: Frequency (in MHz) for the channel on which to send the query
650 * @req: GAS query payload (to be freed by gas_query module in case of success
652 * @cb: Callback function for reporting GAS query result and response
653 * @ctx: Context pointer to use with the @cb call
654 * Returns: dialog token (>= 0) on success or -1 on failure
656 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
658 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
659 enum gas_query_result result,
660 const struct wpabuf *adv_proto,
661 const struct wpabuf *resp, u16 status_code),
664 struct gas_query_pending *query;
666 static int next_start = 0;
668 if (wpabuf_len(req) < 3)
671 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
672 if (gas_query_dialog_token_available(
673 gas, dst, (next_start + dialog_token) % 256))
676 if (dialog_token == 256)
677 return -1; /* Too many pending queries */
678 dialog_token = (next_start + dialog_token) % 256;
679 next_start = (dialog_token + 1) % 256;
681 query = os_zalloc(sizeof(*query));
686 os_memcpy(query->addr, dst, ETH_ALEN);
687 query->dialog_token = dialog_token;
692 dl_list_add(&gas->pending, &query->list);
694 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
696 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
697 " dialog_token=%u freq=%d",
698 MAC2STR(query->addr), query->dialog_token, query->freq);
700 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
702 gas_query_free(query, 1);
711 * gas_query_cancel - Cancel a pending GAS query
712 * @gas: GAS query data from gas_query_init()
713 * @dst: Destination MAC address for the query
714 * @dialog_token: Dialog token from gas_query_req()
716 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
718 struct gas_query_pending *query;
720 query = gas_query_get_pending(gas, dst, dialog_token);
722 gas_query_done(gas, query, GAS_QUERY_CANCELLED);