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