13ff40c12SJohn Marino /*
23ff40c12SJohn Marino  * Generic advertisement service (GAS) query
33ff40c12SJohn Marino  * Copyright (c) 2009, Atheros Communications
43ff40c12SJohn Marino  * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
53ff40c12SJohn Marino  * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
63ff40c12SJohn Marino  *
73ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
83ff40c12SJohn Marino  * See README for more details.
93ff40c12SJohn Marino  */
103ff40c12SJohn Marino 
113ff40c12SJohn Marino #include "includes.h"
123ff40c12SJohn Marino 
133ff40c12SJohn Marino #include "common.h"
143ff40c12SJohn Marino #include "utils/eloop.h"
153ff40c12SJohn Marino #include "common/ieee802_11_defs.h"
163ff40c12SJohn Marino #include "common/gas.h"
173ff40c12SJohn Marino #include "common/wpa_ctrl.h"
183ff40c12SJohn Marino #include "rsn_supp/wpa.h"
193ff40c12SJohn Marino #include "wpa_supplicant_i.h"
20*a1157835SDaniel Fojt #include "config.h"
213ff40c12SJohn Marino #include "driver_i.h"
223ff40c12SJohn Marino #include "offchannel.h"
233ff40c12SJohn Marino #include "gas_query.h"
243ff40c12SJohn Marino 
253ff40c12SJohn Marino 
263ff40c12SJohn Marino /** GAS query timeout in seconds */
273ff40c12SJohn Marino #define GAS_QUERY_TIMEOUT_PERIOD 2
283ff40c12SJohn Marino 
29*a1157835SDaniel Fojt /* GAS query wait-time / duration in ms */
30*a1157835SDaniel Fojt #define GAS_QUERY_WAIT_TIME_INITIAL 1000
31*a1157835SDaniel Fojt #define GAS_QUERY_WAIT_TIME_COMEBACK 150
323ff40c12SJohn Marino 
333ff40c12SJohn Marino /**
343ff40c12SJohn Marino  * struct gas_query_pending - Pending GAS query
353ff40c12SJohn Marino  */
363ff40c12SJohn Marino struct gas_query_pending {
373ff40c12SJohn Marino 	struct dl_list list;
383ff40c12SJohn Marino 	struct gas_query *gas;
393ff40c12SJohn Marino 	u8 addr[ETH_ALEN];
403ff40c12SJohn Marino 	u8 dialog_token;
413ff40c12SJohn Marino 	u8 next_frag_id;
423ff40c12SJohn Marino 	unsigned int wait_comeback:1;
433ff40c12SJohn Marino 	unsigned int offchannel_tx_started:1;
44*a1157835SDaniel Fojt 	unsigned int retry:1;
45*a1157835SDaniel Fojt 	unsigned int wildcard_bssid:1;
463ff40c12SJohn Marino 	int freq;
473ff40c12SJohn Marino 	u16 status_code;
483ff40c12SJohn Marino 	struct wpabuf *req;
493ff40c12SJohn Marino 	struct wpabuf *adv_proto;
503ff40c12SJohn Marino 	struct wpabuf *resp;
51*a1157835SDaniel Fojt 	struct os_reltime last_oper;
523ff40c12SJohn Marino 	void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
533ff40c12SJohn Marino 		   enum gas_query_result result,
543ff40c12SJohn Marino 		   const struct wpabuf *adv_proto,
553ff40c12SJohn Marino 		   const struct wpabuf *resp, u16 status_code);
563ff40c12SJohn Marino 	void *ctx;
57*a1157835SDaniel Fojt 	u8 sa[ETH_ALEN];
583ff40c12SJohn Marino };
593ff40c12SJohn Marino 
603ff40c12SJohn Marino /**
613ff40c12SJohn Marino  * struct gas_query - Internal GAS query data
623ff40c12SJohn Marino  */
633ff40c12SJohn Marino struct gas_query {
643ff40c12SJohn Marino 	struct wpa_supplicant *wpa_s;
653ff40c12SJohn Marino 	struct dl_list pending; /* struct gas_query_pending */
663ff40c12SJohn Marino 	struct gas_query_pending *current;
673ff40c12SJohn Marino 	struct wpa_radio_work *work;
68*a1157835SDaniel Fojt 	struct os_reltime last_mac_addr_rand;
69*a1157835SDaniel Fojt 	int last_rand_sa_type;
70*a1157835SDaniel Fojt 	u8 rand_addr[ETH_ALEN];
713ff40c12SJohn Marino };
723ff40c12SJohn Marino 
733ff40c12SJohn Marino 
743ff40c12SJohn Marino static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
753ff40c12SJohn Marino static void gas_query_timeout(void *eloop_data, void *user_ctx);
76*a1157835SDaniel Fojt static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
77*a1157835SDaniel Fojt static void gas_query_tx_initial_req(struct gas_query *gas,
78*a1157835SDaniel Fojt 				     struct gas_query_pending *query);
79*a1157835SDaniel Fojt static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
80*a1157835SDaniel Fojt 
81*a1157835SDaniel Fojt 
ms_from_time(struct os_reltime * last)82*a1157835SDaniel Fojt static int ms_from_time(struct os_reltime *last)
83*a1157835SDaniel Fojt {
84*a1157835SDaniel Fojt 	struct os_reltime now, res;
85*a1157835SDaniel Fojt 
86*a1157835SDaniel Fojt 	os_get_reltime(&now);
87*a1157835SDaniel Fojt 	os_reltime_sub(&now, last, &res);
88*a1157835SDaniel Fojt 	return res.sec * 1000 + res.usec / 1000;
89*a1157835SDaniel Fojt }
903ff40c12SJohn Marino 
913ff40c12SJohn Marino 
923ff40c12SJohn Marino /**
933ff40c12SJohn Marino  * gas_query_init - Initialize GAS query component
943ff40c12SJohn Marino  * @wpa_s: Pointer to wpa_supplicant data
953ff40c12SJohn Marino  * Returns: Pointer to GAS query data or %NULL on failure
963ff40c12SJohn Marino  */
gas_query_init(struct wpa_supplicant * wpa_s)973ff40c12SJohn Marino struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
983ff40c12SJohn Marino {
993ff40c12SJohn Marino 	struct gas_query *gas;
1003ff40c12SJohn Marino 
1013ff40c12SJohn Marino 	gas = os_zalloc(sizeof(*gas));
1023ff40c12SJohn Marino 	if (gas == NULL)
1033ff40c12SJohn Marino 		return NULL;
1043ff40c12SJohn Marino 
1053ff40c12SJohn Marino 	gas->wpa_s = wpa_s;
1063ff40c12SJohn Marino 	dl_list_init(&gas->pending);
1073ff40c12SJohn Marino 
1083ff40c12SJohn Marino 	return gas;
1093ff40c12SJohn Marino }
1103ff40c12SJohn Marino 
1113ff40c12SJohn Marino 
gas_result_txt(enum gas_query_result result)1123ff40c12SJohn Marino static const char * gas_result_txt(enum gas_query_result result)
1133ff40c12SJohn Marino {
1143ff40c12SJohn Marino 	switch (result) {
1153ff40c12SJohn Marino 	case GAS_QUERY_SUCCESS:
1163ff40c12SJohn Marino 		return "SUCCESS";
1173ff40c12SJohn Marino 	case GAS_QUERY_FAILURE:
1183ff40c12SJohn Marino 		return "FAILURE";
1193ff40c12SJohn Marino 	case GAS_QUERY_TIMEOUT:
1203ff40c12SJohn Marino 		return "TIMEOUT";
1213ff40c12SJohn Marino 	case GAS_QUERY_PEER_ERROR:
1223ff40c12SJohn Marino 		return "PEER_ERROR";
1233ff40c12SJohn Marino 	case GAS_QUERY_INTERNAL_ERROR:
1243ff40c12SJohn Marino 		return "INTERNAL_ERROR";
125*a1157835SDaniel Fojt 	case GAS_QUERY_STOPPED:
126*a1157835SDaniel Fojt 		return "STOPPED";
1273ff40c12SJohn Marino 	case GAS_QUERY_DELETED_AT_DEINIT:
1283ff40c12SJohn Marino 		return "DELETED_AT_DEINIT";
1293ff40c12SJohn Marino 	}
1303ff40c12SJohn Marino 
1313ff40c12SJohn Marino 	return "N/A";
1323ff40c12SJohn Marino }
1333ff40c12SJohn Marino 
1343ff40c12SJohn Marino 
gas_query_free(struct gas_query_pending * query,int del_list)1353ff40c12SJohn Marino static void gas_query_free(struct gas_query_pending *query, int del_list)
1363ff40c12SJohn Marino {
1373ff40c12SJohn Marino 	struct gas_query *gas = query->gas;
1383ff40c12SJohn Marino 
1393ff40c12SJohn Marino 	if (del_list)
1403ff40c12SJohn Marino 		dl_list_del(&query->list);
1413ff40c12SJohn Marino 
1423ff40c12SJohn Marino 	if (gas->work && gas->work->ctx == query) {
1433ff40c12SJohn Marino 		radio_work_done(gas->work);
1443ff40c12SJohn Marino 		gas->work = NULL;
1453ff40c12SJohn Marino 	}
1463ff40c12SJohn Marino 
1473ff40c12SJohn Marino 	wpabuf_free(query->req);
1483ff40c12SJohn Marino 	wpabuf_free(query->adv_proto);
1493ff40c12SJohn Marino 	wpabuf_free(query->resp);
1503ff40c12SJohn Marino 	os_free(query);
1513ff40c12SJohn Marino }
1523ff40c12SJohn Marino 
1533ff40c12SJohn Marino 
gas_query_done(struct gas_query * gas,struct gas_query_pending * query,enum gas_query_result result)1543ff40c12SJohn Marino static void gas_query_done(struct gas_query *gas,
1553ff40c12SJohn Marino 			   struct gas_query_pending *query,
1563ff40c12SJohn Marino 			   enum gas_query_result result)
1573ff40c12SJohn Marino {
1583ff40c12SJohn Marino 	wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
1593ff40c12SJohn Marino 		" dialog_token=%u freq=%d status_code=%u result=%s",
1603ff40c12SJohn Marino 		MAC2STR(query->addr), query->dialog_token, query->freq,
1613ff40c12SJohn Marino 		query->status_code, gas_result_txt(result));
1623ff40c12SJohn Marino 	if (gas->current == query)
1633ff40c12SJohn Marino 		gas->current = NULL;
1643ff40c12SJohn Marino 	if (query->offchannel_tx_started)
1653ff40c12SJohn Marino 		offchannel_send_action_done(gas->wpa_s);
1663ff40c12SJohn Marino 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
1673ff40c12SJohn Marino 	eloop_cancel_timeout(gas_query_timeout, gas, query);
168*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
1693ff40c12SJohn Marino 	dl_list_del(&query->list);
1703ff40c12SJohn Marino 	query->cb(query->ctx, query->addr, query->dialog_token, result,
1713ff40c12SJohn Marino 		  query->adv_proto, query->resp, query->status_code);
1723ff40c12SJohn Marino 	gas_query_free(query, 0);
1733ff40c12SJohn Marino }
1743ff40c12SJohn Marino 
1753ff40c12SJohn Marino 
1763ff40c12SJohn Marino /**
1773ff40c12SJohn Marino  * gas_query_deinit - Deinitialize GAS query component
1783ff40c12SJohn Marino  * @gas: GAS query data from gas_query_init()
1793ff40c12SJohn Marino  */
gas_query_deinit(struct gas_query * gas)1803ff40c12SJohn Marino void gas_query_deinit(struct gas_query *gas)
1813ff40c12SJohn Marino {
1823ff40c12SJohn Marino 	struct gas_query_pending *query, *next;
1833ff40c12SJohn Marino 
1843ff40c12SJohn Marino 	if (gas == NULL)
1853ff40c12SJohn Marino 		return;
1863ff40c12SJohn Marino 
1873ff40c12SJohn Marino 	dl_list_for_each_safe(query, next, &gas->pending,
1883ff40c12SJohn Marino 			      struct gas_query_pending, list)
1893ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
1903ff40c12SJohn Marino 
1913ff40c12SJohn Marino 	os_free(gas);
1923ff40c12SJohn Marino }
1933ff40c12SJohn Marino 
1943ff40c12SJohn Marino 
1953ff40c12SJohn Marino static struct gas_query_pending *
gas_query_get_pending(struct gas_query * gas,const u8 * addr,u8 dialog_token)1963ff40c12SJohn Marino gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
1973ff40c12SJohn Marino {
1983ff40c12SJohn Marino 	struct gas_query_pending *q;
1993ff40c12SJohn Marino 	dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
2003ff40c12SJohn Marino 		if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
2013ff40c12SJohn Marino 		    q->dialog_token == dialog_token)
2023ff40c12SJohn Marino 			return q;
2033ff40c12SJohn Marino 	}
2043ff40c12SJohn Marino 	return NULL;
2053ff40c12SJohn Marino }
2063ff40c12SJohn Marino 
2073ff40c12SJohn Marino 
gas_query_append(struct gas_query_pending * query,const u8 * data,size_t len)2083ff40c12SJohn Marino static int gas_query_append(struct gas_query_pending *query, const u8 *data,
2093ff40c12SJohn Marino 			    size_t len)
2103ff40c12SJohn Marino {
2113ff40c12SJohn Marino 	if (wpabuf_resize(&query->resp, len) < 0) {
2123ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
2133ff40c12SJohn Marino 		return -1;
2143ff40c12SJohn Marino 	}
2153ff40c12SJohn Marino 	wpabuf_put_data(query->resp, data, len);
2163ff40c12SJohn Marino 	return 0;
2173ff40c12SJohn Marino }
2183ff40c12SJohn Marino 
2193ff40c12SJohn Marino 
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)2203ff40c12SJohn Marino static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
2213ff40c12SJohn Marino 				unsigned int freq, const u8 *dst,
2223ff40c12SJohn Marino 				const u8 *src, const u8 *bssid,
2233ff40c12SJohn Marino 				const u8 *data, size_t data_len,
2243ff40c12SJohn Marino 				enum offchannel_send_action_result result)
2253ff40c12SJohn Marino {
2263ff40c12SJohn Marino 	struct gas_query_pending *query;
2273ff40c12SJohn Marino 	struct gas_query *gas = wpa_s->gas;
228*a1157835SDaniel Fojt 	int dur;
2293ff40c12SJohn Marino 
2303ff40c12SJohn Marino 	if (gas->current == NULL) {
2313ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
2323ff40c12SJohn Marino 			   MACSTR " result=%d - no query in progress",
2333ff40c12SJohn Marino 			   freq, MAC2STR(dst), result);
2343ff40c12SJohn Marino 		return;
2353ff40c12SJohn Marino 	}
2363ff40c12SJohn Marino 
2373ff40c12SJohn Marino 	query = gas->current;
2383ff40c12SJohn Marino 
239*a1157835SDaniel Fojt 	dur = ms_from_time(&query->last_oper);
2403ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
241*a1157835SDaniel Fojt 		   " result=%d query=%p dialog_token=%u dur=%d ms",
242*a1157835SDaniel Fojt 		   freq, MAC2STR(dst), result, query, query->dialog_token, dur);
2433ff40c12SJohn Marino 	if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
2443ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
2453ff40c12SJohn Marino 		return;
2463ff40c12SJohn Marino 	}
247*a1157835SDaniel Fojt 	os_get_reltime(&query->last_oper);
2483ff40c12SJohn Marino 
249*a1157835SDaniel Fojt 	if (result == OFFCHANNEL_SEND_ACTION_SUCCESS ||
250*a1157835SDaniel Fojt 	    result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
2513ff40c12SJohn Marino 		eloop_cancel_timeout(gas_query_timeout, gas, query);
252*a1157835SDaniel Fojt 		if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
253*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
254*a1157835SDaniel Fojt 			eloop_register_timeout(0, 250000,
255*a1157835SDaniel Fojt 					       gas_query_timeout, gas, query);
256*a1157835SDaniel Fojt 		} else {
2573ff40c12SJohn Marino 			eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
2583ff40c12SJohn Marino 					       gas_query_timeout, gas, query);
2593ff40c12SJohn Marino 		}
260*a1157835SDaniel Fojt 		if (query->wait_comeback && !query->retry) {
261*a1157835SDaniel Fojt 			eloop_cancel_timeout(gas_query_rx_comeback_timeout,
262*a1157835SDaniel Fojt 					     gas, query);
263*a1157835SDaniel Fojt 			eloop_register_timeout(
264*a1157835SDaniel Fojt 				0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
265*a1157835SDaniel Fojt 				gas_query_rx_comeback_timeout, gas, query);
266*a1157835SDaniel Fojt 		}
267*a1157835SDaniel Fojt 	}
2683ff40c12SJohn Marino 	if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
2693ff40c12SJohn Marino 		eloop_cancel_timeout(gas_query_timeout, gas, query);
2703ff40c12SJohn Marino 		eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
2713ff40c12SJohn Marino 	}
2723ff40c12SJohn Marino }
2733ff40c12SJohn Marino 
2743ff40c12SJohn Marino 
pmf_in_use(struct wpa_supplicant * wpa_s,const u8 * addr)275*a1157835SDaniel Fojt int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
2763ff40c12SJohn Marino {
2773ff40c12SJohn Marino 	if (wpa_s->current_ssid == NULL ||
2783ff40c12SJohn Marino 	    wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
2793ff40c12SJohn Marino 	    os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
2803ff40c12SJohn Marino 		return 0;
2813ff40c12SJohn Marino 	return wpa_sm_pmf_enabled(wpa_s->wpa);
2823ff40c12SJohn Marino }
2833ff40c12SJohn Marino 
2843ff40c12SJohn Marino 
gas_query_tx(struct gas_query * gas,struct gas_query_pending * query,struct wpabuf * req,unsigned int wait_time)2853ff40c12SJohn Marino static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
286*a1157835SDaniel Fojt 			struct wpabuf *req, unsigned int wait_time)
2873ff40c12SJohn Marino {
2883ff40c12SJohn Marino 	int res, prot = pmf_in_use(gas->wpa_s, query->addr);
289*a1157835SDaniel Fojt 	const u8 *bssid;
290*a1157835SDaniel Fojt 	const u8 wildcard_bssid[ETH_ALEN] = {
291*a1157835SDaniel Fojt 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
292*a1157835SDaniel Fojt 	};
2933ff40c12SJohn Marino 
2943ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
295*a1157835SDaniel Fojt 		   "freq=%d prot=%d using src addr " MACSTR,
296*a1157835SDaniel Fojt 		   MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
297*a1157835SDaniel Fojt 		   query->freq, prot, MAC2STR(query->sa));
2983ff40c12SJohn Marino 	if (prot) {
2993ff40c12SJohn Marino 		u8 *categ = wpabuf_mhead_u8(req);
3003ff40c12SJohn Marino 		*categ = WLAN_ACTION_PROTECTED_DUAL;
3013ff40c12SJohn Marino 	}
302*a1157835SDaniel Fojt 	os_get_reltime(&query->last_oper);
303*a1157835SDaniel Fojt 	if (gas->wpa_s->max_remain_on_chan &&
304*a1157835SDaniel Fojt 	    wait_time > gas->wpa_s->max_remain_on_chan)
305*a1157835SDaniel Fojt 		wait_time = gas->wpa_s->max_remain_on_chan;
306*a1157835SDaniel Fojt 	if (!query->wildcard_bssid &&
307*a1157835SDaniel Fojt 	    (!gas->wpa_s->conf->gas_address3 ||
308*a1157835SDaniel Fojt 	     (gas->wpa_s->current_ssid &&
309*a1157835SDaniel Fojt 	      gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
310*a1157835SDaniel Fojt 	      os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0)))
311*a1157835SDaniel Fojt 		bssid = query->addr;
312*a1157835SDaniel Fojt 	else
313*a1157835SDaniel Fojt 		bssid = wildcard_bssid;
314*a1157835SDaniel Fojt 
3153ff40c12SJohn Marino 	res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
316*a1157835SDaniel Fojt 				     query->sa, bssid, wpabuf_head(req),
317*a1157835SDaniel Fojt 				     wpabuf_len(req), wait_time,
3183ff40c12SJohn Marino 				     gas_query_tx_status, 0);
319*a1157835SDaniel Fojt 
3203ff40c12SJohn Marino 	if (res == 0)
3213ff40c12SJohn Marino 		query->offchannel_tx_started = 1;
3223ff40c12SJohn Marino 	return res;
3233ff40c12SJohn Marino }
3243ff40c12SJohn Marino 
3253ff40c12SJohn Marino 
gas_query_tx_comeback_req(struct gas_query * gas,struct gas_query_pending * query)3263ff40c12SJohn Marino static void gas_query_tx_comeback_req(struct gas_query *gas,
3273ff40c12SJohn Marino 				      struct gas_query_pending *query)
3283ff40c12SJohn Marino {
3293ff40c12SJohn Marino 	struct wpabuf *req;
330*a1157835SDaniel Fojt 	unsigned int wait_time;
3313ff40c12SJohn Marino 
3323ff40c12SJohn Marino 	req = gas_build_comeback_req(query->dialog_token);
3333ff40c12SJohn Marino 	if (req == NULL) {
3343ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
3353ff40c12SJohn Marino 		return;
3363ff40c12SJohn Marino 	}
3373ff40c12SJohn Marino 
338*a1157835SDaniel Fojt 	wait_time = (query->retry || !query->offchannel_tx_started) ?
339*a1157835SDaniel Fojt 		GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
340*a1157835SDaniel Fojt 
341*a1157835SDaniel Fojt 	if (gas_query_tx(gas, query, req, wait_time) < 0) {
3423ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
3433ff40c12SJohn Marino 			   MACSTR, MAC2STR(query->addr));
3443ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
3453ff40c12SJohn Marino 	}
3463ff40c12SJohn Marino 
3473ff40c12SJohn Marino 	wpabuf_free(req);
3483ff40c12SJohn Marino }
3493ff40c12SJohn Marino 
3503ff40c12SJohn Marino 
gas_query_rx_comeback_timeout(void * eloop_data,void * user_ctx)351*a1157835SDaniel Fojt static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
352*a1157835SDaniel Fojt {
353*a1157835SDaniel Fojt 	struct gas_query *gas = eloop_data;
354*a1157835SDaniel Fojt 	struct gas_query_pending *query = user_ctx;
355*a1157835SDaniel Fojt 	int dialog_token;
356*a1157835SDaniel Fojt 
357*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
358*a1157835SDaniel Fojt 		   "GAS: No response to comeback request received (retry=%u)",
359*a1157835SDaniel Fojt 		   query->retry);
360*a1157835SDaniel Fojt 	if (gas->current != query || query->retry)
361*a1157835SDaniel Fojt 		return;
362*a1157835SDaniel Fojt 	dialog_token = gas_query_new_dialog_token(gas, query->addr);
363*a1157835SDaniel Fojt 	if (dialog_token < 0)
364*a1157835SDaniel Fojt 		return;
365*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
366*a1157835SDaniel Fojt 		   "GAS: Retry GAS query due to comeback response timeout");
367*a1157835SDaniel Fojt 	query->retry = 1;
368*a1157835SDaniel Fojt 	query->dialog_token = dialog_token;
369*a1157835SDaniel Fojt 	*(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
370*a1157835SDaniel Fojt 	query->wait_comeback = 0;
371*a1157835SDaniel Fojt 	query->next_frag_id = 0;
372*a1157835SDaniel Fojt 	wpabuf_free(query->adv_proto);
373*a1157835SDaniel Fojt 	query->adv_proto = NULL;
374*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
375*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_timeout, gas, query);
376*a1157835SDaniel Fojt 	gas_query_tx_initial_req(gas, query);
377*a1157835SDaniel Fojt }
378*a1157835SDaniel Fojt 
379*a1157835SDaniel Fojt 
gas_query_tx_comeback_timeout(void * eloop_data,void * user_ctx)3803ff40c12SJohn Marino static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
3813ff40c12SJohn Marino {
3823ff40c12SJohn Marino 	struct gas_query *gas = eloop_data;
3833ff40c12SJohn Marino 	struct gas_query_pending *query = user_ctx;
3843ff40c12SJohn Marino 
3853ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
3863ff40c12SJohn Marino 		   MAC2STR(query->addr));
3873ff40c12SJohn Marino 	gas_query_tx_comeback_req(gas, query);
3883ff40c12SJohn Marino }
3893ff40c12SJohn Marino 
3903ff40c12SJohn Marino 
gas_query_tx_comeback_req_delay(struct gas_query * gas,struct gas_query_pending * query,u16 comeback_delay)3913ff40c12SJohn Marino static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
3923ff40c12SJohn Marino 					    struct gas_query_pending *query,
3933ff40c12SJohn Marino 					    u16 comeback_delay)
3943ff40c12SJohn Marino {
3953ff40c12SJohn Marino 	unsigned int secs, usecs;
3963ff40c12SJohn Marino 
397*a1157835SDaniel Fojt 	if (comeback_delay > 1 && query->offchannel_tx_started) {
398*a1157835SDaniel Fojt 		offchannel_send_action_done(gas->wpa_s);
399*a1157835SDaniel Fojt 		query->offchannel_tx_started = 0;
400*a1157835SDaniel Fojt 	}
401*a1157835SDaniel Fojt 
4023ff40c12SJohn Marino 	secs = (comeback_delay * 1024) / 1000000;
4033ff40c12SJohn Marino 	usecs = comeback_delay * 1024 - secs * 1000000;
4043ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
4053ff40c12SJohn Marino 		   " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
4063ff40c12SJohn Marino 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
4073ff40c12SJohn Marino 	eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
4083ff40c12SJohn Marino 			       gas, query);
4093ff40c12SJohn Marino }
4103ff40c12SJohn Marino 
4113ff40c12SJohn Marino 
gas_query_rx_initial(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,const u8 * resp,size_t len,u16 comeback_delay)4123ff40c12SJohn Marino static void gas_query_rx_initial(struct gas_query *gas,
4133ff40c12SJohn Marino 				 struct gas_query_pending *query,
4143ff40c12SJohn Marino 				 const u8 *adv_proto, const u8 *resp,
4153ff40c12SJohn Marino 				 size_t len, u16 comeback_delay)
4163ff40c12SJohn Marino {
4173ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
4183ff40c12SJohn Marino 		   MACSTR " (dialog_token=%u comeback_delay=%u)",
4193ff40c12SJohn Marino 		   MAC2STR(query->addr), query->dialog_token, comeback_delay);
4203ff40c12SJohn Marino 
4213ff40c12SJohn Marino 	query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
4223ff40c12SJohn Marino 	if (query->adv_proto == NULL) {
4233ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
4243ff40c12SJohn Marino 		return;
4253ff40c12SJohn Marino 	}
4263ff40c12SJohn Marino 
4273ff40c12SJohn Marino 	if (comeback_delay) {
428*a1157835SDaniel Fojt 		eloop_cancel_timeout(gas_query_timeout, gas, query);
4293ff40c12SJohn Marino 		query->wait_comeback = 1;
4303ff40c12SJohn Marino 		gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
4313ff40c12SJohn Marino 		return;
4323ff40c12SJohn Marino 	}
4333ff40c12SJohn Marino 
4343ff40c12SJohn Marino 	/* Query was completed without comeback mechanism */
4353ff40c12SJohn Marino 	if (gas_query_append(query, resp, len) < 0) {
4363ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
4373ff40c12SJohn Marino 		return;
4383ff40c12SJohn Marino 	}
4393ff40c12SJohn Marino 
4403ff40c12SJohn Marino 	gas_query_done(gas, query, GAS_QUERY_SUCCESS);
4413ff40c12SJohn Marino }
4423ff40c12SJohn Marino 
4433ff40c12SJohn Marino 
gas_query_rx_comeback(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,const u8 * resp,size_t len,u8 frag_id,u8 more_frags,u16 comeback_delay)4443ff40c12SJohn Marino static void gas_query_rx_comeback(struct gas_query *gas,
4453ff40c12SJohn Marino 				  struct gas_query_pending *query,
4463ff40c12SJohn Marino 				  const u8 *adv_proto, const u8 *resp,
4473ff40c12SJohn Marino 				  size_t len, u8 frag_id, u8 more_frags,
4483ff40c12SJohn Marino 				  u16 comeback_delay)
4493ff40c12SJohn Marino {
4503ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
4513ff40c12SJohn Marino 		   MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
4523ff40c12SJohn Marino 		   "comeback_delay=%u)",
4533ff40c12SJohn Marino 		   MAC2STR(query->addr), query->dialog_token, frag_id,
4543ff40c12SJohn Marino 		   more_frags, comeback_delay);
455*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
4563ff40c12SJohn Marino 
4573ff40c12SJohn Marino 	if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
4583ff40c12SJohn Marino 	    os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
4593ff40c12SJohn Marino 		      wpabuf_len(query->adv_proto)) != 0) {
4603ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
4613ff40c12SJohn Marino 			   "between initial and comeback response from "
4623ff40c12SJohn Marino 			   MACSTR, MAC2STR(query->addr));
4633ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
4643ff40c12SJohn Marino 		return;
4653ff40c12SJohn Marino 	}
4663ff40c12SJohn Marino 
4673ff40c12SJohn Marino 	if (comeback_delay) {
4683ff40c12SJohn Marino 		if (frag_id) {
4693ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
4703ff40c12SJohn Marino 				   "with non-zero frag_id and comeback_delay "
4713ff40c12SJohn Marino 				   "from " MACSTR, MAC2STR(query->addr));
4723ff40c12SJohn Marino 			gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
4733ff40c12SJohn Marino 			return;
4743ff40c12SJohn Marino 		}
4753ff40c12SJohn Marino 		gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
4763ff40c12SJohn Marino 		return;
4773ff40c12SJohn Marino 	}
4783ff40c12SJohn Marino 
4793ff40c12SJohn Marino 	if (frag_id != query->next_frag_id) {
4803ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
4813ff40c12SJohn Marino 			   "from " MACSTR, MAC2STR(query->addr));
4823ff40c12SJohn Marino 		if (frag_id + 1 == query->next_frag_id) {
4833ff40c12SJohn Marino 			wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
4843ff40c12SJohn Marino 				   "retry of previous fragment");
4853ff40c12SJohn Marino 			return;
4863ff40c12SJohn Marino 		}
4873ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
4883ff40c12SJohn Marino 		return;
4893ff40c12SJohn Marino 	}
4903ff40c12SJohn Marino 	query->next_frag_id++;
4913ff40c12SJohn Marino 
4923ff40c12SJohn Marino 	if (gas_query_append(query, resp, len) < 0) {
4933ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
4943ff40c12SJohn Marino 		return;
4953ff40c12SJohn Marino 	}
4963ff40c12SJohn Marino 
4973ff40c12SJohn Marino 	if (more_frags) {
4983ff40c12SJohn Marino 		gas_query_tx_comeback_req(gas, query);
4993ff40c12SJohn Marino 		return;
5003ff40c12SJohn Marino 	}
5013ff40c12SJohn Marino 
5023ff40c12SJohn Marino 	gas_query_done(gas, query, GAS_QUERY_SUCCESS);
5033ff40c12SJohn Marino }
5043ff40c12SJohn Marino 
5053ff40c12SJohn Marino 
5063ff40c12SJohn Marino /**
5073ff40c12SJohn Marino  * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
5083ff40c12SJohn Marino  * @gas: GAS query data from gas_query_init()
5093ff40c12SJohn Marino  * @da: Destination MAC address of the Action frame
5103ff40c12SJohn Marino  * @sa: Source MAC address of the Action frame
5113ff40c12SJohn Marino  * @bssid: BSSID of the Action frame
5123ff40c12SJohn Marino  * @categ: Category of the Action frame
5133ff40c12SJohn Marino  * @data: Payload of the Action frame
5143ff40c12SJohn Marino  * @len: Length of @data
5153ff40c12SJohn Marino  * @freq: Frequency (in MHz) on which the frame was received
5163ff40c12SJohn Marino  * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
5173ff40c12SJohn Marino  */
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)5183ff40c12SJohn Marino int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
5193ff40c12SJohn Marino 		 const u8 *bssid, u8 categ, const u8 *data, size_t len,
5203ff40c12SJohn Marino 		 int freq)
5213ff40c12SJohn Marino {
5223ff40c12SJohn Marino 	struct gas_query_pending *query;
5233ff40c12SJohn Marino 	u8 action, dialog_token, frag_id = 0, more_frags = 0;
5243ff40c12SJohn Marino 	u16 comeback_delay, resp_len;
5253ff40c12SJohn Marino 	const u8 *pos, *adv_proto;
5263ff40c12SJohn Marino 	int prot, pmf;
527*a1157835SDaniel Fojt 	unsigned int left;
5283ff40c12SJohn Marino 
5293ff40c12SJohn Marino 	if (gas == NULL || len < 4)
5303ff40c12SJohn Marino 		return -1;
5313ff40c12SJohn Marino 
5323ff40c12SJohn Marino 	pos = data;
5333ff40c12SJohn Marino 	action = *pos++;
5343ff40c12SJohn Marino 	dialog_token = *pos++;
5353ff40c12SJohn Marino 
5363ff40c12SJohn Marino 	if (action != WLAN_PA_GAS_INITIAL_RESP &&
5373ff40c12SJohn Marino 	    action != WLAN_PA_GAS_COMEBACK_RESP)
5383ff40c12SJohn Marino 		return -1; /* Not a GAS response */
5393ff40c12SJohn Marino 
540*a1157835SDaniel Fojt 	prot = categ == WLAN_ACTION_PROTECTED_DUAL;
541*a1157835SDaniel Fojt 	pmf = pmf_in_use(gas->wpa_s, sa);
542*a1157835SDaniel Fojt 	if (prot && !pmf) {
543*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
544*a1157835SDaniel Fojt 		return 0;
545*a1157835SDaniel Fojt 	}
546*a1157835SDaniel Fojt 	if (!prot && pmf) {
547*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
548*a1157835SDaniel Fojt 		return 0;
549*a1157835SDaniel Fojt 	}
550*a1157835SDaniel Fojt 
5513ff40c12SJohn Marino 	query = gas_query_get_pending(gas, sa, dialog_token);
5523ff40c12SJohn Marino 	if (query == NULL) {
5533ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
5543ff40c12SJohn Marino 			   " dialog token %u", MAC2STR(sa), dialog_token);
5553ff40c12SJohn Marino 		return -1;
5563ff40c12SJohn Marino 	}
5573ff40c12SJohn Marino 
558*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
559*a1157835SDaniel Fojt 		   ms_from_time(&query->last_oper), MAC2STR(sa));
560*a1157835SDaniel Fojt 
5613ff40c12SJohn Marino 	if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
5623ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
5633ff40c12SJohn Marino 			   MACSTR " dialog token %u when waiting for comeback "
5643ff40c12SJohn Marino 			   "response", MAC2STR(sa), dialog_token);
5653ff40c12SJohn Marino 		return 0;
5663ff40c12SJohn Marino 	}
5673ff40c12SJohn Marino 
5683ff40c12SJohn Marino 	if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
5693ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
5703ff40c12SJohn Marino 			   MACSTR " dialog token %u when waiting for initial "
5713ff40c12SJohn Marino 			   "response", MAC2STR(sa), dialog_token);
5723ff40c12SJohn Marino 		return 0;
5733ff40c12SJohn Marino 	}
5743ff40c12SJohn Marino 
5753ff40c12SJohn Marino 	query->status_code = WPA_GET_LE16(pos);
5763ff40c12SJohn Marino 	pos += 2;
5773ff40c12SJohn Marino 
578*a1157835SDaniel Fojt 	if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
579*a1157835SDaniel Fojt 	    action == WLAN_PA_GAS_COMEBACK_RESP) {
580*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
581*a1157835SDaniel Fojt 	} else if (query->status_code != WLAN_STATUS_SUCCESS) {
5823ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
5833ff40c12SJohn Marino 			   "%u failed - status code %u",
5843ff40c12SJohn Marino 			   MAC2STR(sa), dialog_token, query->status_code);
5853ff40c12SJohn Marino 		gas_query_done(gas, query, GAS_QUERY_FAILURE);
5863ff40c12SJohn Marino 		return 0;
5873ff40c12SJohn Marino 	}
5883ff40c12SJohn Marino 
5893ff40c12SJohn Marino 	if (action == WLAN_PA_GAS_COMEBACK_RESP) {
5903ff40c12SJohn Marino 		if (pos + 1 > data + len)
5913ff40c12SJohn Marino 			return 0;
5923ff40c12SJohn Marino 		frag_id = *pos & 0x7f;
5933ff40c12SJohn Marino 		more_frags = (*pos & 0x80) >> 7;
5943ff40c12SJohn Marino 		pos++;
5953ff40c12SJohn Marino 	}
5963ff40c12SJohn Marino 
5973ff40c12SJohn Marino 	/* Comeback Delay */
5983ff40c12SJohn Marino 	if (pos + 2 > data + len)
5993ff40c12SJohn Marino 		return 0;
6003ff40c12SJohn Marino 	comeback_delay = WPA_GET_LE16(pos);
6013ff40c12SJohn Marino 	pos += 2;
6023ff40c12SJohn Marino 
6033ff40c12SJohn Marino 	/* Advertisement Protocol element */
6043ff40c12SJohn Marino 	if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
6053ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
6063ff40c12SJohn Marino 			   "Protocol element in the response from " MACSTR,
6073ff40c12SJohn Marino 			   MAC2STR(sa));
6083ff40c12SJohn Marino 		return 0;
6093ff40c12SJohn Marino 	}
6103ff40c12SJohn Marino 
6113ff40c12SJohn Marino 	if (*pos != WLAN_EID_ADV_PROTO) {
6123ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
6133ff40c12SJohn Marino 			   "Protocol element ID %u in response from " MACSTR,
6143ff40c12SJohn Marino 			   *pos, MAC2STR(sa));
6153ff40c12SJohn Marino 		return 0;
6163ff40c12SJohn Marino 	}
6173ff40c12SJohn Marino 
6183ff40c12SJohn Marino 	adv_proto = pos;
6193ff40c12SJohn Marino 	pos += 2 + pos[1];
6203ff40c12SJohn Marino 
6213ff40c12SJohn Marino 	/* Query Response Length */
6223ff40c12SJohn Marino 	if (pos + 2 > data + len) {
6233ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
6243ff40c12SJohn Marino 		return 0;
6253ff40c12SJohn Marino 	}
6263ff40c12SJohn Marino 	resp_len = WPA_GET_LE16(pos);
6273ff40c12SJohn Marino 	pos += 2;
6283ff40c12SJohn Marino 
629*a1157835SDaniel Fojt 	left = data + len - pos;
630*a1157835SDaniel Fojt 	if (resp_len > left) {
6313ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
6323ff40c12SJohn Marino 			   "response from " MACSTR, MAC2STR(sa));
6333ff40c12SJohn Marino 		return 0;
6343ff40c12SJohn Marino 	}
6353ff40c12SJohn Marino 
636*a1157835SDaniel Fojt 	if (resp_len < left) {
6373ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
6383ff40c12SJohn Marino 			   "after Query Response from " MACSTR,
639*a1157835SDaniel Fojt 			   left - resp_len, MAC2STR(sa));
6403ff40c12SJohn Marino 	}
6413ff40c12SJohn Marino 
6423ff40c12SJohn Marino 	if (action == WLAN_PA_GAS_COMEBACK_RESP)
6433ff40c12SJohn Marino 		gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
6443ff40c12SJohn Marino 				      frag_id, more_frags, comeback_delay);
6453ff40c12SJohn Marino 	else
6463ff40c12SJohn Marino 		gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
6473ff40c12SJohn Marino 				     comeback_delay);
6483ff40c12SJohn Marino 
6493ff40c12SJohn Marino 	return 0;
6503ff40c12SJohn Marino }
6513ff40c12SJohn Marino 
6523ff40c12SJohn Marino 
gas_query_timeout(void * eloop_data,void * user_ctx)6533ff40c12SJohn Marino static void gas_query_timeout(void *eloop_data, void *user_ctx)
6543ff40c12SJohn Marino {
6553ff40c12SJohn Marino 	struct gas_query *gas = eloop_data;
6563ff40c12SJohn Marino 	struct gas_query_pending *query = user_ctx;
6573ff40c12SJohn Marino 
6583ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
6593ff40c12SJohn Marino 		   " dialog token %u",
6603ff40c12SJohn Marino 		   MAC2STR(query->addr), query->dialog_token);
6613ff40c12SJohn Marino 	gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
6623ff40c12SJohn Marino }
6633ff40c12SJohn Marino 
6643ff40c12SJohn Marino 
gas_query_dialog_token_available(struct gas_query * gas,const u8 * dst,u8 dialog_token)6653ff40c12SJohn Marino static int gas_query_dialog_token_available(struct gas_query *gas,
6663ff40c12SJohn Marino 					    const u8 *dst, u8 dialog_token)
6673ff40c12SJohn Marino {
6683ff40c12SJohn Marino 	struct gas_query_pending *q;
6693ff40c12SJohn Marino 	dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
6703ff40c12SJohn Marino 		if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
6713ff40c12SJohn Marino 		    dialog_token == q->dialog_token)
6723ff40c12SJohn Marino 			return 0;
6733ff40c12SJohn Marino 	}
6743ff40c12SJohn Marino 
6753ff40c12SJohn Marino 	return 1;
6763ff40c12SJohn Marino }
6773ff40c12SJohn Marino 
6783ff40c12SJohn Marino 
gas_query_start_cb(struct wpa_radio_work * work,int deinit)6793ff40c12SJohn Marino static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
6803ff40c12SJohn Marino {
6813ff40c12SJohn Marino 	struct gas_query_pending *query = work->ctx;
6823ff40c12SJohn Marino 	struct gas_query *gas = query->gas;
683*a1157835SDaniel Fojt 	struct wpa_supplicant *wpa_s = gas->wpa_s;
6843ff40c12SJohn Marino 
6853ff40c12SJohn Marino 	if (deinit) {
686*a1157835SDaniel Fojt 		if (work->started) {
687*a1157835SDaniel Fojt 			gas->work = NULL;
688*a1157835SDaniel Fojt 			gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
689*a1157835SDaniel Fojt 			return;
690*a1157835SDaniel Fojt 		}
691*a1157835SDaniel Fojt 
6923ff40c12SJohn Marino 		gas_query_free(query, 1);
6933ff40c12SJohn Marino 		return;
6943ff40c12SJohn Marino 	}
6953ff40c12SJohn Marino 
696*a1157835SDaniel Fojt 	if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
697*a1157835SDaniel Fojt 		wpa_msg(wpa_s, MSG_INFO,
698*a1157835SDaniel Fojt 			"Failed to assign random MAC address for GAS");
699*a1157835SDaniel Fojt 		gas_query_free(query, 1);
700*a1157835SDaniel Fojt 		radio_work_done(work);
701*a1157835SDaniel Fojt 		return;
702*a1157835SDaniel Fojt 	}
7033ff40c12SJohn Marino 
704*a1157835SDaniel Fojt 	gas->work = work;
705*a1157835SDaniel Fojt 	gas_query_tx_initial_req(gas, query);
706*a1157835SDaniel Fojt }
707*a1157835SDaniel Fojt 
708*a1157835SDaniel Fojt 
gas_query_tx_initial_req(struct gas_query * gas,struct gas_query_pending * query)709*a1157835SDaniel Fojt static void gas_query_tx_initial_req(struct gas_query *gas,
710*a1157835SDaniel Fojt 				     struct gas_query_pending *query)
711*a1157835SDaniel Fojt {
712*a1157835SDaniel Fojt 	if (gas_query_tx(gas, query, query->req,
713*a1157835SDaniel Fojt 			 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
7143ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
7153ff40c12SJohn Marino 			   MACSTR, MAC2STR(query->addr));
716*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
7173ff40c12SJohn Marino 		return;
7183ff40c12SJohn Marino 	}
7193ff40c12SJohn Marino 	gas->current = query;
7203ff40c12SJohn Marino 
7213ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
7223ff40c12SJohn Marino 		   query->dialog_token);
7233ff40c12SJohn Marino 	eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
7243ff40c12SJohn Marino 			       gas_query_timeout, gas, query);
725*a1157835SDaniel Fojt }
7263ff40c12SJohn Marino 
727*a1157835SDaniel Fojt 
gas_query_new_dialog_token(struct gas_query * gas,const u8 * dst)728*a1157835SDaniel Fojt static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
729*a1157835SDaniel Fojt {
730*a1157835SDaniel Fojt 	static int next_start = 0;
731*a1157835SDaniel Fojt 	int dialog_token;
732*a1157835SDaniel Fojt 
733*a1157835SDaniel Fojt 	for (dialog_token = 0; dialog_token < 256; dialog_token++) {
734*a1157835SDaniel Fojt 		if (gas_query_dialog_token_available(
735*a1157835SDaniel Fojt 			    gas, dst, (next_start + dialog_token) % 256))
736*a1157835SDaniel Fojt 			break;
737*a1157835SDaniel Fojt 	}
738*a1157835SDaniel Fojt 	if (dialog_token == 256)
739*a1157835SDaniel Fojt 		return -1; /* Too many pending queries */
740*a1157835SDaniel Fojt 	dialog_token = (next_start + dialog_token) % 256;
741*a1157835SDaniel Fojt 	next_start = (dialog_token + 1) % 256;
742*a1157835SDaniel Fojt 	return dialog_token;
743*a1157835SDaniel Fojt }
744*a1157835SDaniel Fojt 
745*a1157835SDaniel Fojt 
gas_query_set_sa(struct gas_query * gas,struct gas_query_pending * query)746*a1157835SDaniel Fojt static int gas_query_set_sa(struct gas_query *gas,
747*a1157835SDaniel Fojt 			    struct gas_query_pending *query)
748*a1157835SDaniel Fojt {
749*a1157835SDaniel Fojt 	struct wpa_supplicant *wpa_s = gas->wpa_s;
750*a1157835SDaniel Fojt 	struct os_reltime now;
751*a1157835SDaniel Fojt 
752*a1157835SDaniel Fojt 	if (!wpa_s->conf->gas_rand_mac_addr ||
753*a1157835SDaniel Fojt 	    !(wpa_s->current_bss ?
754*a1157835SDaniel Fojt 	      (wpa_s->drv_flags &
755*a1157835SDaniel Fojt 	       WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
756*a1157835SDaniel Fojt 	      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
757*a1157835SDaniel Fojt 		/* Use own MAC address as the transmitter address */
758*a1157835SDaniel Fojt 		os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
759*a1157835SDaniel Fojt 		return 0;
760*a1157835SDaniel Fojt 	}
761*a1157835SDaniel Fojt 
762*a1157835SDaniel Fojt 	os_get_reltime(&now);
763*a1157835SDaniel Fojt 
764*a1157835SDaniel Fojt 	if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
765*a1157835SDaniel Fojt 	    gas->last_mac_addr_rand.sec != 0 &&
766*a1157835SDaniel Fojt 	    !os_reltime_expired(&now, &gas->last_mac_addr_rand,
767*a1157835SDaniel Fojt 				wpa_s->conf->gas_rand_addr_lifetime)) {
768*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG,
769*a1157835SDaniel Fojt 			   "GAS: Use the previously selected random transmitter address "
770*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(gas->rand_addr));
771*a1157835SDaniel Fojt 		os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
772*a1157835SDaniel Fojt 		return 0;
773*a1157835SDaniel Fojt 	}
774*a1157835SDaniel Fojt 
775*a1157835SDaniel Fojt 	if (wpa_s->conf->gas_rand_mac_addr == 1 &&
776*a1157835SDaniel Fojt 	    random_mac_addr(gas->rand_addr) < 0) {
777*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
778*a1157835SDaniel Fojt 		return -1;
779*a1157835SDaniel Fojt 	}
780*a1157835SDaniel Fojt 
781*a1157835SDaniel Fojt 	if (wpa_s->conf->gas_rand_mac_addr == 2 &&
782*a1157835SDaniel Fojt 	    random_mac_addr_keep_oui(gas->rand_addr) < 0) {
783*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR,
784*a1157835SDaniel Fojt 			   "GAS: Failed to get random address with same OUI");
785*a1157835SDaniel Fojt 		return -1;
786*a1157835SDaniel Fojt 	}
787*a1157835SDaniel Fojt 
788*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
789*a1157835SDaniel Fojt 		   MACSTR, MAC2STR(gas->rand_addr));
790*a1157835SDaniel Fojt 	os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
791*a1157835SDaniel Fojt 	os_get_reltime(&gas->last_mac_addr_rand);
792*a1157835SDaniel Fojt 	gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
793*a1157835SDaniel Fojt 
794*a1157835SDaniel Fojt 	return 0;
7953ff40c12SJohn Marino }
7963ff40c12SJohn Marino 
7973ff40c12SJohn Marino 
7983ff40c12SJohn Marino /**
7993ff40c12SJohn Marino  * gas_query_req - Request a GAS query
8003ff40c12SJohn Marino  * @gas: GAS query data from gas_query_init()
8013ff40c12SJohn Marino  * @dst: Destination MAC address for the query
8023ff40c12SJohn Marino  * @freq: Frequency (in MHz) for the channel on which to send the query
8033ff40c12SJohn Marino  * @req: GAS query payload (to be freed by gas_query module in case of success
8043ff40c12SJohn Marino  *	return)
8053ff40c12SJohn Marino  * @cb: Callback function for reporting GAS query result and response
8063ff40c12SJohn Marino  * @ctx: Context pointer to use with the @cb call
8073ff40c12SJohn Marino  * Returns: dialog token (>= 0) on success or -1 on failure
8083ff40c12SJohn Marino  */
gas_query_req(struct gas_query * gas,const u8 * dst,int freq,int wildcard_bssid,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)8093ff40c12SJohn Marino int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
810*a1157835SDaniel Fojt 		  int wildcard_bssid, struct wpabuf *req,
8113ff40c12SJohn Marino 		  void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
8123ff40c12SJohn Marino 			     enum gas_query_result result,
8133ff40c12SJohn Marino 			     const struct wpabuf *adv_proto,
8143ff40c12SJohn Marino 			     const struct wpabuf *resp, u16 status_code),
8153ff40c12SJohn Marino 		  void *ctx)
8163ff40c12SJohn Marino {
8173ff40c12SJohn Marino 	struct gas_query_pending *query;
8183ff40c12SJohn Marino 	int dialog_token;
8193ff40c12SJohn Marino 
8203ff40c12SJohn Marino 	if (wpabuf_len(req) < 3)
8213ff40c12SJohn Marino 		return -1;
8223ff40c12SJohn Marino 
823*a1157835SDaniel Fojt 	dialog_token = gas_query_new_dialog_token(gas, dst);
824*a1157835SDaniel Fojt 	if (dialog_token < 0)
825*a1157835SDaniel Fojt 		return -1;
8263ff40c12SJohn Marino 
8273ff40c12SJohn Marino 	query = os_zalloc(sizeof(*query));
8283ff40c12SJohn Marino 	if (query == NULL)
8293ff40c12SJohn Marino 		return -1;
8303ff40c12SJohn Marino 
8313ff40c12SJohn Marino 	query->gas = gas;
832*a1157835SDaniel Fojt 	if (gas_query_set_sa(gas, query)) {
833*a1157835SDaniel Fojt 		os_free(query);
834*a1157835SDaniel Fojt 		return -1;
835*a1157835SDaniel Fojt 	}
8363ff40c12SJohn Marino 	os_memcpy(query->addr, dst, ETH_ALEN);
8373ff40c12SJohn Marino 	query->dialog_token = dialog_token;
838*a1157835SDaniel Fojt 	query->wildcard_bssid = !!wildcard_bssid;
8393ff40c12SJohn Marino 	query->freq = freq;
8403ff40c12SJohn Marino 	query->cb = cb;
8413ff40c12SJohn Marino 	query->ctx = ctx;
8423ff40c12SJohn Marino 	query->req = req;
8433ff40c12SJohn Marino 	dl_list_add(&gas->pending, &query->list);
8443ff40c12SJohn Marino 
8453ff40c12SJohn Marino 	*(wpabuf_mhead_u8(req) + 2) = dialog_token;
8463ff40c12SJohn Marino 
8473ff40c12SJohn Marino 	wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
8483ff40c12SJohn Marino 		" dialog_token=%u freq=%d",
8493ff40c12SJohn Marino 		MAC2STR(query->addr), query->dialog_token, query->freq);
8503ff40c12SJohn Marino 
8513ff40c12SJohn Marino 	if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
8523ff40c12SJohn Marino 			   query) < 0) {
853*a1157835SDaniel Fojt 		query->req = NULL; /* caller will free this in error case */
8543ff40c12SJohn Marino 		gas_query_free(query, 1);
8553ff40c12SJohn Marino 		return -1;
8563ff40c12SJohn Marino 	}
8573ff40c12SJohn Marino 
8583ff40c12SJohn Marino 	return dialog_token;
8593ff40c12SJohn Marino }
8603ff40c12SJohn Marino 
8613ff40c12SJohn Marino 
gas_query_stop(struct gas_query * gas,u8 dialog_token)862*a1157835SDaniel Fojt int gas_query_stop(struct gas_query *gas, u8 dialog_token)
8633ff40c12SJohn Marino {
8643ff40c12SJohn Marino 	struct gas_query_pending *query;
8653ff40c12SJohn Marino 
866*a1157835SDaniel Fojt 	dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) {
867*a1157835SDaniel Fojt 		if (query->dialog_token == dialog_token) {
868*a1157835SDaniel Fojt 			if (!gas->work) {
869*a1157835SDaniel Fojt 				/* The pending radio work has not yet been
870*a1157835SDaniel Fojt 				 * started, but the pending entry has a
871*a1157835SDaniel Fojt 				 * reference to the soon to be freed query.
872*a1157835SDaniel Fojt 				 * Need to remove that radio work now to avoid
873*a1157835SDaniel Fojt 				 * leaving behind a reference to freed memory.
874*a1157835SDaniel Fojt 				 */
875*a1157835SDaniel Fojt 				radio_remove_pending_work(gas->wpa_s, query);
876*a1157835SDaniel Fojt 			}
877*a1157835SDaniel Fojt 			gas_query_done(gas, query, GAS_QUERY_STOPPED);
878*a1157835SDaniel Fojt 			return 0;
879*a1157835SDaniel Fojt 		}
880*a1157835SDaniel Fojt 	}
8813ff40c12SJohn Marino 
882*a1157835SDaniel Fojt 	return -1;
8833ff40c12SJohn Marino }
884