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