GAS: Do not start new scan operation during an ongoing GAS query
[mech_eap.git] / wpa_supplicant / gas_query.c
1 /*
2  * Generic advertisement service (GAS) query
3  * Copyright (c) 2009, Atheros Communications
4  * Copyright (c) 2011-2013, Qualcomm Atheros, Inc.
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9
10 #include "includes.h"
11
12 #include "common.h"
13 #include "utils/eloop.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/gas.h"
16 #include "wpa_supplicant_i.h"
17 #include "driver_i.h"
18 #include "offchannel.h"
19 #include "gas_query.h"
20
21
22 /** GAS query timeout in seconds */
23 #define GAS_QUERY_TIMEOUT_PERIOD 2
24 /** Retry period for GAS query requests in milliseconds */
25 #define GAS_SERVICE_RETRY_PERIOD_MS 500
26
27
28 /**
29  * struct gas_query_pending - Pending GAS query
30  */
31 struct gas_query_pending {
32         struct dl_list list;
33         u8 addr[ETH_ALEN];
34         u8 dialog_token;
35         u8 next_frag_id;
36         unsigned int wait_comeback:1;
37         unsigned int offchannel_tx_started:1;
38         int freq;
39         u16 status_code;
40         struct wpabuf *req;
41         struct wpabuf *adv_proto;
42         struct wpabuf *resp;
43         void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
44                    enum gas_query_result result,
45                    const struct wpabuf *adv_proto,
46                    const struct wpabuf *resp, u16 status_code);
47         void *ctx;
48 };
49
50 /**
51  * struct gas_query - Internal GAS query data
52  */
53 struct gas_query {
54         struct wpa_supplicant *wpa_s;
55         struct dl_list pending; /* struct gas_query_pending */
56         struct gas_query_pending *current;
57 };
58
59
60 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
61 static void gas_query_timeout(void *eloop_data, void *user_ctx);
62 static void gas_service_timeout(void *eloop_data, void *user_ctx);
63
64
65 /**
66  * gas_query_init - Initialize GAS query component
67  * @wpa_s: Pointer to wpa_supplicant data
68  * Returns: Pointer to GAS query data or %NULL on failure
69  */
70 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
71 {
72         struct gas_query *gas;
73
74         gas = os_zalloc(sizeof(*gas));
75         if (gas == NULL)
76                 return NULL;
77
78         gas->wpa_s = wpa_s;
79         dl_list_init(&gas->pending);
80
81         return gas;
82 }
83
84
85 static void gas_query_done(struct gas_query *gas,
86                            struct gas_query_pending *query,
87                            enum gas_query_result result)
88 {
89         if (gas->current == query)
90                 gas->current = NULL;
91         if (query->offchannel_tx_started)
92                 offchannel_send_action_done(gas->wpa_s);
93         eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
94         eloop_cancel_timeout(gas_query_timeout, gas, query);
95         eloop_cancel_timeout(gas_service_timeout, gas, query);
96         dl_list_del(&query->list);
97         query->cb(query->ctx, query->addr, query->dialog_token, result,
98                   query->adv_proto, query->resp, query->status_code);
99         wpabuf_free(query->req);
100         wpabuf_free(query->adv_proto);
101         wpabuf_free(query->resp);
102         os_free(query);
103 }
104
105
106 /**
107  * gas_query_deinit - Deinitialize GAS query component
108  * @gas: GAS query data from gas_query_init()
109  */
110 void gas_query_deinit(struct gas_query *gas)
111 {
112         struct gas_query_pending *query, *next;
113
114         if (gas == NULL)
115                 return;
116
117         dl_list_for_each_safe(query, next, &gas->pending,
118                               struct gas_query_pending, list)
119                 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
120
121         os_free(gas);
122 }
123
124
125 static struct gas_query_pending *
126 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
127 {
128         struct gas_query_pending *q;
129         dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
130                 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
131                     q->dialog_token == dialog_token)
132                         return q;
133         }
134         return NULL;
135 }
136
137
138 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
139                             size_t len)
140 {
141         if (wpabuf_resize(&query->resp, len) < 0) {
142                 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
143                 return -1;
144         }
145         wpabuf_put_data(query->resp, data, len);
146         return 0;
147 }
148
149
150 static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
151                                 unsigned int freq, const u8 *dst,
152                                 const u8 *src, const u8 *bssid,
153                                 const u8 *data, size_t data_len,
154                                 enum offchannel_send_action_result result)
155 {
156         struct gas_query_pending *q, *query = NULL;
157         struct gas_query *gas = wpa_s->gas;
158
159         dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
160                 if (os_memcmp(q->addr, dst, ETH_ALEN) == 0) {
161                         query = q;
162                         break;
163                 }
164         }
165
166         wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
167                    " result=%d query=%p",
168                    freq, MAC2STR(dst), result, query);
169         if (!query)
170                 return;
171
172         if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
173                 eloop_cancel_timeout(gas_query_timeout, gas, query);
174                 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
175                                        gas_query_timeout, gas, query);
176         }
177         if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
178                 eloop_cancel_timeout(gas_query_timeout, gas, query);
179                 eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
180         }
181 }
182
183
184 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
185                         struct wpabuf *req)
186 {
187         int res;
188         wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
189                    "freq=%d", MAC2STR(query->addr),
190                    (unsigned int) wpabuf_len(req), query->freq);
191         res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
192                                      gas->wpa_s->own_addr, query->addr,
193                                      wpabuf_head(req), wpabuf_len(req), 1000,
194                                      gas_query_tx_status, 0);
195         if (res == 0)
196                 query->offchannel_tx_started = 1;
197         return res;
198 }
199
200
201 static void gas_query_tx_comeback_req(struct gas_query *gas,
202                                       struct gas_query_pending *query)
203 {
204         struct wpabuf *req;
205
206         req = gas_build_comeback_req(query->dialog_token);
207         if (req == NULL) {
208                 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
209                 return;
210         }
211
212         if (gas_query_tx(gas, query, req) < 0) {
213                 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
214                            MACSTR, MAC2STR(query->addr));
215                 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
216         }
217
218         wpabuf_free(req);
219 }
220
221
222 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
223 {
224         struct gas_query *gas = eloop_data;
225         struct gas_query_pending *query = user_ctx;
226
227         wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
228                    MAC2STR(query->addr));
229         gas_query_tx_comeback_req(gas, query);
230 }
231
232
233 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
234                                             struct gas_query_pending *query,
235                                             u16 comeback_delay)
236 {
237         unsigned int secs, usecs;
238
239         secs = (comeback_delay * 1024) / 1000000;
240         usecs = comeback_delay * 1024 - secs * 1000000;
241         wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
242                    " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
243         eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
244         eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
245                                gas, query);
246 }
247
248
249 static void gas_query_rx_initial(struct gas_query *gas,
250                                  struct gas_query_pending *query,
251                                  const u8 *adv_proto, const u8 *resp,
252                                  size_t len, u16 comeback_delay)
253 {
254         wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
255                    MACSTR " (dialog_token=%u comeback_delay=%u)",
256                    MAC2STR(query->addr), query->dialog_token, comeback_delay);
257
258         query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
259         if (query->adv_proto == NULL) {
260                 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
261                 return;
262         }
263
264         if (comeback_delay) {
265                 query->wait_comeback = 1;
266                 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
267                 return;
268         }
269
270         /* Query was completed without comeback mechanism */
271         if (gas_query_append(query, resp, len) < 0) {
272                 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
273                 return;
274         }
275
276         gas_query_done(gas, query, GAS_QUERY_SUCCESS);
277 }
278
279
280 static void gas_query_rx_comeback(struct gas_query *gas,
281                                   struct gas_query_pending *query,
282                                   const u8 *adv_proto, const u8 *resp,
283                                   size_t len, u8 frag_id, u8 more_frags,
284                                   u16 comeback_delay)
285 {
286         wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
287                    MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
288                    "comeback_delay=%u)",
289                    MAC2STR(query->addr), query->dialog_token, frag_id,
290                    more_frags, comeback_delay);
291
292         if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
293             os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
294                       wpabuf_len(query->adv_proto)) != 0) {
295                 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
296                            "between initial and comeback response from "
297                            MACSTR, MAC2STR(query->addr));
298                 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
299                 return;
300         }
301
302         if (comeback_delay) {
303                 if (frag_id) {
304                         wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
305                                    "with non-zero frag_id and comeback_delay "
306                                    "from " MACSTR, MAC2STR(query->addr));
307                         gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
308                         return;
309                 }
310                 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
311                 return;
312         }
313
314         if (frag_id != query->next_frag_id) {
315                 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
316                            "from " MACSTR, MAC2STR(query->addr));
317                 if (frag_id + 1 == query->next_frag_id) {
318                         wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
319                                    "retry of previous fragment");
320                         return;
321                 }
322                 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
323                 return;
324         }
325         query->next_frag_id++;
326
327         if (gas_query_append(query, resp, len) < 0) {
328                 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
329                 return;
330         }
331
332         if (more_frags) {
333                 gas_query_tx_comeback_req(gas, query);
334                 return;
335         }
336
337         gas_query_done(gas, query, GAS_QUERY_SUCCESS);
338 }
339
340
341 /**
342  * gas_query_rx - Indicate reception of a Public Action frame
343  * @gas: GAS query data from gas_query_init()
344  * @da: Destination MAC address of the Action frame
345  * @sa: Source MAC address of the Action frame
346  * @bssid: BSSID of the Action frame
347  * @data: Payload of the Action frame
348  * @len: Length of @data
349  * @freq: Frequency (in MHz) on which the frame was received
350  * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
351  */
352 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
353                  const u8 *bssid, const u8 *data, size_t len, int freq)
354 {
355         struct gas_query_pending *query;
356         u8 action, dialog_token, frag_id = 0, more_frags = 0;
357         u16 comeback_delay, resp_len;
358         const u8 *pos, *adv_proto;
359
360         if (gas == NULL || len < 4)
361                 return -1;
362
363         pos = data;
364         action = *pos++;
365         dialog_token = *pos++;
366
367         if (action != WLAN_PA_GAS_INITIAL_RESP &&
368             action != WLAN_PA_GAS_COMEBACK_RESP)
369                 return -1; /* Not a GAS response */
370
371         query = gas_query_get_pending(gas, sa, dialog_token);
372         if (query == NULL) {
373                 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
374                            " dialog token %u", MAC2STR(sa), dialog_token);
375                 return -1;
376         }
377
378         if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
379                 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
380                            MACSTR " dialog token %u when waiting for comeback "
381                            "response", MAC2STR(sa), dialog_token);
382                 return 0;
383         }
384
385         if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
386                 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
387                            MACSTR " dialog token %u when waiting for initial "
388                            "response", MAC2STR(sa), dialog_token);
389                 return 0;
390         }
391
392         query->status_code = WPA_GET_LE16(pos);
393         pos += 2;
394
395         if (query->status_code != WLAN_STATUS_SUCCESS) {
396                 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
397                            "%u failed - status code %u",
398                            MAC2STR(sa), dialog_token, query->status_code);
399                 gas_query_done(gas, query, GAS_QUERY_FAILURE);
400                 return 0;
401         }
402
403         if (action == WLAN_PA_GAS_COMEBACK_RESP) {
404                 if (pos + 1 > data + len)
405                         return 0;
406                 frag_id = *pos & 0x7f;
407                 more_frags = (*pos & 0x80) >> 7;
408                 pos++;
409         }
410
411         /* Comeback Delay */
412         if (pos + 2 > data + len)
413                 return 0;
414         comeback_delay = WPA_GET_LE16(pos);
415         pos += 2;
416
417         /* Advertisement Protocol element */
418         if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
419                 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
420                            "Protocol element in the response from " MACSTR,
421                            MAC2STR(sa));
422                 return 0;
423         }
424
425         if (*pos != WLAN_EID_ADV_PROTO) {
426                 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
427                            "Protocol element ID %u in response from " MACSTR,
428                            *pos, MAC2STR(sa));
429                 return 0;
430         }
431
432         adv_proto = pos;
433         pos += 2 + pos[1];
434
435         /* Query Response Length */
436         if (pos + 2 > data + len) {
437                 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
438                 return 0;
439         }
440         resp_len = WPA_GET_LE16(pos);
441         pos += 2;
442
443         if (pos + resp_len > data + len) {
444                 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
445                            "response from " MACSTR, MAC2STR(sa));
446                 return 0;
447         }
448
449         if (pos + resp_len < data + len) {
450                 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
451                            "after Query Response from " MACSTR,
452                            (unsigned int) (data + len - pos - resp_len),
453                            MAC2STR(sa));
454         }
455
456         if (action == WLAN_PA_GAS_COMEBACK_RESP)
457                 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
458                                       frag_id, more_frags, comeback_delay);
459         else
460                 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
461                                      comeback_delay);
462
463         return 0;
464 }
465
466
467 static void gas_query_timeout(void *eloop_data, void *user_ctx)
468 {
469         struct gas_query *gas = eloop_data;
470         struct gas_query_pending *query = user_ctx;
471
472         wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
473                    MAC2STR(query->addr));
474         gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
475 }
476
477
478 static void gas_service_timeout(void *eloop_data, void *user_ctx)
479 {
480         struct gas_query *gas = eloop_data;
481         struct wpa_supplicant *wpa_s = gas->wpa_s;
482         struct gas_query_pending *query = user_ctx;
483         int conn;
484
485         conn = wpas_wpa_is_in_progress(wpa_s, 1);
486         if (conn || wpa_s->scanning || gas->current) {
487                 wpa_printf(MSG_DEBUG, "GAS: Delaying GAS query Tx while another operation is in progress:%s%s%s",
488                            conn ? " connection" : "",
489                            wpa_s->scanning ? " scanning" : "",
490                            gas->current ? " gas_query" : "");
491                 eloop_register_timeout(
492                         GAS_SERVICE_RETRY_PERIOD_MS / 1000,
493                         (GAS_SERVICE_RETRY_PERIOD_MS % 1000) * 1000,
494                         gas_service_timeout, gas, query);
495                 return;
496         }
497
498         if (gas_query_tx(gas, query, query->req) < 0) {
499                 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
500                            MACSTR, MAC2STR(query->addr));
501                 dl_list_del(&query->list);
502                 wpabuf_free(query->req);
503                 os_free(query);
504                 return;
505         }
506         gas->current = query;
507
508         eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
509                                gas_query_timeout, gas, query);
510 }
511
512
513 static int gas_query_dialog_token_available(struct gas_query *gas,
514                                             const u8 *dst, u8 dialog_token)
515 {
516         struct gas_query_pending *q;
517         dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
518                 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
519                     dialog_token == q->dialog_token)
520                         return 0;
521         }
522
523         return 1;
524 }
525
526
527 /**
528  * gas_query_req - Request a GAS query
529  * @gas: GAS query data from gas_query_init()
530  * @dst: Destination MAC address for the query
531  * @freq: Frequency (in MHz) for the channel on which to send the query
532  * @req: GAS query payload (to be freed by gas_query module in case of success
533  *      return)
534  * @cb: Callback function for reporting GAS query result and response
535  * @ctx: Context pointer to use with the @cb call
536  * Returns: dialog token (>= 0) on success or -1 on failure
537  */
538 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
539                   struct wpabuf *req,
540                   void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
541                              enum gas_query_result result,
542                              const struct wpabuf *adv_proto,
543                              const struct wpabuf *resp, u16 status_code),
544                   void *ctx)
545 {
546         struct gas_query_pending *query;
547         int dialog_token;
548         static int next_start = 0;
549
550         if (wpabuf_len(req) < 3)
551                 return -1;
552
553         for (dialog_token = 0; dialog_token < 256; dialog_token++) {
554                 if (gas_query_dialog_token_available(
555                             gas, dst, (next_start + dialog_token) % 256))
556                         break;
557         }
558         if (dialog_token == 256)
559                 return -1; /* Too many pending queries */
560         dialog_token = (next_start + dialog_token) % 256;
561         next_start = (dialog_token + 1) % 256;
562
563         query = os_zalloc(sizeof(*query));
564         if (query == NULL)
565                 return -1;
566
567         os_memcpy(query->addr, dst, ETH_ALEN);
568         query->dialog_token = dialog_token;
569         query->freq = freq;
570         query->cb = cb;
571         query->ctx = ctx;
572         query->req = req;
573         dl_list_add(&gas->pending, &query->list);
574
575         *(wpabuf_mhead_u8(req) + 2) = dialog_token;
576
577         wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
578                    " dialog_token %u", MAC2STR(dst), dialog_token);
579
580         eloop_register_timeout(0, 0, gas_service_timeout, gas, query);
581
582         return dialog_token;
583 }
584
585
586 /**
587  * gas_query_cancel - Cancel a pending GAS query
588  * @gas: GAS query data from gas_query_init()
589  * @dst: Destination MAC address for the query
590  * @dialog_token: Dialog token from gas_query_req()
591  */
592 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
593 {
594         struct gas_query_pending *query;
595
596         query = gas_query_get_pending(gas, dst, dialog_token);
597         if (query)
598                 gas_query_done(gas, query, GAS_QUERY_CANCELLED);
599
600 }
601
602
603 int gas_query_in_progress(struct gas_query *gas)
604 {
605         return gas->current != NULL;
606 }