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 "driver_i.h"
21 #include "offchannel.h"
22 #include "gas_query.h"
23
24
25 /** GAS query timeout in seconds */
26 #define GAS_QUERY_TIMEOUT_PERIOD 2
27
28
29 /**
30 * struct gas_query_pending - Pending GAS query
31 */
32 struct gas_query_pending {
33 struct dl_list list;
34 struct gas_query *gas;
35 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;
42 struct wpabuf *req;
43 struct wpabuf *adv_proto;
44 struct wpabuf *resp;
45 struct os_reltime last_oper;
46 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
53 /**
54 * struct gas_query - Internal GAS query data
55 */
56 struct gas_query {
57 struct wpa_supplicant *wpa_s;
58 struct dl_list pending; /* struct gas_query_pending */
59 struct gas_query_pending *current;
60 struct wpa_radio_work *work;
61 };
62
63
64 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
65 static void gas_query_timeout(void *eloop_data, void *user_ctx);
66
67
ms_from_time(struct os_reltime * last)68 static 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
78 /**
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 */
gas_query_init(struct wpa_supplicant * wpa_s)83 struct 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
gas_result_txt(enum gas_query_result result)98 static 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
gas_query_free(struct gas_query_pending * query,int del_list)121 static 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
gas_query_done(struct gas_query * gas,struct gas_query_pending * query,enum gas_query_result result)140 static void gas_query_done(struct gas_query *gas,
141 struct gas_query_pending *query,
142 enum gas_query_result result)
143 {
144 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));
148 if (gas->current == query)
149 gas->current = NULL;
150 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);
157 gas_query_free(query, 0);
158 }
159
160
161 /**
162 * gas_query_deinit - Deinitialize GAS query component
163 * @gas: GAS query data from gas_query_init()
164 */
gas_query_deinit(struct gas_query * gas)165 void 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
180 static struct gas_query_pending *
gas_query_get_pending(struct gas_query * gas,const u8 * addr,u8 dialog_token)181 gas_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
gas_query_append(struct gas_query_pending * query,const u8 * data,size_t len)193 static 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
gas_query_tx_status(struct wpa_supplicant * wpa_s,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * data,size_t data_len,enum offchannel_send_action_result result)205 static 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;
213 int dur;
214
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
224 dur = ms_from_time(&query->last_oper);
225 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
226 " result=%d query=%p dialog_token=%u dur=%d ms",
227 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
228 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
229 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
230 return;
231 }
232 os_get_reltime(&query->last_oper);
233
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
pmf_in_use(struct wpa_supplicant * wpa_s,const u8 * addr)246 static 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
gas_query_tx(struct gas_query * gas,struct gas_query_pending * query,struct wpabuf * req)256 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
257 struct wpabuf *req)
258 {
259 unsigned int wait_time;
260 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
261
262 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
263 "freq=%d prot=%d", MAC2STR(query->addr),
264 (unsigned int) wpabuf_len(req), query->freq, prot);
265 if (prot) {
266 u8 *categ = wpabuf_mhead_u8(req);
267 *categ = WLAN_ACTION_PROTECTED_DUAL;
268 }
269 os_get_reltime(&query->last_oper);
270 wait_time = 1000;
271 if (gas->wpa_s->max_remain_on_chan &&
272 wait_time > gas->wpa_s->max_remain_on_chan)
273 wait_time = gas->wpa_s->max_remain_on_chan;
274 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
275 gas->wpa_s->own_addr, query->addr,
276 wpabuf_head(req), wpabuf_len(req),
277 wait_time, gas_query_tx_status, 0);
278 if (res == 0)
279 query->offchannel_tx_started = 1;
280 return res;
281 }
282
283
gas_query_tx_comeback_req(struct gas_query * gas,struct gas_query_pending * query)284 static void gas_query_tx_comeback_req(struct gas_query *gas,
285 struct gas_query_pending *query)
286 {
287 struct wpabuf *req;
288
289 req = gas_build_comeback_req(query->dialog_token);
290 if (req == NULL) {
291 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
292 return;
293 }
294
295 if (gas_query_tx(gas, query, req) < 0) {
296 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
297 MACSTR, MAC2STR(query->addr));
298 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
299 }
300
301 wpabuf_free(req);
302 }
303
304
gas_query_tx_comeback_timeout(void * eloop_data,void * user_ctx)305 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
306 {
307 struct gas_query *gas = eloop_data;
308 struct gas_query_pending *query = user_ctx;
309
310 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
311 MAC2STR(query->addr));
312 gas_query_tx_comeback_req(gas, query);
313 }
314
315
gas_query_tx_comeback_req_delay(struct gas_query * gas,struct gas_query_pending * query,u16 comeback_delay)316 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
317 struct gas_query_pending *query,
318 u16 comeback_delay)
319 {
320 unsigned int secs, usecs;
321
322 secs = (comeback_delay * 1024) / 1000000;
323 usecs = comeback_delay * 1024 - secs * 1000000;
324 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
325 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
326 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
327 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
328 gas, query);
329 }
330
331
gas_query_rx_initial(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,const u8 * resp,size_t len,u16 comeback_delay)332 static void gas_query_rx_initial(struct gas_query *gas,
333 struct gas_query_pending *query,
334 const u8 *adv_proto, const u8 *resp,
335 size_t len, u16 comeback_delay)
336 {
337 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
338 MACSTR " (dialog_token=%u comeback_delay=%u)",
339 MAC2STR(query->addr), query->dialog_token, comeback_delay);
340
341 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
342 if (query->adv_proto == NULL) {
343 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
344 return;
345 }
346
347 if (comeback_delay) {
348 query->wait_comeback = 1;
349 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
350 return;
351 }
352
353 /* Query was completed without comeback mechanism */
354 if (gas_query_append(query, resp, len) < 0) {
355 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
356 return;
357 }
358
359 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
360 }
361
362
gas_query_rx_comeback(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,const u8 * resp,size_t len,u8 frag_id,u8 more_frags,u16 comeback_delay)363 static void gas_query_rx_comeback(struct gas_query *gas,
364 struct gas_query_pending *query,
365 const u8 *adv_proto, const u8 *resp,
366 size_t len, u8 frag_id, u8 more_frags,
367 u16 comeback_delay)
368 {
369 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
370 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
371 "comeback_delay=%u)",
372 MAC2STR(query->addr), query->dialog_token, frag_id,
373 more_frags, comeback_delay);
374
375 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
376 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
377 wpabuf_len(query->adv_proto)) != 0) {
378 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
379 "between initial and comeback response from "
380 MACSTR, MAC2STR(query->addr));
381 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
382 return;
383 }
384
385 if (comeback_delay) {
386 if (frag_id) {
387 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
388 "with non-zero frag_id and comeback_delay "
389 "from " MACSTR, MAC2STR(query->addr));
390 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
391 return;
392 }
393 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
394 return;
395 }
396
397 if (frag_id != query->next_frag_id) {
398 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
399 "from " MACSTR, MAC2STR(query->addr));
400 if (frag_id + 1 == query->next_frag_id) {
401 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
402 "retry of previous fragment");
403 return;
404 }
405 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
406 return;
407 }
408 query->next_frag_id++;
409
410 if (gas_query_append(query, resp, len) < 0) {
411 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
412 return;
413 }
414
415 if (more_frags) {
416 gas_query_tx_comeback_req(gas, query);
417 return;
418 }
419
420 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
421 }
422
423
424 /**
425 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
426 * @gas: GAS query data from gas_query_init()
427 * @da: Destination MAC address of the Action frame
428 * @sa: Source MAC address of the Action frame
429 * @bssid: BSSID of the Action frame
430 * @categ: Category of the Action frame
431 * @data: Payload of the Action frame
432 * @len: Length of @data
433 * @freq: Frequency (in MHz) on which the frame was received
434 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
435 */
gas_query_rx(struct gas_query * gas,const u8 * da,const u8 * sa,const u8 * bssid,u8 categ,const u8 * data,size_t len,int freq)436 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
437 const u8 *bssid, u8 categ, const u8 *data, size_t len,
438 int freq)
439 {
440 struct gas_query_pending *query;
441 u8 action, dialog_token, frag_id = 0, more_frags = 0;
442 u16 comeback_delay, resp_len;
443 const u8 *pos, *adv_proto;
444 int prot, pmf;
445
446 if (gas == NULL || len < 4)
447 return -1;
448
449 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
450 pmf = pmf_in_use(gas->wpa_s, bssid);
451 if (prot && !pmf) {
452 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
453 return 0;
454 }
455 if (!prot && pmf) {
456 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
457 return 0;
458 }
459
460 pos = data;
461 action = *pos++;
462 dialog_token = *pos++;
463
464 if (action != WLAN_PA_GAS_INITIAL_RESP &&
465 action != WLAN_PA_GAS_COMEBACK_RESP)
466 return -1; /* Not a GAS response */
467
468 query = gas_query_get_pending(gas, sa, dialog_token);
469 if (query == NULL) {
470 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
471 " dialog token %u", MAC2STR(sa), dialog_token);
472 return -1;
473 }
474
475 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
476 ms_from_time(&query->last_oper), MAC2STR(sa));
477
478 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
479 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
480 MACSTR " dialog token %u when waiting for comeback "
481 "response", MAC2STR(sa), dialog_token);
482 return 0;
483 }
484
485 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
486 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
487 MACSTR " dialog token %u when waiting for initial "
488 "response", MAC2STR(sa), dialog_token);
489 return 0;
490 }
491
492 query->status_code = WPA_GET_LE16(pos);
493 pos += 2;
494
495 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
496 action == WLAN_PA_GAS_COMEBACK_RESP) {
497 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
498 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
499 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
500 "%u failed - status code %u",
501 MAC2STR(sa), dialog_token, query->status_code);
502 gas_query_done(gas, query, GAS_QUERY_FAILURE);
503 return 0;
504 }
505
506 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
507 if (pos + 1 > data + len)
508 return 0;
509 frag_id = *pos & 0x7f;
510 more_frags = (*pos & 0x80) >> 7;
511 pos++;
512 }
513
514 /* Comeback Delay */
515 if (pos + 2 > data + len)
516 return 0;
517 comeback_delay = WPA_GET_LE16(pos);
518 pos += 2;
519
520 /* Advertisement Protocol element */
521 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
522 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
523 "Protocol element in the response from " MACSTR,
524 MAC2STR(sa));
525 return 0;
526 }
527
528 if (*pos != WLAN_EID_ADV_PROTO) {
529 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
530 "Protocol element ID %u in response from " MACSTR,
531 *pos, MAC2STR(sa));
532 return 0;
533 }
534
535 adv_proto = pos;
536 pos += 2 + pos[1];
537
538 /* Query Response Length */
539 if (pos + 2 > data + len) {
540 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
541 return 0;
542 }
543 resp_len = WPA_GET_LE16(pos);
544 pos += 2;
545
546 if (pos + resp_len > data + len) {
547 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
548 "response from " MACSTR, MAC2STR(sa));
549 return 0;
550 }
551
552 if (pos + resp_len < data + len) {
553 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
554 "after Query Response from " MACSTR,
555 (unsigned int) (data + len - pos - resp_len),
556 MAC2STR(sa));
557 }
558
559 if (action == WLAN_PA_GAS_COMEBACK_RESP)
560 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
561 frag_id, more_frags, comeback_delay);
562 else
563 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
564 comeback_delay);
565
566 return 0;
567 }
568
569
gas_query_timeout(void * eloop_data,void * user_ctx)570 static void gas_query_timeout(void *eloop_data, void *user_ctx)
571 {
572 struct gas_query *gas = eloop_data;
573 struct gas_query_pending *query = user_ctx;
574
575 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
576 " dialog token %u",
577 MAC2STR(query->addr), query->dialog_token);
578 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
579 }
580
581
gas_query_dialog_token_available(struct gas_query * gas,const u8 * dst,u8 dialog_token)582 static int gas_query_dialog_token_available(struct gas_query *gas,
583 const u8 *dst, u8 dialog_token)
584 {
585 struct gas_query_pending *q;
586 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
587 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
588 dialog_token == q->dialog_token)
589 return 0;
590 }
591
592 return 1;
593 }
594
595
gas_query_start_cb(struct wpa_radio_work * work,int deinit)596 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
597 {
598 struct gas_query_pending *query = work->ctx;
599 struct gas_query *gas = query->gas;
600 struct wpa_supplicant *wpa_s = gas->wpa_s;
601
602 if (deinit) {
603 if (work->started) {
604 gas->work = NULL;
605 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
606 return;
607 }
608
609 gas_query_free(query, 1);
610 return;
611 }
612
613 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
614 wpa_msg(wpa_s, MSG_INFO,
615 "Failed to assign random MAC address for GAS");
616 gas_query_free(query, 1);
617 radio_work_done(work);
618 return;
619 }
620
621 gas->work = work;
622
623 if (gas_query_tx(gas, query, query->req) < 0) {
624 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
625 MACSTR, MAC2STR(query->addr));
626 gas_query_free(query, 1);
627 return;
628 }
629 gas->current = query;
630
631 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
632 query->dialog_token);
633 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
634 gas_query_timeout, gas, query);
635
636 }
637
638
639 /**
640 * gas_query_req - Request a GAS query
641 * @gas: GAS query data from gas_query_init()
642 * @dst: Destination MAC address for the query
643 * @freq: Frequency (in MHz) for the channel on which to send the query
644 * @req: GAS query payload (to be freed by gas_query module in case of success
645 * return)
646 * @cb: Callback function for reporting GAS query result and response
647 * @ctx: Context pointer to use with the @cb call
648 * Returns: dialog token (>= 0) on success or -1 on failure
649 */
gas_query_req(struct gas_query * gas,const u8 * dst,int freq,struct wpabuf * req,void (* cb)(void * ctx,const u8 * dst,u8 dialog_token,enum gas_query_result result,const struct wpabuf * adv_proto,const struct wpabuf * resp,u16 status_code),void * ctx)650 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
651 struct wpabuf *req,
652 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
653 enum gas_query_result result,
654 const struct wpabuf *adv_proto,
655 const struct wpabuf *resp, u16 status_code),
656 void *ctx)
657 {
658 struct gas_query_pending *query;
659 int dialog_token;
660 static int next_start = 0;
661
662 if (wpabuf_len(req) < 3)
663 return -1;
664
665 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
666 if (gas_query_dialog_token_available(
667 gas, dst, (next_start + dialog_token) % 256))
668 break;
669 }
670 if (dialog_token == 256)
671 return -1; /* Too many pending queries */
672 dialog_token = (next_start + dialog_token) % 256;
673 next_start = (dialog_token + 1) % 256;
674
675 query = os_zalloc(sizeof(*query));
676 if (query == NULL)
677 return -1;
678
679 query->gas = gas;
680 os_memcpy(query->addr, dst, ETH_ALEN);
681 query->dialog_token = dialog_token;
682 query->freq = freq;
683 query->cb = cb;
684 query->ctx = ctx;
685 query->req = req;
686 dl_list_add(&gas->pending, &query->list);
687
688 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
689
690 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
691 " dialog_token=%u freq=%d",
692 MAC2STR(query->addr), query->dialog_token, query->freq);
693
694 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
695 query) < 0) {
696 gas_query_free(query, 1);
697 return -1;
698 }
699
700 return dialog_token;
701 }
702
703
704 /**
705 * gas_query_cancel - Cancel a pending GAS query
706 * @gas: GAS query data from gas_query_init()
707 * @dst: Destination MAC address for the query
708 * @dialog_token: Dialog token from gas_query_req()
709 */
gas_query_cancel(struct gas_query * gas,const u8 * dst,u8 dialog_token)710 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
711 {
712 struct gas_query_pending *query;
713
714 query = gas_query_get_pending(gas, dst, dialog_token);
715 if (query)
716 gas_query_done(gas, query, GAS_QUERY_CANCELLED);
717
718 }
719