2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011, Qualcomm Atheros
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
19 #include "utils/eloop.h"
20 #include "common/ieee802_11_defs.h"
21 #include "common/gas.h"
22 #include "wpa_supplicant_i.h"
24 #include "offchannel.h"
25 #include "gas_query.h"
28 #define GAS_QUERY_TIMEOUT 5
31 struct gas_query_pending {
37 int offchannel_tx_started:1;
40 struct wpabuf *adv_proto;
42 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
43 enum gas_query_result result,
44 const struct wpabuf *adv_proto,
45 const struct wpabuf *resp, u16 status_code);
50 struct wpa_supplicant *wpa_s;
51 struct dl_list pending; /* struct gas_query_pending */
55 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
56 static void gas_query_timeout(void *eloop_data, void *user_ctx);
59 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
61 struct gas_query *gas;
63 gas = os_zalloc(sizeof(*gas));
68 dl_list_init(&gas->pending);
74 static void gas_query_done(struct gas_query *gas,
75 struct gas_query_pending *query,
76 enum gas_query_result result)
78 if (query->offchannel_tx_started)
79 offchannel_send_action_done(gas->wpa_s);
80 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
81 eloop_cancel_timeout(gas_query_timeout, gas, query);
82 dl_list_del(&query->list);
83 query->cb(query->ctx, query->addr, query->dialog_token, result,
84 query->adv_proto, query->resp, query->status_code);
85 wpabuf_free(query->adv_proto);
86 wpabuf_free(query->resp);
91 void gas_query_deinit(struct gas_query *gas)
93 struct gas_query_pending *query, *next;
98 dl_list_for_each_safe(query, next, &gas->pending,
99 struct gas_query_pending, list)
100 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
106 static struct gas_query_pending *
107 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
109 struct gas_query_pending *q;
110 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
111 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
112 q->dialog_token == dialog_token)
119 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
122 if (wpabuf_resize(&query->resp, len) < 0) {
123 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
126 wpabuf_put_data(query->resp, data, len);
131 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
135 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
136 "freq=%d", MAC2STR(query->addr),
137 (unsigned int) wpabuf_len(req), query->freq);
138 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
139 gas->wpa_s->own_addr, query->addr,
140 wpabuf_head(req), wpabuf_len(req), 1000,
143 query->offchannel_tx_started = 1;
148 static void gas_query_tx_comeback_req(struct gas_query *gas,
149 struct gas_query_pending *query)
153 req = gas_build_comeback_req(query->dialog_token);
155 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
159 if (gas_query_tx(gas, query, req) < 0) {
160 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
161 MACSTR, MAC2STR(query->addr));
162 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
169 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
171 struct gas_query *gas = eloop_data;
172 struct gas_query_pending *query = user_ctx;
174 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
175 MAC2STR(query->addr));
176 gas_query_tx_comeback_req(gas, query);
180 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
181 struct gas_query_pending *query,
184 unsigned int secs, usecs;
186 secs = (comeback_delay * 1024) / 1000000;
187 usecs = comeback_delay * 1024 - secs * 1000000;
188 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
189 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
190 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
191 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
196 static void gas_query_rx_initial(struct gas_query *gas,
197 struct gas_query_pending *query,
198 const u8 *adv_proto, const u8 *resp,
199 size_t len, u16 comeback_delay)
201 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
202 MACSTR " (dialog_token=%u comeback_delay=%u)",
203 MAC2STR(query->addr), query->dialog_token, comeback_delay);
205 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
206 if (query->adv_proto == NULL) {
207 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
211 if (comeback_delay) {
212 query->wait_comeback = 1;
213 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
217 /* Query was completed without comeback mechanism */
218 if (gas_query_append(query, resp, len) < 0) {
219 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
223 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
227 static void gas_query_rx_comeback(struct gas_query *gas,
228 struct gas_query_pending *query,
229 const u8 *adv_proto, const u8 *resp,
230 size_t len, u8 frag_id, u8 more_frags,
233 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
234 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
235 "comeback_delay=%u)",
236 MAC2STR(query->addr), query->dialog_token, frag_id,
237 more_frags, comeback_delay);
239 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
240 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
241 wpabuf_len(query->adv_proto)) != 0) {
242 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
243 "between initial and comeback response from "
244 MACSTR, MAC2STR(query->addr));
245 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
249 if (comeback_delay) {
251 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
252 "with non-zero frag_id and comeback_delay "
253 "from " MACSTR, MAC2STR(query->addr));
254 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
257 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
261 if (frag_id != query->next_frag_id) {
262 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
263 "from " MACSTR, MAC2STR(query->addr));
264 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
267 query->next_frag_id++;
269 if (gas_query_append(query, resp, len) < 0) {
270 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
275 gas_query_tx_comeback_req(gas, query);
279 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
283 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
284 const u8 *bssid, const u8 *data, size_t len, int freq)
286 struct gas_query_pending *query;
287 u8 action, dialog_token, frag_id = 0, more_frags = 0;
288 u16 comeback_delay, resp_len;
289 const u8 *pos, *adv_proto;
291 if (gas == NULL || len < 4)
296 dialog_token = *pos++;
298 if (action != WLAN_PA_GAS_INITIAL_RESP &&
299 action != WLAN_PA_GAS_COMEBACK_RESP)
300 return -1; /* Not a GAS response */
302 query = gas_query_get_pending(gas, sa, dialog_token);
304 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
305 " dialog token %u", MAC2STR(sa), dialog_token);
309 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
310 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
311 MACSTR " dialog token %u when waiting for comeback "
312 "response", MAC2STR(sa), dialog_token);
316 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
317 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
318 MACSTR " dialog token %u when waiting for initial "
319 "response", MAC2STR(sa), dialog_token);
323 query->status_code = WPA_GET_LE16(pos);
326 if (query->status_code != WLAN_STATUS_SUCCESS) {
327 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
328 "%u failed - status code %u",
329 MAC2STR(sa), dialog_token, query->status_code);
330 gas_query_done(gas, query, GAS_QUERY_FAILURE);
334 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
335 if (pos + 1 > data + len)
337 frag_id = *pos & 0x7f;
338 more_frags = (*pos & 0x80) >> 7;
343 if (pos + 2 > data + len)
345 comeback_delay = WPA_GET_LE16(pos);
348 /* Advertisement Protocol element */
349 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
350 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
351 "Protocol element in the response from " MACSTR,
356 if (*pos != WLAN_EID_ADV_PROTO) {
357 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
358 "Protocol element ID %u in response from " MACSTR,
366 /* Query Response Length */
367 if (pos + 2 > data + len) {
368 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
371 resp_len = WPA_GET_LE16(pos);
374 if (pos + resp_len > data + len) {
375 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
376 "response from " MACSTR, MAC2STR(sa));
380 if (pos + resp_len < data + len) {
381 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
382 "after Query Response from " MACSTR,
383 (unsigned int) (data + len - pos - resp_len),
387 if (action == WLAN_PA_GAS_COMEBACK_RESP)
388 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
389 frag_id, more_frags, comeback_delay);
391 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
398 static void gas_query_timeout(void *eloop_data, void *user_ctx)
400 struct gas_query *gas = eloop_data;
401 struct gas_query_pending *query = user_ctx;
403 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
404 MAC2STR(query->addr));
405 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
409 static int gas_query_dialog_token_available(struct gas_query *gas,
410 const u8 *dst, u8 dialog_token)
412 struct gas_query_pending *q;
413 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
414 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
415 dialog_token == q->dialog_token)
423 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
425 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
426 enum gas_query_result result,
427 const struct wpabuf *adv_proto,
428 const struct wpabuf *resp, u16 status_code),
431 struct gas_query_pending *query;
434 if (wpabuf_len(req) < 3)
437 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
438 if (gas_query_dialog_token_available(gas, dst, dialog_token))
441 if (dialog_token == 256)
442 return -1; /* Too many pending queries */
444 query = os_zalloc(sizeof(*query));
448 os_memcpy(query->addr, dst, ETH_ALEN);
449 query->dialog_token = dialog_token;
453 dl_list_add(&gas->pending, &query->list);
455 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
457 wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
458 " dialog_token %u", MAC2STR(dst), dialog_token);
459 if (gas_query_tx(gas, query, req) < 0) {
460 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
461 MACSTR, MAC2STR(query->addr));
466 eloop_register_timeout(GAS_QUERY_TIMEOUT, 0, gas_query_timeout,
473 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
475 struct gas_query_pending *query;
477 query = gas_query_get_pending(gas, dst, dialog_token);
479 gas_query_done(gas, query, GAS_QUERY_CANCELLED);