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