blob: a63ee6c1bbd44e02436bca54874dfa6f6f5401cc [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"
20#include "driver_i.h"
21#include "offchannel.h"
22#include "gas_query.h"
23
24
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080025/** GAS query timeout in seconds */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070026#define GAS_QUERY_TIMEOUT_PERIOD 2
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080027
28
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080029/**
30 * struct gas_query_pending - Pending GAS query
31 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080032struct gas_query_pending {
33 struct dl_list list;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080034 struct gas_query *gas;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035 u8 addr[ETH_ALEN];
36 u8 dialog_token;
37 u8 next_frag_id;
38 unsigned int wait_comeback:1;
39 unsigned int offchannel_tx_started:1;
40 int freq;
41 u16 status_code;
Dmitry Shmidt051af732013-10-22 13:52:46 -070042 struct wpabuf *req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043 struct wpabuf *adv_proto;
44 struct wpabuf *resp;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080045 struct os_reltime last_oper;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
47 enum gas_query_result result,
48 const struct wpabuf *adv_proto,
49 const struct wpabuf *resp, u16 status_code);
50 void *ctx;
51};
52
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080053/**
54 * struct gas_query - Internal GAS query data
55 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056struct gas_query {
57 struct wpa_supplicant *wpa_s;
58 struct dl_list pending; /* struct gas_query_pending */
Dmitry Shmidt051af732013-10-22 13:52:46 -070059 struct gas_query_pending *current;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060 struct wpa_radio_work *work;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080061};
62
63
64static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
65static void gas_query_timeout(void *eloop_data, void *user_ctx);
66
67
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080068static int ms_from_time(struct os_reltime *last)
69{
70 struct os_reltime now, res;
71
72 os_get_reltime(&now);
73 os_reltime_sub(&now, last, &res);
74 return res.sec * 1000 + res.usec / 1000;
75}
76
77
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080078/**
79 * gas_query_init - Initialize GAS query component
80 * @wpa_s: Pointer to wpa_supplicant data
81 * Returns: Pointer to GAS query data or %NULL on failure
82 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080083struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
84{
85 struct gas_query *gas;
86
87 gas = os_zalloc(sizeof(*gas));
88 if (gas == NULL)
89 return NULL;
90
91 gas->wpa_s = wpa_s;
92 dl_list_init(&gas->pending);
93
94 return gas;
95}
96
97
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080098static const char * gas_result_txt(enum gas_query_result result)
99{
100 switch (result) {
101 case GAS_QUERY_SUCCESS:
102 return "SUCCESS";
103 case GAS_QUERY_FAILURE:
104 return "FAILURE";
105 case GAS_QUERY_TIMEOUT:
106 return "TIMEOUT";
107 case GAS_QUERY_PEER_ERROR:
108 return "PEER_ERROR";
109 case GAS_QUERY_INTERNAL_ERROR:
110 return "INTERNAL_ERROR";
111 case GAS_QUERY_CANCELLED:
112 return "CANCELLED";
113 case GAS_QUERY_DELETED_AT_DEINIT:
114 return "DELETED_AT_DEINIT";
115 }
116
117 return "N/A";
118}
119
120
121static void gas_query_free(struct gas_query_pending *query, int del_list)
122{
123 struct gas_query *gas = query->gas;
124
125 if (del_list)
126 dl_list_del(&query->list);
127
128 if (gas->work && gas->work->ctx == query) {
129 radio_work_done(gas->work);
130 gas->work = NULL;
131 }
132
133 wpabuf_free(query->req);
134 wpabuf_free(query->adv_proto);
135 wpabuf_free(query->resp);
136 os_free(query);
137}
138
139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800140static void gas_query_done(struct gas_query *gas,
141 struct gas_query_pending *query,
142 enum gas_query_result result)
143{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800144 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
145 " dialog_token=%u freq=%d status_code=%u result=%s",
146 MAC2STR(query->addr), query->dialog_token, query->freq,
147 query->status_code, gas_result_txt(result));
Dmitry Shmidt051af732013-10-22 13:52:46 -0700148 if (gas->current == query)
149 gas->current = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800150 if (query->offchannel_tx_started)
151 offchannel_send_action_done(gas->wpa_s);
152 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
153 eloop_cancel_timeout(gas_query_timeout, gas, query);
154 dl_list_del(&query->list);
155 query->cb(query->ctx, query->addr, query->dialog_token, result,
156 query->adv_proto, query->resp, query->status_code);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800157 gas_query_free(query, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800158}
159
160
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800161/**
162 * gas_query_deinit - Deinitialize GAS query component
163 * @gas: GAS query data from gas_query_init()
164 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165void gas_query_deinit(struct gas_query *gas)
166{
167 struct gas_query_pending *query, *next;
168
169 if (gas == NULL)
170 return;
171
172 dl_list_for_each_safe(query, next, &gas->pending,
173 struct gas_query_pending, list)
174 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
175
176 os_free(gas);
177}
178
179
180static struct gas_query_pending *
181gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
182{
183 struct gas_query_pending *q;
184 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
185 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
186 q->dialog_token == dialog_token)
187 return q;
188 }
189 return NULL;
190}
191
192
193static int gas_query_append(struct gas_query_pending *query, const u8 *data,
194 size_t len)
195{
196 if (wpabuf_resize(&query->resp, len) < 0) {
197 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
198 return -1;
199 }
200 wpabuf_put_data(query->resp, data, len);
201 return 0;
202}
203
204
Dmitry Shmidt051af732013-10-22 13:52:46 -0700205static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
206 unsigned int freq, const u8 *dst,
207 const u8 *src, const u8 *bssid,
208 const u8 *data, size_t data_len,
209 enum offchannel_send_action_result result)
210{
211 struct gas_query_pending *query;
212 struct gas_query *gas = wpa_s->gas;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800213 int dur;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700214
215 if (gas->current == NULL) {
216 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
217 MACSTR " result=%d - no query in progress",
218 freq, MAC2STR(dst), result);
219 return;
220 }
221
222 query = gas->current;
223
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800224 dur = ms_from_time(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700225 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800226 " result=%d query=%p dialog_token=%u dur=%d ms",
227 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700228 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
229 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
230 return;
231 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800232 os_get_reltime(&query->last_oper);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700233
234 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
235 eloop_cancel_timeout(gas_query_timeout, gas, query);
236 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
237 gas_query_timeout, gas, query);
238 }
239 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
240 eloop_cancel_timeout(gas_query_timeout, gas, query);
241 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
242 }
243}
244
245
Dmitry Shmidt18463232014-01-24 12:29:41 -0800246static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
247{
248 if (wpa_s->current_ssid == NULL ||
249 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
250 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
251 return 0;
252 return wpa_sm_pmf_enabled(wpa_s->wpa);
253}
254
255
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800256static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
257 struct wpabuf *req)
258{
Dmitry Shmidt18463232014-01-24 12:29:41 -0800259 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
260
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800261 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
Dmitry Shmidt18463232014-01-24 12:29:41 -0800262 "freq=%d prot=%d", MAC2STR(query->addr),
263 (unsigned int) wpabuf_len(req), query->freq, prot);
264 if (prot) {
265 u8 *categ = wpabuf_mhead_u8(req);
266 *categ = WLAN_ACTION_PROTECTED_DUAL;
267 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800268 os_get_reltime(&query->last_oper);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800269 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
270 gas->wpa_s->own_addr, query->addr,
271 wpabuf_head(req), wpabuf_len(req), 1000,
Dmitry Shmidt051af732013-10-22 13:52:46 -0700272 gas_query_tx_status, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273 if (res == 0)
274 query->offchannel_tx_started = 1;
275 return res;
276}
277
278
279static void gas_query_tx_comeback_req(struct gas_query *gas,
280 struct gas_query_pending *query)
281{
282 struct wpabuf *req;
283
284 req = gas_build_comeback_req(query->dialog_token);
285 if (req == NULL) {
286 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
287 return;
288 }
289
290 if (gas_query_tx(gas, query, req) < 0) {
291 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
292 MACSTR, MAC2STR(query->addr));
293 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
294 }
295
296 wpabuf_free(req);
297}
298
299
300static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
301{
302 struct gas_query *gas = eloop_data;
303 struct gas_query_pending *query = user_ctx;
304
305 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
306 MAC2STR(query->addr));
307 gas_query_tx_comeback_req(gas, query);
308}
309
310
311static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
312 struct gas_query_pending *query,
313 u16 comeback_delay)
314{
315 unsigned int secs, usecs;
316
317 secs = (comeback_delay * 1024) / 1000000;
318 usecs = comeback_delay * 1024 - secs * 1000000;
319 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
320 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
321 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
322 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
323 gas, query);
324}
325
326
327static void gas_query_rx_initial(struct gas_query *gas,
328 struct gas_query_pending *query,
329 const u8 *adv_proto, const u8 *resp,
330 size_t len, u16 comeback_delay)
331{
332 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
333 MACSTR " (dialog_token=%u comeback_delay=%u)",
334 MAC2STR(query->addr), query->dialog_token, comeback_delay);
335
336 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
337 if (query->adv_proto == NULL) {
338 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
339 return;
340 }
341
342 if (comeback_delay) {
343 query->wait_comeback = 1;
344 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
345 return;
346 }
347
348 /* Query was completed without comeback mechanism */
349 if (gas_query_append(query, resp, len) < 0) {
350 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
351 return;
352 }
353
354 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
355}
356
357
358static void gas_query_rx_comeback(struct gas_query *gas,
359 struct gas_query_pending *query,
360 const u8 *adv_proto, const u8 *resp,
361 size_t len, u8 frag_id, u8 more_frags,
362 u16 comeback_delay)
363{
364 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
365 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
366 "comeback_delay=%u)",
367 MAC2STR(query->addr), query->dialog_token, frag_id,
368 more_frags, comeback_delay);
369
370 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
371 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
372 wpabuf_len(query->adv_proto)) != 0) {
373 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
374 "between initial and comeback response from "
375 MACSTR, MAC2STR(query->addr));
376 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
377 return;
378 }
379
380 if (comeback_delay) {
381 if (frag_id) {
382 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
383 "with non-zero frag_id and comeback_delay "
384 "from " MACSTR, MAC2STR(query->addr));
385 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
386 return;
387 }
388 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
389 return;
390 }
391
392 if (frag_id != query->next_frag_id) {
393 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
394 "from " MACSTR, MAC2STR(query->addr));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700395 if (frag_id + 1 == query->next_frag_id) {
396 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
397 "retry of previous fragment");
398 return;
399 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800400 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
401 return;
402 }
403 query->next_frag_id++;
404
405 if (gas_query_append(query, resp, len) < 0) {
406 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
407 return;
408 }
409
410 if (more_frags) {
411 gas_query_tx_comeback_req(gas, query);
412 return;
413 }
414
415 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
416}
417
418
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800419/**
Dmitry Shmidt18463232014-01-24 12:29:41 -0800420 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800421 * @gas: GAS query data from gas_query_init()
422 * @da: Destination MAC address of the Action frame
423 * @sa: Source MAC address of the Action frame
424 * @bssid: BSSID of the Action frame
Dmitry Shmidt18463232014-01-24 12:29:41 -0800425 * @categ: Category of the Action frame
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800426 * @data: Payload of the Action frame
427 * @len: Length of @data
428 * @freq: Frequency (in MHz) on which the frame was received
429 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
430 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
Dmitry Shmidt18463232014-01-24 12:29:41 -0800432 const u8 *bssid, u8 categ, const u8 *data, size_t len,
433 int freq)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434{
435 struct gas_query_pending *query;
436 u8 action, dialog_token, frag_id = 0, more_frags = 0;
437 u16 comeback_delay, resp_len;
438 const u8 *pos, *adv_proto;
Dmitry Shmidt18463232014-01-24 12:29:41 -0800439 int prot, pmf;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800440
441 if (gas == NULL || len < 4)
442 return -1;
443
Dmitry Shmidt18463232014-01-24 12:29:41 -0800444 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
445 pmf = pmf_in_use(gas->wpa_s, bssid);
446 if (prot && !pmf) {
447 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
448 return 0;
449 }
450 if (!prot && pmf) {
451 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
452 return 0;
453 }
454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800455 pos = data;
456 action = *pos++;
457 dialog_token = *pos++;
458
459 if (action != WLAN_PA_GAS_INITIAL_RESP &&
460 action != WLAN_PA_GAS_COMEBACK_RESP)
461 return -1; /* Not a GAS response */
462
463 query = gas_query_get_pending(gas, sa, dialog_token);
464 if (query == NULL) {
465 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
466 " dialog token %u", MAC2STR(sa), dialog_token);
467 return -1;
468 }
469
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800470 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
471 ms_from_time(&query->last_oper), MAC2STR(sa));
472
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800473 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
474 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
475 MACSTR " dialog token %u when waiting for comeback "
476 "response", MAC2STR(sa), dialog_token);
477 return 0;
478 }
479
480 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
481 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
482 MACSTR " dialog token %u when waiting for initial "
483 "response", MAC2STR(sa), dialog_token);
484 return 0;
485 }
486
487 query->status_code = WPA_GET_LE16(pos);
488 pos += 2;
489
490 if (query->status_code != WLAN_STATUS_SUCCESS) {
491 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
492 "%u failed - status code %u",
493 MAC2STR(sa), dialog_token, query->status_code);
494 gas_query_done(gas, query, GAS_QUERY_FAILURE);
495 return 0;
496 }
497
498 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
499 if (pos + 1 > data + len)
500 return 0;
501 frag_id = *pos & 0x7f;
502 more_frags = (*pos & 0x80) >> 7;
503 pos++;
504 }
505
506 /* Comeback Delay */
507 if (pos + 2 > data + len)
508 return 0;
509 comeback_delay = WPA_GET_LE16(pos);
510 pos += 2;
511
512 /* Advertisement Protocol element */
513 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
514 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
515 "Protocol element in the response from " MACSTR,
516 MAC2STR(sa));
517 return 0;
518 }
519
520 if (*pos != WLAN_EID_ADV_PROTO) {
521 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
522 "Protocol element ID %u in response from " MACSTR,
523 *pos, MAC2STR(sa));
524 return 0;
525 }
526
527 adv_proto = pos;
528 pos += 2 + pos[1];
529
530 /* Query Response Length */
531 if (pos + 2 > data + len) {
532 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
533 return 0;
534 }
535 resp_len = WPA_GET_LE16(pos);
536 pos += 2;
537
538 if (pos + resp_len > data + len) {
539 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
540 "response from " MACSTR, MAC2STR(sa));
541 return 0;
542 }
543
544 if (pos + resp_len < data + len) {
545 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
546 "after Query Response from " MACSTR,
547 (unsigned int) (data + len - pos - resp_len),
548 MAC2STR(sa));
549 }
550
551 if (action == WLAN_PA_GAS_COMEBACK_RESP)
552 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
553 frag_id, more_frags, comeback_delay);
554 else
555 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
556 comeback_delay);
557
558 return 0;
559}
560
561
562static void gas_query_timeout(void *eloop_data, void *user_ctx)
563{
564 struct gas_query *gas = eloop_data;
565 struct gas_query_pending *query = user_ctx;
566
Dmitry Shmidt051af732013-10-22 13:52:46 -0700567 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
568 " dialog token %u",
569 MAC2STR(query->addr), query->dialog_token);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800570 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
571}
572
573
574static int gas_query_dialog_token_available(struct gas_query *gas,
575 const u8 *dst, u8 dialog_token)
576{
577 struct gas_query_pending *q;
578 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
579 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
580 dialog_token == q->dialog_token)
581 return 0;
582 }
583
584 return 1;
585}
586
587
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800588static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
589{
590 struct gas_query_pending *query = work->ctx;
591 struct gas_query *gas = query->gas;
592
593 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800594 if (work->started) {
595 gas->work = NULL;
596 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
597 return;
598 }
599
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800600 gas_query_free(query, 1);
601 return;
602 }
603
604 gas->work = work;
605
606 if (gas_query_tx(gas, query, query->req) < 0) {
607 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
608 MACSTR, MAC2STR(query->addr));
609 gas_query_free(query, 1);
610 return;
611 }
612 gas->current = query;
613
614 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
615 query->dialog_token);
616 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
617 gas_query_timeout, gas, query);
618
619}
620
621
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800622/**
623 * gas_query_req - Request a GAS query
624 * @gas: GAS query data from gas_query_init()
625 * @dst: Destination MAC address for the query
626 * @freq: Frequency (in MHz) for the channel on which to send the query
Dmitry Shmidt051af732013-10-22 13:52:46 -0700627 * @req: GAS query payload (to be freed by gas_query module in case of success
628 * return)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800629 * @cb: Callback function for reporting GAS query result and response
630 * @ctx: Context pointer to use with the @cb call
631 * Returns: dialog token (>= 0) on success or -1 on failure
632 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800633int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
634 struct wpabuf *req,
635 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
636 enum gas_query_result result,
637 const struct wpabuf *adv_proto,
638 const struct wpabuf *resp, u16 status_code),
639 void *ctx)
640{
641 struct gas_query_pending *query;
642 int dialog_token;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700643 static int next_start = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800644
645 if (wpabuf_len(req) < 3)
646 return -1;
647
648 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700649 if (gas_query_dialog_token_available(
650 gas, dst, (next_start + dialog_token) % 256))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800651 break;
652 }
653 if (dialog_token == 256)
654 return -1; /* Too many pending queries */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700655 dialog_token = (next_start + dialog_token) % 256;
656 next_start = (dialog_token + 1) % 256;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800657
658 query = os_zalloc(sizeof(*query));
659 if (query == NULL)
660 return -1;
661
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800662 query->gas = gas;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800663 os_memcpy(query->addr, dst, ETH_ALEN);
664 query->dialog_token = dialog_token;
665 query->freq = freq;
666 query->cb = cb;
667 query->ctx = ctx;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700668 query->req = req;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800669 dl_list_add(&gas->pending, &query->list);
670
671 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
672
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800673 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
674 " dialog_token=%u freq=%d",
675 MAC2STR(query->addr), query->dialog_token, query->freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800676
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800677 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
678 query) < 0) {
679 gas_query_free(query, 1);
680 return -1;
681 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682
683 return dialog_token;
684}
685
686
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800687/**
688 * gas_query_cancel - Cancel a pending GAS query
689 * @gas: GAS query data from gas_query_init()
690 * @dst: Destination MAC address for the query
691 * @dialog_token: Dialog token from gas_query_req()
692 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800693void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
694{
695 struct gas_query_pending *query;
696
697 query = gas_query_get_pending(gas, dst, dialog_token);
698 if (query)
699 gas_query_done(gas, query, GAS_QUERY_CANCELLED);
700
701}