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 | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 4 | * Copyright (c) 2011-2013, Qualcomm Atheros, Inc. |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 5 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "includes.h" |
| 11 | |
| 12 | #include "common.h" |
| 13 | #include "utils/eloop.h" |
| 14 | #include "common/ieee802_11_defs.h" |
| 15 | #include "common/gas.h" |
| 16 | #include "wpa_supplicant_i.h" |
| 17 | #include "driver_i.h" |
| 18 | #include "offchannel.h" |
| 19 | #include "gas_query.h" |
| 20 | |
| 21 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 22 | /** GAS query timeout in seconds */ |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 23 | #define GAS_QUERY_TIMEOUT_PERIOD 2 |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 24 | /** Retry period for GAS query requests in milliseconds */ |
| 25 | #define GAS_SERVICE_RETRY_PERIOD_MS 500 |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 26 | |
| 27 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 28 | /** |
| 29 | * struct gas_query_pending - Pending GAS query |
| 30 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 31 | struct gas_query_pending { |
| 32 | struct dl_list list; |
| 33 | u8 addr[ETH_ALEN]; |
| 34 | u8 dialog_token; |
| 35 | u8 next_frag_id; |
| 36 | unsigned int wait_comeback:1; |
| 37 | unsigned int offchannel_tx_started:1; |
| 38 | int freq; |
| 39 | u16 status_code; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 40 | struct wpabuf *req; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 41 | struct wpabuf *adv_proto; |
| 42 | struct wpabuf *resp; |
| 43 | void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, |
| 44 | enum gas_query_result result, |
| 45 | const struct wpabuf *adv_proto, |
| 46 | const struct wpabuf *resp, u16 status_code); |
| 47 | void *ctx; |
| 48 | }; |
| 49 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 50 | /** |
| 51 | * struct gas_query - Internal GAS query data |
| 52 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 53 | struct gas_query { |
| 54 | struct wpa_supplicant *wpa_s; |
| 55 | struct dl_list pending; /* struct gas_query_pending */ |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 56 | struct gas_query_pending *current; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | |
| 60 | static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx); |
| 61 | static void gas_query_timeout(void *eloop_data, void *user_ctx); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 62 | static void gas_service_timeout(void *eloop_data, void *user_ctx); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 63 | |
| 64 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 65 | /** |
| 66 | * gas_query_init - Initialize GAS query component |
| 67 | * @wpa_s: Pointer to wpa_supplicant data |
| 68 | * Returns: Pointer to GAS query data or %NULL on failure |
| 69 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 70 | struct gas_query * gas_query_init(struct wpa_supplicant *wpa_s) |
| 71 | { |
| 72 | struct gas_query *gas; |
| 73 | |
| 74 | gas = os_zalloc(sizeof(*gas)); |
| 75 | if (gas == NULL) |
| 76 | return NULL; |
| 77 | |
| 78 | gas->wpa_s = wpa_s; |
| 79 | dl_list_init(&gas->pending); |
| 80 | |
| 81 | return gas; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static void gas_query_done(struct gas_query *gas, |
| 86 | struct gas_query_pending *query, |
| 87 | enum gas_query_result result) |
| 88 | { |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 89 | if (gas->current == query) |
| 90 | gas->current = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 91 | if (query->offchannel_tx_started) |
| 92 | offchannel_send_action_done(gas->wpa_s); |
| 93 | eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); |
| 94 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 95 | eloop_cancel_timeout(gas_service_timeout, gas, query); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 96 | dl_list_del(&query->list); |
| 97 | query->cb(query->ctx, query->addr, query->dialog_token, result, |
| 98 | query->adv_proto, query->resp, query->status_code); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 99 | wpabuf_free(query->req); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 100 | wpabuf_free(query->adv_proto); |
| 101 | wpabuf_free(query->resp); |
| 102 | os_free(query); |
| 103 | } |
| 104 | |
| 105 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 106 | /** |
| 107 | * gas_query_deinit - Deinitialize GAS query component |
| 108 | * @gas: GAS query data from gas_query_init() |
| 109 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 110 | void gas_query_deinit(struct gas_query *gas) |
| 111 | { |
| 112 | struct gas_query_pending *query, *next; |
| 113 | |
| 114 | if (gas == NULL) |
| 115 | return; |
| 116 | |
| 117 | dl_list_for_each_safe(query, next, &gas->pending, |
| 118 | struct gas_query_pending, list) |
| 119 | gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); |
| 120 | |
| 121 | os_free(gas); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static struct gas_query_pending * |
| 126 | gas_query_get_pending(struct gas_query *gas, const u8 *addr, u8 dialog_token) |
| 127 | { |
| 128 | struct gas_query_pending *q; |
| 129 | dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) { |
| 130 | if (os_memcmp(q->addr, addr, ETH_ALEN) == 0 && |
| 131 | q->dialog_token == dialog_token) |
| 132 | return q; |
| 133 | } |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | static int gas_query_append(struct gas_query_pending *query, const u8 *data, |
| 139 | size_t len) |
| 140 | { |
| 141 | if (wpabuf_resize(&query->resp, len) < 0) { |
| 142 | wpa_printf(MSG_DEBUG, "GAS: No memory to store the response"); |
| 143 | return -1; |
| 144 | } |
| 145 | wpabuf_put_data(query->resp, data, len); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 150 | static void gas_query_tx_status(struct wpa_supplicant *wpa_s, |
| 151 | unsigned int freq, const u8 *dst, |
| 152 | const u8 *src, const u8 *bssid, |
| 153 | const u8 *data, size_t data_len, |
| 154 | enum offchannel_send_action_result result) |
| 155 | { |
| 156 | struct gas_query_pending *query; |
| 157 | struct gas_query *gas = wpa_s->gas; |
| 158 | |
| 159 | if (gas->current == NULL) { |
| 160 | wpa_printf(MSG_DEBUG, "GAS: Unexpected TX status: freq=%u dst=" |
| 161 | MACSTR " result=%d - no query in progress", |
| 162 | freq, MAC2STR(dst), result); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | query = gas->current; |
| 167 | |
| 168 | wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR |
| 169 | " result=%d query=%p dialog_token=%u", |
| 170 | freq, MAC2STR(dst), result, query, query->dialog_token); |
| 171 | if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) { |
| 172 | wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (result == OFFCHANNEL_SEND_ACTION_SUCCESS) { |
| 177 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
| 178 | eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, |
| 179 | gas_query_timeout, gas, query); |
| 180 | } |
| 181 | if (result == OFFCHANNEL_SEND_ACTION_FAILED) { |
| 182 | eloop_cancel_timeout(gas_query_timeout, gas, query); |
| 183 | eloop_register_timeout(0, 0, gas_query_timeout, gas, query); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 188 | static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query, |
| 189 | struct wpabuf *req) |
| 190 | { |
| 191 | int res; |
| 192 | wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u " |
| 193 | "freq=%d", MAC2STR(query->addr), |
| 194 | (unsigned int) wpabuf_len(req), query->freq); |
| 195 | res = offchannel_send_action(gas->wpa_s, query->freq, query->addr, |
| 196 | gas->wpa_s->own_addr, query->addr, |
| 197 | wpabuf_head(req), wpabuf_len(req), 1000, |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 198 | gas_query_tx_status, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 199 | if (res == 0) |
| 200 | query->offchannel_tx_started = 1; |
| 201 | return res; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static void gas_query_tx_comeback_req(struct gas_query *gas, |
| 206 | struct gas_query_pending *query) |
| 207 | { |
| 208 | struct wpabuf *req; |
| 209 | |
| 210 | req = gas_build_comeback_req(query->dialog_token); |
| 211 | if (req == NULL) { |
| 212 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | if (gas_query_tx(gas, query, req) < 0) { |
| 217 | wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " |
| 218 | MACSTR, MAC2STR(query->addr)); |
| 219 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
| 220 | } |
| 221 | |
| 222 | wpabuf_free(req); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static void gas_query_tx_comeback_timeout(void *eloop_data, void *user_ctx) |
| 227 | { |
| 228 | struct gas_query *gas = eloop_data; |
| 229 | struct gas_query_pending *query = user_ctx; |
| 230 | |
| 231 | wpa_printf(MSG_DEBUG, "GAS: Comeback timeout for request to " MACSTR, |
| 232 | MAC2STR(query->addr)); |
| 233 | gas_query_tx_comeback_req(gas, query); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | static void gas_query_tx_comeback_req_delay(struct gas_query *gas, |
| 238 | struct gas_query_pending *query, |
| 239 | u16 comeback_delay) |
| 240 | { |
| 241 | unsigned int secs, usecs; |
| 242 | |
| 243 | secs = (comeback_delay * 1024) / 1000000; |
| 244 | usecs = comeback_delay * 1024 - secs * 1000000; |
| 245 | wpa_printf(MSG_DEBUG, "GAS: Send comeback request to " MACSTR |
| 246 | " in %u secs %u usecs", MAC2STR(query->addr), secs, usecs); |
| 247 | eloop_cancel_timeout(gas_query_tx_comeback_timeout, gas, query); |
| 248 | eloop_register_timeout(secs, usecs, gas_query_tx_comeback_timeout, |
| 249 | gas, query); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | static void gas_query_rx_initial(struct gas_query *gas, |
| 254 | struct gas_query_pending *query, |
| 255 | const u8 *adv_proto, const u8 *resp, |
| 256 | size_t len, u16 comeback_delay) |
| 257 | { |
| 258 | wpa_printf(MSG_DEBUG, "GAS: Received initial response from " |
| 259 | MACSTR " (dialog_token=%u comeback_delay=%u)", |
| 260 | MAC2STR(query->addr), query->dialog_token, comeback_delay); |
| 261 | |
| 262 | query->adv_proto = wpabuf_alloc_copy(adv_proto, 2 + adv_proto[1]); |
| 263 | if (query->adv_proto == NULL) { |
| 264 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | if (comeback_delay) { |
| 269 | query->wait_comeback = 1; |
| 270 | gas_query_tx_comeback_req_delay(gas, query, comeback_delay); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | /* Query was completed without comeback mechanism */ |
| 275 | if (gas_query_append(query, resp, len) < 0) { |
| 276 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | gas_query_done(gas, query, GAS_QUERY_SUCCESS); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | static void gas_query_rx_comeback(struct gas_query *gas, |
| 285 | struct gas_query_pending *query, |
| 286 | const u8 *adv_proto, const u8 *resp, |
| 287 | size_t len, u8 frag_id, u8 more_frags, |
| 288 | u16 comeback_delay) |
| 289 | { |
| 290 | wpa_printf(MSG_DEBUG, "GAS: Received comeback response from " |
| 291 | MACSTR " (dialog_token=%u frag_id=%u more_frags=%u " |
| 292 | "comeback_delay=%u)", |
| 293 | MAC2STR(query->addr), query->dialog_token, frag_id, |
| 294 | more_frags, comeback_delay); |
| 295 | |
| 296 | if ((size_t) 2 + adv_proto[1] != wpabuf_len(query->adv_proto) || |
| 297 | os_memcmp(adv_proto, wpabuf_head(query->adv_proto), |
| 298 | wpabuf_len(query->adv_proto)) != 0) { |
| 299 | wpa_printf(MSG_DEBUG, "GAS: Advertisement Protocol changed " |
| 300 | "between initial and comeback response from " |
| 301 | MACSTR, MAC2STR(query->addr)); |
| 302 | gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | if (comeback_delay) { |
| 307 | if (frag_id) { |
| 308 | wpa_printf(MSG_DEBUG, "GAS: Invalid comeback response " |
| 309 | "with non-zero frag_id and comeback_delay " |
| 310 | "from " MACSTR, MAC2STR(query->addr)); |
| 311 | gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); |
| 312 | return; |
| 313 | } |
| 314 | gas_query_tx_comeback_req_delay(gas, query, comeback_delay); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | if (frag_id != query->next_frag_id) { |
| 319 | wpa_printf(MSG_DEBUG, "GAS: Unexpected frag_id in response " |
| 320 | "from " MACSTR, MAC2STR(query->addr)); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 321 | if (frag_id + 1 == query->next_frag_id) { |
| 322 | wpa_printf(MSG_DEBUG, "GAS: Drop frame as possible " |
| 323 | "retry of previous fragment"); |
| 324 | return; |
| 325 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 326 | gas_query_done(gas, query, GAS_QUERY_PEER_ERROR); |
| 327 | return; |
| 328 | } |
| 329 | query->next_frag_id++; |
| 330 | |
| 331 | if (gas_query_append(query, resp, len) < 0) { |
| 332 | gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (more_frags) { |
| 337 | gas_query_tx_comeback_req(gas, query); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | gas_query_done(gas, query, GAS_QUERY_SUCCESS); |
| 342 | } |
| 343 | |
| 344 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 345 | /** |
| 346 | * gas_query_rx - Indicate reception of a Public Action frame |
| 347 | * @gas: GAS query data from gas_query_init() |
| 348 | * @da: Destination MAC address of the Action frame |
| 349 | * @sa: Source MAC address of the Action frame |
| 350 | * @bssid: BSSID of the Action frame |
| 351 | * @data: Payload of the Action frame |
| 352 | * @len: Length of @data |
| 353 | * @freq: Frequency (in MHz) on which the frame was received |
| 354 | * Returns: 0 if the Public Action frame was a GAS frame or -1 if not |
| 355 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 356 | int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa, |
| 357 | const u8 *bssid, const u8 *data, size_t len, int freq) |
| 358 | { |
| 359 | struct gas_query_pending *query; |
| 360 | u8 action, dialog_token, frag_id = 0, more_frags = 0; |
| 361 | u16 comeback_delay, resp_len; |
| 362 | const u8 *pos, *adv_proto; |
| 363 | |
| 364 | if (gas == NULL || len < 4) |
| 365 | return -1; |
| 366 | |
| 367 | pos = data; |
| 368 | action = *pos++; |
| 369 | dialog_token = *pos++; |
| 370 | |
| 371 | if (action != WLAN_PA_GAS_INITIAL_RESP && |
| 372 | action != WLAN_PA_GAS_COMEBACK_RESP) |
| 373 | return -1; /* Not a GAS response */ |
| 374 | |
| 375 | query = gas_query_get_pending(gas, sa, dialog_token); |
| 376 | if (query == NULL) { |
| 377 | wpa_printf(MSG_DEBUG, "GAS: No pending query found for " MACSTR |
| 378 | " dialog token %u", MAC2STR(sa), dialog_token); |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | if (query->wait_comeback && action == WLAN_PA_GAS_INITIAL_RESP) { |
| 383 | wpa_printf(MSG_DEBUG, "GAS: Unexpected initial response from " |
| 384 | MACSTR " dialog token %u when waiting for comeback " |
| 385 | "response", MAC2STR(sa), dialog_token); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | if (!query->wait_comeback && action == WLAN_PA_GAS_COMEBACK_RESP) { |
| 390 | wpa_printf(MSG_DEBUG, "GAS: Unexpected comeback response from " |
| 391 | MACSTR " dialog token %u when waiting for initial " |
| 392 | "response", MAC2STR(sa), dialog_token); |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | query->status_code = WPA_GET_LE16(pos); |
| 397 | pos += 2; |
| 398 | |
| 399 | if (query->status_code != WLAN_STATUS_SUCCESS) { |
| 400 | wpa_printf(MSG_DEBUG, "GAS: Query to " MACSTR " dialog token " |
| 401 | "%u failed - status code %u", |
| 402 | MAC2STR(sa), dialog_token, query->status_code); |
| 403 | gas_query_done(gas, query, GAS_QUERY_FAILURE); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | if (action == WLAN_PA_GAS_COMEBACK_RESP) { |
| 408 | if (pos + 1 > data + len) |
| 409 | return 0; |
| 410 | frag_id = *pos & 0x7f; |
| 411 | more_frags = (*pos & 0x80) >> 7; |
| 412 | pos++; |
| 413 | } |
| 414 | |
| 415 | /* Comeback Delay */ |
| 416 | if (pos + 2 > data + len) |
| 417 | return 0; |
| 418 | comeback_delay = WPA_GET_LE16(pos); |
| 419 | pos += 2; |
| 420 | |
| 421 | /* Advertisement Protocol element */ |
| 422 | if (pos + 2 > data + len || pos + 2 + pos[1] > data + len) { |
| 423 | wpa_printf(MSG_DEBUG, "GAS: No room for Advertisement " |
| 424 | "Protocol element in the response from " MACSTR, |
| 425 | MAC2STR(sa)); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | if (*pos != WLAN_EID_ADV_PROTO) { |
| 430 | wpa_printf(MSG_DEBUG, "GAS: Unexpected Advertisement " |
| 431 | "Protocol element ID %u in response from " MACSTR, |
| 432 | *pos, MAC2STR(sa)); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | adv_proto = pos; |
| 437 | pos += 2 + pos[1]; |
| 438 | |
| 439 | /* Query Response Length */ |
| 440 | if (pos + 2 > data + len) { |
| 441 | wpa_printf(MSG_DEBUG, "GAS: No room for GAS Response Length"); |
| 442 | return 0; |
| 443 | } |
| 444 | resp_len = WPA_GET_LE16(pos); |
| 445 | pos += 2; |
| 446 | |
| 447 | if (pos + resp_len > data + len) { |
| 448 | wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in " |
| 449 | "response from " MACSTR, MAC2STR(sa)); |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | if (pos + resp_len < data + len) { |
| 454 | wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data " |
| 455 | "after Query Response from " MACSTR, |
| 456 | (unsigned int) (data + len - pos - resp_len), |
| 457 | MAC2STR(sa)); |
| 458 | } |
| 459 | |
| 460 | if (action == WLAN_PA_GAS_COMEBACK_RESP) |
| 461 | gas_query_rx_comeback(gas, query, adv_proto, pos, resp_len, |
| 462 | frag_id, more_frags, comeback_delay); |
| 463 | else |
| 464 | gas_query_rx_initial(gas, query, adv_proto, pos, resp_len, |
| 465 | comeback_delay); |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | static void gas_query_timeout(void *eloop_data, void *user_ctx) |
| 472 | { |
| 473 | struct gas_query *gas = eloop_data; |
| 474 | struct gas_query_pending *query = user_ctx; |
| 475 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 476 | wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR |
| 477 | " dialog token %u", |
| 478 | MAC2STR(query->addr), query->dialog_token); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 479 | gas_query_done(gas, query, GAS_QUERY_TIMEOUT); |
| 480 | } |
| 481 | |
| 482 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 483 | static void gas_service_timeout(void *eloop_data, void *user_ctx) |
| 484 | { |
| 485 | struct gas_query *gas = eloop_data; |
| 486 | struct wpa_supplicant *wpa_s = gas->wpa_s; |
| 487 | struct gas_query_pending *query = user_ctx; |
| 488 | int conn; |
| 489 | |
| 490 | conn = wpas_wpa_is_in_progress(wpa_s, 1); |
| 491 | if (conn || wpa_s->scanning || gas->current) { |
| 492 | wpa_printf(MSG_DEBUG, "GAS: Delaying GAS query Tx while another operation is in progress:%s%s%s", |
| 493 | conn ? " connection" : "", |
| 494 | wpa_s->scanning ? " scanning" : "", |
| 495 | gas->current ? " gas_query" : ""); |
| 496 | eloop_register_timeout( |
| 497 | GAS_SERVICE_RETRY_PERIOD_MS / 1000, |
| 498 | (GAS_SERVICE_RETRY_PERIOD_MS % 1000) * 1000, |
| 499 | gas_service_timeout, gas, query); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | if (gas_query_tx(gas, query, query->req) < 0) { |
| 504 | wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " |
| 505 | MACSTR, MAC2STR(query->addr)); |
| 506 | dl_list_del(&query->list); |
| 507 | wpabuf_free(query->req); |
| 508 | os_free(query); |
| 509 | return; |
| 510 | } |
| 511 | gas->current = query; |
| 512 | |
| 513 | wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", |
| 514 | query->dialog_token); |
| 515 | eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, |
| 516 | gas_query_timeout, gas, query); |
| 517 | } |
| 518 | |
| 519 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 520 | static int gas_query_dialog_token_available(struct gas_query *gas, |
| 521 | const u8 *dst, u8 dialog_token) |
| 522 | { |
| 523 | struct gas_query_pending *q; |
| 524 | dl_list_for_each(q, &gas->pending, struct gas_query_pending, list) { |
| 525 | if (os_memcmp(dst, q->addr, ETH_ALEN) == 0 && |
| 526 | dialog_token == q->dialog_token) |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | return 1; |
| 531 | } |
| 532 | |
| 533 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 534 | /** |
| 535 | * gas_query_req - Request a GAS query |
| 536 | * @gas: GAS query data from gas_query_init() |
| 537 | * @dst: Destination MAC address for the query |
| 538 | * @freq: Frequency (in MHz) for the channel on which to send the query |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 539 | * @req: GAS query payload (to be freed by gas_query module in case of success |
| 540 | * return) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 541 | * @cb: Callback function for reporting GAS query result and response |
| 542 | * @ctx: Context pointer to use with the @cb call |
| 543 | * Returns: dialog token (>= 0) on success or -1 on failure |
| 544 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 545 | int gas_query_req(struct gas_query *gas, const u8 *dst, int freq, |
| 546 | struct wpabuf *req, |
| 547 | void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, |
| 548 | enum gas_query_result result, |
| 549 | const struct wpabuf *adv_proto, |
| 550 | const struct wpabuf *resp, u16 status_code), |
| 551 | void *ctx) |
| 552 | { |
| 553 | struct gas_query_pending *query; |
| 554 | int dialog_token; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 555 | static int next_start = 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 556 | |
| 557 | if (wpabuf_len(req) < 3) |
| 558 | return -1; |
| 559 | |
| 560 | for (dialog_token = 0; dialog_token < 256; dialog_token++) { |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 561 | if (gas_query_dialog_token_available( |
| 562 | gas, dst, (next_start + dialog_token) % 256)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 563 | break; |
| 564 | } |
| 565 | if (dialog_token == 256) |
| 566 | return -1; /* Too many pending queries */ |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 567 | dialog_token = (next_start + dialog_token) % 256; |
| 568 | next_start = (dialog_token + 1) % 256; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 569 | |
| 570 | query = os_zalloc(sizeof(*query)); |
| 571 | if (query == NULL) |
| 572 | return -1; |
| 573 | |
| 574 | os_memcpy(query->addr, dst, ETH_ALEN); |
| 575 | query->dialog_token = dialog_token; |
| 576 | query->freq = freq; |
| 577 | query->cb = cb; |
| 578 | query->ctx = ctx; |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 579 | query->req = req; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 580 | dl_list_add(&gas->pending, &query->list); |
| 581 | |
| 582 | *(wpabuf_mhead_u8(req) + 2) = dialog_token; |
| 583 | |
| 584 | wpa_printf(MSG_DEBUG, "GAS: Starting request for " MACSTR |
| 585 | " dialog_token %u", MAC2STR(dst), dialog_token); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 586 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 587 | eloop_register_timeout(0, 0, gas_service_timeout, gas, query); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 588 | |
| 589 | return dialog_token; |
| 590 | } |
| 591 | |
| 592 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 593 | /** |
| 594 | * gas_query_cancel - Cancel a pending GAS query |
| 595 | * @gas: GAS query data from gas_query_init() |
| 596 | * @dst: Destination MAC address for the query |
| 597 | * @dialog_token: Dialog token from gas_query_req() |
| 598 | */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 599 | void gas_query_cancel(struct gas_query *gas, const u8 *dst, u8 dialog_token) |
| 600 | { |
| 601 | struct gas_query_pending *query; |
| 602 | |
| 603 | query = gas_query_get_pending(gas, dst, dialog_token); |
| 604 | if (query) |
| 605 | gas_query_done(gas, query, GAS_QUERY_CANCELLED); |
| 606 | |
| 607 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame^] | 608 | |
| 609 | |
| 610 | int gas_query_in_progress(struct gas_query *gas) |
| 611 | { |
| 612 | return gas->current != NULL; |
| 613 | } |