blob: db481a59197af89d20a63453d9f50e68df03ebb4 [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;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080045 int freq;
46 u16 status_code;
Dmitry Shmidt051af732013-10-22 13:52:46 -070047 struct wpabuf *req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080048 struct wpabuf *adv_proto;
49 struct wpabuf *resp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080050 struct os_reltime last_oper;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
52 enum gas_query_result result,
53 const struct wpabuf *adv_proto,
54 const struct wpabuf *resp, u16 status_code);
55 void *ctx;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080056 u8 sa[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080057};
58
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080059/**
60 * struct gas_query - Internal GAS query data
61 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080062struct gas_query {
63 struct wpa_supplicant *wpa_s;
64 struct dl_list pending; /* struct gas_query_pending */
Dmitry Shmidt051af732013-10-22 13:52:46 -070065 struct gas_query_pending *current;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080066 struct wpa_radio_work *work;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080067 struct os_reltime last_mac_addr_rand;
68 int last_rand_sa_type;
69 u8 rand_addr[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070};
71
72
73static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
74static void gas_query_timeout(void *eloop_data, void *user_ctx);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080075static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
76static void gas_query_tx_initial_req(struct gas_query *gas,
77 struct gas_query_pending *query);
78static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080079
80
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080081static int ms_from_time(struct os_reltime *last)
82{
83 struct os_reltime now, res;
84
85 os_get_reltime(&now);
86 os_reltime_sub(&now, last, &res);
87 return res.sec * 1000 + res.usec / 1000;
88}
89
90
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080091/**
92 * gas_query_init - Initialize GAS query component
93 * @wpa_s: Pointer to wpa_supplicant data
94 * Returns: Pointer to GAS query data or %NULL on failure
95 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080096struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
97{
98 struct gas_query *gas;
99
100 gas = os_zalloc(sizeof(*gas));
101 if (gas == NULL)
102 return NULL;
103
104 gas->wpa_s = wpa_s;
105 dl_list_init(&gas->pending);
106
107 return gas;
108}
109
110
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800111static const char * gas_result_txt(enum gas_query_result result)
112{
113 switch (result) {
114 case GAS_QUERY_SUCCESS:
115 return "SUCCESS";
116 case GAS_QUERY_FAILURE:
117 return "FAILURE";
118 case GAS_QUERY_TIMEOUT:
119 return "TIMEOUT";
120 case GAS_QUERY_PEER_ERROR:
121 return "PEER_ERROR";
122 case GAS_QUERY_INTERNAL_ERROR:
123 return "INTERNAL_ERROR";
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800124 case GAS_QUERY_DELETED_AT_DEINIT:
125 return "DELETED_AT_DEINIT";
126 }
127
128 return "N/A";
129}
130
131
132static void gas_query_free(struct gas_query_pending *query, int del_list)
133{
134 struct gas_query *gas = query->gas;
135
136 if (del_list)
137 dl_list_del(&query->list);
138
139 if (gas->work && gas->work->ctx == query) {
140 radio_work_done(gas->work);
141 gas->work = NULL;
142 }
143
144 wpabuf_free(query->req);
145 wpabuf_free(query->adv_proto);
146 wpabuf_free(query->resp);
147 os_free(query);
148}
149
150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800151static void gas_query_done(struct gas_query *gas,
152 struct gas_query_pending *query,
153 enum gas_query_result result)
154{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800155 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
156 " dialog_token=%u freq=%d status_code=%u result=%s",
157 MAC2STR(query->addr), query->dialog_token, query->freq,
158 query->status_code, gas_result_txt(result));
Dmitry Shmidt051af732013-10-22 13:52:46 -0700159 if (gas->current == query)
160 gas->current = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800161 if (query->offchannel_tx_started)
162 offchannel_send_action_done(gas->wpa_s);
163 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
164 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800165 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166 dl_list_del(&query->list);
167 query->cb(query->ctx, query->addr, query->dialog_token, result,
168 query->adv_proto, query->resp, query->status_code);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800169 gas_query_free(query, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170}
171
172
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800173/**
174 * gas_query_deinit - Deinitialize GAS query component
175 * @gas: GAS query data from gas_query_init()
176 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177void gas_query_deinit(struct gas_query *gas)
178{
179 struct gas_query_pending *query, *next;
180
181 if (gas == NULL)
182 return;
183
184 dl_list_for_each_safe(query, next, &gas->pending,
185 struct gas_query_pending, list)
186 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
187
188 os_free(gas);
189}
190
191
192static struct gas_query_pending *
193gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
194{
195 struct gas_query_pending *q;
196 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
197 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
198 q->dialog_token == dialog_token)
199 return q;
200 }
201 return NULL;
202}
203
204
205static int gas_query_append(struct gas_query_pending *query, const u8 *data,
206 size_t len)
207{
208 if (wpabuf_resize(&query->resp, len) < 0) {
209 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
210 return -1;
211 }
212 wpabuf_put_data(query->resp, data, len);
213 return 0;
214}
215
216
Dmitry Shmidt051af732013-10-22 13:52:46 -0700217static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
218 unsigned int freq, const u8 *dst,
219 const u8 *src, const u8 *bssid,
220 const u8 *data, size_t data_len,
221 enum offchannel_send_action_result result)
222{
223 struct gas_query_pending *query;
224 struct gas_query *gas = wpa_s->gas;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800225 int dur;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700226
227 if (gas->current == NULL) {
228 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
229 MACSTR " result=%d - no query in progress",
230 freq, MAC2STR(dst), result);
231 return;
232 }
233
234 query = gas->current;
235
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800236 dur = ms_from_time(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700237 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800238 " result=%d query=%p dialog_token=%u dur=%d ms",
239 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700240 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
241 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
242 return;
243 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800244 os_get_reltime(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700245
246 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
247 eloop_cancel_timeout(gas_query_timeout, gas, query);
248 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
249 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800250 if (query->wait_comeback && !query->retry) {
251 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
252 gas, query);
253 eloop_register_timeout(
254 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
255 gas_query_rx_comeback_timeout, gas, query);
256 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700257 }
258 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
259 eloop_cancel_timeout(gas_query_timeout, gas, query);
260 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
261 }
262}
263
264
Dmitry Shmidt18463232014-01-24 12:29:41 -0800265static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
266{
267 if (wpa_s->current_ssid == NULL ||
268 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
269 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
270 return 0;
271 return wpa_sm_pmf_enabled(wpa_s->wpa);
272}
273
274
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800275static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800276 struct wpabuf *req, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800277{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800278 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700279 const u8 *bssid;
280 const u8 wildcard_bssid[ETH_ALEN] = {
281 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
282 };
Dmitry Shmidt18463232014-01-24 12:29:41 -0800283
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800285 "freq=%d prot=%d using src addr " MACSTR,
286 MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
287 query->freq, prot, MAC2STR(query->sa));
Dmitry Shmidt18463232014-01-24 12:29:41 -0800288 if (prot) {
289 u8 *categ = wpabuf_mhead_u8(req);
290 *categ = WLAN_ACTION_PROTECTED_DUAL;
291 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800292 os_get_reltime(&query->last_oper);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700293 if (gas->wpa_s->max_remain_on_chan &&
294 wait_time > gas->wpa_s->max_remain_on_chan)
295 wait_time = gas->wpa_s->max_remain_on_chan;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700296 if (!gas->wpa_s->conf->gas_address3 ||
297 (gas->wpa_s->current_ssid &&
298 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
299 os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0))
300 bssid = query->addr;
301 else
302 bssid = wildcard_bssid;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800303
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800304 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800305 query->sa, bssid, wpabuf_head(req),
306 wpabuf_len(req), wait_time,
307 gas_query_tx_status, 0);
308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800309 if (res == 0)
310 query->offchannel_tx_started = 1;
311 return res;
312}
313
314
315static void gas_query_tx_comeback_req(struct gas_query *gas,
316 struct gas_query_pending *query)
317{
318 struct wpabuf *req;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800319 unsigned int wait_time;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320
321 req = gas_build_comeback_req(query->dialog_token);
322 if (req == NULL) {
323 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
324 return;
325 }
326
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800327 wait_time = (query->retry || !query->offchannel_tx_started) ?
328 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
329
330 if (gas_query_tx(gas, query, req, wait_time) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800331 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
332 MACSTR, MAC2STR(query->addr));
333 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
334 }
335
336 wpabuf_free(req);
337}
338
339
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800340static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
341{
342 struct gas_query *gas = eloop_data;
343 struct gas_query_pending *query = user_ctx;
344 int dialog_token;
345
346 wpa_printf(MSG_DEBUG,
347 "GAS: No response to comeback request received (retry=%u)",
348 query->retry);
349 if (gas->current != query || query->retry)
350 return;
351 dialog_token = gas_query_new_dialog_token(gas, query->addr);
352 if (dialog_token < 0)
353 return;
354 wpa_printf(MSG_DEBUG,
355 "GAS: Retry GAS query due to comeback response timeout");
356 query->retry = 1;
357 query->dialog_token = dialog_token;
358 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
359 query->wait_comeback = 0;
360 query->next_frag_id = 0;
361 wpabuf_free(query->adv_proto);
362 query->adv_proto = NULL;
363 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
364 eloop_cancel_timeout(gas_query_timeout, gas, query);
365 gas_query_tx_initial_req(gas, query);
366}
367
368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800369static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
370{
371 struct gas_query *gas = eloop_data;
372 struct gas_query_pending *query = user_ctx;
373
374 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
375 MAC2STR(query->addr));
376 gas_query_tx_comeback_req(gas, query);
377}
378
379
380static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
381 struct gas_query_pending *query,
382 u16 comeback_delay)
383{
384 unsigned int secs, usecs;
385
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800386 if (comeback_delay > 1 && query->offchannel_tx_started) {
387 offchannel_send_action_done(gas->wpa_s);
388 query->offchannel_tx_started = 0;
389 }
390
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391 secs = (comeback_delay * 1024) / 1000000;
392 usecs = comeback_delay * 1024 - secs * 1000000;
393 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
394 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
395 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
396 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
397 gas, query);
398}
399
400
401static void gas_query_rx_initial(struct gas_query *gas,
402 struct gas_query_pending *query,
403 const u8 *adv_proto, const u8 *resp,
404 size_t len, u16 comeback_delay)
405{
406 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
407 MACSTR " (dialog_token=%u comeback_delay=%u)",
408 MAC2STR(query->addr), query->dialog_token, comeback_delay);
409
410 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
411 if (query->adv_proto == NULL) {
412 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
413 return;
414 }
415
416 if (comeback_delay) {
Paul Stewart092955c2017-02-06 09:13:09 -0800417 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800418 query->wait_comeback = 1;
419 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
420 return;
421 }
422
423 /* Query was completed without comeback mechanism */
424 if (gas_query_append(query, resp, len) < 0) {
425 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
426 return;
427 }
428
429 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
430}
431
432
433static void gas_query_rx_comeback(struct gas_query *gas,
434 struct gas_query_pending *query,
435 const u8 *adv_proto, const u8 *resp,
436 size_t len, u8 frag_id, u8 more_frags,
437 u16 comeback_delay)
438{
439 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
440 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
441 "comeback_delay=%u)",
442 MAC2STR(query->addr), query->dialog_token, frag_id,
443 more_frags, comeback_delay);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800444 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800445
446 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
447 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
448 wpabuf_len(query->adv_proto)) != 0) {
449 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
450 "between initial and comeback response from "
451 MACSTR, MAC2STR(query->addr));
452 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
453 return;
454 }
455
456 if (comeback_delay) {
457 if (frag_id) {
458 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
459 "with non-zero frag_id and comeback_delay "
460 "from " MACSTR, MAC2STR(query->addr));
461 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
462 return;
463 }
464 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
465 return;
466 }
467
468 if (frag_id != query->next_frag_id) {
469 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
470 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700471 if (frag_id + 1 == query->next_frag_id) {
472 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
473 "retry of previous fragment");
474 return;
475 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800476 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
477 return;
478 }
479 query->next_frag_id++;
480
481 if (gas_query_append(query, resp, len) < 0) {
482 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
483 return;
484 }
485
486 if (more_frags) {
487 gas_query_tx_comeback_req(gas, query);
488 return;
489 }
490
491 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
492}
493
494
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800495/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800496 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800497 * @gas: GAS query data from gas_query_init()
498 * @da: Destination MAC address of the Action frame
499 * @sa: Source MAC address of the Action frame
500 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800501 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800502 * @data: Payload of the Action frame
503 * @len: Length of @data
504 * @freq: Frequency (in MHz) on which the frame was received
505 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
506 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800507int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800508 const u8 *bssid, u8 categ, const u8 *data, size_t len,
509 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800510{
511 struct gas_query_pending *query;
512 u8 action, dialog_token, frag_id = 0, more_frags = 0;
513 u16 comeback_delay, resp_len;
514 const u8 *pos, *adv_proto;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800515 int prot, pmf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800516 unsigned int left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800517
518 if (gas == NULL || len < 4)
519 return -1;
520
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700521 pos = data;
522 action = *pos++;
523 dialog_token = *pos++;
524
525 if (action != WLAN_PA_GAS_INITIAL_RESP &&
526 action != WLAN_PA_GAS_COMEBACK_RESP)
527 return -1; /* Not a GAS response */
528
Dmitry Shmidt18463232014-01-24 12:29:41 -0800529 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800530 pmf = pmf_in_use(gas->wpa_s, sa);
Dmitry Shmidt18463232014-01-24 12:29:41 -0800531 if (prot && !pmf) {
532 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
533 return 0;
534 }
535 if (!prot && pmf) {
536 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
537 return 0;
538 }
539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800540 query = gas_query_get_pending(gas, sa, dialog_token);
541 if (query == NULL) {
542 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
543 " dialog token %u", MAC2STR(sa), dialog_token);
544 return -1;
545 }
546
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800547 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
548 ms_from_time(&query->last_oper), MAC2STR(sa));
549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800550 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
551 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
552 MACSTR " dialog token %u when waiting for comeback "
553 "response", MAC2STR(sa), dialog_token);
554 return 0;
555 }
556
557 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
558 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
559 MACSTR " dialog token %u when waiting for initial "
560 "response", MAC2STR(sa), dialog_token);
561 return 0;
562 }
563
564 query->status_code = WPA_GET_LE16(pos);
565 pos += 2;
566
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800567 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
568 action == WLAN_PA_GAS_COMEBACK_RESP) {
569 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
570 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
572 "%u failed - status code %u",
573 MAC2STR(sa), dialog_token, query->status_code);
574 gas_query_done(gas, query, GAS_QUERY_FAILURE);
575 return 0;
576 }
577
578 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
579 if (pos + 1 > data + len)
580 return 0;
581 frag_id = *pos & 0x7f;
582 more_frags = (*pos & 0x80) >> 7;
583 pos++;
584 }
585
586 /* Comeback Delay */
587 if (pos + 2 > data + len)
588 return 0;
589 comeback_delay = WPA_GET_LE16(pos);
590 pos += 2;
591
592 /* Advertisement Protocol element */
593 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
594 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
595 "Protocol element in the response from " MACSTR,
596 MAC2STR(sa));
597 return 0;
598 }
599
600 if (*pos != WLAN_EID_ADV_PROTO) {
601 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
602 "Protocol element ID %u in response from " MACSTR,
603 *pos, MAC2STR(sa));
604 return 0;
605 }
606
607 adv_proto = pos;
608 pos += 2 + pos[1];
609
610 /* Query Response Length */
611 if (pos + 2 > data + len) {
612 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
613 return 0;
614 }
615 resp_len = WPA_GET_LE16(pos);
616 pos += 2;
617
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800618 left = data + len - pos;
619 if (resp_len > left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
621 "response from " MACSTR, MAC2STR(sa));
622 return 0;
623 }
624
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800625 if (resp_len < left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
627 "after Query Response from " MACSTR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800628 left - resp_len, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800629 }
630
631 if (action == WLAN_PA_GAS_COMEBACK_RESP)
632 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
633 frag_id, more_frags, comeback_delay);
634 else
635 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
636 comeback_delay);
637
638 return 0;
639}
640
641
642static void gas_query_timeout(void *eloop_data, void *user_ctx)
643{
644 struct gas_query *gas = eloop_data;
645 struct gas_query_pending *query = user_ctx;
646
Dmitry Shmidt051af732013-10-22 13:52:46 -0700647 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
648 " dialog token %u",
649 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800650 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
651}
652
653
654static int gas_query_dialog_token_available(struct gas_query *gas,
655 const u8 *dst, u8 dialog_token)
656{
657 struct gas_query_pending *q;
658 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
659 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
660 dialog_token == q->dialog_token)
661 return 0;
662 }
663
664 return 1;
665}
666
667
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800668static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
669{
670 struct gas_query_pending *query = work->ctx;
671 struct gas_query *gas = query->gas;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700672 struct wpa_supplicant *wpa_s = gas->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800673
674 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800675 if (work->started) {
676 gas->work = NULL;
677 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
678 return;
679 }
680
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800681 gas_query_free(query, 1);
682 return;
683 }
684
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700685 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
686 wpa_msg(wpa_s, MSG_INFO,
687 "Failed to assign random MAC address for GAS");
688 gas_query_free(query, 1);
689 radio_work_done(work);
690 return;
691 }
692
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800693 gas->work = work;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800694 gas_query_tx_initial_req(gas, query);
695}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800696
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800697
698static void gas_query_tx_initial_req(struct gas_query *gas,
699 struct gas_query_pending *query)
700{
701 if (gas_query_tx(gas, query, query->req,
702 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800703 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
704 MACSTR, MAC2STR(query->addr));
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700705 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800706 return;
707 }
708 gas->current = query;
709
710 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
711 query->dialog_token);
712 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
713 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800714}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800715
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800716
717static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
718{
719 static int next_start = 0;
720 int dialog_token;
721
722 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
723 if (gas_query_dialog_token_available(
724 gas, dst, (next_start + dialog_token) % 256))
725 break;
726 }
727 if (dialog_token == 256)
728 return -1; /* Too many pending queries */
729 dialog_token = (next_start + dialog_token) % 256;
730 next_start = (dialog_token + 1) % 256;
731 return dialog_token;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800732}
733
734
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800735static int gas_query_set_sa(struct gas_query *gas,
736 struct gas_query_pending *query)
737{
738 struct wpa_supplicant *wpa_s = gas->wpa_s;
739 struct os_reltime now;
740
741 if (!wpa_s->conf->gas_rand_mac_addr ||
742 !(wpa_s->current_bss ?
743 (wpa_s->drv_flags &
744 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
745 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
746 /* Use own MAC address as the transmitter address */
747 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
748 return 0;
749 }
750
751 os_get_reltime(&now);
752
753 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
754 gas->last_mac_addr_rand.sec != 0 &&
755 !os_reltime_expired(&now, &gas->last_mac_addr_rand,
756 wpa_s->conf->gas_rand_addr_lifetime)) {
757 wpa_printf(MSG_DEBUG,
758 "GAS: Use the previously selected random transmitter address "
759 MACSTR, MAC2STR(gas->rand_addr));
760 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
761 return 0;
762 }
763
764 if (wpa_s->conf->gas_rand_mac_addr == 1 &&
765 random_mac_addr(gas->rand_addr) < 0) {
766 wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
767 return -1;
768 }
769
770 if (wpa_s->conf->gas_rand_mac_addr == 2 &&
771 random_mac_addr_keep_oui(gas->rand_addr) < 0) {
772 wpa_printf(MSG_ERROR,
773 "GAS: Failed to get random address with same OUI");
774 return -1;
775 }
776
777 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
778 MACSTR, MAC2STR(gas->rand_addr));
779 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
780 os_get_reltime(&gas->last_mac_addr_rand);
781 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
782
783 return 0;
784}
785
786
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800787/**
788 * gas_query_req - Request a GAS query
789 * @gas: GAS query data from gas_query_init()
790 * @dst: Destination MAC address for the query
791 * @freq: Frequency (in MHz) for the channel on which to send the query
Dmitry Shmidt051af732013-10-22 13:52:46 -0700792 * @req: GAS query payload (to be freed by gas_query module in case of success
793 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800794 * @cb: Callback function for reporting GAS query result and response
795 * @ctx: Context pointer to use with the @cb call
796 * Returns: dialog token (>= 0) on success or -1 on failure
797 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
799 struct wpabuf *req,
800 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
801 enum gas_query_result result,
802 const struct wpabuf *adv_proto,
803 const struct wpabuf *resp, u16 status_code),
804 void *ctx)
805{
806 struct gas_query_pending *query;
807 int dialog_token;
808
809 if (wpabuf_len(req) < 3)
810 return -1;
811
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800812 dialog_token = gas_query_new_dialog_token(gas, dst);
813 if (dialog_token < 0)
814 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800815
816 query = os_zalloc(sizeof(*query));
817 if (query == NULL)
818 return -1;
819
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800820 query->gas = gas;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800821 if (gas_query_set_sa(gas, query)) {
822 os_free(query);
823 return -1;
824 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800825 os_memcpy(query->addr, dst, ETH_ALEN);
826 query->dialog_token = dialog_token;
827 query->freq = freq;
828 query->cb = cb;
829 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700830 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831 dl_list_add(&gas->pending, &query->list);
832
833 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
834
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800835 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
836 " dialog_token=%u freq=%d",
837 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800838
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800839 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
840 query) < 0) {
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700841 query->req = NULL; /* caller will free this in error case */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800842 gas_query_free(query, 1);
843 return -1;
844 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845
846 return dialog_token;
847}