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