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