2 * Generic advertisement service (GAS) query
3 * Copyright (c) 2009, Atheros Communications
4 * Copyright (c) 2011, Qualcomm Atheros
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
13 #include "utils/eloop.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/gas.h"
16 #include "wpa_supplicant_i.h"
18 #include "offchannel.h"
19 #include "gas_query.h"
22 /** GAS query timeout in seconds */
23 #define GAS_QUERY_TIMEOUT_PERIOD 5
27 * struct gas_query_pending - Pending GAS query
29 struct gas_query_pending {
34 unsigned int wait_comeback:1;
35 unsigned int offchannel_tx_started:1;
38 struct wpabuf *adv_proto;
40 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
41 enum gas_query_result result,
42 const struct wpabuf *adv_proto,
43 const struct wpabuf *resp, u16 status_code);
48 * struct gas_query - Internal GAS query data
51 struct wpa_supplicant *wpa_s;
52 struct dl_list pending; /* struct gas_query_pending */
56 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
57 static void gas_query_timeout(void *eloop_data, void *user_ctx);
61 * gas_query_init - Initialize GAS query component
62 * @wpa_s: Pointer to wpa_supplicant data
63 * Returns: Pointer to GAS query data or %NULL on failure
65 struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
67 struct gas_query *gas;
69 gas = os_zalloc(sizeof(*gas));
74 dl_list_init(&gas->pending);
80 static void gas_query_done(struct gas_query *gas,
81 struct gas_query_pending *query,
82 enum gas_query_result result)
84 if (query->offchannel_tx_started)
85 offchannel_send_action_done(gas->wpa_s);
86 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
87 eloop_cancel_timeout(gas_query_timeout, gas, query);
88 dl_list_del(&query->list);
89 query->cb(query->ctx, query->addr, query->dialog_token, result,
90 query->adv_proto, query->resp, query->status_code);
91 wpabuf_free(query->adv_proto);
92 wpabuf_free(query->resp);
98 * gas_query_deinit - Deinitialize GAS query component
99 * @gas: GAS query data from gas_query_init()
101 void gas_query_deinit(struct gas_query *gas)
103 struct gas_query_pending *query, *next;
108 dl_list_for_each_safe(query, next, &gas->pending,
109 struct gas_query_pending, list)
110 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
116 static struct gas_query_pending *
117 gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
119 struct gas_query_pending *q;
120 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
121 if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
122 q->dialog_token == dialog_token)
129 static int gas_query_append(struct gas_query_pending *query, const u8 *data,
132 if (wpabuf_resize(&query->resp, len) < 0) {
133 wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
136 wpabuf_put_data(query->resp, data, len);
141 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
145 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
146 "freq=%d", MAC2STR(query->addr),
147 (unsigned int) wpabuf_len(req), query->freq);
148 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
149 gas->wpa_s->own_addr, query->addr,
150 wpabuf_head(req), wpabuf_len(req), 1000,
153 query->offchannel_tx_started = 1;
158 static void gas_query_tx_comeback_req(struct gas_query *gas,
159 struct gas_query_pending *query)
163 req = gas_build_comeback_req(query->dialog_token);
165 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
169 if (gas_query_tx(gas, query, req) < 0) {
170 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
171 MACSTR, MAC2STR(query->addr));
172 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
179 static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
181 struct gas_query *gas = eloop_data;
182 struct gas_query_pending *query = user_ctx;
184 wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
185 MAC2STR(query->addr));
186 gas_query_tx_comeback_req(gas, query);
190 static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
191 struct gas_query_pending *query,
194 unsigned int secs, usecs;
196 secs = (comeback_delay * 1024) / 1000000;
197 usecs = comeback_delay * 1024 - secs * 1000000;
198 wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
199 " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
200 eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
201 eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
206 static void gas_query_rx_initial(struct gas_query *gas,
207 struct gas_query_pending *query,
208 const u8 *adv_proto, const u8 *resp,
209 size_t len, u16 comeback_delay)
211 wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
212 MACSTR " (dialog_token=%u comeback_delay=%u)",
213 MAC2STR(query->addr), query->dialog_token, comeback_delay);
215 query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
216 if (query->adv_proto == NULL) {
217 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
221 if (comeback_delay) {
222 query->wait_comeback = 1;
223 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
227 /* Query was completed without comeback mechanism */
228 if (gas_query_append(query, resp, len) < 0) {
229 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
233 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
237 static void gas_query_rx_comeback(struct gas_query *gas,
238 struct gas_query_pending *query,
239 const u8 *adv_proto, const u8 *resp,
240 size_t len, u8 frag_id, u8 more_frags,
243 wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
244 MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
245 "comeback_delay=%u)",
246 MAC2STR(query->addr), query->dialog_token, frag_id,
247 more_frags, comeback_delay);
249 if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
250 os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
251 wpabuf_len(query->adv_proto)) != 0) {
252 wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
253 "between initial and comeback response from "
254 MACSTR, MAC2STR(query->addr));
255 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
259 if (comeback_delay) {
261 wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
262 "with non-zero frag_id and comeback_delay "
263 "from " MACSTR, MAC2STR(query->addr));
264 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
267 gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
271 if (frag_id != query->next_frag_id) {
272 wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
273 "from " MACSTR, MAC2STR(query->addr));
274 gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
277 query->next_frag_id++;
279 if (gas_query_append(query, resp, len) < 0) {
280 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
285 gas_query_tx_comeback_req(gas, query);
289 gas_query_done(gas, query, GAS_QUERY_SUCCESS);
294 * gas_query_rx - Indicate reception of a Public Action frame
295 * @gas: GAS query data from gas_query_init()
296 * @da: Destination MAC address of the Action frame
297 * @sa: Source MAC address of the Action frame
298 * @bssid: BSSID of the Action frame
299 * @data: Payload of the Action frame
300 * @len: Length of @data
301 * @freq: Frequency (in MHz) on which the frame was received
302 * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
304 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
305 const u8 *bssid, const u8 *data, size_t len, int freq)
307 struct gas_query_pending *query;
308 u8 action, dialog_token, frag_id = 0, more_frags = 0;
309 u16 comeback_delay, resp_len;
310 const u8 *pos, *adv_proto;
312 if (gas == NULL || len < 4)
317 dialog_token = *pos++;
319 if (action != WLAN_PA_GAS_INITIAL_RESP &&
320 action != WLAN_PA_GAS_COMEBACK_RESP)
321 return -1; /* Not a GAS response */
323 query = gas_query_get_pending(gas, sa, dialog_token);
325 wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
326 " dialog token %u", MAC2STR(sa), dialog_token);
330 if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
331 wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
332 MACSTR " dialog token %u when waiting for comeback "
333 "response", MAC2STR(sa), dialog_token);
337 if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
338 wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
339 MACSTR " dialog token %u when waiting for initial "
340 "response", MAC2STR(sa), dialog_token);
344 query->status_code = WPA_GET_LE16(pos);
347 if (query->status_code != WLAN_STATUS_SUCCESS) {
348 wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
349 "%u failed - status code %u",
350 MAC2STR(sa), dialog_token, query->status_code);
351 gas_query_done(gas, query, GAS_QUERY_FAILURE);
355 if (action == WLAN_PA_GAS_COMEBACK_RESP) {
356 if (pos + 1 > data + len)
358 frag_id = *pos & 0x7f;
359 more_frags = (*pos & 0x80) >> 7;
364 if (pos + 2 > data + len)
366 comeback_delay = WPA_GET_LE16(pos);
369 /* Advertisement Protocol element */
370 if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
371 wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
372 "Protocol element in the response from " MACSTR,
377 if (*pos != WLAN_EID_ADV_PROTO) {
378 wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
379 "Protocol element ID %u in response from " MACSTR,
387 /* Query Response Length */
388 if (pos + 2 > data + len) {
389 wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
392 resp_len = WPA_GET_LE16(pos);
395 if (pos + resp_len > data + len) {
396 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
397 "response from " MACSTR, MAC2STR(sa));
401 if (pos + resp_len < data + len) {
402 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
403 "after Query Response from " MACSTR,
404 (unsigned int) (data + len - pos - resp_len),
408 if (action == WLAN_PA_GAS_COMEBACK_RESP)
409 gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
410 frag_id, more_frags, comeback_delay);
412 gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
419 static void gas_query_timeout(void *eloop_data, void *user_ctx)
421 struct gas_query *gas = eloop_data;
422 struct gas_query_pending *query = user_ctx;
424 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR,
425 MAC2STR(query->addr));
426 gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
430 static int gas_query_dialog_token_available(struct gas_query *gas,
431 const u8 *dst, u8 dialog_token)
433 struct gas_query_pending *q;
434 dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
435 if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
436 dialog_token == q->dialog_token)
445 * gas_query_req - Request a GAS query
446 * @gas: GAS query data from gas_query_init()
447 * @dst: Destination MAC address for the query
448 * @freq: Frequency (in MHz) for the channel on which to send the query
449 * @req: GAS query payload
450 * @cb: Callback function for reporting GAS query result and response
451 * @ctx: Context pointer to use with the @cb call
452 * Returns: dialog token (>= 0) on success or -1 on failure
454 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
456 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
457 enum gas_query_result result,
458 const struct wpabuf *adv_proto,
459 const struct wpabuf *resp, u16 status_code),
462 struct gas_query_pending *query;
465 if (wpabuf_len(req) < 3)
468 for (dialog_token = 0; dialog_token < 256; dialog_token++) {
469 if (gas_query_dialog_token_available(gas, dst, dialog_token))
472 if (dialog_token == 256)
473 return -1; /* Too many pending queries */
475 query = os_zalloc(sizeof(*query));
479 os_memcpy(query->addr, dst, ETH_ALEN);
480 query->dialog_token = dialog_token;
484 dl_list_add(&gas->pending, &query->list);
486 *(wpabuf_mhead_u8(req) + 2) = dialog_token;
488 wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR
489 " dialog_token %u", MAC2STR(dst), dialog_token);
490 if (gas_query_tx(gas, query, req) < 0) {
491 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
492 MACSTR, MAC2STR(query->addr));
493 dl_list_del(&query->list);
498 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, gas_query_timeout,
506 * gas_query_cancel - Cancel a pending GAS query
507 * @gas: GAS query data from gas_query_init()
508 * @dst: Destination MAC address for the query
509 * @dialog_token: Dialog token from gas_query_req()
511 void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token)
513 struct gas_query_pending *query;
515 query = gas_query_get_pending(gas, dst, dialog_token);
517 gas_query_done(gas, query, GAS_QUERY_CANCELLED);