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