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