blob: a6172d69233b1fe33f0939ee09b370c081c3e1b9 [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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080033/**
34 * struct gas_query_pending - Pending GAS query
35 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036struct gas_query_pending {
37 struct dl_list list;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080038 struct gas_query *gas;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080039 u8 addr[ETH_ALEN];
40 u8 dialog_token;
41 u8 next_frag_id;
42 unsigned int wait_comeback:1;
43 unsigned int offchannel_tx_started:1;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080044 unsigned int retry:1;
Roshan Pius3a1667e2018-07-03 15:17:14 -070045 unsigned int wildcard_bssid:1;
Hai Shalomb755a2a2020-04-23 21:49:02 -070046 unsigned int maintain_addr:1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080047 int freq;
48 u16 status_code;
Dmitry Shmidt051af732013-10-22 13:52:46 -070049 struct wpabuf *req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050 struct wpabuf *adv_proto;
51 struct wpabuf *resp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080052 struct os_reltime last_oper;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080053 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
54 enum gas_query_result result,
55 const struct wpabuf *adv_proto,
56 const struct wpabuf *resp, u16 status_code);
57 void *ctx;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080058 u8 sa[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080059};
60
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080061/**
62 * struct gas_query - Internal GAS query data
63 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080064struct gas_query {
65 struct wpa_supplicant *wpa_s;
66 struct dl_list pending; /* struct gas_query_pending */
Dmitry Shmidt051af732013-10-22 13:52:46 -070067 struct gas_query_pending *current;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080068 struct wpa_radio_work *work;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080069 struct os_reltime last_mac_addr_rand;
70 int last_rand_sa_type;
71 u8 rand_addr[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080072};
73
74
75static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
76static void gas_query_timeout(void *eloop_data, void *user_ctx);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080077static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
78static void gas_query_tx_initial_req(struct gas_query *gas,
79 struct gas_query_pending *query);
80static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080081
82
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080083static int ms_from_time(struct os_reltime *last)
84{
85 struct os_reltime now, res;
86
87 os_get_reltime(&now);
88 os_reltime_sub(&now, last, &res);
89 return res.sec * 1000 + res.usec / 1000;
90}
91
92
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080093/**
94 * gas_query_init - Initialize GAS query component
95 * @wpa_s: Pointer to wpa_supplicant data
96 * Returns: Pointer to GAS query data or %NULL on failure
97 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080098struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
99{
100 struct gas_query *gas;
101
102 gas = os_zalloc(sizeof(*gas));
103 if (gas == NULL)
104 return NULL;
105
106 gas->wpa_s = wpa_s;
107 dl_list_init(&gas->pending);
108
109 return gas;
110}
111
112
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800113static const char * gas_result_txt(enum gas_query_result result)
114{
115 switch (result) {
116 case GAS_QUERY_SUCCESS:
117 return "SUCCESS";
118 case GAS_QUERY_FAILURE:
119 return "FAILURE";
120 case GAS_QUERY_TIMEOUT:
121 return "TIMEOUT";
122 case GAS_QUERY_PEER_ERROR:
123 return "PEER_ERROR";
124 case GAS_QUERY_INTERNAL_ERROR:
125 return "INTERNAL_ERROR";
Roshan Pius3a1667e2018-07-03 15:17:14 -0700126 case GAS_QUERY_STOPPED:
127 return "STOPPED";
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800128 case GAS_QUERY_DELETED_AT_DEINIT:
129 return "DELETED_AT_DEINIT";
130 }
131
132 return "N/A";
133}
134
135
136static void gas_query_free(struct gas_query_pending *query, int del_list)
137{
138 struct gas_query *gas = query->gas;
139
140 if (del_list)
141 dl_list_del(&query->list);
142
143 if (gas->work && gas->work->ctx == query) {
144 radio_work_done(gas->work);
145 gas->work = NULL;
146 }
147
148 wpabuf_free(query->req);
149 wpabuf_free(query->adv_proto);
150 wpabuf_free(query->resp);
151 os_free(query);
152}
153
154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800155static void gas_query_done(struct gas_query *gas,
156 struct gas_query_pending *query,
157 enum gas_query_result result)
158{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800159 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
160 " dialog_token=%u freq=%d status_code=%u result=%s",
161 MAC2STR(query->addr), query->dialog_token, query->freq,
162 query->status_code, gas_result_txt(result));
Dmitry Shmidt051af732013-10-22 13:52:46 -0700163 if (gas->current == query)
164 gas->current = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165 if (query->offchannel_tx_started)
166 offchannel_send_action_done(gas->wpa_s);
167 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
168 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800169 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170 dl_list_del(&query->list);
171 query->cb(query->ctx, query->addr, query->dialog_token, result,
172 query->adv_proto, query->resp, query->status_code);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800173 gas_query_free(query, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800174}
175
176
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800177/**
178 * gas_query_deinit - Deinitialize GAS query component
179 * @gas: GAS query data from gas_query_init()
180 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800181void gas_query_deinit(struct gas_query *gas)
182{
183 struct gas_query_pending *query, *next;
184
185 if (gas == NULL)
186 return;
187
188 dl_list_for_each_safe(query, next, &gas->pending,
189 struct gas_query_pending, list)
190 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
191
192 os_free(gas);
193}
194
195
196static struct gas_query_pending *
197gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
198{
199 struct gas_query_pending *q;
200 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
201 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
202 q->dialog_token == dialog_token)
203 return q;
204 }
205 return NULL;
206}
207
208
209static int gas_query_append(struct gas_query_pending *query, const u8 *data,
210 size_t len)
211{
212 if (wpabuf_resize(&query->resp, len) < 0) {
213 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
214 return -1;
215 }
216 wpabuf_put_data(query->resp, data, len);
217 return 0;
218}
219
220
Dmitry Shmidt051af732013-10-22 13:52:46 -0700221static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
222 unsigned int freq, const u8 *dst,
223 const u8 *src, const u8 *bssid,
224 const u8 *data, size_t data_len,
225 enum offchannel_send_action_result result)
226{
227 struct gas_query_pending *query;
228 struct gas_query *gas = wpa_s->gas;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800229 int dur;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700230
231 if (gas->current == NULL) {
232 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
233 MACSTR " result=%d - no query in progress",
234 freq, MAC2STR(dst), result);
235 return;
236 }
237
238 query = gas->current;
239
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800240 dur = ms_from_time(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700241 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800242 " result=%d query=%p dialog_token=%u dur=%d ms",
243 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700244 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
245 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
246 return;
247 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800248 os_get_reltime(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700249
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700250 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS ||
251 result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700252 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700253 if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
254 wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
255 eloop_register_timeout(0, 250000,
256 gas_query_timeout, gas, query);
257 } else {
258 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
259 gas_query_timeout, gas, query);
260 }
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800261 if (query->wait_comeback && !query->retry) {
262 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
263 gas, query);
264 eloop_register_timeout(
265 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
266 gas_query_rx_comeback_timeout, gas, query);
267 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700268 }
269 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
270 eloop_cancel_timeout(gas_query_timeout, gas, query);
271 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
272 }
273}
274
275
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800276static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800277 struct wpabuf *req, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800279 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700280 const u8 *bssid;
281 const u8 wildcard_bssid[ETH_ALEN] = {
282 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
283 };
Dmitry Shmidt18463232014-01-24 12:29:41 -0800284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800285 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800286 "freq=%d prot=%d using src addr " MACSTR,
287 MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
288 query->freq, prot, MAC2STR(query->sa));
Dmitry Shmidt18463232014-01-24 12:29:41 -0800289 if (prot) {
290 u8 *categ = wpabuf_mhead_u8(req);
291 *categ = WLAN_ACTION_PROTECTED_DUAL;
292 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800293 os_get_reltime(&query->last_oper);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700294 if (gas->wpa_s->max_remain_on_chan &&
295 wait_time > gas->wpa_s->max_remain_on_chan)
296 wait_time = gas->wpa_s->max_remain_on_chan;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700297 if (!query->wildcard_bssid &&
298 (!gas->wpa_s->conf->gas_address3 ||
299 (gas->wpa_s->current_ssid &&
300 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
301 os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0)))
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700302 bssid = query->addr;
303 else
304 bssid = wildcard_bssid;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800307 query->sa, bssid, wpabuf_head(req),
308 wpabuf_len(req), wait_time,
309 gas_query_tx_status, 0);
310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311 if (res == 0)
312 query->offchannel_tx_started = 1;
313 return res;
314}
315
316
317static void gas_query_tx_comeback_req(struct gas_query *gas,
318 struct gas_query_pending *query)
319{
320 struct wpabuf *req;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800321 unsigned int wait_time;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322
323 req = gas_build_comeback_req(query->dialog_token);
324 if (req == NULL) {
325 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
326 return;
327 }
328
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800329 wait_time = (query->retry || !query->offchannel_tx_started) ?
330 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
331
332 if (gas_query_tx(gas, query, req, wait_time) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
334 MACSTR, MAC2STR(query->addr));
335 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
336 }
337
338 wpabuf_free(req);
339}
340
341
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800342static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
343{
344 struct gas_query *gas = eloop_data;
345 struct gas_query_pending *query = user_ctx;
346 int dialog_token;
347
348 wpa_printf(MSG_DEBUG,
349 "GAS: No response to comeback request received (retry=%u)",
350 query->retry);
351 if (gas->current != query || query->retry)
352 return;
353 dialog_token = gas_query_new_dialog_token(gas, query->addr);
354 if (dialog_token < 0)
355 return;
356 wpa_printf(MSG_DEBUG,
357 "GAS: Retry GAS query due to comeback response timeout");
358 query->retry = 1;
359 query->dialog_token = dialog_token;
360 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
361 query->wait_comeback = 0;
362 query->next_frag_id = 0;
363 wpabuf_free(query->adv_proto);
364 query->adv_proto = NULL;
365 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
366 eloop_cancel_timeout(gas_query_timeout, gas, query);
367 gas_query_tx_initial_req(gas, query);
368}
369
370
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800371static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
372{
373 struct gas_query *gas = eloop_data;
374 struct gas_query_pending *query = user_ctx;
375
376 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
377 MAC2STR(query->addr));
378 gas_query_tx_comeback_req(gas, query);
379}
380
381
382static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
383 struct gas_query_pending *query,
384 u16 comeback_delay)
385{
386 unsigned int secs, usecs;
387
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800388 if (comeback_delay > 1 && query->offchannel_tx_started) {
389 offchannel_send_action_done(gas->wpa_s);
390 query->offchannel_tx_started = 0;
391 }
392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800393 secs = (comeback_delay * 1024) / 1000000;
394 usecs = comeback_delay * 1024 - secs * 1000000;
395 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
396 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
397 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
398 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
399 gas, query);
400}
401
402
403static void gas_query_rx_initial(struct gas_query *gas,
404 struct gas_query_pending *query,
405 const u8 *adv_proto, const u8 *resp,
406 size_t len, u16 comeback_delay)
407{
408 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
409 MACSTR " (dialog_token=%u comeback_delay=%u)",
410 MAC2STR(query->addr), query->dialog_token, comeback_delay);
411
412 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
413 if (query->adv_proto == NULL) {
414 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
415 return;
416 }
417
418 if (comeback_delay) {
Paul Stewart092955c2017-02-06 09:13:09 -0800419 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800420 query->wait_comeback = 1;
421 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
422 return;
423 }
424
425 /* Query was completed without comeback mechanism */
426 if (gas_query_append(query, resp, len) < 0) {
427 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
428 return;
429 }
430
431 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
432}
433
434
435static void gas_query_rx_comeback(struct gas_query *gas,
436 struct gas_query_pending *query,
437 const u8 *adv_proto, const u8 *resp,
438 size_t len, u8 frag_id, u8 more_frags,
439 u16 comeback_delay)
440{
441 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
442 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
443 "comeback_delay=%u)",
444 MAC2STR(query->addr), query->dialog_token, frag_id,
445 more_frags, comeback_delay);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800446 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447
448 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
449 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
450 wpabuf_len(query->adv_proto)) != 0) {
451 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
452 "between initial and comeback response from "
453 MACSTR, MAC2STR(query->addr));
454 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
455 return;
456 }
457
458 if (comeback_delay) {
459 if (frag_id) {
460 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
461 "with non-zero frag_id and comeback_delay "
462 "from " MACSTR, MAC2STR(query->addr));
463 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
464 return;
465 }
466 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
467 return;
468 }
469
470 if (frag_id != query->next_frag_id) {
471 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
472 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700473 if (frag_id + 1 == query->next_frag_id) {
474 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
475 "retry of previous fragment");
476 return;
477 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800478 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
479 return;
480 }
481 query->next_frag_id++;
482
483 if (gas_query_append(query, resp, len) < 0) {
484 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
485 return;
486 }
487
488 if (more_frags) {
489 gas_query_tx_comeback_req(gas, query);
490 return;
491 }
492
493 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
494}
495
496
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800497/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800498 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800499 * @gas: GAS query data from gas_query_init()
500 * @da: Destination MAC address of the Action frame
501 * @sa: Source MAC address of the Action frame
502 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800503 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800504 * @data: Payload of the Action frame
505 * @len: Length of @data
506 * @freq: Frequency (in MHz) on which the frame was received
507 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
508 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800509int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800510 const u8 *bssid, u8 categ, const u8 *data, size_t len,
511 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800512{
513 struct gas_query_pending *query;
514 u8 action, dialog_token, frag_id = 0, more_frags = 0;
515 u16 comeback_delay, resp_len;
516 const u8 *pos, *adv_proto;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800517 int prot, pmf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800518 unsigned int left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800519
520 if (gas == NULL || len < 4)
521 return -1;
522
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700523 pos = data;
524 action = *pos++;
525 dialog_token = *pos++;
526
527 if (action != WLAN_PA_GAS_INITIAL_RESP &&
528 action != WLAN_PA_GAS_COMEBACK_RESP)
529 return -1; /* Not a GAS response */
530
Dmitry Shmidt18463232014-01-24 12:29:41 -0800531 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800532 pmf = pmf_in_use(gas->wpa_s, sa);
Dmitry Shmidt18463232014-01-24 12:29:41 -0800533 if (prot && !pmf) {
534 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
535 return 0;
536 }
537 if (!prot && pmf) {
538 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
539 return 0;
540 }
541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542 query = gas_query_get_pending(gas, sa, dialog_token);
543 if (query == NULL) {
544 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
545 " dialog token %u", MAC2STR(sa), dialog_token);
546 return -1;
547 }
548
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800549 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
550 ms_from_time(&query->last_oper), MAC2STR(sa));
551
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
553 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
554 MACSTR " dialog token %u when waiting for comeback "
555 "response", MAC2STR(sa), dialog_token);
556 return 0;
557 }
558
559 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
560 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
561 MACSTR " dialog token %u when waiting for initial "
562 "response", MAC2STR(sa), dialog_token);
563 return 0;
564 }
565
566 query->status_code = WPA_GET_LE16(pos);
567 pos += 2;
568
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800569 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
570 action == WLAN_PA_GAS_COMEBACK_RESP) {
571 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
572 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
574 "%u failed - status code %u",
575 MAC2STR(sa), dialog_token, query->status_code);
576 gas_query_done(gas, query, GAS_QUERY_FAILURE);
577 return 0;
578 }
579
580 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
581 if (pos + 1 > data + len)
582 return 0;
583 frag_id = *pos & 0x7f;
584 more_frags = (*pos & 0x80) >> 7;
585 pos++;
586 }
587
588 /* Comeback Delay */
589 if (pos + 2 > data + len)
590 return 0;
591 comeback_delay = WPA_GET_LE16(pos);
592 pos += 2;
593
594 /* Advertisement Protocol element */
595 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
596 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
597 "Protocol element in the response from " MACSTR,
598 MAC2STR(sa));
599 return 0;
600 }
601
602 if (*pos != WLAN_EID_ADV_PROTO) {
603 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
604 "Protocol element ID %u in response from " MACSTR,
605 *pos, MAC2STR(sa));
606 return 0;
607 }
608
609 adv_proto = pos;
610 pos += 2 + pos[1];
611
612 /* Query Response Length */
613 if (pos + 2 > data + len) {
614 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
615 return 0;
616 }
617 resp_len = WPA_GET_LE16(pos);
618 pos += 2;
619
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800620 left = data + len - pos;
621 if (resp_len > left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
623 "response from " MACSTR, MAC2STR(sa));
624 return 0;
625 }
626
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800627 if (resp_len < left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
629 "after Query Response from " MACSTR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800630 left - resp_len, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800631 }
632
633 if (action == WLAN_PA_GAS_COMEBACK_RESP)
634 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
635 frag_id, more_frags, comeback_delay);
636 else
637 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
638 comeback_delay);
639
640 return 0;
641}
642
643
644static void gas_query_timeout(void *eloop_data, void *user_ctx)
645{
646 struct gas_query *gas = eloop_data;
647 struct gas_query_pending *query = user_ctx;
648
Dmitry Shmidt051af732013-10-22 13:52:46 -0700649 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
650 " dialog token %u",
651 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
653}
654
655
656static int gas_query_dialog_token_available(struct gas_query *gas,
657 const u8 *dst, u8 dialog_token)
658{
659 struct gas_query_pending *q;
660 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
661 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
662 dialog_token == q->dialog_token)
663 return 0;
664 }
665
666 return 1;
667}
668
669
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800670static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
671{
672 struct gas_query_pending *query = work->ctx;
673 struct gas_query *gas = query->gas;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700674 struct wpa_supplicant *wpa_s = gas->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800675
676 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800677 if (work->started) {
678 gas->work = NULL;
679 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
680 return;
681 }
682
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800683 gas_query_free(query, 1);
684 return;
685 }
686
Hai Shalom899fcc72020-10-19 14:38:18 -0700687 if (!query->maintain_addr && !wpa_s->conf->gas_rand_mac_addr) {
688 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
689 wpa_msg(wpa_s, MSG_INFO,
690 "Failed to assign random MAC address for GAS");
691 gas_query_free(query, 1);
692 radio_work_done(work);
693 return;
694 }
695 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700696 }
697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800698 gas->work = work;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800699 gas_query_tx_initial_req(gas, query);
700}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800701
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800702
703static void gas_query_tx_initial_req(struct gas_query *gas,
704 struct gas_query_pending *query)
705{
706 if (gas_query_tx(gas, query, query->req,
707 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800708 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
709 MACSTR, MAC2STR(query->addr));
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700710 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800711 return;
712 }
713 gas->current = query;
714
715 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
716 query->dialog_token);
717 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
718 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800719}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800720
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800721
722static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
723{
Hai Shalome21d4e82020-04-29 16:34:06 -0700724 u8 dialog_token;
725 int i;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800726
Hai Shalome21d4e82020-04-29 16:34:06 -0700727 /* There should never be more than couple active GAS queries in
728 * progress, so it should be very likely to find an available dialog
729 * token by checking random values. Use a limit on the number of
730 * iterations to handle the unexpected case of large number of pending
731 * queries cleanly. */
732 for (i = 0; i < 256; i++) {
733 /* Get a random number and check if the slot is available */
734 if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0)
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800735 break;
Hai Shalome21d4e82020-04-29 16:34:06 -0700736 if (gas_query_dialog_token_available(gas, dst, dialog_token))
737 return dialog_token;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800738 }
Hai Shalome21d4e82020-04-29 16:34:06 -0700739
740 /* No dialog token value available */
741 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800742}
743
744
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800745static int gas_query_set_sa(struct gas_query *gas,
746 struct gas_query_pending *query)
747{
748 struct wpa_supplicant *wpa_s = gas->wpa_s;
749 struct os_reltime now;
750
Hai Shalomb755a2a2020-04-23 21:49:02 -0700751 if (query->maintain_addr ||
752 !wpa_s->conf->gas_rand_mac_addr ||
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800753 !(wpa_s->current_bss ?
754 (wpa_s->drv_flags &
755 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
756 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
757 /* Use own MAC address as the transmitter address */
Hai Shalomb755a2a2020-04-23 21:49:02 -0700758 wpa_printf(MSG_DEBUG,
759 "GAS: Use own MAC address as the transmitter address%s%s%s",
760 query->maintain_addr ? " (maintain_addr)" : "",
761 !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "",
762 !(wpa_s->current_bss ?
763 (wpa_s->drv_flags &
764 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
765 (wpa_s->drv_flags &
766 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ?
767 " (no driver rand capa" : "");
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800768 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
769 return 0;
770 }
771
772 os_get_reltime(&now);
773
774 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
775 gas->last_mac_addr_rand.sec != 0 &&
776 !os_reltime_expired(&now, &gas->last_mac_addr_rand,
777 wpa_s->conf->gas_rand_addr_lifetime)) {
778 wpa_printf(MSG_DEBUG,
779 "GAS: Use the previously selected random transmitter address "
780 MACSTR, MAC2STR(gas->rand_addr));
781 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
782 return 0;
783 }
784
785 if (wpa_s->conf->gas_rand_mac_addr == 1 &&
786 random_mac_addr(gas->rand_addr) < 0) {
787 wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
788 return -1;
789 }
790
791 if (wpa_s->conf->gas_rand_mac_addr == 2 &&
792 random_mac_addr_keep_oui(gas->rand_addr) < 0) {
793 wpa_printf(MSG_ERROR,
794 "GAS: Failed to get random address with same OUI");
795 return -1;
796 }
797
798 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
799 MACSTR, MAC2STR(gas->rand_addr));
800 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
801 os_get_reltime(&gas->last_mac_addr_rand);
802 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
803
804 return 0;
805}
806
807
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800808/**
809 * gas_query_req - Request a GAS query
810 * @gas: GAS query data from gas_query_init()
811 * @dst: Destination MAC address for the query
812 * @freq: Frequency (in MHz) for the channel on which to send the query
Hai Shalomb755a2a2020-04-23 21:49:02 -0700813 * @wildcard_bssid: Force use of wildcard BSSID value
814 * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC
815 * address randomization rules)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700816 * @req: GAS query payload (to be freed by gas_query module in case of success
817 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800818 * @cb: Callback function for reporting GAS query result and response
819 * @ctx: Context pointer to use with the @cb call
820 * Returns: dialog token (>= 0) on success or -1 on failure
821 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
Hai Shalomb755a2a2020-04-23 21:49:02 -0700823 int wildcard_bssid, int maintain_addr, struct wpabuf *req,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800824 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
825 enum gas_query_result result,
826 const struct wpabuf *adv_proto,
827 const struct wpabuf *resp, u16 status_code),
828 void *ctx)
829{
830 struct gas_query_pending *query;
831 int dialog_token;
832
833 if (wpabuf_len(req) < 3)
834 return -1;
835
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800836 dialog_token = gas_query_new_dialog_token(gas, dst);
837 if (dialog_token < 0)
838 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839
840 query = os_zalloc(sizeof(*query));
841 if (query == NULL)
842 return -1;
843
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800844 query->gas = gas;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700845 query->maintain_addr = !!maintain_addr;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800846 if (gas_query_set_sa(gas, query)) {
847 os_free(query);
848 return -1;
849 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800850 os_memcpy(query->addr, dst, ETH_ALEN);
851 query->dialog_token = dialog_token;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700852 query->wildcard_bssid = !!wildcard_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853 query->freq = freq;
854 query->cb = cb;
855 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700856 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800857 dl_list_add(&gas->pending, &query->list);
858
859 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
860
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800861 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
862 " dialog_token=%u freq=%d",
863 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800864
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800865 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
866 query) < 0) {
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700867 query->req = NULL; /* caller will free this in error case */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800868 gas_query_free(query, 1);
869 return -1;
870 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871
872 return dialog_token;
873}
Roshan Pius3a1667e2018-07-03 15:17:14 -0700874
875
876int gas_query_stop(struct gas_query *gas, u8 dialog_token)
877{
878 struct gas_query_pending *query;
879
880 dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) {
881 if (query->dialog_token == dialog_token) {
882 if (!gas->work) {
883 /* The pending radio work has not yet been
884 * started, but the pending entry has a
885 * reference to the soon to be freed query.
886 * Need to remove that radio work now to avoid
887 * leaving behind a reference to freed memory.
888 */
889 radio_remove_pending_work(gas->wpa_s, query);
890 }
891 gas_query_done(gas, query, GAS_QUERY_STOPPED);
892 return 0;
893 }
894 }
895
896 return -1;
897}