1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo * Generic advertisement service (GAS) query
3f05cddf9SRui Paulo * Copyright (c) 2009, Atheros Communications
45b9c547cSRui Paulo * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
55b9c547cSRui Paulo * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6f05cddf9SRui Paulo *
7f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
8f05cddf9SRui Paulo * See README for more details.
9f05cddf9SRui Paulo */
10f05cddf9SRui Paulo
11f05cddf9SRui Paulo #include "includes.h"
12f05cddf9SRui Paulo
13f05cddf9SRui Paulo #include "common.h"
14f05cddf9SRui Paulo #include "utils/eloop.h"
15f05cddf9SRui Paulo #include "common/ieee802_11_defs.h"
16f05cddf9SRui Paulo #include "common/gas.h"
175b9c547cSRui Paulo #include "common/wpa_ctrl.h"
185b9c547cSRui Paulo #include "rsn_supp/wpa.h"
19f05cddf9SRui Paulo #include "wpa_supplicant_i.h"
20780fb4a2SCy Schubert #include "config.h"
21f05cddf9SRui Paulo #include "driver_i.h"
22f05cddf9SRui Paulo #include "offchannel.h"
23f05cddf9SRui Paulo #include "gas_query.h"
24f05cddf9SRui Paulo
25f05cddf9SRui Paulo
26f05cddf9SRui Paulo /** GAS query timeout in seconds */
275b9c547cSRui Paulo #define GAS_QUERY_TIMEOUT_PERIOD 2
28f05cddf9SRui Paulo
29780fb4a2SCy Schubert /* GAS query wait-time / duration in ms */
30780fb4a2SCy Schubert #define GAS_QUERY_WAIT_TIME_INITIAL 1000
31780fb4a2SCy Schubert #define GAS_QUERY_WAIT_TIME_COMEBACK 150
32f05cddf9SRui Paulo
33*a90b9d01SCy Schubert #define GAS_QUERY_MAX_COMEBACK_DELAY 60000
34*a90b9d01SCy Schubert
35f05cddf9SRui Paulo /**
36f05cddf9SRui Paulo * struct gas_query_pending - Pending GAS query
37f05cddf9SRui Paulo */
38f05cddf9SRui Paulo struct gas_query_pending {
39f05cddf9SRui Paulo struct dl_list list;
405b9c547cSRui Paulo struct gas_query *gas;
41f05cddf9SRui Paulo u8 addr[ETH_ALEN];
42f05cddf9SRui Paulo u8 dialog_token;
43f05cddf9SRui Paulo u8 next_frag_id;
44f05cddf9SRui Paulo unsigned int wait_comeback:1;
45f05cddf9SRui Paulo unsigned int offchannel_tx_started:1;
46780fb4a2SCy Schubert unsigned int retry:1;
4785732ac8SCy Schubert unsigned int wildcard_bssid:1;
48c1d255d3SCy Schubert unsigned int maintain_addr:1;
49f05cddf9SRui Paulo int freq;
50f05cddf9SRui Paulo u16 status_code;
515b9c547cSRui Paulo struct wpabuf *req;
52f05cddf9SRui Paulo struct wpabuf *adv_proto;
53f05cddf9SRui Paulo struct wpabuf *resp;
545b9c547cSRui Paulo struct os_reltime last_oper;
55f05cddf9SRui Paulo void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
56f05cddf9SRui Paulo enum gas_query_result result,
57f05cddf9SRui Paulo const struct wpabuf *adv_proto,
58f05cddf9SRui Paulo const struct wpabuf *resp, u16 status_code);
59f05cddf9SRui Paulo void *ctx;
6085732ac8SCy Schubert u8 sa[ETH_ALEN];
61f05cddf9SRui Paulo };
62f05cddf9SRui Paulo
63f05cddf9SRui Paulo /**
64f05cddf9SRui Paulo * struct gas_query - Internal GAS query data
65f05cddf9SRui Paulo */
66f05cddf9SRui Paulo struct gas_query {
67f05cddf9SRui Paulo struct wpa_supplicant *wpa_s;
68f05cddf9SRui Paulo struct dl_list pending; /* struct gas_query_pending */
695b9c547cSRui Paulo struct gas_query_pending *current;
705b9c547cSRui Paulo struct wpa_radio_work *work;
7185732ac8SCy Schubert struct os_reltime last_mac_addr_rand;
7285732ac8SCy Schubert int last_rand_sa_type;
7385732ac8SCy Schubert u8 rand_addr[ETH_ALEN];
74f05cddf9SRui Paulo };
75f05cddf9SRui Paulo
76f05cddf9SRui Paulo
77f05cddf9SRui Paulo static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
78f05cddf9SRui Paulo static void gas_query_timeout(void *eloop_data, void *user_ctx);
79780fb4a2SCy Schubert static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
80780fb4a2SCy Schubert static void gas_query_tx_initial_req(struct gas_query *gas,
81780fb4a2SCy Schubert struct gas_query_pending *query);
82780fb4a2SCy Schubert static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
83f05cddf9SRui Paulo
84f05cddf9SRui Paulo
ms_from_time(struct os_reltime * last)855b9c547cSRui Paulo static int ms_from_time(struct os_reltime *last)
865b9c547cSRui Paulo {
875b9c547cSRui Paulo struct os_reltime now, res;
885b9c547cSRui Paulo
895b9c547cSRui Paulo os_get_reltime(&now);
905b9c547cSRui Paulo os_reltime_sub(&now, last, &res);
915b9c547cSRui Paulo return res.sec * 1000 + res.usec / 1000;
925b9c547cSRui Paulo }
935b9c547cSRui Paulo
945b9c547cSRui Paulo
95f05cddf9SRui Paulo /**
96f05cddf9SRui Paulo * gas_query_init - Initialize GAS query component
97f05cddf9SRui Paulo * @wpa_s: Pointer to wpa_supplicant data
98f05cddf9SRui Paulo * Returns: Pointer to GAS query data or %NULL on failure
99f05cddf9SRui Paulo */
gas_query_init(struct wpa_supplicant * wpa_s)100f05cddf9SRui Paulo struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
101f05cddf9SRui Paulo {
102f05cddf9SRui Paulo struct gas_query *gas;
103f05cddf9SRui Paulo
104f05cddf9SRui Paulo gas = os_zalloc(sizeof(*gas));
105f05cddf9SRui Paulo if (gas == NULL)
106f05cddf9SRui Paulo return NULL;
107f05cddf9SRui Paulo
108f05cddf9SRui Paulo gas->wpa_s = wpa_s;
109f05cddf9SRui Paulo dl_list_init(&gas->pending);
110f05cddf9SRui Paulo
111f05cddf9SRui Paulo return gas;
112f05cddf9SRui Paulo }
113f05cddf9SRui Paulo
114f05cddf9SRui Paulo
gas_result_txt(enum gas_query_result result)1155b9c547cSRui Paulo static const char * gas_result_txt(enum gas_query_result result)
1165b9c547cSRui Paulo {
1175b9c547cSRui Paulo switch (result) {
1185b9c547cSRui Paulo case GAS_QUERY_SUCCESS:
1195b9c547cSRui Paulo return "SUCCESS";
1205b9c547cSRui Paulo case GAS_QUERY_FAILURE:
1215b9c547cSRui Paulo return "FAILURE";
1225b9c547cSRui Paulo case GAS_QUERY_TIMEOUT:
1235b9c547cSRui Paulo return "TIMEOUT";
1245b9c547cSRui Paulo case GAS_QUERY_PEER_ERROR:
1255b9c547cSRui Paulo return "PEER_ERROR";
1265b9c547cSRui Paulo case GAS_QUERY_INTERNAL_ERROR:
1275b9c547cSRui Paulo return "INTERNAL_ERROR";
12885732ac8SCy Schubert case GAS_QUERY_STOPPED:
12985732ac8SCy Schubert return "STOPPED";
1305b9c547cSRui Paulo case GAS_QUERY_DELETED_AT_DEINIT:
1315b9c547cSRui Paulo return "DELETED_AT_DEINIT";
1325b9c547cSRui Paulo }
1335b9c547cSRui Paulo
1345b9c547cSRui Paulo return "N/A";
1355b9c547cSRui Paulo }
1365b9c547cSRui Paulo
1375b9c547cSRui Paulo
gas_query_free(struct gas_query_pending * query,int del_list)1385b9c547cSRui Paulo static void gas_query_free(struct gas_query_pending *query, int del_list)
1395b9c547cSRui Paulo {
1405b9c547cSRui Paulo struct gas_query *gas = query->gas;
1415b9c547cSRui Paulo
1425b9c547cSRui Paulo if (del_list)
1435b9c547cSRui Paulo dl_list_del(&query->list);
1445b9c547cSRui Paulo
1455b9c547cSRui Paulo if (gas->work && gas->work->ctx == query) {
1465b9c547cSRui Paulo radio_work_done(gas->work);
1475b9c547cSRui Paulo gas->work = NULL;
1485b9c547cSRui Paulo }
1495b9c547cSRui Paulo
1505b9c547cSRui Paulo wpabuf_free(query->req);
1515b9c547cSRui Paulo wpabuf_free(query->adv_proto);
1525b9c547cSRui Paulo wpabuf_free(query->resp);
1535b9c547cSRui Paulo os_free(query);
1545b9c547cSRui Paulo }
1555b9c547cSRui Paulo
1565b9c547cSRui Paulo
gas_query_done(struct gas_query * gas,struct gas_query_pending * query,enum gas_query_result result)157f05cddf9SRui Paulo static void gas_query_done(struct gas_query *gas,
158f05cddf9SRui Paulo struct gas_query_pending *query,
159f05cddf9SRui Paulo enum gas_query_result result)
160f05cddf9SRui Paulo {
1615b9c547cSRui Paulo wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
1625b9c547cSRui Paulo " dialog_token=%u freq=%d status_code=%u result=%s",
1635b9c547cSRui Paulo MAC2STR(query->addr), query->dialog_token, query->freq,
1645b9c547cSRui Paulo query->status_code, gas_result_txt(result));
1655b9c547cSRui Paulo if (gas->current == query)
1665b9c547cSRui Paulo gas->current = NULL;
167f05cddf9SRui Paulo if (query->offchannel_tx_started)
168f05cddf9SRui Paulo offchannel_send_action_done(gas->wpa_s);
169f05cddf9SRui Paulo eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
170f05cddf9SRui Paulo eloop_cancel_timeout(gas_query_timeout, gas, query);
171780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
172f05cddf9SRui Paulo dl_list_del(&query->list);
173f05cddf9SRui Paulo query->cb(query->ctx, query->addr, query->dialog_token, result,
174f05cddf9SRui Paulo query->adv_proto, query->resp, query->status_code);
1755b9c547cSRui Paulo gas_query_free(query, 0);
176f05cddf9SRui Paulo }
177f05cddf9SRui Paulo
178f05cddf9SRui Paulo
179f05cddf9SRui Paulo /**
180f05cddf9SRui Paulo * gas_query_deinit - Deinitialize GAS query component
181f05cddf9SRui Paulo * @gas: GAS query data from gas_query_init()
182f05cddf9SRui Paulo */
gas_query_deinit(struct gas_query * gas)183f05cddf9SRui Paulo void gas_query_deinit(struct gas_query *gas)
184f05cddf9SRui Paulo {
185f05cddf9SRui Paulo struct gas_query_pending *query, *next;
186f05cddf9SRui Paulo
187f05cddf9SRui Paulo if (gas == NULL)
188f05cddf9SRui Paulo return;
189f05cddf9SRui Paulo
190f05cddf9SRui Paulo dl_list_for_each_safe(query, next, &gas->pending,
191f05cddf9SRui Paulo struct gas_query_pending, list)
192f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
193f05cddf9SRui Paulo
194f05cddf9SRui Paulo os_free(gas);
195f05cddf9SRui Paulo }
196f05cddf9SRui Paulo
197f05cddf9SRui Paulo
198f05cddf9SRui Paulo static struct gas_query_pending *
gas_query_get_pending(struct gas_query * gas,const u8 * addr,u8 dialog_token)199f05cddf9SRui Paulo gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
200f05cddf9SRui Paulo {
201f05cddf9SRui Paulo struct gas_query_pending *q;
202*a90b9d01SCy Schubert struct wpa_supplicant *wpa_s = gas->wpa_s;
203*a90b9d01SCy Schubert
204f05cddf9SRui Paulo dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
205*a90b9d01SCy Schubert if (ether_addr_equal(q->addr, addr) &&
206f05cddf9SRui Paulo q->dialog_token == dialog_token)
207f05cddf9SRui Paulo return q;
208*a90b9d01SCy Schubert if (wpa_s->valid_links &&
209*a90b9d01SCy Schubert ether_addr_equal(wpa_s->ap_mld_addr, addr) &&
210*a90b9d01SCy Schubert wpas_ap_link_address(wpa_s, q->addr))
211*a90b9d01SCy Schubert return q;
212f05cddf9SRui Paulo }
213f05cddf9SRui Paulo return NULL;
214f05cddf9SRui Paulo }
215f05cddf9SRui Paulo
216f05cddf9SRui Paulo
gas_query_append(struct gas_query_pending * query,const u8 * data,size_t len)217f05cddf9SRui Paulo static int gas_query_append(struct gas_query_pending *query, const u8 *data,
218f05cddf9SRui Paulo size_t len)
219f05cddf9SRui Paulo {
220f05cddf9SRui Paulo if (wpabuf_resize(&query->resp, len) < 0) {
221f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
222f05cddf9SRui Paulo return -1;
223f05cddf9SRui Paulo }
224f05cddf9SRui Paulo wpabuf_put_data(query->resp, data, len);
225f05cddf9SRui Paulo return 0;
226f05cddf9SRui Paulo }
227f05cddf9SRui Paulo
228f05cddf9SRui Paulo
gas_query_tx_status(struct wpa_supplicant * wpa_s,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,enum offchannel_send_action_result result)2295b9c547cSRui Paulo static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
2305b9c547cSRui Paulo unsigned int freq, const u8 *dst,
2315b9c547cSRui Paulo const u8 *src, const u8 *bssid,
2325b9c547cSRui Paulo const u8 *data, size_t data_len,
2335b9c547cSRui Paulo enum offchannel_send_action_result result)
2345b9c547cSRui Paulo {
2355b9c547cSRui Paulo struct gas_query_pending *query;
2365b9c547cSRui Paulo struct gas_query *gas = wpa_s->gas;
2375b9c547cSRui Paulo int dur;
2385b9c547cSRui Paulo
2395b9c547cSRui Paulo if (gas->current == NULL) {
2405b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
2415b9c547cSRui Paulo MACSTR " result=%d - no query in progress",
2425b9c547cSRui Paulo freq, MAC2STR(dst), result);
2435b9c547cSRui Paulo return;
2445b9c547cSRui Paulo }
2455b9c547cSRui Paulo
2465b9c547cSRui Paulo query = gas->current;
2475b9c547cSRui Paulo
2485b9c547cSRui Paulo dur = ms_from_time(&query->last_oper);
2495b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
2505b9c547cSRui Paulo " result=%d query=%p dialog_token=%u dur=%d ms",
2515b9c547cSRui Paulo freq, MAC2STR(dst), result, query, query->dialog_token, dur);
252*a90b9d01SCy Schubert if (!ether_addr_equal(dst, query->addr)) {
2535b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
2545b9c547cSRui Paulo return;
2555b9c547cSRui Paulo }
2565b9c547cSRui Paulo os_get_reltime(&query->last_oper);
2575b9c547cSRui Paulo
25885732ac8SCy Schubert if (result == OFFCHANNEL_SEND_ACTION_SUCCESS ||
25985732ac8SCy Schubert result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
2605b9c547cSRui Paulo eloop_cancel_timeout(gas_query_timeout, gas, query);
26185732ac8SCy Schubert if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
26285732ac8SCy Schubert wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
26385732ac8SCy Schubert eloop_register_timeout(0, 250000,
26485732ac8SCy Schubert gas_query_timeout, gas, query);
26585732ac8SCy Schubert } else {
2665b9c547cSRui Paulo eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
2675b9c547cSRui Paulo gas_query_timeout, gas, query);
26885732ac8SCy Schubert }
269780fb4a2SCy Schubert if (query->wait_comeback && !query->retry) {
270780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_rx_comeback_timeout,
271780fb4a2SCy Schubert gas, query);
272780fb4a2SCy Schubert eloop_register_timeout(
273780fb4a2SCy Schubert 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
274780fb4a2SCy Schubert gas_query_rx_comeback_timeout, gas, query);
275780fb4a2SCy Schubert }
2765b9c547cSRui Paulo }
2775b9c547cSRui Paulo if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
2785b9c547cSRui Paulo eloop_cancel_timeout(gas_query_timeout, gas, query);
2795b9c547cSRui Paulo eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
2805b9c547cSRui Paulo }
2815b9c547cSRui Paulo }
2825b9c547cSRui Paulo
2835b9c547cSRui Paulo
gas_query_tx(struct gas_query * gas,struct gas_query_pending * query,struct wpabuf * req,unsigned int wait_time)284f05cddf9SRui Paulo static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
285780fb4a2SCy Schubert struct wpabuf *req, unsigned int wait_time)
286f05cddf9SRui Paulo {
2875b9c547cSRui Paulo int res, prot = pmf_in_use(gas->wpa_s, query->addr);
288780fb4a2SCy Schubert const u8 *bssid;
289780fb4a2SCy Schubert const u8 wildcard_bssid[ETH_ALEN] = {
290780fb4a2SCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
291780fb4a2SCy Schubert };
2925b9c547cSRui Paulo
293f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
29485732ac8SCy Schubert "freq=%d prot=%d using src addr " MACSTR,
29585732ac8SCy Schubert MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
29685732ac8SCy Schubert query->freq, prot, MAC2STR(query->sa));
2975b9c547cSRui Paulo if (prot) {
2985b9c547cSRui Paulo u8 *categ = wpabuf_mhead_u8(req);
2995b9c547cSRui Paulo *categ = WLAN_ACTION_PROTECTED_DUAL;
3005b9c547cSRui Paulo }
3015b9c547cSRui Paulo os_get_reltime(&query->last_oper);
3025b9c547cSRui Paulo if (gas->wpa_s->max_remain_on_chan &&
3035b9c547cSRui Paulo wait_time > gas->wpa_s->max_remain_on_chan)
3045b9c547cSRui Paulo wait_time = gas->wpa_s->max_remain_on_chan;
30585732ac8SCy Schubert if (!query->wildcard_bssid &&
30685732ac8SCy Schubert (!gas->wpa_s->conf->gas_address3 ||
307780fb4a2SCy Schubert (gas->wpa_s->current_ssid &&
308780fb4a2SCy Schubert gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
309*a90b9d01SCy Schubert ether_addr_equal(query->addr, gas->wpa_s->bssid))))
310780fb4a2SCy Schubert bssid = query->addr;
311780fb4a2SCy Schubert else
312780fb4a2SCy Schubert bssid = wildcard_bssid;
31385732ac8SCy Schubert
314f05cddf9SRui Paulo res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
31585732ac8SCy Schubert query->sa, bssid, wpabuf_head(req),
31685732ac8SCy Schubert wpabuf_len(req), wait_time,
31785732ac8SCy Schubert gas_query_tx_status, 0);
31885732ac8SCy Schubert
319f05cddf9SRui Paulo if (res == 0)
320f05cddf9SRui Paulo query->offchannel_tx_started = 1;
321f05cddf9SRui Paulo return res;
322f05cddf9SRui Paulo }
323f05cddf9SRui Paulo
324f05cddf9SRui Paulo
gas_query_tx_comeback_req(struct gas_query * gas,struct gas_query_pending * query)325f05cddf9SRui Paulo static void gas_query_tx_comeback_req(struct gas_query *gas,
326f05cddf9SRui Paulo struct gas_query_pending *query)
327f05cddf9SRui Paulo {
328f05cddf9SRui Paulo struct wpabuf *req;
329780fb4a2SCy Schubert unsigned int wait_time;
330f05cddf9SRui Paulo
331f05cddf9SRui Paulo req = gas_build_comeback_req(query->dialog_token);
332f05cddf9SRui Paulo if (req == NULL) {
333f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
334f05cddf9SRui Paulo return;
335f05cddf9SRui Paulo }
336f05cddf9SRui Paulo
337780fb4a2SCy Schubert wait_time = (query->retry || !query->offchannel_tx_started) ?
338780fb4a2SCy Schubert GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
339780fb4a2SCy Schubert
340780fb4a2SCy Schubert if (gas_query_tx(gas, query, req, wait_time) < 0) {
341f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
342f05cddf9SRui Paulo MACSTR, MAC2STR(query->addr));
343f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
344f05cddf9SRui Paulo }
345f05cddf9SRui Paulo
346f05cddf9SRui Paulo wpabuf_free(req);
347f05cddf9SRui Paulo }
348f05cddf9SRui Paulo
349f05cddf9SRui Paulo
gas_query_rx_comeback_timeout(void * eloop_data,void * user_ctx)350780fb4a2SCy Schubert static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
351780fb4a2SCy Schubert {
352780fb4a2SCy Schubert struct gas_query *gas = eloop_data;
353780fb4a2SCy Schubert struct gas_query_pending *query = user_ctx;
354780fb4a2SCy Schubert int dialog_token;
355780fb4a2SCy Schubert
356780fb4a2SCy Schubert wpa_printf(MSG_DEBUG,
357780fb4a2SCy Schubert "GAS: No response to comeback request received (retry=%u)",
358780fb4a2SCy Schubert query->retry);
359780fb4a2SCy Schubert if (gas->current != query || query->retry)
360780fb4a2SCy Schubert return;
361780fb4a2SCy Schubert dialog_token = gas_query_new_dialog_token(gas, query->addr);
362780fb4a2SCy Schubert if (dialog_token < 0)
363780fb4a2SCy Schubert return;
364780fb4a2SCy Schubert wpa_printf(MSG_DEBUG,
365780fb4a2SCy Schubert "GAS: Retry GAS query due to comeback response timeout");
366780fb4a2SCy Schubert query->retry = 1;
367780fb4a2SCy Schubert query->dialog_token = dialog_token;
368780fb4a2SCy Schubert *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
369780fb4a2SCy Schubert query->wait_comeback = 0;
370780fb4a2SCy Schubert query->next_frag_id = 0;
371780fb4a2SCy Schubert wpabuf_free(query->adv_proto);
372780fb4a2SCy Schubert query->adv_proto = NULL;
373780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
374780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_timeout, gas, query);
375780fb4a2SCy Schubert gas_query_tx_initial_req(gas, query);
376780fb4a2SCy Schubert }
377780fb4a2SCy Schubert
378780fb4a2SCy Schubert
gas_query_tx_comeback_timeout(void * eloop_data,void * user_ctx)379f05cddf9SRui Paulo static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
380f05cddf9SRui Paulo {
381f05cddf9SRui Paulo struct gas_query *gas = eloop_data;
382f05cddf9SRui Paulo struct gas_query_pending *query = user_ctx;
383f05cddf9SRui Paulo
384f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
385f05cddf9SRui Paulo MAC2STR(query->addr));
386f05cddf9SRui Paulo gas_query_tx_comeback_req(gas, query);
387f05cddf9SRui Paulo }
388f05cddf9SRui Paulo
389f05cddf9SRui Paulo
gas_query_tx_comeback_req_delay(struct gas_query * gas,struct gas_query_pending * query,u16 comeback_delay)390f05cddf9SRui Paulo static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
391f05cddf9SRui Paulo struct gas_query_pending *query,
392f05cddf9SRui Paulo u16 comeback_delay)
393f05cddf9SRui Paulo {
394f05cddf9SRui Paulo unsigned int secs, usecs;
395f05cddf9SRui Paulo
396780fb4a2SCy Schubert if (comeback_delay > 1 && query->offchannel_tx_started) {
397780fb4a2SCy Schubert offchannel_send_action_done(gas->wpa_s);
398780fb4a2SCy Schubert query->offchannel_tx_started = 0;
399780fb4a2SCy Schubert }
400780fb4a2SCy Schubert
401f05cddf9SRui Paulo secs = (comeback_delay * 1024) / 1000000;
402f05cddf9SRui Paulo usecs = comeback_delay * 1024 - secs * 1000000;
403f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
404f05cddf9SRui Paulo " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
405f05cddf9SRui Paulo eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
406f05cddf9SRui Paulo eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
407f05cddf9SRui Paulo gas, query);
408f05cddf9SRui Paulo }
409f05cddf9SRui Paulo
410f05cddf9SRui Paulo
gas_query_rx_initial(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,size_t adv_proto_len,const u8 * resp,size_t len,u16 comeback_delay)411f05cddf9SRui Paulo static void gas_query_rx_initial(struct gas_query *gas,
412f05cddf9SRui Paulo struct gas_query_pending *query,
413*a90b9d01SCy Schubert const u8 *adv_proto, size_t adv_proto_len,
414*a90b9d01SCy Schubert const u8 *resp, size_t len, u16 comeback_delay)
415f05cddf9SRui Paulo {
416f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
417f05cddf9SRui Paulo MACSTR " (dialog_token=%u comeback_delay=%u)",
418f05cddf9SRui Paulo MAC2STR(query->addr), query->dialog_token, comeback_delay);
419f05cddf9SRui Paulo
420*a90b9d01SCy Schubert query->adv_proto = wpabuf_alloc_copy(adv_proto, adv_proto_len);
421f05cddf9SRui Paulo if (query->adv_proto == NULL) {
422f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
423f05cddf9SRui Paulo return;
424f05cddf9SRui Paulo }
425f05cddf9SRui Paulo
426f05cddf9SRui Paulo if (comeback_delay) {
42785732ac8SCy Schubert eloop_cancel_timeout(gas_query_timeout, gas, query);
428f05cddf9SRui Paulo query->wait_comeback = 1;
429f05cddf9SRui Paulo gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
430f05cddf9SRui Paulo return;
431f05cddf9SRui Paulo }
432f05cddf9SRui Paulo
433f05cddf9SRui Paulo /* Query was completed without comeback mechanism */
434f05cddf9SRui Paulo if (gas_query_append(query, resp, len) < 0) {
435f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
436f05cddf9SRui Paulo return;
437f05cddf9SRui Paulo }
438f05cddf9SRui Paulo
439f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_SUCCESS);
440f05cddf9SRui Paulo }
441f05cddf9SRui Paulo
442f05cddf9SRui Paulo
gas_query_rx_comeback(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,size_t adv_proto_len,const u8 * resp,size_t len,u8 frag_id,u8 more_frags,u16 comeback_delay)443f05cddf9SRui Paulo static void gas_query_rx_comeback(struct gas_query *gas,
444f05cddf9SRui Paulo struct gas_query_pending *query,
445*a90b9d01SCy Schubert const u8 *adv_proto, size_t adv_proto_len,
446*a90b9d01SCy Schubert const u8 *resp, size_t len, u8 frag_id,
447*a90b9d01SCy Schubert u8 more_frags, u16 comeback_delay)
448f05cddf9SRui Paulo {
449f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
450f05cddf9SRui Paulo MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
451f05cddf9SRui Paulo "comeback_delay=%u)",
452f05cddf9SRui Paulo MAC2STR(query->addr), query->dialog_token, frag_id,
453f05cddf9SRui Paulo more_frags, comeback_delay);
454780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
455f05cddf9SRui Paulo
456*a90b9d01SCy Schubert if (adv_proto_len != wpabuf_len(query->adv_proto) ||
457f05cddf9SRui Paulo os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
458f05cddf9SRui Paulo wpabuf_len(query->adv_proto)) != 0) {
459f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
460f05cddf9SRui Paulo "between initial and comeback response from "
461f05cddf9SRui Paulo MACSTR, MAC2STR(query->addr));
462f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
463f05cddf9SRui Paulo return;
464f05cddf9SRui Paulo }
465f05cddf9SRui Paulo
466f05cddf9SRui Paulo if (comeback_delay) {
467f05cddf9SRui Paulo if (frag_id) {
468f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
469f05cddf9SRui Paulo "with non-zero frag_id and comeback_delay "
470f05cddf9SRui Paulo "from " MACSTR, MAC2STR(query->addr));
471f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
472f05cddf9SRui Paulo return;
473f05cddf9SRui Paulo }
474f05cddf9SRui Paulo gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
475f05cddf9SRui Paulo return;
476f05cddf9SRui Paulo }
477f05cddf9SRui Paulo
478f05cddf9SRui Paulo if (frag_id != query->next_frag_id) {
479f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
480f05cddf9SRui Paulo "from " MACSTR, MAC2STR(query->addr));
4815b9c547cSRui Paulo if (frag_id + 1 == query->next_frag_id) {
4825b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
4835b9c547cSRui Paulo "retry of previous fragment");
4845b9c547cSRui Paulo return;
4855b9c547cSRui Paulo }
486f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
487f05cddf9SRui Paulo return;
488f05cddf9SRui Paulo }
489f05cddf9SRui Paulo query->next_frag_id++;
490f05cddf9SRui Paulo
491f05cddf9SRui Paulo if (gas_query_append(query, resp, len) < 0) {
492f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
493f05cddf9SRui Paulo return;
494f05cddf9SRui Paulo }
495f05cddf9SRui Paulo
496f05cddf9SRui Paulo if (more_frags) {
497f05cddf9SRui Paulo gas_query_tx_comeback_req(gas, query);
498f05cddf9SRui Paulo return;
499f05cddf9SRui Paulo }
500f05cddf9SRui Paulo
501f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_SUCCESS);
502f05cddf9SRui Paulo }
503f05cddf9SRui Paulo
504f05cddf9SRui Paulo
505f05cddf9SRui Paulo /**
5065b9c547cSRui Paulo * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
507f05cddf9SRui Paulo * @gas: GAS query data from gas_query_init()
508f05cddf9SRui Paulo * @da: Destination MAC address of the Action frame
509f05cddf9SRui Paulo * @sa: Source MAC address of the Action frame
510f05cddf9SRui Paulo * @bssid: BSSID of the Action frame
5115b9c547cSRui Paulo * @categ: Category of the Action frame
512f05cddf9SRui Paulo * @data: Payload of the Action frame
513f05cddf9SRui Paulo * @len: Length of @data
514f05cddf9SRui Paulo * @freq: Frequency (in MHz) on which the frame was received
515f05cddf9SRui Paulo * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
516f05cddf9SRui Paulo */
gas_query_rx(struct gas_query * gas,const u8 * da,const u8 * sa,const u8 * bssid,u8 categ,const u8 * data,size_t len,int freq)517f05cddf9SRui Paulo int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
5185b9c547cSRui Paulo const u8 *bssid, u8 categ, const u8 *data, size_t len,
5195b9c547cSRui Paulo int freq)
520f05cddf9SRui Paulo {
521f05cddf9SRui Paulo struct gas_query_pending *query;
522f05cddf9SRui Paulo u8 action, dialog_token, frag_id = 0, more_frags = 0;
523f05cddf9SRui Paulo u16 comeback_delay, resp_len;
524f05cddf9SRui Paulo const u8 *pos, *adv_proto;
525*a90b9d01SCy Schubert size_t adv_proto_len;
5265b9c547cSRui Paulo int prot, pmf;
5275b9c547cSRui Paulo unsigned int left;
528f05cddf9SRui Paulo
529f05cddf9SRui Paulo if (gas == NULL || len < 4)
530f05cddf9SRui Paulo return -1;
531f05cddf9SRui Paulo
532780fb4a2SCy Schubert pos = data;
533780fb4a2SCy Schubert action = *pos++;
534780fb4a2SCy Schubert dialog_token = *pos++;
535780fb4a2SCy Schubert
536780fb4a2SCy Schubert if (action != WLAN_PA_GAS_INITIAL_RESP &&
537780fb4a2SCy Schubert action != WLAN_PA_GAS_COMEBACK_RESP)
538780fb4a2SCy Schubert return -1; /* Not a GAS response */
539780fb4a2SCy Schubert
5405b9c547cSRui Paulo prot = categ == WLAN_ACTION_PROTECTED_DUAL;
541780fb4a2SCy Schubert pmf = pmf_in_use(gas->wpa_s, sa);
5425b9c547cSRui Paulo if (prot && !pmf) {
5435b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
5445b9c547cSRui Paulo return 0;
5455b9c547cSRui Paulo }
5465b9c547cSRui Paulo if (!prot && pmf) {
5475b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
5485b9c547cSRui Paulo return 0;
5495b9c547cSRui Paulo }
5505b9c547cSRui Paulo
551f05cddf9SRui Paulo query = gas_query_get_pending(gas, sa, dialog_token);
552f05cddf9SRui Paulo if (query == NULL) {
553f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
554f05cddf9SRui Paulo " dialog token %u", MAC2STR(sa), dialog_token);
555f05cddf9SRui Paulo return -1;
556f05cddf9SRui Paulo }
557f05cddf9SRui Paulo
5585b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
5595b9c547cSRui Paulo ms_from_time(&query->last_oper), MAC2STR(sa));
5605b9c547cSRui Paulo
561f05cddf9SRui Paulo if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
562f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
563f05cddf9SRui Paulo MACSTR " dialog token %u when waiting for comeback "
564f05cddf9SRui Paulo "response", MAC2STR(sa), dialog_token);
565f05cddf9SRui Paulo return 0;
566f05cddf9SRui Paulo }
567f05cddf9SRui Paulo
568f05cddf9SRui Paulo if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
569f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
570f05cddf9SRui Paulo MACSTR " dialog token %u when waiting for initial "
571f05cddf9SRui Paulo "response", MAC2STR(sa), dialog_token);
572f05cddf9SRui Paulo return 0;
573f05cddf9SRui Paulo }
574f05cddf9SRui Paulo
575f05cddf9SRui Paulo query->status_code = WPA_GET_LE16(pos);
576f05cddf9SRui Paulo pos += 2;
577f05cddf9SRui Paulo
5785b9c547cSRui Paulo if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
5795b9c547cSRui Paulo action == WLAN_PA_GAS_COMEBACK_RESP) {
5805b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
5815b9c547cSRui Paulo } else if (query->status_code != WLAN_STATUS_SUCCESS) {
582f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
583f05cddf9SRui Paulo "%u failed - status code %u",
584f05cddf9SRui Paulo MAC2STR(sa), dialog_token, query->status_code);
585f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_FAILURE);
586f05cddf9SRui Paulo return 0;
587f05cddf9SRui Paulo }
588f05cddf9SRui Paulo
589f05cddf9SRui Paulo if (action == WLAN_PA_GAS_COMEBACK_RESP) {
590f05cddf9SRui Paulo if (pos + 1 > data + len)
591f05cddf9SRui Paulo return 0;
592f05cddf9SRui Paulo frag_id = *pos & 0x7f;
593f05cddf9SRui Paulo more_frags = (*pos & 0x80) >> 7;
594f05cddf9SRui Paulo pos++;
595f05cddf9SRui Paulo }
596f05cddf9SRui Paulo
597f05cddf9SRui Paulo /* Comeback Delay */
598f05cddf9SRui Paulo if (pos + 2 > data + len)
599f05cddf9SRui Paulo return 0;
600f05cddf9SRui Paulo comeback_delay = WPA_GET_LE16(pos);
601*a90b9d01SCy Schubert if (comeback_delay > GAS_QUERY_MAX_COMEBACK_DELAY)
602*a90b9d01SCy Schubert comeback_delay = GAS_QUERY_MAX_COMEBACK_DELAY;
603f05cddf9SRui Paulo pos += 2;
604f05cddf9SRui Paulo
605f05cddf9SRui Paulo /* Advertisement Protocol element */
606*a90b9d01SCy Schubert adv_proto = pos;
607*a90b9d01SCy Schubert left = data + len - adv_proto;
608*a90b9d01SCy Schubert if (left < 2 || adv_proto[1] > left - 2) {
609f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
610f05cddf9SRui Paulo "Protocol element in the response from " MACSTR,
611f05cddf9SRui Paulo MAC2STR(sa));
612f05cddf9SRui Paulo return 0;
613f05cddf9SRui Paulo }
614f05cddf9SRui Paulo
615*a90b9d01SCy Schubert if (*adv_proto != WLAN_EID_ADV_PROTO) {
616f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
617f05cddf9SRui Paulo "Protocol element ID %u in response from " MACSTR,
618*a90b9d01SCy Schubert *adv_proto, MAC2STR(sa));
619f05cddf9SRui Paulo return 0;
620f05cddf9SRui Paulo }
621*a90b9d01SCy Schubert adv_proto_len = 2 + adv_proto[1];
622*a90b9d01SCy Schubert if (adv_proto_len > (size_t) (data + len - pos))
623*a90b9d01SCy Schubert return 0; /* unreachable due to an earlier check */
624f05cddf9SRui Paulo
625*a90b9d01SCy Schubert pos += adv_proto_len;
626f05cddf9SRui Paulo
627f05cddf9SRui Paulo /* Query Response Length */
628f05cddf9SRui Paulo if (pos + 2 > data + len) {
629f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
630f05cddf9SRui Paulo return 0;
631f05cddf9SRui Paulo }
632f05cddf9SRui Paulo resp_len = WPA_GET_LE16(pos);
633f05cddf9SRui Paulo pos += 2;
634f05cddf9SRui Paulo
6355b9c547cSRui Paulo left = data + len - pos;
6365b9c547cSRui Paulo if (resp_len > left) {
637f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
638f05cddf9SRui Paulo "response from " MACSTR, MAC2STR(sa));
639f05cddf9SRui Paulo return 0;
640f05cddf9SRui Paulo }
641f05cddf9SRui Paulo
6425b9c547cSRui Paulo if (resp_len < left) {
643f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
644f05cddf9SRui Paulo "after Query Response from " MACSTR,
6455b9c547cSRui Paulo left - resp_len, MAC2STR(sa));
646f05cddf9SRui Paulo }
647f05cddf9SRui Paulo
648f05cddf9SRui Paulo if (action == WLAN_PA_GAS_COMEBACK_RESP)
649*a90b9d01SCy Schubert gas_query_rx_comeback(gas, query, adv_proto, adv_proto_len,
650*a90b9d01SCy Schubert pos, resp_len, frag_id, more_frags,
651f05cddf9SRui Paulo comeback_delay);
652*a90b9d01SCy Schubert else
653*a90b9d01SCy Schubert gas_query_rx_initial(gas, query, adv_proto, adv_proto_len,
654*a90b9d01SCy Schubert pos, resp_len, comeback_delay);
655f05cddf9SRui Paulo
656f05cddf9SRui Paulo return 0;
657f05cddf9SRui Paulo }
658f05cddf9SRui Paulo
659f05cddf9SRui Paulo
gas_query_timeout(void * eloop_data,void * user_ctx)660f05cddf9SRui Paulo static void gas_query_timeout(void *eloop_data, void *user_ctx)
661f05cddf9SRui Paulo {
662f05cddf9SRui Paulo struct gas_query *gas = eloop_data;
663f05cddf9SRui Paulo struct gas_query_pending *query = user_ctx;
664f05cddf9SRui Paulo
6655b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
6665b9c547cSRui Paulo " dialog token %u",
6675b9c547cSRui Paulo MAC2STR(query->addr), query->dialog_token);
668f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
669f05cddf9SRui Paulo }
670f05cddf9SRui Paulo
671f05cddf9SRui Paulo
gas_query_dialog_token_available(struct gas_query * gas,const u8 * dst,u8 dialog_token)672f05cddf9SRui Paulo static int gas_query_dialog_token_available(struct gas_query *gas,
673f05cddf9SRui Paulo const u8 *dst, u8 dialog_token)
674f05cddf9SRui Paulo {
675f05cddf9SRui Paulo struct gas_query_pending *q;
676f05cddf9SRui Paulo dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
677*a90b9d01SCy Schubert if (ether_addr_equal(dst, q->addr) &&
678f05cddf9SRui Paulo dialog_token == q->dialog_token)
679f05cddf9SRui Paulo return 0;
680f05cddf9SRui Paulo }
681f05cddf9SRui Paulo
682f05cddf9SRui Paulo return 1;
683f05cddf9SRui Paulo }
684f05cddf9SRui Paulo
685f05cddf9SRui Paulo
gas_query_start_cb(struct wpa_radio_work * work,int deinit)6865b9c547cSRui Paulo static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
6875b9c547cSRui Paulo {
6885b9c547cSRui Paulo struct gas_query_pending *query = work->ctx;
6895b9c547cSRui Paulo struct gas_query *gas = query->gas;
6905b9c547cSRui Paulo struct wpa_supplicant *wpa_s = gas->wpa_s;
6915b9c547cSRui Paulo
6925b9c547cSRui Paulo if (deinit) {
6935b9c547cSRui Paulo if (work->started) {
6945b9c547cSRui Paulo gas->work = NULL;
6955b9c547cSRui Paulo gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
6965b9c547cSRui Paulo return;
6975b9c547cSRui Paulo }
6985b9c547cSRui Paulo
6995b9c547cSRui Paulo gas_query_free(query, 1);
7005b9c547cSRui Paulo return;
7015b9c547cSRui Paulo }
7025b9c547cSRui Paulo
703c1d255d3SCy Schubert if (!query->maintain_addr && !wpa_s->conf->gas_rand_mac_addr) {
7045b9c547cSRui Paulo if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
7055b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO,
7065b9c547cSRui Paulo "Failed to assign random MAC address for GAS");
7075b9c547cSRui Paulo gas_query_free(query, 1);
7085b9c547cSRui Paulo radio_work_done(work);
7095b9c547cSRui Paulo return;
7105b9c547cSRui Paulo }
711c1d255d3SCy Schubert os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
712c1d255d3SCy Schubert }
7135b9c547cSRui Paulo
7145b9c547cSRui Paulo gas->work = work;
715780fb4a2SCy Schubert gas_query_tx_initial_req(gas, query);
716780fb4a2SCy Schubert }
7175b9c547cSRui Paulo
718780fb4a2SCy Schubert
gas_query_tx_initial_req(struct gas_query * gas,struct gas_query_pending * query)719780fb4a2SCy Schubert static void gas_query_tx_initial_req(struct gas_query *gas,
720780fb4a2SCy Schubert struct gas_query_pending *query)
721780fb4a2SCy Schubert {
722780fb4a2SCy Schubert if (gas_query_tx(gas, query, query->req,
723780fb4a2SCy Schubert GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
7245b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
7255b9c547cSRui Paulo MACSTR, MAC2STR(query->addr));
726780fb4a2SCy Schubert gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
7275b9c547cSRui Paulo return;
7285b9c547cSRui Paulo }
7295b9c547cSRui Paulo gas->current = query;
7305b9c547cSRui Paulo
7315b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
7325b9c547cSRui Paulo query->dialog_token);
7335b9c547cSRui Paulo eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
7345b9c547cSRui Paulo gas_query_timeout, gas, query);
735780fb4a2SCy Schubert }
7365b9c547cSRui Paulo
737780fb4a2SCy Schubert
gas_query_new_dialog_token(struct gas_query * gas,const u8 * dst)738780fb4a2SCy Schubert static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
739780fb4a2SCy Schubert {
740c1d255d3SCy Schubert u8 dialog_token;
741c1d255d3SCy Schubert int i;
742780fb4a2SCy Schubert
743c1d255d3SCy Schubert /* There should never be more than couple active GAS queries in
744c1d255d3SCy Schubert * progress, so it should be very likely to find an available dialog
745c1d255d3SCy Schubert * token by checking random values. Use a limit on the number of
746c1d255d3SCy Schubert * iterations to handle the unexpected case of large number of pending
747c1d255d3SCy Schubert * queries cleanly. */
748c1d255d3SCy Schubert for (i = 0; i < 256; i++) {
749c1d255d3SCy Schubert /* Get a random number and check if the slot is available */
750c1d255d3SCy Schubert if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0)
751780fb4a2SCy Schubert break;
752c1d255d3SCy Schubert if (gas_query_dialog_token_available(gas, dst, dialog_token))
753780fb4a2SCy Schubert return dialog_token;
7545b9c547cSRui Paulo }
7555b9c547cSRui Paulo
756c1d255d3SCy Schubert /* No dialog token value available */
757c1d255d3SCy Schubert return -1;
758c1d255d3SCy Schubert }
759c1d255d3SCy Schubert
7605b9c547cSRui Paulo
gas_query_set_sa(struct gas_query * gas,struct gas_query_pending * query)76185732ac8SCy Schubert static int gas_query_set_sa(struct gas_query *gas,
76285732ac8SCy Schubert struct gas_query_pending *query)
76385732ac8SCy Schubert {
76485732ac8SCy Schubert struct wpa_supplicant *wpa_s = gas->wpa_s;
76585732ac8SCy Schubert struct os_reltime now;
76685732ac8SCy Schubert
767c1d255d3SCy Schubert if (query->maintain_addr ||
768c1d255d3SCy Schubert !wpa_s->conf->gas_rand_mac_addr ||
76985732ac8SCy Schubert !(wpa_s->current_bss ?
77085732ac8SCy Schubert (wpa_s->drv_flags &
77185732ac8SCy Schubert WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
77285732ac8SCy Schubert (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
77385732ac8SCy Schubert /* Use own MAC address as the transmitter address */
774c1d255d3SCy Schubert wpa_printf(MSG_DEBUG,
775c1d255d3SCy Schubert "GAS: Use own MAC address as the transmitter address%s%s%s",
776c1d255d3SCy Schubert query->maintain_addr ? " (maintain_addr)" : "",
777c1d255d3SCy Schubert !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "",
778c1d255d3SCy Schubert !(wpa_s->current_bss ?
779c1d255d3SCy Schubert (wpa_s->drv_flags &
780c1d255d3SCy Schubert WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
781c1d255d3SCy Schubert (wpa_s->drv_flags &
782c1d255d3SCy Schubert WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ?
783c1d255d3SCy Schubert " (no driver rand capa" : "");
78485732ac8SCy Schubert os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
78585732ac8SCy Schubert return 0;
78685732ac8SCy Schubert }
78785732ac8SCy Schubert
78885732ac8SCy Schubert os_get_reltime(&now);
78985732ac8SCy Schubert
79085732ac8SCy Schubert if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
79185732ac8SCy Schubert gas->last_mac_addr_rand.sec != 0 &&
79285732ac8SCy Schubert !os_reltime_expired(&now, &gas->last_mac_addr_rand,
79385732ac8SCy Schubert wpa_s->conf->gas_rand_addr_lifetime)) {
79485732ac8SCy Schubert wpa_printf(MSG_DEBUG,
79585732ac8SCy Schubert "GAS: Use the previously selected random transmitter address "
79685732ac8SCy Schubert MACSTR, MAC2STR(gas->rand_addr));
79785732ac8SCy Schubert os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
79885732ac8SCy Schubert return 0;
79985732ac8SCy Schubert }
80085732ac8SCy Schubert
80185732ac8SCy Schubert if (wpa_s->conf->gas_rand_mac_addr == 1 &&
80285732ac8SCy Schubert random_mac_addr(gas->rand_addr) < 0) {
80385732ac8SCy Schubert wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
80485732ac8SCy Schubert return -1;
80585732ac8SCy Schubert }
80685732ac8SCy Schubert
80785732ac8SCy Schubert if (wpa_s->conf->gas_rand_mac_addr == 2 &&
80885732ac8SCy Schubert random_mac_addr_keep_oui(gas->rand_addr) < 0) {
80985732ac8SCy Schubert wpa_printf(MSG_ERROR,
81085732ac8SCy Schubert "GAS: Failed to get random address with same OUI");
81185732ac8SCy Schubert return -1;
81285732ac8SCy Schubert }
81385732ac8SCy Schubert
81485732ac8SCy Schubert wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
81585732ac8SCy Schubert MACSTR, MAC2STR(gas->rand_addr));
81685732ac8SCy Schubert os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
81785732ac8SCy Schubert os_get_reltime(&gas->last_mac_addr_rand);
81885732ac8SCy Schubert gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
81985732ac8SCy Schubert
82085732ac8SCy Schubert return 0;
82185732ac8SCy Schubert }
82285732ac8SCy Schubert
82385732ac8SCy Schubert
824f05cddf9SRui Paulo /**
825f05cddf9SRui Paulo * gas_query_req - Request a GAS query
826f05cddf9SRui Paulo * @gas: GAS query data from gas_query_init()
827f05cddf9SRui Paulo * @dst: Destination MAC address for the query
828f05cddf9SRui Paulo * @freq: Frequency (in MHz) for the channel on which to send the query
829c1d255d3SCy Schubert * @wildcard_bssid: Force use of wildcard BSSID value
830c1d255d3SCy Schubert * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC
831c1d255d3SCy Schubert * address randomization rules)
8325b9c547cSRui Paulo * @req: GAS query payload (to be freed by gas_query module in case of success
8335b9c547cSRui Paulo * return)
834f05cddf9SRui Paulo * @cb: Callback function for reporting GAS query result and response
835f05cddf9SRui Paulo * @ctx: Context pointer to use with the @cb call
836f05cddf9SRui Paulo * Returns: dialog token (>= 0) on success or -1 on failure
837f05cddf9SRui Paulo */
gas_query_req(struct gas_query * gas,const u8 * dst,int freq,int wildcard_bssid,int maintain_addr,struct wpabuf * req,void (* cb)(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code),void * ctx)838f05cddf9SRui Paulo int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
839c1d255d3SCy Schubert int wildcard_bssid, int maintain_addr, struct wpabuf *req,
840f05cddf9SRui Paulo void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
841f05cddf9SRui Paulo enum gas_query_result result,
842f05cddf9SRui Paulo const struct wpabuf *adv_proto,
843f05cddf9SRui Paulo const struct wpabuf *resp, u16 status_code),
844f05cddf9SRui Paulo void *ctx)
845f05cddf9SRui Paulo {
846f05cddf9SRui Paulo struct gas_query_pending *query;
847f05cddf9SRui Paulo int dialog_token;
848f05cddf9SRui Paulo
849f05cddf9SRui Paulo if (wpabuf_len(req) < 3)
850f05cddf9SRui Paulo return -1;
851f05cddf9SRui Paulo
852780fb4a2SCy Schubert dialog_token = gas_query_new_dialog_token(gas, dst);
853780fb4a2SCy Schubert if (dialog_token < 0)
854780fb4a2SCy Schubert return -1;
855f05cddf9SRui Paulo
856f05cddf9SRui Paulo query = os_zalloc(sizeof(*query));
857f05cddf9SRui Paulo if (query == NULL)
858f05cddf9SRui Paulo return -1;
859f05cddf9SRui Paulo
8605b9c547cSRui Paulo query->gas = gas;
861c1d255d3SCy Schubert query->maintain_addr = !!maintain_addr;
86285732ac8SCy Schubert if (gas_query_set_sa(gas, query)) {
86385732ac8SCy Schubert os_free(query);
86485732ac8SCy Schubert return -1;
86585732ac8SCy Schubert }
866f05cddf9SRui Paulo os_memcpy(query->addr, dst, ETH_ALEN);
867f05cddf9SRui Paulo query->dialog_token = dialog_token;
86885732ac8SCy Schubert query->wildcard_bssid = !!wildcard_bssid;
869f05cddf9SRui Paulo query->freq = freq;
870f05cddf9SRui Paulo query->cb = cb;
871f05cddf9SRui Paulo query->ctx = ctx;
8725b9c547cSRui Paulo query->req = req;
873f05cddf9SRui Paulo dl_list_add(&gas->pending, &query->list);
874f05cddf9SRui Paulo
875f05cddf9SRui Paulo *(wpabuf_mhead_u8(req) + 2) = dialog_token;
876f05cddf9SRui Paulo
8775b9c547cSRui Paulo wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
8785b9c547cSRui Paulo " dialog_token=%u freq=%d",
8795b9c547cSRui Paulo MAC2STR(query->addr), query->dialog_token, query->freq);
8805b9c547cSRui Paulo
8815b9c547cSRui Paulo if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
8825b9c547cSRui Paulo query) < 0) {
883780fb4a2SCy Schubert query->req = NULL; /* caller will free this in error case */
8845b9c547cSRui Paulo gas_query_free(query, 1);
885f05cddf9SRui Paulo return -1;
886f05cddf9SRui Paulo }
887f05cddf9SRui Paulo
888f05cddf9SRui Paulo return dialog_token;
889f05cddf9SRui Paulo }
89085732ac8SCy Schubert
89185732ac8SCy Schubert
gas_query_stop(struct gas_query * gas,u8 dialog_token)89285732ac8SCy Schubert int gas_query_stop(struct gas_query *gas, u8 dialog_token)
89385732ac8SCy Schubert {
89485732ac8SCy Schubert struct gas_query_pending *query;
89585732ac8SCy Schubert
89685732ac8SCy Schubert dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) {
89785732ac8SCy Schubert if (query->dialog_token == dialog_token) {
89885732ac8SCy Schubert if (!gas->work) {
89985732ac8SCy Schubert /* The pending radio work has not yet been
90085732ac8SCy Schubert * started, but the pending entry has a
90185732ac8SCy Schubert * reference to the soon to be freed query.
90285732ac8SCy Schubert * Need to remove that radio work now to avoid
90385732ac8SCy Schubert * leaving behind a reference to freed memory.
90485732ac8SCy Schubert */
90585732ac8SCy Schubert radio_remove_pending_work(gas->wpa_s, query);
90685732ac8SCy Schubert }
90785732ac8SCy Schubert gas_query_done(gas, query, GAS_QUERY_STOPPED);
90885732ac8SCy Schubert return 0;
90985732ac8SCy Schubert }
91085732ac8SCy Schubert }
91185732ac8SCy Schubert
91285732ac8SCy Schubert return -1;
91385732ac8SCy Schubert }
914