blob: 759b9b9cd4015b61b6e810d9270720c11459eddd [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
Hai Shalom74f70d42019-02-11 14:42:39 -0800276int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
Dmitry Shmidt18463232014-01-24 12:29:41 -0800277{
278 if (wpa_s->current_ssid == NULL ||
279 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
280 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
281 return 0;
282 return wpa_sm_pmf_enabled(wpa_s->wpa);
283}
284
285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800287 struct wpabuf *req, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800288{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800289 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700290 const u8 *bssid;
291 const u8 wildcard_bssid[ETH_ALEN] = {
292 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
293 };
Dmitry Shmidt18463232014-01-24 12:29:41 -0800294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800295 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800296 "freq=%d prot=%d using src addr " MACSTR,
297 MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
298 query->freq, prot, MAC2STR(query->sa));
Dmitry Shmidt18463232014-01-24 12:29:41 -0800299 if (prot) {
300 u8 *categ = wpabuf_mhead_u8(req);
301 *categ = WLAN_ACTION_PROTECTED_DUAL;
302 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800303 os_get_reltime(&query->last_oper);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700304 if (gas->wpa_s->max_remain_on_chan &&
305 wait_time > gas->wpa_s->max_remain_on_chan)
306 wait_time = gas->wpa_s->max_remain_on_chan;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700307 if (!query->wildcard_bssid &&
308 (!gas->wpa_s->conf->gas_address3 ||
309 (gas->wpa_s->current_ssid &&
310 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
311 os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0)))
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700312 bssid = query->addr;
313 else
314 bssid = wildcard_bssid;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800317 query->sa, bssid, wpabuf_head(req),
318 wpabuf_len(req), wait_time,
319 gas_query_tx_status, 0);
320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321 if (res == 0)
322 query->offchannel_tx_started = 1;
323 return res;
324}
325
326
327static void gas_query_tx_comeback_req(struct gas_query *gas,
328 struct gas_query_pending *query)
329{
330 struct wpabuf *req;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800331 unsigned int wait_time;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800332
333 req = gas_build_comeback_req(query->dialog_token);
334 if (req == NULL) {
335 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
336 return;
337 }
338
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800339 wait_time = (query->retry || !query->offchannel_tx_started) ?
340 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
341
342 if (gas_query_tx(gas, query, req, wait_time) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800343 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
344 MACSTR, MAC2STR(query->addr));
345 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
346 }
347
348 wpabuf_free(req);
349}
350
351
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800352static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
353{
354 struct gas_query *gas = eloop_data;
355 struct gas_query_pending *query = user_ctx;
356 int dialog_token;
357
358 wpa_printf(MSG_DEBUG,
359 "GAS: No response to comeback request received (retry=%u)",
360 query->retry);
361 if (gas->current != query || query->retry)
362 return;
363 dialog_token = gas_query_new_dialog_token(gas, query->addr);
364 if (dialog_token < 0)
365 return;
366 wpa_printf(MSG_DEBUG,
367 "GAS: Retry GAS query due to comeback response timeout");
368 query->retry = 1;
369 query->dialog_token = dialog_token;
370 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
371 query->wait_comeback = 0;
372 query->next_frag_id = 0;
373 wpabuf_free(query->adv_proto);
374 query->adv_proto = NULL;
375 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
376 eloop_cancel_timeout(gas_query_timeout, gas, query);
377 gas_query_tx_initial_req(gas, query);
378}
379
380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
382{
383 struct gas_query *gas = eloop_data;
384 struct gas_query_pending *query = user_ctx;
385
386 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
387 MAC2STR(query->addr));
388 gas_query_tx_comeback_req(gas, query);
389}
390
391
392static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
393 struct gas_query_pending *query,
394 u16 comeback_delay)
395{
396 unsigned int secs, usecs;
397
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800398 if (comeback_delay > 1 && query->offchannel_tx_started) {
399 offchannel_send_action_done(gas->wpa_s);
400 query->offchannel_tx_started = 0;
401 }
402
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800403 secs = (comeback_delay * 1024) / 1000000;
404 usecs = comeback_delay * 1024 - secs * 1000000;
405 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
406 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
407 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
408 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
409 gas, query);
410}
411
412
413static void gas_query_rx_initial(struct gas_query *gas,
414 struct gas_query_pending *query,
415 const u8 *adv_proto, const u8 *resp,
416 size_t len, u16 comeback_delay)
417{
418 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
419 MACSTR " (dialog_token=%u comeback_delay=%u)",
420 MAC2STR(query->addr), query->dialog_token, comeback_delay);
421
422 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
423 if (query->adv_proto == NULL) {
424 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
425 return;
426 }
427
428 if (comeback_delay) {
Paul Stewart092955c2017-02-06 09:13:09 -0800429 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800430 query->wait_comeback = 1;
431 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
432 return;
433 }
434
435 /* Query was completed without comeback mechanism */
436 if (gas_query_append(query, resp, len) < 0) {
437 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
438 return;
439 }
440
441 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
442}
443
444
445static void gas_query_rx_comeback(struct gas_query *gas,
446 struct gas_query_pending *query,
447 const u8 *adv_proto, const u8 *resp,
448 size_t len, u8 frag_id, u8 more_frags,
449 u16 comeback_delay)
450{
451 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
452 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
453 "comeback_delay=%u)",
454 MAC2STR(query->addr), query->dialog_token, frag_id,
455 more_frags, comeback_delay);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800456 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457
458 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
459 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
460 wpabuf_len(query->adv_proto)) != 0) {
461 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
462 "between initial and comeback response from "
463 MACSTR, MAC2STR(query->addr));
464 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
465 return;
466 }
467
468 if (comeback_delay) {
469 if (frag_id) {
470 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
471 "with non-zero frag_id and comeback_delay "
472 "from " MACSTR, MAC2STR(query->addr));
473 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
474 return;
475 }
476 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
477 return;
478 }
479
480 if (frag_id != query->next_frag_id) {
481 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
482 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700483 if (frag_id + 1 == query->next_frag_id) {
484 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
485 "retry of previous fragment");
486 return;
487 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800488 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
489 return;
490 }
491 query->next_frag_id++;
492
493 if (gas_query_append(query, resp, len) < 0) {
494 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
495 return;
496 }
497
498 if (more_frags) {
499 gas_query_tx_comeback_req(gas, query);
500 return;
501 }
502
503 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
504}
505
506
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800507/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800508 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800509 * @gas: GAS query data from gas_query_init()
510 * @da: Destination MAC address of the Action frame
511 * @sa: Source MAC address of the Action frame
512 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800513 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800514 * @data: Payload of the Action frame
515 * @len: Length of @data
516 * @freq: Frequency (in MHz) on which the frame was received
517 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
518 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800519int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800520 const u8 *bssid, u8 categ, const u8 *data, size_t len,
521 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522{
523 struct gas_query_pending *query;
524 u8 action, dialog_token, frag_id = 0, more_frags = 0;
525 u16 comeback_delay, resp_len;
526 const u8 *pos, *adv_proto;
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);
602 pos += 2;
603
604 /* Advertisement Protocol element */
605 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
606 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
607 "Protocol element in the response from " MACSTR,
608 MAC2STR(sa));
609 return 0;
610 }
611
612 if (*pos != WLAN_EID_ADV_PROTO) {
613 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
614 "Protocol element ID %u in response from " MACSTR,
615 *pos, MAC2STR(sa));
616 return 0;
617 }
618
619 adv_proto = pos;
620 pos += 2 + pos[1];
621
622 /* Query Response Length */
623 if (pos + 2 > data + len) {
624 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
625 return 0;
626 }
627 resp_len = WPA_GET_LE16(pos);
628 pos += 2;
629
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800630 left = data + len - pos;
631 if (resp_len > left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800632 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
633 "response from " MACSTR, MAC2STR(sa));
634 return 0;
635 }
636
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800637 if (resp_len < left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800638 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
639 "after Query Response from " MACSTR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800640 left - resp_len, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800641 }
642
643 if (action == WLAN_PA_GAS_COMEBACK_RESP)
644 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
645 frag_id, more_frags, comeback_delay);
646 else
647 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
648 comeback_delay);
649
650 return 0;
651}
652
653
654static void gas_query_timeout(void *eloop_data, void *user_ctx)
655{
656 struct gas_query *gas = eloop_data;
657 struct gas_query_pending *query = user_ctx;
658
Dmitry Shmidt051af732013-10-22 13:52:46 -0700659 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
660 " dialog token %u",
661 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800662 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
663}
664
665
666static int gas_query_dialog_token_available(struct gas_query *gas,
667 const u8 *dst, u8 dialog_token)
668{
669 struct gas_query_pending *q;
670 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
671 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
672 dialog_token == q->dialog_token)
673 return 0;
674 }
675
676 return 1;
677}
678
679
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800680static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
681{
682 struct gas_query_pending *query = work->ctx;
683 struct gas_query *gas = query->gas;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700684 struct wpa_supplicant *wpa_s = gas->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800685
686 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800687 if (work->started) {
688 gas->work = NULL;
689 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
690 return;
691 }
692
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800693 gas_query_free(query, 1);
694 return;
695 }
696
Hai Shalomb755a2a2020-04-23 21:49:02 -0700697 if (!query->maintain_addr &&
698 wpas_update_random_addr_disassoc(wpa_s) < 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700699 wpa_msg(wpa_s, MSG_INFO,
700 "Failed to assign random MAC address for GAS");
701 gas_query_free(query, 1);
702 radio_work_done(work);
703 return;
704 }
705
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800706 gas->work = work;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800707 gas_query_tx_initial_req(gas, query);
708}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800709
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800710
711static void gas_query_tx_initial_req(struct gas_query *gas,
712 struct gas_query_pending *query)
713{
714 if (gas_query_tx(gas, query, query->req,
715 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800716 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
717 MACSTR, MAC2STR(query->addr));
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700718 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800719 return;
720 }
721 gas->current = query;
722
723 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
724 query->dialog_token);
725 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
726 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800727}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800728
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800729
730static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
731{
732 static int next_start = 0;
733 int dialog_token;
734
735 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
736 if (gas_query_dialog_token_available(
737 gas, dst, (next_start + dialog_token) % 256))
738 break;
739 }
740 if (dialog_token == 256)
741 return -1; /* Too many pending queries */
742 dialog_token = (next_start + dialog_token) % 256;
743 next_start = (dialog_token + 1) % 256;
744 return dialog_token;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800745}
746
747
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800748static int gas_query_set_sa(struct gas_query *gas,
749 struct gas_query_pending *query)
750{
751 struct wpa_supplicant *wpa_s = gas->wpa_s;
752 struct os_reltime now;
753
Hai Shalomb755a2a2020-04-23 21:49:02 -0700754 if (query->maintain_addr ||
755 !wpa_s->conf->gas_rand_mac_addr ||
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800756 !(wpa_s->current_bss ?
757 (wpa_s->drv_flags &
758 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
759 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
760 /* Use own MAC address as the transmitter address */
Hai Shalomb755a2a2020-04-23 21:49:02 -0700761 wpa_printf(MSG_DEBUG,
762 "GAS: Use own MAC address as the transmitter address%s%s%s",
763 query->maintain_addr ? " (maintain_addr)" : "",
764 !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "",
765 !(wpa_s->current_bss ?
766 (wpa_s->drv_flags &
767 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
768 (wpa_s->drv_flags &
769 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ?
770 " (no driver rand capa" : "");
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800771 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
772 return 0;
773 }
774
775 os_get_reltime(&now);
776
777 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
778 gas->last_mac_addr_rand.sec != 0 &&
779 !os_reltime_expired(&now, &gas->last_mac_addr_rand,
780 wpa_s->conf->gas_rand_addr_lifetime)) {
781 wpa_printf(MSG_DEBUG,
782 "GAS: Use the previously selected random transmitter address "
783 MACSTR, MAC2STR(gas->rand_addr));
784 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
785 return 0;
786 }
787
788 if (wpa_s->conf->gas_rand_mac_addr == 1 &&
789 random_mac_addr(gas->rand_addr) < 0) {
790 wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
791 return -1;
792 }
793
794 if (wpa_s->conf->gas_rand_mac_addr == 2 &&
795 random_mac_addr_keep_oui(gas->rand_addr) < 0) {
796 wpa_printf(MSG_ERROR,
797 "GAS: Failed to get random address with same OUI");
798 return -1;
799 }
800
801 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
802 MACSTR, MAC2STR(gas->rand_addr));
803 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
804 os_get_reltime(&gas->last_mac_addr_rand);
805 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
806
807 return 0;
808}
809
810
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800811/**
812 * gas_query_req - Request a GAS query
813 * @gas: GAS query data from gas_query_init()
814 * @dst: Destination MAC address for the query
815 * @freq: Frequency (in MHz) for the channel on which to send the query
Hai Shalomb755a2a2020-04-23 21:49:02 -0700816 * @wildcard_bssid: Force use of wildcard BSSID value
817 * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC
818 * address randomization rules)
Dmitry Shmidt051af732013-10-22 13:52:46 -0700819 * @req: GAS query payload (to be freed by gas_query module in case of success
820 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800821 * @cb: Callback function for reporting GAS query result and response
822 * @ctx: Context pointer to use with the @cb call
823 * Returns: dialog token (>= 0) on success or -1 on failure
824 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
Hai Shalomb755a2a2020-04-23 21:49:02 -0700826 int wildcard_bssid, int maintain_addr, struct wpabuf *req,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800827 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
828 enum gas_query_result result,
829 const struct wpabuf *adv_proto,
830 const struct wpabuf *resp, u16 status_code),
831 void *ctx)
832{
833 struct gas_query_pending *query;
834 int dialog_token;
835
836 if (wpabuf_len(req) < 3)
837 return -1;
838
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800839 dialog_token = gas_query_new_dialog_token(gas, dst);
840 if (dialog_token < 0)
841 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800842
843 query = os_zalloc(sizeof(*query));
844 if (query == NULL)
845 return -1;
846
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800847 query->gas = gas;
Hai Shalomb755a2a2020-04-23 21:49:02 -0700848 query->maintain_addr = !!maintain_addr;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800849 if (gas_query_set_sa(gas, query)) {
850 os_free(query);
851 return -1;
852 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853 os_memcpy(query->addr, dst, ETH_ALEN);
854 query->dialog_token = dialog_token;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700855 query->wildcard_bssid = !!wildcard_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800856 query->freq = freq;
857 query->cb = cb;
858 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700859 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800860 dl_list_add(&gas->pending, &query->list);
861
862 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
863
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800864 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
865 " dialog_token=%u freq=%d",
866 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800868 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
869 query) < 0) {
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700870 query->req = NULL; /* caller will free this in error case */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800871 gas_query_free(query, 1);
872 return -1;
873 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800874
875 return dialog_token;
876}
Roshan Pius3a1667e2018-07-03 15:17:14 -0700877
878
879int gas_query_stop(struct gas_query *gas, u8 dialog_token)
880{
881 struct gas_query_pending *query;
882
883 dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) {
884 if (query->dialog_token == dialog_token) {
885 if (!gas->work) {
886 /* The pending radio work has not yet been
887 * started, but the pending entry has a
888 * reference to the soon to be freed query.
889 * Need to remove that radio work now to avoid
890 * leaving behind a reference to freed memory.
891 */
892 radio_remove_pending_work(gas->wpa_s, query);
893 }
894 gas_query_done(gas, query, GAS_QUERY_STOPPED);
895 return 0;
896 }
897 }
898
899 return -1;
900}