1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt  * Generic advertisement service (GAS) query (hostapd)
3*a1157835SDaniel Fojt  * Copyright (c) 2009, Atheros Communications
4*a1157835SDaniel Fojt  * Copyright (c) 2011-2017, Qualcomm Atheros, Inc.
5*a1157835SDaniel Fojt  * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6*a1157835SDaniel Fojt  *
7*a1157835SDaniel Fojt  * This software may be distributed under the terms of the BSD license.
8*a1157835SDaniel Fojt  * See README for more details.
9*a1157835SDaniel Fojt  */
10*a1157835SDaniel Fojt 
11*a1157835SDaniel Fojt #include "includes.h"
12*a1157835SDaniel Fojt 
13*a1157835SDaniel Fojt #include "common.h"
14*a1157835SDaniel Fojt #include "utils/eloop.h"
15*a1157835SDaniel Fojt #include "utils/list.h"
16*a1157835SDaniel Fojt #include "common/ieee802_11_defs.h"
17*a1157835SDaniel Fojt #include "common/gas.h"
18*a1157835SDaniel Fojt #include "common/wpa_ctrl.h"
19*a1157835SDaniel Fojt #include "hostapd.h"
20*a1157835SDaniel Fojt #include "sta_info.h"
21*a1157835SDaniel Fojt #include "ap_drv_ops.h"
22*a1157835SDaniel Fojt #include "gas_query_ap.h"
23*a1157835SDaniel Fojt 
24*a1157835SDaniel Fojt 
25*a1157835SDaniel Fojt /** GAS query timeout in seconds */
26*a1157835SDaniel Fojt #define GAS_QUERY_TIMEOUT_PERIOD 2
27*a1157835SDaniel Fojt 
28*a1157835SDaniel Fojt /* GAS query wait-time / duration in ms */
29*a1157835SDaniel Fojt #define GAS_QUERY_WAIT_TIME_INITIAL 1000
30*a1157835SDaniel Fojt #define GAS_QUERY_WAIT_TIME_COMEBACK 150
31*a1157835SDaniel Fojt 
32*a1157835SDaniel Fojt /**
33*a1157835SDaniel Fojt  * struct gas_query_pending - Pending GAS query
34*a1157835SDaniel Fojt  */
35*a1157835SDaniel Fojt struct gas_query_pending {
36*a1157835SDaniel Fojt 	struct dl_list list;
37*a1157835SDaniel Fojt 	struct gas_query_ap *gas;
38*a1157835SDaniel Fojt 	u8 addr[ETH_ALEN];
39*a1157835SDaniel Fojt 	u8 dialog_token;
40*a1157835SDaniel Fojt 	u8 next_frag_id;
41*a1157835SDaniel Fojt 	unsigned int wait_comeback:1;
42*a1157835SDaniel Fojt 	unsigned int offchannel_tx_started:1;
43*a1157835SDaniel Fojt 	unsigned int retry:1;
44*a1157835SDaniel Fojt 	int freq;
45*a1157835SDaniel Fojt 	u16 status_code;
46*a1157835SDaniel Fojt 	struct wpabuf *req;
47*a1157835SDaniel Fojt 	struct wpabuf *adv_proto;
48*a1157835SDaniel Fojt 	struct wpabuf *resp;
49*a1157835SDaniel Fojt 	struct os_reltime last_oper;
50*a1157835SDaniel Fojt 	void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
51*a1157835SDaniel Fojt 		   enum gas_query_ap_result result,
52*a1157835SDaniel Fojt 		   const struct wpabuf *adv_proto,
53*a1157835SDaniel Fojt 		   const struct wpabuf *resp, u16 status_code);
54*a1157835SDaniel Fojt 	void *ctx;
55*a1157835SDaniel Fojt 	u8 sa[ETH_ALEN];
56*a1157835SDaniel Fojt };
57*a1157835SDaniel Fojt 
58*a1157835SDaniel Fojt /**
59*a1157835SDaniel Fojt  * struct gas_query_ap - Internal GAS query data
60*a1157835SDaniel Fojt  */
61*a1157835SDaniel Fojt struct gas_query_ap {
62*a1157835SDaniel Fojt 	struct hostapd_data *hapd;
63*a1157835SDaniel Fojt 	void *msg_ctx;
64*a1157835SDaniel Fojt 	struct dl_list pending; /* struct gas_query_pending */
65*a1157835SDaniel Fojt 	struct gas_query_pending *current;
66*a1157835SDaniel Fojt };
67*a1157835SDaniel Fojt 
68*a1157835SDaniel Fojt 
69*a1157835SDaniel Fojt static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
70*a1157835SDaniel Fojt static void gas_query_timeout(void *eloop_data, void *user_ctx);
71*a1157835SDaniel Fojt static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
72*a1157835SDaniel Fojt static void gas_query_tx_initial_req(struct gas_query_ap *gas,
73*a1157835SDaniel Fojt 				     struct gas_query_pending *query);
74*a1157835SDaniel Fojt static int gas_query_new_dialog_token(struct gas_query_ap *gas, const u8 *dst);
75*a1157835SDaniel Fojt 
76*a1157835SDaniel Fojt 
ms_from_time(struct os_reltime * last)77*a1157835SDaniel Fojt static int ms_from_time(struct os_reltime *last)
78*a1157835SDaniel Fojt {
79*a1157835SDaniel Fojt 	struct os_reltime now, res;
80*a1157835SDaniel Fojt 
81*a1157835SDaniel Fojt 	os_get_reltime(&now);
82*a1157835SDaniel Fojt 	os_reltime_sub(&now, last, &res);
83*a1157835SDaniel Fojt 	return res.sec * 1000 + res.usec / 1000;
84*a1157835SDaniel Fojt }
85*a1157835SDaniel Fojt 
86*a1157835SDaniel Fojt 
87*a1157835SDaniel Fojt /**
88*a1157835SDaniel Fojt  * gas_query_ap_init - Initialize GAS query component
89*a1157835SDaniel Fojt  * @hapd: Pointer to hostapd data
90*a1157835SDaniel Fojt  * Returns: Pointer to GAS query data or %NULL on failure
91*a1157835SDaniel Fojt  */
gas_query_ap_init(struct hostapd_data * hapd,void * msg_ctx)92*a1157835SDaniel Fojt struct gas_query_ap * gas_query_ap_init(struct hostapd_data *hapd,
93*a1157835SDaniel Fojt 					void *msg_ctx)
94*a1157835SDaniel Fojt {
95*a1157835SDaniel Fojt 	struct gas_query_ap *gas;
96*a1157835SDaniel Fojt 
97*a1157835SDaniel Fojt 	gas = os_zalloc(sizeof(*gas));
98*a1157835SDaniel Fojt 	if (!gas)
99*a1157835SDaniel Fojt 		return NULL;
100*a1157835SDaniel Fojt 
101*a1157835SDaniel Fojt 	gas->hapd = hapd;
102*a1157835SDaniel Fojt 	gas->msg_ctx = msg_ctx;
103*a1157835SDaniel Fojt 	dl_list_init(&gas->pending);
104*a1157835SDaniel Fojt 
105*a1157835SDaniel Fojt 	return gas;
106*a1157835SDaniel Fojt }
107*a1157835SDaniel Fojt 
108*a1157835SDaniel Fojt 
gas_result_txt(enum gas_query_ap_result result)109*a1157835SDaniel Fojt static const char * gas_result_txt(enum gas_query_ap_result result)
110*a1157835SDaniel Fojt {
111*a1157835SDaniel Fojt 	switch (result) {
112*a1157835SDaniel Fojt 	case GAS_QUERY_AP_SUCCESS:
113*a1157835SDaniel Fojt 		return "SUCCESS";
114*a1157835SDaniel Fojt 	case GAS_QUERY_AP_FAILURE:
115*a1157835SDaniel Fojt 		return "FAILURE";
116*a1157835SDaniel Fojt 	case GAS_QUERY_AP_TIMEOUT:
117*a1157835SDaniel Fojt 		return "TIMEOUT";
118*a1157835SDaniel Fojt 	case GAS_QUERY_AP_PEER_ERROR:
119*a1157835SDaniel Fojt 		return "PEER_ERROR";
120*a1157835SDaniel Fojt 	case GAS_QUERY_AP_INTERNAL_ERROR:
121*a1157835SDaniel Fojt 		return "INTERNAL_ERROR";
122*a1157835SDaniel Fojt 	case GAS_QUERY_AP_DELETED_AT_DEINIT:
123*a1157835SDaniel Fojt 		return "DELETED_AT_DEINIT";
124*a1157835SDaniel Fojt 	}
125*a1157835SDaniel Fojt 
126*a1157835SDaniel Fojt 	return "N/A";
127*a1157835SDaniel Fojt }
128*a1157835SDaniel Fojt 
129*a1157835SDaniel Fojt 
gas_query_free(struct gas_query_pending * query,int del_list)130*a1157835SDaniel Fojt static void gas_query_free(struct gas_query_pending *query, int del_list)
131*a1157835SDaniel Fojt {
132*a1157835SDaniel Fojt 	if (del_list)
133*a1157835SDaniel Fojt 		dl_list_del(&query->list);
134*a1157835SDaniel Fojt 
135*a1157835SDaniel Fojt 	wpabuf_free(query->req);
136*a1157835SDaniel Fojt 	wpabuf_free(query->adv_proto);
137*a1157835SDaniel Fojt 	wpabuf_free(query->resp);
138*a1157835SDaniel Fojt 	os_free(query);
139*a1157835SDaniel Fojt }
140*a1157835SDaniel Fojt 
141*a1157835SDaniel Fojt 
gas_query_done(struct gas_query_ap * gas,struct gas_query_pending * query,enum gas_query_ap_result result)142*a1157835SDaniel Fojt static void gas_query_done(struct gas_query_ap *gas,
143*a1157835SDaniel Fojt 			   struct gas_query_pending *query,
144*a1157835SDaniel Fojt 			   enum gas_query_ap_result result)
145*a1157835SDaniel Fojt {
146*a1157835SDaniel Fojt 	wpa_msg(gas->msg_ctx, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
147*a1157835SDaniel Fojt 		" dialog_token=%u freq=%d status_code=%u result=%s",
148*a1157835SDaniel Fojt 		MAC2STR(query->addr), query->dialog_token, query->freq,
149*a1157835SDaniel Fojt 		query->status_code, gas_result_txt(result));
150*a1157835SDaniel Fojt 	if (gas->current == query)
151*a1157835SDaniel Fojt 		gas->current = NULL;
152*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
153*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_timeout, gas, query);
154*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
155*a1157835SDaniel Fojt 	dl_list_del(&query->list);
156*a1157835SDaniel Fojt 	query->cb(query->ctx, query->addr, query->dialog_token, result,
157*a1157835SDaniel Fojt 		  query->adv_proto, query->resp, query->status_code);
158*a1157835SDaniel Fojt 	gas_query_free(query, 0);
159*a1157835SDaniel Fojt }
160*a1157835SDaniel Fojt 
161*a1157835SDaniel Fojt 
162*a1157835SDaniel Fojt /**
163*a1157835SDaniel Fojt  * gas_query_ap_deinit - Deinitialize GAS query component
164*a1157835SDaniel Fojt  * @gas: GAS query data from gas_query_init()
165*a1157835SDaniel Fojt  */
gas_query_ap_deinit(struct gas_query_ap * gas)166*a1157835SDaniel Fojt void gas_query_ap_deinit(struct gas_query_ap *gas)
167*a1157835SDaniel Fojt {
168*a1157835SDaniel Fojt 	struct gas_query_pending *query, *next;
169*a1157835SDaniel Fojt 
170*a1157835SDaniel Fojt 	if (gas == NULL)
171*a1157835SDaniel Fojt 		return;
172*a1157835SDaniel Fojt 
173*a1157835SDaniel Fojt 	dl_list_for_each_safe(query, next, &gas->pending,
174*a1157835SDaniel Fojt 			      struct gas_query_pending, list)
175*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_DELETED_AT_DEINIT);
176*a1157835SDaniel Fojt 
177*a1157835SDaniel Fojt 	os_free(gas);
178*a1157835SDaniel Fojt }
179*a1157835SDaniel Fojt 
180*a1157835SDaniel Fojt 
181*a1157835SDaniel Fojt static struct gas_query_pending *
gas_query_get_pending(struct gas_query_ap * gas,const u8 * addr,u8 dialog_token)182*a1157835SDaniel Fojt gas_query_get_pending(struct gas_query_ap *gas, const u8 *addr, u8 dialog_token)
183*a1157835SDaniel Fojt {
184*a1157835SDaniel Fojt 	struct gas_query_pending *q;
185*a1157835SDaniel Fojt 	dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
186*a1157835SDaniel Fojt 		if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
187*a1157835SDaniel Fojt 		    q->dialog_token == dialog_token)
188*a1157835SDaniel Fojt 			return q;
189*a1157835SDaniel Fojt 	}
190*a1157835SDaniel Fojt 	return NULL;
191*a1157835SDaniel Fojt }
192*a1157835SDaniel Fojt 
193*a1157835SDaniel Fojt 
gas_query_append(struct gas_query_pending * query,const u8 * data,size_t len)194*a1157835SDaniel Fojt static int gas_query_append(struct gas_query_pending *query, const u8 *data,
195*a1157835SDaniel Fojt 			    size_t len)
196*a1157835SDaniel Fojt {
197*a1157835SDaniel Fojt 	if (wpabuf_resize(&query->resp, len) < 0) {
198*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
199*a1157835SDaniel Fojt 		return -1;
200*a1157835SDaniel Fojt 	}
201*a1157835SDaniel Fojt 	wpabuf_put_data(query->resp, data, len);
202*a1157835SDaniel Fojt 	return 0;
203*a1157835SDaniel Fojt }
204*a1157835SDaniel Fojt 
205*a1157835SDaniel Fojt 
gas_query_ap_tx_status(struct gas_query_ap * gas,const u8 * dst,const u8 * data,size_t data_len,int ok)206*a1157835SDaniel Fojt void gas_query_ap_tx_status(struct gas_query_ap *gas, const u8 *dst,
207*a1157835SDaniel Fojt 			    const u8 *data, size_t data_len, int ok)
208*a1157835SDaniel Fojt {
209*a1157835SDaniel Fojt 	struct gas_query_pending *query;
210*a1157835SDaniel Fojt 	int dur;
211*a1157835SDaniel Fojt 
212*a1157835SDaniel Fojt 	if (!gas || !gas->current) {
213*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: dst=" MACSTR
214*a1157835SDaniel Fojt 			   " ok=%d - no query in progress", MAC2STR(dst), ok);
215*a1157835SDaniel Fojt 		return;
216*a1157835SDaniel Fojt 	}
217*a1157835SDaniel Fojt 
218*a1157835SDaniel Fojt 	query = gas->current;
219*a1157835SDaniel Fojt 
220*a1157835SDaniel Fojt 	dur = ms_from_time(&query->last_oper);
221*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: TX status: dst=" MACSTR
222*a1157835SDaniel Fojt 		   " ok=%d query=%p dialog_token=%u dur=%d ms",
223*a1157835SDaniel Fojt 		   MAC2STR(dst), ok, query, query->dialog_token, dur);
224*a1157835SDaniel Fojt 	if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
225*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
226*a1157835SDaniel Fojt 		return;
227*a1157835SDaniel Fojt 	}
228*a1157835SDaniel Fojt 	os_get_reltime(&query->last_oper);
229*a1157835SDaniel Fojt 
230*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_timeout, gas, query);
231*a1157835SDaniel Fojt 	if (!ok) {
232*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
233*a1157835SDaniel Fojt 		eloop_register_timeout(0, 250000, gas_query_timeout,
234*a1157835SDaniel Fojt 				       gas, query);
235*a1157835SDaniel Fojt 	} else {
236*a1157835SDaniel Fojt 		eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
237*a1157835SDaniel Fojt 				       gas_query_timeout, gas, query);
238*a1157835SDaniel Fojt 	}
239*a1157835SDaniel Fojt 	if (query->wait_comeback && !query->retry) {
240*a1157835SDaniel Fojt 		eloop_cancel_timeout(gas_query_rx_comeback_timeout,
241*a1157835SDaniel Fojt 				     gas, query);
242*a1157835SDaniel Fojt 		eloop_register_timeout(
243*a1157835SDaniel Fojt 			0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
244*a1157835SDaniel Fojt 			gas_query_rx_comeback_timeout, gas, query);
245*a1157835SDaniel Fojt 	}
246*a1157835SDaniel Fojt }
247*a1157835SDaniel Fojt 
248*a1157835SDaniel Fojt 
pmf_in_use(struct hostapd_data * hapd,const u8 * addr)249*a1157835SDaniel Fojt static int pmf_in_use(struct hostapd_data *hapd, const u8 *addr)
250*a1157835SDaniel Fojt {
251*a1157835SDaniel Fojt 	struct sta_info *sta;
252*a1157835SDaniel Fojt 
253*a1157835SDaniel Fojt 	sta = ap_get_sta(hapd, addr);
254*a1157835SDaniel Fojt 	return sta && (sta->flags & WLAN_STA_MFP);
255*a1157835SDaniel Fojt }
256*a1157835SDaniel Fojt 
257*a1157835SDaniel Fojt 
gas_query_tx(struct gas_query_ap * gas,struct gas_query_pending * query,struct wpabuf * req,unsigned int wait_time)258*a1157835SDaniel Fojt static int gas_query_tx(struct gas_query_ap *gas,
259*a1157835SDaniel Fojt 			struct gas_query_pending *query,
260*a1157835SDaniel Fojt 			struct wpabuf *req, unsigned int wait_time)
261*a1157835SDaniel Fojt {
262*a1157835SDaniel Fojt 	int res, prot = pmf_in_use(gas->hapd, query->addr);
263*a1157835SDaniel Fojt 
264*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
265*a1157835SDaniel Fojt 		   "freq=%d prot=%d using src addr " MACSTR,
266*a1157835SDaniel Fojt 		   MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
267*a1157835SDaniel Fojt 		   query->freq, prot, MAC2STR(query->sa));
268*a1157835SDaniel Fojt 	if (prot) {
269*a1157835SDaniel Fojt 		u8 *categ = wpabuf_mhead_u8(req);
270*a1157835SDaniel Fojt 		*categ = WLAN_ACTION_PROTECTED_DUAL;
271*a1157835SDaniel Fojt 	}
272*a1157835SDaniel Fojt 	os_get_reltime(&query->last_oper);
273*a1157835SDaniel Fojt 	res = hostapd_drv_send_action(gas->hapd, query->freq, wait_time,
274*a1157835SDaniel Fojt 				      query->addr, wpabuf_head(req),
275*a1157835SDaniel Fojt 				      wpabuf_len(req));
276*a1157835SDaniel Fojt 	return res;
277*a1157835SDaniel Fojt }
278*a1157835SDaniel Fojt 
279*a1157835SDaniel Fojt 
gas_query_tx_comeback_req(struct gas_query_ap * gas,struct gas_query_pending * query)280*a1157835SDaniel Fojt static void gas_query_tx_comeback_req(struct gas_query_ap *gas,
281*a1157835SDaniel Fojt 				      struct gas_query_pending *query)
282*a1157835SDaniel Fojt {
283*a1157835SDaniel Fojt 	struct wpabuf *req;
284*a1157835SDaniel Fojt 	unsigned int wait_time;
285*a1157835SDaniel Fojt 
286*a1157835SDaniel Fojt 	req = gas_build_comeback_req(query->dialog_token);
287*a1157835SDaniel Fojt 	if (req == NULL) {
288*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_INTERNAL_ERROR);
289*a1157835SDaniel Fojt 		return;
290*a1157835SDaniel Fojt 	}
291*a1157835SDaniel Fojt 
292*a1157835SDaniel Fojt 	wait_time = (query->retry || !query->offchannel_tx_started) ?
293*a1157835SDaniel Fojt 		GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
294*a1157835SDaniel Fojt 
295*a1157835SDaniel Fojt 	if (gas_query_tx(gas, query, req, wait_time) < 0) {
296*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
297*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(query->addr));
298*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_INTERNAL_ERROR);
299*a1157835SDaniel Fojt 	}
300*a1157835SDaniel Fojt 
301*a1157835SDaniel Fojt 	wpabuf_free(req);
302*a1157835SDaniel Fojt }
303*a1157835SDaniel Fojt 
304*a1157835SDaniel Fojt 
gas_query_rx_comeback_timeout(void * eloop_data,void * user_ctx)305*a1157835SDaniel Fojt static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
306*a1157835SDaniel Fojt {
307*a1157835SDaniel Fojt 	struct gas_query_ap *gas = eloop_data;
308*a1157835SDaniel Fojt 	struct gas_query_pending *query = user_ctx;
309*a1157835SDaniel Fojt 	int dialog_token;
310*a1157835SDaniel Fojt 
311*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
312*a1157835SDaniel Fojt 		   "GAS: No response to comeback request received (retry=%u)",
313*a1157835SDaniel Fojt 		   query->retry);
314*a1157835SDaniel Fojt 	if (gas->current != query || query->retry)
315*a1157835SDaniel Fojt 		return;
316*a1157835SDaniel Fojt 	dialog_token = gas_query_new_dialog_token(gas, query->addr);
317*a1157835SDaniel Fojt 	if (dialog_token < 0)
318*a1157835SDaniel Fojt 		return;
319*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG,
320*a1157835SDaniel Fojt 		   "GAS: Retry GAS query due to comeback response timeout");
321*a1157835SDaniel Fojt 	query->retry = 1;
322*a1157835SDaniel Fojt 	query->dialog_token = dialog_token;
323*a1157835SDaniel Fojt 	*(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
324*a1157835SDaniel Fojt 	query->wait_comeback = 0;
325*a1157835SDaniel Fojt 	query->next_frag_id = 0;
326*a1157835SDaniel Fojt 	wpabuf_free(query->adv_proto);
327*a1157835SDaniel Fojt 	query->adv_proto = NULL;
328*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
329*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_timeout, gas, query);
330*a1157835SDaniel Fojt 	gas_query_tx_initial_req(gas, query);
331*a1157835SDaniel Fojt }
332*a1157835SDaniel Fojt 
333*a1157835SDaniel Fojt 
gas_query_tx_comeback_timeout(void * eloop_data,void * user_ctx)334*a1157835SDaniel Fojt static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
335*a1157835SDaniel Fojt {
336*a1157835SDaniel Fojt 	struct gas_query_ap *gas = eloop_data;
337*a1157835SDaniel Fojt 	struct gas_query_pending *query = user_ctx;
338*a1157835SDaniel Fojt 
339*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
340*a1157835SDaniel Fojt 		   MAC2STR(query->addr));
341*a1157835SDaniel Fojt 	gas_query_tx_comeback_req(gas, query);
342*a1157835SDaniel Fojt }
343*a1157835SDaniel Fojt 
344*a1157835SDaniel Fojt 
gas_query_tx_comeback_req_delay(struct gas_query_ap * gas,struct gas_query_pending * query,u16 comeback_delay)345*a1157835SDaniel Fojt static void gas_query_tx_comeback_req_delay(struct gas_query_ap *gas,
346*a1157835SDaniel Fojt 					    struct gas_query_pending *query,
347*a1157835SDaniel Fojt 					    u16 comeback_delay)
348*a1157835SDaniel Fojt {
349*a1157835SDaniel Fojt 	unsigned int secs, usecs;
350*a1157835SDaniel Fojt 
351*a1157835SDaniel Fojt 	secs = (comeback_delay * 1024) / 1000000;
352*a1157835SDaniel Fojt 	usecs = comeback_delay * 1024 - secs * 1000000;
353*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
354*a1157835SDaniel Fojt 		   " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
355*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
356*a1157835SDaniel Fojt 	eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
357*a1157835SDaniel Fojt 			       gas, query);
358*a1157835SDaniel Fojt }
359*a1157835SDaniel Fojt 
360*a1157835SDaniel Fojt 
gas_query_rx_initial(struct gas_query_ap * gas,struct gas_query_pending * query,const u8 * adv_proto,const u8 * resp,size_t len,u16 comeback_delay)361*a1157835SDaniel Fojt static void gas_query_rx_initial(struct gas_query_ap *gas,
362*a1157835SDaniel Fojt 				 struct gas_query_pending *query,
363*a1157835SDaniel Fojt 				 const u8 *adv_proto, const u8 *resp,
364*a1157835SDaniel Fojt 				 size_t len, u16 comeback_delay)
365*a1157835SDaniel Fojt {
366*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
367*a1157835SDaniel Fojt 		   MACSTR " (dialog_token=%u comeback_delay=%u)",
368*a1157835SDaniel Fojt 		   MAC2STR(query->addr), query->dialog_token, comeback_delay);
369*a1157835SDaniel Fojt 
370*a1157835SDaniel Fojt 	query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
371*a1157835SDaniel Fojt 	if (query->adv_proto == NULL) {
372*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_INTERNAL_ERROR);
373*a1157835SDaniel Fojt 		return;
374*a1157835SDaniel Fojt 	}
375*a1157835SDaniel Fojt 
376*a1157835SDaniel Fojt 	if (comeback_delay) {
377*a1157835SDaniel Fojt 		eloop_cancel_timeout(gas_query_timeout, gas, query);
378*a1157835SDaniel Fojt 		query->wait_comeback = 1;
379*a1157835SDaniel Fojt 		gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
380*a1157835SDaniel Fojt 		return;
381*a1157835SDaniel Fojt 	}
382*a1157835SDaniel Fojt 
383*a1157835SDaniel Fojt 	/* Query was completed without comeback mechanism */
384*a1157835SDaniel Fojt 	if (gas_query_append(query, resp, len) < 0) {
385*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_INTERNAL_ERROR);
386*a1157835SDaniel Fojt 		return;
387*a1157835SDaniel Fojt 	}
388*a1157835SDaniel Fojt 
389*a1157835SDaniel Fojt 	gas_query_done(gas, query, GAS_QUERY_AP_SUCCESS);
390*a1157835SDaniel Fojt }
391*a1157835SDaniel Fojt 
392*a1157835SDaniel Fojt 
gas_query_rx_comeback(struct gas_query_ap * 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)393*a1157835SDaniel Fojt static void gas_query_rx_comeback(struct gas_query_ap *gas,
394*a1157835SDaniel Fojt 				  struct gas_query_pending *query,
395*a1157835SDaniel Fojt 				  const u8 *adv_proto, const u8 *resp,
396*a1157835SDaniel Fojt 				  size_t len, u8 frag_id, u8 more_frags,
397*a1157835SDaniel Fojt 				  u16 comeback_delay)
398*a1157835SDaniel Fojt {
399*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
400*a1157835SDaniel Fojt 		   MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
401*a1157835SDaniel Fojt 		   "comeback_delay=%u)",
402*a1157835SDaniel Fojt 		   MAC2STR(query->addr), query->dialog_token, frag_id,
403*a1157835SDaniel Fojt 		   more_frags, comeback_delay);
404*a1157835SDaniel Fojt 	eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
405*a1157835SDaniel Fojt 
406*a1157835SDaniel Fojt 	if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
407*a1157835SDaniel Fojt 	    os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
408*a1157835SDaniel Fojt 		      wpabuf_len(query->adv_proto)) != 0) {
409*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
410*a1157835SDaniel Fojt 			   "between initial and comeback response from "
411*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(query->addr));
412*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_PEER_ERROR);
413*a1157835SDaniel Fojt 		return;
414*a1157835SDaniel Fojt 	}
415*a1157835SDaniel Fojt 
416*a1157835SDaniel Fojt 	if (comeback_delay) {
417*a1157835SDaniel Fojt 		if (frag_id) {
418*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
419*a1157835SDaniel Fojt 				   "with non-zero frag_id and comeback_delay "
420*a1157835SDaniel Fojt 				   "from " MACSTR, MAC2STR(query->addr));
421*a1157835SDaniel Fojt 			gas_query_done(gas, query, GAS_QUERY_AP_PEER_ERROR);
422*a1157835SDaniel Fojt 			return;
423*a1157835SDaniel Fojt 		}
424*a1157835SDaniel Fojt 		gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
425*a1157835SDaniel Fojt 		return;
426*a1157835SDaniel Fojt 	}
427*a1157835SDaniel Fojt 
428*a1157835SDaniel Fojt 	if (frag_id != query->next_frag_id) {
429*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
430*a1157835SDaniel Fojt 			   "from " MACSTR, MAC2STR(query->addr));
431*a1157835SDaniel Fojt 		if (frag_id + 1 == query->next_frag_id) {
432*a1157835SDaniel Fojt 			wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
433*a1157835SDaniel Fojt 				   "retry of previous fragment");
434*a1157835SDaniel Fojt 			return;
435*a1157835SDaniel Fojt 		}
436*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_PEER_ERROR);
437*a1157835SDaniel Fojt 		return;
438*a1157835SDaniel Fojt 	}
439*a1157835SDaniel Fojt 	query->next_frag_id++;
440*a1157835SDaniel Fojt 
441*a1157835SDaniel Fojt 	if (gas_query_append(query, resp, len) < 0) {
442*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_INTERNAL_ERROR);
443*a1157835SDaniel Fojt 		return;
444*a1157835SDaniel Fojt 	}
445*a1157835SDaniel Fojt 
446*a1157835SDaniel Fojt 	if (more_frags) {
447*a1157835SDaniel Fojt 		gas_query_tx_comeback_req(gas, query);
448*a1157835SDaniel Fojt 		return;
449*a1157835SDaniel Fojt 	}
450*a1157835SDaniel Fojt 
451*a1157835SDaniel Fojt 	gas_query_done(gas, query, GAS_QUERY_AP_SUCCESS);
452*a1157835SDaniel Fojt }
453*a1157835SDaniel Fojt 
454*a1157835SDaniel Fojt 
455*a1157835SDaniel Fojt /**
456*a1157835SDaniel Fojt  * gas_query_ap_rx - Indicate reception of a Public Action or Protected Dual
457*a1157835SDaniel Fojt  *	frame
458*a1157835SDaniel Fojt  * @gas: GAS query data from gas_query_init()
459*a1157835SDaniel Fojt  * @sa: Source MAC address of the Action frame
460*a1157835SDaniel Fojt  * @categ: Category of the Action frame
461*a1157835SDaniel Fojt  * @data: Payload of the Action frame
462*a1157835SDaniel Fojt  * @len: Length of @data
463*a1157835SDaniel Fojt  * @freq: Frequency (in MHz) on which the frame was received
464*a1157835SDaniel Fojt  * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
465*a1157835SDaniel Fojt  */
gas_query_ap_rx(struct gas_query_ap * gas,const u8 * sa,u8 categ,const u8 * data,size_t len,int freq)466*a1157835SDaniel Fojt int gas_query_ap_rx(struct gas_query_ap *gas, const u8 *sa, u8 categ,
467*a1157835SDaniel Fojt 		    const u8 *data, size_t len, int freq)
468*a1157835SDaniel Fojt {
469*a1157835SDaniel Fojt 	struct gas_query_pending *query;
470*a1157835SDaniel Fojt 	u8 action, dialog_token, frag_id = 0, more_frags = 0;
471*a1157835SDaniel Fojt 	u16 comeback_delay, resp_len;
472*a1157835SDaniel Fojt 	const u8 *pos, *adv_proto;
473*a1157835SDaniel Fojt 	int prot, pmf;
474*a1157835SDaniel Fojt 	unsigned int left;
475*a1157835SDaniel Fojt 
476*a1157835SDaniel Fojt 	if (!gas || len < 4)
477*a1157835SDaniel Fojt 		return -1;
478*a1157835SDaniel Fojt 
479*a1157835SDaniel Fojt 	pos = data;
480*a1157835SDaniel Fojt 	action = *pos++;
481*a1157835SDaniel Fojt 	dialog_token = *pos++;
482*a1157835SDaniel Fojt 
483*a1157835SDaniel Fojt 	if (action != WLAN_PA_GAS_INITIAL_RESP &&
484*a1157835SDaniel Fojt 	    action != WLAN_PA_GAS_COMEBACK_RESP)
485*a1157835SDaniel Fojt 		return -1; /* Not a GAS response */
486*a1157835SDaniel Fojt 
487*a1157835SDaniel Fojt 	prot = categ == WLAN_ACTION_PROTECTED_DUAL;
488*a1157835SDaniel Fojt 	pmf = pmf_in_use(gas->hapd, sa);
489*a1157835SDaniel Fojt 	if (prot && !pmf) {
490*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
491*a1157835SDaniel Fojt 		return 0;
492*a1157835SDaniel Fojt 	}
493*a1157835SDaniel Fojt 	if (!prot && pmf) {
494*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
495*a1157835SDaniel Fojt 		return 0;
496*a1157835SDaniel Fojt 	}
497*a1157835SDaniel Fojt 
498*a1157835SDaniel Fojt 	query = gas_query_get_pending(gas, sa, dialog_token);
499*a1157835SDaniel Fojt 	if (query == NULL) {
500*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
501*a1157835SDaniel Fojt 			   " dialog token %u", MAC2STR(sa), dialog_token);
502*a1157835SDaniel Fojt 		return -1;
503*a1157835SDaniel Fojt 	}
504*a1157835SDaniel Fojt 
505*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
506*a1157835SDaniel Fojt 		   ms_from_time(&query->last_oper), MAC2STR(sa));
507*a1157835SDaniel Fojt 
508*a1157835SDaniel Fojt 	if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
509*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
510*a1157835SDaniel Fojt 			   MACSTR " dialog token %u when waiting for comeback "
511*a1157835SDaniel Fojt 			   "response", MAC2STR(sa), dialog_token);
512*a1157835SDaniel Fojt 		return 0;
513*a1157835SDaniel Fojt 	}
514*a1157835SDaniel Fojt 
515*a1157835SDaniel Fojt 	if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
516*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
517*a1157835SDaniel Fojt 			   MACSTR " dialog token %u when waiting for initial "
518*a1157835SDaniel Fojt 			   "response", MAC2STR(sa), dialog_token);
519*a1157835SDaniel Fojt 		return 0;
520*a1157835SDaniel Fojt 	}
521*a1157835SDaniel Fojt 
522*a1157835SDaniel Fojt 	query->status_code = WPA_GET_LE16(pos);
523*a1157835SDaniel Fojt 	pos += 2;
524*a1157835SDaniel Fojt 
525*a1157835SDaniel Fojt 	if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
526*a1157835SDaniel Fojt 	    action == WLAN_PA_GAS_COMEBACK_RESP) {
527*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
528*a1157835SDaniel Fojt 	} else if (query->status_code != WLAN_STATUS_SUCCESS) {
529*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
530*a1157835SDaniel Fojt 			   "%u failed - status code %u",
531*a1157835SDaniel Fojt 			   MAC2STR(sa), dialog_token, query->status_code);
532*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_FAILURE);
533*a1157835SDaniel Fojt 		return 0;
534*a1157835SDaniel Fojt 	}
535*a1157835SDaniel Fojt 
536*a1157835SDaniel Fojt 	if (action == WLAN_PA_GAS_COMEBACK_RESP) {
537*a1157835SDaniel Fojt 		if (pos + 1 > data + len)
538*a1157835SDaniel Fojt 			return 0;
539*a1157835SDaniel Fojt 		frag_id = *pos & 0x7f;
540*a1157835SDaniel Fojt 		more_frags = (*pos & 0x80) >> 7;
541*a1157835SDaniel Fojt 		pos++;
542*a1157835SDaniel Fojt 	}
543*a1157835SDaniel Fojt 
544*a1157835SDaniel Fojt 	/* Comeback Delay */
545*a1157835SDaniel Fojt 	if (pos + 2 > data + len)
546*a1157835SDaniel Fojt 		return 0;
547*a1157835SDaniel Fojt 	comeback_delay = WPA_GET_LE16(pos);
548*a1157835SDaniel Fojt 	pos += 2;
549*a1157835SDaniel Fojt 
550*a1157835SDaniel Fojt 	/* Advertisement Protocol element */
551*a1157835SDaniel Fojt 	if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
552*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
553*a1157835SDaniel Fojt 			   "Protocol element in the response from " MACSTR,
554*a1157835SDaniel Fojt 			   MAC2STR(sa));
555*a1157835SDaniel Fojt 		return 0;
556*a1157835SDaniel Fojt 	}
557*a1157835SDaniel Fojt 
558*a1157835SDaniel Fojt 	if (*pos != WLAN_EID_ADV_PROTO) {
559*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
560*a1157835SDaniel Fojt 			   "Protocol element ID %u in response from " MACSTR,
561*a1157835SDaniel Fojt 			   *pos, MAC2STR(sa));
562*a1157835SDaniel Fojt 		return 0;
563*a1157835SDaniel Fojt 	}
564*a1157835SDaniel Fojt 
565*a1157835SDaniel Fojt 	adv_proto = pos;
566*a1157835SDaniel Fojt 	pos += 2 + pos[1];
567*a1157835SDaniel Fojt 
568*a1157835SDaniel Fojt 	/* Query Response Length */
569*a1157835SDaniel Fojt 	if (pos + 2 > data + len) {
570*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
571*a1157835SDaniel Fojt 		return 0;
572*a1157835SDaniel Fojt 	}
573*a1157835SDaniel Fojt 	resp_len = WPA_GET_LE16(pos);
574*a1157835SDaniel Fojt 	pos += 2;
575*a1157835SDaniel Fojt 
576*a1157835SDaniel Fojt 	left = data + len - pos;
577*a1157835SDaniel Fojt 	if (resp_len > left) {
578*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
579*a1157835SDaniel Fojt 			   "response from " MACSTR, MAC2STR(sa));
580*a1157835SDaniel Fojt 		return 0;
581*a1157835SDaniel Fojt 	}
582*a1157835SDaniel Fojt 
583*a1157835SDaniel Fojt 	if (resp_len < left) {
584*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
585*a1157835SDaniel Fojt 			   "after Query Response from " MACSTR,
586*a1157835SDaniel Fojt 			   left - resp_len, MAC2STR(sa));
587*a1157835SDaniel Fojt 	}
588*a1157835SDaniel Fojt 
589*a1157835SDaniel Fojt 	if (action == WLAN_PA_GAS_COMEBACK_RESP)
590*a1157835SDaniel Fojt 		gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
591*a1157835SDaniel Fojt 				      frag_id, more_frags, comeback_delay);
592*a1157835SDaniel Fojt 	else
593*a1157835SDaniel Fojt 		gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
594*a1157835SDaniel Fojt 				     comeback_delay);
595*a1157835SDaniel Fojt 
596*a1157835SDaniel Fojt 	return 0;
597*a1157835SDaniel Fojt }
598*a1157835SDaniel Fojt 
599*a1157835SDaniel Fojt 
gas_query_timeout(void * eloop_data,void * user_ctx)600*a1157835SDaniel Fojt static void gas_query_timeout(void *eloop_data, void *user_ctx)
601*a1157835SDaniel Fojt {
602*a1157835SDaniel Fojt 	struct gas_query_ap *gas = eloop_data;
603*a1157835SDaniel Fojt 	struct gas_query_pending *query = user_ctx;
604*a1157835SDaniel Fojt 
605*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
606*a1157835SDaniel Fojt 		   " dialog token %u",
607*a1157835SDaniel Fojt 		   MAC2STR(query->addr), query->dialog_token);
608*a1157835SDaniel Fojt 	gas_query_done(gas, query, GAS_QUERY_AP_TIMEOUT);
609*a1157835SDaniel Fojt }
610*a1157835SDaniel Fojt 
611*a1157835SDaniel Fojt 
gas_query_dialog_token_available(struct gas_query_ap * gas,const u8 * dst,u8 dialog_token)612*a1157835SDaniel Fojt static int gas_query_dialog_token_available(struct gas_query_ap *gas,
613*a1157835SDaniel Fojt 					    const u8 *dst, u8 dialog_token)
614*a1157835SDaniel Fojt {
615*a1157835SDaniel Fojt 	struct gas_query_pending *q;
616*a1157835SDaniel Fojt 	dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
617*a1157835SDaniel Fojt 		if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
618*a1157835SDaniel Fojt 		    dialog_token == q->dialog_token)
619*a1157835SDaniel Fojt 			return 0;
620*a1157835SDaniel Fojt 	}
621*a1157835SDaniel Fojt 
622*a1157835SDaniel Fojt 	return 1;
623*a1157835SDaniel Fojt }
624*a1157835SDaniel Fojt 
625*a1157835SDaniel Fojt 
gas_query_tx_initial_req(struct gas_query_ap * gas,struct gas_query_pending * query)626*a1157835SDaniel Fojt static void gas_query_tx_initial_req(struct gas_query_ap *gas,
627*a1157835SDaniel Fojt 				     struct gas_query_pending *query)
628*a1157835SDaniel Fojt {
629*a1157835SDaniel Fojt 	if (gas_query_tx(gas, query, query->req,
630*a1157835SDaniel Fojt 			 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
631*a1157835SDaniel Fojt 		wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
632*a1157835SDaniel Fojt 			   MACSTR, MAC2STR(query->addr));
633*a1157835SDaniel Fojt 		gas_query_done(gas, query, GAS_QUERY_AP_INTERNAL_ERROR);
634*a1157835SDaniel Fojt 		return;
635*a1157835SDaniel Fojt 	}
636*a1157835SDaniel Fojt 	gas->current = query;
637*a1157835SDaniel Fojt 
638*a1157835SDaniel Fojt 	wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
639*a1157835SDaniel Fojt 		   query->dialog_token);
640*a1157835SDaniel Fojt 	eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
641*a1157835SDaniel Fojt 			       gas_query_timeout, gas, query);
642*a1157835SDaniel Fojt }
643*a1157835SDaniel Fojt 
644*a1157835SDaniel Fojt 
gas_query_new_dialog_token(struct gas_query_ap * gas,const u8 * dst)645*a1157835SDaniel Fojt static int gas_query_new_dialog_token(struct gas_query_ap *gas, const u8 *dst)
646*a1157835SDaniel Fojt {
647*a1157835SDaniel Fojt 	static int next_start = 0;
648*a1157835SDaniel Fojt 	int dialog_token;
649*a1157835SDaniel Fojt 
650*a1157835SDaniel Fojt 	for (dialog_token = 0; dialog_token < 256; dialog_token++) {
651*a1157835SDaniel Fojt 		if (gas_query_dialog_token_available(
652*a1157835SDaniel Fojt 			    gas, dst, (next_start + dialog_token) % 256))
653*a1157835SDaniel Fojt 			break;
654*a1157835SDaniel Fojt 	}
655*a1157835SDaniel Fojt 	if (dialog_token == 256)
656*a1157835SDaniel Fojt 		return -1; /* Too many pending queries */
657*a1157835SDaniel Fojt 	dialog_token = (next_start + dialog_token) % 256;
658*a1157835SDaniel Fojt 	next_start = (dialog_token + 1) % 256;
659*a1157835SDaniel Fojt 	return dialog_token;
660*a1157835SDaniel Fojt }
661*a1157835SDaniel Fojt 
662*a1157835SDaniel Fojt 
663*a1157835SDaniel Fojt /**
664*a1157835SDaniel Fojt  * gas_query_ap_req - Request a GAS query
665*a1157835SDaniel Fojt  * @gas: GAS query data from gas_query_init()
666*a1157835SDaniel Fojt  * @dst: Destination MAC address for the query
667*a1157835SDaniel Fojt  * @freq: Frequency (in MHz) for the channel on which to send the query
668*a1157835SDaniel Fojt  * @req: GAS query payload (to be freed by gas_query module in case of success
669*a1157835SDaniel Fojt  *	return)
670*a1157835SDaniel Fojt  * @cb: Callback function for reporting GAS query result and response
671*a1157835SDaniel Fojt  * @ctx: Context pointer to use with the @cb call
672*a1157835SDaniel Fojt  * Returns: dialog token (>= 0) on success or -1 on failure
673*a1157835SDaniel Fojt  */
gas_query_ap_req(struct gas_query_ap * gas,const u8 * dst,int freq,struct wpabuf * req,void (* cb)(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_ap_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code),void * ctx)674*a1157835SDaniel Fojt int gas_query_ap_req(struct gas_query_ap *gas, const u8 *dst, int freq,
675*a1157835SDaniel Fojt 		     struct wpabuf *req,
676*a1157835SDaniel Fojt 		     void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
677*a1157835SDaniel Fojt 				enum gas_query_ap_result result,
678*a1157835SDaniel Fojt 				const struct wpabuf *adv_proto,
679*a1157835SDaniel Fojt 				const struct wpabuf *resp, u16 status_code),
680*a1157835SDaniel Fojt 		     void *ctx)
681*a1157835SDaniel Fojt {
682*a1157835SDaniel Fojt 	struct gas_query_pending *query;
683*a1157835SDaniel Fojt 	int dialog_token;
684*a1157835SDaniel Fojt 
685*a1157835SDaniel Fojt 	if (!gas || wpabuf_len(req) < 3)
686*a1157835SDaniel Fojt 		return -1;
687*a1157835SDaniel Fojt 
688*a1157835SDaniel Fojt 	dialog_token = gas_query_new_dialog_token(gas, dst);
689*a1157835SDaniel Fojt 	if (dialog_token < 0)
690*a1157835SDaniel Fojt 		return -1;
691*a1157835SDaniel Fojt 
692*a1157835SDaniel Fojt 	query = os_zalloc(sizeof(*query));
693*a1157835SDaniel Fojt 	if (query == NULL)
694*a1157835SDaniel Fojt 		return -1;
695*a1157835SDaniel Fojt 
696*a1157835SDaniel Fojt 	query->gas = gas;
697*a1157835SDaniel Fojt 	os_memcpy(query->addr, dst, ETH_ALEN);
698*a1157835SDaniel Fojt 	query->dialog_token = dialog_token;
699*a1157835SDaniel Fojt 	query->freq = freq;
700*a1157835SDaniel Fojt 	query->cb = cb;
701*a1157835SDaniel Fojt 	query->ctx = ctx;
702*a1157835SDaniel Fojt 	query->req = req;
703*a1157835SDaniel Fojt 	dl_list_add(&gas->pending, &query->list);
704*a1157835SDaniel Fojt 
705*a1157835SDaniel Fojt 	*(wpabuf_mhead_u8(req) + 2) = dialog_token;
706*a1157835SDaniel Fojt 
707*a1157835SDaniel Fojt 	wpa_msg(gas->msg_ctx, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
708*a1157835SDaniel Fojt 		" dialog_token=%u freq=%d",
709*a1157835SDaniel Fojt 		MAC2STR(query->addr), query->dialog_token, query->freq);
710*a1157835SDaniel Fojt 
711*a1157835SDaniel Fojt 	gas_query_tx_initial_req(gas, query);
712*a1157835SDaniel Fojt 
713*a1157835SDaniel Fojt 	return dialog_token;
714*a1157835SDaniel Fojt }
715