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