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