1 /* 2 * Generic advertisement service (GAS) query 3 * Copyright (c) 2009, Atheros Communications 4 * Copyright (c) 2011-2014, Qualcomm Atheros, Inc. 5 * Copyright (c) 2011-2014, Jouni Malinen <j@w1.fi> 6 * 7 * This software may be distributed under the terms of the BSD license. 8 * See README for more details. 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" 17 #include "common/wpa_ctrl.h" 18 #include "rsn_supp/wpa.h" 19 #include "wpa_supplicant_i.h" 20 #include "config.h" 21 #include "driver_i.h" 22 #include "offchannel.h" 23 #include "gas_query.h" 24 25 26 /** GAS query timeout in seconds */ 27 #define GAS_QUERY_TIMEOUT_PERIOD 2 28 29 /* GAS query wait-time / duration in ms */ 30 #define GAS_QUERY_WAIT_TIME_INITIAL 1000 31 #define GAS_QUERY_WAIT_TIME_COMEBACK 150 32 33 /** 34 * struct gas_query_pending - Pending GAS query 35 */ 36 struct gas_query_pending { 37 struct dl_list list; 38 struct gas_query *gas; 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; 44 unsigned int retry:1; 45 unsigned int wildcard_bssid:1; 46 unsigned int maintain_addr:1; 47 int freq; 48 u16 status_code; 49 struct wpabuf *req; 50 struct wpabuf *adv_proto; 51 struct wpabuf *resp; 52 struct os_reltime last_oper; 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; 58 u8 sa[ETH_ALEN]; 59 }; 60 61 /** 62 * struct gas_query - Internal GAS query data 63 */ 64 struct gas_query { 65 struct wpa_supplicant *wpa_s; 66 struct dl_list pending; /* struct gas_query_pending */ 67 struct gas_query_pending *current; 68 struct wpa_radio_work *work; 69 struct os_reltime last_mac_addr_rand; 70 int last_rand_sa_type; 71 u8 rand_addr[ETH_ALEN]; 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); 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); 81 82 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 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 */ 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 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"; 126 case GAS_QUERY_STOPPED: 127 return "STOPPED"; 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 155 static void gas_query_done(struct gas_query *gas, 156 struct gas_query_pending *query, 157 enum gas_query_result result) 158 { 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)); 163 if (gas->current == query) 164 gas->current = NULL; 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); 169 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); 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); 173 gas_query_free(query, 0); 174 } 175 176 177 /** 178 * gas_query_deinit - Deinitialize GAS query component 179 * @gas: GAS query data from gas_query_init() 180 */ 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 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; 229 int dur; 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 240 dur = ms_from_time(&query->last_oper); 241 wpa_printf(MSG_DEBUG, "GAS: TX status: freq=%u dst=" MACSTR 242 " result=%d query=%p dialog_token=%u dur=%d ms", 243 freq, MAC2STR(dst), result, query, query->dialog_token, dur); 244 if (os_memcmp(dst, query->addr, ETH_ALEN) != 0) { 245 wpa_printf(MSG_DEBUG, "GAS: TX status for unexpected destination"); 246 return; 247 } 248 os_get_reltime(&query->last_oper); 249 250 if (result == OFFCHANNEL_SEND_ACTION_SUCCESS || 251 result == OFFCHANNEL_SEND_ACTION_NO_ACK) { 252 eloop_cancel_timeout(gas_query_timeout, gas, query); 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 } 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 } 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 276 int pmf_in_use(struct wpa_supplicant *wpa_s, const u8 *addr) 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 286 static int gas_query_tx(struct gas_query *gas, struct gas_query_pending *query, 287 struct wpabuf *req, unsigned int wait_time) 288 { 289 int res, prot = pmf_in_use(gas->wpa_s, query->addr); 290 const u8 *bssid; 291 const u8 wildcard_bssid[ETH_ALEN] = { 292 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 293 }; 294 295 wpa_printf(MSG_DEBUG, "GAS: Send action frame to " MACSTR " len=%u " 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)); 299 if (prot) { 300 u8 *categ = wpabuf_mhead_u8(req); 301 *categ = WLAN_ACTION_PROTECTED_DUAL; 302 } 303 os_get_reltime(&query->last_oper); 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; 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))) 312 bssid = query->addr; 313 else 314 bssid = wildcard_bssid; 315 316 res = offchannel_send_action(gas->wpa_s, query->freq, query->addr, 317 query->sa, bssid, wpabuf_head(req), 318 wpabuf_len(req), wait_time, 319 gas_query_tx_status, 0); 320 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; 331 unsigned int wait_time; 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 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) { 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 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 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 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 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) { 429 eloop_cancel_timeout(gas_query_timeout, gas, query); 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); 456 eloop_cancel_timeout(gas_query_rx_comeback_timeout, gas, query); 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)); 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 } 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 507 /** 508 * gas_query_rx - Indicate reception of a Public Action or Protected Dual frame 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 513 * @categ: Category of the Action frame 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 */ 519 int gas_query_rx(struct gas_query *gas, const u8 *da, const u8 *sa, 520 const u8 *bssid, u8 categ, const u8 *data, size_t len, 521 int freq) 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; 527 int prot, pmf; 528 unsigned int left; 529 530 if (gas == NULL || len < 4) 531 return -1; 532 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 541 prot = categ == WLAN_ACTION_PROTECTED_DUAL; 542 pmf = pmf_in_use(gas->wpa_s, sa); 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 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 559 wpa_printf(MSG_DEBUG, "GAS: Response in %d ms from " MACSTR, 560 ms_from_time(&query->last_oper), MAC2STR(sa)); 561 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 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) { 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 630 left = data + len - pos; 631 if (resp_len > left) { 632 wpa_printf(MSG_DEBUG, "GAS: Truncated Query Response in " 633 "response from " MACSTR, MAC2STR(sa)); 634 return 0; 635 } 636 637 if (resp_len < left) { 638 wpa_printf(MSG_DEBUG, "GAS: Ignore %u octets of extra data " 639 "after Query Response from " MACSTR, 640 left - resp_len, MAC2STR(sa)); 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 659 wpa_printf(MSG_DEBUG, "GAS: No response received for query to " MACSTR 660 " dialog token %u", 661 MAC2STR(query->addr), query->dialog_token); 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 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; 684 struct wpa_supplicant *wpa_s = gas->wpa_s; 685 686 if (deinit) { 687 if (work->started) { 688 gas->work = NULL; 689 gas_query_done(gas, query, GAS_QUERY_DELETED_AT_DEINIT); 690 return; 691 } 692 693 gas_query_free(query, 1); 694 return; 695 } 696 697 if (!query->maintain_addr && !wpa_s->conf->gas_rand_mac_addr) { 698 if (wpas_update_random_addr_disassoc(wpa_s) < 0) { 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 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN); 706 } 707 708 gas->work = work; 709 gas_query_tx_initial_req(gas, query); 710 } 711 712 713 static void gas_query_tx_initial_req(struct gas_query *gas, 714 struct gas_query_pending *query) 715 { 716 if (gas_query_tx(gas, query, query->req, 717 GAS_QUERY_WAIT_TIME_INITIAL) < 0) { 718 wpa_printf(MSG_DEBUG, "GAS: Failed to send Action frame to " 719 MACSTR, MAC2STR(query->addr)); 720 gas_query_done(gas, query, GAS_QUERY_INTERNAL_ERROR); 721 return; 722 } 723 gas->current = query; 724 725 wpa_printf(MSG_DEBUG, "GAS: Starting query timeout for dialog token %u", 726 query->dialog_token); 727 eloop_register_timeout(GAS_QUERY_TIMEOUT_PERIOD, 0, 728 gas_query_timeout, gas, query); 729 } 730 731 732 static int gas_query_new_dialog_token(struct gas_query *gas, const u8 *dst) 733 { 734 u8 dialog_token; 735 int i; 736 737 /* There should never be more than couple active GAS queries in 738 * progress, so it should be very likely to find an available dialog 739 * token by checking random values. Use a limit on the number of 740 * iterations to handle the unexpected case of large number of pending 741 * queries cleanly. */ 742 for (i = 0; i < 256; i++) { 743 /* Get a random number and check if the slot is available */ 744 if (os_get_random(&dialog_token, sizeof(dialog_token)) < 0) 745 break; 746 if (gas_query_dialog_token_available(gas, dst, dialog_token)) 747 return dialog_token; 748 } 749 750 /* No dialog token value available */ 751 return -1; 752 } 753 754 755 static int gas_query_set_sa(struct gas_query *gas, 756 struct gas_query_pending *query) 757 { 758 struct wpa_supplicant *wpa_s = gas->wpa_s; 759 struct os_reltime now; 760 761 if (query->maintain_addr || 762 !wpa_s->conf->gas_rand_mac_addr || 763 !(wpa_s->current_bss ? 764 (wpa_s->drv_flags & 765 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) : 766 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA))) { 767 /* Use own MAC address as the transmitter address */ 768 wpa_printf(MSG_DEBUG, 769 "GAS: Use own MAC address as the transmitter address%s%s%s", 770 query->maintain_addr ? " (maintain_addr)" : "", 771 !wpa_s->conf->gas_rand_mac_addr ? " (no gas_rand_mac_adr set)" : "", 772 !(wpa_s->current_bss ? 773 (wpa_s->drv_flags & 774 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA_CONNECTED) : 775 (wpa_s->drv_flags & 776 WPA_DRIVER_FLAGS_MGMT_TX_RANDOM_TA)) ? 777 " (no driver rand capa" : ""); 778 os_memcpy(query->sa, wpa_s->own_addr, ETH_ALEN); 779 return 0; 780 } 781 782 os_get_reltime(&now); 783 784 if (wpa_s->conf->gas_rand_mac_addr == gas->last_rand_sa_type && 785 gas->last_mac_addr_rand.sec != 0 && 786 !os_reltime_expired(&now, &gas->last_mac_addr_rand, 787 wpa_s->conf->gas_rand_addr_lifetime)) { 788 wpa_printf(MSG_DEBUG, 789 "GAS: Use the previously selected random transmitter address " 790 MACSTR, MAC2STR(gas->rand_addr)); 791 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN); 792 return 0; 793 } 794 795 if (wpa_s->conf->gas_rand_mac_addr == 1 && 796 random_mac_addr(gas->rand_addr) < 0) { 797 wpa_printf(MSG_ERROR, "GAS: Failed to get random address"); 798 return -1; 799 } 800 801 if (wpa_s->conf->gas_rand_mac_addr == 2 && 802 random_mac_addr_keep_oui(gas->rand_addr) < 0) { 803 wpa_printf(MSG_ERROR, 804 "GAS: Failed to get random address with same OUI"); 805 return -1; 806 } 807 808 wpa_printf(MSG_DEBUG, "GAS: Use a new random transmitter address " 809 MACSTR, MAC2STR(gas->rand_addr)); 810 os_memcpy(query->sa, gas->rand_addr, ETH_ALEN); 811 os_get_reltime(&gas->last_mac_addr_rand); 812 gas->last_rand_sa_type = wpa_s->conf->gas_rand_mac_addr; 813 814 return 0; 815 } 816 817 818 /** 819 * gas_query_req - Request a GAS query 820 * @gas: GAS query data from gas_query_init() 821 * @dst: Destination MAC address for the query 822 * @freq: Frequency (in MHz) for the channel on which to send the query 823 * @wildcard_bssid: Force use of wildcard BSSID value 824 * @maintain_addr: Maintain own MAC address for exchange (i.e., ignore MAC 825 * address randomization rules) 826 * @req: GAS query payload (to be freed by gas_query module in case of success 827 * return) 828 * @cb: Callback function for reporting GAS query result and response 829 * @ctx: Context pointer to use with the @cb call 830 * Returns: dialog token (>= 0) on success or -1 on failure 831 */ 832 int gas_query_req(struct gas_query *gas, const u8 *dst, int freq, 833 int wildcard_bssid, int maintain_addr, struct wpabuf *req, 834 void (*cb)(void *ctx, const u8 *dst, u8 dialog_token, 835 enum gas_query_result result, 836 const struct wpabuf *adv_proto, 837 const struct wpabuf *resp, u16 status_code), 838 void *ctx) 839 { 840 struct gas_query_pending *query; 841 int dialog_token; 842 843 if (wpabuf_len(req) < 3) 844 return -1; 845 846 dialog_token = gas_query_new_dialog_token(gas, dst); 847 if (dialog_token < 0) 848 return -1; 849 850 query = os_zalloc(sizeof(*query)); 851 if (query == NULL) 852 return -1; 853 854 query->gas = gas; 855 query->maintain_addr = !!maintain_addr; 856 if (gas_query_set_sa(gas, query)) { 857 os_free(query); 858 return -1; 859 } 860 os_memcpy(query->addr, dst, ETH_ALEN); 861 query->dialog_token = dialog_token; 862 query->wildcard_bssid = !!wildcard_bssid; 863 query->freq = freq; 864 query->cb = cb; 865 query->ctx = ctx; 866 query->req = req; 867 dl_list_add(&gas->pending, &query->list); 868 869 *(wpabuf_mhead_u8(req) + 2) = dialog_token; 870 871 wpa_msg(gas->wpa_s, MSG_INFO, GAS_QUERY_START "addr=" MACSTR 872 " dialog_token=%u freq=%d", 873 MAC2STR(query->addr), query->dialog_token, query->freq); 874 875 if (radio_add_work(gas->wpa_s, freq, "gas-query", 0, gas_query_start_cb, 876 query) < 0) { 877 query->req = NULL; /* caller will free this in error case */ 878 gas_query_free(query, 1); 879 return -1; 880 } 881 882 return dialog_token; 883 } 884 885 886 int gas_query_stop(struct gas_query *gas, u8 dialog_token) 887 { 888 struct gas_query_pending *query; 889 890 dl_list_for_each(query, &gas->pending, struct gas_query_pending, list) { 891 if (query->dialog_token == dialog_token) { 892 if (!gas->work) { 893 /* The pending radio work has not yet been 894 * started, but the pending entry has a 895 * reference to the soon to be freed query. 896 * Need to remove that radio work now to avoid 897 * leaving behind a reference to freed memory. 898 */ 899 radio_remove_pending_work(gas->wpa_s, query); 900 } 901 gas_query_done(gas, query, GAS_QUERY_STOPPED); 902 return 0; 903 } 904 } 905 906 return -1; 907 } 908