1 /*
2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
5 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi>
6 *
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
9 */
10
11 #include "includes.h"
12
13 #include "common.h"
14 #include "utils/eloop.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/gas.h"
17 #include "common/wpa_ctrl.h"
18 #include "rsn_supp/wpa.h"
19 #include "wpa_supplicant_i.h"
20 #include "config.h"
21 #include "driver_i.h"
22 #include "offchannel.h"
23 #include "gas_query.h"
24
25
26 /** GAS query timeout in seconds */
27 #define GAS_QUERY_TIMEOUT_PERIOD 2
28
29 /* GAS query wait-time / duration in ms */
30 #define GAS_QUERY_WAIT_TIME_INITIAL 1000
31 #define GAS_QUERY_WAIT_TIME_COMEBACK 150
32
33 #define GAS_QUERY_MAX_COMEBACK_DELAY 60000
34
35 /**
36 * struct gas_query_pending - Pending GAS query
37 */
38 struct gas_query_pending {
39 struct dl_list list;
40 struct gas_query *gas;
41 u8 addr[ETH_ALEN];
42 u8 dialog_token;
43 u8 next_frag_id;
44 unsigned int wait_comeback:1;
45 unsigned int offchannel_tx_started:1;
46 unsigned int retry:1;
47 unsigned int wildcard_bssid:1;
48 unsigned int maintain_addr:1;
49 int freq;
50 u16 status_code;
51 struct wpabuf *req;
52 struct wpabuf *adv_proto;
53 struct wpabuf *resp;
54 struct os_reltime last_oper;
55 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
56 enum gas_query_result result,
57 const struct wpabuf *adv_proto,
58 const struct wpabuf *resp, u16 status_code);
59 void *ctx;
60 u8 sa[ETH_ALEN];
61 };
62
63 /**
64 * struct gas_query - Internal GAS query data
65 */
66 struct gas_query {
67 struct wpa_supplicant *wpa_s;
68 struct dl_list pending; /* struct gas_query_pending */
69 struct gas_query_pending *current;
70 struct wpa_radio_work *work;
71 struct os_reltime last_mac_addr_rand;
72 int last_rand_sa_type;
73 u8 rand_addr[ETH_ALEN];
74 };
75
76
77 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
78 static void gas_query_timeout(void *eloop_data, void *user_ctx);
79 static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
80 static void gas_query_tx_initial_req(struct gas_query *gas,
81 struct gas_query_pending *query);
82 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
83
84
ms_from_time(struct os_reltime * last)85 static int ms_from_time(struct os_reltime *last)
86 {
87 struct os_reltime now, res;
88
89 os_get_reltime(&now);
90 os_reltime_sub(&now, last, &res);
91 return res.sec * 1000 + res.usec / 1000;
92 }
93
94
95 /**
96 * gas_query_init - Initialize GAS query component
97 * @wpa_s: Pointer to wpa_supplicant data
98 * Returns: Pointer to GAS query data or %NULL on failure
99 */
gas_query_init(struct wpa_supplicant * wpa_s)100 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
101 {
102 struct gas_query *gas;
103
104 gas = os_zalloc(sizeof(*gas));
105 if (gas == NULL)
106 return NULL;
107
108 gas->wpa_s = wpa_s;
109 dl_list_init(&gas->pending);
110
111 return gas;
112 }
113
114
gas_result_txt(enum gas_query_result result)115 static const char * gas_result_txt(enum gas_query_result result)
116 {
117 switch (result) {
118 case GAS_QUERY_SUCCESS:
119 return "SUCCESS";
120 case GAS_QUERY_FAILURE:
121 return "FAILURE";
122 case GAS_QUERY_TIMEOUT:
123 return "TIMEOUT";
124 case GAS_QUERY_PEER_ERROR:
125 return "PEER_ERROR";
126 case GAS_QUERY_INTERNAL_ERROR:
127 return "INTERNAL_ERROR";
128 case GAS_QUERY_STOPPED:
129 return "STOPPED";
130 case GAS_QUERY_DELETED_AT_DEINIT:
131 return "DELETED_AT_DEINIT";
132 }
133
134 return "N/A";
135 }
136
137
gas_query_free(struct gas_query_pending * query,int del_list)138 static void gas_query_free(struct gas_query_pending *query, int del_list)
139 {
140 struct gas_query *gas = query->gas;
141
142 if (del_list)
143 dl_list_del(&query->list);
144
145 if (gas->work && gas->work->ctx == query) {
146 radio_work_done(gas->work);
147 gas->work = NULL;
148 }
149
150 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
151 eloop_cancel_timeout(gas_query_timeout, gas, query);
152 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
153
154 wpabuf_free(query->req);
155 wpabuf_free(query->adv_proto);
156 wpabuf_free(query->resp);
157 os_free(query);
158 }
159
160
gas_query_done(struct gas_query * gas,struct gas_query_pending * query,enum gas_query_result result)161 static void gas_query_done(struct gas_query *gas,
162 struct gas_query_pending *query,
163 enum gas_query_result result)
164 {
165 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
166 " dialog_token=%u freq=%d status_code=%u result=%s",
167 MAC2STR(query->addr), query->dialog_token, query->freq,
168 query->status_code, gas_result_txt(result));
169 if (gas->current == query)
170 gas->current = NULL;
171 if (query->offchannel_tx_started)
172 offchannel_send_action_done(gas->wpa_s);
173 dl_list_del(&query->list);
174 query->cb(query->ctx, query->addr, query->dialog_token, result,
175 query->adv_proto, query->resp, query->status_code);
176 gas_query_free(query, 0);
177 }
178
179
180 /**
181 * gas_query_deinit - Deinitialize GAS query component
182 * @gas: GAS query data from gas_query_init()
183 */
gas_query_deinit(struct gas_query * gas)184 void gas_query_deinit(struct gas_query *gas)
185 {
186 struct gas_query_pending *query, *next;
187
188 if (gas == NULL)
189 return;
190
191 dl_list_for_each_safe(query, next, &gas->pending,
192 struct gas_query_pending, list)
193 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
194
195 os_free(gas);
196 }
197
198
199 static struct gas_query_pending *
gas_query_get_pending(struct gas_query * gas,const u8 * addr,u8 dialog_token)200 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
201 {
202 struct gas_query_pending *q;
203 struct wpa_supplicant *wpa_s = gas->wpa_s;
204
205 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
206 if (ether_addr_equal(q->addr, addr) &&
207 q->dialog_token == dialog_token)
208 return q;
209 if (wpa_s->valid_links &&
210 ether_addr_equal(wpa_s->ap_mld_addr, addr) &&
211 wpas_ap_link_address(wpa_s, q->addr))
212 return q;
213 }
214 return NULL;
215 }
216
217
gas_query_append(struct gas_query_pending * query,const u8 * data,size_t len)218 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
219 size_t len)
220 {
221 if (wpabuf_resize(&query->resp, len) < 0) {
222 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
223 return -1;
224 }
225 wpabuf_put_data(query->resp, data, len);
226 return 0;
227 }
228
229
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)230 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
231 unsigned int freq, const u8 *dst,
232 const u8 *src, const u8 *bssid,
233 const u8 *data, size_t data_len,
234 enum offchannel_send_action_result result)
235 {
236 struct gas_query_pending *query;
237 struct gas_query *gas = wpa_s->gas;
238 int dur;
239
240 if (gas->current == NULL) {
241 wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
242 MACSTR " result=%d - no query in progress",
243 freq, MAC2STR(dst), result);
244 return;
245 }
246
247 query = gas->current;
248
249 dur = ms_from_time(&query->last_oper);
250 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
251 " result=%d query=%p dialog_token=%u dur=%d ms",
252 freq, MAC2STR(dst), result, query, query->dialog_token, dur);
253 if (!ether_addr_equal(dst, query->addr)) {
254 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
255 return;
256 }
257 os_get_reltime(&query->last_oper);
258
259 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS ||
260 result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
261 eloop_cancel_timeout(gas_query_timeout, gas, query);
262 if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) {
263 wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request");
264 eloop_register_timeout(0, 250000,
265 gas_query_timeout, gas, query);
266 } else {
267 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
268 gas_query_timeout, gas, query);
269 }
270 if (query->wait_comeback && !query->retry) {
271 eloop_cancel_timeout(gas_query_rx_comeback_timeout,
272 gas, query);
273 eloop_register_timeout(
274 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
275 gas_query_rx_comeback_timeout, gas, query);
276 }
277 }
278 if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
279 eloop_cancel_timeout(gas_query_timeout, gas, query);
280 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
281 }
282 }
283
284
gas_query_tx(struct gas_query * gas,struct gas_query_pending * query,struct wpabuf * req,unsigned int wait_time)285 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
286 struct wpabuf *req, unsigned int wait_time)
287 {
288 int res, prot = pmf_in_use(gas->wpa_s, query->addr);
289 const u8 *bssid;
290 const u8 wildcard_bssid[ETH_ALEN] = {
291 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
292 };
293
294 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
295 "freq=%d prot=%d using src addr " MACSTR,
296 MAC2STR(query->addr), (unsigned int) wpabuf_len(req),
297 query->freq, prot, MAC2STR(query->sa));
298 if (prot) {
299 u8 *categ = wpabuf_mhead_u8(req);
300 *categ = WLAN_ACTION_PROTECTED_DUAL;
301 }
302 os_get_reltime(&query->last_oper);
303 if (gas->wpa_s->max_remain_on_chan &&
304 wait_time > gas->wpa_s->max_remain_on_chan)
305 wait_time = gas->wpa_s->max_remain_on_chan;
306 if (!query->wildcard_bssid &&
307 (!gas->wpa_s->conf->gas_address3 ||
308 (gas->wpa_s->current_ssid &&
309 gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
310 ether_addr_equal(query->addr, gas->wpa_s->bssid))))
311 bssid = query->addr;
312 else
313 bssid = wildcard_bssid;
314
315 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
316 query->sa, bssid, wpabuf_head(req),
317 wpabuf_len(req), wait_time,
318 gas_query_tx_status, 0);
319
320 if (res == 0)
321 query->offchannel_tx_started = 1;
322 return res;
323 }
324
325
gas_query_tx_comeback_req(struct gas_query * gas,struct gas_query_pending * query)326 static void gas_query_tx_comeback_req(struct gas_query *gas,
327 struct gas_query_pending *query)
328 {
329 struct wpabuf *req;
330 unsigned int wait_time;
331
332 req = gas_build_comeback_req(query->dialog_token);
333 if (req == NULL) {
334 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
335 return;
336 }
337
338 wait_time = (query->retry || !query->offchannel_tx_started) ?
339 GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
340
341 if (gas_query_tx(gas, query, req, wait_time) < 0) {
342 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
343 MACSTR, MAC2STR(query->addr));
344 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
345 }
346
347 wpabuf_free(req);
348 }
349
350
gas_query_rx_comeback_timeout(void * eloop_data,void * user_ctx)351 static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
352 {
353 struct gas_query *gas = eloop_data;
354 struct gas_query_pending *query = user_ctx;
355 int dialog_token;
356
357 wpa_printf(MSG_DEBUG,
358 "GAS: No response to comeback request received (retry=%u)",
359 query->retry);
360 if (gas->current != query || query->retry)
361 return;
362 dialog_token = gas_query_new_dialog_token(gas, query->addr);
363 if (dialog_token < 0)
364 return;
365 wpa_printf(MSG_DEBUG,
366 "GAS: Retry GAS query due to comeback response timeout");
367 query->retry = 1;
368 query->dialog_token = dialog_token;
369 *(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
370 query->wait_comeback = 0;
371 query->next_frag_id = 0;
372 wpabuf_free(query->adv_proto);
373 query->adv_proto = NULL;
374 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
375 eloop_cancel_timeout(gas_query_timeout, gas, query);
376 gas_query_tx_initial_req(gas, query);
377 }
378
379
gas_query_tx_comeback_timeout(void * eloop_data,void * user_ctx)380 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
381 {
382 struct gas_query *gas = eloop_data;
383 struct gas_query_pending *query = user_ctx;
384
385 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
386 MAC2STR(query->addr));
387 gas_query_tx_comeback_req(gas, query);
388 }
389
390
gas_query_tx_comeback_req_delay(struct gas_query * gas,struct gas_query_pending * query,u16 comeback_delay)391 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
392 struct gas_query_pending *query,
393 u16 comeback_delay)
394 {
395 unsigned int secs, usecs;
396
397 if (comeback_delay > 1 && query->offchannel_tx_started) {
398 offchannel_send_action_done(gas->wpa_s);
399 query->offchannel_tx_started = 0;
400 }
401
402 secs = (comeback_delay * 1024) / 1000000;
403 usecs = comeback_delay * 1024 - secs * 1000000;
404 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
405 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
406 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
407 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
408 gas, query);
409 }
410
411
gas_query_rx_initial(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,size_t adv_proto_len,const u8 * resp,size_t len,u16 comeback_delay)412 static void gas_query_rx_initial(struct gas_query *gas,
413 struct gas_query_pending *query,
414 const u8 *adv_proto, size_t adv_proto_len,
415 const u8 *resp, size_t len, u16 comeback_delay)
416 {
417 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
418 MACSTR " (dialog_token=%u comeback_delay=%u)",
419 MAC2STR(query->addr), query->dialog_token, comeback_delay);
420
421 query->adv_proto = wpabuf_alloc_copy(adv_proto, adv_proto_len);
422 if (query->adv_proto == NULL) {
423 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
424 return;
425 }
426
427 if (comeback_delay) {
428 eloop_cancel_timeout(gas_query_timeout, gas, query);
429 query->wait_comeback = 1;
430 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
431 return;
432 }
433
434 /* Query was completed without comeback mechanism */
435 if (gas_query_append(query, resp, len) < 0) {
436 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
437 return;
438 }
439
440 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
441 }
442
443
gas_query_rx_comeback(struct gas_query * gas,struct gas_query_pending * query,const u8 * adv_proto,size_t adv_proto_len,const u8 * resp,size_t len,u8 frag_id,u8 more_frags,u16 comeback_delay)444 static void gas_query_rx_comeback(struct gas_query *gas,
445 struct gas_query_pending *query,
446 const u8 *adv_proto, size_t adv_proto_len,
447 const u8 *resp, size_t len, u8 frag_id,
448 u8 more_frags, u16 comeback_delay)
449 {
450 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
451 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
452 "comeback_delay=%u)",
453 MAC2STR(query->addr), query->dialog_token, frag_id,
454 more_frags, comeback_delay);
455 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
456
457 if (adv_proto_len != wpabuf_len(query->adv_proto) ||
458 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
459 wpabuf_len(query->adv_proto)) != 0) {
460 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
461 "between initial and comeback response from "
462 MACSTR, MAC2STR(query->addr));
463 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
464 return;
465 }
466
467 if (comeback_delay) {
468 if (frag_id) {
469 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
470 "with non-zero frag_id and comeback_delay "
471 "from " MACSTR, MAC2STR(query->addr));
472 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
473 return;
474 }
475 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
476 return;
477 }
478
479 if (frag_id != query->next_frag_id) {
480 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
481 "from " MACSTR, MAC2STR(query->addr));
482 if (frag_id + 1 == query->next_frag_id) {
483 wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
484 "retry of previous fragment");
485 return;
486 }
487 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
488 return;
489 }
490 query->next_frag_id++;
491
492 if (gas_query_append(query, resp, len) < 0) {
493 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
494 return;
495 }
496
497 if (more_frags) {
498 gas_query_tx_comeback_req(gas, query);
499 return;
500 }
501
502 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
503 }
504
505
506 /**
507 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
508 * @gas: GAS query data from gas_query_init()
509 * @da: Destination MAC address of the Action frame
510 * @sa: Source MAC address of the Action frame
511 * @bssid: BSSID of the Action frame
512 * @categ: Category of the Action frame
513 * @data: Payload of the Action frame
514 * @len: Length of @data
515 * @freq: Frequency (in MHz) on which the frame was received
516 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
517 */
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)518 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
519 const u8 *bssid, u8 categ, const u8 *data, size_t len,
520 int freq)
521 {
522 struct gas_query_pending *query;
523 u8 action, dialog_token, frag_id = 0, more_frags = 0;
524 u16 comeback_delay, resp_len;
525 const u8 *pos, *adv_proto;
526 size_t adv_proto_len;
527 int prot, pmf;
528 unsigned int left;
529
530 if (gas == NULL || len < 4)
531 return -1;
532
533 pos = data;
534 action = *pos++;
535 dialog_token = *pos++;
536
537 if (action != WLAN_PA_GAS_INITIAL_RESP &&
538 action != WLAN_PA_GAS_COMEBACK_RESP)
539 return -1; /* Not a GAS response */
540
541 prot = categ == WLAN_ACTION_PROTECTED_DUAL;
542 pmf = pmf_in_use(gas->wpa_s, sa);
543 if (prot && !pmf) {
544 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
545 return 0;
546 }
547 if (!prot && pmf) {
548 wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
549 return 0;
550 }
551
552 query = gas_query_get_pending(gas, sa, dialog_token);
553 if (query == NULL) {
554 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
555 " dialog token %u", MAC2STR(sa), dialog_token);
556 return -1;
557 }
558
559 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
560 ms_from_time(&query->last_oper), MAC2STR(sa));
561
562 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
563 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
564 MACSTR " dialog token %u when waiting for comeback "
565 "response", MAC2STR(sa), dialog_token);
566 return 0;
567 }
568
569 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
570 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
571 MACSTR " dialog token %u when waiting for initial "
572 "response", MAC2STR(sa), dialog_token);
573 return 0;
574 }
575
576 query->status_code = WPA_GET_LE16(pos);
577 pos += 2;
578
579 if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
580 action == WLAN_PA_GAS_COMEBACK_RESP) {
581 wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
582 } else if (query->status_code != WLAN_STATUS_SUCCESS) {
583 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
584 "%u failed - status code %u",
585 MAC2STR(sa), dialog_token, query->status_code);
586 gas_query_done(gas, query, GAS_QUERY_FAILURE);
587 return 0;
588 }
589
590 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
591 if (pos + 1 > data + len)
592 return 0;
593 frag_id = *pos & 0x7f;
594 more_frags = (*pos & 0x80) >> 7;
595 pos++;
596 }
597
598 /* Comeback Delay */
599 if (pos + 2 > data + len)
600 return 0;
601 comeback_delay = WPA_GET_LE16(pos);
602 if (comeback_delay > GAS_QUERY_MAX_COMEBACK_DELAY)
603 comeback_delay = GAS_QUERY_MAX_COMEBACK_DELAY;
604 pos += 2;
605
606 /* Advertisement Protocol element */
607 adv_proto = pos;
608 left = data + len - adv_proto;
609 if (left < 2 || adv_proto[1] > left - 2) {
610 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
611 "Protocol element in the response from " MACSTR,
612 MAC2STR(sa));
613 return 0;
614 }
615
616 if (*adv_proto != WLAN_EID_ADV_PROTO) {
617 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
618 "Protocol element ID %u in response from " MACSTR,
619 *adv_proto, MAC2STR(sa));
620 return 0;
621 }
622 adv_proto_len = 2 + adv_proto[1];
623 if (adv_proto_len > (size_t) (data + len - pos))
624 return 0; /* unreachable due to an earlier check */
625
626 pos += adv_proto_len;
627
628 /* Query Response Length */
629 if (pos + 2 > data + len) {
630 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
631 return 0;
632 }
633 resp_len = WPA_GET_LE16(pos);
634 pos += 2;
635
636 left = data + len - pos;
637 if (resp_len > left) {
638 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
639 "response from " MACSTR, MAC2STR(sa));
640 return 0;
641 }
642
643 if (resp_len < left) {
644 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
645 "after Query Response from " MACSTR,
646 left - resp_len, MAC2STR(sa));
647 }
648
649 if (action == WLAN_PA_GAS_COMEBACK_RESP)
650 gas_query_rx_comeback(gas, query, adv_proto, adv_proto_len,
651 pos, resp_len, frag_id, more_frags,
652 comeback_delay);
653 else
654 gas_query_rx_initial(gas, query, adv_proto, adv_proto_len,
655 pos, resp_len, comeback_delay);
656
657 return 0;
658 }
659
660
gas_query_timeout(void * eloop_data,void * user_ctx)661 static void gas_query_timeout(void *eloop_data, void *user_ctx)
662 {
663 struct gas_query *gas = eloop_data;
664 struct gas_query_pending *query = user_ctx;
665
666 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
667 " dialog token %u",
668 MAC2STR(query->addr), query->dialog_token);
669 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
670 }
671
672
gas_query_dialog_token_available(struct gas_query * gas,const u8 * dst,u8 dialog_token)673 static int gas_query_dialog_token_available(struct gas_query *gas,
674 const u8 *dst, u8 dialog_token)
675 {
676 struct gas_query_pending *q;
677 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
678 if (ether_addr_equal(dst, q->addr) &&
679 dialog_token == q->dialog_token)
680 return 0;
681 }
682
683 return 1;
684 }
685
686
gas_query_start_cb(struct wpa_radio_work * work,int deinit)687 static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
688 {
689 struct gas_query_pending *query = work->ctx;
690 struct gas_query *gas = query->gas;
691 struct wpa_supplicant *wpa_s = gas->wpa_s;
692
693 if (deinit) {
694 if (work->started) {
695 gas->work = NULL;
696 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
697 return;
698 }
699
700 gas_query_free(query, 1);
701 return;
702 }
703
704 if (!query->maintain_addr && !wpa_s->conf->gas_rand_mac_addr) {
705 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
706 wpa_msg(wpa_s, MSG_INFO,
707 "Failed to assign random MAC address for GAS");
708 gas_query_free(query, 1);
709 radio_work_done(work);
710 return;
711 }
712 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
713 }
714
715 gas->work = work;
716 gas_query_tx_initial_req(gas, query);
717 }
718
719
gas_query_tx_initial_req(struct gas_query * gas,struct gas_query_pending * query)720 static void gas_query_tx_initial_req(struct gas_query *gas,
721 struct gas_query_pending *query)
722 {
723 if (gas_query_tx(gas, query, query->req,
724 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
725 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
726 MACSTR, MAC2STR(query->addr));
727 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
728 return;
729 }
730 gas->current = query;
731
732 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
733 query->dialog_token);
734 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
735 gas_query_timeout, gas, query);
736 }
737
738
gas_query_new_dialog_token(struct gas_query * gas,const u8 * dst)739 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
740 {
741 u8 dialog_token;
742 int i;
743
744 /* There should never be more than couple active GAS queries in
745 * progress, so it should be very likely to find an available dialog
746 * token by checking random values. Use a limit on the number of
747 * iterations to handle the unexpected case of large number of pending
748 * queries cleanly. */
749 for (i = 0; i < 256; i++) {
750 /* Get a random number and check if the slot is available */
751 if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0)
752 break;
753 if (gas_query_dialog_token_available(gas, dst, dialog_token))
754 return dialog_token;
755 }
756
757 /* No dialog token value available */
758 return -1;
759 }
760
761
gas_query_set_sa(struct gas_query * gas,struct gas_query_pending * query)762 static int gas_query_set_sa(struct gas_query *gas,
763 struct gas_query_pending *query)
764 {
765 struct wpa_supplicant *wpa_s = gas->wpa_s;
766 struct os_reltime now;
767
768 if (query->maintain_addr ||
769 !wpa_s->conf->gas_rand_mac_addr ||
770 !(wpa_s->current_bss ?
771 (wpa_s->drv_flags &
772 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
773 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) {
774 /* Use own MAC address as the transmitter address */
775 wpa_printf(MSG_DEBUG,
776 "GAS: Use own MAC address as the transmitter address%s%s%s",
777 query->maintain_addr ? " (maintain_addr)" : "",
778 !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "",
779 !(wpa_s->current_bss ?
780 (wpa_s->drv_flags &
781 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) :
782 (wpa_s->drv_flags &
783 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ?
784 " (no driver rand capa" : "");
785 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN);
786 return 0;
787 }
788
789 os_get_reltime(&now);
790
791 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type &&
792 gas->last_mac_addr_rand.sec != 0 &&
793 !os_reltime_expired(&now, &gas->last_mac_addr_rand,
794 wpa_s->conf->gas_rand_addr_lifetime)) {
795 wpa_printf(MSG_DEBUG,
796 "GAS: Use the previously selected random transmitter address "
797 MACSTR, MAC2STR(gas->rand_addr));
798 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
799 return 0;
800 }
801
802 if (wpa_s->conf->gas_rand_mac_addr == 1 &&
803 random_mac_addr(gas->rand_addr) < 0) {
804 wpa_printf(MSG_ERROR, "GAS: Failed to get random address");
805 return -1;
806 }
807
808 if (wpa_s->conf->gas_rand_mac_addr == 2 &&
809 random_mac_addr_keep_oui(gas->rand_addr) < 0) {
810 wpa_printf(MSG_ERROR,
811 "GAS: Failed to get random address with same OUI");
812 return -1;
813 }
814
815 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address "
816 MACSTR, MAC2STR(gas->rand_addr));
817 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN);
818 os_get_reltime(&gas->last_mac_addr_rand);
819 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr;
820
821 return 0;
822 }
823
824
825 /**
826 * gas_query_req - Request a GAS query
827 * @gas: GAS query data from gas_query_init()
828 * @dst: Destination MAC address for the query
829 * @freq: Frequency (in MHz) for the channel on which to send the query
830 * @wildcard_bssid: Force use of wildcard BSSID value
831 * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC
832 * address randomization rules)
833 * @req: GAS query payload (to be freed by gas_query module in case of success
834 * return)
835 * @cb: Callback function for reporting GAS query result and response
836 * @ctx: Context pointer to use with the @cb call
837 * Returns: dialog token (>= 0) on success or -1 on failure
838 */
gas_query_req(struct gas_query * gas,const u8 * dst,int freq,int wildcard_bssid,int maintain_addr,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)839 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
840 int wildcard_bssid, int maintain_addr, struct wpabuf *req,
841 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
842 enum gas_query_result result,
843 const struct wpabuf *adv_proto,
844 const struct wpabuf *resp, u16 status_code),
845 void *ctx)
846 {
847 struct gas_query_pending *query;
848 int dialog_token;
849
850 if (wpabuf_len(req) < 3)
851 return -1;
852
853 dialog_token = gas_query_new_dialog_token(gas, dst);
854 if (dialog_token < 0)
855 return -1;
856
857 query = os_zalloc(sizeof(*query));
858 if (query == NULL)
859 return -1;
860
861 query->gas = gas;
862 query->maintain_addr = !!maintain_addr;
863 if (gas_query_set_sa(gas, query)) {
864 os_free(query);
865 return -1;
866 }
867 os_memcpy(query->addr, dst, ETH_ALEN);
868 query->dialog_token = dialog_token;
869 query->wildcard_bssid = !!wildcard_bssid;
870 query->freq = freq;
871 query->cb = cb;
872 query->ctx = ctx;
873 query->req = req;
874 dl_list_add(&gas->pending, &query->list);
875
876 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
877
878 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
879 " dialog_token=%u freq=%d",
880 MAC2STR(query->addr), query->dialog_token, query->freq);
881
882 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
883 query) < 0) {
884 query->req = NULL; /* caller will free this in error case */
885 gas_query_free(query, 1);
886 return -1;
887 }
888
889 return dialog_token;
890 }
891
892
gas_query_stop(struct gas_query * gas,u8 dialog_token)893 int gas_query_stop(struct gas_query *gas, u8 dialog_token)
894 {
895 struct gas_query_pending *query;
896
897 dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) {
898 if (query->dialog_token == dialog_token) {
899 if (!gas->work) {
900 /* The pending radio work has not yet been
901 * started, but the pending entry has a
902 * reference to the soon to be freed query.
903 * Need to remove that radio work now to avoid
904 * leaving behind a reference to freed memory.
905 */
906 radio_remove_pending_work(gas->wpa_s, query);
907 }
908 gas_query_done(gas, query, GAS_QUERY_STOPPED);
909 return 0;
910 }
911 }
912
913 return -1;
914 }
915