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