1f05cddf9SRui Paulo /* 2f05cddf9SRui Paulo * Generic advertisement service (GAS) query 3f05cddf9SRui Paulo * Copyright (c) 2009, Atheros Communications 45b9c547cSRui Paulo * Copyright (c) 2011-2014, Qualcomm Atheros, Inc. 55b9c547cSRui Paulo * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi> 6f05cddf9SRui Paulo * 7f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license. 8f05cddf9SRui Paulo * See README for more details. 9f05cddf9SRui Paulo */ 10f05cddf9SRui Paulo 11f05cddf9SRui Paulo #include "includes.h" 12f05cddf9SRui Paulo 13f05cddf9SRui Paulo #include "common.h" 14f05cddf9SRui Paulo #include "utils/eloop.h" 15f05cddf9SRui Paulo #include "common/ieee802_11_defs.h" 16f05cddf9SRui Paulo #include "common/gas.h" 175b9c547cSRui Paulo #include "common/wpa_ctrl.h" 185b9c547cSRui Paulo #include "rsn_supp/wpa.h" 19f05cddf9SRui Paulo #include "wpa_supplicant_i.h" 20780fb4a2SCy Schubert #include "config.h" 21f05cddf9SRui Paulo #include "driver_i.h" 22f05cddf9SRui Paulo #include "offchannel.h" 23f05cddf9SRui Paulo #include "gas_query.h" 24f05cddf9SRui Paulo 25f05cddf9SRui Paulo 26f05cddf9SRui Paulo /** GAS query timeout in seconds */ 275b9c547cSRui Paulo #define GAS_QUERY_TIMEOUT_PERIOD 2 28f05cddf9SRui Paulo 29780fb4a2SCy Schubert /* GAS query wait-time / duration in ms */ 30780fb4a2SCy Schubert #define GAS_QUERY_WAIT_TIME_INITIAL 1000 31780fb4a2SCy Schubert #define GAS_QUERY_WAIT_TIME_COMEBACK 150 32f05cddf9SRui Paulo 33f05cddf9SRui Paulo /** 34f05cddf9SRui Paulo * struct gas_query_pending - Pending GAS query 35f05cddf9SRui Paulo */ 36f05cddf9SRui Paulo struct gas_query_pending { 37f05cddf9SRui Paulo struct dl_list list; 385b9c547cSRui Paulo struct gas_query *gas; 39f05cddf9SRui Paulo u8 addr[ETH_ALEN]; 40f05cddf9SRui Paulo u8 dialog_token; 41f05cddf9SRui Paulo u8 next_frag_id; 42f05cddf9SRui Paulo unsigned int wait_comeback:1; 43f05cddf9SRui Paulo unsigned int offchannel_tx_started:1; 44780fb4a2SCy Schubert unsigned int retry:1; 45*85732ac8SCy Schubert unsigned int wildcard_bssid:1; 46f05cddf9SRui Paulo int freq; 47f05cddf9SRui Paulo u16 status_code; 485b9c547cSRui Paulo struct wpabuf *req; 49f05cddf9SRui Paulo struct wpabuf *adv_proto; 50f05cddf9SRui Paulo struct wpabuf *resp; 515b9c547cSRui Paulo struct os_reltime last_oper; 52f05cddf9SRui Paulo void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, 53f05cddf9SRui Paulo enum gas_query_result result, 54f05cddf9SRui Paulo const struct wpabuf *adv_proto, 55f05cddf9SRui Paulo const struct wpabuf *resp, u16 status_code); 56f05cddf9SRui Paulo void *ctx; 57*85732ac8SCy Schubert u8 sa[ETH_ALEN]; 58f05cddf9SRui Paulo }; 59f05cddf9SRui Paulo 60f05cddf9SRui Paulo /** 61f05cddf9SRui Paulo * struct gas_query - Internal GAS query data 62f05cddf9SRui Paulo */ 63f05cddf9SRui Paulo struct gas_query { 64f05cddf9SRui Paulo struct wpa_supplicant *wpa_s; 65f05cddf9SRui Paulo struct dl_list pending; /* struct gas_query_pending */ 665b9c547cSRui Paulo struct gas_query_pending *current; 675b9c547cSRui Paulo struct wpa_radio_work *work; 68*85732ac8SCy Schubert struct os_reltime last_mac_addr_rand; 69*85732ac8SCy Schubert int last_rand_sa_type; 70*85732ac8SCy Schubert u8 rand_addr[ETH_ALEN]; 71f05cddf9SRui Paulo }; 72f05cddf9SRui Paulo 73f05cddf9SRui Paulo 74f05cddf9SRui Paulo static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx); 75f05cddf9SRui Paulo static void gas_query_timeout(void *eloop_data, void *user_ctx); 76780fb4a2SCy Schubert static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx); 77780fb4a2SCy Schubert static void gas_query_tx_initial_req(struct gas_query *gas, 78780fb4a2SCy Schubert struct gas_query_pending *query); 79780fb4a2SCy Schubert static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst); 80f05cddf9SRui Paulo 81f05cddf9SRui Paulo 825b9c547cSRui Paulo static int ms_from_time(struct os_reltime *last) 835b9c547cSRui Paulo { 845b9c547cSRui Paulo struct os_reltime now, res; 855b9c547cSRui Paulo 865b9c547cSRui Paulo os_get_reltime(&now); 875b9c547cSRui Paulo os_reltime_sub(&now, last, &res); 885b9c547cSRui Paulo return res.sec * 1000 + res.usec / 1000; 895b9c547cSRui Paulo } 905b9c547cSRui Paulo 915b9c547cSRui Paulo 92f05cddf9SRui Paulo /** 93f05cddf9SRui Paulo * gas_query_init - Initialize GAS query component 94f05cddf9SRui Paulo * @wpa_s: Pointer to wpa_supplicant data 95f05cddf9SRui Paulo * Returns: Pointer to GAS query data or %NULL on failure 96f05cddf9SRui Paulo */ 97f05cddf9SRui Paulo struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s) 98f05cddf9SRui Paulo { 99f05cddf9SRui Paulo struct gas_query *gas; 100f05cddf9SRui Paulo 101f05cddf9SRui Paulo gas = os_zalloc(sizeof(*gas)); 102f05cddf9SRui Paulo if (gas == NULL) 103f05cddf9SRui Paulo return NULL; 104f05cddf9SRui Paulo 105f05cddf9SRui Paulo gas->wpa_s = wpa_s; 106f05cddf9SRui Paulo dl_list_init(&gas->pending); 107f05cddf9SRui Paulo 108f05cddf9SRui Paulo return gas; 109f05cddf9SRui Paulo } 110f05cddf9SRui Paulo 111f05cddf9SRui Paulo 1125b9c547cSRui Paulo static const char * gas_result_txt(enum gas_query_result result) 1135b9c547cSRui Paulo { 1145b9c547cSRui Paulo switch (result) { 1155b9c547cSRui Paulo case GAS_QUERY_SUCCESS: 1165b9c547cSRui Paulo return "SUCCESS"; 1175b9c547cSRui Paulo case GAS_QUERY_FAILURE: 1185b9c547cSRui Paulo return "FAILURE"; 1195b9c547cSRui Paulo case GAS_QUERY_TIMEOUT: 1205b9c547cSRui Paulo return "TIMEOUT"; 1215b9c547cSRui Paulo case GAS_QUERY_PEER_ERROR: 1225b9c547cSRui Paulo return "PEER_ERROR"; 1235b9c547cSRui Paulo case GAS_QUERY_INTERNAL_ERROR: 1245b9c547cSRui Paulo return "INTERNAL_ERROR"; 125*85732ac8SCy Schubert case GAS_QUERY_STOPPED: 126*85732ac8SCy Schubert return "STOPPED"; 1275b9c547cSRui Paulo case GAS_QUERY_DELETED_AT_DEINIT: 1285b9c547cSRui Paulo return "DELETED_AT_DEINIT"; 1295b9c547cSRui Paulo } 1305b9c547cSRui Paulo 1315b9c547cSRui Paulo return "N/A"; 1325b9c547cSRui Paulo } 1335b9c547cSRui Paulo 1345b9c547cSRui Paulo 1355b9c547cSRui Paulo static void gas_query_free(struct gas_query_pending *query, int del_list) 1365b9c547cSRui Paulo { 1375b9c547cSRui Paulo struct gas_query *gas = query->gas; 1385b9c547cSRui Paulo 1395b9c547cSRui Paulo if (del_list) 1405b9c547cSRui Paulo dl_list_del(&query->list); 1415b9c547cSRui Paulo 1425b9c547cSRui Paulo if (gas->work && gas->work->ctx == query) { 1435b9c547cSRui Paulo radio_work_done(gas->work); 1445b9c547cSRui Paulo gas->work = NULL; 1455b9c547cSRui Paulo } 1465b9c547cSRui Paulo 1475b9c547cSRui Paulo wpabuf_free(query->req); 1485b9c547cSRui Paulo wpabuf_free(query->adv_proto); 1495b9c547cSRui Paulo wpabuf_free(query->resp); 1505b9c547cSRui Paulo os_free(query); 1515b9c547cSRui Paulo } 1525b9c547cSRui Paulo 1535b9c547cSRui Paulo 154f05cddf9SRui Paulo static void gas_query_done(struct gas_query *gas, 155f05cddf9SRui Paulo struct gas_query_pending *query, 156f05cddf9SRui Paulo enum gas_query_result result) 157f05cddf9SRui Paulo { 1585b9c547cSRui Paulo wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_DONE "addr=" MACSTR 1595b9c547cSRui Paulo " dialog_token=%u freq=%d status_code=%u result=%s", 1605b9c547cSRui Paulo MAC2STR(query->addr), query->dialog_token, query->freq, 1615b9c547cSRui Paulo query->status_code, gas_result_txt(result)); 1625b9c547cSRui Paulo if (gas->current == query) 1635b9c547cSRui Paulo gas->current = NULL; 164f05cddf9SRui Paulo if (query->offchannel_tx_started) 165f05cddf9SRui Paulo offchannel_send_action_done(gas->wpa_s); 166f05cddf9SRui Paulo eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); 167f05cddf9SRui Paulo eloop_cancel_timeout(gas_query_timeout, gas, query); 168780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); 169f05cddf9SRui Paulo dl_list_del(&query->list); 170f05cddf9SRui Paulo query->cb(query->ctx, query->addr, query->dialog_token, result, 171f05cddf9SRui Paulo query->adv_proto, query->resp, query->status_code); 1725b9c547cSRui Paulo gas_query_free(query, 0); 173f05cddf9SRui Paulo } 174f05cddf9SRui Paulo 175f05cddf9SRui Paulo 176f05cddf9SRui Paulo /** 177f05cddf9SRui Paulo * gas_query_deinit - Deinitialize GAS query component 178f05cddf9SRui Paulo * @gas: GAS query data from gas_query_init() 179f05cddf9SRui Paulo */ 180f05cddf9SRui Paulo void gas_query_deinit(struct gas_query *gas) 181f05cddf9SRui Paulo { 182f05cddf9SRui Paulo struct gas_query_pending *query, *next; 183f05cddf9SRui Paulo 184f05cddf9SRui Paulo if (gas == NULL) 185f05cddf9SRui Paulo return; 186f05cddf9SRui Paulo 187f05cddf9SRui Paulo dl_list_for_each_safe(query, next, &gas->pending, 188f05cddf9SRui Paulo struct gas_query_pending, list) 189f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); 190f05cddf9SRui Paulo 191f05cddf9SRui Paulo os_free(gas); 192f05cddf9SRui Paulo } 193f05cddf9SRui Paulo 194f05cddf9SRui Paulo 195f05cddf9SRui Paulo static struct gas_query_pending * 196f05cddf9SRui Paulo gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token) 197f05cddf9SRui Paulo { 198f05cddf9SRui Paulo struct gas_query_pending *q; 199f05cddf9SRui Paulo dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) { 200f05cddf9SRui Paulo if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 && 201f05cddf9SRui Paulo q->dialog_token == dialog_token) 202f05cddf9SRui Paulo return q; 203f05cddf9SRui Paulo } 204f05cddf9SRui Paulo return NULL; 205f05cddf9SRui Paulo } 206f05cddf9SRui Paulo 207f05cddf9SRui Paulo 208f05cddf9SRui Paulo static int gas_query_append(struct gas_query_pending *query, const u8 *data, 209f05cddf9SRui Paulo size_t len) 210f05cddf9SRui Paulo { 211f05cddf9SRui Paulo if (wpabuf_resize(&query->resp, len) < 0) { 212f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No memory to store the response"); 213f05cddf9SRui Paulo return -1; 214f05cddf9SRui Paulo } 215f05cddf9SRui Paulo wpabuf_put_data(query->resp, data, len); 216f05cddf9SRui Paulo return 0; 217f05cddf9SRui Paulo } 218f05cddf9SRui Paulo 219f05cddf9SRui Paulo 2205b9c547cSRui Paulo static void gas_query_tx_status(struct wpa_supplicant *wpa_s, 2215b9c547cSRui Paulo unsigned int freq, const u8 *dst, 2225b9c547cSRui Paulo const u8 *src, const u8 *bssid, 2235b9c547cSRui Paulo const u8 *data, size_t data_len, 2245b9c547cSRui Paulo enum offchannel_send_action_result result) 2255b9c547cSRui Paulo { 2265b9c547cSRui Paulo struct gas_query_pending *query; 2275b9c547cSRui Paulo struct gas_query *gas = wpa_s->gas; 2285b9c547cSRui Paulo int dur; 2295b9c547cSRui Paulo 2305b9c547cSRui Paulo if (gas->current == NULL) { 2315b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst=" 2325b9c547cSRui Paulo MACSTR " result=%d - no query in progress", 2335b9c547cSRui Paulo freq, MAC2STR(dst), result); 2345b9c547cSRui Paulo return; 2355b9c547cSRui Paulo } 2365b9c547cSRui Paulo 2375b9c547cSRui Paulo query = gas->current; 2385b9c547cSRui Paulo 2395b9c547cSRui Paulo dur = ms_from_time(&query->last_oper); 2405b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR 2415b9c547cSRui Paulo " result=%d query=%p dialog_token=%u dur=%d ms", 2425b9c547cSRui Paulo freq, MAC2STR(dst), result, query, query->dialog_token, dur); 2435b9c547cSRui Paulo if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) { 2445b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination"); 2455b9c547cSRui Paulo return; 2465b9c547cSRui Paulo } 2475b9c547cSRui Paulo os_get_reltime(&query->last_oper); 2485b9c547cSRui Paulo 249*85732ac8SCy Schubert if (result == OFFCHANNEL_SEND_ACTION_SUCCESS || 250*85732ac8SCy Schubert result == OFFCHANNEL_SEND_ACTION_NO_ACK) { 2515b9c547cSRui Paulo eloop_cancel_timeout(gas_query_timeout, gas, query); 252*85732ac8SCy Schubert if (result == OFFCHANNEL_SEND_ACTION_NO_ACK) { 253*85732ac8SCy Schubert wpa_printf(MSG_DEBUG, "GAS: No ACK to GAS request"); 254*85732ac8SCy Schubert eloop_register_timeout(0, 250000, 255*85732ac8SCy Schubert gas_query_timeout, gas, query); 256*85732ac8SCy Schubert } else { 2575b9c547cSRui Paulo eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, 2585b9c547cSRui Paulo gas_query_timeout, gas, query); 259*85732ac8SCy Schubert } 260780fb4a2SCy Schubert if (query->wait_comeback && !query->retry) { 261780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_rx_comeback_timeout, 262780fb4a2SCy Schubert gas, query); 263780fb4a2SCy Schubert eloop_register_timeout( 264780fb4a2SCy Schubert 0, (GAS_QUERY_WAIT_TIME_COMEBACK + 10) * 1000, 265780fb4a2SCy Schubert gas_query_rx_comeback_timeout, gas, query); 266780fb4a2SCy Schubert } 2675b9c547cSRui Paulo } 2685b9c547cSRui Paulo if (result == OFFCHANNEL_SEND_ACTION_FAILED) { 2695b9c547cSRui Paulo eloop_cancel_timeout(gas_query_timeout, gas, query); 2705b9c547cSRui Paulo eloop_register_timeout(0, 0, gas_query_timeout, gas, query); 2715b9c547cSRui Paulo } 2725b9c547cSRui Paulo } 2735b9c547cSRui Paulo 2745b9c547cSRui Paulo 2755b9c547cSRui Paulo static int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr) 2765b9c547cSRui Paulo { 2775b9c547cSRui Paulo if (wpa_s->current_ssid == NULL || 2785b9c547cSRui Paulo wpa_s->wpa_state < WPA_4WAY_HANDSHAKE || 2795b9c547cSRui Paulo os_memcmp(addr, wpa_s->bssid, ETH_ALEN) != 0) 2805b9c547cSRui Paulo return 0; 2815b9c547cSRui Paulo return wpa_sm_pmf_enabled(wpa_s->wpa); 2825b9c547cSRui Paulo } 2835b9c547cSRui Paulo 2845b9c547cSRui Paulo 285f05cddf9SRui Paulo static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query, 286780fb4a2SCy Schubert struct wpabuf *req, unsigned int wait_time) 287f05cddf9SRui Paulo { 2885b9c547cSRui Paulo int res, prot = pmf_in_use(gas->wpa_s, query->addr); 289780fb4a2SCy Schubert const u8 *bssid; 290780fb4a2SCy Schubert const u8 wildcard_bssid[ETH_ALEN] = { 291780fb4a2SCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 292780fb4a2SCy Schubert }; 2935b9c547cSRui Paulo 294f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u " 295*85732ac8SCy Schubert "freq=%d prot=%d using src addr " MACSTR, 296*85732ac8SCy Schubert MAC2STR(query->addr), (unsigned int) wpabuf_len(req), 297*85732ac8SCy Schubert query->freq, prot, MAC2STR(query->sa)); 2985b9c547cSRui Paulo if (prot) { 2995b9c547cSRui Paulo u8 *categ = wpabuf_mhead_u8(req); 3005b9c547cSRui Paulo *categ = WLAN_ACTION_PROTECTED_DUAL; 3015b9c547cSRui Paulo } 3025b9c547cSRui Paulo os_get_reltime(&query->last_oper); 3035b9c547cSRui Paulo if (gas->wpa_s->max_remain_on_chan && 3045b9c547cSRui Paulo wait_time > gas->wpa_s->max_remain_on_chan) 3055b9c547cSRui Paulo wait_time = gas->wpa_s->max_remain_on_chan; 306*85732ac8SCy Schubert if (!query->wildcard_bssid && 307*85732ac8SCy Schubert (!gas->wpa_s->conf->gas_address3 || 308780fb4a2SCy Schubert (gas->wpa_s->current_ssid && 309780fb4a2SCy Schubert gas->wpa_s->wpa_state >= WPA_ASSOCIATED && 310*85732ac8SCy Schubert os_memcmp(query->addr, gas->wpa_s->bssid, ETH_ALEN) == 0))) 311780fb4a2SCy Schubert bssid = query->addr; 312780fb4a2SCy Schubert else 313780fb4a2SCy Schubert bssid = wildcard_bssid; 314*85732ac8SCy Schubert 315f05cddf9SRui Paulo res = offchannel_send_action(gas->wpa_s, query->freq, query->addr, 316*85732ac8SCy Schubert query->sa, bssid, wpabuf_head(req), 317*85732ac8SCy Schubert wpabuf_len(req), wait_time, 318*85732ac8SCy Schubert gas_query_tx_status, 0); 319*85732ac8SCy Schubert 320f05cddf9SRui Paulo if (res == 0) 321f05cddf9SRui Paulo query->offchannel_tx_started = 1; 322f05cddf9SRui Paulo return res; 323f05cddf9SRui Paulo } 324f05cddf9SRui Paulo 325f05cddf9SRui Paulo 326f05cddf9SRui Paulo static void gas_query_tx_comeback_req(struct gas_query *gas, 327f05cddf9SRui Paulo struct gas_query_pending *query) 328f05cddf9SRui Paulo { 329f05cddf9SRui Paulo struct wpabuf *req; 330780fb4a2SCy Schubert unsigned int wait_time; 331f05cddf9SRui Paulo 332f05cddf9SRui Paulo req = gas_build_comeback_req(query->dialog_token); 333f05cddf9SRui Paulo if (req == NULL) { 334f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 335f05cddf9SRui Paulo return; 336f05cddf9SRui Paulo } 337f05cddf9SRui Paulo 338780fb4a2SCy Schubert wait_time = (query->retry || !query->offchannel_tx_started) ? 339780fb4a2SCy Schubert GAS_QUERY_WAIT_TIME_INITIAL : GAS_QUERY_WAIT_TIME_COMEBACK; 340780fb4a2SCy Schubert 341780fb4a2SCy Schubert if (gas_query_tx(gas, query, req, wait_time) < 0) { 342f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " 343f05cddf9SRui Paulo MACSTR, MAC2STR(query->addr)); 344f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 345f05cddf9SRui Paulo } 346f05cddf9SRui Paulo 347f05cddf9SRui Paulo wpabuf_free(req); 348f05cddf9SRui Paulo } 349f05cddf9SRui Paulo 350f05cddf9SRui Paulo 351780fb4a2SCy Schubert static void gas_query_rx_comeback_timeout(void *eloop_data, void *user_ctx) 352780fb4a2SCy Schubert { 353780fb4a2SCy Schubert struct gas_query *gas = eloop_data; 354780fb4a2SCy Schubert struct gas_query_pending *query = user_ctx; 355780fb4a2SCy Schubert int dialog_token; 356780fb4a2SCy Schubert 357780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, 358780fb4a2SCy Schubert "GAS: No response to comeback request received (retry=%u)", 359780fb4a2SCy Schubert query->retry); 360780fb4a2SCy Schubert if (gas->current != query || query->retry) 361780fb4a2SCy Schubert return; 362780fb4a2SCy Schubert dialog_token = gas_query_new_dialog_token(gas, query->addr); 363780fb4a2SCy Schubert if (dialog_token < 0) 364780fb4a2SCy Schubert return; 365780fb4a2SCy Schubert wpa_printf(MSG_DEBUG, 366780fb4a2SCy Schubert "GAS: Retry GAS query due to comeback response timeout"); 367780fb4a2SCy Schubert query->retry = 1; 368780fb4a2SCy Schubert query->dialog_token = dialog_token; 369780fb4a2SCy Schubert *(wpabuf_mhead_u8(query->req) + 2) = dialog_token; 370780fb4a2SCy Schubert query->wait_comeback = 0; 371780fb4a2SCy Schubert query->next_frag_id = 0; 372780fb4a2SCy Schubert wpabuf_free(query->adv_proto); 373780fb4a2SCy Schubert query->adv_proto = NULL; 374780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); 375780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_timeout, gas, query); 376780fb4a2SCy Schubert gas_query_tx_initial_req(gas, query); 377780fb4a2SCy Schubert } 378780fb4a2SCy Schubert 379780fb4a2SCy Schubert 380f05cddf9SRui Paulo static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx) 381f05cddf9SRui Paulo { 382f05cddf9SRui Paulo struct gas_query *gas = eloop_data; 383f05cddf9SRui Paulo struct gas_query_pending *query = user_ctx; 384f05cddf9SRui Paulo 385f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR, 386f05cddf9SRui Paulo MAC2STR(query->addr)); 387f05cddf9SRui Paulo gas_query_tx_comeback_req(gas, query); 388f05cddf9SRui Paulo } 389f05cddf9SRui Paulo 390f05cddf9SRui Paulo 391f05cddf9SRui Paulo static void gas_query_tx_comeback_req_delay(struct gas_query *gas, 392f05cddf9SRui Paulo struct gas_query_pending *query, 393f05cddf9SRui Paulo u16 comeback_delay) 394f05cddf9SRui Paulo { 395f05cddf9SRui Paulo unsigned int secs, usecs; 396f05cddf9SRui Paulo 397780fb4a2SCy Schubert if (comeback_delay > 1 && query->offchannel_tx_started) { 398780fb4a2SCy Schubert offchannel_send_action_done(gas->wpa_s); 399780fb4a2SCy Schubert query->offchannel_tx_started = 0; 400780fb4a2SCy Schubert } 401780fb4a2SCy Schubert 402f05cddf9SRui Paulo secs = (comeback_delay * 1024) / 1000000; 403f05cddf9SRui Paulo usecs = comeback_delay * 1024 - secs * 1000000; 404f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR 405f05cddf9SRui Paulo " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs); 406f05cddf9SRui Paulo eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); 407f05cddf9SRui Paulo eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout, 408f05cddf9SRui Paulo gas, query); 409f05cddf9SRui Paulo } 410f05cddf9SRui Paulo 411f05cddf9SRui Paulo 412f05cddf9SRui Paulo static void gas_query_rx_initial(struct gas_query *gas, 413f05cddf9SRui Paulo struct gas_query_pending *query, 414f05cddf9SRui Paulo const u8 *adv_proto, const u8 *resp, 415f05cddf9SRui Paulo size_t len, u16 comeback_delay) 416f05cddf9SRui Paulo { 417f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Received initial response from " 418f05cddf9SRui Paulo MACSTR " (dialog_token=%u comeback_delay=%u)", 419f05cddf9SRui Paulo MAC2STR(query->addr), query->dialog_token, comeback_delay); 420f05cddf9SRui Paulo 421f05cddf9SRui Paulo query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]); 422f05cddf9SRui Paulo if (query->adv_proto == NULL) { 423f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 424f05cddf9SRui Paulo return; 425f05cddf9SRui Paulo } 426f05cddf9SRui Paulo 427f05cddf9SRui Paulo if (comeback_delay) { 428*85732ac8SCy Schubert eloop_cancel_timeout(gas_query_timeout, gas, query); 429f05cddf9SRui Paulo query->wait_comeback = 1; 430f05cddf9SRui Paulo gas_query_tx_comeback_req_delay(gas, query, comeback_delay); 431f05cddf9SRui Paulo return; 432f05cddf9SRui Paulo } 433f05cddf9SRui Paulo 434f05cddf9SRui Paulo /* Query was completed without comeback mechanism */ 435f05cddf9SRui Paulo if (gas_query_append(query, resp, len) < 0) { 436f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 437f05cddf9SRui Paulo return; 438f05cddf9SRui Paulo } 439f05cddf9SRui Paulo 440f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_SUCCESS); 441f05cddf9SRui Paulo } 442f05cddf9SRui Paulo 443f05cddf9SRui Paulo 444f05cddf9SRui Paulo static void gas_query_rx_comeback(struct gas_query *gas, 445f05cddf9SRui Paulo struct gas_query_pending *query, 446f05cddf9SRui Paulo const u8 *adv_proto, const u8 *resp, 447f05cddf9SRui Paulo size_t len, u8 frag_id, u8 more_frags, 448f05cddf9SRui Paulo u16 comeback_delay) 449f05cddf9SRui Paulo { 450f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Received comeback response from " 451f05cddf9SRui Paulo MACSTR " (dialog_token=%u frag_id=%u more_frags=%u " 452f05cddf9SRui Paulo "comeback_delay=%u)", 453f05cddf9SRui Paulo MAC2STR(query->addr), query->dialog_token, frag_id, 454f05cddf9SRui Paulo more_frags, comeback_delay); 455780fb4a2SCy Schubert eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); 456f05cddf9SRui Paulo 457f05cddf9SRui Paulo if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) || 458f05cddf9SRui Paulo os_memcmp(adv_proto, wpabuf_head(query->adv_proto), 459f05cddf9SRui Paulo wpabuf_len(query->adv_proto)) != 0) { 460f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed " 461f05cddf9SRui Paulo "between initial and comeback response from " 462f05cddf9SRui Paulo MACSTR, MAC2STR(query->addr)); 463f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); 464f05cddf9SRui Paulo return; 465f05cddf9SRui Paulo } 466f05cddf9SRui Paulo 467f05cddf9SRui Paulo if (comeback_delay) { 468f05cddf9SRui Paulo if (frag_id) { 469f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response " 470f05cddf9SRui Paulo "with non-zero frag_id and comeback_delay " 471f05cddf9SRui Paulo "from " MACSTR, MAC2STR(query->addr)); 472f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); 473f05cddf9SRui Paulo return; 474f05cddf9SRui Paulo } 475f05cddf9SRui Paulo gas_query_tx_comeback_req_delay(gas, query, comeback_delay); 476f05cddf9SRui Paulo return; 477f05cddf9SRui Paulo } 478f05cddf9SRui Paulo 479f05cddf9SRui Paulo if (frag_id != query->next_frag_id) { 480f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response " 481f05cddf9SRui Paulo "from " MACSTR, MAC2STR(query->addr)); 4825b9c547cSRui Paulo if (frag_id + 1 == query->next_frag_id) { 4835b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible " 4845b9c547cSRui Paulo "retry of previous fragment"); 4855b9c547cSRui Paulo return; 4865b9c547cSRui Paulo } 487f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); 488f05cddf9SRui Paulo return; 489f05cddf9SRui Paulo } 490f05cddf9SRui Paulo query->next_frag_id++; 491f05cddf9SRui Paulo 492f05cddf9SRui Paulo if (gas_query_append(query, resp, len) < 0) { 493f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 494f05cddf9SRui Paulo return; 495f05cddf9SRui Paulo } 496f05cddf9SRui Paulo 497f05cddf9SRui Paulo if (more_frags) { 498f05cddf9SRui Paulo gas_query_tx_comeback_req(gas, query); 499f05cddf9SRui Paulo return; 500f05cddf9SRui Paulo } 501f05cddf9SRui Paulo 502f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_SUCCESS); 503f05cddf9SRui Paulo } 504f05cddf9SRui Paulo 505f05cddf9SRui Paulo 506f05cddf9SRui Paulo /** 5075b9c547cSRui Paulo * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame 508f05cddf9SRui Paulo * @gas: GAS query data from gas_query_init() 509f05cddf9SRui Paulo * @da: Destination MAC address of the Action frame 510f05cddf9SRui Paulo * @sa: Source MAC address of the Action frame 511f05cddf9SRui Paulo * @bssid: BSSID of the Action frame 5125b9c547cSRui Paulo * @categ: Category of the Action frame 513f05cddf9SRui Paulo * @data: Payload of the Action frame 514f05cddf9SRui Paulo * @len: Length of @data 515f05cddf9SRui Paulo * @freq: Frequency (in MHz) on which the frame was received 516f05cddf9SRui Paulo * Returns: 0 if the Public Action frame was a GAS frame or -1 if not 517f05cddf9SRui Paulo */ 518f05cddf9SRui Paulo int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa, 5195b9c547cSRui Paulo const u8 *bssid, u8 categ, const u8 *data, size_t len, 5205b9c547cSRui Paulo int freq) 521f05cddf9SRui Paulo { 522f05cddf9SRui Paulo struct gas_query_pending *query; 523f05cddf9SRui Paulo u8 action, dialog_token, frag_id = 0, more_frags = 0; 524f05cddf9SRui Paulo u16 comeback_delay, resp_len; 525f05cddf9SRui Paulo const u8 *pos, *adv_proto; 5265b9c547cSRui Paulo int prot, pmf; 5275b9c547cSRui Paulo unsigned int left; 528f05cddf9SRui Paulo 529f05cddf9SRui Paulo if (gas == NULL || len < 4) 530f05cddf9SRui Paulo return -1; 531f05cddf9SRui Paulo 532780fb4a2SCy Schubert pos = data; 533780fb4a2SCy Schubert action = *pos++; 534780fb4a2SCy Schubert dialog_token = *pos++; 535780fb4a2SCy Schubert 536780fb4a2SCy Schubert if (action != WLAN_PA_GAS_INITIAL_RESP && 537780fb4a2SCy Schubert action != WLAN_PA_GAS_COMEBACK_RESP) 538780fb4a2SCy Schubert return -1; /* Not a GAS response */ 539780fb4a2SCy Schubert 5405b9c547cSRui Paulo prot = categ == WLAN_ACTION_PROTECTED_DUAL; 541780fb4a2SCy Schubert pmf = pmf_in_use(gas->wpa_s, sa); 5425b9c547cSRui Paulo if (prot && !pmf) { 5435b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Drop unexpected protected GAS frame when PMF is disabled"); 5445b9c547cSRui Paulo return 0; 5455b9c547cSRui Paulo } 5465b9c547cSRui Paulo if (!prot && pmf) { 5475b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Drop unexpected unprotected GAS frame when PMF is enabled"); 5485b9c547cSRui Paulo return 0; 5495b9c547cSRui Paulo } 5505b9c547cSRui Paulo 551f05cddf9SRui Paulo query = gas_query_get_pending(gas, sa, dialog_token); 552f05cddf9SRui Paulo if (query == NULL) { 553f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR 554f05cddf9SRui Paulo " dialog token %u", MAC2STR(sa), dialog_token); 555f05cddf9SRui Paulo return -1; 556f05cddf9SRui Paulo } 557f05cddf9SRui Paulo 5585b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR, 5595b9c547cSRui Paulo ms_from_time(&query->last_oper), MAC2STR(sa)); 5605b9c547cSRui Paulo 561f05cddf9SRui Paulo if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) { 562f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from " 563f05cddf9SRui Paulo MACSTR " dialog token %u when waiting for comeback " 564f05cddf9SRui Paulo "response", MAC2STR(sa), dialog_token); 565f05cddf9SRui Paulo return 0; 566f05cddf9SRui Paulo } 567f05cddf9SRui Paulo 568f05cddf9SRui Paulo if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) { 569f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from " 570f05cddf9SRui Paulo MACSTR " dialog token %u when waiting for initial " 571f05cddf9SRui Paulo "response", MAC2STR(sa), dialog_token); 572f05cddf9SRui Paulo return 0; 573f05cddf9SRui Paulo } 574f05cddf9SRui Paulo 575f05cddf9SRui Paulo query->status_code = WPA_GET_LE16(pos); 576f05cddf9SRui Paulo pos += 2; 577f05cddf9SRui Paulo 5785b9c547cSRui Paulo if (query->status_code == WLAN_STATUS_QUERY_RESP_OUTSTANDING && 5795b9c547cSRui Paulo action == WLAN_PA_GAS_COMEBACK_RESP) { 5805b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Allow non-zero status for outstanding comeback response"); 5815b9c547cSRui Paulo } else if (query->status_code != WLAN_STATUS_SUCCESS) { 582f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token " 583f05cddf9SRui Paulo "%u failed - status code %u", 584f05cddf9SRui Paulo MAC2STR(sa), dialog_token, query->status_code); 585f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_FAILURE); 586f05cddf9SRui Paulo return 0; 587f05cddf9SRui Paulo } 588f05cddf9SRui Paulo 589f05cddf9SRui Paulo if (action == WLAN_PA_GAS_COMEBACK_RESP) { 590f05cddf9SRui Paulo if (pos + 1 > data + len) 591f05cddf9SRui Paulo return 0; 592f05cddf9SRui Paulo frag_id = *pos & 0x7f; 593f05cddf9SRui Paulo more_frags = (*pos & 0x80) >> 7; 594f05cddf9SRui Paulo pos++; 595f05cddf9SRui Paulo } 596f05cddf9SRui Paulo 597f05cddf9SRui Paulo /* Comeback Delay */ 598f05cddf9SRui Paulo if (pos + 2 > data + len) 599f05cddf9SRui Paulo return 0; 600f05cddf9SRui Paulo comeback_delay = WPA_GET_LE16(pos); 601f05cddf9SRui Paulo pos += 2; 602f05cddf9SRui Paulo 603f05cddf9SRui Paulo /* Advertisement Protocol element */ 604f05cddf9SRui Paulo if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) { 605f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement " 606f05cddf9SRui Paulo "Protocol element in the response from " MACSTR, 607f05cddf9SRui Paulo MAC2STR(sa)); 608f05cddf9SRui Paulo return 0; 609f05cddf9SRui Paulo } 610f05cddf9SRui Paulo 611f05cddf9SRui Paulo if (*pos != WLAN_EID_ADV_PROTO) { 612f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement " 613f05cddf9SRui Paulo "Protocol element ID %u in response from " MACSTR, 614f05cddf9SRui Paulo *pos, MAC2STR(sa)); 615f05cddf9SRui Paulo return 0; 616f05cddf9SRui Paulo } 617f05cddf9SRui Paulo 618f05cddf9SRui Paulo adv_proto = pos; 619f05cddf9SRui Paulo pos += 2 + pos[1]; 620f05cddf9SRui Paulo 621f05cddf9SRui Paulo /* Query Response Length */ 622f05cddf9SRui Paulo if (pos + 2 > data + len) { 623f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length"); 624f05cddf9SRui Paulo return 0; 625f05cddf9SRui Paulo } 626f05cddf9SRui Paulo resp_len = WPA_GET_LE16(pos); 627f05cddf9SRui Paulo pos += 2; 628f05cddf9SRui Paulo 6295b9c547cSRui Paulo left = data + len - pos; 6305b9c547cSRui Paulo if (resp_len > left) { 631f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in " 632f05cddf9SRui Paulo "response from " MACSTR, MAC2STR(sa)); 633f05cddf9SRui Paulo return 0; 634f05cddf9SRui Paulo } 635f05cddf9SRui Paulo 6365b9c547cSRui Paulo if (resp_len < left) { 637f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data " 638f05cddf9SRui Paulo "after Query Response from " MACSTR, 6395b9c547cSRui Paulo left - resp_len, MAC2STR(sa)); 640f05cddf9SRui Paulo } 641f05cddf9SRui Paulo 642f05cddf9SRui Paulo if (action == WLAN_PA_GAS_COMEBACK_RESP) 643f05cddf9SRui Paulo gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len, 644f05cddf9SRui Paulo frag_id, more_frags, comeback_delay); 645f05cddf9SRui Paulo else 646f05cddf9SRui Paulo gas_query_rx_initial(gas, query, adv_proto, pos, resp_len, 647f05cddf9SRui Paulo comeback_delay); 648f05cddf9SRui Paulo 649f05cddf9SRui Paulo return 0; 650f05cddf9SRui Paulo } 651f05cddf9SRui Paulo 652f05cddf9SRui Paulo 653f05cddf9SRui Paulo static void gas_query_timeout(void *eloop_data, void *user_ctx) 654f05cddf9SRui Paulo { 655f05cddf9SRui Paulo struct gas_query *gas = eloop_data; 656f05cddf9SRui Paulo struct gas_query_pending *query = user_ctx; 657f05cddf9SRui Paulo 6585b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR 6595b9c547cSRui Paulo " dialog token %u", 6605b9c547cSRui Paulo MAC2STR(query->addr), query->dialog_token); 661f05cddf9SRui Paulo gas_query_done(gas, query, GAS_QUERY_TIMEOUT); 662f05cddf9SRui Paulo } 663f05cddf9SRui Paulo 664f05cddf9SRui Paulo 665f05cddf9SRui Paulo static int gas_query_dialog_token_available(struct gas_query *gas, 666f05cddf9SRui Paulo const u8 *dst, u8 dialog_token) 667f05cddf9SRui Paulo { 668f05cddf9SRui Paulo struct gas_query_pending *q; 669f05cddf9SRui Paulo dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) { 670f05cddf9SRui Paulo if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 && 671f05cddf9SRui Paulo dialog_token == q->dialog_token) 672f05cddf9SRui Paulo return 0; 673f05cddf9SRui Paulo } 674f05cddf9SRui Paulo 675f05cddf9SRui Paulo return 1; 676f05cddf9SRui Paulo } 677f05cddf9SRui Paulo 678f05cddf9SRui Paulo 6795b9c547cSRui Paulo static void gas_query_start_cb(struct wpa_radio_work *work, int deinit) 6805b9c547cSRui Paulo { 6815b9c547cSRui Paulo struct gas_query_pending *query = work->ctx; 6825b9c547cSRui Paulo struct gas_query *gas = query->gas; 6835b9c547cSRui Paulo struct wpa_supplicant *wpa_s = gas->wpa_s; 6845b9c547cSRui Paulo 6855b9c547cSRui Paulo if (deinit) { 6865b9c547cSRui Paulo if (work->started) { 6875b9c547cSRui Paulo gas->work = NULL; 6885b9c547cSRui Paulo gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); 6895b9c547cSRui Paulo return; 6905b9c547cSRui Paulo } 6915b9c547cSRui Paulo 6925b9c547cSRui Paulo gas_query_free(query, 1); 6935b9c547cSRui Paulo return; 6945b9c547cSRui Paulo } 6955b9c547cSRui Paulo 6965b9c547cSRui Paulo if (wpas_update_random_addr_disassoc(wpa_s) < 0) { 6975b9c547cSRui Paulo wpa_msg(wpa_s, MSG_INFO, 6985b9c547cSRui Paulo "Failed to assign random MAC address for GAS"); 6995b9c547cSRui Paulo gas_query_free(query, 1); 7005b9c547cSRui Paulo radio_work_done(work); 7015b9c547cSRui Paulo return; 7025b9c547cSRui Paulo } 7035b9c547cSRui Paulo 7045b9c547cSRui Paulo gas->work = work; 705780fb4a2SCy Schubert gas_query_tx_initial_req(gas, query); 706780fb4a2SCy Schubert } 7075b9c547cSRui Paulo 708780fb4a2SCy Schubert 709780fb4a2SCy Schubert static void gas_query_tx_initial_req(struct gas_query *gas, 710780fb4a2SCy Schubert struct gas_query_pending *query) 711780fb4a2SCy Schubert { 712780fb4a2SCy Schubert if (gas_query_tx(gas, query, query->req, 713780fb4a2SCy Schubert GAS_QUERY_WAIT_TIME_INITIAL) < 0) { 7145b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " 7155b9c547cSRui Paulo MACSTR, MAC2STR(query->addr)); 716780fb4a2SCy Schubert gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 7175b9c547cSRui Paulo return; 7185b9c547cSRui Paulo } 7195b9c547cSRui Paulo gas->current = query; 7205b9c547cSRui Paulo 7215b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", 7225b9c547cSRui Paulo query->dialog_token); 7235b9c547cSRui Paulo eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, 7245b9c547cSRui Paulo gas_query_timeout, gas, query); 725780fb4a2SCy Schubert } 7265b9c547cSRui Paulo 727780fb4a2SCy Schubert 728780fb4a2SCy Schubert static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst) 729780fb4a2SCy Schubert { 730780fb4a2SCy Schubert static int next_start = 0; 731780fb4a2SCy Schubert int dialog_token; 732780fb4a2SCy Schubert 733780fb4a2SCy Schubert for (dialog_token = 0; dialog_token < 256; dialog_token++) { 734780fb4a2SCy Schubert if (gas_query_dialog_token_available( 735780fb4a2SCy Schubert gas, dst, (next_start + dialog_token) % 256)) 736780fb4a2SCy Schubert break; 737780fb4a2SCy Schubert } 738780fb4a2SCy Schubert if (dialog_token == 256) 739780fb4a2SCy Schubert return -1; /* Too many pending queries */ 740780fb4a2SCy Schubert dialog_token = (next_start + dialog_token) % 256; 741780fb4a2SCy Schubert next_start = (dialog_token + 1) % 256; 742780fb4a2SCy Schubert return dialog_token; 7435b9c547cSRui Paulo } 7445b9c547cSRui Paulo 7455b9c547cSRui Paulo 746*85732ac8SCy Schubert static int gas_query_set_sa(struct gas_query *gas, 747*85732ac8SCy Schubert struct gas_query_pending *query) 748*85732ac8SCy Schubert { 749*85732ac8SCy Schubert struct wpa_supplicant *wpa_s = gas->wpa_s; 750*85732ac8SCy Schubert struct os_reltime now; 751*85732ac8SCy Schubert 752*85732ac8SCy Schubert if (!wpa_s->conf->gas_rand_mac_addr || 753*85732ac8SCy Schubert !(wpa_s->current_bss ? 754*85732ac8SCy Schubert (wpa_s->drv_flags & 755*85732ac8SCy Schubert WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) : 756*85732ac8SCy Schubert (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) { 757*85732ac8SCy Schubert /* Use own MAC address as the transmitter address */ 758*85732ac8SCy Schubert os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN); 759*85732ac8SCy Schubert return 0; 760*85732ac8SCy Schubert } 761*85732ac8SCy Schubert 762*85732ac8SCy Schubert os_get_reltime(&now); 763*85732ac8SCy Schubert 764*85732ac8SCy Schubert if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type && 765*85732ac8SCy Schubert gas->last_mac_addr_rand.sec != 0 && 766*85732ac8SCy Schubert !os_reltime_expired(&now, &gas->last_mac_addr_rand, 767*85732ac8SCy Schubert wpa_s->conf->gas_rand_addr_lifetime)) { 768*85732ac8SCy Schubert wpa_printf(MSG_DEBUG, 769*85732ac8SCy Schubert "GAS: Use the previously selected random transmitter address " 770*85732ac8SCy Schubert MACSTR, MAC2STR(gas->rand_addr)); 771*85732ac8SCy Schubert os_memcpy(query->sa, gas->rand_addr, ETH_ALEN); 772*85732ac8SCy Schubert return 0; 773*85732ac8SCy Schubert } 774*85732ac8SCy Schubert 775*85732ac8SCy Schubert if (wpa_s->conf->gas_rand_mac_addr == 1 && 776*85732ac8SCy Schubert random_mac_addr(gas->rand_addr) < 0) { 777*85732ac8SCy Schubert wpa_printf(MSG_ERROR, "GAS: Failed to get random address"); 778*85732ac8SCy Schubert return -1; 779*85732ac8SCy Schubert } 780*85732ac8SCy Schubert 781*85732ac8SCy Schubert if (wpa_s->conf->gas_rand_mac_addr == 2 && 782*85732ac8SCy Schubert random_mac_addr_keep_oui(gas->rand_addr) < 0) { 783*85732ac8SCy Schubert wpa_printf(MSG_ERROR, 784*85732ac8SCy Schubert "GAS: Failed to get random address with same OUI"); 785*85732ac8SCy Schubert return -1; 786*85732ac8SCy Schubert } 787*85732ac8SCy Schubert 788*85732ac8SCy Schubert wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address " 789*85732ac8SCy Schubert MACSTR, MAC2STR(gas->rand_addr)); 790*85732ac8SCy Schubert os_memcpy(query->sa, gas->rand_addr, ETH_ALEN); 791*85732ac8SCy Schubert os_get_reltime(&gas->last_mac_addr_rand); 792*85732ac8SCy Schubert gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr; 793*85732ac8SCy Schubert 794*85732ac8SCy Schubert return 0; 795*85732ac8SCy Schubert } 796*85732ac8SCy Schubert 797*85732ac8SCy Schubert 798f05cddf9SRui Paulo /** 799f05cddf9SRui Paulo * gas_query_req - Request a GAS query 800f05cddf9SRui Paulo * @gas: GAS query data from gas_query_init() 801f05cddf9SRui Paulo * @dst: Destination MAC address for the query 802f05cddf9SRui Paulo * @freq: Frequency (in MHz) for the channel on which to send the query 8035b9c547cSRui Paulo * @req: GAS query payload (to be freed by gas_query module in case of success 8045b9c547cSRui Paulo * return) 805f05cddf9SRui Paulo * @cb: Callback function for reporting GAS query result and response 806f05cddf9SRui Paulo * @ctx: Context pointer to use with the @cb call 807f05cddf9SRui Paulo * Returns: dialog token (>= 0) on success or -1 on failure 808f05cddf9SRui Paulo */ 809f05cddf9SRui Paulo int gas_query_req(struct gas_query *gas, const u8 *dst, int freq, 810*85732ac8SCy Schubert int wildcard_bssid, struct wpabuf *req, 811f05cddf9SRui Paulo void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, 812f05cddf9SRui Paulo enum gas_query_result result, 813f05cddf9SRui Paulo const struct wpabuf *adv_proto, 814f05cddf9SRui Paulo const struct wpabuf *resp, u16 status_code), 815f05cddf9SRui Paulo void *ctx) 816f05cddf9SRui Paulo { 817f05cddf9SRui Paulo struct gas_query_pending *query; 818f05cddf9SRui Paulo int dialog_token; 819f05cddf9SRui Paulo 820f05cddf9SRui Paulo if (wpabuf_len(req) < 3) 821f05cddf9SRui Paulo return -1; 822f05cddf9SRui Paulo 823780fb4a2SCy Schubert dialog_token = gas_query_new_dialog_token(gas, dst); 824780fb4a2SCy Schubert if (dialog_token < 0) 825780fb4a2SCy Schubert return -1; 826f05cddf9SRui Paulo 827f05cddf9SRui Paulo query = os_zalloc(sizeof(*query)); 828f05cddf9SRui Paulo if (query == NULL) 829f05cddf9SRui Paulo return -1; 830f05cddf9SRui Paulo 8315b9c547cSRui Paulo query->gas = gas; 832*85732ac8SCy Schubert if (gas_query_set_sa(gas, query)) { 833*85732ac8SCy Schubert os_free(query); 834*85732ac8SCy Schubert return -1; 835*85732ac8SCy Schubert } 836f05cddf9SRui Paulo os_memcpy(query->addr, dst, ETH_ALEN); 837f05cddf9SRui Paulo query->dialog_token = dialog_token; 838*85732ac8SCy Schubert query->wildcard_bssid = !!wildcard_bssid; 839f05cddf9SRui Paulo query->freq = freq; 840f05cddf9SRui Paulo query->cb = cb; 841f05cddf9SRui Paulo query->ctx = ctx; 8425b9c547cSRui Paulo query->req = req; 843f05cddf9SRui Paulo dl_list_add(&gas->pending, &query->list); 844f05cddf9SRui Paulo 845f05cddf9SRui Paulo *(wpabuf_mhead_u8(req) + 2) = dialog_token; 846f05cddf9SRui Paulo 8475b9c547cSRui Paulo wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR 8485b9c547cSRui Paulo " dialog_token=%u freq=%d", 8495b9c547cSRui Paulo MAC2STR(query->addr), query->dialog_token, query->freq); 8505b9c547cSRui Paulo 8515b9c547cSRui Paulo if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb, 8525b9c547cSRui Paulo query) < 0) { 853780fb4a2SCy Schubert query->req = NULL; /* caller will free this in error case */ 8545b9c547cSRui Paulo gas_query_free(query, 1); 855f05cddf9SRui Paulo return -1; 856f05cddf9SRui Paulo } 857f05cddf9SRui Paulo 858f05cddf9SRui Paulo return dialog_token; 859f05cddf9SRui Paulo } 860*85732ac8SCy Schubert 861*85732ac8SCy Schubert 862*85732ac8SCy Schubert int gas_query_stop(struct gas_query *gas, u8 dialog_token) 863*85732ac8SCy Schubert { 864*85732ac8SCy Schubert struct gas_query_pending *query; 865*85732ac8SCy Schubert 866*85732ac8SCy Schubert dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) { 867*85732ac8SCy Schubert if (query->dialog_token == dialog_token) { 868*85732ac8SCy Schubert if (!gas->work) { 869*85732ac8SCy Schubert /* The pending radio work has not yet been 870*85732ac8SCy Schubert * started, but the pending entry has a 871*85732ac8SCy Schubert * reference to the soon to be freed query. 872*85732ac8SCy Schubert * Need to remove that radio work now to avoid 873*85732ac8SCy Schubert * leaving behind a reference to freed memory. 874*85732ac8SCy Schubert */ 875*85732ac8SCy Schubert radio_remove_pending_work(gas->wpa_s, query); 876*85732ac8SCy Schubert } 877*85732ac8SCy Schubert gas_query_done(gas, query, GAS_QUERY_STOPPED); 878*85732ac8SCy Schubert return 0; 879*85732ac8SCy Schubert } 880*85732ac8SCy Schubert } 881*85732ac8SCy Schubert 882*85732ac8SCy Schubert return -1; 883*85732ac8SCy Schubert } 884