p2p.c revision 661b4f78e48c697429dc46154a4125892c001718
1/* 2 * Wi-Fi Direct - P2P module 3 * Copyright (c) 2009-2010, Atheros Communications 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "includes.h" 10 11#include "common.h" 12#include "eloop.h" 13#include "common/ieee802_11_defs.h" 14#include "common/ieee802_11_common.h" 15#include "common/wpa_ctrl.h" 16#include "wps/wps_i.h" 17#include "p2p_i.h" 18#include "p2p.h" 19 20 21static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx); 22static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev); 23static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, 24 const u8 *sa, const u8 *data, size_t len, 25 int rx_freq); 26static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, 27 const u8 *sa, const u8 *data, 28 size_t len); 29static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx); 30static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx); 31 32 33/* 34 * p2p_scan recovery timeout 35 * 36 * Many drivers are using 30 second timeout on scan results. Allow a bit larger 37 * timeout for this to avoid hitting P2P timeout unnecessarily. 38 */ 39#define P2P_SCAN_TIMEOUT 35 40 41/** 42 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer 43 * entries will be removed 44 */ 45#ifndef P2P_PEER_EXPIRATION_AGE 46#define P2P_PEER_EXPIRATION_AGE 60 47#endif /* P2P_PEER_EXPIRATION_AGE */ 48 49#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2) 50 51static void p2p_expire_peers(struct p2p_data *p2p) 52{ 53 struct p2p_device *dev, *n; 54 struct os_reltime now; 55 size_t i; 56 57 os_get_reltime(&now); 58 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { 59 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec) 60 continue; 61 62 if (dev == p2p->go_neg_peer) { 63 /* 64 * GO Negotiation is in progress with the peer, so 65 * don't expire the peer entry until GO Negotiation 66 * fails or times out. 67 */ 68 continue; 69 } 70 71 if (p2p->cfg->go_connected && 72 p2p->cfg->go_connected(p2p->cfg->cb_ctx, 73 dev->info.p2p_device_addr)) { 74 /* 75 * We are connected as a client to a group in which the 76 * peer is the GO, so do not expire the peer entry. 77 */ 78 os_get_reltime(&dev->last_seen); 79 continue; 80 } 81 82 for (i = 0; i < p2p->num_groups; i++) { 83 if (p2p_group_is_client_connected( 84 p2p->groups[i], dev->info.p2p_device_addr)) 85 break; 86 } 87 if (i < p2p->num_groups) { 88 /* 89 * The peer is connected as a client in a group where 90 * we are the GO, so do not expire the peer entry. 91 */ 92 os_get_reltime(&dev->last_seen); 93 continue; 94 } 95 96 p2p_dbg(p2p, "Expiring old peer entry " MACSTR, 97 MAC2STR(dev->info.p2p_device_addr)); 98 dl_list_del(&dev->list); 99 p2p_device_free(p2p, dev); 100 } 101} 102 103 104static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx) 105{ 106 struct p2p_data *p2p = eloop_ctx; 107 p2p_expire_peers(p2p); 108 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, 109 p2p_expiration_timeout, p2p, NULL); 110} 111 112 113static const char * p2p_state_txt(int state) 114{ 115 switch (state) { 116 case P2P_IDLE: 117 return "IDLE"; 118 case P2P_SEARCH: 119 return "SEARCH"; 120 case P2P_CONNECT: 121 return "CONNECT"; 122 case P2P_CONNECT_LISTEN: 123 return "CONNECT_LISTEN"; 124 case P2P_GO_NEG: 125 return "GO_NEG"; 126 case P2P_LISTEN_ONLY: 127 return "LISTEN_ONLY"; 128 case P2P_WAIT_PEER_CONNECT: 129 return "WAIT_PEER_CONNECT"; 130 case P2P_WAIT_PEER_IDLE: 131 return "WAIT_PEER_IDLE"; 132 case P2P_SD_DURING_FIND: 133 return "SD_DURING_FIND"; 134 case P2P_PROVISIONING: 135 return "PROVISIONING"; 136 case P2P_PD_DURING_FIND: 137 return "PD_DURING_FIND"; 138 case P2P_INVITE: 139 return "INVITE"; 140 case P2P_INVITE_LISTEN: 141 return "INVITE_LISTEN"; 142 default: 143 return "?"; 144 } 145} 146 147 148const char * p2p_get_state_txt(struct p2p_data *p2p) 149{ 150 return p2p_state_txt(p2p->state); 151} 152 153 154u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr) 155{ 156 struct p2p_device *dev = NULL; 157 158 if (!addr || !p2p) 159 return 0; 160 161 dev = p2p_get_device(p2p, addr); 162 if (dev) 163 return dev->wps_prov_info; 164 else 165 return 0; 166} 167 168 169void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr) 170{ 171 struct p2p_device *dev = NULL; 172 173 if (!addr || !p2p) 174 return; 175 176 dev = p2p_get_device(p2p, addr); 177 if (dev) 178 dev->wps_prov_info = 0; 179} 180 181 182void p2p_set_state(struct p2p_data *p2p, int new_state) 183{ 184 p2p_dbg(p2p, "State %s -> %s", 185 p2p_state_txt(p2p->state), p2p_state_txt(new_state)); 186 p2p->state = new_state; 187 188 if (new_state == P2P_IDLE && p2p->pending_channel) { 189 p2p_dbg(p2p, "Apply change in listen channel"); 190 p2p->cfg->reg_class = p2p->pending_reg_class; 191 p2p->cfg->channel = p2p->pending_channel; 192 p2p->pending_reg_class = 0; 193 p2p->pending_channel = 0; 194 } 195} 196 197 198void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec) 199{ 200 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec", 201 p2p_state_txt(p2p->state), sec, usec); 202 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); 203 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL); 204} 205 206 207void p2p_clear_timeout(struct p2p_data *p2p) 208{ 209 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state)); 210 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL); 211} 212 213 214void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer, 215 int status) 216{ 217 struct p2p_go_neg_results res; 218 p2p_clear_timeout(p2p); 219 p2p_set_state(p2p, P2P_IDLE); 220 if (p2p->go_neg_peer) { 221 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; 222 p2p->go_neg_peer->wps_method = WPS_NOT_READY; 223 p2p->go_neg_peer->oob_pw_id = 0; 224 } 225 p2p->go_neg_peer = NULL; 226 227 os_memset(&res, 0, sizeof(res)); 228 res.status = status; 229 if (peer) { 230 wpabuf_free(peer->go_neg_conf); 231 peer->go_neg_conf = NULL; 232 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, 233 ETH_ALEN); 234 os_memcpy(res.peer_interface_addr, peer->intended_addr, 235 ETH_ALEN); 236 } 237 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); 238} 239 240 241static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc) 242{ 243 unsigned int r, tu; 244 int freq; 245 struct wpabuf *ies; 246 247 p2p_dbg(p2p, "Starting short listen state (state=%s)", 248 p2p_state_txt(p2p->state)); 249 250 if (p2p->pending_listen_freq) { 251 /* We have a pending p2p_listen request */ 252 p2p_dbg(p2p, "p2p_listen command pending already"); 253 return; 254 } 255 256 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel); 257 if (freq < 0) { 258 p2p_dbg(p2p, "Unknown regulatory class/channel"); 259 return; 260 } 261 262 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 263 r = 0; 264 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) + 265 p2p->min_disc_int) * 100; 266 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu) 267 tu = p2p->max_disc_tu; 268 if (!dev_disc && tu < 100) 269 tu = 100; /* Need to wait in non-device discovery use cases */ 270 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen) 271 tu = p2p->cfg->max_listen * 1000 / 1024; 272 273 if (tu == 0) { 274 p2p_dbg(p2p, "Skip listen state since duration was 0 TU"); 275 p2p_set_timeout(p2p, 0, 0); 276 return; 277 } 278 279 ies = p2p_build_probe_resp_ies(p2p); 280 if (ies == NULL) 281 return; 282 283 p2p->pending_listen_freq = freq; 284 p2p->pending_listen_sec = 0; 285 p2p->pending_listen_usec = 1024 * tu; 286 287 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000, 288 ies) < 0) { 289 p2p_dbg(p2p, "Failed to start listen mode"); 290 p2p->pending_listen_freq = 0; 291 } 292 wpabuf_free(ies); 293} 294 295 296int p2p_listen(struct p2p_data *p2p, unsigned int timeout) 297{ 298 int freq; 299 struct wpabuf *ies; 300 301 p2p_dbg(p2p, "Going to listen(only) state"); 302 303 if (p2p->pending_listen_freq) { 304 /* We have a pending p2p_listen request */ 305 p2p_dbg(p2p, "p2p_listen command pending already"); 306 return -1; 307 } 308 309 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel); 310 if (freq < 0) { 311 p2p_dbg(p2p, "Unknown regulatory class/channel"); 312 return -1; 313 } 314 315 p2p->pending_listen_sec = timeout / 1000; 316 p2p->pending_listen_usec = (timeout % 1000) * 1000; 317 318 if (p2p->p2p_scan_running) { 319 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) { 320 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen"); 321 return 0; 322 } 323 p2p_dbg(p2p, "p2p_scan running - delay start of listen state"); 324 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN; 325 return 0; 326 } 327 328 ies = p2p_build_probe_resp_ies(p2p); 329 if (ies == NULL) 330 return -1; 331 332 p2p->pending_listen_freq = freq; 333 334 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) { 335 p2p_dbg(p2p, "Failed to start listen mode"); 336 p2p->pending_listen_freq = 0; 337 wpabuf_free(ies); 338 return -1; 339 } 340 wpabuf_free(ies); 341 342 p2p_set_state(p2p, P2P_LISTEN_ONLY); 343 344 return 0; 345} 346 347 348static void p2p_device_clear_reported(struct p2p_data *p2p) 349{ 350 struct p2p_device *dev; 351 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) 352 dev->flags &= ~P2P_DEV_REPORTED; 353} 354 355 356/** 357 * p2p_get_device - Fetch a peer entry 358 * @p2p: P2P module context from p2p_init() 359 * @addr: P2P Device Address of the peer 360 * Returns: Pointer to the device entry or %NULL if not found 361 */ 362struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr) 363{ 364 struct p2p_device *dev; 365 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 366 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0) 367 return dev; 368 } 369 return NULL; 370} 371 372 373/** 374 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address 375 * @p2p: P2P module context from p2p_init() 376 * @addr: P2P Interface Address of the peer 377 * Returns: Pointer to the device entry or %NULL if not found 378 */ 379struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p, 380 const u8 *addr) 381{ 382 struct p2p_device *dev; 383 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 384 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0) 385 return dev; 386 } 387 return NULL; 388} 389 390 391/** 392 * p2p_create_device - Create a peer entry 393 * @p2p: P2P module context from p2p_init() 394 * @addr: P2P Device Address of the peer 395 * Returns: Pointer to the device entry or %NULL on failure 396 * 397 * If there is already an entry for the peer, it will be returned instead of 398 * creating a new one. 399 */ 400static struct p2p_device * p2p_create_device(struct p2p_data *p2p, 401 const u8 *addr) 402{ 403 struct p2p_device *dev, *oldest = NULL; 404 size_t count = 0; 405 406 dev = p2p_get_device(p2p, addr); 407 if (dev) 408 return dev; 409 410 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 411 count++; 412 if (oldest == NULL || 413 os_reltime_before(&dev->last_seen, &oldest->last_seen)) 414 oldest = dev; 415 } 416 if (count + 1 > p2p->cfg->max_peers && oldest) { 417 p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer"); 418 dl_list_del(&oldest->list); 419 p2p_device_free(p2p, oldest); 420 } 421 422 dev = os_zalloc(sizeof(*dev)); 423 if (dev == NULL) 424 return NULL; 425 dl_list_add(&p2p->devices, &dev->list); 426 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN); 427 428 return dev; 429} 430 431 432static void p2p_copy_client_info(struct p2p_device *dev, 433 struct p2p_client_info *cli) 434{ 435 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len); 436 dev->info.device_name[cli->dev_name_len] = '\0'; 437 dev->info.dev_capab = cli->dev_capab; 438 dev->info.config_methods = cli->config_methods; 439 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8); 440 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types; 441 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types, 442 dev->info.wps_sec_dev_type_list_len); 443} 444 445 446static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr, 447 const u8 *go_interface_addr, int freq, 448 const u8 *gi, size_t gi_len) 449{ 450 struct p2p_group_info info; 451 size_t c; 452 struct p2p_device *dev; 453 454 if (gi == NULL) 455 return 0; 456 457 if (p2p_group_info_parse(gi, gi_len, &info) < 0) 458 return -1; 459 460 /* 461 * Clear old data for this group; if the devices are still in the 462 * group, the information will be restored in the loop following this. 463 */ 464 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 465 if (os_memcmp(dev->member_in_go_iface, go_interface_addr, 466 ETH_ALEN) == 0) { 467 os_memset(dev->member_in_go_iface, 0, ETH_ALEN); 468 os_memset(dev->member_in_go_dev, 0, ETH_ALEN); 469 } 470 } 471 472 for (c = 0; c < info.num_clients; c++) { 473 struct p2p_client_info *cli = &info.client[c]; 474 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr, 475 ETH_ALEN) == 0) 476 continue; /* ignore our own entry */ 477 dev = p2p_get_device(p2p, cli->p2p_device_addr); 478 if (dev) { 479 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY | 480 P2P_DEV_PROBE_REQ_ONLY)) { 481 /* 482 * Update information since we have not 483 * received this directly from the client. 484 */ 485 p2p_copy_client_info(dev, cli); 486 } else { 487 /* 488 * Need to update P2P Client Discoverability 489 * flag since it is valid only in P2P Group 490 * Info attribute. 491 */ 492 dev->info.dev_capab &= 493 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 494 dev->info.dev_capab |= 495 cli->dev_capab & 496 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 497 } 498 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { 499 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; 500 } 501 } else { 502 dev = p2p_create_device(p2p, cli->p2p_device_addr); 503 if (dev == NULL) 504 continue; 505 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; 506 p2p_copy_client_info(dev, cli); 507 dev->oper_freq = freq; 508 p2p->cfg->dev_found(p2p->cfg->cb_ctx, 509 dev->info.p2p_device_addr, 510 &dev->info, 1); 511 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; 512 } 513 514 os_memcpy(dev->interface_addr, cli->p2p_interface_addr, 515 ETH_ALEN); 516 os_get_reltime(&dev->last_seen); 517 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN); 518 os_memcpy(dev->member_in_go_iface, go_interface_addr, 519 ETH_ALEN); 520 } 521 522 return 0; 523} 524 525 526static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev, 527 int probe_req, const struct p2p_message *msg) 528{ 529 os_memcpy(dev->info.device_name, msg->device_name, 530 sizeof(dev->info.device_name)); 531 532 if (msg->manufacturer && 533 msg->manufacturer_len < sizeof(dev->info.manufacturer)) { 534 os_memset(dev->info.manufacturer, 0, 535 sizeof(dev->info.manufacturer)); 536 os_memcpy(dev->info.manufacturer, msg->manufacturer, 537 msg->manufacturer_len); 538 } 539 540 if (msg->model_name && 541 msg->model_name_len < sizeof(dev->info.model_name)) { 542 os_memset(dev->info.model_name, 0, 543 sizeof(dev->info.model_name)); 544 os_memcpy(dev->info.model_name, msg->model_name, 545 msg->model_name_len); 546 } 547 548 if (msg->model_number && 549 msg->model_number_len < sizeof(dev->info.model_number)) { 550 os_memset(dev->info.model_number, 0, 551 sizeof(dev->info.model_number)); 552 os_memcpy(dev->info.model_number, msg->model_number, 553 msg->model_number_len); 554 } 555 556 if (msg->serial_number && 557 msg->serial_number_len < sizeof(dev->info.serial_number)) { 558 os_memset(dev->info.serial_number, 0, 559 sizeof(dev->info.serial_number)); 560 os_memcpy(dev->info.serial_number, msg->serial_number, 561 msg->serial_number_len); 562 } 563 564 if (msg->pri_dev_type) 565 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type, 566 sizeof(dev->info.pri_dev_type)); 567 else if (msg->wps_pri_dev_type) 568 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type, 569 sizeof(dev->info.pri_dev_type)); 570 571 if (msg->wps_sec_dev_type_list) { 572 os_memcpy(dev->info.wps_sec_dev_type_list, 573 msg->wps_sec_dev_type_list, 574 msg->wps_sec_dev_type_list_len); 575 dev->info.wps_sec_dev_type_list_len = 576 msg->wps_sec_dev_type_list_len; 577 } 578 579 if (msg->capability) { 580 /* 581 * P2P Client Discoverability bit is reserved in all frames 582 * that use this function, so do not change its value here. 583 */ 584 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 585 dev->info.dev_capab |= msg->capability[0] & 586 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 587 dev->info.group_capab = msg->capability[1]; 588 } 589 590 if (msg->ext_listen_timing) { 591 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing); 592 dev->ext_listen_interval = 593 WPA_GET_LE16(msg->ext_listen_timing + 2); 594 } 595 596 if (!probe_req) { 597 u16 new_config_methods; 598 new_config_methods = msg->config_methods ? 599 msg->config_methods : msg->wps_config_methods; 600 if (new_config_methods && 601 dev->info.config_methods != new_config_methods) { 602 p2p_dbg(p2p, "Update peer " MACSTR 603 " config_methods 0x%x -> 0x%x", 604 MAC2STR(dev->info.p2p_device_addr), 605 dev->info.config_methods, 606 new_config_methods); 607 dev->info.config_methods = new_config_methods; 608 } 609 } 610} 611 612 613static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies, 614 size_t ies_len) 615{ 616 const u8 *pos, *end; 617 u8 id, len; 618 619 wpabuf_free(dev->info.vendor_elems); 620 dev->info.vendor_elems = NULL; 621 622 end = ies + ies_len; 623 624 for (pos = ies; pos + 1 < end; pos += len) { 625 id = *pos++; 626 len = *pos++; 627 628 if (pos + len > end) 629 break; 630 631 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3) 632 continue; 633 634 if (len >= 4) { 635 u32 type = WPA_GET_BE32(pos); 636 637 if (type == WPA_IE_VENDOR_TYPE || 638 type == WMM_IE_VENDOR_TYPE || 639 type == WPS_IE_VENDOR_TYPE || 640 type == P2P_IE_VENDOR_TYPE || 641 type == WFD_IE_VENDOR_TYPE) 642 continue; 643 } 644 645 /* Unknown vendor element - make raw IE data available */ 646 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0) 647 break; 648 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len); 649 } 650} 651 652 653/** 654 * p2p_add_device - Add peer entries based on scan results or P2P frames 655 * @p2p: P2P module context from p2p_init() 656 * @addr: Source address of Beacon or Probe Response frame (may be either 657 * P2P Device Address or P2P Interface Address) 658 * @level: Signal level (signal strength of the received frame from the peer) 659 * @freq: Frequency on which the Beacon or Probe Response frame was received 660 * @rx_time: Time when the result was received 661 * @ies: IEs from the Beacon or Probe Response frame 662 * @ies_len: Length of ies buffer in octets 663 * @scan_res: Whether this was based on scan results 664 * Returns: 0 on success, -1 on failure 665 * 666 * If the scan result is for a GO, the clients in the group will also be added 667 * to the peer table. This function can also be used with some other frames 668 * like Provision Discovery Request that contains P2P Capability and P2P Device 669 * Info attributes. 670 */ 671int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, 672 struct os_reltime *rx_time, int level, const u8 *ies, 673 size_t ies_len, int scan_res) 674{ 675 struct p2p_device *dev; 676 struct p2p_message msg; 677 const u8 *p2p_dev_addr; 678 int i; 679 struct os_reltime time_now; 680 681 os_memset(&msg, 0, sizeof(msg)); 682 if (p2p_parse_ies(ies, ies_len, &msg)) { 683 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry"); 684 p2p_parse_free(&msg); 685 return -1; 686 } 687 688 if (msg.p2p_device_addr) 689 p2p_dev_addr = msg.p2p_device_addr; 690 else if (msg.device_id) 691 p2p_dev_addr = msg.device_id; 692 else { 693 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); 694 p2p_parse_free(&msg); 695 return -1; 696 } 697 698 if (!is_zero_ether_addr(p2p->peer_filter) && 699 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) { 700 p2p_dbg(p2p, "Do not add peer filter for " MACSTR 701 " due to peer filter", MAC2STR(p2p_dev_addr)); 702 p2p_parse_free(&msg); 703 return 0; 704 } 705 706 dev = p2p_create_device(p2p, p2p_dev_addr); 707 if (dev == NULL) { 708 p2p_parse_free(&msg); 709 return -1; 710 } 711 712 if (rx_time == NULL) { 713 os_get_reltime(&time_now); 714 rx_time = &time_now; 715 } 716 717 /* 718 * Update the device entry only if the new peer 719 * entry is newer than the one previously stored. 720 */ 721 if (dev->last_seen.sec > 0 && 722 os_reltime_before(rx_time, &dev->last_seen)) { 723 p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)", 724 (unsigned int) rx_time->sec, 725 (unsigned int) rx_time->usec, 726 (unsigned int) dev->last_seen.sec, 727 (unsigned int) dev->last_seen.usec); 728 p2p_parse_free(&msg); 729 return -1; 730 } 731 732 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime)); 733 734 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); 735 736 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0) 737 os_memcpy(dev->interface_addr, addr, ETH_ALEN); 738 if (msg.ssid && 739 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN || 740 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) 741 != 0)) { 742 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]); 743 dev->oper_ssid_len = msg.ssid[1]; 744 } 745 746 if (freq >= 2412 && freq <= 2484 && msg.ds_params && 747 *msg.ds_params >= 1 && *msg.ds_params <= 14) { 748 int ds_freq; 749 if (*msg.ds_params == 14) 750 ds_freq = 2484; 751 else 752 ds_freq = 2407 + *msg.ds_params * 5; 753 if (freq != ds_freq) { 754 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz", 755 freq, ds_freq); 756 freq = ds_freq; 757 } 758 } 759 760 if (dev->listen_freq && dev->listen_freq != freq && scan_res) { 761 p2p_dbg(p2p, "Update Listen frequency based on scan results (" 762 MACSTR " %d -> %d MHz (DS param %d)", 763 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq, 764 freq, msg.ds_params ? *msg.ds_params : -1); 765 } 766 if (scan_res) { 767 dev->listen_freq = freq; 768 if (msg.group_info) 769 dev->oper_freq = freq; 770 } 771 dev->info.level = level; 772 773 p2p_copy_wps_info(p2p, dev, 0, &msg); 774 775 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { 776 wpabuf_free(dev->info.wps_vendor_ext[i]); 777 dev->info.wps_vendor_ext[i] = NULL; 778 } 779 780 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { 781 if (msg.wps_vendor_ext[i] == NULL) 782 break; 783 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy( 784 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]); 785 if (dev->info.wps_vendor_ext[i] == NULL) 786 break; 787 } 788 789 if (msg.wfd_subelems) { 790 wpabuf_free(dev->info.wfd_subelems); 791 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); 792 } 793 794 if (scan_res) { 795 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, 796 msg.group_info, msg.group_info_len); 797 } 798 799 p2p_parse_free(&msg); 800 801 p2p_update_peer_vendor_elems(dev, ies, ies_len); 802 803 if (dev->flags & P2P_DEV_REPORTED) 804 return 0; 805 806 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)", 807 freq, (unsigned int) rx_time->sec, 808 (unsigned int) rx_time->usec); 809 if (dev->flags & P2P_DEV_USER_REJECTED) { 810 p2p_dbg(p2p, "Do not report rejected device"); 811 return 0; 812 } 813 814 if (dev->info.config_methods == 0 && 815 (freq == 2412 || freq == 2437 || freq == 2462)) { 816 /* 817 * If we have only seen a Beacon frame from a GO, we do not yet 818 * know what WPS config methods it supports. Since some 819 * applications use config_methods value from P2P-DEVICE-FOUND 820 * events, postpone reporting this peer until we've fully 821 * discovered its capabilities. 822 * 823 * At least for now, do this only if the peer was detected on 824 * one of the social channels since that peer can be easily be 825 * found again and there are no limitations of having to use 826 * passive scan on this channels, so this can be done through 827 * Probe Response frame that includes the config_methods 828 * information. 829 */ 830 p2p_dbg(p2p, "Do not report peer " MACSTR 831 " with unknown config methods", MAC2STR(addr)); 832 return 0; 833 } 834 835 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, 836 !(dev->flags & P2P_DEV_REPORTED_ONCE)); 837 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; 838 839 return 0; 840} 841 842 843static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev) 844{ 845 int i; 846 847 if (p2p->go_neg_peer == dev) { 848 /* 849 * If GO Negotiation is in progress, report that it has failed. 850 */ 851 p2p_go_neg_failed(p2p, dev, -1); 852 p2p->go_neg_peer = NULL; 853 } 854 if (p2p->invite_peer == dev) 855 p2p->invite_peer = NULL; 856 if (p2p->sd_peer == dev) 857 p2p->sd_peer = NULL; 858 if (p2p->pending_client_disc_go == dev) 859 p2p->pending_client_disc_go = NULL; 860 861 /* dev_lost() device, but only if it was previously dev_found() */ 862 if (dev->flags & P2P_DEV_REPORTED_ONCE) 863 p2p->cfg->dev_lost(p2p->cfg->cb_ctx, 864 dev->info.p2p_device_addr); 865 866 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { 867 wpabuf_free(dev->info.wps_vendor_ext[i]); 868 dev->info.wps_vendor_ext[i] = NULL; 869 } 870 871 wpabuf_free(dev->info.wfd_subelems); 872 wpabuf_free(dev->info.vendor_elems); 873 wpabuf_free(dev->go_neg_conf); 874 875 os_free(dev); 876} 877 878 879static int p2p_get_next_prog_freq(struct p2p_data *p2p) 880{ 881 struct p2p_channels *c; 882 struct p2p_reg_class *cla; 883 size_t cl, ch; 884 int found = 0; 885 u8 reg_class; 886 u8 channel; 887 int freq; 888 889 c = &p2p->cfg->channels; 890 for (cl = 0; cl < c->reg_classes; cl++) { 891 cla = &c->reg_class[cl]; 892 if (cla->reg_class != p2p->last_prog_scan_class) 893 continue; 894 for (ch = 0; ch < cla->channels; ch++) { 895 if (cla->channel[ch] == p2p->last_prog_scan_chan) { 896 found = 1; 897 break; 898 } 899 } 900 if (found) 901 break; 902 } 903 904 if (!found) { 905 /* Start from beginning */ 906 reg_class = c->reg_class[0].reg_class; 907 channel = c->reg_class[0].channel[0]; 908 } else { 909 /* Pick the next channel */ 910 ch++; 911 if (ch == cla->channels) { 912 cl++; 913 if (cl == c->reg_classes) 914 cl = 0; 915 ch = 0; 916 } 917 reg_class = c->reg_class[cl].reg_class; 918 channel = c->reg_class[cl].channel[ch]; 919 } 920 921 freq = p2p_channel_to_freq(reg_class, channel); 922 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz", 923 reg_class, channel, freq); 924 p2p->last_prog_scan_class = reg_class; 925 p2p->last_prog_scan_chan = channel; 926 927 if (freq == 2412 || freq == 2437 || freq == 2462) 928 return 0; /* No need to add social channels */ 929 return freq; 930} 931 932 933static void p2p_search(struct p2p_data *p2p) 934{ 935 int freq = 0; 936 enum p2p_scan_type type; 937 u16 pw_id = DEV_PW_DEFAULT; 938 int res; 939 940 if (p2p->drv_in_listen) { 941 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing"); 942 return; 943 } 944 p2p->cfg->stop_listen(p2p->cfg->cb_ctx); 945 946 if (p2p->find_type == P2P_FIND_PROGRESSIVE && 947 (freq = p2p_get_next_prog_freq(p2p)) > 0) { 948 type = P2P_SCAN_SOCIAL_PLUS_ONE; 949 p2p_dbg(p2p, "Starting search (+ freq %u)", freq); 950 } else { 951 type = P2P_SCAN_SOCIAL; 952 p2p_dbg(p2p, "Starting search"); 953 } 954 955 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq, 956 p2p->num_req_dev_types, p2p->req_dev_types, 957 p2p->find_dev_id, pw_id); 958 if (res < 0) { 959 p2p_dbg(p2p, "Scan request failed"); 960 p2p_continue_find(p2p); 961 } else { 962 p2p_dbg(p2p, "Running p2p_scan"); 963 p2p->p2p_scan_running = 1; 964 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); 965 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, 966 p2p, NULL); 967 } 968} 969 970 971static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx) 972{ 973 struct p2p_data *p2p = eloop_ctx; 974 p2p_dbg(p2p, "Find timeout -> stop"); 975 p2p_stop_find(p2p); 976} 977 978 979static int p2p_run_after_scan(struct p2p_data *p2p) 980{ 981 struct p2p_device *dev; 982 enum p2p_after_scan op; 983 984 if (p2p->after_scan_tx) { 985 p2p->after_scan_tx_in_progress = 1; 986 p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion"); 987 p2p->cfg->send_action(p2p->cfg->cb_ctx, 988 p2p->after_scan_tx->freq, 989 p2p->after_scan_tx->dst, 990 p2p->after_scan_tx->src, 991 p2p->after_scan_tx->bssid, 992 (u8 *) (p2p->after_scan_tx + 1), 993 p2p->after_scan_tx->len, 994 p2p->after_scan_tx->wait_time); 995 os_free(p2p->after_scan_tx); 996 p2p->after_scan_tx = NULL; 997 return 1; 998 } 999 1000 op = p2p->start_after_scan; 1001 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; 1002 switch (op) { 1003 case P2P_AFTER_SCAN_NOTHING: 1004 break; 1005 case P2P_AFTER_SCAN_LISTEN: 1006 p2p_dbg(p2p, "Start previously requested Listen state"); 1007 p2p_listen(p2p, p2p->pending_listen_sec * 1000 + 1008 p2p->pending_listen_usec / 1000); 1009 return 1; 1010 case P2P_AFTER_SCAN_CONNECT: 1011 p2p_dbg(p2p, "Start previously requested connect with " MACSTR, 1012 MAC2STR(p2p->after_scan_peer)); 1013 dev = p2p_get_device(p2p, p2p->after_scan_peer); 1014 if (dev == NULL) { 1015 p2p_dbg(p2p, "Peer not known anymore"); 1016 break; 1017 } 1018 p2p_connect_send(p2p, dev); 1019 return 1; 1020 } 1021 1022 return 0; 1023} 1024 1025 1026static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx) 1027{ 1028 struct p2p_data *p2p = eloop_ctx; 1029 int running; 1030 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running); 1031 running = p2p->p2p_scan_running; 1032 /* Make sure we recover from missed scan results callback */ 1033 p2p->p2p_scan_running = 0; 1034 1035 if (running) 1036 p2p_run_after_scan(p2p); 1037} 1038 1039 1040static void p2p_free_req_dev_types(struct p2p_data *p2p) 1041{ 1042 p2p->num_req_dev_types = 0; 1043 os_free(p2p->req_dev_types); 1044 p2p->req_dev_types = NULL; 1045} 1046 1047 1048int p2p_find(struct p2p_data *p2p, unsigned int timeout, 1049 enum p2p_discovery_type type, 1050 unsigned int num_req_dev_types, const u8 *req_dev_types, 1051 const u8 *dev_id, unsigned int search_delay) 1052{ 1053 int res; 1054 1055 p2p_dbg(p2p, "Starting find (type=%d)", type); 1056 os_get_reltime(&p2p->find_start); 1057 if (p2p->p2p_scan_running) { 1058 p2p_dbg(p2p, "p2p_scan is already running"); 1059 } 1060 1061 p2p_free_req_dev_types(p2p); 1062 if (req_dev_types && num_req_dev_types) { 1063 p2p->req_dev_types = os_malloc(num_req_dev_types * 1064 WPS_DEV_TYPE_LEN); 1065 if (p2p->req_dev_types == NULL) 1066 return -1; 1067 os_memcpy(p2p->req_dev_types, req_dev_types, 1068 num_req_dev_types * WPS_DEV_TYPE_LEN); 1069 p2p->num_req_dev_types = num_req_dev_types; 1070 } 1071 1072 if (dev_id) { 1073 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN); 1074 p2p->find_dev_id = p2p->find_dev_id_buf; 1075 } else 1076 p2p->find_dev_id = NULL; 1077 1078 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; 1079 p2p_clear_timeout(p2p); 1080 p2p->cfg->stop_listen(p2p->cfg->cb_ctx); 1081 p2p->find_type = type; 1082 p2p_device_clear_reported(p2p); 1083 p2p_set_state(p2p, P2P_SEARCH); 1084 p2p->search_delay = search_delay; 1085 p2p->in_search_delay = 0; 1086 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); 1087 p2p->last_p2p_find_timeout = timeout; 1088 if (timeout) 1089 eloop_register_timeout(timeout, 0, p2p_find_timeout, 1090 p2p, NULL); 1091 switch (type) { 1092 case P2P_FIND_START_WITH_FULL: 1093 case P2P_FIND_PROGRESSIVE: 1094 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0, 1095 p2p->num_req_dev_types, 1096 p2p->req_dev_types, dev_id, 1097 DEV_PW_DEFAULT); 1098 break; 1099 case P2P_FIND_ONLY_SOCIAL: 1100 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0, 1101 p2p->num_req_dev_types, 1102 p2p->req_dev_types, dev_id, 1103 DEV_PW_DEFAULT); 1104 break; 1105 default: 1106 return -1; 1107 } 1108 1109 if (res == 0) { 1110 p2p_dbg(p2p, "Running p2p_scan"); 1111 p2p->p2p_scan_running = 1; 1112 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); 1113 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout, 1114 p2p, NULL); 1115 } else if (p2p->p2p_scan_running) { 1116 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running"); 1117 /* wait for the previous p2p_scan to complete */ 1118 res = 0; /* do not report failure */ 1119 } else { 1120 p2p_dbg(p2p, "Failed to start p2p_scan"); 1121 p2p_set_state(p2p, P2P_IDLE); 1122 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); 1123 } 1124 1125 return res; 1126} 1127 1128 1129void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq) 1130{ 1131 p2p_dbg(p2p, "Stopping find"); 1132 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL); 1133 p2p_clear_timeout(p2p); 1134 if (p2p->state == P2P_SEARCH) 1135 p2p->cfg->find_stopped(p2p->cfg->cb_ctx); 1136 p2p_set_state(p2p, P2P_IDLE); 1137 p2p_free_req_dev_types(p2p); 1138 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; 1139 if (p2p->go_neg_peer) 1140 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE; 1141 p2p->go_neg_peer = NULL; 1142 p2p->sd_peer = NULL; 1143 p2p->invite_peer = NULL; 1144 p2p_stop_listen_for_freq(p2p, freq); 1145} 1146 1147 1148void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq) 1149{ 1150 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) { 1151 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response"); 1152 return; 1153 } 1154 if (p2p->in_listen) { 1155 p2p->in_listen = 0; 1156 p2p_clear_timeout(p2p); 1157 } 1158 if (p2p->drv_in_listen) { 1159 /* 1160 * The driver may not deliver callback to p2p_listen_end() 1161 * when the operation gets canceled, so clear the internal 1162 * variable that is tracking driver state. 1163 */ 1164 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen); 1165 p2p->drv_in_listen = 0; 1166 } 1167 p2p->cfg->stop_listen(p2p->cfg->cb_ctx); 1168} 1169 1170 1171void p2p_stop_listen(struct p2p_data *p2p) 1172{ 1173 if (p2p->state != P2P_LISTEN_ONLY) { 1174 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state."); 1175 return; 1176 } 1177 1178 p2p_stop_listen_for_freq(p2p, 0); 1179 p2p_set_state(p2p, P2P_IDLE); 1180} 1181 1182 1183void p2p_stop_find(struct p2p_data *p2p) 1184{ 1185 p2p->pending_listen_freq = 0; 1186 p2p_stop_find_for_freq(p2p, 0); 1187} 1188 1189 1190static int p2p_prepare_channel_pref(struct p2p_data *p2p, 1191 unsigned int force_freq, 1192 unsigned int pref_freq, int go) 1193{ 1194 u8 op_class, op_channel; 1195 unsigned int freq = force_freq ? force_freq : pref_freq; 1196 1197 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d", 1198 force_freq, pref_freq, go); 1199 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) { 1200 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq); 1201 return -1; 1202 } 1203 1204 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) && 1205 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class, 1206 op_channel))) { 1207 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P", 1208 freq, op_class, op_channel); 1209 return -1; 1210 } 1211 1212 p2p->op_reg_class = op_class; 1213 p2p->op_channel = op_channel; 1214 1215 if (force_freq) { 1216 p2p->channels.reg_classes = 1; 1217 p2p->channels.reg_class[0].channels = 1; 1218 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class; 1219 p2p->channels.reg_class[0].channel[0] = p2p->op_channel; 1220 } else { 1221 os_memcpy(&p2p->channels, &p2p->cfg->channels, 1222 sizeof(struct p2p_channels)); 1223 } 1224 1225 return 0; 1226} 1227 1228 1229static void p2p_prepare_channel_best(struct p2p_data *p2p) 1230{ 1231 u8 op_class, op_channel; 1232 const int op_classes_5ghz[] = { 124, 115, 0 }; 1233 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 }; 1234 const int op_classes_vht[] = { 128, 0 }; 1235 1236 p2p_dbg(p2p, "Prepare channel best"); 1237 1238 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 && 1239 p2p_supported_freq(p2p, p2p->best_freq_overall) && 1240 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel) 1241 == 0) { 1242 p2p_dbg(p2p, "Select best overall channel as operating channel preference"); 1243 p2p->op_reg_class = op_class; 1244 p2p->op_channel = op_channel; 1245 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 && 1246 p2p_supported_freq(p2p, p2p->best_freq_5) && 1247 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel) 1248 == 0) { 1249 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference"); 1250 p2p->op_reg_class = op_class; 1251 p2p->op_channel = op_channel; 1252 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 && 1253 p2p_supported_freq(p2p, p2p->best_freq_24) && 1254 p2p_freq_to_channel(p2p->best_freq_24, &op_class, 1255 &op_channel) == 0) { 1256 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference"); 1257 p2p->op_reg_class = op_class; 1258 p2p->op_channel = op_channel; 1259 } else if (p2p->cfg->num_pref_chan > 0 && 1260 p2p_channels_includes(&p2p->cfg->channels, 1261 p2p->cfg->pref_chan[0].op_class, 1262 p2p->cfg->pref_chan[0].chan)) { 1263 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference"); 1264 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class; 1265 p2p->op_channel = p2p->cfg->pref_chan[0].chan; 1266 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht, 1267 &p2p->op_reg_class, &p2p->op_channel) == 1268 0) { 1269 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference", 1270 p2p->op_reg_class, p2p->op_channel); 1271 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40, 1272 &p2p->op_reg_class, &p2p->op_channel) == 1273 0) { 1274 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference", 1275 p2p->op_reg_class, p2p->op_channel); 1276 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz, 1277 &p2p->op_reg_class, &p2p->op_channel) == 1278 0) { 1279 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference", 1280 p2p->op_reg_class, p2p->op_channel); 1281 } else if (p2p_channels_includes(&p2p->cfg->channels, 1282 p2p->cfg->op_reg_class, 1283 p2p->cfg->op_channel)) { 1284 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference"); 1285 p2p->op_reg_class = p2p->cfg->op_reg_class; 1286 p2p->op_channel = p2p->cfg->op_channel; 1287 } else if (p2p_channel_random_social(&p2p->cfg->channels, 1288 &p2p->op_reg_class, 1289 &p2p->op_channel) == 0) { 1290 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference", 1291 p2p->op_reg_class, p2p->op_channel); 1292 } else { 1293 /* Select any random available channel from the first available 1294 * operating class */ 1295 p2p_channel_select(&p2p->cfg->channels, NULL, 1296 &p2p->op_reg_class, 1297 &p2p->op_channel); 1298 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference", 1299 p2p->op_channel, p2p->op_reg_class); 1300 } 1301 1302 os_memcpy(&p2p->channels, &p2p->cfg->channels, 1303 sizeof(struct p2p_channels)); 1304} 1305 1306 1307/** 1308 * p2p_prepare_channel - Select operating channel for GO Negotiation 1309 * @p2p: P2P module context from p2p_init() 1310 * @dev: Selected peer device 1311 * @force_freq: Forced frequency in MHz or 0 if not forced 1312 * @pref_freq: Preferred frequency in MHz or 0 if no preference 1313 * @go: Whether the local end will be forced to be GO 1314 * Returns: 0 on success, -1 on failure (channel not supported for P2P) 1315 * 1316 * This function is used to do initial operating channel selection for GO 1317 * Negotiation prior to having received peer information. The selected channel 1318 * may be further optimized in p2p_reselect_channel() once the peer information 1319 * is available. 1320 */ 1321int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev, 1322 unsigned int force_freq, unsigned int pref_freq, int go) 1323{ 1324 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d", 1325 force_freq, pref_freq, go); 1326 if (force_freq || pref_freq) { 1327 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) < 1328 0) 1329 return -1; 1330 } else { 1331 p2p_prepare_channel_best(p2p); 1332 } 1333 p2p_channels_dump(p2p, "prepared channels", &p2p->channels); 1334 if (go) 1335 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq); 1336 else if (!force_freq) 1337 p2p_channels_union(&p2p->channels, &p2p->cfg->cli_channels, 1338 &p2p->channels); 1339 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels); 1340 1341 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s", 1342 p2p->op_reg_class, p2p->op_channel, 1343 force_freq ? " (forced)" : ""); 1344 1345 if (force_freq) 1346 dev->flags |= P2P_DEV_FORCE_FREQ; 1347 else 1348 dev->flags &= ~P2P_DEV_FORCE_FREQ; 1349 1350 return 0; 1351} 1352 1353 1354static void p2p_set_dev_persistent(struct p2p_device *dev, 1355 int persistent_group) 1356{ 1357 switch (persistent_group) { 1358 case 0: 1359 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP | 1360 P2P_DEV_PREFER_PERSISTENT_RECONN); 1361 break; 1362 case 1: 1363 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP; 1364 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN; 1365 break; 1366 case 2: 1367 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP | 1368 P2P_DEV_PREFER_PERSISTENT_RECONN; 1369 break; 1370 } 1371} 1372 1373 1374int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr, 1375 enum p2p_wps_method wps_method, 1376 int go_intent, const u8 *own_interface_addr, 1377 unsigned int force_freq, int persistent_group, 1378 const u8 *force_ssid, size_t force_ssid_len, 1379 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id) 1380{ 1381 struct p2p_device *dev; 1382 1383 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR 1384 " GO Intent=%d Intended Interface Address=" MACSTR 1385 " wps_method=%d persistent_group=%d pd_before_go_neg=%d " 1386 "oob_pw_id=%u", 1387 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), 1388 wps_method, persistent_group, pd_before_go_neg, oob_pw_id); 1389 1390 dev = p2p_get_device(p2p, peer_addr); 1391 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) { 1392 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR, 1393 MAC2STR(peer_addr)); 1394 return -1; 1395 } 1396 1397 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, 1398 go_intent == 15) < 0) 1399 return -1; 1400 1401 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) { 1402 if (!(dev->info.dev_capab & 1403 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) { 1404 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR 1405 " that is in a group and is not discoverable", 1406 MAC2STR(peer_addr)); 1407 return -1; 1408 } 1409 if (dev->oper_freq <= 0) { 1410 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR 1411 " with incomplete information", 1412 MAC2STR(peer_addr)); 1413 return -1; 1414 } 1415 1416 /* 1417 * First, try to connect directly. If the peer does not 1418 * acknowledge frames, assume it is sleeping and use device 1419 * discoverability via the GO at that point. 1420 */ 1421 } 1422 1423 p2p->ssid_set = 0; 1424 if (force_ssid) { 1425 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", 1426 force_ssid, force_ssid_len); 1427 os_memcpy(p2p->ssid, force_ssid, force_ssid_len); 1428 p2p->ssid_len = force_ssid_len; 1429 p2p->ssid_set = 1; 1430 } 1431 1432 dev->flags &= ~P2P_DEV_NOT_YET_READY; 1433 dev->flags &= ~P2P_DEV_USER_REJECTED; 1434 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; 1435 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; 1436 if (pd_before_go_neg) 1437 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG; 1438 else { 1439 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG; 1440 /* 1441 * Assign dialog token and tie breaker here to use the same 1442 * values in each retry within the same GO Negotiation exchange. 1443 */ 1444 dev->dialog_token++; 1445 if (dev->dialog_token == 0) 1446 dev->dialog_token = 1; 1447 dev->tie_breaker = p2p->next_tie_breaker; 1448 p2p->next_tie_breaker = !p2p->next_tie_breaker; 1449 } 1450 dev->connect_reqs = 0; 1451 dev->go_neg_req_sent = 0; 1452 dev->go_state = UNKNOWN_GO; 1453 p2p_set_dev_persistent(dev, persistent_group); 1454 p2p->go_intent = go_intent; 1455 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); 1456 1457 if (p2p->state != P2P_IDLE) 1458 p2p_stop_find(p2p); 1459 1460 if (p2p->after_scan_tx) { 1461 /* 1462 * We need to drop the pending frame to avoid issues with the 1463 * new GO Negotiation, e.g., when the pending frame was from a 1464 * previous attempt at starting a GO Negotiation. 1465 */ 1466 p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion"); 1467 os_free(p2p->after_scan_tx); 1468 p2p->after_scan_tx = NULL; 1469 } 1470 1471 dev->wps_method = wps_method; 1472 dev->oob_pw_id = oob_pw_id; 1473 dev->status = P2P_SC_SUCCESS; 1474 1475 if (p2p->p2p_scan_running) { 1476 p2p_dbg(p2p, "p2p_scan running - delay connect send"); 1477 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT; 1478 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN); 1479 return 0; 1480 } 1481 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING; 1482 1483 return p2p_connect_send(p2p, dev); 1484} 1485 1486 1487int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr, 1488 enum p2p_wps_method wps_method, 1489 int go_intent, const u8 *own_interface_addr, 1490 unsigned int force_freq, int persistent_group, 1491 const u8 *force_ssid, size_t force_ssid_len, 1492 unsigned int pref_freq, u16 oob_pw_id) 1493{ 1494 struct p2p_device *dev; 1495 1496 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR 1497 " GO Intent=%d Intended Interface Address=" MACSTR 1498 " wps_method=%d persistent_group=%d oob_pw_id=%u", 1499 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr), 1500 wps_method, persistent_group, oob_pw_id); 1501 1502 dev = p2p_get_device(p2p, peer_addr); 1503 if (dev == NULL) { 1504 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR, 1505 MAC2STR(peer_addr)); 1506 return -1; 1507 } 1508 1509 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent == 1510 15) < 0) 1511 return -1; 1512 1513 p2p->ssid_set = 0; 1514 if (force_ssid) { 1515 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID", 1516 force_ssid, force_ssid_len); 1517 os_memcpy(p2p->ssid, force_ssid, force_ssid_len); 1518 p2p->ssid_len = force_ssid_len; 1519 p2p->ssid_set = 1; 1520 } 1521 1522 dev->flags &= ~P2P_DEV_NOT_YET_READY; 1523 dev->flags &= ~P2P_DEV_USER_REJECTED; 1524 dev->go_neg_req_sent = 0; 1525 dev->go_state = UNKNOWN_GO; 1526 p2p_set_dev_persistent(dev, persistent_group); 1527 p2p->go_intent = go_intent; 1528 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); 1529 1530 dev->wps_method = wps_method; 1531 dev->oob_pw_id = oob_pw_id; 1532 dev->status = P2P_SC_SUCCESS; 1533 1534 return 0; 1535} 1536 1537 1538void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr, 1539 struct p2p_device *dev, struct p2p_message *msg) 1540{ 1541 os_get_reltime(&dev->last_seen); 1542 1543 p2p_copy_wps_info(p2p, dev, 0, msg); 1544 1545 if (msg->listen_channel) { 1546 int freq; 1547 freq = p2p_channel_to_freq(msg->listen_channel[3], 1548 msg->listen_channel[4]); 1549 if (freq < 0) { 1550 p2p_dbg(p2p, "Unknown peer Listen channel: " 1551 "country=%c%c(0x%02x) reg_class=%u channel=%u", 1552 msg->listen_channel[0], 1553 msg->listen_channel[1], 1554 msg->listen_channel[2], 1555 msg->listen_channel[3], 1556 msg->listen_channel[4]); 1557 } else { 1558 p2p_dbg(p2p, "Update peer " MACSTR 1559 " Listen channel: %u -> %u MHz", 1560 MAC2STR(dev->info.p2p_device_addr), 1561 dev->listen_freq, freq); 1562 dev->listen_freq = freq; 1563 } 1564 } 1565 1566 if (msg->wfd_subelems) { 1567 wpabuf_free(dev->info.wfd_subelems); 1568 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems); 1569 } 1570 1571 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { 1572 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY; 1573 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request"); 1574 } else { 1575 p2p_dbg(p2p, "Created device entry based on GO Neg Req: " 1576 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' " 1577 "listen_freq=%d", 1578 MAC2STR(dev->info.p2p_device_addr), 1579 dev->info.dev_capab, dev->info.group_capab, 1580 dev->info.device_name, dev->listen_freq); 1581 } 1582 1583 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY; 1584 1585 if (dev->flags & P2P_DEV_USER_REJECTED) { 1586 p2p_dbg(p2p, "Do not report rejected device"); 1587 return; 1588 } 1589 1590 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info, 1591 !(dev->flags & P2P_DEV_REPORTED_ONCE)); 1592 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; 1593} 1594 1595 1596void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len) 1597{ 1598 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); 1599 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2); 1600 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2], 1601 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len); 1602 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len; 1603} 1604 1605 1606int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params) 1607{ 1608 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len); 1609 p2p_random(params->passphrase, p2p->cfg->passphrase_len); 1610 return 0; 1611} 1612 1613 1614void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer) 1615{ 1616 struct p2p_go_neg_results res; 1617 int go = peer->go_state == LOCAL_GO; 1618 struct p2p_channels intersection; 1619 int freqs; 1620 size_t i, j; 1621 1622 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)", 1623 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer"); 1624 1625 os_memset(&res, 0, sizeof(res)); 1626 res.role_go = go; 1627 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN); 1628 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN); 1629 res.wps_method = peer->wps_method; 1630 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) { 1631 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN) 1632 res.persistent_group = 2; 1633 else 1634 res.persistent_group = 1; 1635 } 1636 1637 if (go) { 1638 /* Setup AP mode for WPS provisioning */ 1639 res.freq = p2p_channel_to_freq(p2p->op_reg_class, 1640 p2p->op_channel); 1641 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); 1642 res.ssid_len = p2p->ssid_len; 1643 p2p_random(res.passphrase, p2p->cfg->passphrase_len); 1644 } else { 1645 res.freq = peer->oper_freq; 1646 if (p2p->ssid_len) { 1647 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len); 1648 res.ssid_len = p2p->ssid_len; 1649 } 1650 } 1651 1652 p2p_channels_dump(p2p, "own channels", &p2p->channels); 1653 p2p_channels_dump(p2p, "peer channels", &peer->channels); 1654 p2p_channels_intersect(&p2p->channels, &peer->channels, 1655 &intersection); 1656 if (go) { 1657 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq); 1658 p2p_channels_dump(p2p, "intersection after no-GO removal", 1659 &intersection); 1660 } 1661 freqs = 0; 1662 for (i = 0; i < intersection.reg_classes; i++) { 1663 struct p2p_reg_class *c = &intersection.reg_class[i]; 1664 if (freqs + 1 == P2P_MAX_CHANNELS) 1665 break; 1666 for (j = 0; j < c->channels; j++) { 1667 int freq; 1668 if (freqs + 1 == P2P_MAX_CHANNELS) 1669 break; 1670 freq = p2p_channel_to_freq(c->reg_class, c->channel[j]); 1671 if (freq < 0) 1672 continue; 1673 res.freq_list[freqs++] = freq; 1674 } 1675 } 1676 1677 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout; 1678 1679 p2p_clear_timeout(p2p); 1680 p2p->ssid_set = 0; 1681 peer->go_neg_req_sent = 0; 1682 peer->wps_method = WPS_NOT_READY; 1683 peer->oob_pw_id = 0; 1684 wpabuf_free(peer->go_neg_conf); 1685 peer->go_neg_conf = NULL; 1686 1687 p2p_set_state(p2p, P2P_PROVISIONING); 1688 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res); 1689} 1690 1691 1692static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa, 1693 const u8 *data, size_t len, int rx_freq) 1694{ 1695 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa)); 1696 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len); 1697 1698 if (len < 1) 1699 return; 1700 1701 switch (data[0]) { 1702 case P2P_GO_NEG_REQ: 1703 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq); 1704 break; 1705 case P2P_GO_NEG_RESP: 1706 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq); 1707 break; 1708 case P2P_GO_NEG_CONF: 1709 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1); 1710 break; 1711 case P2P_INVITATION_REQ: 1712 p2p_process_invitation_req(p2p, sa, data + 1, len - 1, 1713 rx_freq); 1714 break; 1715 case P2P_INVITATION_RESP: 1716 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 1717 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1); 1718 break; 1719 case P2P_PROV_DISC_REQ: 1720 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq); 1721 break; 1722 case P2P_PROV_DISC_RESP: 1723 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1); 1724 break; 1725 case P2P_DEV_DISC_REQ: 1726 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq); 1727 break; 1728 case P2P_DEV_DISC_RESP: 1729 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1); 1730 break; 1731 default: 1732 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d", 1733 data[0]); 1734 break; 1735 } 1736} 1737 1738 1739static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, 1740 const u8 *sa, const u8 *bssid, const u8 *data, 1741 size_t len, int freq) 1742{ 1743 if (len < 1) 1744 return; 1745 1746 switch (data[0]) { 1747 case WLAN_PA_VENDOR_SPECIFIC: 1748 data++; 1749 len--; 1750 if (len < 4) 1751 return; 1752 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) 1753 return; 1754 1755 data += 4; 1756 len -= 4; 1757 1758 p2p_rx_p2p_action(p2p, sa, data, len, freq); 1759 break; 1760 case WLAN_PA_GAS_INITIAL_REQ: 1761 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq); 1762 break; 1763 case WLAN_PA_GAS_INITIAL_RESP: 1764 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq); 1765 break; 1766 case WLAN_PA_GAS_COMEBACK_REQ: 1767 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq); 1768 break; 1769 case WLAN_PA_GAS_COMEBACK_RESP: 1770 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq); 1771 break; 1772 } 1773} 1774 1775 1776void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa, 1777 const u8 *bssid, u8 category, 1778 const u8 *data, size_t len, int freq) 1779{ 1780 if (category == WLAN_ACTION_PUBLIC) { 1781 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq); 1782 return; 1783 } 1784 1785 if (category != WLAN_ACTION_VENDOR_SPECIFIC) 1786 return; 1787 1788 if (len < 4) 1789 return; 1790 1791 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE) 1792 return; 1793 data += 4; 1794 len -= 4; 1795 1796 /* P2P action frame */ 1797 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa)); 1798 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len); 1799 1800 if (len < 1) 1801 return; 1802 switch (data[0]) { 1803 case P2P_NOA: 1804 p2p_dbg(p2p, "Received P2P Action - Notice of Absence"); 1805 /* TODO */ 1806 break; 1807 case P2P_PRESENCE_REQ: 1808 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq); 1809 break; 1810 case P2P_PRESENCE_RESP: 1811 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1); 1812 break; 1813 case P2P_GO_DISC_REQ: 1814 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq); 1815 break; 1816 default: 1817 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]); 1818 break; 1819 } 1820} 1821 1822 1823static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx) 1824{ 1825 struct p2p_data *p2p = eloop_ctx; 1826 if (p2p->go_neg_peer == NULL) 1827 return; 1828 if (p2p->pending_listen_freq) { 1829 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start"); 1830 p2p->pending_listen_freq = 0; 1831 } 1832 p2p->cfg->stop_listen(p2p->cfg->cb_ctx); 1833 p2p->go_neg_peer->status = P2P_SC_SUCCESS; 1834 /* 1835 * Set new timeout to make sure a previously set one does not expire 1836 * too quickly while waiting for the GO Negotiation to complete. 1837 */ 1838 p2p_set_timeout(p2p, 0, 500000); 1839 p2p_connect_send(p2p, p2p->go_neg_peer); 1840} 1841 1842 1843static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx) 1844{ 1845 struct p2p_data *p2p = eloop_ctx; 1846 if (p2p->invite_peer == NULL) 1847 return; 1848 if (p2p->pending_listen_freq) { 1849 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start"); 1850 p2p->pending_listen_freq = 0; 1851 } 1852 p2p->cfg->stop_listen(p2p->cfg->cb_ctx); 1853 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr, 1854 p2p->invite_dev_pw_id); 1855} 1856 1857 1858static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr, 1859 const u8 *ie, size_t ie_len) 1860{ 1861 struct p2p_message msg; 1862 struct p2p_device *dev; 1863 1864 os_memset(&msg, 0, sizeof(msg)); 1865 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL) 1866 { 1867 p2p_parse_free(&msg); 1868 return; /* not a P2P probe */ 1869 } 1870 1871 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN || 1872 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) 1873 != 0) { 1874 /* The Probe Request is not part of P2P Device Discovery. It is 1875 * not known whether the source address of the frame is the P2P 1876 * Device Address or P2P Interface Address. Do not add a new 1877 * peer entry based on this frames. 1878 */ 1879 p2p_parse_free(&msg); 1880 return; 1881 } 1882 1883 dev = p2p_get_device(p2p, addr); 1884 if (dev) { 1885 if (dev->country[0] == 0 && msg.listen_channel) 1886 os_memcpy(dev->country, msg.listen_channel, 3); 1887 os_get_reltime(&dev->last_seen); 1888 p2p_parse_free(&msg); 1889 return; /* already known */ 1890 } 1891 1892 dev = p2p_create_device(p2p, addr); 1893 if (dev == NULL) { 1894 p2p_parse_free(&msg); 1895 return; 1896 } 1897 1898 os_get_reltime(&dev->last_seen); 1899 dev->flags |= P2P_DEV_PROBE_REQ_ONLY; 1900 1901 if (msg.listen_channel) { 1902 os_memcpy(dev->country, msg.listen_channel, 3); 1903 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3], 1904 msg.listen_channel[4]); 1905 } 1906 1907 p2p_copy_wps_info(p2p, dev, 1, &msg); 1908 1909 if (msg.wfd_subelems) { 1910 wpabuf_free(dev->info.wfd_subelems); 1911 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems); 1912 } 1913 1914 p2p_parse_free(&msg); 1915 1916 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR 1917 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d", 1918 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab, 1919 dev->info.group_capab, dev->info.device_name, 1920 dev->listen_freq); 1921} 1922 1923 1924struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p, 1925 const u8 *addr, 1926 struct p2p_message *msg) 1927{ 1928 struct p2p_device *dev; 1929 1930 dev = p2p_get_device(p2p, addr); 1931 if (dev) { 1932 os_get_reltime(&dev->last_seen); 1933 return dev; /* already known */ 1934 } 1935 1936 dev = p2p_create_device(p2p, addr); 1937 if (dev == NULL) 1938 return NULL; 1939 1940 p2p_add_dev_info(p2p, addr, dev, msg); 1941 1942 return dev; 1943} 1944 1945 1946static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type) 1947{ 1948 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0) 1949 return 1; 1950 if (os_memcmp(dev_type, req_dev_type, 2) == 0 && 1951 WPA_GET_BE32(&req_dev_type[2]) == 0 && 1952 WPA_GET_BE16(&req_dev_type[6]) == 0) 1953 return 1; /* Category match with wildcard OUI/sub-category */ 1954 return 0; 1955} 1956 1957 1958int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[], 1959 size_t num_req_dev_type) 1960{ 1961 size_t i; 1962 for (i = 0; i < num_req_dev_type; i++) { 1963 if (dev_type_match(dev_type, req_dev_type[i])) 1964 return 1; 1965 } 1966 return 0; 1967} 1968 1969 1970/** 1971 * p2p_match_dev_type - Match local device type with requested type 1972 * @p2p: P2P module context from p2p_init() 1973 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs) 1974 * Returns: 1 on match, 0 on mismatch 1975 * 1976 * This function can be used to match the Requested Device Type attribute in 1977 * WPS IE with the local device types for deciding whether to reply to a Probe 1978 * Request frame. 1979 */ 1980int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps) 1981{ 1982 struct wps_parse_attr attr; 1983 size_t i; 1984 1985 if (wps_parse_msg(wps, &attr)) 1986 return 1; /* assume no Requested Device Type attributes */ 1987 1988 if (attr.num_req_dev_type == 0) 1989 return 1; /* no Requested Device Type attributes -> match */ 1990 1991 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type, 1992 attr.num_req_dev_type)) 1993 return 1; /* Own Primary Device Type matches */ 1994 1995 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) 1996 if (dev_type_list_match(p2p->cfg->sec_dev_type[i], 1997 attr.req_dev_type, 1998 attr.num_req_dev_type)) 1999 return 1; /* Own Secondary Device Type matches */ 2000 2001 /* No matching device type found */ 2002 return 0; 2003} 2004 2005 2006struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p) 2007{ 2008 struct wpabuf *buf; 2009 u8 *len; 2010 int pw_id = -1; 2011 size_t extra = 0; 2012 2013#ifdef CONFIG_WIFI_DISPLAY 2014 if (p2p->wfd_ie_probe_resp) 2015 extra = wpabuf_len(p2p->wfd_ie_probe_resp); 2016#endif /* CONFIG_WIFI_DISPLAY */ 2017 2018 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]) 2019 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]); 2020 2021 buf = wpabuf_alloc(1000 + extra); 2022 if (buf == NULL) 2023 return NULL; 2024 2025 if (p2p->go_neg_peer) { 2026 /* Advertise immediate availability of WPS credential */ 2027 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method); 2028 } 2029 2030 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) { 2031 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response"); 2032 wpabuf_free(buf); 2033 return NULL; 2034 } 2035 2036#ifdef CONFIG_WIFI_DISPLAY 2037 if (p2p->wfd_ie_probe_resp) 2038 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp); 2039#endif /* CONFIG_WIFI_DISPLAY */ 2040 2041 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]) 2042 wpabuf_put_buf(buf, 2043 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]); 2044 2045 /* P2P IE */ 2046 len = p2p_buf_add_ie_hdr(buf); 2047 p2p_buf_add_capability(buf, p2p->dev_capab & 2048 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); 2049 if (p2p->ext_listen_interval) 2050 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period, 2051 p2p->ext_listen_interval); 2052 p2p_buf_add_device_info(buf, p2p, NULL); 2053 p2p_buf_update_ie_hdr(buf, len); 2054 2055 return buf; 2056} 2057 2058 2059static enum p2p_probe_req_status 2060p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst, 2061 const u8 *bssid, const u8 *ie, size_t ie_len) 2062{ 2063 struct ieee802_11_elems elems; 2064 struct wpabuf *buf; 2065 struct ieee80211_mgmt *resp; 2066 struct p2p_message msg; 2067 struct wpabuf *ies; 2068 2069 if (!p2p->in_listen || !p2p->drv_in_listen) { 2070 /* not in Listen state - ignore Probe Request */ 2071 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request", 2072 p2p->in_listen, p2p->drv_in_listen); 2073 return P2P_PREQ_NOT_LISTEN; 2074 } 2075 2076 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) == 2077 ParseFailed) { 2078 /* Ignore invalid Probe Request frames */ 2079 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it"); 2080 return P2P_PREQ_MALFORMED; 2081 } 2082 2083 if (elems.p2p == NULL) { 2084 /* not a P2P probe - ignore it */ 2085 p2p_dbg(p2p, "Not a P2P probe - ignore it"); 2086 return P2P_PREQ_NOT_P2P; 2087 } 2088 2089 if (dst && !is_broadcast_ether_addr(dst) && 2090 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) { 2091 /* Not sent to the broadcast address or our P2P Device Address 2092 */ 2093 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it", 2094 MAC2STR(dst)); 2095 return P2P_PREQ_NOT_PROCESSED; 2096 } 2097 2098 if (bssid && !is_broadcast_ether_addr(bssid)) { 2099 /* Not sent to the Wildcard BSSID */ 2100 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it", 2101 MAC2STR(bssid)); 2102 return P2P_PREQ_NOT_PROCESSED; 2103 } 2104 2105 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN || 2106 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) != 2107 0) { 2108 /* not using P2P Wildcard SSID - ignore */ 2109 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it"); 2110 return P2P_PREQ_NOT_PROCESSED; 2111 } 2112 2113 if (supp_rates_11b_only(&elems)) { 2114 /* Indicates support for 11b rates only */ 2115 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it"); 2116 return P2P_PREQ_NOT_P2P; 2117 } 2118 2119 os_memset(&msg, 0, sizeof(msg)); 2120 if (p2p_parse_ies(ie, ie_len, &msg) < 0) { 2121 /* Could not parse P2P attributes */ 2122 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it"); 2123 return P2P_PREQ_NOT_P2P; 2124 } 2125 2126 if (msg.device_id && 2127 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) { 2128 /* Device ID did not match */ 2129 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it", 2130 MAC2STR(msg.device_id)); 2131 p2p_parse_free(&msg); 2132 return P2P_PREQ_NOT_PROCESSED; 2133 } 2134 2135 /* Check Requested Device Type match */ 2136 if (msg.wps_attributes && 2137 !p2p_match_dev_type(p2p, msg.wps_attributes)) { 2138 /* No match with Requested Device Type */ 2139 p2p_dbg(p2p, "Probe Req requestred Device Type did not match - ignore it"); 2140 p2p_parse_free(&msg); 2141 return P2P_PREQ_NOT_PROCESSED; 2142 } 2143 p2p_parse_free(&msg); 2144 2145 if (!p2p->cfg->send_probe_resp) { 2146 /* Response generated elsewhere */ 2147 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response"); 2148 return P2P_PREQ_NOT_PROCESSED; 2149 } 2150 2151 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state"); 2152 2153 /* 2154 * We do not really have a specific BSS that this frame is advertising, 2155 * so build a frame that has some information in valid format. This is 2156 * really only used for discovery purposes, not to learn exact BSS 2157 * parameters. 2158 */ 2159 ies = p2p_build_probe_resp_ies(p2p); 2160 if (ies == NULL) 2161 return P2P_PREQ_NOT_PROCESSED; 2162 2163 buf = wpabuf_alloc(200 + wpabuf_len(ies)); 2164 if (buf == NULL) { 2165 wpabuf_free(ies); 2166 return P2P_PREQ_NOT_PROCESSED; 2167 } 2168 2169 resp = NULL; 2170 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp); 2171 2172 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) | 2173 (WLAN_FC_STYPE_PROBE_RESP << 4)); 2174 os_memcpy(resp->da, addr, ETH_ALEN); 2175 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN); 2176 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN); 2177 resp->u.probe_resp.beacon_int = host_to_le16(100); 2178 /* hardware or low-level driver will setup seq_ctrl and timestamp */ 2179 resp->u.probe_resp.capab_info = 2180 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE | 2181 WLAN_CAPABILITY_PRIVACY | 2182 WLAN_CAPABILITY_SHORT_SLOT_TIME); 2183 2184 wpabuf_put_u8(buf, WLAN_EID_SSID); 2185 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN); 2186 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); 2187 2188 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES); 2189 wpabuf_put_u8(buf, 8); 2190 wpabuf_put_u8(buf, (60 / 5) | 0x80); 2191 wpabuf_put_u8(buf, 90 / 5); 2192 wpabuf_put_u8(buf, (120 / 5) | 0x80); 2193 wpabuf_put_u8(buf, 180 / 5); 2194 wpabuf_put_u8(buf, (240 / 5) | 0x80); 2195 wpabuf_put_u8(buf, 360 / 5); 2196 wpabuf_put_u8(buf, 480 / 5); 2197 wpabuf_put_u8(buf, 540 / 5); 2198 2199 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS); 2200 wpabuf_put_u8(buf, 1); 2201 wpabuf_put_u8(buf, p2p->cfg->channel); 2202 2203 wpabuf_put_buf(buf, ies); 2204 wpabuf_free(ies); 2205 2206 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf); 2207 2208 wpabuf_free(buf); 2209 2210 return P2P_PREQ_NOT_PROCESSED; 2211} 2212 2213 2214enum p2p_probe_req_status 2215p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst, 2216 const u8 *bssid, const u8 *ie, size_t ie_len) 2217{ 2218 enum p2p_probe_req_status res; 2219 2220 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len); 2221 2222 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len); 2223 2224 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) && 2225 p2p->go_neg_peer && 2226 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN) 2227 == 0 && 2228 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { 2229 /* Received a Probe Request from GO Negotiation peer */ 2230 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout"); 2231 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); 2232 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL); 2233 return P2P_PREQ_PROCESSED; 2234 } 2235 2236 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) && 2237 p2p->invite_peer && 2238 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) && 2239 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN) 2240 == 0) { 2241 /* Received a Probe Request from Invite peer */ 2242 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout"); 2243 eloop_cancel_timeout(p2p_invite_start, p2p, NULL); 2244 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL); 2245 return P2P_PREQ_PROCESSED; 2246 } 2247 2248 return res; 2249} 2250 2251 2252static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid, 2253 u8 *buf, size_t len, struct wpabuf *p2p_ie) 2254{ 2255 struct wpabuf *tmp; 2256 u8 *lpos; 2257 size_t tmplen; 2258 int res; 2259 u8 group_capab; 2260 2261 if (p2p_ie == NULL) 2262 return 0; /* WLAN AP is not a P2P manager */ 2263 2264 /* 2265 * (Re)Association Request - P2P IE 2266 * P2P Capability attribute (shall be present) 2267 * P2P Interface attribute (present if concurrent device and 2268 * P2P Management is enabled) 2269 */ 2270 tmp = wpabuf_alloc(200); 2271 if (tmp == NULL) 2272 return -1; 2273 2274 lpos = p2p_buf_add_ie_hdr(tmp); 2275 group_capab = 0; 2276 if (p2p->num_groups > 0) { 2277 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER; 2278 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && 2279 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) && 2280 p2p->cross_connect) 2281 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN; 2282 } 2283 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab); 2284 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) && 2285 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED)) 2286 p2p_buf_add_p2p_interface(tmp, p2p); 2287 p2p_buf_update_ie_hdr(tmp, lpos); 2288 2289 tmplen = wpabuf_len(tmp); 2290 if (tmplen > len) 2291 res = -1; 2292 else { 2293 os_memcpy(buf, wpabuf_head(tmp), tmplen); 2294 res = tmplen; 2295 } 2296 wpabuf_free(tmp); 2297 2298 return res; 2299} 2300 2301 2302int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf, 2303 size_t len, int p2p_group, struct wpabuf *p2p_ie) 2304{ 2305 struct wpabuf *tmp; 2306 u8 *lpos; 2307 struct p2p_device *peer; 2308 size_t tmplen; 2309 int res; 2310 size_t extra = 0; 2311 2312 if (!p2p_group) 2313 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie); 2314 2315#ifdef CONFIG_WIFI_DISPLAY 2316 if (p2p->wfd_ie_assoc_req) 2317 extra = wpabuf_len(p2p->wfd_ie_assoc_req); 2318#endif /* CONFIG_WIFI_DISPLAY */ 2319 2320 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]) 2321 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]); 2322 2323 /* 2324 * (Re)Association Request - P2P IE 2325 * P2P Capability attribute (shall be present) 2326 * Extended Listen Timing (may be present) 2327 * P2P Device Info attribute (shall be present) 2328 */ 2329 tmp = wpabuf_alloc(200 + extra); 2330 if (tmp == NULL) 2331 return -1; 2332 2333#ifdef CONFIG_WIFI_DISPLAY 2334 if (p2p->wfd_ie_assoc_req) 2335 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req); 2336#endif /* CONFIG_WIFI_DISPLAY */ 2337 2338 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]) 2339 wpabuf_put_buf(tmp, 2340 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]); 2341 2342 peer = bssid ? p2p_get_device(p2p, bssid) : NULL; 2343 2344 lpos = p2p_buf_add_ie_hdr(tmp); 2345 p2p_buf_add_capability(tmp, p2p->dev_capab, 0); 2346 if (p2p->ext_listen_interval) 2347 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period, 2348 p2p->ext_listen_interval); 2349 p2p_buf_add_device_info(tmp, p2p, peer); 2350 p2p_buf_update_ie_hdr(tmp, lpos); 2351 2352 tmplen = wpabuf_len(tmp); 2353 if (tmplen > len) 2354 res = -1; 2355 else { 2356 os_memcpy(buf, wpabuf_head(tmp), tmplen); 2357 res = tmplen; 2358 } 2359 wpabuf_free(tmp); 2360 2361 return res; 2362} 2363 2364 2365int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end) 2366{ 2367 struct wpabuf *p2p_ie; 2368 int ret; 2369 2370 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE); 2371 if (p2p_ie == NULL) 2372 return 0; 2373 2374 ret = p2p_attr_text(p2p_ie, buf, end); 2375 wpabuf_free(p2p_ie); 2376 return ret; 2377} 2378 2379 2380int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr) 2381{ 2382 struct p2p_message msg; 2383 2384 os_memset(&msg, 0, sizeof(msg)); 2385 if (p2p_parse_p2p_ie(p2p_ie, &msg)) 2386 return -1; 2387 2388 if (msg.p2p_device_addr) { 2389 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN); 2390 return 0; 2391 } else if (msg.device_id) { 2392 os_memcpy(dev_addr, msg.device_id, ETH_ALEN); 2393 return 0; 2394 } 2395 return -1; 2396} 2397 2398 2399int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr) 2400{ 2401 struct wpabuf *p2p_ie; 2402 int ret; 2403 2404 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, 2405 P2P_IE_VENDOR_TYPE); 2406 if (p2p_ie == NULL) 2407 return -1; 2408 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr); 2409 wpabuf_free(p2p_ie); 2410 return ret; 2411} 2412 2413 2414static void p2p_clear_go_neg(struct p2p_data *p2p) 2415{ 2416 p2p->go_neg_peer = NULL; 2417 p2p_clear_timeout(p2p); 2418 p2p_set_state(p2p, P2P_IDLE); 2419} 2420 2421 2422void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr) 2423{ 2424 if (p2p->go_neg_peer == NULL) { 2425 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification"); 2426 return; /* No pending Group Formation */ 2427 } 2428 2429 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) != 2430 0) { 2431 p2p_dbg(p2p, "Ignore WPS registration success notification for " 2432 MACSTR " (GO Negotiation peer " MACSTR ")", 2433 MAC2STR(mac_addr), 2434 MAC2STR(p2p->go_neg_peer->intended_addr)); 2435 return; /* Ignore unexpected peer address */ 2436 } 2437 2438 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR, 2439 MAC2STR(mac_addr)); 2440 2441 p2p_clear_go_neg(p2p); 2442} 2443 2444 2445void p2p_group_formation_failed(struct p2p_data *p2p) 2446{ 2447 if (p2p->go_neg_peer == NULL) { 2448 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification"); 2449 return; /* No pending Group Formation */ 2450 } 2451 2452 p2p_dbg(p2p, "Group Formation failed with " MACSTR, 2453 MAC2STR(p2p->go_neg_peer->intended_addr)); 2454 2455 p2p_clear_go_neg(p2p); 2456} 2457 2458 2459struct p2p_data * p2p_init(const struct p2p_config *cfg) 2460{ 2461 struct p2p_data *p2p; 2462 2463 if (cfg->max_peers < 1 || 2464 cfg->passphrase_len < 8 || cfg->passphrase_len > 63) 2465 return NULL; 2466 2467 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg)); 2468 if (p2p == NULL) 2469 return NULL; 2470 p2p->cfg = (struct p2p_config *) (p2p + 1); 2471 os_memcpy(p2p->cfg, cfg, sizeof(*cfg)); 2472 if (cfg->dev_name) 2473 p2p->cfg->dev_name = os_strdup(cfg->dev_name); 2474 if (cfg->manufacturer) 2475 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer); 2476 if (cfg->model_name) 2477 p2p->cfg->model_name = os_strdup(cfg->model_name); 2478 if (cfg->model_number) 2479 p2p->cfg->model_number = os_strdup(cfg->model_number); 2480 if (cfg->serial_number) 2481 p2p->cfg->serial_number = os_strdup(cfg->serial_number); 2482 if (cfg->pref_chan) { 2483 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan * 2484 sizeof(struct p2p_channel)); 2485 if (p2p->cfg->pref_chan) { 2486 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan, 2487 cfg->num_pref_chan * 2488 sizeof(struct p2p_channel)); 2489 } else 2490 p2p->cfg->num_pref_chan = 0; 2491 } 2492 2493 p2p->min_disc_int = 1; 2494 p2p->max_disc_int = 3; 2495 p2p->max_disc_tu = -1; 2496 2497 if (os_get_random(&p2p->next_tie_breaker, 1) < 0) 2498 p2p->next_tie_breaker = 0; 2499 p2p->next_tie_breaker &= 0x01; 2500 if (cfg->sd_request) 2501 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY; 2502 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE; 2503 if (cfg->concurrent_operations) 2504 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER; 2505 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 2506 2507 dl_list_init(&p2p->devices); 2508 2509 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0, 2510 p2p_expiration_timeout, p2p, NULL); 2511 2512 p2p->go_timeout = 100; 2513 p2p->client_timeout = 20; 2514 p2p->num_p2p_sd_queries = 0; 2515 2516 p2p_dbg(p2p, "initialized"); 2517 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); 2518 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); 2519 2520 return p2p; 2521} 2522 2523 2524void p2p_deinit(struct p2p_data *p2p) 2525{ 2526#ifdef CONFIG_WIFI_DISPLAY 2527 wpabuf_free(p2p->wfd_ie_beacon); 2528 wpabuf_free(p2p->wfd_ie_probe_req); 2529 wpabuf_free(p2p->wfd_ie_probe_resp); 2530 wpabuf_free(p2p->wfd_ie_assoc_req); 2531 wpabuf_free(p2p->wfd_ie_invitation); 2532 wpabuf_free(p2p->wfd_ie_prov_disc_req); 2533 wpabuf_free(p2p->wfd_ie_prov_disc_resp); 2534 wpabuf_free(p2p->wfd_ie_go_neg); 2535 wpabuf_free(p2p->wfd_dev_info); 2536 wpabuf_free(p2p->wfd_assoc_bssid); 2537 wpabuf_free(p2p->wfd_coupled_sink_info); 2538#endif /* CONFIG_WIFI_DISPLAY */ 2539 2540 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL); 2541 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); 2542 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); 2543 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL); 2544 p2p_flush(p2p); 2545 p2p_free_req_dev_types(p2p); 2546 os_free(p2p->cfg->dev_name); 2547 os_free(p2p->cfg->manufacturer); 2548 os_free(p2p->cfg->model_name); 2549 os_free(p2p->cfg->model_number); 2550 os_free(p2p->cfg->serial_number); 2551 os_free(p2p->cfg->pref_chan); 2552 os_free(p2p->groups); 2553 wpabuf_free(p2p->sd_resp); 2554 os_free(p2p->after_scan_tx); 2555 p2p_remove_wps_vendor_extensions(p2p); 2556 os_free(p2p->no_go_freq.range); 2557 os_free(p2p); 2558} 2559 2560 2561void p2p_flush(struct p2p_data *p2p) 2562{ 2563 struct p2p_device *dev, *prev; 2564 p2p_stop_find(p2p); 2565 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device, 2566 list) { 2567 dl_list_del(&dev->list); 2568 p2p_device_free(p2p, dev); 2569 } 2570 p2p_free_sd_queries(p2p); 2571 os_free(p2p->after_scan_tx); 2572 p2p->after_scan_tx = NULL; 2573} 2574 2575 2576int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr) 2577{ 2578 struct p2p_device *dev; 2579 2580 dev = p2p_get_device(p2p, addr); 2581 if (dev == NULL) 2582 return -1; 2583 2584 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr)); 2585 2586 if (p2p->go_neg_peer == dev) 2587 p2p->go_neg_peer = NULL; 2588 2589 dev->wps_method = WPS_NOT_READY; 2590 dev->oob_pw_id = 0; 2591 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE; 2592 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM; 2593 2594 /* Check if after_scan_tx is for this peer. If so free it */ 2595 if (p2p->after_scan_tx && 2596 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) { 2597 os_free(p2p->after_scan_tx); 2598 p2p->after_scan_tx = NULL; 2599 } 2600 2601 return 0; 2602} 2603 2604 2605int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name) 2606{ 2607 os_free(p2p->cfg->dev_name); 2608 if (dev_name) { 2609 p2p->cfg->dev_name = os_strdup(dev_name); 2610 if (p2p->cfg->dev_name == NULL) 2611 return -1; 2612 } else 2613 p2p->cfg->dev_name = NULL; 2614 return 0; 2615} 2616 2617 2618int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer) 2619{ 2620 os_free(p2p->cfg->manufacturer); 2621 p2p->cfg->manufacturer = NULL; 2622 if (manufacturer) { 2623 p2p->cfg->manufacturer = os_strdup(manufacturer); 2624 if (p2p->cfg->manufacturer == NULL) 2625 return -1; 2626 } 2627 2628 return 0; 2629} 2630 2631 2632int p2p_set_model_name(struct p2p_data *p2p, const char *model_name) 2633{ 2634 os_free(p2p->cfg->model_name); 2635 p2p->cfg->model_name = NULL; 2636 if (model_name) { 2637 p2p->cfg->model_name = os_strdup(model_name); 2638 if (p2p->cfg->model_name == NULL) 2639 return -1; 2640 } 2641 2642 return 0; 2643} 2644 2645 2646int p2p_set_model_number(struct p2p_data *p2p, const char *model_number) 2647{ 2648 os_free(p2p->cfg->model_number); 2649 p2p->cfg->model_number = NULL; 2650 if (model_number) { 2651 p2p->cfg->model_number = os_strdup(model_number); 2652 if (p2p->cfg->model_number == NULL) 2653 return -1; 2654 } 2655 2656 return 0; 2657} 2658 2659 2660int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number) 2661{ 2662 os_free(p2p->cfg->serial_number); 2663 p2p->cfg->serial_number = NULL; 2664 if (serial_number) { 2665 p2p->cfg->serial_number = os_strdup(serial_number); 2666 if (p2p->cfg->serial_number == NULL) 2667 return -1; 2668 } 2669 2670 return 0; 2671} 2672 2673 2674void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods) 2675{ 2676 p2p->cfg->config_methods = config_methods; 2677} 2678 2679 2680void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid) 2681{ 2682 os_memcpy(p2p->cfg->uuid, uuid, 16); 2683} 2684 2685 2686int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type) 2687{ 2688 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8); 2689 return 0; 2690} 2691 2692 2693int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8], 2694 size_t num_dev_types) 2695{ 2696 if (num_dev_types > P2P_SEC_DEVICE_TYPES) 2697 num_dev_types = P2P_SEC_DEVICE_TYPES; 2698 p2p->cfg->num_sec_dev_types = num_dev_types; 2699 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8); 2700 return 0; 2701} 2702 2703 2704void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p) 2705{ 2706 int i; 2707 2708 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { 2709 wpabuf_free(p2p->wps_vendor_ext[i]); 2710 p2p->wps_vendor_ext[i] = NULL; 2711 } 2712} 2713 2714 2715int p2p_add_wps_vendor_extension(struct p2p_data *p2p, 2716 const struct wpabuf *vendor_ext) 2717{ 2718 int i; 2719 2720 if (vendor_ext == NULL) 2721 return -1; 2722 2723 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) { 2724 if (p2p->wps_vendor_ext[i] == NULL) 2725 break; 2726 } 2727 if (i >= P2P_MAX_WPS_VENDOR_EXT) 2728 return -1; 2729 2730 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext); 2731 if (p2p->wps_vendor_ext[i] == NULL) 2732 return -1; 2733 2734 return 0; 2735} 2736 2737 2738int p2p_set_country(struct p2p_data *p2p, const char *country) 2739{ 2740 os_memcpy(p2p->cfg->country, country, 3); 2741 return 0; 2742} 2743 2744 2745void p2p_continue_find(struct p2p_data *p2p) 2746{ 2747 struct p2p_device *dev; 2748 p2p_set_state(p2p, P2P_SEARCH); 2749 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 2750 if (dev->sd_pending_bcast_queries == 0) { 2751 /* Initialize with total number of registered broadcast 2752 * SD queries. */ 2753 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries; 2754 } 2755 2756 if (p2p_start_sd(p2p, dev) == 0) 2757 return; 2758 if (dev->req_config_methods && 2759 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) { 2760 p2p_dbg(p2p, "Send pending Provision Discovery Request to " 2761 MACSTR " (config methods 0x%x)", 2762 MAC2STR(dev->info.p2p_device_addr), 2763 dev->req_config_methods); 2764 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0) 2765 return; 2766 } 2767 } 2768 2769 p2p_listen_in_find(p2p, 1); 2770} 2771 2772 2773static void p2p_sd_cb(struct p2p_data *p2p, int success) 2774{ 2775 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d", 2776 success); 2777 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 2778 2779 if (!success) { 2780 p2p->sd_peer = NULL; 2781 p2p_continue_find(p2p); 2782 return; 2783 } 2784 2785 if (p2p->sd_peer == NULL) { 2786 p2p_dbg(p2p, "No SD peer entry known"); 2787 p2p_continue_find(p2p); 2788 return; 2789 } 2790 2791 if (p2p->sd_query->for_all_peers) { 2792 /* Update the pending broadcast SD query count for this device 2793 */ 2794 p2p->sd_peer->sd_pending_bcast_queries--; 2795 2796 /* 2797 * If there are no pending broadcast queries for this device, 2798 * mark it as done (-1). 2799 */ 2800 if (p2p->sd_peer->sd_pending_bcast_queries == 0) 2801 p2p->sd_peer->sd_pending_bcast_queries = -1; 2802 } 2803 2804 /* Wait for response from the peer */ 2805 p2p_set_state(p2p, P2P_SD_DURING_FIND); 2806 p2p_set_timeout(p2p, 0, 200000); 2807} 2808 2809 2810/** 2811 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state 2812 * @p2p: P2P module context from p2p_init() 2813 */ 2814static void p2p_retry_pd(struct p2p_data *p2p) 2815{ 2816 struct p2p_device *dev; 2817 2818 if (p2p->state != P2P_IDLE) 2819 return; 2820 2821 /* 2822 * Retry the prov disc req attempt only for the peer that the user had 2823 * requested. 2824 */ 2825 2826 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 2827 if (os_memcmp(p2p->pending_pd_devaddr, 2828 dev->info.p2p_device_addr, ETH_ALEN) != 0) 2829 continue; 2830 if (!dev->req_config_methods) 2831 continue; 2832 2833 p2p_dbg(p2p, "Send pending Provision Discovery Request to " 2834 MACSTR " (config methods 0x%x)", 2835 MAC2STR(dev->info.p2p_device_addr), 2836 dev->req_config_methods); 2837 p2p_send_prov_disc_req(p2p, dev, 2838 dev->flags & P2P_DEV_PD_FOR_JOIN, 2839 p2p->pd_force_freq); 2840 return; 2841 } 2842} 2843 2844 2845static void p2p_prov_disc_cb(struct p2p_data *p2p, int success) 2846{ 2847 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d", 2848 success); 2849 2850 /* 2851 * Postpone resetting the pending action state till after we actually 2852 * time out. This allows us to take some action like notifying any 2853 * interested parties about no response to the request. 2854 * 2855 * When the timer (below) goes off we check in IDLE, SEARCH, or 2856 * LISTEN_ONLY state, which are the only allowed states to issue a PD 2857 * requests in, if this was still pending and then raise notification. 2858 */ 2859 2860 if (!success) { 2861 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 2862 2863 if (p2p->user_initiated_pd && 2864 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY)) 2865 { 2866 /* Retry request from timeout to avoid busy loops */ 2867 p2p->pending_action_state = P2P_PENDING_PD; 2868 p2p_set_timeout(p2p, 0, 50000); 2869 } else if (p2p->state != P2P_IDLE) 2870 p2p_continue_find(p2p); 2871 else if (p2p->user_initiated_pd) { 2872 p2p->pending_action_state = P2P_PENDING_PD; 2873 p2p_set_timeout(p2p, 0, 300000); 2874 } 2875 return; 2876 } 2877 2878 /* 2879 * This postponing, of resetting pending_action_state, needs to be 2880 * done only for user initiated PD requests and not internal ones. 2881 */ 2882 if (p2p->user_initiated_pd) 2883 p2p->pending_action_state = P2P_PENDING_PD; 2884 else 2885 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 2886 2887 /* Wait for response from the peer */ 2888 if (p2p->state == P2P_SEARCH) 2889 p2p_set_state(p2p, P2P_PD_DURING_FIND); 2890 p2p_set_timeout(p2p, 0, 200000); 2891} 2892 2893 2894int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq, 2895 struct os_reltime *rx_time, int level, const u8 *ies, 2896 size_t ies_len) 2897{ 2898 if (os_reltime_before(rx_time, &p2p->find_start)) { 2899 /* 2900 * The driver may have cached (e.g., in cfg80211 BSS table) the 2901 * scan results for relatively long time. To avoid reporting 2902 * stale information, update P2P peers only based on results 2903 * that have based on frames received after the last p2p_find 2904 * operation was started. 2905 */ 2906 p2p_dbg(p2p, "Ignore old scan result for " MACSTR 2907 " (rx_time=%u.%06u)", 2908 MAC2STR(bssid), (unsigned int) rx_time->sec, 2909 (unsigned int) rx_time->usec); 2910 return 0; 2911 } 2912 2913 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1); 2914 2915 return 0; 2916} 2917 2918 2919void p2p_scan_res_handled(struct p2p_data *p2p) 2920{ 2921 if (!p2p->p2p_scan_running) { 2922 p2p_dbg(p2p, "p2p_scan was not running, but scan results received"); 2923 } 2924 p2p->p2p_scan_running = 0; 2925 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL); 2926 2927 if (p2p_run_after_scan(p2p)) 2928 return; 2929 if (p2p->state == P2P_SEARCH) 2930 p2p_continue_find(p2p); 2931} 2932 2933 2934void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id) 2935{ 2936 u8 *len; 2937 2938#ifdef CONFIG_WIFI_DISPLAY 2939 if (p2p->wfd_ie_probe_req) 2940 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req); 2941#endif /* CONFIG_WIFI_DISPLAY */ 2942 2943 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]) 2944 wpabuf_put_buf(ies, 2945 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]); 2946 2947 len = p2p_buf_add_ie_hdr(ies); 2948 p2p_buf_add_capability(ies, p2p->dev_capab & 2949 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); 2950 if (dev_id) 2951 p2p_buf_add_device_id(ies, dev_id); 2952 if (p2p->cfg->reg_class && p2p->cfg->channel) 2953 p2p_buf_add_listen_channel(ies, p2p->cfg->country, 2954 p2p->cfg->reg_class, 2955 p2p->cfg->channel); 2956 if (p2p->ext_listen_interval) 2957 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period, 2958 p2p->ext_listen_interval); 2959 /* TODO: p2p_buf_add_operating_channel() if GO */ 2960 p2p_buf_update_ie_hdr(ies, len); 2961} 2962 2963 2964size_t p2p_scan_ie_buf_len(struct p2p_data *p2p) 2965{ 2966 size_t len = 100; 2967 2968#ifdef CONFIG_WIFI_DISPLAY 2969 if (p2p && p2p->wfd_ie_probe_req) 2970 len += wpabuf_len(p2p->wfd_ie_probe_req); 2971#endif /* CONFIG_WIFI_DISPLAY */ 2972 2973 if (p2p && p2p->vendor_elem && 2974 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]) 2975 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]); 2976 2977 return len; 2978} 2979 2980 2981int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end) 2982{ 2983 return p2p_attr_text(p2p_ie, buf, end); 2984} 2985 2986 2987static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success) 2988{ 2989 struct p2p_device *dev = p2p->go_neg_peer; 2990 int timeout; 2991 2992 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success); 2993 2994 if (dev == NULL) { 2995 p2p_dbg(p2p, "No pending GO Negotiation"); 2996 return; 2997 } 2998 2999 if (success) { 3000 if (dev->flags & P2P_DEV_USER_REJECTED) { 3001 p2p_set_state(p2p, P2P_IDLE); 3002 return; 3003 } 3004 } else if (dev->go_neg_req_sent) { 3005 /* Cancel the increment from p2p_connect_send() on failure */ 3006 dev->go_neg_req_sent--; 3007 } 3008 3009 if (!success && 3010 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) && 3011 !is_zero_ether_addr(dev->member_in_go_dev)) { 3012 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO", 3013 MAC2STR(dev->info.p2p_device_addr)); 3014 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3015 p2p_send_dev_disc_req(p2p, dev); 3016 return; 3017 } 3018 3019 /* 3020 * Use P2P find, if needed, to find the other device from its listen 3021 * channel. 3022 */ 3023 p2p_set_state(p2p, P2P_CONNECT); 3024 timeout = success ? 500000 : 100000; 3025 if (!success && p2p->go_neg_peer && 3026 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) { 3027 unsigned int r; 3028 /* 3029 * Peer is expected to wait our response and we will skip the 3030 * listen phase. Add some randomness to the wait time here to 3031 * make it less likely to hit cases where we could end up in 3032 * sync with peer not listening. 3033 */ 3034 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 3035 r = 0; 3036 timeout += r % 100000; 3037 } 3038 p2p_set_timeout(p2p, 0, timeout); 3039} 3040 3041 3042static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success) 3043{ 3044 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d", 3045 success); 3046 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) { 3047 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore"); 3048 return; 3049 } 3050 p2p_set_state(p2p, P2P_CONNECT); 3051 p2p_set_timeout(p2p, 0, 500000); 3052} 3053 3054 3055static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success, 3056 const u8 *addr) 3057{ 3058 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success); 3059 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) { 3060 p2p_go_neg_failed(p2p, p2p->go_neg_peer, 3061 p2p->go_neg_peer->status); 3062 } else if (success) { 3063 struct p2p_device *dev; 3064 dev = p2p_get_device(p2p, addr); 3065 if (dev && 3066 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { 3067 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE; 3068 if ((p2p->state == P2P_SEARCH) || 3069 (p2p->state == P2P_LISTEN_ONLY)) { 3070 /* Clear our search state or Listen state since 3071 * now peer is awaiting response from our side. 3072 */ 3073 p2p_dbg(p2p, "Clear the P2P discovery state"); 3074 p2p_stop_find(p2p); 3075 } 3076 } 3077 } 3078} 3079 3080 3081static void p2p_go_neg_conf_cb(struct p2p_data *p2p, 3082 enum p2p_send_action_result result) 3083{ 3084 struct p2p_device *dev; 3085 3086 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result); 3087 if (result == P2P_SEND_ACTION_FAILED) { 3088 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3089 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); 3090 return; 3091 } 3092 3093 dev = p2p->go_neg_peer; 3094 3095 if (result == P2P_SEND_ACTION_NO_ACK) { 3096 /* 3097 * Retry GO Negotiation Confirmation 3098 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive 3099 * ACK for confirmation. 3100 */ 3101 if (dev && dev->go_neg_conf && 3102 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) { 3103 p2p_dbg(p2p, "GO Negotiation Confirm retry %d", 3104 dev->go_neg_conf_sent); 3105 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM; 3106 if (p2p_send_action(p2p, dev->go_neg_conf_freq, 3107 dev->info.p2p_device_addr, 3108 p2p->cfg->dev_addr, 3109 dev->info.p2p_device_addr, 3110 wpabuf_head(dev->go_neg_conf), 3111 wpabuf_len(dev->go_neg_conf), 0) >= 3112 0) { 3113 dev->go_neg_conf_sent++; 3114 return; 3115 } 3116 p2p_dbg(p2p, "Failed to re-send Action frame"); 3117 3118 /* 3119 * Continue with the assumption that the first attempt 3120 * went through and just the ACK frame was lost. 3121 */ 3122 } 3123 3124 /* 3125 * It looks like the TX status for GO Negotiation Confirm is 3126 * often showing failure even when the peer has actually 3127 * received the frame. Since the peer may change channels 3128 * immediately after having received the frame, we may not see 3129 * an Ack for retries, so just dropping a single frame may 3130 * trigger this. To allow the group formation to succeed if the 3131 * peer did indeed receive the frame, continue regardless of 3132 * the TX status. 3133 */ 3134 p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported"); 3135 } 3136 3137 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3138 3139 if (dev == NULL) 3140 return; 3141 3142 p2p_go_complete(p2p, dev); 3143} 3144 3145 3146void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst, 3147 const u8 *src, const u8 *bssid, 3148 enum p2p_send_action_result result) 3149{ 3150 enum p2p_pending_action_state state; 3151 int success; 3152 3153 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR 3154 " src=" MACSTR " bssid=" MACSTR " result=%d", 3155 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src), 3156 MAC2STR(bssid), result); 3157 success = result == P2P_SEND_ACTION_SUCCESS; 3158 state = p2p->pending_action_state; 3159 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 3160 switch (state) { 3161 case P2P_NO_PENDING_ACTION: 3162 if (p2p->send_action_in_progress) { 3163 p2p->send_action_in_progress = 0; 3164 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3165 } 3166 if (p2p->after_scan_tx_in_progress) { 3167 p2p->after_scan_tx_in_progress = 0; 3168 if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING && 3169 p2p_run_after_scan(p2p)) 3170 break; 3171 if (p2p->state == P2P_SEARCH) { 3172 p2p_dbg(p2p, "Continue find after after_scan_tx completion"); 3173 p2p_continue_find(p2p); 3174 } 3175 } 3176 break; 3177 case P2P_PENDING_GO_NEG_REQUEST: 3178 p2p_go_neg_req_cb(p2p, success); 3179 break; 3180 case P2P_PENDING_GO_NEG_RESPONSE: 3181 p2p_go_neg_resp_cb(p2p, success); 3182 break; 3183 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE: 3184 p2p_go_neg_resp_failure_cb(p2p, success, dst); 3185 break; 3186 case P2P_PENDING_GO_NEG_CONFIRM: 3187 p2p_go_neg_conf_cb(p2p, result); 3188 break; 3189 case P2P_PENDING_SD: 3190 p2p_sd_cb(p2p, success); 3191 break; 3192 case P2P_PENDING_PD: 3193 p2p_prov_disc_cb(p2p, success); 3194 break; 3195 case P2P_PENDING_INVITATION_REQUEST: 3196 p2p_invitation_req_cb(p2p, success); 3197 break; 3198 case P2P_PENDING_INVITATION_RESPONSE: 3199 p2p_invitation_resp_cb(p2p, success); 3200 break; 3201 case P2P_PENDING_DEV_DISC_REQUEST: 3202 p2p_dev_disc_req_cb(p2p, success); 3203 break; 3204 case P2P_PENDING_DEV_DISC_RESPONSE: 3205 p2p_dev_disc_resp_cb(p2p, success); 3206 break; 3207 case P2P_PENDING_GO_DISC_REQ: 3208 p2p_go_disc_req_cb(p2p, success); 3209 break; 3210 } 3211 3212 p2p->after_scan_tx_in_progress = 0; 3213} 3214 3215 3216void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq, 3217 unsigned int duration) 3218{ 3219 if (freq == p2p->pending_client_disc_freq) { 3220 p2p_dbg(p2p, "Client discoverability remain-awake completed"); 3221 p2p->pending_client_disc_freq = 0; 3222 return; 3223 } 3224 3225 if (freq != p2p->pending_listen_freq) { 3226 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)", 3227 freq, duration, p2p->pending_listen_freq); 3228 return; 3229 } 3230 3231 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback", 3232 p2p->pending_listen_sec, p2p->pending_listen_usec, 3233 p2p->pending_listen_freq); 3234 p2p->in_listen = 1; 3235 p2p->drv_in_listen = freq; 3236 if (p2p->pending_listen_sec || p2p->pending_listen_usec) { 3237 /* 3238 * Add 20 msec extra wait to avoid race condition with driver 3239 * remain-on-channel end event, i.e., give driver more time to 3240 * complete the operation before our timeout expires. 3241 */ 3242 p2p_set_timeout(p2p, p2p->pending_listen_sec, 3243 p2p->pending_listen_usec + 20000); 3244 } 3245 3246 p2p->pending_listen_freq = 0; 3247} 3248 3249 3250int p2p_listen_end(struct p2p_data *p2p, unsigned int freq) 3251{ 3252 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq); 3253 p2p->drv_in_listen = 0; 3254 if (p2p->in_listen) 3255 return 0; /* Internal timeout will trigger the next step */ 3256 3257 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) { 3258 if (p2p->go_neg_peer->connect_reqs >= 120) { 3259 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); 3260 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); 3261 return 0; 3262 } 3263 3264 p2p_set_state(p2p, P2P_CONNECT); 3265 p2p_connect_send(p2p, p2p->go_neg_peer); 3266 return 1; 3267 } else if (p2p->state == P2P_SEARCH) { 3268 if (p2p->p2p_scan_running) { 3269 /* 3270 * Search is already in progress. This can happen if 3271 * an Action frame RX is reported immediately after 3272 * the end of a remain-on-channel operation and the 3273 * response frame to that is sent using an offchannel 3274 * operation while in p2p_find. Avoid an attempt to 3275 * restart a scan here. 3276 */ 3277 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one"); 3278 return 1; 3279 } 3280 if (p2p->pending_listen_freq) { 3281 /* 3282 * Better wait a bit if the driver is unable to start 3283 * offchannel operation for some reason. p2p_search() 3284 * will be started from internal timeout. 3285 */ 3286 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop"); 3287 p2p_set_timeout(p2p, 0, 100000); 3288 return 1; 3289 } 3290 if (p2p->search_delay) { 3291 p2p_dbg(p2p, "Delay search operation by %u ms", 3292 p2p->search_delay); 3293 p2p_set_timeout(p2p, p2p->search_delay / 1000, 3294 (p2p->search_delay % 1000) * 1000); 3295 return 1; 3296 } 3297 p2p_search(p2p); 3298 return 1; 3299 } 3300 3301 return 0; 3302} 3303 3304 3305static void p2p_timeout_connect(struct p2p_data *p2p) 3306{ 3307 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3308 if (p2p->go_neg_peer && 3309 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) { 3310 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed"); 3311 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); 3312 return; 3313 } 3314 if (p2p->go_neg_peer && 3315 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) && 3316 p2p->go_neg_peer->connect_reqs < 120) { 3317 p2p_dbg(p2p, "Peer expected to wait our response - skip listen"); 3318 p2p_connect_send(p2p, p2p->go_neg_peer); 3319 return; 3320 } 3321 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) { 3322 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)"); 3323 p2p_set_state(p2p, P2P_CONNECT_LISTEN); 3324 p2p_set_timeout(p2p, 0, 30000); 3325 return; 3326 } 3327 p2p_set_state(p2p, P2P_CONNECT_LISTEN); 3328 p2p_listen_in_find(p2p, 0); 3329} 3330 3331 3332static void p2p_timeout_connect_listen(struct p2p_data *p2p) 3333{ 3334 if (p2p->go_neg_peer) { 3335 if (p2p->drv_in_listen) { 3336 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete"); 3337 return; 3338 } 3339 3340 if (p2p->go_neg_peer->connect_reqs >= 120) { 3341 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response"); 3342 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1); 3343 return; 3344 } 3345 3346 p2p_set_state(p2p, P2P_CONNECT); 3347 p2p_connect_send(p2p, p2p->go_neg_peer); 3348 } else 3349 p2p_set_state(p2p, P2P_IDLE); 3350} 3351 3352 3353static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p) 3354{ 3355 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE); 3356 3357 if (p2p->cfg->is_concurrent_session_active && 3358 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx)) 3359 p2p_set_timeout(p2p, 0, 500000); 3360 else 3361 p2p_set_timeout(p2p, 0, 200000); 3362} 3363 3364 3365static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p) 3366{ 3367 struct p2p_device *dev = p2p->go_neg_peer; 3368 struct os_reltime now; 3369 3370 if (dev == NULL) { 3371 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait"); 3372 return; 3373 } 3374 3375 os_get_reltime(&now); 3376 if (os_reltime_expired(&now, &dev->go_neg_wait_started, 120)) { 3377 p2p_dbg(p2p, "Timeout on waiting peer to become ready for GO Negotiation"); 3378 p2p_go_neg_failed(p2p, dev, -1); 3379 return; 3380 } 3381 3382 p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation"); 3383 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT); 3384 p2p_listen_in_find(p2p, 0); 3385} 3386 3387 3388static void p2p_timeout_sd_during_find(struct p2p_data *p2p) 3389{ 3390 p2p_dbg(p2p, "Service Discovery Query timeout"); 3391 if (p2p->sd_peer) { 3392 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3393 p2p->sd_peer = NULL; 3394 } 3395 p2p_continue_find(p2p); 3396} 3397 3398 3399static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p) 3400{ 3401 p2p_dbg(p2p, "Provision Discovery Request timeout"); 3402 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3403 p2p_continue_find(p2p); 3404} 3405 3406 3407static void p2p_timeout_prov_disc_req(struct p2p_data *p2p) 3408{ 3409 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 3410 3411 /* 3412 * For user initiated PD requests that we have not gotten any responses 3413 * for while in IDLE state, we retry them a couple of times before 3414 * giving up. 3415 */ 3416 if (!p2p->user_initiated_pd) 3417 return; 3418 3419 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout"); 3420 3421 if (p2p->pd_retries) { 3422 p2p->pd_retries--; 3423 p2p_retry_pd(p2p); 3424 } else { 3425 struct p2p_device *dev; 3426 int for_join = 0; 3427 3428 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) { 3429 if (os_memcmp(p2p->pending_pd_devaddr, 3430 dev->info.p2p_device_addr, ETH_ALEN) != 0) 3431 continue; 3432 if (dev->req_config_methods && 3433 (dev->flags & P2P_DEV_PD_FOR_JOIN)) 3434 for_join = 1; 3435 } 3436 3437 if (p2p->cfg->prov_disc_fail) 3438 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx, 3439 p2p->pending_pd_devaddr, 3440 for_join ? 3441 P2P_PROV_DISC_TIMEOUT_JOIN : 3442 P2P_PROV_DISC_TIMEOUT); 3443 p2p_reset_pending_pd(p2p); 3444 } 3445} 3446 3447 3448static void p2p_timeout_invite(struct p2p_data *p2p) 3449{ 3450 p2p->cfg->send_action_done(p2p->cfg->cb_ctx); 3451 p2p_set_state(p2p, P2P_INVITE_LISTEN); 3452 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) { 3453 /* 3454 * Better remain on operating channel instead of listen channel 3455 * when running a group. 3456 */ 3457 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel"); 3458 p2p_set_timeout(p2p, 0, 100000); 3459 return; 3460 } 3461 p2p_listen_in_find(p2p, 0); 3462} 3463 3464 3465static void p2p_timeout_invite_listen(struct p2p_data *p2p) 3466{ 3467 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) { 3468 p2p_set_state(p2p, P2P_INVITE); 3469 p2p_invite_send(p2p, p2p->invite_peer, 3470 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id); 3471 } else { 3472 if (p2p->invite_peer) { 3473 p2p_dbg(p2p, "Invitation Request retry limit reached"); 3474 if (p2p->cfg->invitation_result) 3475 p2p->cfg->invitation_result( 3476 p2p->cfg->cb_ctx, -1, NULL, NULL, 3477 p2p->invite_peer->info.p2p_device_addr, 3478 0, 0); 3479 } 3480 p2p_set_state(p2p, P2P_IDLE); 3481 } 3482} 3483 3484 3485static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx) 3486{ 3487 struct p2p_data *p2p = eloop_ctx; 3488 3489 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state)); 3490 3491 p2p->in_listen = 0; 3492 3493 switch (p2p->state) { 3494 case P2P_IDLE: 3495 /* Check if we timed out waiting for PD req */ 3496 if (p2p->pending_action_state == P2P_PENDING_PD) 3497 p2p_timeout_prov_disc_req(p2p); 3498 break; 3499 case P2P_SEARCH: 3500 /* Check if we timed out waiting for PD req */ 3501 if (p2p->pending_action_state == P2P_PENDING_PD) 3502 p2p_timeout_prov_disc_req(p2p); 3503 if (p2p->search_delay && !p2p->in_search_delay) { 3504 p2p_dbg(p2p, "Delay search operation by %u ms", 3505 p2p->search_delay); 3506 p2p->in_search_delay = 1; 3507 p2p_set_timeout(p2p, p2p->search_delay / 1000, 3508 (p2p->search_delay % 1000) * 1000); 3509 break; 3510 } 3511 p2p->in_search_delay = 0; 3512 p2p_search(p2p); 3513 break; 3514 case P2P_CONNECT: 3515 p2p_timeout_connect(p2p); 3516 break; 3517 case P2P_CONNECT_LISTEN: 3518 p2p_timeout_connect_listen(p2p); 3519 break; 3520 case P2P_GO_NEG: 3521 break; 3522 case P2P_LISTEN_ONLY: 3523 /* Check if we timed out waiting for PD req */ 3524 if (p2p->pending_action_state == P2P_PENDING_PD) 3525 p2p_timeout_prov_disc_req(p2p); 3526 3527 if (p2p->ext_listen_only) { 3528 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed"); 3529 p2p->ext_listen_only = 0; 3530 p2p_set_state(p2p, P2P_IDLE); 3531 } 3532 break; 3533 case P2P_WAIT_PEER_CONNECT: 3534 p2p_timeout_wait_peer_connect(p2p); 3535 break; 3536 case P2P_WAIT_PEER_IDLE: 3537 p2p_timeout_wait_peer_idle(p2p); 3538 break; 3539 case P2P_SD_DURING_FIND: 3540 p2p_timeout_sd_during_find(p2p); 3541 break; 3542 case P2P_PROVISIONING: 3543 break; 3544 case P2P_PD_DURING_FIND: 3545 p2p_timeout_prov_disc_during_find(p2p); 3546 break; 3547 case P2P_INVITE: 3548 p2p_timeout_invite(p2p); 3549 break; 3550 case P2P_INVITE_LISTEN: 3551 p2p_timeout_invite_listen(p2p); 3552 break; 3553 } 3554} 3555 3556 3557int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr) 3558{ 3559 struct p2p_device *dev; 3560 3561 dev = p2p_get_device(p2p, peer_addr); 3562 p2p_dbg(p2p, "Local request to reject connection attempts by peer " 3563 MACSTR, MAC2STR(peer_addr)); 3564 if (dev == NULL) { 3565 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr)); 3566 return -1; 3567 } 3568 dev->status = P2P_SC_FAIL_REJECTED_BY_USER; 3569 dev->flags |= P2P_DEV_USER_REJECTED; 3570 return 0; 3571} 3572 3573 3574const char * p2p_wps_method_text(enum p2p_wps_method method) 3575{ 3576 switch (method) { 3577 case WPS_NOT_READY: 3578 return "not-ready"; 3579 case WPS_PIN_DISPLAY: 3580 return "Display"; 3581 case WPS_PIN_KEYPAD: 3582 return "Keypad"; 3583 case WPS_PBC: 3584 return "PBC"; 3585 case WPS_NFC: 3586 return "NFC"; 3587 } 3588 3589 return "??"; 3590} 3591 3592 3593static const char * p2p_go_state_text(enum p2p_go_state go_state) 3594{ 3595 switch (go_state) { 3596 case UNKNOWN_GO: 3597 return "unknown"; 3598 case LOCAL_GO: 3599 return "local"; 3600 case REMOTE_GO: 3601 return "remote"; 3602 } 3603 3604 return "??"; 3605} 3606 3607 3608const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p, 3609 const u8 *addr, int next) 3610{ 3611 struct p2p_device *dev; 3612 3613 if (addr) 3614 dev = p2p_get_device(p2p, addr); 3615 else 3616 dev = dl_list_first(&p2p->devices, struct p2p_device, list); 3617 3618 if (dev && next) { 3619 dev = dl_list_first(&dev->list, struct p2p_device, list); 3620 if (&dev->list == &p2p->devices) 3621 dev = NULL; 3622 } 3623 3624 if (dev == NULL) 3625 return NULL; 3626 3627 return &dev->info; 3628} 3629 3630 3631int p2p_get_peer_info_txt(const struct p2p_peer_info *info, 3632 char *buf, size_t buflen) 3633{ 3634 struct p2p_device *dev; 3635 int res; 3636 char *pos, *end; 3637 struct os_reltime now; 3638 3639 if (info == NULL) 3640 return -1; 3641 3642 dev = (struct p2p_device *) (((u8 *) info) - 3643 offsetof(struct p2p_device, info)); 3644 3645 pos = buf; 3646 end = buf + buflen; 3647 3648 os_get_reltime(&now); 3649 res = os_snprintf(pos, end - pos, 3650 "age=%d\n" 3651 "listen_freq=%d\n" 3652 "wps_method=%s\n" 3653 "interface_addr=" MACSTR "\n" 3654 "member_in_go_dev=" MACSTR "\n" 3655 "member_in_go_iface=" MACSTR "\n" 3656 "go_neg_req_sent=%d\n" 3657 "go_state=%s\n" 3658 "dialog_token=%u\n" 3659 "intended_addr=" MACSTR "\n" 3660 "country=%c%c\n" 3661 "oper_freq=%d\n" 3662 "req_config_methods=0x%x\n" 3663 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s\n" 3664 "status=%d\n" 3665 "invitation_reqs=%u\n", 3666 (int) (now.sec - dev->last_seen.sec), 3667 dev->listen_freq, 3668 p2p_wps_method_text(dev->wps_method), 3669 MAC2STR(dev->interface_addr), 3670 MAC2STR(dev->member_in_go_dev), 3671 MAC2STR(dev->member_in_go_iface), 3672 dev->go_neg_req_sent, 3673 p2p_go_state_text(dev->go_state), 3674 dev->dialog_token, 3675 MAC2STR(dev->intended_addr), 3676 dev->country[0] ? dev->country[0] : '_', 3677 dev->country[1] ? dev->country[1] : '_', 3678 dev->oper_freq, 3679 dev->req_config_methods, 3680 dev->flags & P2P_DEV_PROBE_REQ_ONLY ? 3681 "[PROBE_REQ_ONLY]" : "", 3682 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "", 3683 dev->flags & P2P_DEV_NOT_YET_READY ? 3684 "[NOT_YET_READY]" : "", 3685 dev->flags & P2P_DEV_PD_PEER_DISPLAY ? 3686 "[PD_PEER_DISPLAY]" : "", 3687 dev->flags & P2P_DEV_PD_PEER_KEYPAD ? 3688 "[PD_PEER_KEYPAD]" : "", 3689 dev->flags & P2P_DEV_USER_REJECTED ? 3690 "[USER_REJECTED]" : "", 3691 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ? 3692 "[PEER_WAITING_RESPONSE]" : "", 3693 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ? 3694 "[PREFER_PERSISTENT_GROUP]" : "", 3695 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ? 3696 "[WAIT_GO_NEG_RESPONSE]" : "", 3697 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ? 3698 "[WAIT_GO_NEG_CONFIRM]" : "", 3699 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ? 3700 "[GROUP_CLIENT_ONLY]" : "", 3701 dev->flags & P2P_DEV_FORCE_FREQ ? 3702 "[FORCE_FREQ]" : "", 3703 dev->flags & P2P_DEV_PD_FOR_JOIN ? 3704 "[PD_FOR_JOIN]" : "", 3705 dev->status, 3706 dev->invitation_reqs); 3707 if (res < 0 || res >= end - pos) 3708 return pos - buf; 3709 pos += res; 3710 3711 if (dev->ext_listen_period) { 3712 res = os_snprintf(pos, end - pos, 3713 "ext_listen_period=%u\n" 3714 "ext_listen_interval=%u\n", 3715 dev->ext_listen_period, 3716 dev->ext_listen_interval); 3717 if (res < 0 || res >= end - pos) 3718 return pos - buf; 3719 pos += res; 3720 } 3721 3722 if (dev->oper_ssid_len) { 3723 res = os_snprintf(pos, end - pos, 3724 "oper_ssid=%s\n", 3725 wpa_ssid_txt(dev->oper_ssid, 3726 dev->oper_ssid_len)); 3727 if (res < 0 || res >= end - pos) 3728 return pos - buf; 3729 pos += res; 3730 } 3731 3732#ifdef CONFIG_WIFI_DISPLAY 3733 if (dev->info.wfd_subelems) { 3734 res = os_snprintf(pos, end - pos, "wfd_subelems="); 3735 if (res < 0 || res >= end - pos) 3736 return pos - buf; 3737 pos += res; 3738 3739 pos += wpa_snprintf_hex(pos, end - pos, 3740 wpabuf_head(dev->info.wfd_subelems), 3741 wpabuf_len(dev->info.wfd_subelems)); 3742 3743 res = os_snprintf(pos, end - pos, "\n"); 3744 if (res < 0 || res >= end - pos) 3745 return pos - buf; 3746 pos += res; 3747 } 3748#endif /* CONFIG_WIFI_DISPLAY */ 3749 3750 return pos - buf; 3751} 3752 3753 3754int p2p_peer_known(struct p2p_data *p2p, const u8 *addr) 3755{ 3756 return p2p_get_device(p2p, addr) != NULL; 3757} 3758 3759 3760void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled) 3761{ 3762 if (enabled) { 3763 p2p_dbg(p2p, "Client discoverability enabled"); 3764 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 3765 } else { 3766 p2p_dbg(p2p, "Client discoverability disabled"); 3767 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY; 3768 } 3769} 3770 3771 3772static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1, 3773 u32 duration2, u32 interval2) 3774{ 3775 struct wpabuf *req; 3776 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL; 3777 u8 *len; 3778 3779 req = wpabuf_alloc(100); 3780 if (req == NULL) 3781 return NULL; 3782 3783 if (duration1 || interval1) { 3784 os_memset(&desc1, 0, sizeof(desc1)); 3785 desc1.count_type = 1; 3786 desc1.duration = duration1; 3787 desc1.interval = interval1; 3788 ptr1 = &desc1; 3789 3790 if (duration2 || interval2) { 3791 os_memset(&desc2, 0, sizeof(desc2)); 3792 desc2.count_type = 2; 3793 desc2.duration = duration2; 3794 desc2.interval = interval2; 3795 ptr2 = &desc2; 3796 } 3797 } 3798 3799 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1); 3800 len = p2p_buf_add_ie_hdr(req); 3801 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2); 3802 p2p_buf_update_ie_hdr(req, len); 3803 3804 return req; 3805} 3806 3807 3808int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr, 3809 const u8 *own_interface_addr, unsigned int freq, 3810 u32 duration1, u32 interval1, u32 duration2, 3811 u32 interval2) 3812{ 3813 struct wpabuf *req; 3814 3815 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR 3816 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u " 3817 "dur2=%u int2=%u", 3818 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr), 3819 freq, duration1, interval1, duration2, interval2); 3820 3821 req = p2p_build_presence_req(duration1, interval1, duration2, 3822 interval2); 3823 if (req == NULL) 3824 return -1; 3825 3826 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 3827 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr, 3828 go_interface_addr, 3829 wpabuf_head(req), wpabuf_len(req), 200) < 0) { 3830 p2p_dbg(p2p, "Failed to send Action frame"); 3831 } 3832 wpabuf_free(req); 3833 3834 return 0; 3835} 3836 3837 3838static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa, 3839 size_t noa_len, u8 dialog_token) 3840{ 3841 struct wpabuf *resp; 3842 u8 *len; 3843 3844 resp = wpabuf_alloc(100 + noa_len); 3845 if (resp == NULL) 3846 return NULL; 3847 3848 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token); 3849 len = p2p_buf_add_ie_hdr(resp); 3850 p2p_buf_add_status(resp, status); 3851 if (noa) { 3852 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE); 3853 wpabuf_put_le16(resp, noa_len); 3854 wpabuf_put_data(resp, noa, noa_len); 3855 } else 3856 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL); 3857 p2p_buf_update_ie_hdr(resp, len); 3858 3859 return resp; 3860} 3861 3862 3863static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da, 3864 const u8 *sa, const u8 *data, size_t len, 3865 int rx_freq) 3866{ 3867 struct p2p_message msg; 3868 u8 status; 3869 struct wpabuf *resp; 3870 size_t g; 3871 struct p2p_group *group = NULL; 3872 int parsed = 0; 3873 u8 noa[50]; 3874 int noa_len; 3875 3876 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request"); 3877 3878 for (g = 0; g < p2p->num_groups; g++) { 3879 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]), 3880 ETH_ALEN) == 0) { 3881 group = p2p->groups[g]; 3882 break; 3883 } 3884 } 3885 if (group == NULL) { 3886 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group " 3887 MACSTR, MAC2STR(da)); 3888 return; 3889 } 3890 3891 if (p2p_parse(data, len, &msg) < 0) { 3892 p2p_dbg(p2p, "Failed to parse P2P Presence Request"); 3893 status = P2P_SC_FAIL_INVALID_PARAMS; 3894 goto fail; 3895 } 3896 parsed = 1; 3897 3898 if (msg.noa == NULL) { 3899 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request"); 3900 status = P2P_SC_FAIL_INVALID_PARAMS; 3901 goto fail; 3902 } 3903 3904 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len); 3905 3906fail: 3907 if (p2p->cfg->get_noa) 3908 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa, 3909 sizeof(noa)); 3910 else 3911 noa_len = -1; 3912 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL, 3913 noa_len > 0 ? noa_len : 0, 3914 msg.dialog_token); 3915 if (parsed) 3916 p2p_parse_free(&msg); 3917 if (resp == NULL) 3918 return; 3919 3920 p2p->pending_action_state = P2P_NO_PENDING_ACTION; 3921 if (p2p_send_action(p2p, rx_freq, sa, da, da, 3922 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) { 3923 p2p_dbg(p2p, "Failed to send Action frame"); 3924 } 3925 wpabuf_free(resp); 3926} 3927 3928 3929static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da, 3930 const u8 *sa, const u8 *data, size_t len) 3931{ 3932 struct p2p_message msg; 3933 3934 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response"); 3935 3936 if (p2p_parse(data, len, &msg) < 0) { 3937 p2p_dbg(p2p, "Failed to parse P2P Presence Response"); 3938 return; 3939 } 3940 3941 if (msg.status == NULL || msg.noa == NULL) { 3942 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response"); 3943 p2p_parse_free(&msg); 3944 return; 3945 } 3946 3947 if (p2p->cfg->presence_resp) { 3948 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status, 3949 msg.noa, msg.noa_len); 3950 } 3951 3952 if (*msg.status) { 3953 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u", 3954 *msg.status); 3955 p2p_parse_free(&msg); 3956 return; 3957 } 3958 3959 p2p_dbg(p2p, "P2P Presence Request was accepted"); 3960 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA", 3961 msg.noa, msg.noa_len); 3962 /* TODO: process NoA */ 3963 p2p_parse_free(&msg); 3964} 3965 3966 3967static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx) 3968{ 3969 struct p2p_data *p2p = eloop_ctx; 3970 3971 if (p2p->ext_listen_interval) { 3972 /* Schedule next extended listen timeout */ 3973 eloop_register_timeout(p2p->ext_listen_interval_sec, 3974 p2p->ext_listen_interval_usec, 3975 p2p_ext_listen_timeout, p2p, NULL); 3976 } 3977 3978 if ((p2p->cfg->is_p2p_in_progress && 3979 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) || 3980 (p2p->pending_action_state == P2P_PENDING_PD && 3981 p2p->pd_retries > 0)) { 3982 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)", 3983 p2p_state_txt(p2p->state)); 3984 return; 3985 } 3986 3987 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) { 3988 /* 3989 * This should not really happen, but it looks like the Listen 3990 * command may fail is something else (e.g., a scan) was 3991 * running at an inconvenient time. As a workaround, allow new 3992 * Extended Listen operation to be started. 3993 */ 3994 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again"); 3995 p2p->ext_listen_only = 0; 3996 p2p_set_state(p2p, P2P_IDLE); 3997 } 3998 3999 if (p2p->state != P2P_IDLE) { 4000 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state)); 4001 return; 4002 } 4003 4004 p2p_dbg(p2p, "Extended Listen timeout"); 4005 p2p->ext_listen_only = 1; 4006 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) { 4007 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing"); 4008 p2p->ext_listen_only = 0; 4009 } 4010} 4011 4012 4013int p2p_ext_listen(struct p2p_data *p2p, unsigned int period, 4014 unsigned int interval) 4015{ 4016 if (period > 65535 || interval > 65535 || period > interval || 4017 (period == 0 && interval > 0) || (period > 0 && interval == 0)) { 4018 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u", 4019 period, interval); 4020 return -1; 4021 } 4022 4023 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL); 4024 4025 if (interval == 0) { 4026 p2p_dbg(p2p, "Disabling Extended Listen Timing"); 4027 p2p->ext_listen_period = 0; 4028 p2p->ext_listen_interval = 0; 4029 return 0; 4030 } 4031 4032 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec", 4033 period, interval); 4034 p2p->ext_listen_period = period; 4035 p2p->ext_listen_interval = interval; 4036 p2p->ext_listen_interval_sec = interval / 1000; 4037 p2p->ext_listen_interval_usec = (interval % 1000) * 1000; 4038 4039 eloop_register_timeout(p2p->ext_listen_interval_sec, 4040 p2p->ext_listen_interval_usec, 4041 p2p_ext_listen_timeout, p2p, NULL); 4042 4043 return 0; 4044} 4045 4046 4047void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, 4048 const u8 *ie, size_t ie_len) 4049{ 4050 struct p2p_message msg; 4051 4052 if (bssid == NULL || ie == NULL) 4053 return; 4054 4055 os_memset(&msg, 0, sizeof(msg)); 4056 if (p2p_parse_ies(ie, ie_len, &msg)) 4057 return; 4058 if (msg.minor_reason_code == NULL) { 4059 p2p_parse_free(&msg); 4060 return; 4061 } 4062 4063 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR 4064 " reason_code=%u minor_reason_code=%u", 4065 MAC2STR(bssid), reason_code, *msg.minor_reason_code); 4066 4067 p2p_parse_free(&msg); 4068} 4069 4070 4071void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code, 4072 const u8 *ie, size_t ie_len) 4073{ 4074 struct p2p_message msg; 4075 4076 if (bssid == NULL || ie == NULL) 4077 return; 4078 4079 os_memset(&msg, 0, sizeof(msg)); 4080 if (p2p_parse_ies(ie, ie_len, &msg)) 4081 return; 4082 if (msg.minor_reason_code == NULL) { 4083 p2p_parse_free(&msg); 4084 return; 4085 } 4086 4087 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR 4088 " reason_code=%u minor_reason_code=%u", 4089 MAC2STR(bssid), reason_code, *msg.minor_reason_code); 4090 4091 p2p_parse_free(&msg); 4092} 4093 4094 4095void p2p_set_managed_oper(struct p2p_data *p2p, int enabled) 4096{ 4097 if (enabled) { 4098 p2p_dbg(p2p, "Managed P2P Device operations enabled"); 4099 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED; 4100 } else { 4101 p2p_dbg(p2p, "Managed P2P Device operations disabled"); 4102 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED; 4103 } 4104} 4105 4106 4107int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class, 4108 u8 *op_channel) 4109{ 4110 return p2p_channel_random_social(&p2p->channels, op_class, op_channel); 4111} 4112 4113 4114int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel, 4115 u8 forced) 4116{ 4117 if (p2p_channel_to_freq(reg_class, channel) < 0) 4118 return -1; 4119 4120 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u", 4121 reg_class, channel); 4122 4123 /* 4124 * Listen channel was set in configuration or set by control interface; 4125 * cannot override it. 4126 */ 4127 if (p2p->cfg->channel_forced && forced == 0) 4128 return -1; 4129 4130 if (p2p->state == P2P_IDLE) { 4131 p2p->cfg->reg_class = reg_class; 4132 p2p->cfg->channel = channel; 4133 p2p->cfg->channel_forced = forced; 4134 } else { 4135 p2p_dbg(p2p, "Defer setting listen channel"); 4136 p2p->pending_reg_class = reg_class; 4137 p2p->pending_channel = channel; 4138 p2p->pending_channel_forced = forced; 4139 } 4140 4141 return 0; 4142} 4143 4144 4145u8 p2p_get_listen_channel(struct p2p_data *p2p) 4146{ 4147 return p2p->cfg->channel; 4148} 4149 4150 4151int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len) 4152{ 4153 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len)); 4154 if (postfix == NULL) { 4155 p2p->cfg->ssid_postfix_len = 0; 4156 return 0; 4157 } 4158 if (len > sizeof(p2p->cfg->ssid_postfix)) 4159 return -1; 4160 os_memcpy(p2p->cfg->ssid_postfix, postfix, len); 4161 p2p->cfg->ssid_postfix_len = len; 4162 return 0; 4163} 4164 4165 4166int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel, 4167 int cfg_op_channel) 4168{ 4169 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0) 4170 return -1; 4171 4172 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u", 4173 op_reg_class, op_channel); 4174 p2p->cfg->op_reg_class = op_reg_class; 4175 p2p->cfg->op_channel = op_channel; 4176 p2p->cfg->cfg_op_channel = cfg_op_channel; 4177 return 0; 4178} 4179 4180 4181int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan, 4182 const struct p2p_channel *pref_chan) 4183{ 4184 struct p2p_channel *n; 4185 4186 if (pref_chan) { 4187 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel)); 4188 if (n == NULL) 4189 return -1; 4190 os_memcpy(n, pref_chan, 4191 num_pref_chan * sizeof(struct p2p_channel)); 4192 } else 4193 n = NULL; 4194 4195 os_free(p2p->cfg->pref_chan); 4196 p2p->cfg->pref_chan = n; 4197 p2p->cfg->num_pref_chan = num_pref_chan; 4198 4199 return 0; 4200} 4201 4202 4203int p2p_set_no_go_freq(struct p2p_data *p2p, 4204 const struct wpa_freq_range_list *list) 4205{ 4206 struct wpa_freq_range *tmp; 4207 4208 if (list == NULL || list->num == 0) { 4209 os_free(p2p->no_go_freq.range); 4210 p2p->no_go_freq.range = NULL; 4211 p2p->no_go_freq.num = 0; 4212 return 0; 4213 } 4214 4215 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range)); 4216 if (tmp == NULL) 4217 return -1; 4218 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range)); 4219 os_free(p2p->no_go_freq.range); 4220 p2p->no_go_freq.range = tmp; 4221 p2p->no_go_freq.num = list->num; 4222 p2p_dbg(p2p, "Updated no GO chan list"); 4223 4224 return 0; 4225} 4226 4227 4228int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr, 4229 u8 *iface_addr) 4230{ 4231 struct p2p_device *dev = p2p_get_device(p2p, dev_addr); 4232 if (dev == NULL || is_zero_ether_addr(dev->interface_addr)) 4233 return -1; 4234 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN); 4235 return 0; 4236} 4237 4238 4239int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr, 4240 u8 *dev_addr) 4241{ 4242 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); 4243 if (dev == NULL) 4244 return -1; 4245 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN); 4246 return 0; 4247} 4248 4249 4250void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr) 4251{ 4252 os_memcpy(p2p->peer_filter, addr, ETH_ALEN); 4253 if (is_zero_ether_addr(p2p->peer_filter)) 4254 p2p_dbg(p2p, "Disable peer filter"); 4255 else 4256 p2p_dbg(p2p, "Enable peer filter for " MACSTR, 4257 MAC2STR(p2p->peer_filter)); 4258} 4259 4260 4261void p2p_set_cross_connect(struct p2p_data *p2p, int enabled) 4262{ 4263 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled"); 4264 if (p2p->cross_connect == enabled) 4265 return; 4266 p2p->cross_connect = enabled; 4267 /* TODO: may need to tear down any action group where we are GO(?) */ 4268} 4269 4270 4271int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr) 4272{ 4273 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr); 4274 if (dev == NULL) 4275 return -1; 4276 if (dev->oper_freq <= 0) 4277 return -1; 4278 return dev->oper_freq; 4279} 4280 4281 4282void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled) 4283{ 4284 p2p_dbg(p2p, "Intra BSS distribution %s", 4285 enabled ? "enabled" : "disabled"); 4286 p2p->cfg->p2p_intra_bss = enabled; 4287} 4288 4289 4290void p2p_update_channel_list(struct p2p_data *p2p, 4291 const struct p2p_channels *chan, 4292 const struct p2p_channels *cli_chan) 4293{ 4294 p2p_dbg(p2p, "Update channel list"); 4295 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels)); 4296 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels); 4297 os_memcpy(&p2p->cfg->cli_channels, cli_chan, 4298 sizeof(struct p2p_channels)); 4299 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels); 4300} 4301 4302 4303int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst, 4304 const u8 *src, const u8 *bssid, const u8 *buf, 4305 size_t len, unsigned int wait_time) 4306{ 4307 if (p2p->p2p_scan_running) { 4308 p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes"); 4309 if (p2p->after_scan_tx) { 4310 p2p_dbg(p2p, "Dropped previous pending Action frame TX"); 4311 os_free(p2p->after_scan_tx); 4312 } 4313 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) + 4314 len); 4315 if (p2p->after_scan_tx == NULL) 4316 return -1; 4317 p2p->after_scan_tx->freq = freq; 4318 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN); 4319 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN); 4320 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN); 4321 p2p->after_scan_tx->len = len; 4322 p2p->after_scan_tx->wait_time = wait_time; 4323 os_memcpy(p2p->after_scan_tx + 1, buf, len); 4324 return 0; 4325 } 4326 4327 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid, 4328 buf, len, wait_time); 4329} 4330 4331 4332void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5, 4333 int freq_overall) 4334{ 4335 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d", 4336 freq_24, freq_5, freq_overall); 4337 p2p->best_freq_24 = freq_24; 4338 p2p->best_freq_5 = freq_5; 4339 p2p->best_freq_overall = freq_overall; 4340} 4341 4342 4343void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq) 4344{ 4345 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq); 4346 p2p->own_freq_preference = freq; 4347} 4348 4349 4350const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p) 4351{ 4352 if (p2p == NULL || p2p->go_neg_peer == NULL) 4353 return NULL; 4354 return p2p->go_neg_peer->info.p2p_device_addr; 4355} 4356 4357 4358const struct p2p_peer_info * 4359p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next) 4360{ 4361 struct p2p_device *dev; 4362 4363 if (addr) { 4364 dev = p2p_get_device(p2p, addr); 4365 if (!dev) 4366 return NULL; 4367 4368 if (!next) { 4369 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) 4370 return NULL; 4371 4372 return &dev->info; 4373 } else { 4374 do { 4375 dev = dl_list_first(&dev->list, 4376 struct p2p_device, 4377 list); 4378 if (!dev || &dev->list == &p2p->devices) 4379 return NULL; 4380 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY); 4381 } 4382 } else { 4383 dev = dl_list_first(&p2p->devices, struct p2p_device, list); 4384 if (!dev) 4385 return NULL; 4386 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) { 4387 dev = dl_list_first(&dev->list, 4388 struct p2p_device, 4389 list); 4390 if (!dev || &dev->list == &p2p->devices) 4391 return NULL; 4392 } 4393 } 4394 4395 return &dev->info; 4396} 4397 4398 4399int p2p_in_progress(struct p2p_data *p2p) 4400{ 4401 if (p2p == NULL) 4402 return 0; 4403 if (p2p->state == P2P_SEARCH) 4404 return 2; 4405 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING; 4406} 4407 4408 4409void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout, 4410 u8 client_timeout) 4411{ 4412 if (p2p) { 4413 p2p->go_timeout = go_timeout; 4414 p2p->client_timeout = client_timeout; 4415 } 4416} 4417 4418 4419#ifdef CONFIG_WIFI_DISPLAY 4420 4421static void p2p_update_wfd_ie_groups(struct p2p_data *p2p) 4422{ 4423 size_t g; 4424 struct p2p_group *group; 4425 4426 for (g = 0; g < p2p->num_groups; g++) { 4427 group = p2p->groups[g]; 4428 p2p_group_force_beacon_update_ies(group); 4429 } 4430} 4431 4432 4433int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie) 4434{ 4435 wpabuf_free(p2p->wfd_ie_beacon); 4436 p2p->wfd_ie_beacon = ie; 4437 p2p_update_wfd_ie_groups(p2p); 4438 return 0; 4439} 4440 4441 4442int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie) 4443{ 4444 wpabuf_free(p2p->wfd_ie_probe_req); 4445 p2p->wfd_ie_probe_req = ie; 4446 return 0; 4447} 4448 4449 4450int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie) 4451{ 4452 wpabuf_free(p2p->wfd_ie_probe_resp); 4453 p2p->wfd_ie_probe_resp = ie; 4454 p2p_update_wfd_ie_groups(p2p); 4455 return 0; 4456} 4457 4458 4459int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie) 4460{ 4461 wpabuf_free(p2p->wfd_ie_assoc_req); 4462 p2p->wfd_ie_assoc_req = ie; 4463 return 0; 4464} 4465 4466 4467int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie) 4468{ 4469 wpabuf_free(p2p->wfd_ie_invitation); 4470 p2p->wfd_ie_invitation = ie; 4471 return 0; 4472} 4473 4474 4475int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie) 4476{ 4477 wpabuf_free(p2p->wfd_ie_prov_disc_req); 4478 p2p->wfd_ie_prov_disc_req = ie; 4479 return 0; 4480} 4481 4482 4483int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie) 4484{ 4485 wpabuf_free(p2p->wfd_ie_prov_disc_resp); 4486 p2p->wfd_ie_prov_disc_resp = ie; 4487 return 0; 4488} 4489 4490 4491int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie) 4492{ 4493 wpabuf_free(p2p->wfd_ie_go_neg); 4494 p2p->wfd_ie_go_neg = ie; 4495 return 0; 4496} 4497 4498 4499int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem) 4500{ 4501 wpabuf_free(p2p->wfd_dev_info); 4502 if (elem) { 4503 p2p->wfd_dev_info = wpabuf_dup(elem); 4504 if (p2p->wfd_dev_info == NULL) 4505 return -1; 4506 } else 4507 p2p->wfd_dev_info = NULL; 4508 4509 return 0; 4510} 4511 4512 4513int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem) 4514{ 4515 wpabuf_free(p2p->wfd_assoc_bssid); 4516 if (elem) { 4517 p2p->wfd_assoc_bssid = wpabuf_dup(elem); 4518 if (p2p->wfd_assoc_bssid == NULL) 4519 return -1; 4520 } else 4521 p2p->wfd_assoc_bssid = NULL; 4522 4523 return 0; 4524} 4525 4526 4527int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p, 4528 const struct wpabuf *elem) 4529{ 4530 wpabuf_free(p2p->wfd_coupled_sink_info); 4531 if (elem) { 4532 p2p->wfd_coupled_sink_info = wpabuf_dup(elem); 4533 if (p2p->wfd_coupled_sink_info == NULL) 4534 return -1; 4535 } else 4536 p2p->wfd_coupled_sink_info = NULL; 4537 4538 return 0; 4539} 4540 4541#endif /* CONFIG_WIFI_DISPLAY */ 4542 4543 4544int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int, 4545 int max_disc_tu) 4546{ 4547 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0) 4548 return -1; 4549 4550 p2p->min_disc_int = min_disc_int; 4551 p2p->max_disc_int = max_disc_int; 4552 p2p->max_disc_tu = max_disc_tu; 4553 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d", 4554 min_disc_int, max_disc_int, max_disc_tu); 4555 4556 return 0; 4557} 4558 4559 4560void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...) 4561{ 4562 va_list ap; 4563 char buf[500]; 4564 4565 if (!p2p->cfg->debug_print) 4566 return; 4567 4568 va_start(ap, fmt); 4569 vsnprintf(buf, sizeof(buf), fmt, ap); 4570 buf[sizeof(buf) - 1] = '\0'; 4571 va_end(ap); 4572 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf); 4573} 4574 4575 4576void p2p_info(struct p2p_data *p2p, const char *fmt, ...) 4577{ 4578 va_list ap; 4579 char buf[500]; 4580 4581 if (!p2p->cfg->debug_print) 4582 return; 4583 4584 va_start(ap, fmt); 4585 vsnprintf(buf, sizeof(buf), fmt, ap); 4586 buf[sizeof(buf) - 1] = '\0'; 4587 va_end(ap); 4588 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf); 4589} 4590 4591 4592void p2p_err(struct p2p_data *p2p, const char *fmt, ...) 4593{ 4594 va_list ap; 4595 char buf[500]; 4596 4597 if (!p2p->cfg->debug_print) 4598 return; 4599 4600 va_start(ap, fmt); 4601 vsnprintf(buf, sizeof(buf), fmt, ap); 4602 buf[sizeof(buf) - 1] = '\0'; 4603 va_end(ap); 4604 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf); 4605} 4606 4607 4608void p2p_loop_on_known_peers(struct p2p_data *p2p, 4609 void (*peer_callback)(struct p2p_peer_info *peer, 4610 void *user_data), 4611 void *user_data) 4612{ 4613 struct p2p_device *dev, *n; 4614 4615 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) { 4616 peer_callback(&dev->info, user_data); 4617 } 4618} 4619 4620 4621#ifdef CONFIG_WPS_NFC 4622 4623static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p, 4624 int client_freq, 4625 const u8 *go_dev_addr, 4626 const u8 *ssid, size_t ssid_len) 4627{ 4628 struct wpabuf *buf; 4629 u8 op_class, channel; 4630 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP; 4631 4632 buf = wpabuf_alloc(1000); 4633 if (buf == NULL) 4634 return NULL; 4635 4636 op_class = p2p->cfg->reg_class; 4637 channel = p2p->cfg->channel; 4638 4639 p2p_buf_add_capability(buf, p2p->dev_capab & 4640 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0); 4641 p2p_buf_add_device_info(buf, p2p, NULL); 4642 4643 if (p2p->num_groups > 0) { 4644 int freq = p2p_group_get_freq(p2p->groups[0]); 4645 role = P2P_GO_IN_A_GROUP; 4646 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) { 4647 p2p_dbg(p2p, 4648 "Unknown GO operating frequency %d MHz for NFC handover", 4649 freq); 4650 wpabuf_free(buf); 4651 return NULL; 4652 } 4653 } else if (client_freq > 0) { 4654 role = P2P_CLIENT_IN_A_GROUP; 4655 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) { 4656 p2p_dbg(p2p, 4657 "Unknown client operating frequency %d MHz for NFC handover", 4658 client_freq); 4659 wpabuf_free(buf); 4660 return NULL; 4661 } 4662 } 4663 4664 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class, 4665 channel, role); 4666 4667 if (p2p->num_groups > 0) { 4668 /* Limit number of clients to avoid very long message */ 4669 p2p_buf_add_group_info(p2p->groups[0], buf, 5); 4670 p2p_group_buf_add_id(p2p->groups[0], buf); 4671 } else if (client_freq > 0 && 4672 go_dev_addr && !is_zero_ether_addr(go_dev_addr) && 4673 ssid && ssid_len > 0) { 4674 /* 4675 * Add the optional P2P Group ID to indicate in which group this 4676 * device is a P2P Client. 4677 */ 4678 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len); 4679 } 4680 4681 return buf; 4682} 4683 4684 4685struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p, 4686 int client_freq, 4687 const u8 *go_dev_addr, 4688 const u8 *ssid, size_t ssid_len) 4689{ 4690 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, 4691 ssid_len); 4692} 4693 4694 4695struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p, 4696 int client_freq, 4697 const u8 *go_dev_addr, 4698 const u8 *ssid, size_t ssid_len) 4699{ 4700 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid, 4701 ssid_len); 4702} 4703 4704 4705int p2p_process_nfc_connection_handover(struct p2p_data *p2p, 4706 struct p2p_nfc_params *params) 4707{ 4708 struct p2p_message msg; 4709 struct p2p_device *dev; 4710 const u8 *p2p_dev_addr; 4711 int freq; 4712 enum p2p_role_indication role; 4713 4714 params->next_step = NO_ACTION; 4715 4716 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len, 4717 params->p2p_attr, params->p2p_len, &msg)) { 4718 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC"); 4719 p2p_parse_free(&msg); 4720 return -1; 4721 } 4722 4723 if (msg.p2p_device_addr) 4724 p2p_dev_addr = msg.p2p_device_addr; 4725 else if (msg.device_id) 4726 p2p_dev_addr = msg.device_id; 4727 else { 4728 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id"); 4729 p2p_parse_free(&msg); 4730 return -1; 4731 } 4732 4733 if (msg.oob_dev_password) { 4734 os_memcpy(params->oob_dev_pw, msg.oob_dev_password, 4735 msg.oob_dev_password_len); 4736 params->oob_dev_pw_len = msg.oob_dev_password_len; 4737 } 4738 4739 dev = p2p_create_device(p2p, p2p_dev_addr); 4740 if (dev == NULL) { 4741 p2p_parse_free(&msg); 4742 return -1; 4743 } 4744 4745 params->peer = &dev->info; 4746 4747 os_get_reltime(&dev->last_seen); 4748 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY); 4749 p2p_copy_wps_info(p2p, dev, 0, &msg); 4750 4751 if (!msg.oob_go_neg_channel) { 4752 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included"); 4753 return -1; 4754 } 4755 4756 if (msg.oob_go_neg_channel[3] == 0 && 4757 msg.oob_go_neg_channel[4] == 0) 4758 freq = 0; 4759 else 4760 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3], 4761 msg.oob_go_neg_channel[4]); 4762 if (freq < 0) { 4763 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel"); 4764 return -1; 4765 } 4766 role = msg.oob_go_neg_channel[5]; 4767 4768 if (role == P2P_GO_IN_A_GROUP) { 4769 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq); 4770 params->go_freq = freq; 4771 } else if (role == P2P_CLIENT_IN_A_GROUP) { 4772 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz", 4773 freq); 4774 params->go_freq = freq; 4775 } else 4776 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq); 4777 dev->oob_go_neg_freq = freq; 4778 4779 if (!params->sel && role != P2P_GO_IN_A_GROUP) { 4780 freq = p2p_channel_to_freq(p2p->cfg->reg_class, 4781 p2p->cfg->channel); 4782 if (freq < 0) { 4783 p2p_dbg(p2p, "Own listen channel not known"); 4784 return -1; 4785 } 4786 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq); 4787 dev->oob_go_neg_freq = freq; 4788 } 4789 4790 if (msg.group_id) { 4791 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN); 4792 params->go_ssid_len = msg.group_id_len - ETH_ALEN; 4793 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN, 4794 params->go_ssid_len); 4795 } 4796 4797 if (dev->flags & P2P_DEV_USER_REJECTED) { 4798 p2p_dbg(p2p, "Do not report rejected device"); 4799 p2p_parse_free(&msg); 4800 return 0; 4801 } 4802 4803 if (!(dev->flags & P2P_DEV_REPORTED)) { 4804 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info, 4805 !(dev->flags & P2P_DEV_REPORTED_ONCE)); 4806 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE; 4807 } 4808 p2p_parse_free(&msg); 4809 4810 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0) 4811 params->next_step = BOTH_GO; 4812 else if (role == P2P_GO_IN_A_GROUP) 4813 params->next_step = JOIN_GROUP; 4814 else if (role == P2P_CLIENT_IN_A_GROUP) { 4815 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY; 4816 params->next_step = PEER_CLIENT; 4817 } else if (p2p->num_groups > 0) 4818 params->next_step = AUTH_JOIN; 4819 else if (params->sel) 4820 params->next_step = INIT_GO_NEG; 4821 else 4822 params->next_step = RESP_GO_NEG; 4823 4824 return 0; 4825} 4826 4827 4828void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id, 4829 int go_intent, 4830 const u8 *own_interface_addr) 4831{ 4832 4833 p2p->authorized_oob_dev_pw_id = dev_pw_id; 4834 if (dev_pw_id == 0) { 4835 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover"); 4836 return; 4837 } 4838 4839 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover", 4840 dev_pw_id); 4841 4842 p2p->go_intent = go_intent; 4843 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN); 4844} 4845 4846#endif /* CONFIG_WPS_NFC */ 4847 4848 4849int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len) 4850{ 4851 if (len < 8 || len > 63) 4852 return -1; 4853 p2p->cfg->passphrase_len = len; 4854 return 0; 4855} 4856 4857 4858void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem) 4859{ 4860 p2p->vendor_elem = vendor_elem; 4861} 4862