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