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