blob: 88564d51890a69f3b0d5373815f16271f907baef [file] [log] [blame]
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001/*
2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
Dmitry Shmidt18463232014-01-24 12:29:41 -08004 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009 */
10
11#include "includes.h"
12
13#include "common.h"
14#include "utils/eloop.h"
15#include "common/ieee802_11_defs.h"
16#include "common/gas.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080017#include "common/wpa_ctrl.h"
Dmitry Shmidt18463232014-01-24 12:29:41 -080018#include "rsn_supp/wpa.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "wpa_supplicant_i.h"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070020#include "config.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080021#include "driver_i.h"
22#include "offchannel.h"
23#include "gas_query.h"
24
25
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080026/** GAS query timeout in seconds */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070027#define GAS_QUERY_TIMEOUT_PERIOD 2
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080028
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080029/* GAS query wait-time / duration in ms */
30#define GAS_QUERY_WAIT_TIME_INITIAL 1000
31#define GAS_QUERY_WAIT_TIME_COMEBACK 150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032
Sunil8cd6f4d2022-06-28 18:40:46 +000033#define GAS_QUERY_MAX_COMEBACK_DELAY 60000
34
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080035/**
36 * struct gas_query_pending - Pending GAS query
37 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038struct gas_query_pending {
39 struct dl_list list;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080040 struct gas_query *gas;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041 u8 addr[ETH_ALEN];
42 u8 dialog_token;
43 u8 next_frag_id;
44 unsigned int wait_comeback:1;
45 unsigned int offchannel_tx_started:1;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080046 unsigned int retry:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -070047 unsigned int wildcard_bssid:1;
Hai Shalomb755a2a2020-04-23 21:49:02 -070048 unsigned int maintain_addr:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049 int freq;
50 u16 status_code;
Dmitry Shmidt051af732013-10-22 13:52:46 -070051 struct wpabuf *req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080052 struct wpabuf *adv_proto;
53 struct wpabuf *resp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080054 struct os_reltime last_oper;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080055 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
56 enum gas_query_result result,
57 const struct wpabuf *adv_proto,
58 const struct wpabuf *resp, u16 status_code);
59 void *ctx;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080060 u8 sa[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080061};
62
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080063/**
64 * struct gas_query - Internal GAS query data
65 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066struct gas_query {
67 struct wpa_supplicant *wpa_s;
68 struct dl_list pending; /* struct gas_query_pending */
Dmitry Shmidt051af732013-10-22 13:52:46 -070069 struct gas_query_pending *current;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080070 struct wpa_radio_work *work;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080071 struct os_reltime last_mac_addr_rand;
72 int last_rand_sa_type;
73 u8 rand_addr[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080074};
75
76
77static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
78static void gas_query_timeout(void *eloop_data, void *user_ctx);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080079static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
80static void gas_query_tx_initial_req(struct gas_query *gas,
81 struct gas_query_pending *query);
82static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080083
84
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080085static int ms_from_time(struct os_reltime *last)
86{
87 struct os_reltime now, res;
88
89 os_get_reltime(&now);
90 os_reltime_sub(&now, last, &res);
91 return res.sec * 1000 + res.usec / 1000;
92}
93
94
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080095/**
96 * gas_query_init - Initialize GAS query component
97 * @wpa_s: Pointer to wpa_supplicant data
98 * Returns: Pointer to GAS query data or %NULL on failure
99 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800100struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
101{
102 struct gas_query *gas;
103
104 gas = os_zalloc(sizeof(*gas));
105 if (gas == NULL)
106 return NULL;
107
108 gas->wpa_s = wpa_s;
109 dl_list_init(&gas->pending);
110
111 return gas;
112}
113
114
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800115static const char * gas_result_txt(enum gas_query_result result)
116{
117 switch (result) {
118 case GAS_QUERY_SUCCESS:
119 return "SUCCESS";
120 case GAS_QUERY_FAILURE:
121 return "FAILURE";
122 case GAS_QUERY_TIMEOUT:
123 return "TIMEOUT";
124 case GAS_QUERY_PEER_ERROR:
125 return "PEER_ERROR";
126 case GAS_QUERY_INTERNAL_ERROR:
127 return "INTERNAL_ERROR";
Roshan Pius3a1667e2018-07-03 15:17:14 -0700128 case GAS_QUERY_STOPPED:
129 return "STOPPED";
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800130 case GAS_QUERY_DELETED_AT_DEINIT:
131 return "DELETED_AT_DEINIT";
132 }
133
134 return "N/A";
135}
136
137
138static void gas_query_free(struct gas_query_pending *query, int del_list)
139{
140 struct gas_query *gas = query->gas;
141
142 if (del_list)
143 dl_list_del(&query->list);
144
145 if (gas->work && gas->work->ctx == query) {
146 radio_work_done(gas->work);
147 gas->work = NULL;
148 }
149
Sunil Ravi876a49b2025-02-03 19:18:32 +0000150 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
151 eloop_cancel_timeout(gas_query_timeout, gas, query);
152 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
153
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800154 wpabuf_free(query->req);
155 wpabuf_free(query->adv_proto);
156 wpabuf_free(query->resp);
157 os_free(query);
158}
159
160
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800161static void gas_query_done(struct gas_query *gas,
162 struct gas_query_pending *query,
163 enum gas_query_result result)
164{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800165 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
166 " dialog_token=%u freq=%d status_code=%u result=%s",
167 MAC2STR(query->addr), query->dialog_token, query->freq,
168 query->status_code, gas_result_txt(result));
Dmitry Shmidt051af732013-10-22 13:52:46 -0700169 if (gas->current == query)
170 gas->current = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800171 if (query->offchannel_tx_started)
172 offchannel_send_action_done(gas->wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800173 dl_list_del(&query->list);
174 query->cb(query->ctx, query->addr, query->dialog_token, result,
175 query->adv_proto, query->resp, query->status_code);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800176 gas_query_free(query, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177}
178
179
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800180/**
181 * gas_query_deinit - Deinitialize GAS query component
182 * @gas: GAS query data from gas_query_init()
183 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800184void gas_query_deinit(struct gas_query *gas)
185{
186 struct gas_query_pending *query, *next;
187
188 if (gas == NULL)
189 return;
190
191 dl_list_for_each_safe(query, next, &gas->pending,
192 struct gas_query_pending, list)
193 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
194
195 os_free(gas);
196}
197
198
199static struct gas_query_pending *
200gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
201{
202 struct gas_query_pending *q;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000203 struct wpa_supplicant *wpa_s = gas->wpa_s;
204
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800205 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000206 if (ether_addr_equal(q->addr, addr) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800207 q->dialog_token == dialog_token)
208 return q;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000209 if (wpa_s->valid_links &&
210 ether_addr_equal(wpa_s->ap_mld_addr, addr) &&
211 wpas_ap_link_address(wpa_s, q->addr))
212 return q;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800213 }
214 return NULL;
215}
216
217
218static int gas_query_append(struct gas_query_pending *query, const u8 *data,
219 size_t len)
220{
221 if (wpabuf_resize(&query->resp, len) < 0) {
222 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
223 return -1;
224 }
225 wpabuf_put_data(query->resp, data, len);
226 return 0;
227}
228
229
Dmitry Shmidt051af732013-10-22 13:52:46 -0700230static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
231 unsigned int freq, const u8 *dst,
232 const u8 *src, const u8 *bssid,
233 const u8 *data, size_t data_len,
234 enum offchannel_send_action_result result)
235{
236 struct gas_query_pending *query;
237 struct gas_query *gas = wpa_s->gas;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800238 int dur;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700239
240 if (gas->current == NULL) {
241 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
242 MACSTR " result=%d - no query in progress",
243 freq, MAC2STR(dst), result);
244 return;
245 }
246
247 query = gas->current;
248
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800249 dur = ms_from_time(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700250 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800251 " result=%d query=%p dialog_token=%u dur=%d ms",
252 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000253 if (!ether_addr_equal(dst, query->addr)) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700254 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
255 return;
256 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800257 os_get_reltime(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700258
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700259 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS ||
260 result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700261 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700262 if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
263 wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
264 eloop_register_timeout(0, 250000,
265 gas_query_timeout, gas, query);
266 } else {
267 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
268 gas_query_timeout, gas, query);
269 }
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800270 if (query->wait_comeback && !query->retry) {
271 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
272 gas, query);
273 eloop_register_timeout(
274 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
275 gas_query_rx_comeback_timeout, gas, query);
276 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700277 }
278 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
279 eloop_cancel_timeout(gas_query_timeout, gas, query);
280 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
281 }
282}
283
284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800285static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800286 struct wpabuf *req, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800288 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700289 const u8 *bssid;
290 const u8 wildcard_bssid[ETH_ALEN] = {
291 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
292 };
Dmitry Shmidt18463232014-01-24 12:29:41 -0800293
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800294 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800295 "freq=%d prot=%d using src addr " MACSTR,
296 MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
297 query->freq, prot, MAC2STR(query->sa));
Dmitry Shmidt18463232014-01-24 12:29:41 -0800298 if (prot) {
299 u8 *categ = wpabuf_mhead_u8(req);
300 *categ = WLAN_ACTION_PROTECTED_DUAL;
301 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800302 os_get_reltime(&query->last_oper);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700303 if (gas->wpa_s->max_remain_on_chan &&
304 wait_time > gas->wpa_s->max_remain_on_chan)
305 wait_time = gas->wpa_s->max_remain_on_chan;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700306 if (!query->wildcard_bssid &&
307 (!gas->wpa_s->conf->gas_address3 ||
308 (gas->wpa_s->current_ssid &&
309 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000310 ether_addr_equal(query->addr, gas->wpa_s->bssid))))
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700311 bssid = query->addr;
312 else
313 bssid = wildcard_bssid;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800314
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800316 query->sa, bssid, wpabuf_head(req),
317 wpabuf_len(req), wait_time,
318 gas_query_tx_status, 0);
319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 if (res == 0)
321 query->offchannel_tx_started = 1;
322 return res;
323}
324
325
326static void gas_query_tx_comeback_req(struct gas_query *gas,
327 struct gas_query_pending *query)
328{
329 struct wpabuf *req;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800330 unsigned int wait_time;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800331
332 req = gas_build_comeback_req(query->dialog_token);
333 if (req == NULL) {
334 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
335 return;
336 }
337
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800338 wait_time = (query->retry || !query->offchannel_tx_started) ?
339 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
340
341 if (gas_query_tx(gas, query, req, wait_time) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800342 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
343 MACSTR, MAC2STR(query->addr));
344 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
345 }
346
347 wpabuf_free(req);
348}
349
350
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800351static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
352{
353 struct gas_query *gas = eloop_data;
354 struct gas_query_pending *query = user_ctx;
355 int dialog_token;
356
357 wpa_printf(MSG_DEBUG,
358 "GAS: No response to comeback request received (retry=%u)",
359 query->retry);
360 if (gas->current != query || query->retry)
361 return;
362 dialog_token = gas_query_new_dialog_token(gas, query->addr);
363 if (dialog_token < 0)
364 return;
365 wpa_printf(MSG_DEBUG,
366 "GAS: Retry GAS query due to comeback response timeout");
367 query->retry = 1;
368 query->dialog_token = dialog_token;
369 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
370 query->wait_comeback = 0;
371 query->next_frag_id = 0;
372 wpabuf_free(query->adv_proto);
373 query->adv_proto = NULL;
374 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
375 eloop_cancel_timeout(gas_query_timeout, gas, query);
376 gas_query_tx_initial_req(gas, query);
377}
378
379
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800380static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
381{
382 struct gas_query *gas = eloop_data;
383 struct gas_query_pending *query = user_ctx;
384
385 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
386 MAC2STR(query->addr));
387 gas_query_tx_comeback_req(gas, query);
388}
389
390
391static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
392 struct gas_query_pending *query,
393 u16 comeback_delay)
394{
395 unsigned int secs, usecs;
396
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800397 if (comeback_delay > 1 && query->offchannel_tx_started) {
398 offchannel_send_action_done(gas->wpa_s);
399 query->offchannel_tx_started = 0;
400 }
401
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800402 secs = (comeback_delay * 1024) / 1000000;
403 usecs = comeback_delay * 1024 - secs * 1000000;
404 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
405 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
406 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
407 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
408 gas, query);
409}
410
411
412static void gas_query_rx_initial(struct gas_query *gas,
413 struct gas_query_pending *query,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000414 const u8 *adv_proto, size_t adv_proto_len,
415 const u8 *resp, size_t len, u16 comeback_delay)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416{
417 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
418 MACSTR " (dialog_token=%u comeback_delay=%u)",
419 MAC2STR(query->addr), query->dialog_token, comeback_delay);
420
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000421 query->adv_proto = wpabuf_alloc_copy(adv_proto, adv_proto_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800422 if (query->adv_proto == NULL) {
423 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
424 return;
425 }
426
427 if (comeback_delay) {
Paul Stewart092955c2017-02-06 09:13:09 -0800428 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800429 query->wait_comeback = 1;
430 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
431 return;
432 }
433
434 /* Query was completed without comeback mechanism */
435 if (gas_query_append(query, resp, len) < 0) {
436 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
437 return;
438 }
439
440 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
441}
442
443
444static void gas_query_rx_comeback(struct gas_query *gas,
445 struct gas_query_pending *query,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000446 const u8 *adv_proto, size_t adv_proto_len,
447 const u8 *resp, size_t len, u8 frag_id,
448 u8 more_frags, u16 comeback_delay)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800449{
450 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
451 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
452 "comeback_delay=%u)",
453 MAC2STR(query->addr), query->dialog_token, frag_id,
454 more_frags, comeback_delay);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800455 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800456
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000457 if (adv_proto_len != wpabuf_len(query->adv_proto) ||
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800458 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
459 wpabuf_len(query->adv_proto)) != 0) {
460 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
461 "between initial and comeback response from "
462 MACSTR, MAC2STR(query->addr));
463 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
464 return;
465 }
466
467 if (comeback_delay) {
468 if (frag_id) {
469 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
470 "with non-zero frag_id and comeback_delay "
471 "from " MACSTR, MAC2STR(query->addr));
472 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
473 return;
474 }
475 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
476 return;
477 }
478
479 if (frag_id != query->next_frag_id) {
480 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
481 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700482 if (frag_id + 1 == query->next_frag_id) {
483 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
484 "retry of previous fragment");
485 return;
486 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800487 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
488 return;
489 }
490 query->next_frag_id++;
491
492 if (gas_query_append(query, resp, len) < 0) {
493 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
494 return;
495 }
496
497 if (more_frags) {
498 gas_query_tx_comeback_req(gas, query);
499 return;
500 }
501
502 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
503}
504
505
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800506/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800507 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800508 * @gas: GAS query data from gas_query_init()
509 * @da: Destination MAC address of the Action frame
510 * @sa: Source MAC address of the Action frame
511 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800512 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800513 * @data: Payload of the Action frame
514 * @len: Length of @data
515 * @freq: Frequency (in MHz) on which the frame was received
516 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
517 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800518int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800519 const u8 *bssid, u8 categ, const u8 *data, size_t len,
520 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800521{
522 struct gas_query_pending *query;
523 u8 action, dialog_token, frag_id = 0, more_frags = 0;
524 u16 comeback_delay, resp_len;
525 const u8 *pos, *adv_proto;
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000526 size_t adv_proto_len;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800527 int prot, pmf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800528 unsigned int left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800529
530 if (gas == NULL || len < 4)
531 return -1;
532
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700533 pos = data;
534 action = *pos++;
535 dialog_token = *pos++;
536
537 if (action != WLAN_PA_GAS_INITIAL_RESP &&
538 action != WLAN_PA_GAS_COMEBACK_RESP)
539 return -1; /* Not a GAS response */
540
Dmitry Shmidt18463232014-01-24 12:29:41 -0800541 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800542 pmf = pmf_in_use(gas->wpa_s, sa);
Dmitry Shmidt18463232014-01-24 12:29:41 -0800543 if (prot && !pmf) {
544 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
545 return 0;
546 }
547 if (!prot && pmf) {
548 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
549 return 0;
550 }
551
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552 query = gas_query_get_pending(gas, sa, dialog_token);
553 if (query == NULL) {
554 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
555 " dialog token %u", MAC2STR(sa), dialog_token);
556 return -1;
557 }
558
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800559 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
560 ms_from_time(&query->last_oper), MAC2STR(sa));
561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800562 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
563 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
564 MACSTR " dialog token %u when waiting for comeback "
565 "response", MAC2STR(sa), dialog_token);
566 return 0;
567 }
568
569 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
570 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
571 MACSTR " dialog token %u when waiting for initial "
572 "response", MAC2STR(sa), dialog_token);
573 return 0;
574 }
575
576 query->status_code = WPA_GET_LE16(pos);
577 pos += 2;
578
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800579 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
580 action == WLAN_PA_GAS_COMEBACK_RESP) {
581 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
582 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
584 "%u failed - status code %u",
585 MAC2STR(sa), dialog_token, query->status_code);
586 gas_query_done(gas, query, GAS_QUERY_FAILURE);
587 return 0;
588 }
589
590 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
591 if (pos + 1 > data + len)
592 return 0;
593 frag_id = *pos & 0x7f;
594 more_frags = (*pos & 0x80) >> 7;
595 pos++;
596 }
597
598 /* Comeback Delay */
599 if (pos + 2 > data + len)
600 return 0;
601 comeback_delay = WPA_GET_LE16(pos);
Sunil8cd6f4d2022-06-28 18:40:46 +0000602 if (comeback_delay > GAS_QUERY_MAX_COMEBACK_DELAY)
603 comeback_delay = GAS_QUERY_MAX_COMEBACK_DELAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604 pos += 2;
605
606 /* Advertisement Protocol element */
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000607 adv_proto = pos;
608 left = data + len - adv_proto;
609 if (left < 2 || adv_proto[1] > left - 2) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800610 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
611 "Protocol element in the response from " MACSTR,
612 MAC2STR(sa));
613 return 0;
614 }
615
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000616 if (*adv_proto != WLAN_EID_ADV_PROTO) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800617 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
618 "Protocol element ID %u in response from " MACSTR,
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000619 *adv_proto, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 return 0;
621 }
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000622 adv_proto_len = 2 + adv_proto[1];
623 if (adv_proto_len > (size_t) (data + len - pos))
624 return 0; /* unreachable due to an earlier check */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800625
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000626 pos += adv_proto_len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627
628 /* Query Response Length */
629 if (pos + 2 > data + len) {
630 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
631 return 0;
632 }
633 resp_len = WPA_GET_LE16(pos);
634 pos += 2;
635
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800636 left = data + len - pos;
637 if (resp_len > left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800638 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
639 "response from " MACSTR, MAC2STR(sa));
640 return 0;
641 }
642
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800643 if (resp_len < left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800644 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
645 "after Query Response from " MACSTR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800646 left - resp_len, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800647 }
648
649 if (action == WLAN_PA_GAS_COMEBACK_RESP)
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000650 gas_query_rx_comeback(gas, query, adv_proto, adv_proto_len,
651 pos, resp_len, frag_id, more_frags,
652 comeback_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653 else
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000654 gas_query_rx_initial(gas, query, adv_proto, adv_proto_len,
655 pos, resp_len, comeback_delay);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800656
657 return 0;
658}
659
660
661static void gas_query_timeout(void *eloop_data, void *user_ctx)
662{
663 struct gas_query *gas = eloop_data;
664 struct gas_query_pending *query = user_ctx;
665
Dmitry Shmidt051af732013-10-22 13:52:46 -0700666 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
667 " dialog token %u",
668 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800669 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
670}
671
672
673static int gas_query_dialog_token_available(struct gas_query *gas,
674 const u8 *dst, u8 dialog_token)
675{
676 struct gas_query_pending *q;
677 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000678 if (ether_addr_equal(dst, q->addr) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800679 dialog_token == q->dialog_token)
680 return 0;
681 }
682
683 return 1;
684}
685
686
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800687static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
688{
689 struct gas_query_pending *query = work->ctx;
690 struct gas_query *gas = query->gas;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700691 struct wpa_supplicant *wpa_s = gas->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800692
693 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800694 if (work->started) {
695 gas->work = NULL;
696 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
697 return;
698 }
699
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800700 gas_query_free(query, 1);
701 return;
702 }
703
Hai Shalom899fcc72020-10-19 14:38:18 -0700704 if (!query->maintain_addr && !wpa_s->conf->gas_rand_mac_addr) {
705 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
706 wpa_msg(wpa_s, MSG_INFO,
707 "Failed to assign random MAC address for GAS");
708 gas_query_free(query, 1);
709 radio_work_done(work);
710 return;
711 }
712 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700713 }
714
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800715 gas->work = work;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800716 gas_query_tx_initial_req(gas, query);
717}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800718
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800719
720static void gas_query_tx_initial_req(struct gas_query *gas,
721 struct gas_query_pending *query)
722{
723 if (gas_query_tx(gas, query, query->req,
724 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800725 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
726 MACSTR, MAC2STR(query->addr));
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700727 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800728 return;
729 }
730 gas->current = query;
731
732 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
733 query->dialog_token);
734 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
735 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800736}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800737
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800738
739static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
740{
Hai Shalome21d4e82020-04-29 16:34:06 -0700741 u8 dialog_token;
742 int i;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800743
Hai Shalome21d4e82020-04-29 16:34:06 -0700744 /* There should never be more than couple active GAS queries in
745 * progress, so it should be very likely to find an available dialog
746 * token by checking random values. Use a limit on the number of
747 * iterations to handle the unexpected case of large number of pending
748 * queries cleanly. */
749 for (i = 0; i < 256; i++) {
750 /* Get a random number and check if the slot is available */
751 if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0)
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800752 break;
Hai Shalome21d4e82020-04-29 16:34:06 -0700753 if (gas_query_dialog_token_available(gas, dst, dialog_token))
754 return dialog_token;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800755 }
Hai Shalome21d4e82020-04-29 16:34:06 -0700756
757 /* No dialog token value available */
758 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800759}
760
761
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800762static int gas_query_set_sa(struct gas_query *gas,
763 struct gas_query_pending *query)
764{
765 struct wpa_supplicant *wpa_s = gas->wpa_s;
766 struct os_reltime now;
767
Hai Shalomb755a2a2020-04-23 21:49:02 -0700768 if (query->maintain_addr ||
769 !wpa_s->conf->gas_rand_mac_addr ||
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800770 !(wpa_s->current_bss ?
771 (wpa_s->drv_flags &
772 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
773 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
774 /* Use own MAC address as the transmitter address */
Hai Shalomb755a2a2020-04-23 21:49:02 -0700775 wpa_printf(MSG_DEBUG,
776 "GAS: Use own MAC address as the transmitter address%s%s%s",
777 query->maintain_addr ? " (maintain_addr)" : "",
778 !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "",
779 !(wpa_s->current_bss ?
780 (wpa_s->drv_flags &
781 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
782 (wpa_s->drv_flags &
783 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ?
784 " (no driver rand capa" : "");
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800785 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
786 return 0;
787 }
788
789 os_get_reltime(&now);
790
791 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
792 gas->last_mac_addr_rand.sec != 0 &&
793 !os_reltime_expired(&now, &gas->last_mac_addr_rand,
794 wpa_s->conf->gas_rand_addr_lifetime)) {
795 wpa_printf(MSG_DEBUG,
796 "GAS: Use the previously selected random transmitter address "
797 MACSTR, MAC2STR(gas->rand_addr));
798 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
799 return 0;
800 }
801
802 if (wpa_s->conf->gas_rand_mac_addr == 1 &&
803 random_mac_addr(gas->rand_addr) < 0) {
804 wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
805 return -1;
806 }
807
808 if (wpa_s->conf->gas_rand_mac_addr == 2 &&
809 random_mac_addr_keep_oui(gas->rand_addr) < 0) {
810 wpa_printf(MSG_ERROR,
811 "GAS: Failed to get random address with same OUI");
812 return -1;
813 }
814
815 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
816 MACSTR, MAC2STR(gas->rand_addr));
817 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
818 os_get_reltime(&gas->last_mac_addr_rand);
819 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
820
821 return 0;
822}
823
824
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800825/**
826 * gas_query_req - Request a GAS query
827 * @gas: GAS query data from gas_query_init()
828 * @dst: Destination MAC address for the query
829 * @freq: Frequency (in MHz) for the channel on which to send the query
Hai Shalomb755a2a2020-04-23 21:49:02 -0700830 * @wildcard_bssid: Force use of wildcard BSSID value
831 * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC
832 * address randomization rules)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700833 * @req: GAS query payload (to be freed by gas_query module in case of success
834 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800835 * @cb: Callback function for reporting GAS query result and response
836 * @ctx: Context pointer to use with the @cb call
837 * Returns: dialog token (>= 0) on success or -1 on failure
838 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
Hai Shalomb755a2a2020-04-23 21:49:02 -0700840 int wildcard_bssid, int maintain_addr, struct wpabuf *req,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800841 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
842 enum gas_query_result result,
843 const struct wpabuf *adv_proto,
844 const struct wpabuf *resp, u16 status_code),
845 void *ctx)
846{
847 struct gas_query_pending *query;
848 int dialog_token;
849
850 if (wpabuf_len(req) < 3)
851 return -1;
852
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800853 dialog_token = gas_query_new_dialog_token(gas, dst);
854 if (dialog_token < 0)
855 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856
857 query = os_zalloc(sizeof(*query));
858 if (query == NULL)
859 return -1;
860
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800861 query->gas = gas;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700862 query->maintain_addr = !!maintain_addr;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800863 if (gas_query_set_sa(gas, query)) {
864 os_free(query);
865 return -1;
866 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867 os_memcpy(query->addr, dst, ETH_ALEN);
868 query->dialog_token = dialog_token;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700869 query->wildcard_bssid = !!wildcard_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800870 query->freq = freq;
871 query->cb = cb;
872 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700873 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800874 dl_list_add(&gas->pending, &query->list);
875
876 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
877
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800878 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
879 " dialog_token=%u freq=%d",
880 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800881
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800882 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
883 query) < 0) {
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700884 query->req = NULL; /* caller will free this in error case */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800885 gas_query_free(query, 1);
886 return -1;
887 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888
889 return dialog_token;
890}
Roshan Pius3a1667e2018-07-03 15:17:14 -0700891
892
893int gas_query_stop(struct gas_query *gas, u8 dialog_token)
894{
895 struct gas_query_pending *query;
896
897 dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) {
898 if (query->dialog_token == dialog_token) {
899 if (!gas->work) {
900 /* The pending radio work has not yet been
901 * started, but the pending entry has a
902 * reference to the soon to be freed query.
903 * Need to remove that radio work now to avoid
904 * leaving behind a reference to freed memory.
905 */
906 radio_remove_pending_work(gas->wpa_s, query);
907 }
908 gas_query_done(gas, query, GAS_QUERY_STOPPED);
909 return 0;
910 }
911 }
912
913 return -1;
914}