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