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