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