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