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