Coverage Report

Created: 2026-10-28 06:04

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