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