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