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