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