blob: 691de0345d136c993c54d5ae24ecd55fd732c8ba [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;
56};
57
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080058/**
59 * struct gas_query - Internal GAS query data
60 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080061struct gas_query {
62 struct wpa_supplicant *wpa_s;
63 struct dl_list pending; /* struct gas_query_pending */
Dmitry Shmidt051af732013-10-22 13:52:46 -070064 struct gas_query_pending *current;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080065 struct wpa_radio_work *work;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066};
67
68
69static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
70static void gas_query_timeout(void *eloop_data, void *user_ctx);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -080071static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
72static void gas_query_tx_initial_req(struct gas_query *gas,
73 struct gas_query_pending *query);
74static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080075
76
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080077static int ms_from_time(struct os_reltime *last)
78{
79 struct os_reltime now, res;
80
81 os_get_reltime(&now);
82 os_reltime_sub(&now, last, &res);
83 return res.sec * 1000 + res.usec / 1000;
84}
85
86
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080087/**
88 * gas_query_init - Initialize GAS query component
89 * @wpa_s: Pointer to wpa_supplicant data
90 * Returns: Pointer to GAS query data or %NULL on failure
91 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
93{
94 struct gas_query *gas;
95
96 gas = os_zalloc(sizeof(*gas));
97 if (gas == NULL)
98 return NULL;
99
100 gas->wpa_s = wpa_s;
101 dl_list_init(&gas->pending);
102
103 return gas;
104}
105
106
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800107static const char * gas_result_txt(enum gas_query_result result)
108{
109 switch (result) {
110 case GAS_QUERY_SUCCESS:
111 return "SUCCESS";
112 case GAS_QUERY_FAILURE:
113 return "FAILURE";
114 case GAS_QUERY_TIMEOUT:
115 return "TIMEOUT";
116 case GAS_QUERY_PEER_ERROR:
117 return "PEER_ERROR";
118 case GAS_QUERY_INTERNAL_ERROR:
119 return "INTERNAL_ERROR";
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800120 case GAS_QUERY_DELETED_AT_DEINIT:
121 return "DELETED_AT_DEINIT";
122 }
123
124 return "N/A";
125}
126
127
128static void gas_query_free(struct gas_query_pending *query, int del_list)
129{
130 struct gas_query *gas = query->gas;
131
132 if (del_list)
133 dl_list_del(&query->list);
134
135 if (gas->work && gas->work->ctx == query) {
136 radio_work_done(gas->work);
137 gas->work = NULL;
138 }
139
140 wpabuf_free(query->req);
141 wpabuf_free(query->adv_proto);
142 wpabuf_free(query->resp);
143 os_free(query);
144}
145
146
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800147static void gas_query_done(struct gas_query *gas,
148 struct gas_query_pending *query,
149 enum gas_query_result result)
150{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800151 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
152 " dialog_token=%u freq=%d status_code=%u result=%s",
153 MAC2STR(query->addr), query->dialog_token, query->freq,
154 query->status_code, gas_result_txt(result));
Dmitry Shmidt051af732013-10-22 13:52:46 -0700155 if (gas->current == query)
156 gas->current = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800157 if (query->offchannel_tx_started)
158 offchannel_send_action_done(gas->wpa_s);
159 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
160 eloop_cancel_timeout(gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800161 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800162 dl_list_del(&query->list);
163 query->cb(query->ctx, query->addr, query->dialog_token, result,
164 query->adv_proto, query->resp, query->status_code);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800165 gas_query_free(query, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166}
167
168
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800169/**
170 * gas_query_deinit - Deinitialize GAS query component
171 * @gas: GAS query data from gas_query_init()
172 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800173void gas_query_deinit(struct gas_query *gas)
174{
175 struct gas_query_pending *query, *next;
176
177 if (gas == NULL)
178 return;
179
180 dl_list_for_each_safe(query, next, &gas->pending,
181 struct gas_query_pending, list)
182 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
183
184 os_free(gas);
185}
186
187
188static struct gas_query_pending *
189gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
190{
191 struct gas_query_pending *q;
192 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
193 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
194 q->dialog_token == dialog_token)
195 return q;
196 }
197 return NULL;
198}
199
200
201static int gas_query_append(struct gas_query_pending *query, const u8 *data,
202 size_t len)
203{
204 if (wpabuf_resize(&query->resp, len) < 0) {
205 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
206 return -1;
207 }
208 wpabuf_put_data(query->resp, data, len);
209 return 0;
210}
211
212
Dmitry Shmidt051af732013-10-22 13:52:46 -0700213static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
214 unsigned int freq, const u8 *dst,
215 const u8 *src, const u8 *bssid,
216 const u8 *data, size_t data_len,
217 enum offchannel_send_action_result result)
218{
219 struct gas_query_pending *query;
220 struct gas_query *gas = wpa_s->gas;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800221 int dur;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700222
223 if (gas->current == NULL) {
224 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
225 MACSTR " result=%d - no query in progress",
226 freq, MAC2STR(dst), result);
227 return;
228 }
229
230 query = gas->current;
231
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800232 dur = ms_from_time(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700233 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800234 " result=%d query=%p dialog_token=%u dur=%d ms",
235 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700236 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
237 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
238 return;
239 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800240 os_get_reltime(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700241
242 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
243 eloop_cancel_timeout(gas_query_timeout, gas, query);
244 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
245 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800246 if (query->wait_comeback && !query->retry) {
247 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
248 gas, query);
249 eloop_register_timeout(
250 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
251 gas_query_rx_comeback_timeout, gas, query);
252 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700253 }
254 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
255 eloop_cancel_timeout(gas_query_timeout, gas, query);
256 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
257 }
258}
259
260
Dmitry Shmidt18463232014-01-24 12:29:41 -0800261static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
262{
263 if (wpa_s->current_ssid == NULL ||
264 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
265 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
266 return 0;
267 return wpa_sm_pmf_enabled(wpa_s->wpa);
268}
269
270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800271static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800272 struct wpabuf *req, unsigned int wait_time)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800274 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700275 const u8 *bssid;
276 const u8 wildcard_bssid[ETH_ALEN] = {
277 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
278 };
Dmitry Shmidt18463232014-01-24 12:29:41 -0800279
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800280 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidt18463232014-01-24 12:29:41 -0800281 "freq=%d prot=%d", MAC2STR(query->addr),
282 (unsigned int) wpabuf_len(req), query->freq, prot);
283 if (prot) {
284 u8 *categ = wpabuf_mhead_u8(req);
285 *categ = WLAN_ACTION_PROTECTED_DUAL;
286 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800287 os_get_reltime(&query->last_oper);
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700288 if (gas->wpa_s->max_remain_on_chan &&
289 wait_time > gas->wpa_s->max_remain_on_chan)
290 wait_time = gas->wpa_s->max_remain_on_chan;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700291 if (!gas->wpa_s->conf->gas_address3 ||
292 (gas->wpa_s->current_ssid &&
293 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
294 os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0))
295 bssid = query->addr;
296 else
297 bssid = wildcard_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800298 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700299 gas->wpa_s->own_addr, bssid,
Dmitry Shmidt623d63a2014-06-13 11:05:14 -0700300 wpabuf_head(req), wpabuf_len(req),
301 wait_time, gas_query_tx_status, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800302 if (res == 0)
303 query->offchannel_tx_started = 1;
304 return res;
305}
306
307
308static void gas_query_tx_comeback_req(struct gas_query *gas,
309 struct gas_query_pending *query)
310{
311 struct wpabuf *req;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800312 unsigned int wait_time;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313
314 req = gas_build_comeback_req(query->dialog_token);
315 if (req == NULL) {
316 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
317 return;
318 }
319
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800320 wait_time = (query->retry || !query->offchannel_tx_started) ?
321 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
322
323 if (gas_query_tx(gas, query, req, wait_time) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
325 MACSTR, MAC2STR(query->addr));
326 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
327 }
328
329 wpabuf_free(req);
330}
331
332
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800333static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
334{
335 struct gas_query *gas = eloop_data;
336 struct gas_query_pending *query = user_ctx;
337 int dialog_token;
338
339 wpa_printf(MSG_DEBUG,
340 "GAS: No response to comeback request received (retry=%u)",
341 query->retry);
342 if (gas->current != query || query->retry)
343 return;
344 dialog_token = gas_query_new_dialog_token(gas, query->addr);
345 if (dialog_token < 0)
346 return;
347 wpa_printf(MSG_DEBUG,
348 "GAS: Retry GAS query due to comeback response timeout");
349 query->retry = 1;
350 query->dialog_token = dialog_token;
351 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
352 query->wait_comeback = 0;
353 query->next_frag_id = 0;
354 wpabuf_free(query->adv_proto);
355 query->adv_proto = NULL;
356 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
357 eloop_cancel_timeout(gas_query_timeout, gas, query);
358 gas_query_tx_initial_req(gas, query);
359}
360
361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800362static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
363{
364 struct gas_query *gas = eloop_data;
365 struct gas_query_pending *query = user_ctx;
366
367 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
368 MAC2STR(query->addr));
369 gas_query_tx_comeback_req(gas, query);
370}
371
372
373static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
374 struct gas_query_pending *query,
375 u16 comeback_delay)
376{
377 unsigned int secs, usecs;
378
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800379 if (comeback_delay > 1 && query->offchannel_tx_started) {
380 offchannel_send_action_done(gas->wpa_s);
381 query->offchannel_tx_started = 0;
382 }
383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800384 secs = (comeback_delay * 1024) / 1000000;
385 usecs = comeback_delay * 1024 - secs * 1000000;
386 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
387 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
388 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
389 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
390 gas, query);
391}
392
393
394static void gas_query_rx_initial(struct gas_query *gas,
395 struct gas_query_pending *query,
396 const u8 *adv_proto, const u8 *resp,
397 size_t len, u16 comeback_delay)
398{
399 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
400 MACSTR " (dialog_token=%u comeback_delay=%u)",
401 MAC2STR(query->addr), query->dialog_token, comeback_delay);
402
403 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
404 if (query->adv_proto == NULL) {
405 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
406 return;
407 }
408
409 if (comeback_delay) {
410 query->wait_comeback = 1;
411 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
412 return;
413 }
414
415 /* Query was completed without comeback mechanism */
416 if (gas_query_append(query, resp, len) < 0) {
417 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
418 return;
419 }
420
421 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
422}
423
424
425static void gas_query_rx_comeback(struct gas_query *gas,
426 struct gas_query_pending *query,
427 const u8 *adv_proto, const u8 *resp,
428 size_t len, u8 frag_id, u8 more_frags,
429 u16 comeback_delay)
430{
431 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
432 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
433 "comeback_delay=%u)",
434 MAC2STR(query->addr), query->dialog_token, frag_id,
435 more_frags, comeback_delay);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800436 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800437
438 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
439 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
440 wpabuf_len(query->adv_proto)) != 0) {
441 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
442 "between initial and comeback response from "
443 MACSTR, MAC2STR(query->addr));
444 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
445 return;
446 }
447
448 if (comeback_delay) {
449 if (frag_id) {
450 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
451 "with non-zero frag_id and comeback_delay "
452 "from " MACSTR, MAC2STR(query->addr));
453 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
454 return;
455 }
456 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
457 return;
458 }
459
460 if (frag_id != query->next_frag_id) {
461 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
462 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700463 if (frag_id + 1 == query->next_frag_id) {
464 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
465 "retry of previous fragment");
466 return;
467 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800468 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
469 return;
470 }
471 query->next_frag_id++;
472
473 if (gas_query_append(query, resp, len) < 0) {
474 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
475 return;
476 }
477
478 if (more_frags) {
479 gas_query_tx_comeback_req(gas, query);
480 return;
481 }
482
483 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
484}
485
486
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800487/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800488 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800489 * @gas: GAS query data from gas_query_init()
490 * @da: Destination MAC address of the Action frame
491 * @sa: Source MAC address of the Action frame
492 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800493 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800494 * @data: Payload of the Action frame
495 * @len: Length of @data
496 * @freq: Frequency (in MHz) on which the frame was received
497 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
498 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800499int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800500 const u8 *bssid, u8 categ, const u8 *data, size_t len,
501 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800502{
503 struct gas_query_pending *query;
504 u8 action, dialog_token, frag_id = 0, more_frags = 0;
505 u16 comeback_delay, resp_len;
506 const u8 *pos, *adv_proto;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800507 int prot, pmf;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800508 unsigned int left;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800509
510 if (gas == NULL || len < 4)
511 return -1;
512
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700513 pos = data;
514 action = *pos++;
515 dialog_token = *pos++;
516
517 if (action != WLAN_PA_GAS_INITIAL_RESP &&
518 action != WLAN_PA_GAS_COMEBACK_RESP)
519 return -1; /* Not a GAS response */
520
Dmitry Shmidt18463232014-01-24 12:29:41 -0800521 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800522 pmf = pmf_in_use(gas->wpa_s, sa);
Dmitry Shmidt18463232014-01-24 12:29:41 -0800523 if (prot && !pmf) {
524 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
525 return 0;
526 }
527 if (!prot && pmf) {
528 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
529 return 0;
530 }
531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800532 query = gas_query_get_pending(gas, sa, dialog_token);
533 if (query == NULL) {
534 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
535 " dialog token %u", MAC2STR(sa), dialog_token);
536 return -1;
537 }
538
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800539 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
540 ms_from_time(&query->last_oper), MAC2STR(sa));
541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800542 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
543 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
544 MACSTR " dialog token %u when waiting for comeback "
545 "response", MAC2STR(sa), dialog_token);
546 return 0;
547 }
548
549 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
550 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
551 MACSTR " dialog token %u when waiting for initial "
552 "response", MAC2STR(sa), dialog_token);
553 return 0;
554 }
555
556 query->status_code = WPA_GET_LE16(pos);
557 pos += 2;
558
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800559 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
560 action == WLAN_PA_GAS_COMEBACK_RESP) {
561 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
562 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800563 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
564 "%u failed - status code %u",
565 MAC2STR(sa), dialog_token, query->status_code);
566 gas_query_done(gas, query, GAS_QUERY_FAILURE);
567 return 0;
568 }
569
570 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
571 if (pos + 1 > data + len)
572 return 0;
573 frag_id = *pos & 0x7f;
574 more_frags = (*pos & 0x80) >> 7;
575 pos++;
576 }
577
578 /* Comeback Delay */
579 if (pos + 2 > data + len)
580 return 0;
581 comeback_delay = WPA_GET_LE16(pos);
582 pos += 2;
583
584 /* Advertisement Protocol element */
585 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
586 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
587 "Protocol element in the response from " MACSTR,
588 MAC2STR(sa));
589 return 0;
590 }
591
592 if (*pos != WLAN_EID_ADV_PROTO) {
593 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
594 "Protocol element ID %u in response from " MACSTR,
595 *pos, MAC2STR(sa));
596 return 0;
597 }
598
599 adv_proto = pos;
600 pos += 2 + pos[1];
601
602 /* Query Response Length */
603 if (pos + 2 > data + len) {
604 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
605 return 0;
606 }
607 resp_len = WPA_GET_LE16(pos);
608 pos += 2;
609
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800610 left = data + len - pos;
611 if (resp_len > left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800612 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
613 "response from " MACSTR, MAC2STR(sa));
614 return 0;
615 }
616
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800617 if (resp_len < left) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800618 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
619 "after Query Response from " MACSTR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800620 left - resp_len, MAC2STR(sa));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800621 }
622
623 if (action == WLAN_PA_GAS_COMEBACK_RESP)
624 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
625 frag_id, more_frags, comeback_delay);
626 else
627 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
628 comeback_delay);
629
630 return 0;
631}
632
633
634static void gas_query_timeout(void *eloop_data, void *user_ctx)
635{
636 struct gas_query *gas = eloop_data;
637 struct gas_query_pending *query = user_ctx;
638
Dmitry Shmidt051af732013-10-22 13:52:46 -0700639 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
640 " dialog token %u",
641 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800642 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
643}
644
645
646static int gas_query_dialog_token_available(struct gas_query *gas,
647 const u8 *dst, u8 dialog_token)
648{
649 struct gas_query_pending *q;
650 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
651 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
652 dialog_token == q->dialog_token)
653 return 0;
654 }
655
656 return 1;
657}
658
659
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800660static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
661{
662 struct gas_query_pending *query = work->ctx;
663 struct gas_query *gas = query->gas;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700664 struct wpa_supplicant *wpa_s = gas->wpa_s;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800665
666 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800667 if (work->started) {
668 gas->work = NULL;
669 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
670 return;
671 }
672
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800673 gas_query_free(query, 1);
674 return;
675 }
676
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700677 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
678 wpa_msg(wpa_s, MSG_INFO,
679 "Failed to assign random MAC address for GAS");
680 gas_query_free(query, 1);
681 radio_work_done(work);
682 return;
683 }
684
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800685 gas->work = work;
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800686 gas_query_tx_initial_req(gas, query);
687}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800688
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800689
690static void gas_query_tx_initial_req(struct gas_query *gas,
691 struct gas_query_pending *query)
692{
693 if (gas_query_tx(gas, query, query->req,
694 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800695 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
696 MACSTR, MAC2STR(query->addr));
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700697 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800698 return;
699 }
700 gas->current = query;
701
702 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
703 query->dialog_token);
704 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
705 gas_query_timeout, gas, query);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800706}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800707
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800708
709static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
710{
711 static int next_start = 0;
712 int dialog_token;
713
714 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
715 if (gas_query_dialog_token_available(
716 gas, dst, (next_start + dialog_token) % 256))
717 break;
718 }
719 if (dialog_token == 256)
720 return -1; /* Too many pending queries */
721 dialog_token = (next_start + dialog_token) % 256;
722 next_start = (dialog_token + 1) % 256;
723 return dialog_token;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800724}
725
726
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800727/**
728 * gas_query_req - Request a GAS query
729 * @gas: GAS query data from gas_query_init()
730 * @dst: Destination MAC address for the query
731 * @freq: Frequency (in MHz) for the channel on which to send the query
Dmitry Shmidt051af732013-10-22 13:52:46 -0700732 * @req: GAS query payload (to be freed by gas_query module in case of success
733 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800734 * @cb: Callback function for reporting GAS query result and response
735 * @ctx: Context pointer to use with the @cb call
736 * Returns: dialog token (>= 0) on success or -1 on failure
737 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
739 struct wpabuf *req,
740 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
741 enum gas_query_result result,
742 const struct wpabuf *adv_proto,
743 const struct wpabuf *resp, u16 status_code),
744 void *ctx)
745{
746 struct gas_query_pending *query;
747 int dialog_token;
748
749 if (wpabuf_len(req) < 3)
750 return -1;
751
Dmitry Shmidt7d56b752015-12-22 10:59:44 -0800752 dialog_token = gas_query_new_dialog_token(gas, dst);
753 if (dialog_token < 0)
754 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800755
756 query = os_zalloc(sizeof(*query));
757 if (query == NULL)
758 return -1;
759
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800760 query->gas = gas;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800761 os_memcpy(query->addr, dst, ETH_ALEN);
762 query->dialog_token = dialog_token;
763 query->freq = freq;
764 query->cb = cb;
765 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700766 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767 dl_list_add(&gas->pending, &query->list);
768
769 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
770
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800771 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
772 " dialog_token=%u freq=%d",
773 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800774
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800775 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
776 query) < 0) {
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700777 query->req = NULL; /* caller will free this in error case */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800778 gas_query_free(query, 1);
779 return -1;
780 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800781
782 return dialog_token;
783}