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