]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/gas_query.c
GAS: Check protected/unprotected drop after action code check
[thirdparty/hostap.git] / wpa_supplicant / gas_query.c
1 /*
2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
5 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6 *
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
9 */
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"
17 #include "common/wpa_ctrl.h"
18 #include "rsn_supp/wpa.h"
19 #include "wpa_supplicant_i.h"
20 #include "config.h"
21 #include "driver_i.h"
22 #include "offchannel.h"
23 #include "gas_query.h"
24
25
26 /** GAS query timeout in seconds */
27 #define GAS_QUERY_TIMEOUT_PERIOD 2
28
29 /* GAS query wait-time / duration in ms */
30 #define GAS_QUERY_WAIT_TIME_INITIAL 1000
31 #define GAS_QUERY_WAIT_TIME_COMEBACK 150
32
33 /**
34 * struct gas_query_pending - Pending GAS query
35 */
36 struct gas_query_pending {
37 struct dl_list list;
38 struct gas_query *gas;
39 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;
44 unsigned int retry:1;
45 int freq;
46 u16 status_code;
47 struct wpabuf *req;
48 struct wpabuf *adv_proto;
49 struct wpabuf *resp;
50 struct os_reltime last_oper;
51 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
58 /**
59 * struct gas_query - Internal GAS query data
60 */
61 struct gas_query {
62 struct wpa_supplicant *wpa_s;
63 struct dl_list pending; /* struct gas_query_pending */
64 struct gas_query_pending *current;
65 struct wpa_radio_work *work;
66 };
67
68
69 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
70 static void gas_query_timeout(void *eloop_data, void *user_ctx);
71 static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
72 static void gas_query_tx_initial_req(struct gas_query *gas,
73 struct gas_query_pending *query);
74 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
75
76
77 static 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
87 /**
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 */
92 struct 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
107 static 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";
120 case GAS_QUERY_CANCELLED:
121 return "CANCELLED";
122 case GAS_QUERY_DELETED_AT_DEINIT:
123 return "DELETED_AT_DEINIT";
124 }
125
126 return "N/A";
127 }
128
129
130 static void gas_query_free(struct gas_query_pending *query, int del_list)
131 {
132 struct gas_query *gas = query->gas;
133
134 if (del_list)
135 dl_list_del(&query->list);
136
137 if (gas->work && gas->work->ctx == query) {
138 radio_work_done(gas->work);
139 gas->work = NULL;
140 }
141
142 wpabuf_free(query->req);
143 wpabuf_free(query->adv_proto);
144 wpabuf_free(query->resp);
145 os_free(query);
146 }
147
148
149 static void gas_query_done(struct gas_query *gas,
150 struct gas_query_pending *query,
151 enum gas_query_result result)
152 {
153 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
154 " dialog_token=%u freq=%d status_code=%u result=%s",
155 MAC2STR(query->addr), query->dialog_token, query->freq,
156 query->status_code, gas_result_txt(result));
157 if (gas->current == query)
158 gas->current = NULL;
159 if (query->offchannel_tx_started)
160 offchannel_send_action_done(gas->wpa_s);
161 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
162 eloop_cancel_timeout(gas_query_timeout, gas, query);
163 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
164 dl_list_del(&query->list);
165 query->cb(query->ctx, query->addr, query->dialog_token, result,
166 query->adv_proto, query->resp, query->status_code);
167 gas_query_free(query, 0);
168 }
169
170
171 /**
172 * gas_query_deinit - Deinitialize GAS query component
173 * @gas: GAS query data from gas_query_init()
174 */
175 void gas_query_deinit(struct gas_query *gas)
176 {
177 struct gas_query_pending *query, *next;
178
179 if (gas == NULL)
180 return;
181
182 dl_list_for_each_safe(query, next, &gas->pending,
183 struct gas_query_pending, list)
184 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
185
186 os_free(gas);
187 }
188
189
190 static struct gas_query_pending *
191 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
192 {
193 struct gas_query_pending *q;
194 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
195 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
196 q->dialog_token == dialog_token)
197 return q;
198 }
199 return NULL;
200 }
201
202
203 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
204 size_t len)
205 {
206 if (wpabuf_resize(&query->resp, len) < 0) {
207 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
208 return -1;
209 }
210 wpabuf_put_data(query->resp, data, len);
211 return 0;
212 }
213
214
215 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
216 unsigned int freq, const u8 *dst,
217 const u8 *src, const u8 *bssid,
218 const u8 *data, size_t data_len,
219 enum offchannel_send_action_result result)
220 {
221 struct gas_query_pending *query;
222 struct gas_query *gas = wpa_s->gas;
223 int dur;
224
225 if (gas->current == NULL) {
226 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
227 MACSTR " result=%d - no query in progress",
228 freq, MAC2STR(dst), result);
229 return;
230 }
231
232 query = gas->current;
233
234 dur = ms_from_time(&query->last_oper);
235 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
236 " result=%d query=%p dialog_token=%u dur=%d ms",
237 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
238 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
239 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
240 return;
241 }
242 os_get_reltime(&query->last_oper);
243
244 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
245 eloop_cancel_timeout(gas_query_timeout, gas, query);
246 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
247 gas_query_timeout, gas, query);
248 if (query->wait_comeback && !query->retry) {
249 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
250 gas, query);
251 eloop_register_timeout(
252 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
253 gas_query_rx_comeback_timeout, gas, query);
254 }
255 }
256 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
257 eloop_cancel_timeout(gas_query_timeout, gas, query);
258 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
259 }
260 }
261
262
263 static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
264 {
265 if (wpa_s->current_ssid == NULL ||
266 wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
267 os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
268 return 0;
269 return wpa_sm_pmf_enabled(wpa_s->wpa);
270 }
271
272
273 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
274 struct wpabuf *req, unsigned int wait_time)
275 {
276 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
277 const u8 *bssid;
278 const u8 wildcard_bssid[ETH_ALEN] = {
279 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
280 };
281
282 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
283 "freq=%d prot=%d", MAC2STR(query->addr),
284 (unsigned int) wpabuf_len(req), query->freq, prot);
285 if (prot) {
286 u8 *categ = wpabuf_mhead_u8(req);
287 *categ = WLAN_ACTION_PROTECTED_DUAL;
288 }
289 os_get_reltime(&query->last_oper);
290 if (gas->wpa_s->max_remain_on_chan &&
291 wait_time > gas->wpa_s->max_remain_on_chan)
292 wait_time = gas->wpa_s->max_remain_on_chan;
293 if (!gas->wpa_s->conf->gas_address3 ||
294 (gas->wpa_s->current_ssid &&
295 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
296 os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0))
297 bssid = query->addr;
298 else
299 bssid = wildcard_bssid;
300 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
301 gas->wpa_s->own_addr, bssid,
302 wpabuf_head(req), wpabuf_len(req),
303 wait_time, gas_query_tx_status, 0);
304 if (res == 0)
305 query->offchannel_tx_started = 1;
306 return res;
307 }
308
309
310 static void gas_query_tx_comeback_req(struct gas_query *gas,
311 struct gas_query_pending *query)
312 {
313 struct wpabuf *req;
314 unsigned int wait_time;
315
316 req = gas_build_comeback_req(query->dialog_token);
317 if (req == NULL) {
318 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
319 return;
320 }
321
322 wait_time = (query->retry || !query->offchannel_tx_started) ?
323 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
324
325 if (gas_query_tx(gas, query, req, wait_time) < 0) {
326 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
327 MACSTR, MAC2STR(query->addr));
328 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
329 }
330
331 wpabuf_free(req);
332 }
333
334
335 static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
336 {
337 struct gas_query *gas = eloop_data;
338 struct gas_query_pending *query = user_ctx;
339 int dialog_token;
340
341 wpa_printf(MSG_DEBUG,
342 "GAS: No response to comeback request received (retry=%u)",
343 query->retry);
344 if (gas->current != query || query->retry)
345 return;
346 dialog_token = gas_query_new_dialog_token(gas, query->addr);
347 if (dialog_token < 0)
348 return;
349 wpa_printf(MSG_DEBUG,
350 "GAS: Retry GAS query due to comeback response timeout");
351 query->retry = 1;
352 query->dialog_token = dialog_token;
353 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
354 query->wait_comeback = 0;
355 query->next_frag_id = 0;
356 wpabuf_free(query->adv_proto);
357 query->adv_proto = NULL;
358 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
359 eloop_cancel_timeout(gas_query_timeout, gas, query);
360 gas_query_tx_initial_req(gas, query);
361 }
362
363
364 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
365 {
366 struct gas_query *gas = eloop_data;
367 struct gas_query_pending *query = user_ctx;
368
369 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
370 MAC2STR(query->addr));
371 gas_query_tx_comeback_req(gas, query);
372 }
373
374
375 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
376 struct gas_query_pending *query,
377 u16 comeback_delay)
378 {
379 unsigned int secs, usecs;
380
381 if (comeback_delay > 1 && query->offchannel_tx_started) {
382 offchannel_send_action_done(gas->wpa_s);
383 query->offchannel_tx_started = 0;
384 }
385
386 secs = (comeback_delay * 1024) / 1000000;
387 usecs = comeback_delay * 1024 - secs * 1000000;
388 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
389 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
390 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
391 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
392 gas, query);
393 }
394
395
396 static void gas_query_rx_initial(struct gas_query *gas,
397 struct gas_query_pending *query,
398 const u8 *adv_proto, const u8 *resp,
399 size_t len, u16 comeback_delay)
400 {
401 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
402 MACSTR " (dialog_token=%u comeback_delay=%u)",
403 MAC2STR(query->addr), query->dialog_token, comeback_delay);
404
405 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
406 if (query->adv_proto == NULL) {
407 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
408 return;
409 }
410
411 if (comeback_delay) {
412 query->wait_comeback = 1;
413 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
414 return;
415 }
416
417 /* Query was completed without comeback mechanism */
418 if (gas_query_append(query, resp, len) < 0) {
419 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
420 return;
421 }
422
423 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
424 }
425
426
427 static void gas_query_rx_comeback(struct gas_query *gas,
428 struct gas_query_pending *query,
429 const u8 *adv_proto, const u8 *resp,
430 size_t len, u8 frag_id, u8 more_frags,
431 u16 comeback_delay)
432 {
433 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
434 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
435 "comeback_delay=%u)",
436 MAC2STR(query->addr), query->dialog_token, frag_id,
437 more_frags, comeback_delay);
438 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
439
440 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
441 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
442 wpabuf_len(query->adv_proto)) != 0) {
443 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
444 "between initial and comeback response from "
445 MACSTR, MAC2STR(query->addr));
446 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
447 return;
448 }
449
450 if (comeback_delay) {
451 if (frag_id) {
452 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
453 "with non-zero frag_id and comeback_delay "
454 "from " MACSTR, MAC2STR(query->addr));
455 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
456 return;
457 }
458 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
459 return;
460 }
461
462 if (frag_id != query->next_frag_id) {
463 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
464 "from " MACSTR, MAC2STR(query->addr));
465 if (frag_id + 1 == query->next_frag_id) {
466 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
467 "retry of previous fragment");
468 return;
469 }
470 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
471 return;
472 }
473 query->next_frag_id++;
474
475 if (gas_query_append(query, resp, len) < 0) {
476 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
477 return;
478 }
479
480 if (more_frags) {
481 gas_query_tx_comeback_req(gas, query);
482 return;
483 }
484
485 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
486 }
487
488
489 /**
490 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
491 * @gas: GAS query data from gas_query_init()
492 * @da: Destination MAC address of the Action frame
493 * @sa: Source MAC address of the Action frame
494 * @bssid: BSSID of the Action frame
495 * @categ: Category of the Action frame
496 * @data: Payload of the Action frame
497 * @len: Length of @data
498 * @freq: Frequency (in MHz) on which the frame was received
499 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
500 */
501 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
502 const u8 *bssid, u8 categ, const u8 *data, size_t len,
503 int freq)
504 {
505 struct gas_query_pending *query;
506 u8 action, dialog_token, frag_id = 0, more_frags = 0;
507 u16 comeback_delay, resp_len;
508 const u8 *pos, *adv_proto;
509 int prot, pmf;
510 unsigned int left;
511
512 if (gas == NULL || len < 4)
513 return -1;
514
515 pos = data;
516 action = *pos++;
517 dialog_token = *pos++;
518
519 if (action != WLAN_PA_GAS_INITIAL_RESP &&
520 action != WLAN_PA_GAS_COMEBACK_RESP)
521 return -1; /* Not a GAS response */
522
523 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
524 pmf = pmf_in_use(gas->wpa_s, sa);
525 if (prot && !pmf) {
526 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
527 return 0;
528 }
529 if (!prot && pmf) {
530 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
531 return 0;
532 }
533
534 query = gas_query_get_pending(gas, sa, dialog_token);
535 if (query == NULL) {
536 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
537 " dialog token %u", MAC2STR(sa), dialog_token);
538 return -1;
539 }
540
541 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
542 ms_from_time(&query->last_oper), MAC2STR(sa));
543
544 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
545 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
546 MACSTR " dialog token %u when waiting for comeback "
547 "response", MAC2STR(sa), dialog_token);
548 return 0;
549 }
550
551 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
552 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
553 MACSTR " dialog token %u when waiting for initial "
554 "response", MAC2STR(sa), dialog_token);
555 return 0;
556 }
557
558 query->status_code = WPA_GET_LE16(pos);
559 pos += 2;
560
561 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
562 action == WLAN_PA_GAS_COMEBACK_RESP) {
563 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
564 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
565 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
566 "%u failed - status code %u",
567 MAC2STR(sa), dialog_token, query->status_code);
568 gas_query_done(gas, query, GAS_QUERY_FAILURE);
569 return 0;
570 }
571
572 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
573 if (pos + 1 > data + len)
574 return 0;
575 frag_id = *pos & 0x7f;
576 more_frags = (*pos & 0x80) >> 7;
577 pos++;
578 }
579
580 /* Comeback Delay */
581 if (pos + 2 > data + len)
582 return 0;
583 comeback_delay = WPA_GET_LE16(pos);
584 pos += 2;
585
586 /* Advertisement Protocol element */
587 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
588 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
589 "Protocol element in the response from " MACSTR,
590 MAC2STR(sa));
591 return 0;
592 }
593
594 if (*pos != WLAN_EID_ADV_PROTO) {
595 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
596 "Protocol element ID %u in response from " MACSTR,
597 *pos, MAC2STR(sa));
598 return 0;
599 }
600
601 adv_proto = pos;
602 pos += 2 + pos[1];
603
604 /* Query Response Length */
605 if (pos + 2 > data + len) {
606 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
607 return 0;
608 }
609 resp_len = WPA_GET_LE16(pos);
610 pos += 2;
611
612 left = data + len - pos;
613 if (resp_len > left) {
614 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
615 "response from " MACSTR, MAC2STR(sa));
616 return 0;
617 }
618
619 if (resp_len < left) {
620 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
621 "after Query Response from " MACSTR,
622 left - resp_len, MAC2STR(sa));
623 }
624
625 if (action == WLAN_PA_GAS_COMEBACK_RESP)
626 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
627 frag_id, more_frags, comeback_delay);
628 else
629 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
630 comeback_delay);
631
632 return 0;
633 }
634
635
636 static void gas_query_timeout(void *eloop_data, void *user_ctx)
637 {
638 struct gas_query *gas = eloop_data;
639 struct gas_query_pending *query = user_ctx;
640
641 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
642 " dialog token %u",
643 MAC2STR(query->addr), query->dialog_token);
644 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
645 }
646
647
648 static int gas_query_dialog_token_available(struct gas_query *gas,
649 const u8 *dst, u8 dialog_token)
650 {
651 struct gas_query_pending *q;
652 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
653 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
654 dialog_token == q->dialog_token)
655 return 0;
656 }
657
658 return 1;
659 }
660
661
662 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
663 {
664 struct gas_query_pending *query = work->ctx;
665 struct gas_query *gas = query->gas;
666 struct wpa_supplicant *wpa_s = gas->wpa_s;
667
668 if (deinit) {
669 if (work->started) {
670 gas->work = NULL;
671 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
672 return;
673 }
674
675 gas_query_free(query, 1);
676 return;
677 }
678
679 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
680 wpa_msg(wpa_s, MSG_INFO,
681 "Failed to assign random MAC address for GAS");
682 gas_query_free(query, 1);
683 radio_work_done(work);
684 return;
685 }
686
687 gas->work = work;
688 gas_query_tx_initial_req(gas, query);
689 }
690
691
692 static void gas_query_tx_initial_req(struct gas_query *gas,
693 struct gas_query_pending *query)
694 {
695 if (gas_query_tx(gas, query, query->req,
696 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
697 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
698 MACSTR, MAC2STR(query->addr));
699 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
700 return;
701 }
702 gas->current = query;
703
704 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
705 query->dialog_token);
706 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
707 gas_query_timeout, gas, query);
708 }
709
710
711 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
712 {
713 static int next_start = 0;
714 int dialog_token;
715
716 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
717 if (gas_query_dialog_token_available(
718 gas, dst, (next_start + dialog_token) % 256))
719 break;
720 }
721 if (dialog_token == 256)
722 return -1; /* Too many pending queries */
723 dialog_token = (next_start + dialog_token) % 256;
724 next_start = (dialog_token + 1) % 256;
725 return dialog_token;
726 }
727
728
729 /**
730 * gas_query_req - Request a GAS query
731 * @gas: GAS query data from gas_query_init()
732 * @dst: Destination MAC address for the query
733 * @freq: Frequency (in MHz) for the channel on which to send the query
734 * @req: GAS query payload (to be freed by gas_query module in case of success
735 * return)
736 * @cb: Callback function for reporting GAS query result and response
737 * @ctx: Context pointer to use with the @cb call
738 * Returns: dialog token (>= 0) on success or -1 on failure
739 */
740 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
741 struct wpabuf *req,
742 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
743 enum gas_query_result result,
744 const struct wpabuf *adv_proto,
745 const struct wpabuf *resp, u16 status_code),
746 void *ctx)
747 {
748 struct gas_query_pending *query;
749 int dialog_token;
750
751 if (wpabuf_len(req) < 3)
752 return -1;
753
754 dialog_token = gas_query_new_dialog_token(gas, dst);
755 if (dialog_token < 0)
756 return -1;
757
758 query = os_zalloc(sizeof(*query));
759 if (query == NULL)
760 return -1;
761
762 query->gas = gas;
763 os_memcpy(query->addr, dst, ETH_ALEN);
764 query->dialog_token = dialog_token;
765 query->freq = freq;
766 query->cb = cb;
767 query->ctx = ctx;
768 query->req = req;
769 dl_list_add(&gas->pending, &query->list);
770
771 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
772
773 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
774 " dialog_token=%u freq=%d",
775 MAC2STR(query->addr), query->dialog_token, query->freq);
776
777 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
778 query) < 0) {
779 gas_query_free(query, 1);
780 return -1;
781 }
782
783 return dialog_token;
784 }
785
786
787 /**
788 * gas_query_cancel - Cancel a pending GAS query
789 * @gas: GAS query data from gas_query_init()
790 * @dst: Destination MAC address for the query
791 * @dialog_token: Dialog token from gas_query_req()
792 */
793 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
794 {
795 struct gas_query_pending *query;
796
797 query = gas_query_get_pending(gas, dst, dialog_token);
798 if (query)
799 gas_query_done(gas, query, GAS_QUERY_CANCELLED);
800
801 }