Home | History | Annotate | Line # | Download | only in wpa_supplicant
gas_query.c revision 1.1.1.2.4.1.6.1
      1              1.1  christos /*
      2              1.1  christos  * Generic advertisement service (GAS) query
      3              1.1  christos  * Copyright (c) 2009, Atheros Communications
      4      1.1.1.2.4.1       snj  * Copyright (c) 2011-2014, Qualcomm Atheros, Inc.
      5      1.1.1.2.4.1       snj  * Copyright (c) 2011-2014, Jouni Malinen <j (at) w1.fi>
      6              1.1  christos  *
      7          1.1.1.2  christos  * This software may be distributed under the terms of the BSD license.
      8          1.1.1.2  christos  * See README for more details.
      9              1.1  christos  */
     10              1.1  christos 
     11              1.1  christos #include "includes.h"
     12              1.1  christos 
     13              1.1  christos #include "common.h"
     14              1.1  christos #include "utils/eloop.h"
     15              1.1  christos #include "common/ieee802_11_defs.h"
     16              1.1  christos #include "common/gas.h"
     17      1.1.1.2.4.1       snj #include "common/wpa_ctrl.h"
     18      1.1.1.2.4.1       snj #include "rsn_supp/wpa.h"
     19              1.1  christos #include "wpa_supplicant_i.h"
     20  1.1.1.2.4.1.6.1       snj #include "config.h"
     21              1.1  christos #include "driver_i.h"
     22              1.1  christos #include "offchannel.h"
     23              1.1  christos #include "gas_query.h"
     24              1.1  christos 
     25              1.1  christos 
     26          1.1.1.2  christos /** GAS query timeout in seconds */
     27      1.1.1.2.4.1       snj #define GAS_QUERY_TIMEOUT_PERIOD 2
     28              1.1  christos 
     29  1.1.1.2.4.1.6.1       snj /* GAS query wait-time / duration in ms */
     30  1.1.1.2.4.1.6.1       snj #define GAS_QUERY_WAIT_TIME_INITIAL 1000
     31  1.1.1.2.4.1.6.1       snj #define GAS_QUERY_WAIT_TIME_COMEBACK 150
     32              1.1  christos 
     33          1.1.1.2  christos /**
     34          1.1.1.2  christos  * struct gas_query_pending - Pending GAS query
     35          1.1.1.2  christos  */
     36              1.1  christos struct gas_query_pending {
     37              1.1  christos 	struct dl_list list;
     38      1.1.1.2.4.1       snj 	struct gas_query *gas;
     39              1.1  christos 	u8 addr[ETH_ALEN];
     40              1.1  christos 	u8 dialog_token;
     41              1.1  christos 	u8 next_frag_id;
     42              1.1  christos 	unsigned int wait_comeback:1;
     43              1.1  christos 	unsigned int offchannel_tx_started:1;
     44  1.1.1.2.4.1.6.1       snj 	unsigned int retry:1;
     45              1.1  christos 	int freq;
     46              1.1  christos 	u16 status_code;
     47      1.1.1.2.4.1       snj 	struct wpabuf *req;
     48              1.1  christos 	struct wpabuf *adv_proto;
     49              1.1  christos 	struct wpabuf *resp;
     50      1.1.1.2.4.1       snj 	struct os_reltime last_oper;
     51              1.1  christos 	void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
     52              1.1  christos 		   enum gas_query_result result,
     53              1.1  christos 		   const struct wpabuf *adv_proto,
     54              1.1  christos 		   const struct wpabuf *resp, u16 status_code);
     55              1.1  christos 	void *ctx;
     56              1.1  christos };
     57              1.1  christos 
     58          1.1.1.2  christos /**
     59          1.1.1.2  christos  * struct gas_query - Internal GAS query data
     60          1.1.1.2  christos  */
     61              1.1  christos struct gas_query {
     62              1.1  christos 	struct wpa_supplicant *wpa_s;
     63              1.1  christos 	struct dl_list pending; /* struct gas_query_pending */
     64      1.1.1.2.4.1       snj 	struct gas_query_pending *current;
     65      1.1.1.2.4.1       snj 	struct wpa_radio_work *work;
     66              1.1  christos };
     67              1.1  christos 
     68              1.1  christos 
     69              1.1  christos static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx);
     70              1.1  christos static void gas_query_timeout(void *eloop_data, void *user_ctx);
     71  1.1.1.2.4.1.6.1       snj static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx);
     72  1.1.1.2.4.1.6.1       snj static void gas_query_tx_initial_req(struct gas_query *gas,
     73  1.1.1.2.4.1.6.1       snj 				     struct gas_query_pending *query);
     74  1.1.1.2.4.1.6.1       snj static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst);
     75              1.1  christos 
     76              1.1  christos 
     77      1.1.1.2.4.1       snj static int ms_from_time(struct os_reltime *last)
     78      1.1.1.2.4.1       snj {
     79      1.1.1.2.4.1       snj 	struct os_reltime now, res;
     80      1.1.1.2.4.1       snj 
     81      1.1.1.2.4.1       snj 	os_get_reltime(&now);
     82      1.1.1.2.4.1       snj 	os_reltime_sub(&now, last, &res);
     83      1.1.1.2.4.1       snj 	return res.sec * 1000 + res.usec / 1000;
     84      1.1.1.2.4.1       snj }
     85      1.1.1.2.4.1       snj 
     86      1.1.1.2.4.1       snj 
     87          1.1.1.2  christos /**
     88          1.1.1.2  christos  * gas_query_init - Initialize GAS query component
     89          1.1.1.2  christos  * @wpa_s: Pointer to wpa_supplicant data
     90          1.1.1.2  christos  * Returns: Pointer to GAS query data or %NULL on failure
     91          1.1.1.2  christos  */
     92              1.1  christos struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s)
     93              1.1  christos {
     94              1.1  christos 	struct gas_query *gas;
     95              1.1  christos 
     96              1.1  christos 	gas = os_zalloc(sizeof(*gas));
     97              1.1  christos 	if (gas == NULL)
     98              1.1  christos 		return NULL;
     99              1.1  christos 
    100              1.1  christos 	gas->wpa_s = wpa_s;
    101              1.1  christos 	dl_list_init(&gas->pending);
    102              1.1  christos 
    103              1.1  christos 	return gas;
    104              1.1  christos }
    105              1.1  christos 
    106              1.1  christos 
    107      1.1.1.2.4.1       snj static const char * gas_result_txt(enum gas_query_result result)
    108      1.1.1.2.4.1       snj {
    109      1.1.1.2.4.1       snj 	switch (result) {
    110      1.1.1.2.4.1       snj 	case GAS_QUERY_SUCCESS:
    111      1.1.1.2.4.1       snj 		return "SUCCESS";
    112      1.1.1.2.4.1       snj 	case GAS_QUERY_FAILURE:
    113      1.1.1.2.4.1       snj 		return "FAILURE";
    114      1.1.1.2.4.1       snj 	case GAS_QUERY_TIMEOUT:
    115      1.1.1.2.4.1       snj 		return "TIMEOUT";
    116      1.1.1.2.4.1       snj 	case GAS_QUERY_PEER_ERROR:
    117      1.1.1.2.4.1       snj 		return "PEER_ERROR";
    118      1.1.1.2.4.1       snj 	case GAS_QUERY_INTERNAL_ERROR:
    119      1.1.1.2.4.1       snj 		return "INTERNAL_ERROR";
    120      1.1.1.2.4.1       snj 	case GAS_QUERY_DELETED_AT_DEINIT:
    121      1.1.1.2.4.1       snj 		return "DELETED_AT_DEINIT";
    122      1.1.1.2.4.1       snj 	}
    123      1.1.1.2.4.1       snj 
    124      1.1.1.2.4.1       snj 	return "N/A";
    125      1.1.1.2.4.1       snj }
    126      1.1.1.2.4.1       snj 
    127      1.1.1.2.4.1       snj 
    128      1.1.1.2.4.1       snj static void gas_query_free(struct gas_query_pending *query, int del_list)
    129      1.1.1.2.4.1       snj {
    130      1.1.1.2.4.1       snj 	struct gas_query *gas = query->gas;
    131      1.1.1.2.4.1       snj 
    132      1.1.1.2.4.1       snj 	if (del_list)
    133      1.1.1.2.4.1       snj 		dl_list_del(&query->list);
    134      1.1.1.2.4.1       snj 
    135      1.1.1.2.4.1       snj 	if (gas->work && gas->work->ctx == query) {
    136      1.1.1.2.4.1       snj 		radio_work_done(gas->work);
    137      1.1.1.2.4.1       snj 		gas->work = NULL;
    138      1.1.1.2.4.1       snj 	}
    139      1.1.1.2.4.1       snj 
    140      1.1.1.2.4.1       snj 	wpabuf_free(query->req);
    141      1.1.1.2.4.1       snj 	wpabuf_free(query->adv_proto);
    142      1.1.1.2.4.1       snj 	wpabuf_free(query->resp);
    143      1.1.1.2.4.1       snj 	os_free(query);
    144      1.1.1.2.4.1       snj }
    145      1.1.1.2.4.1       snj 
    146      1.1.1.2.4.1       snj 
    147              1.1  christos static void gas_query_done(struct gas_query *gas,
    148              1.1  christos 			   struct gas_query_pending *query,
    149              1.1  christos 			   enum gas_query_result result)
    150              1.1  christos {
    151      1.1.1.2.4.1       snj 	wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR
    152      1.1.1.2.4.1       snj 		" dialog_token=%u freq=%d status_code=%u result=%s",
    153      1.1.1.2.4.1       snj 		MAC2STR(query->addr), query->dialog_token, query->freq,
    154      1.1.1.2.4.1       snj 		query->status_code, gas_result_txt(result));
    155      1.1.1.2.4.1       snj 	if (gas->current == query)
    156      1.1.1.2.4.1       snj 		gas->current = NULL;
    157              1.1  christos 	if (query->offchannel_tx_started)
    158              1.1  christos 		offchannel_send_action_done(gas->wpa_s);
    159              1.1  christos 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
    160              1.1  christos 	eloop_cancel_timeout(gas_query_timeout, gas, query);
    161  1.1.1.2.4.1.6.1       snj 	eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
    162              1.1  christos 	dl_list_del(&query->list);
    163              1.1  christos 	query->cb(query->ctx, query->addr, query->dialog_token, result,
    164              1.1  christos 		  query->adv_proto, query->resp, query->status_code);
    165      1.1.1.2.4.1       snj 	gas_query_free(query, 0);
    166              1.1  christos }
    167              1.1  christos 
    168              1.1  christos 
    169          1.1.1.2  christos /**
    170          1.1.1.2  christos  * gas_query_deinit - Deinitialize GAS query component
    171          1.1.1.2  christos  * @gas: GAS query data from gas_query_init()
    172          1.1.1.2  christos  */
    173              1.1  christos void gas_query_deinit(struct gas_query *gas)
    174              1.1  christos {
    175              1.1  christos 	struct gas_query_pending *query, *next;
    176              1.1  christos 
    177              1.1  christos 	if (gas == NULL)
    178              1.1  christos 		return;
    179              1.1  christos 
    180              1.1  christos 	dl_list_for_each_safe(query, next, &gas->pending,
    181              1.1  christos 			      struct gas_query_pending, list)
    182              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
    183              1.1  christos 
    184              1.1  christos 	os_free(gas);
    185              1.1  christos }
    186              1.1  christos 
    187              1.1  christos 
    188              1.1  christos static struct gas_query_pending *
    189              1.1  christos gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token)
    190              1.1  christos {
    191              1.1  christos 	struct gas_query_pending *q;
    192              1.1  christos 	dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
    193              1.1  christos 		if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 &&
    194              1.1  christos 		    q->dialog_token == dialog_token)
    195              1.1  christos 			return q;
    196              1.1  christos 	}
    197              1.1  christos 	return NULL;
    198              1.1  christos }
    199              1.1  christos 
    200              1.1  christos 
    201              1.1  christos static int gas_query_append(struct gas_query_pending *query, const u8 *data,
    202              1.1  christos 			    size_t len)
    203              1.1  christos {
    204              1.1  christos 	if (wpabuf_resize(&query->resp, len) < 0) {
    205              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: No memory to store the response");
    206              1.1  christos 		return -1;
    207              1.1  christos 	}
    208              1.1  christos 	wpabuf_put_data(query->resp, data, len);
    209              1.1  christos 	return 0;
    210              1.1  christos }
    211              1.1  christos 
    212              1.1  christos 
    213      1.1.1.2.4.1       snj static void gas_query_tx_status(struct wpa_supplicant *wpa_s,
    214      1.1.1.2.4.1       snj 				unsigned int freq, const u8 *dst,
    215      1.1.1.2.4.1       snj 				const u8 *src, const u8 *bssid,
    216      1.1.1.2.4.1       snj 				const u8 *data, size_t data_len,
    217      1.1.1.2.4.1       snj 				enum offchannel_send_action_result result)
    218      1.1.1.2.4.1       snj {
    219      1.1.1.2.4.1       snj 	struct gas_query_pending *query;
    220      1.1.1.2.4.1       snj 	struct gas_query *gas = wpa_s->gas;
    221      1.1.1.2.4.1       snj 	int dur;
    222      1.1.1.2.4.1       snj 
    223      1.1.1.2.4.1       snj 	if (gas->current == NULL) {
    224      1.1.1.2.4.1       snj 		wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst="
    225      1.1.1.2.4.1       snj 			   MACSTR " result=%d - no query in progress",
    226      1.1.1.2.4.1       snj 			   freq, MAC2STR(dst), result);
    227      1.1.1.2.4.1       snj 		return;
    228      1.1.1.2.4.1       snj 	}
    229      1.1.1.2.4.1       snj 
    230      1.1.1.2.4.1       snj 	query = gas->current;
    231      1.1.1.2.4.1       snj 
    232      1.1.1.2.4.1       snj 	dur = ms_from_time(&query->last_oper);
    233      1.1.1.2.4.1       snj 	wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR
    234      1.1.1.2.4.1       snj 		   " result=%d query=%p dialog_token=%u dur=%d ms",
    235      1.1.1.2.4.1       snj 		   freq, MAC2STR(dst), result, query, query->dialog_token, dur);
    236      1.1.1.2.4.1       snj 	if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) {
    237      1.1.1.2.4.1       snj 		wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination");
    238      1.1.1.2.4.1       snj 		return;
    239      1.1.1.2.4.1       snj 	}
    240      1.1.1.2.4.1       snj 	os_get_reltime(&query->last_oper);
    241      1.1.1.2.4.1       snj 
    242      1.1.1.2.4.1       snj 	if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) {
    243      1.1.1.2.4.1       snj 		eloop_cancel_timeout(gas_query_timeout, gas, query);
    244      1.1.1.2.4.1       snj 		eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
    245      1.1.1.2.4.1       snj 				       gas_query_timeout, gas, query);
    246  1.1.1.2.4.1.6.1       snj 		if (query->wait_comeback && !query->retry) {
    247  1.1.1.2.4.1.6.1       snj 			eloop_cancel_timeout(gas_query_rx_comeback_timeout,
    248  1.1.1.2.4.1.6.1       snj 					     gas, query);
    249  1.1.1.2.4.1.6.1       snj 			eloop_register_timeout(
    250  1.1.1.2.4.1.6.1       snj 				0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000,
    251  1.1.1.2.4.1.6.1       snj 				gas_query_rx_comeback_timeout, gas, query);
    252  1.1.1.2.4.1.6.1       snj 		}
    253      1.1.1.2.4.1       snj 	}
    254      1.1.1.2.4.1       snj 	if (result == OFFCHANNEL_SEND_ACTION_FAILED) {
    255      1.1.1.2.4.1       snj 		eloop_cancel_timeout(gas_query_timeout, gas, query);
    256      1.1.1.2.4.1       snj 		eloop_register_timeout(0, 0, gas_query_timeout, gas, query);
    257      1.1.1.2.4.1       snj 	}
    258      1.1.1.2.4.1       snj }
    259      1.1.1.2.4.1       snj 
    260      1.1.1.2.4.1       snj 
    261      1.1.1.2.4.1       snj static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr)
    262      1.1.1.2.4.1       snj {
    263      1.1.1.2.4.1       snj 	if (wpa_s->current_ssid == NULL ||
    264      1.1.1.2.4.1       snj 	    wpa_s->wpa_state < WPA_4WAY_HANDSHAKE ||
    265      1.1.1.2.4.1       snj 	    os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0)
    266      1.1.1.2.4.1       snj 		return 0;
    267      1.1.1.2.4.1       snj 	return wpa_sm_pmf_enabled(wpa_s->wpa);
    268      1.1.1.2.4.1       snj }
    269      1.1.1.2.4.1       snj 
    270      1.1.1.2.4.1       snj 
    271              1.1  christos static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query,
    272  1.1.1.2.4.1.6.1       snj 			struct wpabuf *req, unsigned int wait_time)
    273              1.1  christos {
    274      1.1.1.2.4.1       snj 	int res, prot = pmf_in_use(gas->wpa_s, query->addr);
    275  1.1.1.2.4.1.6.1       snj 	const u8 *bssid;
    276  1.1.1.2.4.1.6.1       snj 	const u8 wildcard_bssid[ETH_ALEN] = {
    277  1.1.1.2.4.1.6.1       snj 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    278  1.1.1.2.4.1.6.1       snj 	};
    279      1.1.1.2.4.1       snj 
    280              1.1  christos 	wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u "
    281      1.1.1.2.4.1       snj 		   "freq=%d prot=%d", MAC2STR(query->addr),
    282      1.1.1.2.4.1       snj 		   (unsigned int) wpabuf_len(req), query->freq, prot);
    283      1.1.1.2.4.1       snj 	if (prot) {
    284      1.1.1.2.4.1       snj 		u8 *categ = wpabuf_mhead_u8(req);
    285      1.1.1.2.4.1       snj 		*categ = WLAN_ACTION_PROTECTED_DUAL;
    286      1.1.1.2.4.1       snj 	}
    287      1.1.1.2.4.1       snj 	os_get_reltime(&query->last_oper);
    288      1.1.1.2.4.1       snj 	if (gas->wpa_s->max_remain_on_chan &&
    289      1.1.1.2.4.1       snj 	    wait_time > gas->wpa_s->max_remain_on_chan)
    290      1.1.1.2.4.1       snj 		wait_time = gas->wpa_s->max_remain_on_chan;
    291  1.1.1.2.4.1.6.1       snj 	if (!gas->wpa_s->conf->gas_address3 ||
    292  1.1.1.2.4.1.6.1       snj 	    (gas->wpa_s->current_ssid &&
    293  1.1.1.2.4.1.6.1       snj 	     gas->wpa_s->wpa_state >= WPA_ASSOCIATED &&
    294  1.1.1.2.4.1.6.1       snj 	     os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0))
    295  1.1.1.2.4.1.6.1       snj 		bssid = query->addr;
    296  1.1.1.2.4.1.6.1       snj 	else
    297  1.1.1.2.4.1.6.1       snj 		bssid = wildcard_bssid;
    298              1.1  christos 	res = offchannel_send_action(gas->wpa_s, query->freq, query->addr,
    299  1.1.1.2.4.1.6.1       snj 				     gas->wpa_s->own_addr, bssid,
    300      1.1.1.2.4.1       snj 				     wpabuf_head(req), wpabuf_len(req),
    301      1.1.1.2.4.1       snj 				     wait_time, gas_query_tx_status, 0);
    302              1.1  christos 	if (res == 0)
    303              1.1  christos 		query->offchannel_tx_started = 1;
    304              1.1  christos 	return res;
    305              1.1  christos }
    306              1.1  christos 
    307              1.1  christos 
    308              1.1  christos static void gas_query_tx_comeback_req(struct gas_query *gas,
    309              1.1  christos 				      struct gas_query_pending *query)
    310              1.1  christos {
    311              1.1  christos 	struct wpabuf *req;
    312  1.1.1.2.4.1.6.1       snj 	unsigned int wait_time;
    313              1.1  christos 
    314              1.1  christos 	req = gas_build_comeback_req(query->dialog_token);
    315              1.1  christos 	if (req == NULL) {
    316              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
    317              1.1  christos 		return;
    318              1.1  christos 	}
    319              1.1  christos 
    320  1.1.1.2.4.1.6.1       snj 	wait_time = (query->retry || !query->offchannel_tx_started) ?
    321  1.1.1.2.4.1.6.1       snj 		GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK;
    322  1.1.1.2.4.1.6.1       snj 
    323  1.1.1.2.4.1.6.1       snj 	if (gas_query_tx(gas, query, req, wait_time) < 0) {
    324              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
    325              1.1  christos 			   MACSTR, MAC2STR(query->addr));
    326              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
    327              1.1  christos 	}
    328              1.1  christos 
    329              1.1  christos 	wpabuf_free(req);
    330              1.1  christos }
    331              1.1  christos 
    332              1.1  christos 
    333  1.1.1.2.4.1.6.1       snj static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx)
    334  1.1.1.2.4.1.6.1       snj {
    335  1.1.1.2.4.1.6.1       snj 	struct gas_query *gas = eloop_data;
    336  1.1.1.2.4.1.6.1       snj 	struct gas_query_pending *query = user_ctx;
    337  1.1.1.2.4.1.6.1       snj 	int dialog_token;
    338  1.1.1.2.4.1.6.1       snj 
    339  1.1.1.2.4.1.6.1       snj 	wpa_printf(MSG_DEBUG,
    340  1.1.1.2.4.1.6.1       snj 		   "GAS: No response to comeback request received (retry=%u)",
    341  1.1.1.2.4.1.6.1       snj 		   query->retry);
    342  1.1.1.2.4.1.6.1       snj 	if (gas->current != query || query->retry)
    343  1.1.1.2.4.1.6.1       snj 		return;
    344  1.1.1.2.4.1.6.1       snj 	dialog_token = gas_query_new_dialog_token(gas, query->addr);
    345  1.1.1.2.4.1.6.1       snj 	if (dialog_token < 0)
    346  1.1.1.2.4.1.6.1       snj 		return;
    347  1.1.1.2.4.1.6.1       snj 	wpa_printf(MSG_DEBUG,
    348  1.1.1.2.4.1.6.1       snj 		   "GAS: Retry GAS query due to comeback response timeout");
    349  1.1.1.2.4.1.6.1       snj 	query->retry = 1;
    350  1.1.1.2.4.1.6.1       snj 	query->dialog_token = dialog_token;
    351  1.1.1.2.4.1.6.1       snj 	*(wpabuf_mhead_u8(query->req) + 2) = dialog_token;
    352  1.1.1.2.4.1.6.1       snj 	query->wait_comeback = 0;
    353  1.1.1.2.4.1.6.1       snj 	query->next_frag_id = 0;
    354  1.1.1.2.4.1.6.1       snj 	wpabuf_free(query->adv_proto);
    355  1.1.1.2.4.1.6.1       snj 	query->adv_proto = NULL;
    356  1.1.1.2.4.1.6.1       snj 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
    357  1.1.1.2.4.1.6.1       snj 	eloop_cancel_timeout(gas_query_timeout, gas, query);
    358  1.1.1.2.4.1.6.1       snj 	gas_query_tx_initial_req(gas, query);
    359  1.1.1.2.4.1.6.1       snj }
    360  1.1.1.2.4.1.6.1       snj 
    361  1.1.1.2.4.1.6.1       snj 
    362              1.1  christos static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx)
    363              1.1  christos {
    364              1.1  christos 	struct gas_query *gas = eloop_data;
    365              1.1  christos 	struct gas_query_pending *query = user_ctx;
    366              1.1  christos 
    367              1.1  christos 	wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR,
    368              1.1  christos 		   MAC2STR(query->addr));
    369              1.1  christos 	gas_query_tx_comeback_req(gas, query);
    370              1.1  christos }
    371              1.1  christos 
    372              1.1  christos 
    373              1.1  christos static void gas_query_tx_comeback_req_delay(struct gas_query *gas,
    374              1.1  christos 					    struct gas_query_pending *query,
    375              1.1  christos 					    u16 comeback_delay)
    376              1.1  christos {
    377              1.1  christos 	unsigned int secs, usecs;
    378              1.1  christos 
    379  1.1.1.2.4.1.6.1       snj 	if (comeback_delay > 1 && query->offchannel_tx_started) {
    380  1.1.1.2.4.1.6.1       snj 		offchannel_send_action_done(gas->wpa_s);
    381  1.1.1.2.4.1.6.1       snj 		query->offchannel_tx_started = 0;
    382  1.1.1.2.4.1.6.1       snj 	}
    383  1.1.1.2.4.1.6.1       snj 
    384              1.1  christos 	secs = (comeback_delay * 1024) / 1000000;
    385              1.1  christos 	usecs = comeback_delay * 1024 - secs * 1000000;
    386              1.1  christos 	wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR
    387              1.1  christos 		   " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs);
    388              1.1  christos 	eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query);
    389              1.1  christos 	eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout,
    390              1.1  christos 			       gas, query);
    391              1.1  christos }
    392              1.1  christos 
    393              1.1  christos 
    394              1.1  christos static void gas_query_rx_initial(struct gas_query *gas,
    395              1.1  christos 				 struct gas_query_pending *query,
    396              1.1  christos 				 const u8 *adv_proto, const u8 *resp,
    397              1.1  christos 				 size_t len, u16 comeback_delay)
    398              1.1  christos {
    399              1.1  christos 	wpa_printf(MSG_DEBUG, "GAS: Received initial response from "
    400              1.1  christos 		   MACSTR " (dialog_token=%u comeback_delay=%u)",
    401              1.1  christos 		   MAC2STR(query->addr), query->dialog_token, comeback_delay);
    402              1.1  christos 
    403              1.1  christos 	query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]);
    404              1.1  christos 	if (query->adv_proto == NULL) {
    405              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
    406              1.1  christos 		return;
    407              1.1  christos 	}
    408              1.1  christos 
    409              1.1  christos 	if (comeback_delay) {
    410              1.1  christos 		query->wait_comeback = 1;
    411              1.1  christos 		gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
    412              1.1  christos 		return;
    413              1.1  christos 	}
    414              1.1  christos 
    415              1.1  christos 	/* Query was completed without comeback mechanism */
    416              1.1  christos 	if (gas_query_append(query, resp, len) < 0) {
    417              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
    418              1.1  christos 		return;
    419              1.1  christos 	}
    420              1.1  christos 
    421              1.1  christos 	gas_query_done(gas, query, GAS_QUERY_SUCCESS);
    422              1.1  christos }
    423              1.1  christos 
    424              1.1  christos 
    425              1.1  christos static void gas_query_rx_comeback(struct gas_query *gas,
    426              1.1  christos 				  struct gas_query_pending *query,
    427              1.1  christos 				  const u8 *adv_proto, const u8 *resp,
    428              1.1  christos 				  size_t len, u8 frag_id, u8 more_frags,
    429              1.1  christos 				  u16 comeback_delay)
    430              1.1  christos {
    431              1.1  christos 	wpa_printf(MSG_DEBUG, "GAS: Received comeback response from "
    432              1.1  christos 		   MACSTR " (dialog_token=%u frag_id=%u more_frags=%u "
    433              1.1  christos 		   "comeback_delay=%u)",
    434              1.1  christos 		   MAC2STR(query->addr), query->dialog_token, frag_id,
    435              1.1  christos 		   more_frags, comeback_delay);
    436  1.1.1.2.4.1.6.1       snj 	eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query);
    437              1.1  christos 
    438              1.1  christos 	if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) ||
    439              1.1  christos 	    os_memcmp(adv_proto, wpabuf_head(query->adv_proto),
    440              1.1  christos 		      wpabuf_len(query->adv_proto)) != 0) {
    441              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed "
    442              1.1  christos 			   "between initial and comeback response from "
    443              1.1  christos 			   MACSTR, MAC2STR(query->addr));
    444              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
    445              1.1  christos 		return;
    446              1.1  christos 	}
    447              1.1  christos 
    448              1.1  christos 	if (comeback_delay) {
    449              1.1  christos 		if (frag_id) {
    450              1.1  christos 			wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response "
    451              1.1  christos 				   "with non-zero frag_id and comeback_delay "
    452              1.1  christos 				   "from " MACSTR, MAC2STR(query->addr));
    453              1.1  christos 			gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
    454              1.1  christos 			return;
    455              1.1  christos 		}
    456              1.1  christos 		gas_query_tx_comeback_req_delay(gas, query, comeback_delay);
    457              1.1  christos 		return;
    458              1.1  christos 	}
    459              1.1  christos 
    460              1.1  christos 	if (frag_id != query->next_frag_id) {
    461              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response "
    462              1.1  christos 			   "from " MACSTR, MAC2STR(query->addr));
    463      1.1.1.2.4.1       snj 		if (frag_id + 1 == query->next_frag_id) {
    464      1.1.1.2.4.1       snj 			wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible "
    465      1.1.1.2.4.1       snj 				   "retry of previous fragment");
    466      1.1.1.2.4.1       snj 			return;
    467      1.1.1.2.4.1       snj 		}
    468              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_PEER_ERROR);
    469              1.1  christos 		return;
    470              1.1  christos 	}
    471              1.1  christos 	query->next_frag_id++;
    472              1.1  christos 
    473              1.1  christos 	if (gas_query_append(query, resp, len) < 0) {
    474              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
    475              1.1  christos 		return;
    476              1.1  christos 	}
    477              1.1  christos 
    478              1.1  christos 	if (more_frags) {
    479              1.1  christos 		gas_query_tx_comeback_req(gas, query);
    480              1.1  christos 		return;
    481              1.1  christos 	}
    482              1.1  christos 
    483              1.1  christos 	gas_query_done(gas, query, GAS_QUERY_SUCCESS);
    484              1.1  christos }
    485              1.1  christos 
    486              1.1  christos 
    487          1.1.1.2  christos /**
    488      1.1.1.2.4.1       snj  * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame
    489          1.1.1.2  christos  * @gas: GAS query data from gas_query_init()
    490          1.1.1.2  christos  * @da: Destination MAC address of the Action frame
    491          1.1.1.2  christos  * @sa: Source MAC address of the Action frame
    492          1.1.1.2  christos  * @bssid: BSSID of the Action frame
    493      1.1.1.2.4.1       snj  * @categ: Category of the Action frame
    494          1.1.1.2  christos  * @data: Payload of the Action frame
    495          1.1.1.2  christos  * @len: Length of @data
    496          1.1.1.2  christos  * @freq: Frequency (in MHz) on which the frame was received
    497          1.1.1.2  christos  * Returns: 0 if the Public Action frame was a GAS frame or -1 if not
    498          1.1.1.2  christos  */
    499              1.1  christos int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa,
    500      1.1.1.2.4.1       snj 		 const u8 *bssid, u8 categ, const u8 *data, size_t len,
    501      1.1.1.2.4.1       snj 		 int freq)
    502              1.1  christos {
    503              1.1  christos 	struct gas_query_pending *query;
    504              1.1  christos 	u8 action, dialog_token, frag_id = 0, more_frags = 0;
    505              1.1  christos 	u16 comeback_delay, resp_len;
    506              1.1  christos 	const u8 *pos, *adv_proto;
    507      1.1.1.2.4.1       snj 	int prot, pmf;
    508      1.1.1.2.4.1       snj 	unsigned int left;
    509              1.1  christos 
    510              1.1  christos 	if (gas == NULL || len < 4)
    511              1.1  christos 		return -1;
    512              1.1  christos 
    513  1.1.1.2.4.1.6.1       snj 	pos = data;
    514  1.1.1.2.4.1.6.1       snj 	action = *pos++;
    515  1.1.1.2.4.1.6.1       snj 	dialog_token = *pos++;
    516  1.1.1.2.4.1.6.1       snj 
    517  1.1.1.2.4.1.6.1       snj 	if (action != WLAN_PA_GAS_INITIAL_RESP &&
    518  1.1.1.2.4.1.6.1       snj 	    action != WLAN_PA_GAS_COMEBACK_RESP)
    519  1.1.1.2.4.1.6.1       snj 		return -1; /* Not a GAS response */
    520  1.1.1.2.4.1.6.1       snj 
    521      1.1.1.2.4.1       snj 	prot = categ == WLAN_ACTION_PROTECTED_DUAL;
    522  1.1.1.2.4.1.6.1       snj 	pmf = pmf_in_use(gas->wpa_s, sa);
    523      1.1.1.2.4.1       snj 	if (prot && !pmf) {
    524      1.1.1.2.4.1       snj 		wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled");
    525      1.1.1.2.4.1       snj 		return 0;
    526      1.1.1.2.4.1       snj 	}
    527      1.1.1.2.4.1       snj 	if (!prot && pmf) {
    528      1.1.1.2.4.1       snj 		wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled");
    529      1.1.1.2.4.1       snj 		return 0;
    530      1.1.1.2.4.1       snj 	}
    531      1.1.1.2.4.1       snj 
    532              1.1  christos 	query = gas_query_get_pending(gas, sa, dialog_token);
    533              1.1  christos 	if (query == NULL) {
    534              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR
    535              1.1  christos 			   " dialog token %u", MAC2STR(sa), dialog_token);
    536              1.1  christos 		return -1;
    537              1.1  christos 	}
    538              1.1  christos 
    539      1.1.1.2.4.1       snj 	wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR,
    540      1.1.1.2.4.1       snj 		   ms_from_time(&query->last_oper), MAC2STR(sa));
    541      1.1.1.2.4.1       snj 
    542              1.1  christos 	if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) {
    543              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from "
    544              1.1  christos 			   MACSTR " dialog token %u when waiting for comeback "
    545              1.1  christos 			   "response", MAC2STR(sa), dialog_token);
    546              1.1  christos 		return 0;
    547              1.1  christos 	}
    548              1.1  christos 
    549              1.1  christos 	if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) {
    550              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from "
    551              1.1  christos 			   MACSTR " dialog token %u when waiting for initial "
    552              1.1  christos 			   "response", MAC2STR(sa), dialog_token);
    553              1.1  christos 		return 0;
    554              1.1  christos 	}
    555              1.1  christos 
    556              1.1  christos 	query->status_code = WPA_GET_LE16(pos);
    557              1.1  christos 	pos += 2;
    558              1.1  christos 
    559      1.1.1.2.4.1       snj 	if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING &&
    560      1.1.1.2.4.1       snj 	    action == WLAN_PA_GAS_COMEBACK_RESP) {
    561      1.1.1.2.4.1       snj 		wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response");
    562      1.1.1.2.4.1       snj 	} else if (query->status_code != WLAN_STATUS_SUCCESS) {
    563              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token "
    564              1.1  christos 			   "%u failed - status code %u",
    565              1.1  christos 			   MAC2STR(sa), dialog_token, query->status_code);
    566              1.1  christos 		gas_query_done(gas, query, GAS_QUERY_FAILURE);
    567              1.1  christos 		return 0;
    568              1.1  christos 	}
    569              1.1  christos 
    570              1.1  christos 	if (action == WLAN_PA_GAS_COMEBACK_RESP) {
    571              1.1  christos 		if (pos + 1 > data + len)
    572              1.1  christos 			return 0;
    573              1.1  christos 		frag_id = *pos & 0x7f;
    574              1.1  christos 		more_frags = (*pos & 0x80) >> 7;
    575              1.1  christos 		pos++;
    576              1.1  christos 	}
    577              1.1  christos 
    578              1.1  christos 	/* Comeback Delay */
    579              1.1  christos 	if (pos + 2 > data + len)
    580              1.1  christos 		return 0;
    581              1.1  christos 	comeback_delay = WPA_GET_LE16(pos);
    582              1.1  christos 	pos += 2;
    583              1.1  christos 
    584              1.1  christos 	/* Advertisement Protocol element */
    585              1.1  christos 	if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) {
    586              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement "
    587              1.1  christos 			   "Protocol element in the response from " MACSTR,
    588              1.1  christos 			   MAC2STR(sa));
    589              1.1  christos 		return 0;
    590              1.1  christos 	}
    591              1.1  christos 
    592              1.1  christos 	if (*pos != WLAN_EID_ADV_PROTO) {
    593              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement "
    594              1.1  christos 			   "Protocol element ID %u in response from " MACSTR,
    595              1.1  christos 			   *pos, MAC2STR(sa));
    596              1.1  christos 		return 0;
    597              1.1  christos 	}
    598              1.1  christos 
    599              1.1  christos 	adv_proto = pos;
    600              1.1  christos 	pos += 2 + pos[1];
    601              1.1  christos 
    602              1.1  christos 	/* Query Response Length */
    603              1.1  christos 	if (pos + 2 > data + len) {
    604              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length");
    605              1.1  christos 		return 0;
    606              1.1  christos 	}
    607              1.1  christos 	resp_len = WPA_GET_LE16(pos);
    608              1.1  christos 	pos += 2;
    609              1.1  christos 
    610      1.1.1.2.4.1       snj 	left = data + len - pos;
    611      1.1.1.2.4.1       snj 	if (resp_len > left) {
    612              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in "
    613              1.1  christos 			   "response from " MACSTR, MAC2STR(sa));
    614              1.1  christos 		return 0;
    615              1.1  christos 	}
    616              1.1  christos 
    617      1.1.1.2.4.1       snj 	if (resp_len < left) {
    618              1.1  christos 		wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data "
    619              1.1  christos 			   "after Query Response from " MACSTR,
    620      1.1.1.2.4.1       snj 			   left - resp_len, MAC2STR(sa));
    621              1.1  christos 	}
    622              1.1  christos 
    623              1.1  christos 	if (action == WLAN_PA_GAS_COMEBACK_RESP)
    624              1.1  christos 		gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len,
    625              1.1  christos 				      frag_id, more_frags, comeback_delay);
    626              1.1  christos 	else
    627              1.1  christos 		gas_query_rx_initial(gas, query, adv_proto, pos, resp_len,
    628              1.1  christos 				     comeback_delay);
    629              1.1  christos 
    630              1.1  christos 	return 0;
    631              1.1  christos }
    632              1.1  christos 
    633              1.1  christos 
    634              1.1  christos static void gas_query_timeout(void *eloop_data, void *user_ctx)
    635              1.1  christos {
    636              1.1  christos 	struct gas_query *gas = eloop_data;
    637              1.1  christos 	struct gas_query_pending *query = user_ctx;
    638              1.1  christos 
    639      1.1.1.2.4.1       snj 	wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR
    640      1.1.1.2.4.1       snj 		   " dialog token %u",
    641      1.1.1.2.4.1       snj 		   MAC2STR(query->addr), query->dialog_token);
    642              1.1  christos 	gas_query_done(gas, query, GAS_QUERY_TIMEOUT);
    643              1.1  christos }
    644              1.1  christos 
    645              1.1  christos 
    646              1.1  christos static int gas_query_dialog_token_available(struct gas_query *gas,
    647              1.1  christos 					    const u8 *dst, u8 dialog_token)
    648              1.1  christos {
    649              1.1  christos 	struct gas_query_pending *q;
    650              1.1  christos 	dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) {
    651              1.1  christos 		if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 &&
    652              1.1  christos 		    dialog_token == q->dialog_token)
    653              1.1  christos 			return 0;
    654              1.1  christos 	}
    655              1.1  christos 
    656              1.1  christos 	return 1;
    657              1.1  christos }
    658              1.1  christos 
    659              1.1  christos 
    660      1.1.1.2.4.1       snj static void gas_query_start_cb(struct wpa_radio_work *work, int deinit)
    661      1.1.1.2.4.1       snj {
    662      1.1.1.2.4.1       snj 	struct gas_query_pending *query = work->ctx;
    663      1.1.1.2.4.1       snj 	struct gas_query *gas = query->gas;
    664      1.1.1.2.4.1       snj 	struct wpa_supplicant *wpa_s = gas->wpa_s;
    665      1.1.1.2.4.1       snj 
    666      1.1.1.2.4.1       snj 	if (deinit) {
    667      1.1.1.2.4.1       snj 		if (work->started) {
    668      1.1.1.2.4.1       snj 			gas->work = NULL;
    669      1.1.1.2.4.1       snj 			gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT);
    670      1.1.1.2.4.1       snj 			return;
    671      1.1.1.2.4.1       snj 		}
    672      1.1.1.2.4.1       snj 
    673      1.1.1.2.4.1       snj 		gas_query_free(query, 1);
    674      1.1.1.2.4.1       snj 		return;
    675      1.1.1.2.4.1       snj 	}
    676      1.1.1.2.4.1       snj 
    677      1.1.1.2.4.1       snj 	if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
    678      1.1.1.2.4.1       snj 		wpa_msg(wpa_s, MSG_INFO,
    679      1.1.1.2.4.1       snj 			"Failed to assign random MAC address for GAS");
    680      1.1.1.2.4.1       snj 		gas_query_free(query, 1);
    681      1.1.1.2.4.1       snj 		radio_work_done(work);
    682      1.1.1.2.4.1       snj 		return;
    683      1.1.1.2.4.1       snj 	}
    684      1.1.1.2.4.1       snj 
    685      1.1.1.2.4.1       snj 	gas->work = work;
    686  1.1.1.2.4.1.6.1       snj 	gas_query_tx_initial_req(gas, query);
    687  1.1.1.2.4.1.6.1       snj }
    688  1.1.1.2.4.1.6.1       snj 
    689      1.1.1.2.4.1       snj 
    690  1.1.1.2.4.1.6.1       snj static void gas_query_tx_initial_req(struct gas_query *gas,
    691  1.1.1.2.4.1.6.1       snj 				     struct gas_query_pending *query)
    692  1.1.1.2.4.1.6.1       snj {
    693  1.1.1.2.4.1.6.1       snj 	if (gas_query_tx(gas, query, query->req,
    694  1.1.1.2.4.1.6.1       snj 			 GAS_QUERY_WAIT_TIME_INITIAL) < 0) {
    695      1.1.1.2.4.1       snj 		wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to "
    696      1.1.1.2.4.1       snj 			   MACSTR, MAC2STR(query->addr));
    697  1.1.1.2.4.1.6.1       snj 		gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR);
    698      1.1.1.2.4.1       snj 		return;
    699      1.1.1.2.4.1       snj 	}
    700      1.1.1.2.4.1       snj 	gas->current = query;
    701      1.1.1.2.4.1       snj 
    702      1.1.1.2.4.1       snj 	wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u",
    703      1.1.1.2.4.1       snj 		   query->dialog_token);
    704      1.1.1.2.4.1       snj 	eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0,
    705      1.1.1.2.4.1       snj 			       gas_query_timeout, gas, query);
    706  1.1.1.2.4.1.6.1       snj }
    707  1.1.1.2.4.1.6.1       snj 
    708  1.1.1.2.4.1.6.1       snj 
    709  1.1.1.2.4.1.6.1       snj static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst)
    710  1.1.1.2.4.1.6.1       snj {
    711  1.1.1.2.4.1.6.1       snj 	static int next_start = 0;
    712  1.1.1.2.4.1.6.1       snj 	int dialog_token;
    713      1.1.1.2.4.1       snj 
    714  1.1.1.2.4.1.6.1       snj 	for (dialog_token = 0; dialog_token < 256; dialog_token++) {
    715  1.1.1.2.4.1.6.1       snj 		if (gas_query_dialog_token_available(
    716  1.1.1.2.4.1.6.1       snj 			    gas, dst, (next_start + dialog_token) % 256))
    717  1.1.1.2.4.1.6.1       snj 			break;
    718  1.1.1.2.4.1.6.1       snj 	}
    719  1.1.1.2.4.1.6.1       snj 	if (dialog_token == 256)
    720  1.1.1.2.4.1.6.1       snj 		return -1; /* Too many pending queries */
    721  1.1.1.2.4.1.6.1       snj 	dialog_token = (next_start + dialog_token) % 256;
    722  1.1.1.2.4.1.6.1       snj 	next_start = (dialog_token + 1) % 256;
    723  1.1.1.2.4.1.6.1       snj 	return dialog_token;
    724      1.1.1.2.4.1       snj }
    725      1.1.1.2.4.1       snj 
    726      1.1.1.2.4.1       snj 
    727          1.1.1.2  christos /**
    728          1.1.1.2  christos  * gas_query_req - Request a GAS query
    729          1.1.1.2  christos  * @gas: GAS query data from gas_query_init()
    730          1.1.1.2  christos  * @dst: Destination MAC address for the query
    731          1.1.1.2  christos  * @freq: Frequency (in MHz) for the channel on which to send the query
    732      1.1.1.2.4.1       snj  * @req: GAS query payload (to be freed by gas_query module in case of success
    733      1.1.1.2.4.1       snj  *	return)
    734          1.1.1.2  christos  * @cb: Callback function for reporting GAS query result and response
    735          1.1.1.2  christos  * @ctx: Context pointer to use with the @cb call
    736          1.1.1.2  christos  * Returns: dialog token (>= 0) on success or -1 on failure
    737          1.1.1.2  christos  */
    738              1.1  christos int gas_query_req(struct gas_query *gas, const u8 *dst, int freq,
    739              1.1  christos 		  struct wpabuf *req,
    740              1.1  christos 		  void (*cb)(void *ctx, const u8 *dst, u8 dialog_token,
    741              1.1  christos 			     enum gas_query_result result,
    742              1.1  christos 			     const struct wpabuf *adv_proto,
    743              1.1  christos 			     const struct wpabuf *resp, u16 status_code),
    744              1.1  christos 		  void *ctx)
    745              1.1  christos {
    746              1.1  christos 	struct gas_query_pending *query;
    747              1.1  christos 	int dialog_token;
    748              1.1  christos 
    749              1.1  christos 	if (wpabuf_len(req) < 3)
    750              1.1  christos 		return -1;
    751              1.1  christos 
    752  1.1.1.2.4.1.6.1       snj 	dialog_token = gas_query_new_dialog_token(gas, dst);
    753  1.1.1.2.4.1.6.1       snj 	if (dialog_token < 0)
    754  1.1.1.2.4.1.6.1       snj 		return -1;
    755              1.1  christos 
    756              1.1  christos 	query = os_zalloc(sizeof(*query));
    757              1.1  christos 	if (query == NULL)
    758              1.1  christos 		return -1;
    759              1.1  christos 
    760      1.1.1.2.4.1       snj 	query->gas = gas;
    761              1.1  christos 	os_memcpy(query->addr, dst, ETH_ALEN);
    762              1.1  christos 	query->dialog_token = dialog_token;
    763              1.1  christos 	query->freq = freq;
    764              1.1  christos 	query->cb = cb;
    765              1.1  christos 	query->ctx = ctx;
    766      1.1.1.2.4.1       snj 	query->req = req;
    767              1.1  christos 	dl_list_add(&gas->pending, &query->list);
    768              1.1  christos 
    769              1.1  christos 	*(wpabuf_mhead_u8(req) + 2) = dialog_token;
    770              1.1  christos 
    771      1.1.1.2.4.1       snj 	wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR
    772      1.1.1.2.4.1       snj 		" dialog_token=%u freq=%d",
    773      1.1.1.2.4.1       snj 		MAC2STR(query->addr), query->dialog_token, query->freq);
    774      1.1.1.2.4.1       snj 
    775      1.1.1.2.4.1       snj 	if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb,
    776      1.1.1.2.4.1       snj 			   query) < 0) {
    777  1.1.1.2.4.1.6.1       snj 		query->req = NULL; /* caller will free this in error case */
    778      1.1.1.2.4.1       snj 		gas_query_free(query, 1);
    779              1.1  christos 		return -1;
    780              1.1  christos 	}
    781              1.1  christos 
    782              1.1  christos 	return dialog_token;
    783              1.1  christos }
    784