p2p_supplicant.c revision 8367dc909f536f600c7474582fe0a96bef7c78fc
1/* 2 * wpa_supplicant - P2P 3 * Copyright (c) 2009-2010, Atheros Communications 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "includes.h" 10 11#include "common.h" 12#include "eloop.h" 13#include "common/ieee802_11_common.h" 14#include "common/ieee802_11_defs.h" 15#include "common/wpa_ctrl.h" 16#include "wps/wps_i.h" 17#include "p2p/p2p.h" 18#include "ap/hostapd.h" 19#include "ap/ap_config.h" 20#include "ap/p2p_hostapd.h" 21#include "eapol_supp/eapol_supp_sm.h" 22#include "rsn_supp/wpa.h" 23#include "wpa_supplicant_i.h" 24#include "driver_i.h" 25#include "ap.h" 26#include "config_ssid.h" 27#include "config.h" 28#include "notify.h" 29#include "scan.h" 30#include "bss.h" 31#include "offchannel.h" 32#include "wps_supplicant.h" 33#include "p2p_supplicant.h" 34 35 36/* 37 * How many times to try to scan to find the GO before giving up on join 38 * request. 39 */ 40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10 41 42#define P2P_AUTO_PD_SCAN_ATTEMPTS 5 43 44#ifndef P2P_MAX_CLIENT_IDLE 45/* 46 * How many seconds to try to reconnect to the GO when connection in P2P client 47 * role has been lost. 48 */ 49#ifdef ANDROID_P2P 50#define P2P_MAX_CLIENT_IDLE 20 51#else 52#define P2P_MAX_CLIENT_IDLE 10 53#endif /* ANDROID_P2P */ 54#endif /* P2P_MAX_CLIENT_IDLE */ 55 56#ifndef P2P_MAX_INITIAL_CONN_WAIT 57/* 58 * How many seconds to wait for initial 4-way handshake to get completed after 59 * WPS provisioning step. 60 */ 61#define P2P_MAX_INITIAL_CONN_WAIT 10 62#endif /* P2P_MAX_INITIAL_CONN_WAIT */ 63 64#ifndef P2P_CONCURRENT_SEARCH_DELAY 65#ifdef ANDROID_P2P 66#define P2P_CONCURRENT_SEARCH_DELAY 0 67#else 68#define P2P_CONCURRENT_SEARCH_DELAY 500 69#endif 70#endif /* P2P_CONCURRENT_SEARCH_DELAY */ 71 72enum p2p_group_removal_reason { 73 P2P_GROUP_REMOVAL_UNKNOWN, 74 P2P_GROUP_REMOVAL_SILENT, 75 P2P_GROUP_REMOVAL_FORMATION_FAILED, 76 P2P_GROUP_REMOVAL_REQUESTED, 77 P2P_GROUP_REMOVAL_IDLE_TIMEOUT, 78 P2P_GROUP_REMOVAL_UNAVAILABLE, 79 P2P_GROUP_REMOVAL_GO_ENDING_SESSION, 80#ifdef ANDROID_P2P 81 P2P_GROUP_REMOVAL_FREQ_CONFLICT 82#endif 83}; 84 85#ifdef ANDROID_P2P 86static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s); 87#endif 88static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); 89static struct wpa_supplicant * 90wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, 91 int go); 92static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s); 93static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq); 94static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); 95static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, 96 const u8 *dev_addr, enum p2p_wps_method wps_method, 97 int auto_join); 98static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, 99 void *timeout_ctx); 100static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); 101static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); 102static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); 103static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s); 104static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, 105 int group_added); 106 107#ifdef ANDROID_P2P 108static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s) 109{ 110 struct wpa_supplicant *iface = NULL; 111 112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 113 if(iface->scanning || iface->wpa_state == WPA_SCANNING) { 114 wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s," 115 "defer P2P SEARCH", iface->ifname); 116 return 1; 117 } 118 } 119 120 return 0; 121} 122#endif 123 124static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, 125 struct wpa_scan_results *scan_res) 126{ 127 size_t i; 128 129 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 130 return; 131 132 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", 133 (int) scan_res->num); 134 135 for (i = 0; i < scan_res->num; i++) { 136 struct wpa_scan_res *bss = scan_res->res[i]; 137 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, 138 bss->freq, bss->level, 139 (const u8 *) (bss + 1), 140 bss->ie_len) > 0) 141 break; 142 } 143 144 p2p_scan_res_handled(wpa_s->global->p2p); 145} 146 147 148static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, 149 unsigned int num_req_dev_types, 150 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id) 151{ 152 struct wpa_supplicant *wpa_s = ctx; 153 struct wpa_supplicant *ifs; 154 struct wpa_driver_scan_params params; 155 int ret; 156 struct wpabuf *wps_ie, *ies; 157 int social_channels[] = { 2412, 2437, 2462, 0, 0 }; 158 size_t ielen; 159 160 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 161 return -1; 162 163 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { 164 if (ifs->sta_scan_pending && 165 wpas_p2p_in_progress(wpa_s) == 2) { 166 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow " 167 "pending station mode scan to be " 168 "completed on interface %s", ifs->ifname); 169 wpa_s->p2p_cb_on_scan_complete = 1; 170 wpa_supplicant_req_scan(ifs, 0, 0); 171 return 1; 172 } 173 } 174 175 os_memset(¶ms, 0, sizeof(params)); 176 177 /* P2P Wildcard SSID */ 178 params.num_ssids = 1; 179 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; 180 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; 181 182 wpa_s->wps->dev.p2p = 1; 183 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, 184 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 185 num_req_dev_types, req_dev_types); 186 if (wps_ie == NULL) 187 return -1; 188 189 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); 190 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); 191 if (ies == NULL) { 192 wpabuf_free(wps_ie); 193 return -1; 194 } 195 wpabuf_put_buf(ies, wps_ie); 196 wpabuf_free(wps_ie); 197 198 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id); 199 200 params.p2p_probe = 1; 201 params.extra_ies = wpabuf_head(ies); 202 params.extra_ies_len = wpabuf_len(ies); 203 204 switch (type) { 205 case P2P_SCAN_SOCIAL: 206 params.freqs = social_channels; 207 break; 208 case P2P_SCAN_FULL: 209 break; 210 case P2P_SCAN_SPECIFIC: 211 social_channels[0] = freq; 212 social_channels[1] = 0; 213 params.freqs = social_channels; 214 break; 215 case P2P_SCAN_SOCIAL_PLUS_ONE: 216 social_channels[3] = freq; 217 params.freqs = social_channels; 218 break; 219 } 220 221 ret = wpa_drv_scan(wpa_s, ¶ms); 222 223 wpabuf_free(ies); 224 225 if (ret) { 226 if (wpa_s->scanning || 227 wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) { 228 wpa_s->p2p_cb_on_scan_complete = 1; 229 ret = 1; 230 } 231 } else 232 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; 233 234 return ret; 235} 236 237 238static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) 239{ 240 switch (p2p_group_interface) { 241 case P2P_GROUP_INTERFACE_PENDING: 242 return WPA_IF_P2P_GROUP; 243 case P2P_GROUP_INTERFACE_GO: 244 return WPA_IF_P2P_GO; 245 case P2P_GROUP_INTERFACE_CLIENT: 246 return WPA_IF_P2P_CLIENT; 247 } 248 249 return WPA_IF_P2P_GROUP; 250} 251 252 253static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, 254 const u8 *ssid, 255 size_t ssid_len, int *go) 256{ 257 struct wpa_ssid *s; 258 259 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 260 for (s = wpa_s->conf->ssid; s; s = s->next) { 261 if (s->disabled != 0 || !s->p2p_group || 262 s->ssid_len != ssid_len || 263 os_memcmp(ssid, s->ssid, ssid_len) != 0) 264 continue; 265 if (s->mode == WPAS_MODE_P2P_GO && 266 s != wpa_s->current_ssid) 267 continue; 268 if (go) 269 *go = s->mode == WPAS_MODE_P2P_GO; 270 return wpa_s; 271 } 272 } 273 274 return NULL; 275} 276 277 278static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, 279 enum p2p_group_removal_reason removal_reason) 280{ 281 struct wpa_ssid *ssid; 282 char *gtype; 283 const char *reason; 284 285 ssid = wpa_s->current_ssid; 286#ifdef ANDROID_P2P 287 if ((ssid == NULL) && (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { 288#else 289 if (ssid == NULL) { 290#endif 291 /* 292 * The current SSID was not known, but there may still be a 293 * pending P2P group interface waiting for provisioning. 294 */ 295 ssid = wpa_s->conf->ssid; 296 while (ssid) { 297 if (ssid->p2p_group && 298 (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || 299 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) 300 break; 301 ssid = ssid->next; 302 } 303 if (ssid == NULL && 304 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) 305 { 306 wpa_printf(MSG_ERROR, "P2P: P2P group interface " 307 "not found"); 308 return -1; 309 } 310 } 311 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) 312 gtype = "GO"; 313 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || 314 (ssid && ssid->mode == WPAS_MODE_INFRA)) { 315 wpa_s->reassociate = 0; 316 wpa_s->disconnected = 1; 317 wpa_supplicant_deauthenticate(wpa_s, 318 WLAN_REASON_DEAUTH_LEAVING); 319 gtype = "client"; 320 } else 321 gtype = "GO"; 322 if (wpa_s->cross_connect_in_use) { 323 wpa_s->cross_connect_in_use = 0; 324 wpa_msg(wpa_s->parent, MSG_INFO, 325 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", 326 wpa_s->ifname, wpa_s->cross_connect_uplink); 327 } 328 switch (removal_reason) { 329 case P2P_GROUP_REMOVAL_REQUESTED: 330 reason = " reason=REQUESTED"; 331 break; 332 case P2P_GROUP_REMOVAL_FORMATION_FAILED: 333 reason = " reason=FORMATION_FAILED"; 334 break; 335 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: 336 reason = " reason=IDLE"; 337 break; 338 case P2P_GROUP_REMOVAL_UNAVAILABLE: 339 reason = " reason=UNAVAILABLE"; 340 break; 341 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: 342 reason = " reason=GO_ENDING_SESSION"; 343 break; 344#ifdef ANDROID_P2P 345 case P2P_GROUP_REMOVAL_FREQ_CONFLICT: 346 reason = " reason=FREQ_CONFLICT"; 347 break; 348#endif 349 default: 350 reason = ""; 351 break; 352 } 353 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { 354 wpa_msg(wpa_s->parent, MSG_INFO, 355 P2P_EVENT_GROUP_REMOVED "%s %s%s", 356 wpa_s->ifname, gtype, reason); 357 } 358 359 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) 360 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); 361 362 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) 363 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); 364 365 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { 366 struct wpa_global *global; 367 char *ifname; 368 enum wpa_driver_if_type type; 369 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", 370 wpa_s->ifname); 371 global = wpa_s->global; 372 ifname = os_strdup(wpa_s->ifname); 373 type = wpas_p2p_if_type(wpa_s->p2p_group_interface); 374 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); 375 wpa_s = global->ifaces; 376 if (wpa_s && ifname) 377 wpa_drv_if_remove(wpa_s, type, ifname); 378 os_free(ifname); 379 return 1; 380 } 381 382 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); 383 if (ssid && (ssid->p2p_group || 384 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || 385 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { 386 int id = ssid->id; 387 if (ssid == wpa_s->current_ssid) { 388 wpa_sm_set_config(wpa_s->wpa, NULL); 389 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); 390 wpa_s->current_ssid = NULL; 391 } 392 /* 393 * Networks objects created during any P2P activities are not 394 * exposed out as they might/will confuse certain non-P2P aware 395 * applications since these network objects won't behave like 396 * regular ones. 397 * 398 * Likewise, we don't send out network removed signals for such 399 * network objects. 400 */ 401 wpa_config_remove_network(wpa_s->conf, id); 402 wpa_supplicant_clear_status(wpa_s); 403 wpa_supplicant_cancel_sched_scan(wpa_s); 404 } else { 405 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " 406 "found"); 407 } 408 if (wpa_s->ap_iface) 409 wpa_supplicant_ap_deinit(wpa_s); 410 else 411 wpa_drv_deinit_p2p_cli(wpa_s); 412 413 return 0; 414} 415 416 417static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, 418 u8 *go_dev_addr, 419 const u8 *ssid, size_t ssid_len) 420{ 421 struct wpa_bss *bss; 422 const u8 *bssid; 423 struct wpabuf *p2p; 424 u8 group_capab; 425 const u8 *addr; 426 427 if (wpa_s->go_params) 428 bssid = wpa_s->go_params->peer_interface_addr; 429 else 430 bssid = wpa_s->bssid; 431 432 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); 433 if (bss == NULL) { 434 u8 iface_addr[ETH_ALEN]; 435 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, 436 iface_addr) == 0) 437 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); 438 } 439 if (bss == NULL) { 440 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " 441 "group is persistent - BSS " MACSTR " not found", 442 MAC2STR(bssid)); 443 return 0; 444 } 445 446 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); 447 if (p2p == NULL) { 448 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " 449 "group is persistent - BSS " MACSTR 450 " did not include P2P IE", MAC2STR(bssid)); 451 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", 452 (u8 *) (bss + 1), bss->ie_len); 453 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", 454 ((u8 *) bss + 1) + bss->ie_len, 455 bss->beacon_ie_len); 456 return 0; 457 } 458 459 group_capab = p2p_get_group_capab(p2p); 460 addr = p2p_get_go_dev_addr(p2p); 461 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " 462 "group_capab=0x%x", group_capab); 463 if (addr) { 464 os_memcpy(go_dev_addr, addr, ETH_ALEN); 465 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, 466 MAC2STR(addr)); 467 } else 468 os_memset(go_dev_addr, 0, ETH_ALEN); 469 wpabuf_free(p2p); 470 471 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " 472 "go_dev_addr=" MACSTR, 473 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); 474 475 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP; 476} 477 478 479static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, 480 struct wpa_ssid *ssid, 481 const u8 *go_dev_addr) 482{ 483 struct wpa_ssid *s; 484 int changed = 0; 485 486 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " 487 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); 488 for (s = wpa_s->conf->ssid; s; s = s->next) { 489 if (s->disabled == 2 && 490 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && 491 s->ssid_len == ssid->ssid_len && 492 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) 493 break; 494 } 495 496 if (s) { 497 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " 498 "entry"); 499 if (ssid->passphrase && !s->passphrase) 500 changed = 1; 501 else if (ssid->passphrase && s->passphrase && 502 os_strcmp(ssid->passphrase, s->passphrase) != 0) 503 changed = 1; 504 } else { 505 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " 506 "entry"); 507 changed = 1; 508 s = wpa_config_add_network(wpa_s->conf); 509 if (s == NULL) 510 return -1; 511 512 /* 513 * Instead of network_added we emit persistent_group_added 514 * notification. Also to keep the defense checks in 515 * persistent_group obj registration method, we set the 516 * relevant flags in s to designate it as a persistent group. 517 */ 518 s->p2p_group = 1; 519 s->p2p_persistent_group = 1; 520 wpas_notify_persistent_group_added(wpa_s, s); 521 wpa_config_set_network_defaults(s); 522 } 523 524 s->p2p_group = 1; 525 s->p2p_persistent_group = 1; 526 s->disabled = 2; 527 s->bssid_set = 1; 528 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); 529 s->mode = ssid->mode; 530 s->auth_alg = WPA_AUTH_ALG_OPEN; 531 s->key_mgmt = WPA_KEY_MGMT_PSK; 532 s->proto = WPA_PROTO_RSN; 533 s->pairwise_cipher = WPA_CIPHER_CCMP; 534 s->export_keys = 1; 535 if (ssid->passphrase) { 536 os_free(s->passphrase); 537 s->passphrase = os_strdup(ssid->passphrase); 538 } 539 if (ssid->psk_set) { 540 s->psk_set = 1; 541 os_memcpy(s->psk, ssid->psk, 32); 542 } 543 if (s->passphrase && !s->psk_set) 544 wpa_config_update_psk(s); 545 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { 546 os_free(s->ssid); 547 s->ssid = os_malloc(ssid->ssid_len); 548 } 549 if (s->ssid) { 550 s->ssid_len = ssid->ssid_len; 551 os_memcpy(s->ssid, ssid->ssid, s->ssid_len); 552 } 553 554#ifndef CONFIG_NO_CONFIG_WRITE 555 if (changed && wpa_s->conf->update_config && 556 wpa_config_write(wpa_s->confname, wpa_s->conf)) { 557 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 558 } 559#endif /* CONFIG_NO_CONFIG_WRITE */ 560 561 return s->id; 562} 563 564 565static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, 566 const u8 *addr) 567{ 568 struct wpa_ssid *ssid, *s; 569 u8 *n; 570 size_t i; 571 int found = 0; 572 573 ssid = wpa_s->current_ssid; 574 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || 575 !ssid->p2p_persistent_group) 576 return; 577 578 for (s = wpa_s->parent->conf->ssid; s; s = s->next) { 579 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) 580 continue; 581 582 if (s->ssid_len == ssid->ssid_len && 583 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) 584 break; 585 } 586 587 if (s == NULL) 588 return; 589 590 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { 591 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr, 592 ETH_ALEN) != 0) 593 continue; 594 595 if (i == s->num_p2p_clients - 1) 596 return; /* already the most recent entry */ 597 598 /* move the entry to mark it most recent */ 599 os_memmove(s->p2p_client_list + i * ETH_ALEN, 600 s->p2p_client_list + (i + 1) * ETH_ALEN, 601 (s->num_p2p_clients - i - 1) * ETH_ALEN); 602 os_memcpy(s->p2p_client_list + 603 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN); 604 found = 1; 605 break; 606 } 607 608 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { 609 n = os_realloc_array(s->p2p_client_list, 610 s->num_p2p_clients + 1, ETH_ALEN); 611 if (n == NULL) 612 return; 613 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN); 614 s->p2p_client_list = n; 615 s->num_p2p_clients++; 616 } else if (!found) { 617 /* Not enough room for an additional entry - drop the oldest 618 * entry */ 619 os_memmove(s->p2p_client_list, 620 s->p2p_client_list + ETH_ALEN, 621 (s->num_p2p_clients - 1) * ETH_ALEN); 622 os_memcpy(s->p2p_client_list + 623 (s->num_p2p_clients - 1) * ETH_ALEN, 624 addr, ETH_ALEN); 625 } 626 627#ifndef CONFIG_NO_CONFIG_WRITE 628 if (wpa_s->parent->conf->update_config && 629 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf)) 630 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 631#endif /* CONFIG_NO_CONFIG_WRITE */ 632} 633 634 635static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, 636 int success) 637{ 638 struct wpa_ssid *ssid; 639 const char *ssid_txt; 640 int client; 641 int persistent; 642 u8 go_dev_addr[ETH_ALEN]; 643 int network_id = -1; 644 645 /* 646 * This callback is likely called for the main interface. Update wpa_s 647 * to use the group interface if a new interface was created for the 648 * group. 649 */ 650 if (wpa_s->global->p2p_group_formation) 651 wpa_s = wpa_s->global->p2p_group_formation; 652 wpa_s->global->p2p_group_formation = NULL; 653 wpa_s->p2p_in_provisioning = 0; 654 655 if (!success) { 656 wpa_msg(wpa_s->parent, MSG_INFO, 657 P2P_EVENT_GROUP_FORMATION_FAILURE); 658 wpas_p2p_group_delete(wpa_s, 659 P2P_GROUP_REMOVAL_FORMATION_FAILED); 660 return; 661 } 662 663 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS); 664 665 ssid = wpa_s->current_ssid; 666 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { 667 ssid->mode = WPAS_MODE_P2P_GO; 668 p2p_group_notif_formation_done(wpa_s->p2p_group); 669 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); 670 } 671 672 persistent = 0; 673 if (ssid) { 674 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); 675 client = ssid->mode == WPAS_MODE_INFRA; 676 if (ssid->mode == WPAS_MODE_P2P_GO) { 677 persistent = ssid->p2p_persistent_group; 678 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, 679 ETH_ALEN); 680 } else 681 persistent = wpas_p2p_persistent_group(wpa_s, 682 go_dev_addr, 683 ssid->ssid, 684 ssid->ssid_len); 685 } else { 686 ssid_txt = ""; 687 client = wpa_s->p2p_group_interface == 688 P2P_GROUP_INTERFACE_CLIENT; 689 os_memset(go_dev_addr, 0, ETH_ALEN); 690 } 691 692 wpa_s->show_group_started = 0; 693 if (client) { 694 /* 695 * Indicate event only after successfully completed 4-way 696 * handshake, i.e., when the interface is ready for data 697 * packets. 698 */ 699 wpa_s->show_group_started = 1; 700#ifdef ANDROID_P2P 701 /* For client Second phase of Group formation (4-way handshake) can be still pending 702 * So we need to restore wpa_s->global->p2p_group_formation */ 703 wpa_s->global->p2p_group_formation = wpa_s; 704#endif 705 706 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) { 707 char psk[65]; 708 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); 709 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED 710 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR 711 "%s", 712 wpa_s->ifname, ssid_txt, ssid->frequency, psk, 713 MAC2STR(go_dev_addr), 714 persistent ? " [PERSISTENT]" : ""); 715 wpas_p2p_cross_connect_setup(wpa_s); 716 wpas_p2p_set_group_idle_timeout(wpa_s); 717 } else { 718 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED 719 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " 720 "go_dev_addr=" MACSTR "%s", 721 wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0, 722 ssid && ssid->passphrase ? ssid->passphrase : "", 723 MAC2STR(go_dev_addr), 724 persistent ? " [PERSISTENT]" : ""); 725 wpas_p2p_cross_connect_setup(wpa_s); 726 wpas_p2p_set_group_idle_timeout(wpa_s); 727 } 728 729 if (persistent) 730 network_id = wpas_p2p_store_persistent_group(wpa_s->parent, 731 ssid, go_dev_addr); 732 if (network_id < 0 && ssid) 733 network_id = ssid->id; 734 if (!client) 735 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); 736} 737 738 739static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, 740 unsigned int freq, 741 const u8 *dst, const u8 *src, 742 const u8 *bssid, 743 const u8 *data, size_t data_len, 744 enum offchannel_send_action_result 745 result) 746{ 747 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; 748 749 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) 750 return; 751 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 752 return; 753 754 switch (result) { 755 case OFFCHANNEL_SEND_ACTION_SUCCESS: 756 res = P2P_SEND_ACTION_SUCCESS; 757 break; 758 case OFFCHANNEL_SEND_ACTION_NO_ACK: 759 res = P2P_SEND_ACTION_NO_ACK; 760 break; 761 case OFFCHANNEL_SEND_ACTION_FAILED: 762 res = P2P_SEND_ACTION_FAILED; 763 break; 764 } 765 766 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res); 767 768 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && 769 wpa_s->pending_pd_before_join && 770 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || 771 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { 772 wpa_s->pending_pd_before_join = 0; 773 if (wpa_s->p2p_fallback_to_go_neg) { 774 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " 775 "during p2p_connect-auto"); 776 wpas_p2p_fallback_to_go_neg(wpa_s, 0); 777 return; 778 } 779 780 wpa_printf(MSG_DEBUG, "P2P: Starting pending " 781 "join-existing-group operation (no ACK for PD " 782 "Req)"); 783 wpas_p2p_join_start(wpa_s); 784 } 785} 786 787 788static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, 789 const u8 *src, const u8 *bssid, const u8 *buf, 790 size_t len, unsigned int wait_time) 791{ 792 struct wpa_supplicant *wpa_s = ctx; 793 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, 794 wait_time, 795 wpas_p2p_send_action_tx_status, 1); 796} 797 798 799static void wpas_send_action_done(void *ctx) 800{ 801 struct wpa_supplicant *wpa_s = ctx; 802 offchannel_send_action_done(wpa_s); 803} 804 805 806static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, 807 struct p2p_go_neg_results *params) 808{ 809 if (wpa_s->go_params == NULL) { 810 wpa_s->go_params = os_malloc(sizeof(*params)); 811 if (wpa_s->go_params == NULL) 812 return -1; 813 } 814 os_memcpy(wpa_s->go_params, params, sizeof(*params)); 815 return 0; 816} 817 818 819static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, 820 struct p2p_go_neg_results *res) 821{ 822 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR, 823 MAC2STR(res->peer_interface_addr)); 824 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", 825 res->ssid, res->ssid_len); 826 wpa_supplicant_ap_deinit(wpa_s); 827 wpas_copy_go_neg_results(wpa_s, res); 828 if (res->wps_method == WPS_PBC) 829 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1); 830 else { 831 u16 dev_pw_id = DEV_PW_DEFAULT; 832 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) 833 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; 834 wpas_wps_start_pin(wpa_s, res->peer_interface_addr, 835 wpa_s->p2p_pin, 1, dev_pw_id); 836 } 837} 838 839 840static void p2p_go_configured(void *ctx, void *data) 841{ 842 struct wpa_supplicant *wpa_s = ctx; 843 struct p2p_go_neg_results *params = data; 844 struct wpa_ssid *ssid; 845 int network_id = -1; 846 847 ssid = wpa_s->current_ssid; 848 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { 849 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); 850 if (wpa_s->global->p2p_group_formation == wpa_s) 851 wpa_s->global->p2p_group_formation = NULL; 852 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED 853 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" " 854 "go_dev_addr=" MACSTR "%s", 855 wpa_s->ifname, 856 wpa_ssid_txt(ssid->ssid, ssid->ssid_len), 857 ssid->frequency, 858 params->passphrase ? params->passphrase : "", 859 MAC2STR(wpa_s->global->p2p_dev_addr), 860 params->persistent_group ? " [PERSISTENT]" : ""); 861 862 if (params->persistent_group) 863 network_id = wpas_p2p_store_persistent_group( 864 wpa_s->parent, ssid, 865 wpa_s->global->p2p_dev_addr); 866 if (network_id < 0) 867 network_id = ssid->id; 868 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0); 869 wpas_p2p_cross_connect_setup(wpa_s); 870 wpas_p2p_set_group_idle_timeout(wpa_s); 871 return; 872 } 873 874 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); 875 if (wpa_supplicant_ap_mac_addr_filter(wpa_s, 876 params->peer_interface_addr)) { 877 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " 878 "filtering"); 879 return; 880 } 881 if (params->wps_method == WPS_PBC) 882 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, 883 params->peer_device_addr); 884 else if (wpa_s->p2p_pin[0]) 885 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, 886 wpa_s->p2p_pin, NULL, 0); 887 os_free(wpa_s->go_params); 888 wpa_s->go_params = NULL; 889} 890 891 892static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, 893 struct p2p_go_neg_results *params, 894 int group_formation) 895{ 896 struct wpa_ssid *ssid; 897 898 if (wpas_copy_go_neg_results(wpa_s, params) < 0) 899 return; 900 901 ssid = wpa_config_add_network(wpa_s->conf); 902 if (ssid == NULL) 903 return; 904 905 wpa_s->show_group_started = 0; 906 907 wpa_config_set_network_defaults(ssid); 908 ssid->temporary = 1; 909 ssid->p2p_group = 1; 910 ssid->p2p_persistent_group = params->persistent_group; 911 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : 912 WPAS_MODE_P2P_GO; 913 ssid->frequency = params->freq; 914 ssid->ht40 = params->ht40; 915 ssid->ssid = os_zalloc(params->ssid_len + 1); 916 if (ssid->ssid) { 917 os_memcpy(ssid->ssid, params->ssid, params->ssid_len); 918 ssid->ssid_len = params->ssid_len; 919 } 920 ssid->auth_alg = WPA_AUTH_ALG_OPEN; 921 ssid->key_mgmt = WPA_KEY_MGMT_PSK; 922 ssid->proto = WPA_PROTO_RSN; 923 ssid->pairwise_cipher = WPA_CIPHER_CCMP; 924 ssid->passphrase = os_strdup(params->passphrase); 925 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity; 926 927 wpa_s->ap_configured_cb = p2p_go_configured; 928 wpa_s->ap_configured_cb_ctx = wpa_s; 929 wpa_s->ap_configured_cb_data = wpa_s->go_params; 930 wpa_s->connect_without_scan = ssid; 931 wpa_s->reassociate = 1; 932 wpa_s->disconnected = 0; 933 wpa_supplicant_req_scan(wpa_s, 0, 0); 934} 935 936 937static void wpas_p2p_clone_config(struct wpa_supplicant *dst, 938 const struct wpa_supplicant *src) 939{ 940 struct wpa_config *d; 941 const struct wpa_config *s; 942 943 d = dst->conf; 944 s = src->conf; 945 946#define C(n) if (s->n) d->n = os_strdup(s->n) 947 C(device_name); 948 C(manufacturer); 949 C(model_name); 950 C(model_number); 951 C(serial_number); 952 C(config_methods); 953#undef C 954 955 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); 956 os_memcpy(d->sec_device_type, s->sec_device_type, 957 sizeof(d->sec_device_type)); 958 d->num_sec_device_types = s->num_sec_device_types; 959 960 d->p2p_group_idle = s->p2p_group_idle; 961 d->p2p_intra_bss = s->p2p_intra_bss; 962 d->persistent_reconnect = s->persistent_reconnect; 963 d->max_num_sta = s->max_num_sta; 964 d->pbc_in_m1 = s->pbc_in_m1; 965} 966 967 968static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, 969 enum wpa_driver_if_type type) 970{ 971 char ifname[120], force_ifname[120]; 972 973 if (wpa_s->pending_interface_name[0]) { 974 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " 975 "- skip creation of a new one"); 976 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { 977 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " 978 "unknown?! ifname='%s'", 979 wpa_s->pending_interface_name); 980 return -1; 981 } 982 return 0; 983 } 984 985 os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname, 986 wpa_s->p2p_group_idx); 987 if (os_strlen(ifname) >= IFNAMSIZ && 988 os_strlen(wpa_s->ifname) < IFNAMSIZ) { 989 /* Try to avoid going over the IFNAMSIZ length limit */ 990 os_snprintf(ifname, sizeof(ifname), "p2p-%d", 991 wpa_s->p2p_group_idx); 992 } 993 force_ifname[0] = '\0'; 994 995 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", 996 ifname); 997 wpa_s->p2p_group_idx++; 998 999 wpa_s->pending_interface_type = type; 1000 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, 1001 wpa_s->pending_interface_addr, NULL) < 0) { 1002 wpa_printf(MSG_ERROR, "P2P: Failed to create new group " 1003 "interface"); 1004 return -1; 1005 } 1006 1007 if (force_ifname[0]) { 1008 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", 1009 force_ifname); 1010 os_strlcpy(wpa_s->pending_interface_name, force_ifname, 1011 sizeof(wpa_s->pending_interface_name)); 1012 } else 1013 os_strlcpy(wpa_s->pending_interface_name, ifname, 1014 sizeof(wpa_s->pending_interface_name)); 1015 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " 1016 MACSTR, wpa_s->pending_interface_name, 1017 MAC2STR(wpa_s->pending_interface_addr)); 1018 1019 return 0; 1020} 1021 1022 1023static void wpas_p2p_remove_pending_group_interface( 1024 struct wpa_supplicant *wpa_s) 1025{ 1026 if (!wpa_s->pending_interface_name[0] || 1027 is_zero_ether_addr(wpa_s->pending_interface_addr)) 1028 return; /* No pending virtual interface */ 1029 1030 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", 1031 wpa_s->pending_interface_name); 1032 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, 1033 wpa_s->pending_interface_name); 1034 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); 1035 wpa_s->pending_interface_name[0] = '\0'; 1036} 1037 1038 1039static struct wpa_supplicant * 1040wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) 1041{ 1042 struct wpa_interface iface; 1043 struct wpa_supplicant *group_wpa_s; 1044 1045 if (!wpa_s->pending_interface_name[0]) { 1046 wpa_printf(MSG_ERROR, "P2P: No pending group interface"); 1047 if (!wpas_p2p_create_iface(wpa_s)) 1048 return NULL; 1049 /* 1050 * Something has forced us to remove the pending interface; try 1051 * to create a new one and hope for the best that we will get 1052 * the same local address. 1053 */ 1054 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : 1055 WPA_IF_P2P_CLIENT) < 0) 1056 return NULL; 1057 } 1058 1059 os_memset(&iface, 0, sizeof(iface)); 1060 iface.ifname = wpa_s->pending_interface_name; 1061 iface.driver = wpa_s->driver->name; 1062 iface.ctrl_interface = wpa_s->conf->ctrl_interface; 1063 iface.driver_param = wpa_s->conf->driver_param; 1064 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface); 1065 if (group_wpa_s == NULL) { 1066 wpa_printf(MSG_ERROR, "P2P: Failed to create new " 1067 "wpa_supplicant interface"); 1068 return NULL; 1069 } 1070 wpa_s->pending_interface_name[0] = '\0'; 1071 group_wpa_s->parent = wpa_s; 1072 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : 1073 P2P_GROUP_INTERFACE_CLIENT; 1074 wpa_s->global->p2p_group_formation = group_wpa_s; 1075 1076 wpas_p2p_clone_config(group_wpa_s, wpa_s); 1077 1078 return group_wpa_s; 1079} 1080 1081 1082static void wpas_p2p_group_formation_timeout(void *eloop_ctx, 1083 void *timeout_ctx) 1084{ 1085 struct wpa_supplicant *wpa_s = eloop_ctx; 1086 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); 1087 if (wpa_s->global->p2p) 1088 p2p_group_formation_failed(wpa_s->global->p2p); 1089 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 1090 wpa_drv_p2p_group_formation_failed(wpa_s); 1091 wpas_group_formation_completed(wpa_s, 0); 1092} 1093 1094 1095void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) 1096{ 1097 struct wpa_supplicant *wpa_s = ctx; 1098 1099 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { 1100 wpa_drv_cancel_remain_on_channel(wpa_s); 1101 wpa_s->off_channel_freq = 0; 1102 wpa_s->roc_waiting_drv_freq = 0; 1103 } 1104 1105 if (res->status) { 1106 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d", 1107 res->status); 1108 wpas_notify_p2p_go_neg_completed(wpa_s, res); 1109 wpas_p2p_remove_pending_group_interface(wpa_s); 1110 return; 1111 } 1112 1113 if (wpa_s->p2p_go_ht40) 1114 res->ht40 = 1; 1115 1116 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS); 1117 wpas_notify_p2p_go_neg_completed(wpa_s, res); 1118 1119 if (res->role_go && wpa_s->p2p_persistent_id >= 0) { 1120 struct wpa_ssid *ssid; 1121 ssid = wpa_config_get_network(wpa_s->conf, 1122 wpa_s->p2p_persistent_id); 1123 if (ssid && ssid->disabled == 2 && 1124 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { 1125 size_t len = os_strlen(ssid->passphrase); 1126 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " 1127 "on requested persistent group"); 1128 os_memcpy(res->passphrase, ssid->passphrase, len); 1129 res->passphrase[len] = '\0'; 1130 } 1131 } 1132 1133 if (wpa_s->create_p2p_iface) { 1134 struct wpa_supplicant *group_wpa_s = 1135 wpas_p2p_init_group_interface(wpa_s, res->role_go); 1136 if (group_wpa_s == NULL) { 1137 wpas_p2p_remove_pending_group_interface(wpa_s); 1138 return; 1139 } 1140 if (group_wpa_s != wpa_s) { 1141 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, 1142 sizeof(group_wpa_s->p2p_pin)); 1143 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; 1144 } 1145 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); 1146 wpa_s->pending_interface_name[0] = '\0'; 1147 group_wpa_s->p2p_in_provisioning = 1; 1148 1149 if (res->role_go) 1150 wpas_start_wps_go(group_wpa_s, res, 1); 1151 else 1152 wpas_start_wps_enrollee(group_wpa_s, res); 1153 } else { 1154 wpa_s->p2p_in_provisioning = 1; 1155 wpa_s->global->p2p_group_formation = wpa_s; 1156 1157 if (res->role_go) 1158 wpas_start_wps_go(wpa_s, res, 1); 1159 else 1160 wpas_start_wps_enrollee(ctx, res); 1161 } 1162 1163 wpa_s->p2p_long_listen = 0; 1164 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 1165 1166 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); 1167 eloop_register_timeout(15 + res->peer_config_timeout / 100, 1168 (res->peer_config_timeout % 100) * 10000, 1169 wpas_p2p_group_formation_timeout, wpa_s, NULL); 1170} 1171 1172 1173void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id) 1174{ 1175 struct wpa_supplicant *wpa_s = ctx; 1176 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR 1177 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id); 1178 1179 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id); 1180} 1181 1182 1183void wpas_dev_found(void *ctx, const u8 *addr, 1184 const struct p2p_peer_info *info, 1185 int new_device) 1186{ 1187#ifndef CONFIG_NO_STDOUT_DEBUG 1188 struct wpa_supplicant *wpa_s = ctx; 1189 char devtype[WPS_DEV_TYPE_BUFSIZE]; 1190#define WFD_DEV_INFO_SIZE 9 1191 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1]; 1192 1193#ifdef CONFIG_WIFI_DISPLAY 1194 if (info->wfd_subelems) { 1195 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex), 1196 wpabuf_head(info->wfd_subelems), 1197 WFD_DEV_INFO_SIZE); 1198 } 1199#endif /* CONFIG_WIFI_DISPLAY */ 1200 1201 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR 1202 " p2p_dev_addr=" MACSTR 1203 " pri_dev_type=%s name='%s' config_methods=0x%x " 1204 "dev_capab=0x%x group_capab=0x%x%s%s", 1205 MAC2STR(addr), MAC2STR(info->p2p_device_addr), 1206 wps_dev_type_bin2str(info->pri_dev_type, devtype, 1207 sizeof(devtype)), 1208 info->device_name, info->config_methods, 1209 info->dev_capab, info->group_capab, 1210 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex); 1211#endif /* CONFIG_NO_STDOUT_DEBUG */ 1212 1213 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); 1214} 1215 1216 1217static void wpas_dev_lost(void *ctx, const u8 *dev_addr) 1218{ 1219 struct wpa_supplicant *wpa_s = ctx; 1220 1221 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST 1222 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); 1223 1224 wpas_notify_p2p_device_lost(wpa_s, dev_addr); 1225} 1226 1227 1228static int wpas_start_listen(void *ctx, unsigned int freq, 1229 unsigned int duration, 1230 const struct wpabuf *probe_resp_ie) 1231{ 1232 struct wpa_supplicant *wpa_s = ctx; 1233 1234 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL); 1235 1236 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { 1237 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " 1238 "report received Probe Request frames"); 1239 return -1; 1240 } 1241 1242 wpa_s->pending_listen_freq = freq; 1243 wpa_s->pending_listen_duration = duration; 1244 1245 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) { 1246 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " 1247 "to remain on channel (%u MHz) for Listen " 1248 "state", freq); 1249 wpa_s->pending_listen_freq = 0; 1250 return -1; 1251 } 1252 wpa_s->off_channel_freq = 0; 1253 wpa_s->roc_waiting_drv_freq = freq; 1254 1255 return 0; 1256} 1257 1258 1259static void wpas_stop_listen(void *ctx) 1260{ 1261 struct wpa_supplicant *wpa_s = ctx; 1262 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { 1263 wpa_drv_cancel_remain_on_channel(wpa_s); 1264 wpa_s->off_channel_freq = 0; 1265 wpa_s->roc_waiting_drv_freq = 0; 1266 } 1267 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); 1268 wpa_drv_probe_req_report(wpa_s, 0); 1269} 1270 1271 1272static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf) 1273{ 1274 struct wpa_supplicant *wpa_s = ctx; 1275 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1); 1276} 1277 1278 1279static struct p2p_srv_bonjour * 1280wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s, 1281 const struct wpabuf *query) 1282{ 1283 struct p2p_srv_bonjour *bsrv; 1284 size_t len; 1285 1286 len = wpabuf_len(query); 1287 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, 1288 struct p2p_srv_bonjour, list) { 1289 if (len == wpabuf_len(bsrv->query) && 1290 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query), 1291 len) == 0) 1292 return bsrv; 1293 } 1294 return NULL; 1295} 1296 1297 1298static struct p2p_srv_upnp * 1299wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version, 1300 const char *service) 1301{ 1302 struct p2p_srv_upnp *usrv; 1303 1304 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, 1305 struct p2p_srv_upnp, list) { 1306 if (version == usrv->version && 1307 os_strcmp(service, usrv->service) == 0) 1308 return usrv; 1309 } 1310 return NULL; 1311} 1312 1313 1314static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto, 1315 u8 srv_trans_id) 1316{ 1317 u8 *len_pos; 1318 1319 if (wpabuf_tailroom(resp) < 5) 1320 return; 1321 1322 /* Length (to be filled) */ 1323 len_pos = wpabuf_put(resp, 2); 1324 wpabuf_put_u8(resp, srv_proto); 1325 wpabuf_put_u8(resp, srv_trans_id); 1326 /* Status Code */ 1327 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE); 1328 /* Response Data: empty */ 1329 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); 1330} 1331 1332 1333static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s, 1334 struct wpabuf *resp, u8 srv_trans_id) 1335{ 1336 struct p2p_srv_bonjour *bsrv; 1337 u8 *len_pos; 1338 1339 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services"); 1340 1341 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { 1342 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); 1343 return; 1344 } 1345 1346 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour, 1347 struct p2p_srv_bonjour, list) { 1348 if (wpabuf_tailroom(resp) < 1349 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) 1350 return; 1351 /* Length (to be filled) */ 1352 len_pos = wpabuf_put(resp, 2); 1353 wpabuf_put_u8(resp, P2P_SERV_BONJOUR); 1354 wpabuf_put_u8(resp, srv_trans_id); 1355 /* Status Code */ 1356 wpabuf_put_u8(resp, P2P_SD_SUCCESS); 1357 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", 1358 wpabuf_head(bsrv->resp), 1359 wpabuf_len(bsrv->resp)); 1360 /* Response Data */ 1361 wpabuf_put_buf(resp, bsrv->query); /* Key */ 1362 wpabuf_put_buf(resp, bsrv->resp); /* Value */ 1363 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 1364 2); 1365 } 1366} 1367 1368 1369static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s, 1370 struct wpabuf *resp, u8 srv_trans_id, 1371 const u8 *query, size_t query_len) 1372{ 1373 struct p2p_srv_bonjour *bsrv; 1374 struct wpabuf buf; 1375 u8 *len_pos; 1376 1377 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour", 1378 query, query_len); 1379 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { 1380 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available"); 1381 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR, 1382 srv_trans_id); 1383 return; 1384 } 1385 1386 if (query_len == 0) { 1387 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); 1388 return; 1389 } 1390 1391 if (wpabuf_tailroom(resp) < 5) 1392 return; 1393 /* Length (to be filled) */ 1394 len_pos = wpabuf_put(resp, 2); 1395 wpabuf_put_u8(resp, P2P_SERV_BONJOUR); 1396 wpabuf_put_u8(resp, srv_trans_id); 1397 1398 wpabuf_set(&buf, query, query_len); 1399 bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf); 1400 if (bsrv == NULL) { 1401 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not " 1402 "available"); 1403 1404 /* Status Code */ 1405 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); 1406 /* Response Data: empty */ 1407 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 1408 2); 1409 return; 1410 } 1411 1412 /* Status Code */ 1413 wpabuf_put_u8(resp, P2P_SD_SUCCESS); 1414 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service", 1415 wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp)); 1416 1417 if (wpabuf_tailroom(resp) >= 1418 wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) { 1419 /* Response Data */ 1420 wpabuf_put_buf(resp, bsrv->query); /* Key */ 1421 wpabuf_put_buf(resp, bsrv->resp); /* Value */ 1422 } 1423 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); 1424} 1425 1426 1427static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s, 1428 struct wpabuf *resp, u8 srv_trans_id) 1429{ 1430 struct p2p_srv_upnp *usrv; 1431 u8 *len_pos; 1432 1433 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services"); 1434 1435 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { 1436 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); 1437 return; 1438 } 1439 1440 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, 1441 struct p2p_srv_upnp, list) { 1442 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service)) 1443 return; 1444 1445 /* Length (to be filled) */ 1446 len_pos = wpabuf_put(resp, 2); 1447 wpabuf_put_u8(resp, P2P_SERV_UPNP); 1448 wpabuf_put_u8(resp, srv_trans_id); 1449 1450 /* Status Code */ 1451 wpabuf_put_u8(resp, P2P_SD_SUCCESS); 1452 /* Response Data */ 1453 wpabuf_put_u8(resp, usrv->version); 1454 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", 1455 usrv->service); 1456 wpabuf_put_str(resp, usrv->service); 1457 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 1458 2); 1459 } 1460} 1461 1462 1463static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s, 1464 struct wpabuf *resp, u8 srv_trans_id, 1465 const u8 *query, size_t query_len) 1466{ 1467 struct p2p_srv_upnp *usrv; 1468 u8 *len_pos; 1469 u8 version; 1470 char *str; 1471 int count = 0; 1472 1473 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP", 1474 query, query_len); 1475 1476 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) { 1477 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available"); 1478 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP, 1479 srv_trans_id); 1480 return; 1481 } 1482 1483 if (query_len == 0) { 1484 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); 1485 return; 1486 } 1487 1488 if (wpabuf_tailroom(resp) < 5) 1489 return; 1490 1491 /* Length (to be filled) */ 1492 len_pos = wpabuf_put(resp, 2); 1493 wpabuf_put_u8(resp, P2P_SERV_UPNP); 1494 wpabuf_put_u8(resp, srv_trans_id); 1495 1496 version = query[0]; 1497 str = os_malloc(query_len); 1498 if (str == NULL) 1499 return; 1500 os_memcpy(str, query + 1, query_len - 1); 1501 str[query_len - 1] = '\0'; 1502 1503 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp, 1504 struct p2p_srv_upnp, list) { 1505 if (version != usrv->version) 1506 continue; 1507 1508 if (os_strcmp(str, "ssdp:all") != 0 && 1509 os_strstr(usrv->service, str) == NULL) 1510 continue; 1511 1512 if (wpabuf_tailroom(resp) < 2) 1513 break; 1514 if (count == 0) { 1515 /* Status Code */ 1516 wpabuf_put_u8(resp, P2P_SD_SUCCESS); 1517 /* Response Data */ 1518 wpabuf_put_u8(resp, version); 1519 } else 1520 wpabuf_put_u8(resp, ','); 1521 1522 count++; 1523 1524 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s", 1525 usrv->service); 1526 if (wpabuf_tailroom(resp) < os_strlen(usrv->service)) 1527 break; 1528 wpabuf_put_str(resp, usrv->service); 1529 } 1530 os_free(str); 1531 1532 if (count == 0) { 1533 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not " 1534 "available"); 1535 /* Status Code */ 1536 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE); 1537 /* Response Data: empty */ 1538 } 1539 1540 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); 1541} 1542 1543 1544#ifdef CONFIG_WIFI_DISPLAY 1545static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s, 1546 struct wpabuf *resp, u8 srv_trans_id, 1547 const u8 *query, size_t query_len) 1548{ 1549 const u8 *pos; 1550 u8 role; 1551 u8 *len_pos; 1552 1553 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len); 1554 1555 if (!wpa_s->global->wifi_display) { 1556 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available"); 1557 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY, 1558 srv_trans_id); 1559 return; 1560 } 1561 1562 if (query_len < 1) { 1563 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device " 1564 "Role"); 1565 return; 1566 } 1567 1568 if (wpabuf_tailroom(resp) < 5) 1569 return; 1570 1571 pos = query; 1572 role = *pos++; 1573 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role); 1574 1575 /* TODO: role specific handling */ 1576 1577 /* Length (to be filled) */ 1578 len_pos = wpabuf_put(resp, 2); 1579 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY); 1580 wpabuf_put_u8(resp, srv_trans_id); 1581 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */ 1582 1583 while (pos < query + query_len) { 1584 if (*pos < MAX_WFD_SUBELEMS && 1585 wpa_s->global->wfd_subelem[*pos] && 1586 wpabuf_tailroom(resp) >= 1587 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) { 1588 wpa_printf(MSG_DEBUG, "P2P: Add WSD response " 1589 "subelement %u", *pos); 1590 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]); 1591 } 1592 pos++; 1593 } 1594 1595 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2); 1596} 1597#endif /* CONFIG_WIFI_DISPLAY */ 1598 1599 1600void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token, 1601 u16 update_indic, const u8 *tlvs, size_t tlvs_len) 1602{ 1603 struct wpa_supplicant *wpa_s = ctx; 1604 const u8 *pos = tlvs; 1605 const u8 *end = tlvs + tlvs_len; 1606 const u8 *tlv_end; 1607 u16 slen; 1608 struct wpabuf *resp; 1609 u8 srv_proto, srv_trans_id; 1610 size_t buf_len; 1611 char *buf; 1612 1613 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs", 1614 tlvs, tlvs_len); 1615 buf_len = 2 * tlvs_len + 1; 1616 buf = os_malloc(buf_len); 1617 if (buf) { 1618 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); 1619 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d " 1620 MACSTR " %u %u %s", 1621 freq, MAC2STR(sa), dialog_token, update_indic, 1622 buf); 1623 os_free(buf); 1624 } 1625 1626 if (wpa_s->p2p_sd_over_ctrl_iface) { 1627 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, 1628 update_indic, tlvs, tlvs_len); 1629 return; /* to be processed by an external program */ 1630 } 1631 1632 resp = wpabuf_alloc(10000); 1633 if (resp == NULL) 1634 return; 1635 1636 while (pos + 1 < end) { 1637 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV"); 1638 slen = WPA_GET_LE16(pos); 1639 pos += 2; 1640 if (pos + slen > end || slen < 2) { 1641 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data " 1642 "length"); 1643 wpabuf_free(resp); 1644 return; 1645 } 1646 tlv_end = pos + slen; 1647 1648 srv_proto = *pos++; 1649 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", 1650 srv_proto); 1651 srv_trans_id = *pos++; 1652 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", 1653 srv_trans_id); 1654 1655 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data", 1656 pos, tlv_end - pos); 1657 1658 1659 if (wpa_s->force_long_sd) { 1660 wpa_printf(MSG_DEBUG, "P2P: SD test - force long " 1661 "response"); 1662 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); 1663 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); 1664 goto done; 1665 } 1666 1667 switch (srv_proto) { 1668 case P2P_SERV_ALL_SERVICES: 1669 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request " 1670 "for all services"); 1671 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) && 1672 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) { 1673 wpa_printf(MSG_DEBUG, "P2P: No service " 1674 "discovery protocols available"); 1675 wpas_sd_add_proto_not_avail( 1676 resp, P2P_SERV_ALL_SERVICES, 1677 srv_trans_id); 1678 break; 1679 } 1680 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id); 1681 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id); 1682 break; 1683 case P2P_SERV_BONJOUR: 1684 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id, 1685 pos, tlv_end - pos); 1686 break; 1687 case P2P_SERV_UPNP: 1688 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id, 1689 pos, tlv_end - pos); 1690 break; 1691#ifdef CONFIG_WIFI_DISPLAY 1692 case P2P_SERV_WIFI_DISPLAY: 1693 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id, 1694 pos, tlv_end - pos); 1695 break; 1696#endif /* CONFIG_WIFI_DISPLAY */ 1697 default: 1698 wpa_printf(MSG_DEBUG, "P2P: Unavailable service " 1699 "protocol %u", srv_proto); 1700 wpas_sd_add_proto_not_avail(resp, srv_proto, 1701 srv_trans_id); 1702 break; 1703 } 1704 1705 pos = tlv_end; 1706 } 1707 1708done: 1709 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token, 1710 update_indic, tlvs, tlvs_len); 1711 1712 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp); 1713 1714 wpabuf_free(resp); 1715} 1716 1717 1718void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic, 1719 const u8 *tlvs, size_t tlvs_len) 1720{ 1721 struct wpa_supplicant *wpa_s = ctx; 1722 const u8 *pos = tlvs; 1723 const u8 *end = tlvs + tlvs_len; 1724 const u8 *tlv_end; 1725 u16 slen; 1726 size_t buf_len; 1727 char *buf; 1728 1729 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs", 1730 tlvs, tlvs_len); 1731 if (tlvs_len > 1500) { 1732 /* TODO: better way for handling this */ 1733 wpa_msg_ctrl(wpa_s, MSG_INFO, 1734 P2P_EVENT_SERV_DISC_RESP MACSTR 1735 " %u <long response: %u bytes>", 1736 MAC2STR(sa), update_indic, 1737 (unsigned int) tlvs_len); 1738 } else { 1739 buf_len = 2 * tlvs_len + 1; 1740 buf = os_malloc(buf_len); 1741 if (buf) { 1742 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len); 1743 wpa_msg_ctrl(wpa_s, MSG_INFO, 1744 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s", 1745 MAC2STR(sa), update_indic, buf); 1746 os_free(buf); 1747 } 1748 } 1749 1750 while (pos < end) { 1751 u8 srv_proto, srv_trans_id, status; 1752 1753 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV"); 1754 slen = WPA_GET_LE16(pos); 1755 pos += 2; 1756 if (pos + slen > end || slen < 3) { 1757 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data " 1758 "length"); 1759 return; 1760 } 1761 tlv_end = pos + slen; 1762 1763 srv_proto = *pos++; 1764 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u", 1765 srv_proto); 1766 srv_trans_id = *pos++; 1767 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u", 1768 srv_trans_id); 1769 status = *pos++; 1770 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u", 1771 status); 1772 1773 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data", 1774 pos, tlv_end - pos); 1775 1776 pos = tlv_end; 1777 } 1778 1779 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len); 1780} 1781 1782 1783u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst, 1784 const struct wpabuf *tlvs) 1785{ 1786 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 1787 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs); 1788 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 1789 return 0; 1790 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs); 1791} 1792 1793 1794u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst, 1795 u8 version, const char *query) 1796{ 1797 struct wpabuf *tlvs; 1798 u64 ret; 1799 1800 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query)); 1801 if (tlvs == NULL) 1802 return 0; 1803 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query)); 1804 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */ 1805 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */ 1806 wpabuf_put_u8(tlvs, version); 1807 wpabuf_put_str(tlvs, query); 1808 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs); 1809 wpabuf_free(tlvs); 1810 return ret; 1811} 1812 1813 1814#ifdef CONFIG_WIFI_DISPLAY 1815 1816static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst, 1817 const struct wpabuf *tlvs) 1818{ 1819 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 1820 return 0; 1821 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 1822 return 0; 1823 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs); 1824} 1825 1826 1827#define MAX_WFD_SD_SUBELEMS 20 1828 1829static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role, 1830 const char *subelems) 1831{ 1832 u8 *len; 1833 const char *pos; 1834 int val; 1835 int count = 0; 1836 1837 len = wpabuf_put(tlvs, 2); 1838 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */ 1839 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */ 1840 1841 wpabuf_put_u8(tlvs, role); 1842 1843 pos = subelems; 1844 while (*pos) { 1845 val = atoi(pos); 1846 if (val >= 0 && val < 256) { 1847 wpabuf_put_u8(tlvs, val); 1848 count++; 1849 if (count == MAX_WFD_SD_SUBELEMS) 1850 break; 1851 } 1852 pos = os_strchr(pos + 1, ','); 1853 if (pos == NULL) 1854 break; 1855 pos++; 1856 } 1857 1858 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2); 1859} 1860 1861 1862u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s, 1863 const u8 *dst, const char *role) 1864{ 1865 struct wpabuf *tlvs; 1866 u64 ret; 1867 const char *subelems; 1868 u8 id = 1; 1869 1870 subelems = os_strchr(role, ' '); 1871 if (subelems == NULL) 1872 return 0; 1873 subelems++; 1874 1875 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS)); 1876 if (tlvs == NULL) 1877 return 0; 1878 1879 if (os_strstr(role, "[source]")) 1880 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems); 1881 if (os_strstr(role, "[pri-sink]")) 1882 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems); 1883 if (os_strstr(role, "[sec-sink]")) 1884 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems); 1885 if (os_strstr(role, "[source+sink]")) 1886 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems); 1887 1888 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs); 1889 wpabuf_free(tlvs); 1890 return ret; 1891} 1892 1893#endif /* CONFIG_WIFI_DISPLAY */ 1894 1895 1896int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req) 1897{ 1898 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 1899 return wpa_drv_p2p_sd_cancel_request(wpa_s, req); 1900 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 1901 return -1; 1902 return p2p_sd_cancel_request(wpa_s->global->p2p, 1903 (void *) (uintptr_t) req); 1904} 1905 1906 1907void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq, 1908 const u8 *dst, u8 dialog_token, 1909 const struct wpabuf *resp_tlvs) 1910{ 1911 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { 1912 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token, 1913 resp_tlvs); 1914 return; 1915 } 1916 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 1917 return; 1918 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token, 1919 resp_tlvs); 1920} 1921 1922#ifdef ANDROID_P2P 1923void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action) 1924#else 1925void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s) 1926#endif 1927{ 1928 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { 1929 wpa_drv_p2p_service_update(wpa_s); 1930 return; 1931 } 1932 if (wpa_s->global->p2p) 1933#ifdef ANDROID_P2P 1934 p2p_sd_service_update(wpa_s->global->p2p, action); 1935#else 1936 p2p_sd_service_update(wpa_s->global->p2p); 1937#endif 1938} 1939 1940 1941static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv) 1942{ 1943 dl_list_del(&bsrv->list); 1944 wpabuf_free(bsrv->query); 1945 wpabuf_free(bsrv->resp); 1946 os_free(bsrv); 1947} 1948 1949 1950static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv) 1951{ 1952 dl_list_del(&usrv->list); 1953 os_free(usrv->service); 1954 os_free(usrv); 1955} 1956 1957 1958void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s) 1959{ 1960 struct p2p_srv_bonjour *bsrv, *bn; 1961 struct p2p_srv_upnp *usrv, *un; 1962 1963 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour, 1964 struct p2p_srv_bonjour, list) 1965 wpas_p2p_srv_bonjour_free(bsrv); 1966 1967 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp, 1968 struct p2p_srv_upnp, list) 1969 wpas_p2p_srv_upnp_free(usrv); 1970 1971#ifdef ANDROID_P2P 1972 wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH); 1973#else 1974 wpas_p2p_sd_service_update(wpa_s); 1975#endif 1976} 1977 1978 1979int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s, 1980 struct wpabuf *query, struct wpabuf *resp) 1981{ 1982 struct p2p_srv_bonjour *bsrv; 1983 1984 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); 1985 if (bsrv) { 1986 wpabuf_free(query); 1987 wpabuf_free(bsrv->resp); 1988 bsrv->resp = resp; 1989 return 0; 1990 } 1991 1992 bsrv = os_zalloc(sizeof(*bsrv)); 1993 if (bsrv == NULL) 1994 return -1; 1995 bsrv->query = query; 1996 bsrv->resp = resp; 1997 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list); 1998 1999#ifdef ANDROID_P2P 2000 wpas_p2p_sd_service_update(wpa_s, SRV_ADD); 2001#else 2002 wpas_p2p_sd_service_update(wpa_s); 2003#endif 2004 return 0; 2005} 2006 2007 2008int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s, 2009 const struct wpabuf *query) 2010{ 2011 struct p2p_srv_bonjour *bsrv; 2012 2013 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query); 2014 if (bsrv == NULL) 2015 return -1; 2016 wpas_p2p_srv_bonjour_free(bsrv); 2017#ifdef ANDROID_P2P 2018 wpas_p2p_sd_service_update(wpa_s, SRV_DEL); 2019#else 2020 wpas_p2p_sd_service_update(wpa_s); 2021#endif 2022 return 0; 2023} 2024 2025 2026int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version, 2027 const char *service) 2028{ 2029 struct p2p_srv_upnp *usrv; 2030 2031 if (wpas_p2p_service_get_upnp(wpa_s, version, service)) 2032 return 0; /* Already listed */ 2033 usrv = os_zalloc(sizeof(*usrv)); 2034 if (usrv == NULL) 2035 return -1; 2036 usrv->version = version; 2037 usrv->service = os_strdup(service); 2038 if (usrv->service == NULL) { 2039 os_free(usrv); 2040 return -1; 2041 } 2042 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list); 2043 2044#ifdef ANDROID_P2P 2045 wpas_p2p_sd_service_update(wpa_s, SRV_ADD); 2046#else 2047 wpas_p2p_sd_service_update(wpa_s); 2048#endif 2049 return 0; 2050} 2051 2052 2053int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version, 2054 const char *service) 2055{ 2056 struct p2p_srv_upnp *usrv; 2057 2058 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service); 2059 if (usrv == NULL) 2060 return -1; 2061 wpas_p2p_srv_upnp_free(usrv); 2062#ifdef ANDROID_P2P 2063 wpas_p2p_sd_service_update(wpa_s, SRV_DEL); 2064#else 2065 wpas_p2p_sd_service_update(wpa_s); 2066#endif 2067 return 0; 2068} 2069 2070 2071static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, 2072 const u8 *peer, const char *params, 2073 unsigned int generated_pin) 2074{ 2075 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s", 2076 MAC2STR(peer), generated_pin, params); 2077} 2078 2079 2080static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, 2081 const u8 *peer, const char *params) 2082{ 2083 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s", 2084 MAC2STR(peer), params); 2085} 2086 2087 2088void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, 2089 const u8 *dev_addr, const u8 *pri_dev_type, 2090 const char *dev_name, u16 supp_config_methods, 2091 u8 dev_capab, u8 group_capab, const u8 *group_id, 2092 size_t group_id_len) 2093{ 2094 struct wpa_supplicant *wpa_s = ctx; 2095 char devtype[WPS_DEV_TYPE_BUFSIZE]; 2096 char params[300]; 2097 u8 empty_dev_type[8]; 2098 unsigned int generated_pin = 0; 2099 struct wpa_supplicant *group = NULL; 2100 2101 if (group_id) { 2102 for (group = wpa_s->global->ifaces; group; group = group->next) 2103 { 2104 struct wpa_ssid *s = group->current_ssid; 2105 if (s != NULL && 2106 s->mode == WPAS_MODE_P2P_GO && 2107 group_id_len - ETH_ALEN == s->ssid_len && 2108 os_memcmp(group_id + ETH_ALEN, s->ssid, 2109 s->ssid_len) == 0) 2110 break; 2111 } 2112 } 2113 2114 if (pri_dev_type == NULL) { 2115 os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); 2116 pri_dev_type = empty_dev_type; 2117 } 2118 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR 2119 " pri_dev_type=%s name='%s' config_methods=0x%x " 2120 "dev_capab=0x%x group_capab=0x%x%s%s", 2121 MAC2STR(dev_addr), 2122 wps_dev_type_bin2str(pri_dev_type, devtype, 2123 sizeof(devtype)), 2124 dev_name, supp_config_methods, dev_capab, group_capab, 2125 group ? " group=" : "", 2126 group ? group->ifname : ""); 2127 params[sizeof(params) - 1] = '\0'; 2128 2129 if (config_methods & WPS_CONFIG_DISPLAY) { 2130 generated_pin = wps_generate_pin(); 2131 wpas_prov_disc_local_display(wpa_s, peer, params, 2132 generated_pin); 2133 } else if (config_methods & WPS_CONFIG_KEYPAD) 2134 wpas_prov_disc_local_keypad(wpa_s, peer, params); 2135 else if (config_methods & WPS_CONFIG_PUSHBUTTON) 2136 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR 2137 "%s", MAC2STR(peer), params); 2138 2139 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, 2140 P2P_PROV_DISC_SUCCESS, 2141 config_methods, generated_pin); 2142} 2143 2144 2145void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) 2146{ 2147 struct wpa_supplicant *wpa_s = ctx; 2148 unsigned int generated_pin = 0; 2149 char params[20]; 2150 2151 if (wpa_s->pending_pd_before_join && 2152 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || 2153 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { 2154 wpa_s->pending_pd_before_join = 0; 2155 wpa_printf(MSG_DEBUG, "P2P: Starting pending " 2156 "join-existing-group operation"); 2157 wpas_p2p_join_start(wpa_s); 2158 return; 2159 } 2160 2161 if (wpa_s->pending_pd_use == AUTO_PD_JOIN || 2162 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) 2163 os_snprintf(params, sizeof(params), " peer_go=%d", 2164 wpa_s->pending_pd_use == AUTO_PD_JOIN); 2165 else 2166 params[0] = '\0'; 2167 2168 if (config_methods & WPS_CONFIG_DISPLAY) 2169 wpas_prov_disc_local_keypad(wpa_s, peer, params); 2170 else if (config_methods & WPS_CONFIG_KEYPAD) { 2171 generated_pin = wps_generate_pin(); 2172 wpas_prov_disc_local_display(wpa_s, peer, params, 2173 generated_pin); 2174 } else if (config_methods & WPS_CONFIG_PUSHBUTTON) 2175 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR 2176 "%s", MAC2STR(peer), params); 2177 2178 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, 2179 P2P_PROV_DISC_SUCCESS, 2180 config_methods, generated_pin); 2181} 2182 2183 2184static void wpas_prov_disc_fail(void *ctx, const u8 *peer, 2185 enum p2p_prov_disc_status status) 2186{ 2187 struct wpa_supplicant *wpa_s = ctx; 2188 2189 if (wpa_s->p2p_fallback_to_go_neg) { 2190 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " 2191 "failed - fall back to GO Negotiation"); 2192 wpas_p2p_fallback_to_go_neg(wpa_s, 0); 2193 return; 2194 } 2195 2196 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE 2197 " p2p_dev_addr=" MACSTR " status=%d", 2198 MAC2STR(peer), status); 2199 2200 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, 2201 status, 0, 0); 2202} 2203 2204 2205static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, 2206 const u8 *go_dev_addr, const u8 *ssid, 2207 size_t ssid_len, int *go, u8 *group_bssid, 2208 int *force_freq, int persistent_group) 2209{ 2210 struct wpa_supplicant *wpa_s = ctx; 2211 struct wpa_ssid *s; 2212 u8 cur_bssid[ETH_ALEN]; 2213 int res; 2214 struct wpa_supplicant *grp; 2215 2216 if (!persistent_group) { 2217 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR 2218 " to join an active group", MAC2STR(sa)); 2219 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && 2220 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) 2221 == 0 || 2222 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { 2223 wpa_printf(MSG_DEBUG, "P2P: Accept previously " 2224 "authorized invitation"); 2225 goto accept_inv; 2226 } 2227 /* 2228 * Do not accept the invitation automatically; notify user and 2229 * request approval. 2230 */ 2231 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; 2232 } 2233 2234 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); 2235 if (grp) { 2236 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " 2237 "running persistent group"); 2238 if (*go) 2239 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); 2240 goto accept_inv; 2241 } 2242 2243 if (!wpa_s->conf->persistent_reconnect) 2244 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; 2245 2246 for (s = wpa_s->conf->ssid; s; s = s->next) { 2247 if (s->disabled == 2 && 2248 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && 2249 s->ssid_len == ssid_len && 2250 os_memcmp(ssid, s->ssid, ssid_len) == 0) 2251 break; 2252 } 2253 2254 if (!s) { 2255 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR 2256 " requested reinvocation of an unknown group", 2257 MAC2STR(sa)); 2258 return P2P_SC_FAIL_UNKNOWN_GROUP; 2259 } 2260 2261 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { 2262 *go = 1; 2263 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { 2264 wpa_printf(MSG_DEBUG, "P2P: The only available " 2265 "interface is already in use - reject " 2266 "invitation"); 2267 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 2268 } 2269 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); 2270 } else if (s->mode == WPAS_MODE_P2P_GO) { 2271 *go = 1; 2272 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) 2273 { 2274 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " 2275 "interface address for the group"); 2276 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 2277 } 2278 os_memcpy(group_bssid, wpa_s->pending_interface_addr, 2279 ETH_ALEN); 2280 } 2281 2282accept_inv: 2283 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 && 2284 wpa_s->assoc_freq) { 2285 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match " 2286 "the channel we are already using"); 2287 *force_freq = wpa_s->assoc_freq; 2288 } 2289 2290 res = wpa_drv_shared_freq(wpa_s); 2291 if (res > 0) { 2292 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match " 2293 "with the channel we are already using on a " 2294 "shared interface"); 2295 *force_freq = res; 2296 } 2297 2298 return P2P_SC_SUCCESS; 2299} 2300 2301 2302static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, 2303 const u8 *ssid, size_t ssid_len, 2304 const u8 *go_dev_addr, u8 status, 2305 int op_freq) 2306{ 2307 struct wpa_supplicant *wpa_s = ctx; 2308 struct wpa_ssid *s; 2309 2310 for (s = wpa_s->conf->ssid; s; s = s->next) { 2311 if (s->disabled == 2 && 2312 s->ssid_len == ssid_len && 2313 os_memcmp(ssid, s->ssid, ssid_len) == 0) 2314 break; 2315 } 2316 2317 if (status == P2P_SC_SUCCESS) { 2318 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR 2319 " was accepted; op_freq=%d MHz", 2320 MAC2STR(sa), op_freq); 2321 if (s) { 2322 wpas_p2p_group_add_persistent( 2323 wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0); 2324 } else if (bssid) { 2325 wpas_p2p_join(wpa_s, bssid, go_dev_addr, 2326 wpa_s->p2p_wps_method, 0); 2327 } 2328 return; 2329 } 2330 2331 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { 2332 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR 2333 " was rejected (status %u)", MAC2STR(sa), status); 2334 return; 2335 } 2336 2337 if (!s) { 2338 if (bssid) { 2339 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED 2340 "sa=" MACSTR " go_dev_addr=" MACSTR 2341 " bssid=" MACSTR " unknown-network", 2342 MAC2STR(sa), MAC2STR(go_dev_addr), 2343 MAC2STR(bssid)); 2344 } else { 2345 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED 2346 "sa=" MACSTR " go_dev_addr=" MACSTR 2347 " unknown-network", 2348 MAC2STR(sa), MAC2STR(go_dev_addr)); 2349 } 2350 return; 2351 } 2352 2353 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR 2354 " persistent=%d", MAC2STR(sa), s->id); 2355} 2356 2357 2358static void wpas_invitation_result(void *ctx, int status, const u8 *bssid) 2359{ 2360 struct wpa_supplicant *wpa_s = ctx; 2361 struct wpa_ssid *ssid; 2362 2363 if (bssid) { 2364 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT 2365 "status=%d " MACSTR, 2366 status, MAC2STR(bssid)); 2367 } else { 2368 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT 2369 "status=%d ", status); 2370 } 2371 wpas_notify_p2p_invitation_result(wpa_s, status, bssid); 2372 2373 if (wpa_s->pending_invite_ssid_id == -1) 2374 return; /* Invitation to active group */ 2375 2376 if (status != P2P_SC_SUCCESS) { 2377 wpas_p2p_remove_pending_group_interface(wpa_s); 2378 return; 2379 } 2380 2381 ssid = wpa_config_get_network(wpa_s->conf, 2382 wpa_s->pending_invite_ssid_id); 2383 if (ssid == NULL) { 2384 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " 2385 "data matching with invitation"); 2386 return; 2387 } 2388 2389 wpas_p2p_group_add_persistent(wpa_s, ssid, 2390 ssid->mode == WPAS_MODE_P2P_GO, 2391 wpa_s->p2p_persistent_go_freq, 2392 wpa_s->p2p_go_ht40); 2393} 2394 2395 2396static int wpas_p2p_disallowed_freq(struct wpa_global *global, 2397 unsigned int freq) 2398{ 2399 unsigned int i; 2400 2401 if (global->p2p_disallow_freq == NULL) 2402 return 0; 2403 2404 for (i = 0; i < global->num_p2p_disallow_freq; i++) { 2405 if (freq >= global->p2p_disallow_freq[i].min && 2406 freq <= global->p2p_disallow_freq[i].max) 2407 return 1; 2408 } 2409 2410 return 0; 2411} 2412 2413 2414static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) 2415{ 2416 reg->channel[reg->channels] = chan; 2417 reg->channels++; 2418} 2419 2420 2421static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, 2422 struct p2p_channels *chan) 2423{ 2424 int i, cla = 0; 2425 2426 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " 2427 "band"); 2428 2429 /* Operating class 81 - 2.4 GHz band channels 1..13 */ 2430 chan->reg_class[cla].reg_class = 81; 2431 chan->reg_class[cla].channels = 0; 2432 for (i = 0; i < 11; i++) { 2433 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) 2434 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); 2435 } 2436 if (chan->reg_class[cla].channels) 2437 cla++; 2438 2439 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " 2440 "band"); 2441 2442 /* Operating class 115 - 5 GHz, channels 36-48 */ 2443 chan->reg_class[cla].reg_class = 115; 2444 chan->reg_class[cla].channels = 0; 2445 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) 2446 wpas_p2p_add_chan(&chan->reg_class[cla], 36); 2447 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) 2448 wpas_p2p_add_chan(&chan->reg_class[cla], 40); 2449 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) 2450 wpas_p2p_add_chan(&chan->reg_class[cla], 44); 2451 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) 2452 wpas_p2p_add_chan(&chan->reg_class[cla], 48); 2453 if (chan->reg_class[cla].channels) 2454 cla++; 2455 2456 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " 2457 "band"); 2458 2459 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ 2460 chan->reg_class[cla].reg_class = 124; 2461 chan->reg_class[cla].channels = 0; 2462 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) 2463 wpas_p2p_add_chan(&chan->reg_class[cla], 149); 2464 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) 2465 wpas_p2p_add_chan(&chan->reg_class[cla], 153); 2466 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) 2467 wpas_p2p_add_chan(&chan->reg_class[cla], 157); 2468 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) 2469 wpas_p2p_add_chan(&chan->reg_class[cla], 161); 2470 if (chan->reg_class[cla].channels) 2471 cla++; 2472 2473 chan->reg_classes = cla; 2474 return 0; 2475} 2476 2477 2478static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes, 2479 u16 num_modes, 2480 enum hostapd_hw_mode mode) 2481{ 2482 u16 i; 2483 2484 for (i = 0; i < num_modes; i++) { 2485 if (modes[i].mode == mode) 2486 return &modes[i]; 2487 } 2488 2489 return NULL; 2490} 2491 2492 2493static int has_channel(struct wpa_global *global, 2494 struct hostapd_hw_modes *mode, u8 chan, int *flags) 2495{ 2496 int i; 2497 unsigned int freq; 2498 2499 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) + 2500 chan * 5; 2501 if (wpas_p2p_disallowed_freq(global, freq)) 2502 return 0; 2503 2504 for (i = 0; i < mode->num_channels; i++) { 2505 if (mode->channels[i].chan == chan) { 2506 if (flags) 2507 *flags = mode->channels[i].flag; 2508 return !(mode->channels[i].flag & 2509 (HOSTAPD_CHAN_DISABLED | 2510 HOSTAPD_CHAN_PASSIVE_SCAN | 2511 HOSTAPD_CHAN_NO_IBSS | 2512 HOSTAPD_CHAN_RADAR)); 2513 } 2514 } 2515 2516 return 0; 2517} 2518 2519 2520struct p2p_oper_class_map { 2521 enum hostapd_hw_mode mode; 2522 u8 op_class; 2523 u8 min_chan; 2524 u8 max_chan; 2525 u8 inc; 2526 enum { BW20, BW40PLUS, BW40MINUS } bw; 2527}; 2528 2529static struct p2p_oper_class_map op_class[] = { 2530 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 }, 2531 { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 }, 2532#if 0 /* Do not enable HT40 on 2 GHz for now */ 2533 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS }, 2534 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS }, 2535#endif 2536 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 }, 2537 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 }, 2538 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS }, 2539 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS }, 2540 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS }, 2541 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS }, 2542 { -1, 0, 0, 0, 0, BW20 } 2543}; 2544 2545 2546static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, 2547 struct hostapd_hw_modes *mode, 2548 u8 channel, u8 bw) 2549{ 2550 int flag; 2551 2552 if (!has_channel(wpa_s->global, mode, channel, &flag)) 2553 return -1; 2554 if (bw == BW40MINUS && 2555 (!(flag & HOSTAPD_CHAN_HT40MINUS) || 2556 !has_channel(wpa_s->global, mode, channel - 4, NULL))) 2557 return 0; 2558 if (bw == BW40PLUS && 2559 (!(flag & HOSTAPD_CHAN_HT40PLUS) || 2560 !has_channel(wpa_s->global, mode, channel + 4, NULL))) 2561 return 0; 2562 return 1; 2563} 2564 2565 2566static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, 2567 struct p2p_channels *chan) 2568{ 2569 struct hostapd_hw_modes *mode; 2570 int cla, op; 2571 2572 if (wpa_s->hw.modes == NULL) { 2573 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " 2574 "of all supported channels; assume dualband " 2575 "support"); 2576 return wpas_p2p_default_channels(wpa_s, chan); 2577 } 2578 2579 cla = 0; 2580 2581 for (op = 0; op_class[op].op_class; op++) { 2582 struct p2p_oper_class_map *o = &op_class[op]; 2583 u8 ch; 2584 struct p2p_reg_class *reg = NULL; 2585 2586 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode); 2587 if (mode == NULL) 2588 continue; 2589 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { 2590 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1) 2591 continue; 2592 if (reg == NULL) { 2593 wpa_printf(MSG_DEBUG, "P2P: Add operating " 2594 "class %u", o->op_class); 2595 reg = &chan->reg_class[cla]; 2596 cla++; 2597 reg->reg_class = o->op_class; 2598 } 2599 reg->channel[reg->channels] = ch; 2600 reg->channels++; 2601 } 2602 if (reg) { 2603 wpa_hexdump(MSG_DEBUG, "P2P: Channels", 2604 reg->channel, reg->channels); 2605 } 2606 } 2607 2608 chan->reg_classes = cla; 2609 2610 return 0; 2611} 2612 2613 2614int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, 2615 struct hostapd_hw_modes *mode, u8 channel) 2616{ 2617 int op, ret; 2618 2619 for (op = 0; op_class[op].op_class; op++) { 2620 struct p2p_oper_class_map *o = &op_class[op]; 2621 u8 ch; 2622 2623 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { 2624 if (o->mode != HOSTAPD_MODE_IEEE80211A || 2625 o->bw == BW20 || ch != channel) 2626 continue; 2627 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw); 2628 if (ret < 0) 2629 continue; 2630 else if (ret > 0) 2631 return (o->bw == BW40MINUS) ? -1 : 1; 2632 else 2633 return 0; 2634 } 2635 } 2636 return 0; 2637} 2638 2639 2640static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, 2641 size_t buf_len) 2642{ 2643 struct wpa_supplicant *wpa_s = ctx; 2644 2645 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 2646 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) 2647 break; 2648 } 2649 if (wpa_s == NULL) 2650 return -1; 2651 2652 return wpa_drv_get_noa(wpa_s, buf, buf_len); 2653} 2654 2655 2656static int wpas_go_connected(void *ctx, const u8 *dev_addr) 2657{ 2658 struct wpa_supplicant *wpa_s = ctx; 2659 2660 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 2661 struct wpa_ssid *ssid = wpa_s->current_ssid; 2662 if (ssid == NULL) 2663 continue; 2664 if (ssid->mode != WPAS_MODE_INFRA) 2665 continue; 2666 if (wpa_s->wpa_state != WPA_COMPLETED && 2667 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE) 2668 continue; 2669 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0) 2670 return 1; 2671 } 2672 2673 return 0; 2674} 2675 2676 2677/** 2678 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant 2679 * @global: Pointer to global data from wpa_supplicant_init() 2680 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 2681 * Returns: 0 on success, -1 on failure 2682 */ 2683int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) 2684{ 2685 struct p2p_config p2p; 2686 unsigned int r; 2687 int i; 2688 2689 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) 2690 return 0; 2691 2692 if (global->p2p) 2693 return 0; 2694 2695 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { 2696 struct p2p_params params; 2697 2698 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management"); 2699 os_memset(¶ms, 0, sizeof(params)); 2700 params.dev_name = wpa_s->conf->device_name; 2701 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type, 2702 WPS_DEV_TYPE_LEN); 2703 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types; 2704 os_memcpy(params.sec_dev_type, 2705 wpa_s->conf->sec_device_type, 2706 params.num_sec_dev_types * WPS_DEV_TYPE_LEN); 2707 2708 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0) 2709 return -1; 2710 2711 return 0; 2712 } 2713 2714 os_memset(&p2p, 0, sizeof(p2p)); 2715 p2p.msg_ctx = wpa_s; 2716 p2p.cb_ctx = wpa_s; 2717 p2p.p2p_scan = wpas_p2p_scan; 2718 p2p.send_action = wpas_send_action; 2719 p2p.send_action_done = wpas_send_action_done; 2720 p2p.go_neg_completed = wpas_go_neg_completed; 2721 p2p.go_neg_req_rx = wpas_go_neg_req_rx; 2722 p2p.dev_found = wpas_dev_found; 2723 p2p.dev_lost = wpas_dev_lost; 2724 p2p.start_listen = wpas_start_listen; 2725 p2p.stop_listen = wpas_stop_listen; 2726 p2p.send_probe_resp = wpas_send_probe_resp; 2727 p2p.sd_request = wpas_sd_request; 2728 p2p.sd_response = wpas_sd_response; 2729 p2p.prov_disc_req = wpas_prov_disc_req; 2730 p2p.prov_disc_resp = wpas_prov_disc_resp; 2731 p2p.prov_disc_fail = wpas_prov_disc_fail; 2732 p2p.invitation_process = wpas_invitation_process; 2733 p2p.invitation_received = wpas_invitation_received; 2734 p2p.invitation_result = wpas_invitation_result; 2735 p2p.get_noa = wpas_get_noa; 2736 p2p.go_connected = wpas_go_connected; 2737 2738 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN); 2739 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN); 2740 p2p.dev_name = wpa_s->conf->device_name; 2741 p2p.manufacturer = wpa_s->conf->manufacturer; 2742 p2p.model_name = wpa_s->conf->model_name; 2743 p2p.model_number = wpa_s->conf->model_number; 2744 p2p.serial_number = wpa_s->conf->serial_number; 2745 if (wpa_s->wps) { 2746 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); 2747 p2p.config_methods = wpa_s->wps->config_methods; 2748 } 2749 2750 if (wpa_s->conf->p2p_listen_reg_class && 2751 wpa_s->conf->p2p_listen_channel) { 2752 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; 2753 p2p.channel = wpa_s->conf->p2p_listen_channel; 2754 } else { 2755 p2p.reg_class = 81; 2756 /* 2757 * Pick one of the social channels randomly as the listen 2758 * channel. 2759 */ 2760 os_get_random((u8 *) &r, sizeof(r)); 2761 p2p.channel = 1 + (r % 3) * 5; 2762 } 2763 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel); 2764 2765 if (wpa_s->conf->p2p_oper_reg_class && 2766 wpa_s->conf->p2p_oper_channel) { 2767 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; 2768 p2p.op_channel = wpa_s->conf->p2p_oper_channel; 2769 p2p.cfg_op_channel = 1; 2770 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " 2771 "%d:%d", p2p.op_reg_class, p2p.op_channel); 2772 2773 } else { 2774 p2p.op_reg_class = 81; 2775 /* 2776 * Use random operation channel from (1, 6, 11) if no other 2777 * preference is indicated. 2778 */ 2779 os_get_random((u8 *) &r, sizeof(r)); 2780 p2p.op_channel = 1 + (r % 3) * 5; 2781 p2p.cfg_op_channel = 0; 2782 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " 2783 "%d:%d", p2p.op_reg_class, p2p.op_channel); 2784 } 2785 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { 2786 os_memcpy(p2p.country, wpa_s->conf->country, 2); 2787 p2p.country[2] = 0x04; 2788 } else 2789 os_memcpy(p2p.country, "XX\x04", 3); 2790 2791 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) { 2792 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported " 2793 "channel list"); 2794 return -1; 2795 } 2796 2797 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, 2798 WPS_DEV_TYPE_LEN); 2799 2800 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; 2801 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, 2802 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); 2803 2804 p2p.concurrent_operations = !!(wpa_s->drv_flags & 2805 WPA_DRIVER_FLAGS_P2P_CONCURRENT); 2806 2807 p2p.max_peers = 100; 2808 2809 if (wpa_s->conf->p2p_ssid_postfix) { 2810 p2p.ssid_postfix_len = 2811 os_strlen(wpa_s->conf->p2p_ssid_postfix); 2812 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) 2813 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); 2814 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, 2815 p2p.ssid_postfix_len); 2816 } 2817 2818 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; 2819 2820#ifdef ANDROID_P2P 2821 if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) 2822 p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT; 2823 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT) 2824 p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT; 2825#endif 2826 2827 global->p2p = p2p_init(&p2p); 2828 if (global->p2p == NULL) 2829 return -1; 2830 global->p2p_init_wpa_s = wpa_s; 2831 2832 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { 2833 if (wpa_s->conf->wps_vendor_ext[i] == NULL) 2834 continue; 2835 p2p_add_wps_vendor_extension( 2836 global->p2p, wpa_s->conf->wps_vendor_ext[i]); 2837 } 2838 2839 return 0; 2840} 2841 2842 2843/** 2844 * wpas_p2p_deinit - Deinitialize per-interface P2P data 2845 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 2846 * 2847 * This function deinitialize per-interface P2P data. 2848 */ 2849void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) 2850{ 2851 if (wpa_s->driver && wpa_s->drv_priv) 2852 wpa_drv_probe_req_report(wpa_s, 0); 2853 2854 if (wpa_s->go_params) { 2855 /* Clear any stored provisioning info */ 2856 p2p_clear_provisioning_info( 2857 wpa_s->global->p2p, 2858 wpa_s->go_params->peer_device_addr); 2859 } 2860 2861 os_free(wpa_s->go_params); 2862 wpa_s->go_params = NULL; 2863 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); 2864 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 2865 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL); 2866 wpa_s->p2p_long_listen = 0; 2867 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 2868 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); 2869 wpas_p2p_remove_pending_group_interface(wpa_s); 2870 2871 /* TODO: remove group interface from the driver if this wpa_s instance 2872 * is on top of a P2P group interface */ 2873} 2874 2875 2876/** 2877 * wpas_p2p_deinit_global - Deinitialize global P2P module 2878 * @global: Pointer to global data from wpa_supplicant_init() 2879 * 2880 * This function deinitializes the global (per device) P2P module. 2881 */ 2882void wpas_p2p_deinit_global(struct wpa_global *global) 2883{ 2884 struct wpa_supplicant *wpa_s, *tmp; 2885 2886 wpa_s = global->ifaces; 2887 if (wpa_s) 2888 wpas_p2p_service_flush(wpa_s); 2889 2890 if (global->p2p == NULL) 2891 return; 2892 2893 /* Remove remaining P2P group interfaces */ 2894 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) 2895 wpa_s = wpa_s->next; 2896 while (wpa_s) { 2897 tmp = global->ifaces; 2898 while (tmp && 2899 (tmp == wpa_s || 2900 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { 2901 tmp = tmp->next; 2902 } 2903 if (tmp == NULL) 2904 break; 2905 /* Disconnect from the P2P group and deinit the interface */ 2906 wpas_p2p_disconnect(tmp); 2907 } 2908 2909 /* 2910 * Deinit GO data on any possibly remaining interface (if main 2911 * interface is used as GO). 2912 */ 2913 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 2914 if (wpa_s->ap_iface) 2915 wpas_p2p_group_deinit(wpa_s); 2916 } 2917 2918 p2p_deinit(global->p2p); 2919 global->p2p = NULL; 2920 global->p2p_init_wpa_s = NULL; 2921} 2922 2923 2924static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) 2925{ 2926 if (wpa_s->drv_flags & 2927 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | 2928 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) 2929 return 1; /* P2P group requires a new interface in every case 2930 */ 2931 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) 2932 return 0; /* driver does not support concurrent operations */ 2933 if (wpa_s->global->ifaces->next) 2934 return 1; /* more that one interface already in use */ 2935 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) 2936 return 1; /* this interface is already in use */ 2937 return 0; 2938} 2939 2940 2941static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, 2942 const u8 *peer_addr, 2943 enum p2p_wps_method wps_method, 2944 int go_intent, const u8 *own_interface_addr, 2945 unsigned int force_freq, int persistent_group, 2946 struct wpa_ssid *ssid) 2947{ 2948 if (persistent_group && wpa_s->conf->persistent_reconnect) 2949 persistent_group = 2; 2950 2951 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { 2952 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method, 2953 go_intent, own_interface_addr, 2954 force_freq, persistent_group); 2955 } 2956 2957 /* 2958 * Increase GO config timeout if HT40 is used since it takes some time 2959 * to scan channels for coex purposes before the BSS can be started. 2960 */ 2961 p2p_set_config_timeout(wpa_s->global->p2p, 2962 wpa_s->p2p_go_ht40 ? 255 : 100, 20); 2963 2964 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, 2965 go_intent, own_interface_addr, force_freq, 2966 persistent_group, ssid ? ssid->ssid : NULL, 2967 ssid ? ssid->ssid_len : 0, 2968 wpa_s->p2p_pd_before_go_neg); 2969} 2970 2971 2972static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, 2973 const u8 *peer_addr, 2974 enum p2p_wps_method wps_method, 2975 int go_intent, const u8 *own_interface_addr, 2976 unsigned int force_freq, int persistent_group, 2977 struct wpa_ssid *ssid) 2978{ 2979 if (persistent_group && wpa_s->conf->persistent_reconnect) 2980 persistent_group = 2; 2981 2982 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 2983 return -1; 2984 2985 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, 2986 go_intent, own_interface_addr, force_freq, 2987 persistent_group, ssid ? ssid->ssid : NULL, 2988 ssid ? ssid->ssid_len : 0); 2989} 2990 2991 2992static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) 2993{ 2994 wpa_s->p2p_join_scan_count++; 2995 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", 2996 wpa_s->p2p_join_scan_count); 2997 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { 2998 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR 2999 " for join operationg - stop join attempt", 3000 MAC2STR(wpa_s->pending_join_iface_addr)); 3001 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 3002 if (wpa_s->p2p_auto_pd) { 3003 wpa_s->p2p_auto_pd = 0; 3004 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE 3005 " p2p_dev_addr=" MACSTR " status=N/A", 3006 MAC2STR(wpa_s->pending_join_dev_addr)); 3007 return; 3008 } 3009 wpa_msg(wpa_s->parent, MSG_INFO, 3010 P2P_EVENT_GROUP_FORMATION_FAILURE); 3011 } 3012} 3013 3014 3015static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx) 3016{ 3017 struct wpa_supplicant *wpa_s = eloop_ctx; 3018 if (!wpa_s->pending_pd_before_join) 3019 return; 3020 /* 3021 * Provision Discovery Response may have been lost - try to connect 3022 * anyway since we do not need any information from this PD. 3023 */ 3024 wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - " 3025 "try to connect anyway"); 3026 wpas_p2p_join_start(wpa_s); 3027} 3028 3029 3030static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) 3031{ 3032 struct wpa_supplicant *iface; 3033 int shared_freq; 3034 u8 bssid[ETH_ALEN]; 3035 3036 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) 3037 return 0; 3038 3039 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 3040 if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s) 3041 continue; 3042 if (iface->current_ssid == NULL || iface->assoc_freq == 0) 3043 continue; 3044 if (iface->current_ssid->mode == WPAS_MODE_AP || 3045 iface->current_ssid->mode == WPAS_MODE_P2P_GO) 3046 shared_freq = iface->current_ssid->frequency; 3047 else if (wpa_drv_get_bssid(iface, bssid) == 0) 3048 shared_freq = iface->assoc_freq; 3049 else 3050 shared_freq = 0; 3051 3052 if (shared_freq && freq != shared_freq) { 3053 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s " 3054 "connected on %d MHz - new connection on " 3055 "%d MHz", iface->ifname, shared_freq, freq); 3056 return 1; 3057 } 3058 } 3059 3060 shared_freq = wpa_drv_shared_freq(wpa_s); 3061 if (shared_freq > 0 && shared_freq != freq) { 3062 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared " 3063 "virtual interface connected on %d MHz - new " 3064 "connection on %d MHz", shared_freq, freq); 3065 return 1; 3066 } 3067 3068 return 0; 3069} 3070 3071 3072static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, 3073 const u8 *peer_dev_addr) 3074{ 3075 struct wpa_bss *bss; 3076 int updated; 3077 3078 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); 3079 if (bss == NULL) 3080 return -1; 3081 if (bss->last_update_idx < wpa_s->bss_update_idx) { 3082 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " 3083 "last scan"); 3084 return 0; 3085 } 3086 3087 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update); 3088 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " 3089 "%ld.%06ld (%supdated in last scan)", 3090 bss->last_update.sec, bss->last_update.usec, 3091 updated ? "": "not "); 3092 3093 return updated; 3094} 3095 3096 3097static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, 3098 struct wpa_scan_results *scan_res) 3099{ 3100 struct wpa_bss *bss; 3101 int freq; 3102 u8 iface_addr[ETH_ALEN]; 3103 3104 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 3105 3106 if (wpa_s->global->p2p_disabled) 3107 return; 3108 3109 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", 3110 scan_res ? (int) scan_res->num : -1, 3111 wpa_s->p2p_auto_join ? "auto_" : ""); 3112 3113 if (scan_res) 3114 wpas_p2p_scan_res_handler(wpa_s, scan_res); 3115 3116 if (wpa_s->p2p_auto_pd) { 3117 int join = wpas_p2p_peer_go(wpa_s, 3118 wpa_s->pending_join_dev_addr); 3119 if (join == 0 && 3120 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { 3121 wpa_s->auto_pd_scan_retry++; 3122 bss = wpa_bss_get_bssid(wpa_s, 3123 wpa_s->pending_join_dev_addr); 3124 if (bss) { 3125 freq = bss->freq; 3126 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " 3127 "the peer " MACSTR " at %d MHz", 3128 wpa_s->auto_pd_scan_retry, 3129 MAC2STR(wpa_s-> 3130 pending_join_dev_addr), 3131 freq); 3132 wpas_p2p_join_scan_req(wpa_s, freq); 3133 return; 3134 } 3135 } 3136 3137 if (join < 0) 3138 join = 0; 3139 3140 wpa_s->p2p_auto_pd = 0; 3141 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; 3142 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", 3143 MAC2STR(wpa_s->pending_join_dev_addr), join); 3144 if (p2p_prov_disc_req(wpa_s->global->p2p, 3145 wpa_s->pending_join_dev_addr, 3146 wpa_s->pending_pd_config_methods, join, 3147 0) < 0) { 3148 wpa_s->p2p_auto_pd = 0; 3149 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE 3150 " p2p_dev_addr=" MACSTR " status=N/A", 3151 MAC2STR(wpa_s->pending_join_dev_addr)); 3152 } 3153 return; 3154 } 3155 3156 if (wpa_s->p2p_auto_join) { 3157 int join = wpas_p2p_peer_go(wpa_s, 3158 wpa_s->pending_join_dev_addr); 3159 if (join < 0) { 3160 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " 3161 "running a GO -> use GO Negotiation"); 3162 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, 3163 wpa_s->p2p_pin, wpa_s->p2p_wps_method, 3164 wpa_s->p2p_persistent_group, 0, 0, 0, 3165 wpa_s->p2p_go_intent, 3166 wpa_s->p2p_connect_freq, 3167 wpa_s->p2p_persistent_id, 3168 wpa_s->p2p_pd_before_go_neg, 3169 wpa_s->p2p_go_ht40); 3170 return; 3171 } 3172 3173 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " 3174 "try to join the group", join ? "" : 3175 " in older scan"); 3176 if (!join) 3177 wpa_s->p2p_fallback_to_go_neg = 1; 3178 } 3179 3180 freq = p2p_get_oper_freq(wpa_s->global->p2p, 3181 wpa_s->pending_join_iface_addr); 3182 if (freq < 0 && 3183 p2p_get_interface_addr(wpa_s->global->p2p, 3184 wpa_s->pending_join_dev_addr, 3185 iface_addr) == 0 && 3186 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0) 3187 { 3188 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " 3189 "address for join from " MACSTR " to " MACSTR 3190 " based on newly discovered P2P peer entry", 3191 MAC2STR(wpa_s->pending_join_iface_addr), 3192 MAC2STR(iface_addr)); 3193 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, 3194 ETH_ALEN); 3195 3196 freq = p2p_get_oper_freq(wpa_s->global->p2p, 3197 wpa_s->pending_join_iface_addr); 3198 } 3199 if (freq >= 0) { 3200 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " 3201 "from P2P peer table: %d MHz", freq); 3202 } 3203 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr); 3204 if (bss) { 3205 freq = bss->freq; 3206 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " 3207 "from BSS table: %d MHz", freq); 3208 } 3209 if (freq > 0) { 3210 u16 method; 3211 3212 if (wpas_check_freq_conflict(wpa_s, freq) > 0) { 3213 wpa_msg(wpa_s->parent, MSG_INFO, 3214 P2P_EVENT_GROUP_FORMATION_FAILURE 3215 "reason=FREQ_CONFLICT"); 3216 return; 3217 } 3218 3219 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " 3220 "prior to joining an existing group (GO " MACSTR 3221 " freq=%u MHz)", 3222 MAC2STR(wpa_s->pending_join_dev_addr), freq); 3223 wpa_s->pending_pd_before_join = 1; 3224 3225 switch (wpa_s->pending_join_wps_method) { 3226 case WPS_PIN_DISPLAY: 3227 method = WPS_CONFIG_KEYPAD; 3228 break; 3229 case WPS_PIN_KEYPAD: 3230 method = WPS_CONFIG_DISPLAY; 3231 break; 3232 case WPS_PBC: 3233 method = WPS_CONFIG_PUSHBUTTON; 3234 break; 3235 default: 3236 method = 0; 3237 break; 3238 } 3239 3240 if ((p2p_get_provisioning_info(wpa_s->global->p2p, 3241 wpa_s->pending_join_dev_addr) == 3242 method)) { 3243 /* 3244 * We have already performed provision discovery for 3245 * joining the group. Proceed directly to join 3246 * operation without duplicated provision discovery. */ 3247 wpa_printf(MSG_DEBUG, "P2P: Provision discovery " 3248 "with " MACSTR " already done - proceed to " 3249 "join", 3250 MAC2STR(wpa_s->pending_join_dev_addr)); 3251 wpa_s->pending_pd_before_join = 0; 3252 goto start; 3253 } 3254 3255 if (p2p_prov_disc_req(wpa_s->global->p2p, 3256 wpa_s->pending_join_dev_addr, method, 1, 3257 freq) < 0) { 3258 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " 3259 "Discovery Request before joining an " 3260 "existing group"); 3261 wpa_s->pending_pd_before_join = 0; 3262 goto start; 3263 } 3264 3265 /* 3266 * Actual join operation will be started from the Action frame 3267 * TX status callback (if no ACK is received) or when the 3268 * Provision Discovery Response is received. Use a short 3269 * timeout as a backup mechanism should the Provision Discovery 3270 * Response be lost for any reason. 3271 */ 3272 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, 3273 NULL); 3274 eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout, 3275 wpa_s, NULL); 3276 return; 3277 } 3278 3279 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); 3280 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 3281 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); 3282 wpas_p2p_check_join_scan_limit(wpa_s); 3283 return; 3284 3285start: 3286 /* Start join operation immediately */ 3287 wpas_p2p_join_start(wpa_s); 3288} 3289 3290 3291static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq) 3292{ 3293 int ret; 3294 struct wpa_driver_scan_params params; 3295 struct wpabuf *wps_ie, *ies; 3296 size_t ielen; 3297 int freqs[2] = { 0, 0 }; 3298#ifdef ANDROID_P2P 3299 int oper_freq; 3300 3301 /* If freq is not provided, check the operating freq of the GO and do a 3302 * a directed scan to save time 3303 */ 3304 if(!freq) { 3305 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, 3306 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq; 3307 } 3308#endif 3309 os_memset(¶ms, 0, sizeof(params)); 3310 3311 /* P2P Wildcard SSID */ 3312 params.num_ssids = 1; 3313 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; 3314 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; 3315 3316 wpa_s->wps->dev.p2p = 1; 3317 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, 3318 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, 3319 NULL); 3320 if (wps_ie == NULL) { 3321 wpas_p2p_scan_res_join(wpa_s, NULL); 3322 return; 3323 } 3324 3325 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); 3326 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); 3327 if (ies == NULL) { 3328 wpabuf_free(wps_ie); 3329 wpas_p2p_scan_res_join(wpa_s, NULL); 3330 return; 3331 } 3332 wpabuf_put_buf(ies, wps_ie); 3333 wpabuf_free(wps_ie); 3334 3335 p2p_scan_ie(wpa_s->global->p2p, ies, NULL); 3336 3337 params.p2p_probe = 1; 3338 params.extra_ies = wpabuf_head(ies); 3339 params.extra_ies_len = wpabuf_len(ies); 3340 if (freq > 0) { 3341 freqs[0] = freq; 3342 params.freqs = freqs; 3343 } 3344 3345 /* 3346 * Run a scan to update BSS table and start Provision Discovery once 3347 * the new scan results become available. 3348 */ 3349 ret = wpa_drv_scan(wpa_s, ¶ms); 3350 if (!ret) 3351 wpa_s->scan_res_handler = wpas_p2p_scan_res_join; 3352 3353 wpabuf_free(ies); 3354 3355 if (ret) { 3356 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " 3357 "try again later"); 3358 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 3359 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); 3360 wpas_p2p_check_join_scan_limit(wpa_s); 3361 } 3362} 3363 3364 3365static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) 3366{ 3367 struct wpa_supplicant *wpa_s = eloop_ctx; 3368 wpas_p2p_join_scan_req(wpa_s, 0); 3369} 3370 3371 3372static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, 3373 const u8 *dev_addr, enum p2p_wps_method wps_method, 3374 int auto_join) 3375{ 3376 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " 3377 MACSTR " dev " MACSTR ")%s", 3378 MAC2STR(iface_addr), MAC2STR(dev_addr), 3379 auto_join ? " (auto_join)" : ""); 3380 3381 wpa_s->p2p_auto_pd = 0; 3382 wpa_s->p2p_auto_join = !!auto_join; 3383 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); 3384 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); 3385 wpa_s->pending_join_wps_method = wps_method; 3386 3387 /* Make sure we are not running find during connection establishment */ 3388 wpas_p2p_stop_find(wpa_s); 3389 3390 wpa_s->p2p_join_scan_count = 0; 3391 wpas_p2p_join_scan(wpa_s, NULL); 3392 return 0; 3393} 3394 3395 3396static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s) 3397{ 3398 struct wpa_supplicant *group; 3399 struct p2p_go_neg_results res; 3400 struct wpa_bss *bss; 3401 3402 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL); 3403 group = wpas_p2p_get_group_iface(wpa_s, 0, 0); 3404 if (group == NULL) 3405 return -1; 3406 if (group != wpa_s) { 3407 os_memcpy(group->p2p_pin, wpa_s->p2p_pin, 3408 sizeof(group->p2p_pin)); 3409 group->p2p_wps_method = wpa_s->p2p_wps_method; 3410 } 3411 3412 group->p2p_in_provisioning = 1; 3413 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg; 3414 3415 os_memset(&res, 0, sizeof(res)); 3416 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, 3417 ETH_ALEN); 3418 res.wps_method = wpa_s->pending_join_wps_method; 3419 bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr); 3420 if (bss) { 3421 res.freq = bss->freq; 3422 res.ssid_len = bss->ssid_len; 3423 os_memcpy(res.ssid, bss->ssid, bss->ssid_len); 3424 } 3425 3426 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { 3427 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " 3428 "starting client"); 3429 wpa_drv_cancel_remain_on_channel(wpa_s); 3430 wpa_s->off_channel_freq = 0; 3431 wpa_s->roc_waiting_drv_freq = 0; 3432 } 3433 wpas_start_wps_enrollee(group, &res); 3434 3435 /* 3436 * Allow a longer timeout for join-a-running-group than normal 15 3437 * second group formation timeout since the GO may not have authorized 3438 * our connection yet. 3439 */ 3440 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); 3441 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, 3442 wpa_s, NULL); 3443 3444 return 0; 3445} 3446 3447 3448/** 3449 * wpas_p2p_connect - Request P2P Group Formation to be started 3450 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 3451 * @peer_addr: Address of the peer P2P Device 3452 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode 3453 * @persistent_group: Whether to create a persistent group 3454 * @auto_join: Whether to select join vs. GO Negotiation automatically 3455 * @join: Whether to join an existing group (as a client) instead of starting 3456 * Group Owner negotiation; @peer_addr is BSSID in that case 3457 * @auth: Whether to only authorize the connection instead of doing that and 3458 * initiating Group Owner negotiation 3459 * @go_intent: GO Intent or -1 to use default 3460 * @freq: Frequency for the group or 0 for auto-selection 3461 * @persistent_id: Persistent group credentials to use for forcing GO 3462 * parameters or -1 to generate new values (SSID/passphrase) 3463 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an 3464 * interoperability workaround when initiating group formation 3465 * @ht40: Start GO with 40 MHz channel width 3466 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified 3467 * failure, -2 on failure due to channel not currently available, 3468 * -3 if forced channel is not supported 3469 */ 3470int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 3471 const char *pin, enum p2p_wps_method wps_method, 3472 int persistent_group, int auto_join, int join, int auth, 3473 int go_intent, int freq, int persistent_id, int pd, 3474 int ht40) 3475{ 3476 int force_freq = 0, oper_freq = 0; 3477 u8 bssid[ETH_ALEN]; 3478 int ret = 0; 3479 enum wpa_driver_if_type iftype; 3480 const u8 *if_addr; 3481 struct wpa_ssid *ssid = NULL; 3482 3483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 3484 return -1; 3485 3486 if (persistent_id >= 0) { 3487 ssid = wpa_config_get_network(wpa_s->conf, persistent_id); 3488 if (ssid == NULL || ssid->disabled != 2 || 3489 ssid->mode != WPAS_MODE_P2P_GO) 3490 return -1; 3491 } 3492 3493 if (go_intent < 0) 3494 go_intent = wpa_s->conf->p2p_go_intent; 3495 3496 if (!auth) 3497 wpa_s->p2p_long_listen = 0; 3498 3499 wpa_s->p2p_wps_method = wps_method; 3500 wpa_s->p2p_persistent_group = !!persistent_group; 3501 wpa_s->p2p_persistent_id = persistent_id; 3502 wpa_s->p2p_go_intent = go_intent; 3503 wpa_s->p2p_connect_freq = freq; 3504 wpa_s->p2p_fallback_to_go_neg = 0; 3505 wpa_s->p2p_pd_before_go_neg = !!pd; 3506 wpa_s->p2p_go_ht40 = !!ht40; 3507 3508 if (pin) 3509 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); 3510 else if (wps_method == WPS_PIN_DISPLAY) { 3511 ret = wps_generate_pin(); 3512 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d", 3513 ret); 3514 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", 3515 wpa_s->p2p_pin); 3516 } else 3517 wpa_s->p2p_pin[0] = '\0'; 3518 3519 if (join || auto_join) { 3520 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; 3521 if (auth) { 3522 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " 3523 "connect a running group from " MACSTR, 3524 MAC2STR(peer_addr)); 3525 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); 3526 return ret; 3527 } 3528 os_memcpy(dev_addr, peer_addr, ETH_ALEN); 3529 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, 3530 iface_addr) < 0) { 3531 os_memcpy(iface_addr, peer_addr, ETH_ALEN); 3532 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, 3533 dev_addr); 3534 } 3535 if (auto_join) { 3536 os_get_time(&wpa_s->p2p_auto_started); 3537 wpa_printf(MSG_DEBUG, "P2P: Auto join started at " 3538 "%ld.%06ld", 3539 wpa_s->p2p_auto_started.sec, 3540 wpa_s->p2p_auto_started.usec); 3541 } 3542 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, 3543 auto_join) < 0) 3544 return -1; 3545 return ret; 3546 } 3547 3548 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 && 3549 wpa_s->assoc_freq) 3550 oper_freq = wpa_s->assoc_freq; 3551 else { 3552 oper_freq = wpa_drv_shared_freq(wpa_s); 3553 if (oper_freq < 0) 3554 oper_freq = 0; 3555 } 3556 3557 if (freq > 0) { 3558 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { 3559 wpa_printf(MSG_DEBUG, "P2P: The forced channel " 3560 "(%u MHz) is not supported for P2P uses", 3561 freq); 3562 return -3; 3563 } 3564 3565 if (oper_freq > 0 && freq != oper_freq && 3566 !(wpa_s->drv_flags & 3567 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { 3568 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group " 3569 "on %u MHz while connected on another " 3570 "channel (%u MHz)", freq, oper_freq); 3571 return -2; 3572 } 3573 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " 3574 "requested channel (%u MHz)", freq); 3575 force_freq = freq; 3576 } else if (oper_freq > 0 && 3577 !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) { 3578 if (!(wpa_s->drv_flags & 3579 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { 3580 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group " 3581 "while connected on non-P2P supported " 3582 "channel (%u MHz)", oper_freq); 3583 return -2; 3584 } 3585 wpa_printf(MSG_DEBUG, "P2P: Current operating channel " 3586 "(%u MHz) not available for P2P - try to use " 3587 "another channel", oper_freq); 3588 force_freq = 0; 3589 } else if (oper_freq > 0) { 3590 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " 3591 "channel we are already using (%u MHz) on another " 3592 "interface", oper_freq); 3593 force_freq = oper_freq; 3594 } 3595 3596 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); 3597 3598 if (wpa_s->create_p2p_iface) { 3599 /* Prepare to add a new interface for the group */ 3600 iftype = WPA_IF_P2P_GROUP; 3601 if (go_intent == 15) 3602 iftype = WPA_IF_P2P_GO; 3603 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { 3604 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " 3605 "interface for the group"); 3606 return -1; 3607 } 3608 3609 if_addr = wpa_s->pending_interface_addr; 3610 } else 3611 if_addr = wpa_s->own_addr; 3612 3613 if (auth) { 3614 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, 3615 go_intent, if_addr, 3616 force_freq, persistent_group, ssid) < 3617 0) 3618 return -1; 3619 return ret; 3620 } 3621 3622 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, 3623 go_intent, if_addr, force_freq, 3624 persistent_group, ssid) < 0) { 3625 if (wpa_s->create_p2p_iface) 3626 wpas_p2p_remove_pending_group_interface(wpa_s); 3627 return -1; 3628 } 3629 return ret; 3630} 3631 3632 3633/** 3634 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start 3635 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 3636 * @freq: Frequency of the channel in MHz 3637 * @duration: Duration of the stay on the channel in milliseconds 3638 * 3639 * This callback is called when the driver indicates that it has started the 3640 * requested remain-on-channel duration. 3641 */ 3642void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, 3643 unsigned int freq, unsigned int duration) 3644{ 3645 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 3646 return; 3647 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { 3648 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, 3649 wpa_s->pending_listen_duration); 3650 wpa_s->pending_listen_freq = 0; 3651 } 3652} 3653 3654 3655static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, 3656 unsigned int timeout) 3657{ 3658 /* Limit maximum Listen state time based on driver limitation. */ 3659 if (timeout > wpa_s->max_remain_on_chan) 3660 timeout = wpa_s->max_remain_on_chan; 3661 3662 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 3663 return wpa_drv_p2p_listen(wpa_s, timeout); 3664 3665 return p2p_listen(wpa_s->global->p2p, timeout); 3666} 3667 3668 3669/** 3670 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout 3671 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 3672 * @freq: Frequency of the channel in MHz 3673 * 3674 * This callback is called when the driver indicates that a remain-on-channel 3675 * operation has been completed, i.e., the duration on the requested channel 3676 * has timed out. 3677 */ 3678void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, 3679 unsigned int freq) 3680{ 3681 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " 3682 "(p2p_long_listen=%d ms pending_action_tx=%p)", 3683 wpa_s->p2p_long_listen, wpa_s->pending_action_tx); 3684 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 3685 return; 3686 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) 3687 return; /* P2P module started a new operation */ 3688 if (wpa_s->pending_action_tx) 3689 return; 3690 if (wpa_s->p2p_long_listen > 0) 3691 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan; 3692 if (wpa_s->p2p_long_listen > 0) { 3693 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); 3694 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen); 3695 } 3696} 3697 3698 3699/** 3700 * wpas_p2p_group_remove - Remove a P2P group 3701 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 3702 * @ifname: Network interface name of the group interface or "*" to remove all 3703 * groups 3704 * Returns: 0 on success, -1 on failure 3705 * 3706 * This function is used to remove a P2P group. This can be used to disconnect 3707 * from a group in which the local end is a P2P Client or to end a P2P Group in 3708 * case the local end is the Group Owner. If a virtual network interface was 3709 * created for this group, that interface will be removed. Otherwise, only the 3710 * configured P2P group network will be removed from the interface. 3711 */ 3712int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) 3713{ 3714 struct wpa_global *global = wpa_s->global; 3715 3716 if (os_strcmp(ifname, "*") == 0) { 3717 struct wpa_supplicant *prev; 3718 wpa_s = global->ifaces; 3719 while (wpa_s) { 3720 prev = wpa_s; 3721 wpa_s = wpa_s->next; 3722 wpas_p2p_disconnect(prev); 3723 } 3724 return 0; 3725 } 3726 3727 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 3728 if (os_strcmp(wpa_s->ifname, ifname) == 0) 3729 break; 3730 } 3731 3732 return wpas_p2p_disconnect(wpa_s); 3733} 3734 3735 3736static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, 3737 struct p2p_go_neg_results *params, 3738 int freq, int ht40) 3739{ 3740 u8 bssid[ETH_ALEN]; 3741 int res; 3742 3743 os_memset(params, 0, sizeof(*params)); 3744 params->role_go = 1; 3745 params->ht40 = ht40; 3746 if (freq) { 3747 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced " 3748 "frequency %d MHz", freq); 3749 params->freq = freq; 3750 } else if (wpa_s->conf->p2p_oper_reg_class == 81 && 3751 wpa_s->conf->p2p_oper_channel >= 1 && 3752 wpa_s->conf->p2p_oper_channel <= 11) { 3753 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; 3754 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " 3755 "frequency %d MHz", params->freq); 3756 } else if (wpa_s->conf->p2p_oper_reg_class == 115 || 3757 wpa_s->conf->p2p_oper_reg_class == 124) { 3758 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; 3759 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " 3760 "frequency %d MHz", params->freq); 3761 } else if (wpa_s->conf->p2p_oper_channel == 0 && 3762 wpa_s->best_overall_freq > 0 && 3763 p2p_supported_freq(wpa_s->global->p2p, 3764 wpa_s->best_overall_freq)) { 3765 params->freq = wpa_s->best_overall_freq; 3766 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " 3767 "channel %d MHz", params->freq); 3768 } else if (wpa_s->conf->p2p_oper_channel == 0 && 3769 wpa_s->best_24_freq > 0 && 3770 p2p_supported_freq(wpa_s->global->p2p, 3771 wpa_s->best_24_freq)) { 3772 params->freq = wpa_s->best_24_freq; 3773 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " 3774 "channel %d MHz", params->freq); 3775 } else if (wpa_s->conf->p2p_oper_channel == 0 && 3776 wpa_s->best_5_freq > 0 && 3777 p2p_supported_freq(wpa_s->global->p2p, 3778 wpa_s->best_5_freq)) { 3779 params->freq = wpa_s->best_5_freq; 3780 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " 3781 "channel %d MHz", params->freq); 3782 } else { 3783 int chan; 3784 for (chan = 0; chan < 11; chan++) { 3785 params->freq = 2412 + chan * 5; 3786 if (!wpas_p2p_disallowed_freq(wpa_s->global, 3787 params->freq)) 3788 break; 3789 } 3790 if (chan == 11) { 3791 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel " 3792 "allowed"); 3793 return -1; 3794 } 3795 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference " 3796 "known)", params->freq); 3797 } 3798 3799 if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 && 3800 wpa_s->assoc_freq && !freq) { 3801 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are " 3802 "already using"); 3803 params->freq = wpa_s->assoc_freq; 3804 } 3805 3806 res = wpa_drv_shared_freq(wpa_s); 3807 if (res > 0 && !freq) { 3808 wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are " 3809 "already using on a shared interface"); 3810 params->freq = res; 3811 } else if (res > 0 && freq != res && 3812 !(wpa_s->drv_flags & 3813 WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) { 3814 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz " 3815 "while connected on another channel (%u MHz)", 3816 freq, res); 3817 return -1; 3818 } 3819 3820 return 0; 3821} 3822 3823 3824static struct wpa_supplicant * 3825wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, 3826 int go) 3827{ 3828 struct wpa_supplicant *group_wpa_s; 3829 3830 if (!wpas_p2p_create_iface(wpa_s)) 3831 return wpa_s; 3832 3833 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : 3834 WPA_IF_P2P_CLIENT) < 0) 3835 return NULL; 3836 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); 3837 if (group_wpa_s == NULL) { 3838 wpas_p2p_remove_pending_group_interface(wpa_s); 3839 return NULL; 3840 } 3841 3842 return group_wpa_s; 3843} 3844 3845 3846/** 3847 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner 3848 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 3849 * @persistent_group: Whether to create a persistent group 3850 * @freq: Frequency for the group or 0 to indicate no hardcoding 3851 * Returns: 0 on success, -1 on failure 3852 * 3853 * This function creates a new P2P group with the local end as the Group Owner, 3854 * i.e., without using Group Owner Negotiation. 3855 */ 3856int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group, 3857 int freq, int ht40) 3858{ 3859 struct p2p_go_neg_results params; 3860 unsigned int r; 3861 3862 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 3863 return -1; 3864 3865 /* Make sure we are not running find during connection establishment */ 3866 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); 3867 wpas_p2p_stop_find(wpa_s); 3868 3869 if (freq == 2) { 3870 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " 3871 "band"); 3872 if (wpa_s->best_24_freq > 0 && 3873 p2p_supported_freq(wpa_s->global->p2p, 3874 wpa_s->best_24_freq)) { 3875 freq = wpa_s->best_24_freq; 3876 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " 3877 "channel: %d MHz", freq); 3878 } else { 3879 os_get_random((u8 *) &r, sizeof(r)); 3880 freq = 2412 + (r % 3) * 25; 3881 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " 3882 "channel: %d MHz", freq); 3883 } 3884 } 3885 3886 if (freq == 5) { 3887 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " 3888 "band"); 3889 if (wpa_s->best_5_freq > 0 && 3890 p2p_supported_freq(wpa_s->global->p2p, 3891 wpa_s->best_5_freq)) { 3892 freq = wpa_s->best_5_freq; 3893 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " 3894 "channel: %d MHz", freq); 3895 } else { 3896 os_get_random((u8 *) &r, sizeof(r)); 3897 freq = 5180 + (r % 4) * 20; 3898 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { 3899 wpa_printf(MSG_DEBUG, "P2P: Could not select " 3900 "5 GHz channel for P2P group"); 3901 return -1; 3902 } 3903 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " 3904 "channel: %d MHz", freq); 3905 } 3906 } 3907 3908 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) { 3909 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " 3910 "(%u MHz) is not supported for P2P uses", 3911 freq); 3912 return -1; 3913 } 3914 3915 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40)) 3916 return -1; 3917 if (params.freq && 3918 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) { 3919 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO " 3920 "(%u MHz) is not supported for P2P uses", 3921 params.freq); 3922 return -1; 3923 } 3924 p2p_go_params(wpa_s->global->p2p, ¶ms); 3925 params.persistent_group = persistent_group; 3926 3927 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); 3928 if (wpa_s == NULL) 3929 return -1; 3930 wpas_start_wps_go(wpa_s, ¶ms, 0); 3931 3932 return 0; 3933} 3934 3935 3936static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, 3937 struct wpa_ssid *params, int addr_allocated) 3938{ 3939 struct wpa_ssid *ssid; 3940 3941 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); 3942 if (wpa_s == NULL) 3943 return -1; 3944 3945 wpa_supplicant_ap_deinit(wpa_s); 3946 3947 ssid = wpa_config_add_network(wpa_s->conf); 3948 if (ssid == NULL) 3949 return -1; 3950 wpa_config_set_network_defaults(ssid); 3951 ssid->temporary = 1; 3952 ssid->proto = WPA_PROTO_RSN; 3953 ssid->pairwise_cipher = WPA_CIPHER_CCMP; 3954 ssid->group_cipher = WPA_CIPHER_CCMP; 3955 ssid->key_mgmt = WPA_KEY_MGMT_PSK; 3956 ssid->ssid = os_malloc(params->ssid_len); 3957 if (ssid->ssid == NULL) { 3958 wpa_config_remove_network(wpa_s->conf, ssid->id); 3959 return -1; 3960 } 3961 os_memcpy(ssid->ssid, params->ssid, params->ssid_len); 3962 ssid->ssid_len = params->ssid_len; 3963 ssid->p2p_group = 1; 3964 ssid->export_keys = 1; 3965 if (params->psk_set) { 3966 os_memcpy(ssid->psk, params->psk, 32); 3967 ssid->psk_set = 1; 3968 } 3969 if (params->passphrase) 3970 ssid->passphrase = os_strdup(params->passphrase); 3971 3972 wpa_supplicant_select_network(wpa_s, ssid); 3973 3974 wpa_s->show_group_started = 1; 3975 3976 return 0; 3977} 3978 3979 3980int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, 3981 struct wpa_ssid *ssid, int addr_allocated, 3982 int freq, int ht40) 3983{ 3984 struct p2p_go_neg_results params; 3985 int go = 0; 3986 3987 if (ssid->disabled != 2 || ssid->ssid == NULL) 3988 return -1; 3989 3990 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && 3991 go == (ssid->mode == WPAS_MODE_P2P_GO)) { 3992 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " 3993 "already running"); 3994 return 0; 3995 } 3996 3997 /* Make sure we are not running find during connection establishment */ 3998 wpas_p2p_stop_find(wpa_s); 3999 4000 wpa_s->p2p_fallback_to_go_neg = 0; 4001 4002 if (ssid->mode == WPAS_MODE_INFRA) 4003 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated); 4004 4005 if (ssid->mode != WPAS_MODE_P2P_GO) 4006 return -1; 4007 4008 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40)) 4009 return -1; 4010 4011 params.role_go = 1; 4012 if (ssid->passphrase == NULL || 4013 os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { 4014 wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent " 4015 "group"); 4016 return -1; 4017 } 4018 os_strlcpy(params.passphrase, ssid->passphrase, 4019 sizeof(params.passphrase)); 4020 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); 4021 params.ssid_len = ssid->ssid_len; 4022 params.persistent_group = 1; 4023 4024 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); 4025 if (wpa_s == NULL) 4026 return -1; 4027 4028 wpas_start_wps_go(wpa_s, ¶ms, 0); 4029 4030 return 0; 4031} 4032 4033 4034static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, 4035 struct wpabuf *proberesp_ies) 4036{ 4037 struct wpa_supplicant *wpa_s = ctx; 4038 if (wpa_s->ap_iface) { 4039 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; 4040 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { 4041 wpabuf_free(beacon_ies); 4042 wpabuf_free(proberesp_ies); 4043 return; 4044 } 4045 if (beacon_ies) { 4046 wpabuf_free(hapd->p2p_beacon_ie); 4047 hapd->p2p_beacon_ie = beacon_ies; 4048 } 4049 wpabuf_free(hapd->p2p_probe_resp_ie); 4050 hapd->p2p_probe_resp_ie = proberesp_ies; 4051 } else { 4052 wpabuf_free(beacon_ies); 4053 wpabuf_free(proberesp_ies); 4054 } 4055 wpa_supplicant_ap_update_beacon(wpa_s); 4056} 4057 4058 4059static void wpas_p2p_idle_update(void *ctx, int idle) 4060{ 4061 struct wpa_supplicant *wpa_s = ctx; 4062 if (!wpa_s->ap_iface) 4063 return; 4064 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); 4065 if (idle) 4066 wpas_p2p_set_group_idle_timeout(wpa_s); 4067 else 4068 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); 4069} 4070 4071 4072struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, 4073 struct wpa_ssid *ssid) 4074{ 4075 struct p2p_group *group; 4076 struct p2p_group_config *cfg; 4077 4078 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4079 return NULL; 4080 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4081 return NULL; 4082 4083 cfg = os_zalloc(sizeof(*cfg)); 4084 if (cfg == NULL) 4085 return NULL; 4086 4087 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) 4088 cfg->persistent_group = 2; 4089 else if (ssid->p2p_persistent_group) 4090 cfg->persistent_group = 1; 4091 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); 4092 if (wpa_s->max_stations && 4093 wpa_s->max_stations < wpa_s->conf->max_num_sta) 4094 cfg->max_clients = wpa_s->max_stations; 4095 else 4096 cfg->max_clients = wpa_s->conf->max_num_sta; 4097 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); 4098 cfg->ssid_len = ssid->ssid_len; 4099 cfg->cb_ctx = wpa_s; 4100 cfg->ie_update = wpas_p2p_ie_update; 4101 cfg->idle_update = wpas_p2p_idle_update; 4102 4103 group = p2p_group_init(wpa_s->global->p2p, cfg); 4104 if (group == NULL) 4105 os_free(cfg); 4106 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) 4107 p2p_group_notif_formation_done(group); 4108 wpa_s->p2p_group = group; 4109 return group; 4110} 4111 4112 4113void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 4114 int registrar) 4115{ 4116 struct wpa_ssid *ssid = wpa_s->current_ssid; 4117 4118 if (!wpa_s->p2p_in_provisioning) { 4119 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " 4120 "provisioning not in progress"); 4121 return; 4122 } 4123 4124 if (ssid && ssid->mode == WPAS_MODE_INFRA) { 4125 u8 go_dev_addr[ETH_ALEN]; 4126 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); 4127 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, 4128 ssid->ssid_len); 4129 /* Clear any stored provisioning info */ 4130 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); 4131 } 4132 4133 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent, 4134 NULL); 4135 if (ssid && ssid->mode == WPAS_MODE_INFRA) { 4136 /* 4137 * Use a separate timeout for initial data connection to 4138 * complete to allow the group to be removed automatically if 4139 * something goes wrong in this step before the P2P group idle 4140 * timeout mechanism is taken into use. 4141 */ 4142 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, 4143 wpas_p2p_group_formation_timeout, 4144 wpa_s->parent, NULL); 4145 } 4146 if (wpa_s->global->p2p) 4147 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); 4148 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4149 wpa_drv_wps_success_cb(wpa_s, peer_addr); 4150 wpas_group_formation_completed(wpa_s, 1); 4151} 4152 4153 4154void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, 4155 struct wps_event_fail *fail) 4156{ 4157 if (!wpa_s->p2p_in_provisioning) { 4158 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " 4159 "provisioning not in progress"); 4160 return; 4161 } 4162 4163 if (wpa_s->go_params) { 4164 p2p_clear_provisioning_info( 4165 wpa_s->global->p2p, 4166 wpa_s->go_params->peer_device_addr); 4167 } 4168 4169 wpas_notify_p2p_wps_failed(wpa_s, fail); 4170} 4171 4172 4173int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 4174 const char *config_method, 4175 enum wpas_p2p_prov_disc_use use) 4176{ 4177 u16 config_methods; 4178 4179 wpa_s->p2p_fallback_to_go_neg = 0; 4180 wpa_s->pending_pd_use = NORMAL_PD; 4181 if (os_strncmp(config_method, "display", 7) == 0) 4182 config_methods = WPS_CONFIG_DISPLAY; 4183 else if (os_strncmp(config_method, "keypad", 6) == 0) 4184 config_methods = WPS_CONFIG_KEYPAD; 4185 else if (os_strncmp(config_method, "pbc", 3) == 0 || 4186 os_strncmp(config_method, "pushbutton", 10) == 0) 4187 config_methods = WPS_CONFIG_PUSHBUTTON; 4188 else { 4189 wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); 4190 return -1; 4191 } 4192 4193 if (use == WPAS_P2P_PD_AUTO) { 4194 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); 4195 wpa_s->pending_pd_config_methods = config_methods; 4196 wpa_s->p2p_auto_pd = 1; 4197 wpa_s->p2p_auto_join = 0; 4198 wpa_s->pending_pd_before_join = 0; 4199 wpa_s->auto_pd_scan_retry = 0; 4200 wpas_p2p_stop_find(wpa_s); 4201 wpa_s->p2p_join_scan_count = 0; 4202 os_get_time(&wpa_s->p2p_auto_started); 4203 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", 4204 wpa_s->p2p_auto_started.sec, 4205 wpa_s->p2p_auto_started.usec); 4206 wpas_p2p_join_scan(wpa_s, NULL); 4207 return 0; 4208 } 4209 4210 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { 4211 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr, 4212 config_methods, 4213 use == WPAS_P2P_PD_FOR_JOIN); 4214 } 4215 4216 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) 4217 return -1; 4218 4219 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, 4220 config_methods, use == WPAS_P2P_PD_FOR_JOIN, 4221 0); 4222} 4223 4224 4225int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, 4226 char *end) 4227{ 4228 return p2p_scan_result_text(ies, ies_len, buf, end); 4229} 4230 4231 4232static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) 4233{ 4234 if (!wpa_s->pending_action_tx) 4235 return; 4236 4237 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " 4238 "operation request"); 4239 wpabuf_free(wpa_s->pending_action_tx); 4240 wpa_s->pending_action_tx = NULL; 4241} 4242 4243 4244int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, 4245 enum p2p_discovery_type type, 4246 unsigned int num_req_dev_types, const u8 *req_dev_types, 4247 const u8 *dev_id, unsigned int search_delay) 4248{ 4249 wpas_p2p_clear_pending_action_tx(wpa_s); 4250 wpa_s->p2p_long_listen = 0; 4251 4252 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4253 return wpa_drv_p2p_find(wpa_s, timeout, type); 4254 4255 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || 4256 wpa_s->p2p_in_provisioning) 4257 return -1; 4258 4259 wpa_supplicant_cancel_sched_scan(wpa_s); 4260 4261 return p2p_find(wpa_s->global->p2p, timeout, type, 4262 num_req_dev_types, req_dev_types, dev_id, 4263 search_delay); 4264} 4265 4266 4267void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) 4268{ 4269 wpas_p2p_clear_pending_action_tx(wpa_s); 4270 wpa_s->p2p_long_listen = 0; 4271 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 4272 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 4273 wpa_s->p2p_cb_on_scan_complete = 0; 4274 4275 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) { 4276 wpa_drv_p2p_stop_find(wpa_s); 4277 return; 4278 } 4279 4280 if (wpa_s->global->p2p) 4281 p2p_stop_find(wpa_s->global->p2p); 4282 4283 wpas_p2p_remove_pending_group_interface(wpa_s); 4284} 4285 4286 4287static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) 4288{ 4289 struct wpa_supplicant *wpa_s = eloop_ctx; 4290 wpa_s->p2p_long_listen = 0; 4291} 4292 4293 4294int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) 4295{ 4296 int res; 4297 4298 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4299 return -1; 4300 4301 wpa_supplicant_cancel_sched_scan(wpa_s); 4302 wpas_p2p_clear_pending_action_tx(wpa_s); 4303 4304 if (timeout == 0) { 4305 /* 4306 * This is a request for unlimited Listen state. However, at 4307 * least for now, this is mapped to a Listen state for one 4308 * hour. 4309 */ 4310 timeout = 3600; 4311 } 4312 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 4313 wpa_s->p2p_long_listen = 0; 4314 4315 /* 4316 * Stop previous find/listen operation to avoid trying to request a new 4317 * remain-on-channel operation while the driver is still running the 4318 * previous one. 4319 */ 4320 if (wpa_s->global->p2p) 4321 p2p_stop_find(wpa_s->global->p2p); 4322 4323 res = wpas_p2p_listen_start(wpa_s, timeout * 1000); 4324 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { 4325 wpa_s->p2p_long_listen = timeout * 1000; 4326 eloop_register_timeout(timeout, 0, 4327 wpas_p2p_long_listen_timeout, 4328 wpa_s, NULL); 4329 } 4330 4331 return res; 4332} 4333 4334 4335int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, 4336 u8 *buf, size_t len, int p2p_group) 4337{ 4338 struct wpabuf *p2p_ie; 4339 int ret; 4340 4341 if (wpa_s->global->p2p_disabled) 4342 return -1; 4343 if (wpa_s->global->p2p == NULL) 4344 return -1; 4345 if (bss == NULL) 4346 return -1; 4347 4348 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); 4349 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, 4350 p2p_group, p2p_ie); 4351 wpabuf_free(p2p_ie); 4352 4353 return ret; 4354} 4355 4356 4357int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr, 4358 const u8 *dst, const u8 *bssid, 4359 const u8 *ie, size_t ie_len, int ssi_signal) 4360{ 4361 if (wpa_s->global->p2p_disabled) 4362 return 0; 4363 if (wpa_s->global->p2p == NULL) 4364 return 0; 4365 4366 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, 4367 ie, ie_len)) { 4368 case P2P_PREQ_NOT_P2P: 4369 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, 4370 ssi_signal); 4371 /* fall through */ 4372 case P2P_PREQ_MALFORMED: 4373 case P2P_PREQ_NOT_LISTEN: 4374 case P2P_PREQ_NOT_PROCESSED: 4375 default: /* make gcc happy */ 4376 return 0; 4377 case P2P_PREQ_PROCESSED: 4378 return 1; 4379 } 4380} 4381 4382 4383void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, 4384 const u8 *sa, const u8 *bssid, 4385 u8 category, const u8 *data, size_t len, int freq) 4386{ 4387 if (wpa_s->global->p2p_disabled) 4388 return; 4389 if (wpa_s->global->p2p == NULL) 4390 return; 4391 4392 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, 4393 freq); 4394} 4395 4396 4397void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) 4398{ 4399 if (wpa_s->global->p2p_disabled) 4400 return; 4401 if (wpa_s->global->p2p == NULL) 4402 return; 4403 4404 p2p_scan_ie(wpa_s->global->p2p, ies, NULL); 4405} 4406 4407 4408void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) 4409{ 4410 p2p_group_deinit(wpa_s->p2p_group); 4411 wpa_s->p2p_group = NULL; 4412 4413 wpa_s->ap_configured_cb = NULL; 4414 wpa_s->ap_configured_cb_ctx = NULL; 4415 wpa_s->ap_configured_cb_data = NULL; 4416 wpa_s->connect_without_scan = NULL; 4417} 4418 4419 4420int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) 4421{ 4422 wpa_s->p2p_long_listen = 0; 4423 4424 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4425 return wpa_drv_p2p_reject(wpa_s, addr); 4426 4427 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4428 return -1; 4429 4430 return p2p_reject(wpa_s->global->p2p, addr); 4431} 4432 4433 4434/* Invite to reinvoke a persistent group */ 4435int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 4436 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, 4437 int ht40) 4438{ 4439 enum p2p_invite_role role; 4440 u8 *bssid = NULL; 4441 4442 wpa_s->p2p_persistent_go_freq = freq; 4443 wpa_s->p2p_go_ht40 = !!ht40; 4444 if (ssid->mode == WPAS_MODE_P2P_GO) { 4445 role = P2P_INVITE_ROLE_GO; 4446 if (peer_addr == NULL) { 4447 wpa_printf(MSG_DEBUG, "P2P: Missing peer " 4448 "address in invitation command"); 4449 return -1; 4450 } 4451 if (wpas_p2p_create_iface(wpa_s)) { 4452 if (wpas_p2p_add_group_interface(wpa_s, 4453 WPA_IF_P2P_GO) < 0) { 4454 wpa_printf(MSG_ERROR, "P2P: Failed to " 4455 "allocate a new interface for the " 4456 "group"); 4457 return -1; 4458 } 4459 bssid = wpa_s->pending_interface_addr; 4460 } else 4461 bssid = wpa_s->own_addr; 4462 } else { 4463 role = P2P_INVITE_ROLE_CLIENT; 4464 peer_addr = ssid->bssid; 4465 } 4466 wpa_s->pending_invite_ssid_id = ssid->id; 4467 4468 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4469 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid, 4470 ssid->ssid, ssid->ssid_len, 4471 go_dev_addr, 1); 4472 4473 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4474 return -1; 4475 4476 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, 4477 ssid->ssid, ssid->ssid_len, freq, go_dev_addr, 1); 4478} 4479 4480 4481/* Invite to join an active group */ 4482int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, 4483 const u8 *peer_addr, const u8 *go_dev_addr) 4484{ 4485 struct wpa_global *global = wpa_s->global; 4486 enum p2p_invite_role role; 4487 u8 *bssid = NULL; 4488 struct wpa_ssid *ssid; 4489 int persistent; 4490 4491 wpa_s->p2p_persistent_go_freq = 0; 4492 wpa_s->p2p_go_ht40 = 0; 4493 4494 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 4495 if (os_strcmp(wpa_s->ifname, ifname) == 0) 4496 break; 4497 } 4498 if (wpa_s == NULL) { 4499 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); 4500 return -1; 4501 } 4502 4503 ssid = wpa_s->current_ssid; 4504 if (ssid == NULL) { 4505 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " 4506 "invitation"); 4507 return -1; 4508 } 4509 4510 persistent = ssid->p2p_persistent_group && 4511 wpas_p2p_get_persistent(wpa_s->parent, peer_addr, 4512 ssid->ssid, ssid->ssid_len); 4513 4514 if (ssid->mode == WPAS_MODE_P2P_GO) { 4515 role = P2P_INVITE_ROLE_ACTIVE_GO; 4516 bssid = wpa_s->own_addr; 4517 if (go_dev_addr == NULL) 4518 go_dev_addr = wpa_s->global->p2p_dev_addr; 4519 } else { 4520 role = P2P_INVITE_ROLE_CLIENT; 4521 if (wpa_s->wpa_state < WPA_ASSOCIATED) { 4522 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " 4523 "invite to current group"); 4524 return -1; 4525 } 4526 bssid = wpa_s->bssid; 4527 if (go_dev_addr == NULL && 4528 !is_zero_ether_addr(wpa_s->go_dev_addr)) 4529 go_dev_addr = wpa_s->go_dev_addr; 4530 } 4531 wpa_s->parent->pending_invite_ssid_id = -1; 4532 4533 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4534 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid, 4535 ssid->ssid, ssid->ssid_len, 4536 go_dev_addr, persistent); 4537 4538 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4539 return -1; 4540 4541 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, 4542 ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq, 4543 go_dev_addr, persistent); 4544} 4545 4546 4547void wpas_p2p_completed(struct wpa_supplicant *wpa_s) 4548{ 4549 struct wpa_ssid *ssid = wpa_s->current_ssid; 4550 const char *ssid_txt; 4551 u8 go_dev_addr[ETH_ALEN]; 4552 int network_id = -1; 4553 int persistent; 4554 int freq; 4555 4556 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { 4557 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 4558 wpa_s->parent, NULL); 4559 } 4560 4561 if (!wpa_s->show_group_started || !ssid) 4562 goto done; 4563 4564 wpa_s->show_group_started = 0; 4565 4566 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); 4567 os_memset(go_dev_addr, 0, ETH_ALEN); 4568 if (ssid->bssid_set) 4569 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); 4570 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, 4571 ssid->ssid_len); 4572 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); 4573 4574 if (wpa_s->global->p2p_group_formation == wpa_s) 4575 wpa_s->global->p2p_group_formation = NULL; 4576 4577 freq = wpa_s->current_bss ? wpa_s->current_bss->freq : 4578 (int) wpa_s->assoc_freq; 4579 if (ssid->passphrase == NULL && ssid->psk_set) { 4580 char psk[65]; 4581 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32); 4582 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED 4583 "%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr=" 4584 MACSTR "%s", 4585 wpa_s->ifname, ssid_txt, freq, psk, 4586 MAC2STR(go_dev_addr), 4587 persistent ? " [PERSISTENT]" : ""); 4588 } else { 4589 wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED 4590 "%s client ssid=\"%s\" freq=%d passphrase=\"%s\" " 4591 "go_dev_addr=" MACSTR "%s", 4592 wpa_s->ifname, ssid_txt, freq, 4593 ssid->passphrase ? ssid->passphrase : "", 4594 MAC2STR(go_dev_addr), 4595 persistent ? " [PERSISTENT]" : ""); 4596 } 4597 4598 if (persistent) 4599 network_id = wpas_p2p_store_persistent_group(wpa_s->parent, 4600 ssid, go_dev_addr); 4601 if (network_id < 0) 4602 network_id = ssid->id; 4603 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1); 4604 4605done: 4606 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled && 4607 wpa_s->global->p2p != NULL) { 4608 wpa_s->p2p_cb_on_scan_complete = 0; 4609 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) { 4610 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation " 4611 "continued after successful connection"); 4612 p2p_increase_search_delay( 4613 wpa_s->global->p2p, 4614 wpas_p2p_search_delay(wpa_s)); 4615 } 4616 } 4617} 4618 4619 4620int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, 4621 u32 interval1, u32 duration2, u32 interval2) 4622{ 4623 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4624 return -1; 4625 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4626 return -1; 4627 4628 if (wpa_s->wpa_state < WPA_ASSOCIATED || 4629 wpa_s->current_ssid == NULL || 4630 wpa_s->current_ssid->mode != WPAS_MODE_INFRA) 4631 return -1; 4632 4633 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, 4634 wpa_s->own_addr, wpa_s->assoc_freq, 4635 duration1, interval1, duration2, interval2); 4636} 4637 4638 4639int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, 4640 unsigned int interval) 4641{ 4642 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4643 return -1; 4644 4645 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4646 return -1; 4647 4648 return p2p_ext_listen(wpa_s->global->p2p, period, interval); 4649} 4650 4651 4652static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) 4653{ 4654 return wpa_s->current_ssid != NULL && 4655 wpa_s->current_ssid->p2p_group && 4656 wpa_s->current_ssid->mode == WPAS_MODE_INFRA; 4657} 4658 4659 4660static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) 4661{ 4662 struct wpa_supplicant *wpa_s = eloop_ctx; 4663 4664 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) { 4665 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " 4666 "disabled"); 4667 return; 4668 } 4669 4670 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " 4671 "group"); 4672 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); 4673} 4674 4675 4676static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) 4677{ 4678 int timeout; 4679 4680 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) 4681 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); 4682 4683 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) 4684 return; 4685 4686 timeout = wpa_s->conf->p2p_group_idle; 4687 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && 4688 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) 4689 timeout = P2P_MAX_CLIENT_IDLE; 4690 4691 if (timeout == 0) 4692 return; 4693 4694 if (timeout < 0) { 4695 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) 4696 timeout = 0; /* special client mode no-timeout */ 4697 else 4698 return; 4699 } 4700 4701 if (wpa_s->p2p_in_provisioning) { 4702 /* 4703 * Use the normal group formation timeout during the 4704 * provisioning phase to avoid terminating this process too 4705 * early due to group idle timeout. 4706 */ 4707 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " 4708 "during provisioning"); 4709 return; 4710 } 4711#ifndef ANDROID_P2P 4712 if (wpa_s->show_group_started) { 4713 /* 4714 * Use the normal group formation timeout between the end of 4715 * the provisioning phase and completion of 4-way handshake to 4716 * avoid terminating this process too early due to group idle 4717 * timeout. 4718 */ 4719 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " 4720 "while waiting for initial 4-way handshake to " 4721 "complete"); 4722 return; 4723 } 4724#endif 4725 4726 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds", 4727 timeout); 4728 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, 4729 wpa_s, NULL); 4730} 4731 4732 4733/* Returns 1 if the interface was removed */ 4734int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, 4735 u16 reason_code, const u8 *ie, size_t ie_len, 4736 int locally_generated) 4737{ 4738 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4739 return 0; 4740 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4741 return 0; 4742 4743 if (!locally_generated) 4744 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, 4745 ie_len); 4746 4747 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && 4748 wpa_s->current_ssid && 4749 wpa_s->current_ssid->p2p_group && 4750 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { 4751 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " 4752 "session is ending"); 4753 if (wpas_p2p_group_delete(wpa_s, 4754 P2P_GROUP_REMOVAL_GO_ENDING_SESSION) 4755 > 0) 4756 return 1; 4757 } 4758 4759 return 0; 4760} 4761 4762 4763void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, 4764 u16 reason_code, const u8 *ie, size_t ie_len, 4765 int locally_generated) 4766{ 4767 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4768 return; 4769 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4770 return; 4771 4772 if (!locally_generated) 4773 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, 4774 ie_len); 4775} 4776 4777 4778void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) 4779{ 4780 struct p2p_data *p2p = wpa_s->global->p2p; 4781 4782 if (p2p == NULL) 4783 return; 4784 4785 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) 4786 return; 4787 4788 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) 4789 p2p_set_dev_name(p2p, wpa_s->conf->device_name); 4790 4791 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) 4792 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); 4793 4794 if (wpa_s->wps && 4795 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) 4796 p2p_set_config_methods(p2p, wpa_s->wps->config_methods); 4797 4798 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) 4799 p2p_set_uuid(p2p, wpa_s->wps->uuid); 4800 4801 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { 4802 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); 4803 p2p_set_model_name(p2p, wpa_s->conf->model_name); 4804 p2p_set_model_number(p2p, wpa_s->conf->model_number); 4805 p2p_set_serial_number(p2p, wpa_s->conf->serial_number); 4806 } 4807 4808 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) 4809 p2p_set_sec_dev_types(p2p, 4810 (void *) wpa_s->conf->sec_device_type, 4811 wpa_s->conf->num_sec_device_types); 4812 4813 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { 4814 int i; 4815 p2p_remove_wps_vendor_extensions(p2p); 4816 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { 4817 if (wpa_s->conf->wps_vendor_ext[i] == NULL) 4818 continue; 4819 p2p_add_wps_vendor_extension( 4820 p2p, wpa_s->conf->wps_vendor_ext[i]); 4821 } 4822 } 4823 4824 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && 4825 wpa_s->conf->country[0] && wpa_s->conf->country[1]) { 4826 char country[3]; 4827 country[0] = wpa_s->conf->country[0]; 4828 country[1] = wpa_s->conf->country[1]; 4829 country[2] = 0x04; 4830 p2p_set_country(p2p, country); 4831 } 4832 4833 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { 4834 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, 4835 wpa_s->conf->p2p_ssid_postfix ? 4836 os_strlen(wpa_s->conf->p2p_ssid_postfix) : 4837 0); 4838 } 4839 4840 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) 4841 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); 4842 4843 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { 4844 u8 reg_class, channel; 4845 int ret; 4846 unsigned int r; 4847 if (wpa_s->conf->p2p_listen_reg_class && 4848 wpa_s->conf->p2p_listen_channel) { 4849 reg_class = wpa_s->conf->p2p_listen_reg_class; 4850 channel = wpa_s->conf->p2p_listen_channel; 4851 } else { 4852 reg_class = 81; 4853 /* 4854 * Pick one of the social channels randomly as the 4855 * listen channel. 4856 */ 4857 os_get_random((u8 *) &r, sizeof(r)); 4858 channel = 1 + (r % 3) * 5; 4859 } 4860 ret = p2p_set_listen_channel(p2p, reg_class, channel); 4861 if (ret) 4862 wpa_printf(MSG_ERROR, "P2P: Own listen channel update " 4863 "failed: %d", ret); 4864 } 4865 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { 4866 u8 op_reg_class, op_channel, cfg_op_channel; 4867 int ret = 0; 4868 unsigned int r; 4869 if (wpa_s->conf->p2p_oper_reg_class && 4870 wpa_s->conf->p2p_oper_channel) { 4871 op_reg_class = wpa_s->conf->p2p_oper_reg_class; 4872 op_channel = wpa_s->conf->p2p_oper_channel; 4873 cfg_op_channel = 1; 4874 } else { 4875 op_reg_class = 81; 4876 /* 4877 * Use random operation channel from (1, 6, 11) 4878 *if no other preference is indicated. 4879 */ 4880 os_get_random((u8 *) &r, sizeof(r)); 4881 op_channel = 1 + (r % 3) * 5; 4882 cfg_op_channel = 0; 4883 } 4884 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, 4885 cfg_op_channel); 4886 if (ret) 4887 wpa_printf(MSG_ERROR, "P2P: Own oper channel update " 4888 "failed: %d", ret); 4889 } 4890 4891 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { 4892 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, 4893 wpa_s->conf->p2p_pref_chan) < 0) { 4894 wpa_printf(MSG_ERROR, "P2P: Preferred channel list " 4895 "update failed"); 4896 } 4897 } 4898} 4899 4900 4901int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, 4902 int duration) 4903{ 4904 if (!wpa_s->ap_iface) 4905 return -1; 4906 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, 4907 duration); 4908} 4909 4910 4911int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) 4912{ 4913 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 4914 return -1; 4915 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) 4916 return -1; 4917 4918 wpa_s->global->cross_connection = enabled; 4919 p2p_set_cross_connect(wpa_s->global->p2p, enabled); 4920 4921 if (!enabled) { 4922 struct wpa_supplicant *iface; 4923 4924 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) 4925 { 4926 if (iface->cross_connect_enabled == 0) 4927 continue; 4928 4929 iface->cross_connect_enabled = 0; 4930 iface->cross_connect_in_use = 0; 4931 wpa_msg(iface->parent, MSG_INFO, 4932 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", 4933 iface->ifname, iface->cross_connect_uplink); 4934 } 4935 } 4936 4937 return 0; 4938} 4939 4940 4941static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) 4942{ 4943 struct wpa_supplicant *iface; 4944 4945 if (!uplink->global->cross_connection) 4946 return; 4947 4948 for (iface = uplink->global->ifaces; iface; iface = iface->next) { 4949 if (!iface->cross_connect_enabled) 4950 continue; 4951 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != 4952 0) 4953 continue; 4954 if (iface->ap_iface == NULL) 4955 continue; 4956 if (iface->cross_connect_in_use) 4957 continue; 4958 4959 iface->cross_connect_in_use = 1; 4960 wpa_msg(iface->parent, MSG_INFO, 4961 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", 4962 iface->ifname, iface->cross_connect_uplink); 4963 } 4964} 4965 4966 4967static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) 4968{ 4969 struct wpa_supplicant *iface; 4970 4971 for (iface = uplink->global->ifaces; iface; iface = iface->next) { 4972 if (!iface->cross_connect_enabled) 4973 continue; 4974 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != 4975 0) 4976 continue; 4977 if (!iface->cross_connect_in_use) 4978 continue; 4979 4980 wpa_msg(iface->parent, MSG_INFO, 4981 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", 4982 iface->ifname, iface->cross_connect_uplink); 4983 iface->cross_connect_in_use = 0; 4984 } 4985} 4986 4987 4988void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) 4989{ 4990 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || 4991 wpa_s->current_ssid->mode != WPAS_MODE_INFRA || 4992 wpa_s->cross_connect_disallowed) 4993 wpas_p2p_disable_cross_connect(wpa_s); 4994 else 4995 wpas_p2p_enable_cross_connect(wpa_s); 4996 if (!wpa_s->ap_iface && 4997 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) 4998 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); 4999} 5000 5001 5002void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) 5003{ 5004 wpas_p2p_disable_cross_connect(wpa_s); 5005 if (!wpa_s->ap_iface && 5006 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, 5007 wpa_s, NULL)) 5008 wpas_p2p_set_group_idle_timeout(wpa_s); 5009} 5010 5011 5012static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) 5013{ 5014 struct wpa_supplicant *iface; 5015 5016 if (!wpa_s->global->cross_connection) 5017 return; 5018 5019 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 5020 if (iface == wpa_s) 5021 continue; 5022 if (iface->drv_flags & 5023 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) 5024 continue; 5025 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) 5026 continue; 5027 5028 wpa_s->cross_connect_enabled = 1; 5029 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, 5030 sizeof(wpa_s->cross_connect_uplink)); 5031 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " 5032 "%s to %s whenever uplink is available", 5033 wpa_s->ifname, wpa_s->cross_connect_uplink); 5034 5035 if (iface->ap_iface || iface->current_ssid == NULL || 5036 iface->current_ssid->mode != WPAS_MODE_INFRA || 5037 iface->cross_connect_disallowed || 5038 iface->wpa_state != WPA_COMPLETED) 5039 break; 5040 5041 wpa_s->cross_connect_in_use = 1; 5042 wpa_msg(wpa_s->parent, MSG_INFO, 5043 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", 5044 wpa_s->ifname, wpa_s->cross_connect_uplink); 5045 break; 5046 } 5047} 5048 5049 5050int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) 5051{ 5052 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && 5053 !wpa_s->p2p_in_provisioning) 5054 return 0; /* not P2P client operation */ 5055 5056 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " 5057 "session overlap"); 5058 if (wpa_s != wpa_s->parent) 5059 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP); 5060 5061 if (wpa_s->global->p2p) 5062 p2p_group_formation_failed(wpa_s->global->p2p); 5063 5064 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 5065 wpa_s->parent, NULL); 5066 5067 wpas_group_formation_completed(wpa_s, 0); 5068 return 1; 5069} 5070 5071 5072void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s) 5073{ 5074 struct p2p_channels chan; 5075 5076 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) 5077 return; 5078 5079 os_memset(&chan, 0, sizeof(chan)); 5080 if (wpas_p2p_setup_channels(wpa_s, &chan)) { 5081 wpa_printf(MSG_ERROR, "P2P: Failed to update supported " 5082 "channel list"); 5083 return; 5084 } 5085 5086 p2p_update_channel_list(wpa_s->global->p2p, &chan); 5087} 5088 5089 5090int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) 5091{ 5092 struct wpa_global *global = wpa_s->global; 5093 int found = 0; 5094 const u8 *peer; 5095 5096 if (global->p2p == NULL) 5097 return -1; 5098 5099 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); 5100 5101 if (wpa_s->pending_interface_name[0] && 5102 !is_zero_ether_addr(wpa_s->pending_interface_addr)) 5103 found = 1; 5104 5105 peer = p2p_get_go_neg_peer(global->p2p); 5106 if (peer) { 5107 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " 5108 MACSTR, MAC2STR(peer)); 5109 p2p_unauthorize(global->p2p, peer); 5110 found = 1; 5111 } 5112 5113 wpas_p2p_stop_find(wpa_s); 5114 5115 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 5116 if (wpa_s == global->p2p_group_formation && 5117 (wpa_s->p2p_in_provisioning || 5118 wpa_s->parent->pending_interface_type == 5119 WPA_IF_P2P_CLIENT)) { 5120 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " 5121 "formation found - cancelling", 5122 wpa_s->ifname); 5123 found = 1; 5124 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 5125 wpa_s->parent, NULL); 5126 wpas_p2p_group_delete(wpa_s, 5127 P2P_GROUP_REMOVAL_REQUESTED); 5128 break; 5129 } 5130 } 5131 5132 if (!found) { 5133 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); 5134 return -1; 5135 } 5136 5137 return 0; 5138} 5139 5140 5141void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) 5142{ 5143 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) 5144 return; 5145 5146 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " 5147 "being available anymore"); 5148 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); 5149} 5150 5151 5152void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, 5153 int freq_24, int freq_5, int freq_overall) 5154{ 5155 struct p2p_data *p2p = wpa_s->global->p2p; 5156 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) 5157 return; 5158 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); 5159} 5160 5161 5162int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) 5163{ 5164 u8 peer[ETH_ALEN]; 5165 struct p2p_data *p2p = wpa_s->global->p2p; 5166 5167 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)) 5168 return -1; 5169 5170 if (hwaddr_aton(addr, peer)) 5171 return -1; 5172 5173 return p2p_unauthorize(p2p, peer); 5174} 5175 5176 5177/** 5178 * wpas_p2p_disconnect - Disconnect from a P2P Group 5179 * @wpa_s: Pointer to wpa_supplicant data 5180 * Returns: 0 on success, -1 on failure 5181 * 5182 * This can be used to disconnect from a group in which the local end is a P2P 5183 * Client or to end a P2P Group in case the local end is the Group Owner. If a 5184 * virtual network interface was created for this group, that interface will be 5185 * removed. Otherwise, only the configured P2P group network will be removed 5186 * from the interface. 5187 */ 5188int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) 5189{ 5190 5191 if (wpa_s == NULL) 5192 return -1; 5193 5194 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? 5195 -1 : 0; 5196} 5197 5198 5199int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) 5200{ 5201#ifdef ANDROID_P2P 5202 struct wpa_supplicant *group = wpa_s; 5203#endif 5204 5205 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 5206 return 0; 5207 5208#ifdef ANDROID_P2P 5209 while (group && (group->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) { 5210 if(group->wpa_state == WPA_ASSOCIATED) { 5211 /* WPA_ASSOCIATED hasn't moved to WPA_COMPLETED. So it could be in WPS 5212 * or 4Way Hanshake phase. Avoid allowing scan during this time critical 5213 * phase 5214 */ 5215 wpa_printf(MSG_ERROR, "P2P: WPS/4way handshake in Progress." 5216 " Defer SCAN "); 5217 return 1; 5218 } 5219 group = group->next; 5220 } 5221#endif 5222 5223 return p2p_in_progress(wpa_s->global->p2p); 5224} 5225 5226 5227void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, 5228 struct wpa_ssid *ssid) 5229{ 5230 if (wpa_s->p2p_in_provisioning && ssid->p2p_group && 5231 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 5232 wpa_s->parent, NULL) > 0) { 5233 /** 5234 * Remove the network by scheduling the group formation 5235 * timeout to happen immediately. The teardown code 5236 * needs to be scheduled to run asynch later so that we 5237 * don't delete data from under ourselves unexpectedly. 5238 * Calling wpas_p2p_group_formation_timeout directly 5239 * causes a series of crashes in WPS failure scenarios. 5240 */ 5241 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " 5242 "P2P group network getting removed"); 5243 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, 5244 wpa_s->parent, NULL); 5245 } 5246} 5247 5248 5249struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, 5250 const u8 *addr, const u8 *ssid, 5251 size_t ssid_len) 5252{ 5253 struct wpa_ssid *s; 5254 size_t i; 5255 5256 for (s = wpa_s->conf->ssid; s; s = s->next) { 5257 if (s->disabled != 2) 5258 continue; 5259 if (ssid && 5260 (ssid_len != s->ssid_len || 5261 os_memcmp(ssid, s->ssid, ssid_len) != 0)) 5262 continue; 5263 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) 5264 return s; /* peer is GO in the persistent group */ 5265 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) 5266 continue; 5267 for (i = 0; i < s->num_p2p_clients; i++) { 5268 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, 5269 addr, ETH_ALEN) == 0) 5270 return s; /* peer is P2P client in persistent 5271 * group */ 5272 } 5273 } 5274 5275 return NULL; 5276} 5277 5278 5279void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, 5280 const u8 *addr) 5281{ 5282 if (addr == NULL) 5283 return; 5284 wpas_p2p_add_persistent_group_client(wpa_s, addr); 5285} 5286 5287 5288static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, 5289 int group_added) 5290{ 5291 struct wpa_supplicant *group = wpa_s; 5292 eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL); 5293 if (wpa_s->global->p2p_group_formation) 5294 group = wpa_s->global->p2p_group_formation; 5295 wpa_s = wpa_s->parent; 5296 offchannel_send_action_done(wpa_s); 5297 if (group_added) 5298 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); 5299 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); 5300 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, 5301 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, 5302 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, 5303 wpa_s->p2p_persistent_id, 5304 wpa_s->p2p_pd_before_go_neg, 5305 wpa_s->p2p_go_ht40); 5306} 5307 5308 5309int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) 5310{ 5311 if (!wpa_s->p2p_fallback_to_go_neg || 5312 wpa_s->p2p_in_provisioning <= 5) 5313 return 0; 5314 5315 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) 5316 return 0; /* peer operating as a GO */ 5317 5318 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " 5319 "fallback to GO Negotiation"); 5320 wpas_p2p_fallback_to_go_neg(wpa_s, 1); 5321 5322 return 1; 5323} 5324 5325 5326unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) 5327{ 5328 const char *rn, *rn2; 5329 struct wpa_supplicant *ifs; 5330 5331 if (wpa_s->wpa_state > WPA_SCANNING) { 5332 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " 5333 "concurrent operation", 5334 P2P_CONCURRENT_SEARCH_DELAY); 5335 return P2P_CONCURRENT_SEARCH_DELAY; 5336 } 5337 5338 if (!wpa_s->driver->get_radio_name) 5339 return 0; 5340 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv); 5341 if (rn == NULL || rn[0] == '\0') 5342 return 0; 5343 5344 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { 5345 if (ifs == wpa_s || !ifs->driver->get_radio_name) 5346 continue; 5347 5348 rn2 = ifs->driver->get_radio_name(ifs->drv_priv); 5349 if (!rn2 || os_strcmp(rn, rn2) != 0) 5350 continue; 5351 if (ifs->wpa_state > WPA_SCANNING) { 5352 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " 5353 "delay due to concurrent operation on " 5354 "interface %s", 5355 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname); 5356 return P2P_CONCURRENT_SEARCH_DELAY; 5357 } 5358 } 5359 5360 return 0; 5361} 5362 5363#ifdef ANDROID_P2P 5364int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq) 5365{ 5366 struct wpa_supplicant *iface = NULL; 5367 struct p2p_data *p2p = wpa_s->global->p2p; 5368 5369 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 5370 if((iface->p2p_group_interface) && (iface->current_ssid) && 5371 (iface->current_ssid->frequency != freq)) { 5372 5373 if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) { 5374 /* Try to see whether we can move the GO. If it 5375 * is not possible, remove the GO interface 5376 */ 5377 if(wpa_drv_switch_channel(iface, freq) == 0) { 5378 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq); 5379 iface->current_ssid->frequency = freq; 5380 continue; 5381 } 5382 } 5383 5384 /* If GO cannot be moved or if the conflicting interface is a 5385 * P2P Client, remove the interface depending up on the connection 5386 * priority */ 5387 if(!wpas_is_p2p_prioritized(wpa_s)) { 5388 /* STA connection has priority over existing 5389 * P2P connection. So remove the interface */ 5390 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel" 5391 "concurrent mode frequency conflict"); 5392 wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT); 5393 } else { 5394 /* Existing connection has the priority. Disable the newly 5395 * selected network and let the application know about it. 5396 */ 5397 return -1; 5398 } 5399 } 5400 } 5401 return 0; 5402} 5403#endif 5404