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