blob: 91cf19a20f20496197291b315adfdd62a3c95f7f [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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700246 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS ||
247 result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700248 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700249 if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
250 wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
251 eloop_register_timeout(0, 250000,
252 gas_query_timeout, gas, query);
253 } else {
254 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
255 gas_query_timeout, gas, query);
256 }
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800257 if (query->wait_comeback && !query->retry) {
258 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
259 gas, query);
260 eloop_register_timeout(
261 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
262 gas_query_rx_comeback_timeout, gas, query);
263 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700264 }
265 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
266 eloop_cancel_timeout(gas_query_timeout, gas, query);
267 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
268 }
269}
270
271
Dmitry Shmidt18463232014-01-24 12:29:41 -0800272static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
273{
274 if (wpa_s->current_ssid == NULL ||
275 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
276 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
277 return 0;
278 return wpa_sm_pmf_enabled(wpa_s->wpa);
279}
280
281
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800282static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800283 struct wpabuf *req, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800285 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700286 const u8 *bssid;
287 const u8 wildcard_bssid[ETH_ALEN] = {
288 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
289 };
Dmitry Shmidt18463232014-01-24 12:29:41 -0800290
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800292 "freq=%d prot=%d using src addr " MACSTR,
293 MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
294 query->freq, prot, MAC2STR(query->sa));
Dmitry Shmidt18463232014-01-24 12:29:41 -0800295 if (prot) {
296 u8 *categ = wpabuf_mhead_u8(req);
297 *categ = WLAN_ACTION_PROTECTED_DUAL;
298 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800299 os_get_reltime(&query->last_oper);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700300 if (gas->wpa_s->max_remain_on_chan &&
301 wait_time > gas->wpa_s->max_remain_on_chan)
302 wait_time = gas->wpa_s->max_remain_on_chan;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700303 if (!gas->wpa_s->conf->gas_address3 ||
304 (gas->wpa_s->current_ssid &&
305 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
306 os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0))
307 bssid = query->addr;
308 else
309 bssid = wildcard_bssid;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800312 query->sa, bssid, wpabuf_head(req),
313 wpabuf_len(req), wait_time,
314 gas_query_tx_status, 0);
315
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 if (res == 0)
317 query->offchannel_tx_started = 1;
318 return res;
319}
320
321
322static void gas_query_tx_comeback_req(struct gas_query *gas,
323 struct gas_query_pending *query)
324{
325 struct wpabuf *req;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800326 unsigned int wait_time;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800327
328 req = gas_build_comeback_req(query->dialog_token);
329 if (req == NULL) {
330 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
331 return;
332 }
333
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800334 wait_time = (query->retry || !query->offchannel_tx_started) ?
335 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
336
337 if (gas_query_tx(gas, query, req, wait_time) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800338 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
339 MACSTR, MAC2STR(query->addr));
340 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
341 }
342
343 wpabuf_free(req);
344}
345
346
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800347static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
348{
349 struct gas_query *gas = eloop_data;
350 struct gas_query_pending *query = user_ctx;
351 int dialog_token;
352
353 wpa_printf(MSG_DEBUG,
354 "GAS: No response to comeback request received (retry=%u)",
355 query->retry);
356 if (gas->current != query || query->retry)
357 return;
358 dialog_token = gas_query_new_dialog_token(gas, query->addr);
359 if (dialog_token < 0)
360 return;
361 wpa_printf(MSG_DEBUG,
362 "GAS: Retry GAS query due to comeback response timeout");
363 query->retry = 1;
364 query->dialog_token = dialog_token;
365 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
366 query->wait_comeback = 0;
367 query->next_frag_id = 0;
368 wpabuf_free(query->adv_proto);
369 query->adv_proto = NULL;
370 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
371 eloop_cancel_timeout(gas_query_timeout, gas, query);
372 gas_query_tx_initial_req(gas, query);
373}
374
375
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800376static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
377{
378 struct gas_query *gas = eloop_data;
379 struct gas_query_pending *query = user_ctx;
380
381 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
382 MAC2STR(query->addr));
383 gas_query_tx_comeback_req(gas, query);
384}
385
386
387static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
388 struct gas_query_pending *query,
389 u16 comeback_delay)
390{
391 unsigned int secs, usecs;
392
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800393 if (comeback_delay > 1 && query->offchannel_tx_started) {
394 offchannel_send_action_done(gas->wpa_s);
395 query->offchannel_tx_started = 0;
396 }
397
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800398 secs = (comeback_delay * 1024) / 1000000;
399 usecs = comeback_delay * 1024 - secs * 1000000;
400 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
401 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
402 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
403 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
404 gas, query);
405}
406
407
408static void gas_query_rx_initial(struct gas_query *gas,
409 struct gas_query_pending *query,
410 const u8 *adv_proto, const u8 *resp,
411 size_t len, u16 comeback_delay)
412{
413 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
414 MACSTR " (dialog_token=%u comeback_delay=%u)",
415 MAC2STR(query->addr), query->dialog_token, comeback_delay);
416
417 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
418 if (query->adv_proto == NULL) {
419 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
420 return;
421 }
422
423 if (comeback_delay) {
Paul Stewart092955c2017-02-06 09:13:09 -0800424 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800425 query->wait_comeback = 1;
426 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
427 return;
428 }
429
430 /* Query was completed without comeback mechanism */
431 if (gas_query_append(query, resp, len) < 0) {
432 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
433 return;
434 }
435
436 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
437}
438
439
440static void gas_query_rx_comeback(struct gas_query *gas,
441 struct gas_query_pending *query,
442 const u8 *adv_proto, const u8 *resp,
443 size_t len, u8 frag_id, u8 more_frags,
444 u16 comeback_delay)
445{
446 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
447 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
448 "comeback_delay=%u)",
449 MAC2STR(query->addr), query->dialog_token, frag_id,
450 more_frags, comeback_delay);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800451 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800452
453 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
454 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
455 wpabuf_len(query->adv_proto)) != 0) {
456 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
457 "between initial and comeback response from "
458 MACSTR, MAC2STR(query->addr));
459 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
460 return;
461 }
462
463 if (comeback_delay) {
464 if (frag_id) {
465 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
466 "with non-zero frag_id and comeback_delay "
467 "from " MACSTR, MAC2STR(query->addr));
468 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
469 return;
470 }
471 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
472 return;
473 }
474
475 if (frag_id != query->next_frag_id) {
476 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
477 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700478 if (frag_id + 1 == query->next_frag_id) {
479 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
480 "retry of previous fragment");
481 return;
482 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800483 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
484 return;
485 }
486 query->next_frag_id++;
487
488 if (gas_query_append(query, resp, len) < 0) {
489 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
490 return;
491 }
492
493 if (more_frags) {
494 gas_query_tx_comeback_req(gas, query);
495 return;
496 }
497
498 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
499}
500
501
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800502/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800503 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800504 * @gas: GAS query data from gas_query_init()
505 * @da: Destination MAC address of the Action frame
506 * @sa: Source MAC address of the Action frame
507 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800508 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800509 * @data: Payload of the Action frame
510 * @len: Length of @data
511 * @freq: Frequency (in MHz) on which the frame was received
512 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
513 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800515 const u8 *bssid, u8 categ, const u8 *data, size_t len,
516 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800517{
518 struct gas_query_pending *query;
519 u8 action, dialog_token, frag_id = 0, more_frags = 0;
520 u16 comeback_delay, resp_len;
521 const u8 *pos, *adv_proto;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800522 int prot, pmf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800523 unsigned int left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800524
525 if (gas == NULL || len < 4)
526 return -1;
527
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700528 pos = data;
529 action = *pos++;
530 dialog_token = *pos++;
531
532 if (action != WLAN_PA_GAS_INITIAL_RESP &&
533 action != WLAN_PA_GAS_COMEBACK_RESP)
534 return -1; /* Not a GAS response */
535
Dmitry Shmidt18463232014-01-24 12:29:41 -0800536 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800537 pmf = pmf_in_use(gas->wpa_s, sa);
Dmitry Shmidt18463232014-01-24 12:29:41 -0800538 if (prot && !pmf) {
539 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
540 return 0;
541 }
542 if (!prot && pmf) {
543 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
544 return 0;
545 }
546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547 query = gas_query_get_pending(gas, sa, dialog_token);
548 if (query == NULL) {
549 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
550 " dialog token %u", MAC2STR(sa), dialog_token);
551 return -1;
552 }
553
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800554 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
555 ms_from_time(&query->last_oper), MAC2STR(sa));
556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800557 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
558 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
559 MACSTR " dialog token %u when waiting for comeback "
560 "response", MAC2STR(sa), dialog_token);
561 return 0;
562 }
563
564 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
565 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
566 MACSTR " dialog token %u when waiting for initial "
567 "response", MAC2STR(sa), dialog_token);
568 return 0;
569 }
570
571 query->status_code = WPA_GET_LE16(pos);
572 pos += 2;
573
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800574 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
575 action == WLAN_PA_GAS_COMEBACK_RESP) {
576 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
577 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800578 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
579 "%u failed - status code %u",
580 MAC2STR(sa), dialog_token, query->status_code);
581 gas_query_done(gas, query, GAS_QUERY_FAILURE);
582 return 0;
583 }
584
585 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
586 if (pos + 1 > data + len)
587 return 0;
588 frag_id = *pos & 0x7f;
589 more_frags = (*pos & 0x80) >> 7;
590 pos++;
591 }
592
593 /* Comeback Delay */
594 if (pos + 2 > data + len)
595 return 0;
596 comeback_delay = WPA_GET_LE16(pos);
597 pos += 2;
598
599 /* Advertisement Protocol element */
600 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
601 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
602 "Protocol element in the response from " MACSTR,
603 MAC2STR(sa));
604 return 0;
605 }
606
607 if (*pos != WLAN_EID_ADV_PROTO) {
608 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
609 "Protocol element ID %u in response from " MACSTR,
610 *pos, MAC2STR(sa));
611 return 0;
612 }
613
614 adv_proto = pos;
615 pos += 2 + pos[1];
616
617 /* Query Response Length */
618 if (pos + 2 > data + len) {
619 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
620 return 0;
621 }
622 resp_len = WPA_GET_LE16(pos);
623 pos += 2;
624
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800625 left = data + len - pos;
626 if (resp_len > left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
628 "response from " MACSTR, MAC2STR(sa));
629 return 0;
630 }
631
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800632 if (resp_len < left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800633 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
634 "after Query Response from " MACSTR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800635 left - resp_len, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800636 }
637
638 if (action == WLAN_PA_GAS_COMEBACK_RESP)
639 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
640 frag_id, more_frags, comeback_delay);
641 else
642 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
643 comeback_delay);
644
645 return 0;
646}
647
648
649static void gas_query_timeout(void *eloop_data, void *user_ctx)
650{
651 struct gas_query *gas = eloop_data;
652 struct gas_query_pending *query = user_ctx;
653
Dmitry Shmidt051af732013-10-22 13:52:46 -0700654 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
655 " dialog token %u",
656 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800657 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
658}
659
660
661static int gas_query_dialog_token_available(struct gas_query *gas,
662 const u8 *dst, u8 dialog_token)
663{
664 struct gas_query_pending *q;
665 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
666 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
667 dialog_token == q->dialog_token)
668 return 0;
669 }
670
671 return 1;
672}
673
674
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800675static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
676{
677 struct gas_query_pending *query = work->ctx;
678 struct gas_query *gas = query->gas;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700679 struct wpa_supplicant *wpa_s = gas->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800680
681 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800682 if (work->started) {
683 gas->work = NULL;
684 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
685 return;
686 }
687
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800688 gas_query_free(query, 1);
689 return;
690 }
691
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700692 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
693 wpa_msg(wpa_s, MSG_INFO,
694 "Failed to assign random MAC address for GAS");
695 gas_query_free(query, 1);
696 radio_work_done(work);
697 return;
698 }
699
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800700 gas->work = work;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800701 gas_query_tx_initial_req(gas, query);
702}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800703
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800704
705static void gas_query_tx_initial_req(struct gas_query *gas,
706 struct gas_query_pending *query)
707{
708 if (gas_query_tx(gas, query, query->req,
709 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800710 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
711 MACSTR, MAC2STR(query->addr));
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700712 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800713 return;
714 }
715 gas->current = query;
716
717 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
718 query->dialog_token);
719 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
720 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800721}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800722
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800723
724static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
725{
726 static int next_start = 0;
727 int dialog_token;
728
729 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
730 if (gas_query_dialog_token_available(
731 gas, dst, (next_start + dialog_token) % 256))
732 break;
733 }
734 if (dialog_token == 256)
735 return -1; /* Too many pending queries */
736 dialog_token = (next_start + dialog_token) % 256;
737 next_start = (dialog_token + 1) % 256;
738 return dialog_token;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800739}
740
741
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800742static int gas_query_set_sa(struct gas_query *gas,
743 struct gas_query_pending *query)
744{
745 struct wpa_supplicant *wpa_s = gas->wpa_s;
746 struct os_reltime now;
747
748 if (!wpa_s->conf->gas_rand_mac_addr ||
749 !(wpa_s->current_bss ?
750 (wpa_s->drv_flags &
751 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
752 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
753 /* Use own MAC address as the transmitter address */
754 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
755 return 0;
756 }
757
758 os_get_reltime(&now);
759
760 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
761 gas->last_mac_addr_rand.sec != 0 &&
762 !os_reltime_expired(&now, &gas->last_mac_addr_rand,
763 wpa_s->conf->gas_rand_addr_lifetime)) {
764 wpa_printf(MSG_DEBUG,
765 "GAS: Use the previously selected random transmitter address "
766 MACSTR, MAC2STR(gas->rand_addr));
767 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
768 return 0;
769 }
770
771 if (wpa_s->conf->gas_rand_mac_addr == 1 &&
772 random_mac_addr(gas->rand_addr) < 0) {
773 wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
774 return -1;
775 }
776
777 if (wpa_s->conf->gas_rand_mac_addr == 2 &&
778 random_mac_addr_keep_oui(gas->rand_addr) < 0) {
779 wpa_printf(MSG_ERROR,
780 "GAS: Failed to get random address with same OUI");
781 return -1;
782 }
783
784 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
785 MACSTR, MAC2STR(gas->rand_addr));
786 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
787 os_get_reltime(&gas->last_mac_addr_rand);
788 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
789
790 return 0;
791}
792
793
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800794/**
795 * gas_query_req - Request a GAS query
796 * @gas: GAS query data from gas_query_init()
797 * @dst: Destination MAC address for the query
798 * @freq: Frequency (in MHz) for the channel on which to send the query
Dmitry Shmidt051af732013-10-22 13:52:46 -0700799 * @req: GAS query payload (to be freed by gas_query module in case of success
800 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800801 * @cb: Callback function for reporting GAS query result and response
802 * @ctx: Context pointer to use with the @cb call
803 * Returns: dialog token (>= 0) on success or -1 on failure
804 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800805int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
806 struct wpabuf *req,
807 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
808 enum gas_query_result result,
809 const struct wpabuf *adv_proto,
810 const struct wpabuf *resp, u16 status_code),
811 void *ctx)
812{
813 struct gas_query_pending *query;
814 int dialog_token;
815
816 if (wpabuf_len(req) < 3)
817 return -1;
818
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800819 dialog_token = gas_query_new_dialog_token(gas, dst);
820 if (dialog_token < 0)
821 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822
823 query = os_zalloc(sizeof(*query));
824 if (query == NULL)
825 return -1;
826
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800827 query->gas = gas;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800828 if (gas_query_set_sa(gas, query)) {
829 os_free(query);
830 return -1;
831 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800832 os_memcpy(query->addr, dst, ETH_ALEN);
833 query->dialog_token = dialog_token;
834 query->freq = freq;
835 query->cb = cb;
836 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700837 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800838 dl_list_add(&gas->pending, &query->list);
839
840 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
841
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800842 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
843 " dialog_token=%u freq=%d",
844 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800846 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
847 query) < 0) {
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700848 query->req = NULL; /* caller will free this in error case */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800849 gas_query_free(query, 1);
850 return -1;
851 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852
853 return dialog_token;
854}