1 /* 2 * wpa_supplicant - P2P 3 * Copyright (c) 2009-2010, Atheros Communications 4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi> 5 * 6 * This software may be distributed under the terms of the BSD license. 7 * See README for more details. 8 */ 9 10 #include "includes.h" 11 12 #include "common.h" 13 #include "eloop.h" 14 #include "common/ieee802_11_common.h" 15 #include "common/ieee802_11_defs.h" 16 #include "common/wpa_ctrl.h" 17 #include "wps/wps_i.h" 18 #include "p2p/p2p.h" 19 #include "ap/hostapd.h" 20 #include "ap/ap_config.h" 21 #include "ap/sta_info.h" 22 #include "ap/ap_drv_ops.h" 23 #include "ap/wps_hostapd.h" 24 #include "ap/p2p_hostapd.h" 25 #include "ap/dfs.h" 26 #include "eapol_supp/eapol_supp_sm.h" 27 #include "rsn_supp/wpa.h" 28 #include "wpa_supplicant_i.h" 29 #include "driver_i.h" 30 #include "ap.h" 31 #include "config_ssid.h" 32 #include "config.h" 33 #include "notify.h" 34 #include "scan.h" 35 #include "bss.h" 36 #include "offchannel.h" 37 #include "wps_supplicant.h" 38 #include "p2p_supplicant.h" 39 #include "wifi_display.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 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5 49 50 /** 51 * Defines time interval in seconds when a GO needs to evacuate a frequency that 52 * it is currently using, but is no longer valid for P2P use cases. 53 */ 54 #define P2P_GO_FREQ_CHANGE_TIME 5 55 56 /** 57 * Defines CSA parameters which are used when GO evacuates the no longer valid 58 * channel (and if the driver supports channel switch). 59 */ 60 #define P2P_GO_CSA_COUNT 7 61 #define P2P_GO_CSA_BLOCK_TX 0 62 63 #ifndef P2P_MAX_CLIENT_IDLE 64 /* 65 * How many seconds to try to reconnect to the GO when connection in P2P client 66 * role has been lost. 67 */ 68 #define P2P_MAX_CLIENT_IDLE 10 69 #endif /* P2P_MAX_CLIENT_IDLE */ 70 71 #ifndef P2P_MAX_INITIAL_CONN_WAIT 72 /* 73 * How many seconds to wait for initial 4-way handshake to get completed after 74 * WPS provisioning step or after the re-invocation of a persistent group on a 75 * P2P Client. 76 */ 77 #define P2P_MAX_INITIAL_CONN_WAIT 10 78 #endif /* P2P_MAX_INITIAL_CONN_WAIT */ 79 80 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO 81 /* 82 * How many seconds to wait for initial 4-way handshake to get completed after 83 * WPS provisioning step on the GO. This controls the extra time the P2P 84 * operation is considered to be in progress (e.g., to delay other scans) after 85 * WPS provisioning has been completed on the GO during group formation. 86 */ 87 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10 88 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */ 89 90 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 91 /* 92 * How many seconds to wait for initial 4-way handshake to get completed after 93 * re-invocation of a persistent group on the GO when the client is expected 94 * to connect automatically (no user interaction). 95 */ 96 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15 97 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */ 98 99 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" 100 101 /* 102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt 103 * was not possible. 104 */ 105 #define P2P_RECONSIDER_GO_MOVE_DELAY 30 106 107 enum p2p_group_removal_reason { 108 P2P_GROUP_REMOVAL_UNKNOWN, 109 P2P_GROUP_REMOVAL_SILENT, 110 P2P_GROUP_REMOVAL_FORMATION_FAILED, 111 P2P_GROUP_REMOVAL_REQUESTED, 112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT, 113 P2P_GROUP_REMOVAL_UNAVAILABLE, 114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION, 115 P2P_GROUP_REMOVAL_PSK_FAILURE, 116 P2P_GROUP_REMOVAL_FREQ_CONFLICT, 117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL 118 }; 119 120 121 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx); 122 static struct wpa_supplicant * 123 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, 124 int go); 125 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, 126 const u8 *ssid, size_t ssid_len); 127 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq, 128 int *force_freq, int *pref_freq, int go, 129 unsigned int *pref_freq_list, 130 unsigned int *num_pref_freq); 131 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, 132 const u8 *ssid, size_t ssid_len); 133 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx); 134 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, 135 const u8 *dev_addr, enum p2p_wps_method wps_method, 136 int auto_join, int freq, 137 const u8 *ssid, size_t ssid_len); 138 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s); 139 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s); 140 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx); 141 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s); 142 static void wpas_p2p_group_formation_timeout(void *eloop_ctx, 143 void *timeout_ctx); 144 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx); 145 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, 146 int group_added); 147 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s); 148 static void wpas_stop_listen(void *ctx); 149 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx); 150 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s); 151 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, 152 enum wpa_driver_if_type type); 153 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s, 154 int already_deleted); 155 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s, 156 struct wpa_used_freq_data *freqs, 157 unsigned int num); 158 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx); 159 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq); 160 static void 161 wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s, 162 struct wpa_used_freq_data *freqs, unsigned int num, 163 enum wpas_p2p_channel_update_trig trig); 164 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx); 165 166 167 /* 168 * Get the number of concurrent channels that the HW can operate, but that are 169 * currently not in use by any of the wpa_supplicant interfaces. 170 */ 171 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s) 172 { 173 int *freqs; 174 int num, unused; 175 176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int)); 177 if (!freqs) 178 return -1; 179 180 num = get_shared_radio_freqs(wpa_s, freqs, 181 wpa_s->num_multichan_concurrent); 182 os_free(freqs); 183 184 unused = wpa_s->num_multichan_concurrent - num; 185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused); 186 return unused; 187 } 188 189 190 /* 191 * Get the frequencies that are currently in use by one or more of the virtual 192 * interfaces, and that are also valid for P2P operation. 193 */ 194 static unsigned int 195 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s, 196 struct wpa_used_freq_data *p2p_freqs, 197 unsigned int len) 198 { 199 struct wpa_used_freq_data *freqs; 200 unsigned int num, i, j; 201 202 freqs = os_calloc(wpa_s->num_multichan_concurrent, 203 sizeof(struct wpa_used_freq_data)); 204 if (!freqs) 205 return 0; 206 207 num = get_shared_radio_freqs_data(wpa_s, freqs, 208 wpa_s->num_multichan_concurrent); 209 210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len); 211 212 for (i = 0, j = 0; i < num && j < len; i++) { 213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) 214 p2p_freqs[j++] = freqs[i]; 215 } 216 217 os_free(freqs); 218 219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j); 220 221 return j; 222 } 223 224 225 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s, 226 int freq) 227 { 228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 229 return; 230 231 /* Use the wpa_s used to control the P2P Device operation */ 232 wpa_s = wpa_s->global->p2p_init_wpa_s; 233 234 if (wpa_s->conf->p2p_ignore_shared_freq && 235 freq > 0 && wpa_s->num_multichan_concurrent > 1 && 236 wpas_p2p_num_unused_channels(wpa_s) > 0) { 237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration", 238 freq); 239 freq = 0; 240 } 241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq); 242 } 243 244 245 static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s) 246 { 247 unsigned int delay = wpas_p2p_search_delay(wpa_s); 248 249 /* In case of concurrent P2P and external scans, delay P2P search. */ 250 if (external_scan_running(wpa_s->radio)) { 251 delay = wpa_s->conf->p2p_search_delay; 252 wpa_printf(MSG_DEBUG, 253 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete", 254 delay); 255 } 256 257 p2p_scan_res_handled(wpa_s->global->p2p, delay); 258 } 259 260 261 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s, 262 struct wpa_scan_results *scan_res) 263 { 264 size_t i; 265 266 if (wpa_s->p2p_scan_work) { 267 struct wpa_radio_work *work = wpa_s->p2p_scan_work; 268 wpa_s->p2p_scan_work = NULL; 269 radio_work_done(work); 270 } 271 272 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 273 return; 274 275 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)", 276 (int) scan_res->num); 277 278 for (i = 0; i < scan_res->num; i++) { 279 struct wpa_scan_res *bss = scan_res->res[i]; 280 struct os_reltime time_tmp_age, entry_ts; 281 const u8 *ies; 282 size_t ies_len; 283 284 time_tmp_age.sec = bss->age / 1000; 285 time_tmp_age.usec = (bss->age % 1000) * 1000; 286 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts); 287 288 ies = (const u8 *) (bss + 1); 289 ies_len = bss->ie_len; 290 if (bss->beacon_ie_len > 0 && 291 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && 292 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) { 293 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for " 294 MACSTR, MAC2STR(bss->bssid)); 295 ies = ies + ies_len; 296 ies_len = bss->beacon_ie_len; 297 } 298 299 300 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid, 301 bss->freq, &entry_ts, bss->level, 302 ies, ies_len) > 0) 303 break; 304 } 305 306 wpas_p2p_scan_res_handled(wpa_s); 307 } 308 309 310 static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s) 311 { 312 if (wpa_s->p2p_scan_work) { 313 struct wpa_radio_work *work = wpa_s->p2p_scan_work; 314 315 wpa_s->p2p_scan_work = NULL; 316 radio_work_done(work); 317 } 318 319 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) 320 return; 321 322 wpa_dbg(wpa_s, MSG_DEBUG, 323 "P2P: Failed to get scan results - try to continue"); 324 wpas_p2p_scan_res_handled(wpa_s); 325 } 326 327 328 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit) 329 { 330 struct wpa_supplicant *wpa_s = work->wpa_s; 331 struct wpa_driver_scan_params *params = work->ctx; 332 int ret; 333 334 if (deinit) { 335 if (!work->started) { 336 wpa_scan_free_params(params); 337 return; 338 } 339 340 wpa_s->p2p_scan_work = NULL; 341 return; 342 } 343 344 if (wpa_s->clear_driver_scan_cache) { 345 wpa_printf(MSG_DEBUG, 346 "Request driver to clear scan cache due to local BSS flush"); 347 params->only_new_results = 1; 348 } 349 350 if (!params->p2p_include_6ghz && !params->freqs) { 351 wpa_printf(MSG_DEBUG, 352 "P2P: Exclude 6 GHz channels - update the scan frequency list"); 353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, params, 354 0); 355 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, params, 356 0); 357 } 358 ret = wpa_drv_scan(wpa_s, params); 359 if (ret == 0) 360 wpa_s->curr_scan_cookie = params->scan_cookie; 361 wpa_scan_free_params(params); 362 work->ctx = NULL; 363 if (ret) { 364 radio_work_done(work); 365 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret); 366 return; 367 } 368 369 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret); 370 os_get_reltime(&wpa_s->scan_trigger_time); 371 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler; 372 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler; 373 wpa_s->own_scan_requested = 1; 374 wpa_s->clear_driver_scan_cache = 0; 375 wpa_s->p2p_scan_work = work; 376 } 377 378 379 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s, 380 int freq) 381 { 382 if (wpa_s->global->p2p_24ghz_social_channels && 383 (freq == 2412 || freq == 2437 || freq == 2462)) { 384 /* 385 * Search all social channels regardless of whether these have 386 * been disabled for P2P operating channel use to avoid missing 387 * peers. 388 */ 389 return 1; 390 } 391 return p2p_supported_freq(wpa_s->global->p2p, freq); 392 } 393 394 395 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq, 396 unsigned int num_req_dev_types, 397 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id, 398 bool include_6ghz) 399 { 400 struct wpa_supplicant *wpa_s = ctx; 401 struct wpa_driver_scan_params *params = NULL; 402 struct wpabuf *wps_ie, *ies; 403 unsigned int num_channels = 0; 404 int social_channels_freq[] = { 2412, 2437, 2462, 60480 }; 405 size_t ielen; 406 u8 *n, i; 407 unsigned int bands; 408 409 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 410 return -1; 411 412 if (wpa_s->p2p_scan_work) { 413 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending"); 414 return -1; 415 } 416 417 params = os_zalloc(sizeof(*params)); 418 if (params == NULL) 419 return -1; 420 421 /* P2P Wildcard SSID */ 422 params->num_ssids = 1; 423 n = os_malloc(P2P_WILDCARD_SSID_LEN); 424 if (n == NULL) 425 goto fail; 426 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN); 427 params->ssids[0].ssid = n; 428 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; 429 430 wpa_s->wps->dev.p2p = 1; 431 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev, 432 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 433 num_req_dev_types, req_dev_types); 434 if (wps_ie == NULL) 435 goto fail; 436 if (!wpa_s->conf->p2p_6ghz_disable) 437 params->p2p_include_6ghz = include_6ghz; 438 switch (type) { 439 case P2P_SCAN_SOCIAL: 440 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1, 441 sizeof(int)); 442 if (params->freqs == NULL) 443 goto fail; 444 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) { 445 if (wpas_p2p_search_social_channel( 446 wpa_s, social_channels_freq[i])) 447 params->freqs[num_channels++] = 448 social_channels_freq[i]; 449 } 450 params->freqs[num_channels++] = 0; 451 break; 452 case P2P_SCAN_FULL: 453 break; 454 case P2P_SCAN_SPECIFIC: 455 params->freqs = os_calloc(2, sizeof(int)); 456 if (params->freqs == NULL) 457 goto fail; 458 params->freqs[0] = freq; 459 params->freqs[1] = 0; 460 break; 461 case P2P_SCAN_SOCIAL_PLUS_ONE: 462 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2, 463 sizeof(int)); 464 if (params->freqs == NULL) 465 goto fail; 466 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) { 467 if (wpas_p2p_search_social_channel( 468 wpa_s, social_channels_freq[i])) 469 params->freqs[num_channels++] = 470 social_channels_freq[i]; 471 } 472 if (p2p_supported_freq(wpa_s->global->p2p, freq)) 473 params->freqs[num_channels++] = freq; 474 params->freqs[num_channels++] = 0; 475 break; 476 } 477 478 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); 479 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); 480 if (ies == NULL) { 481 wpabuf_free(wps_ie); 482 goto fail; 483 } 484 wpabuf_put_buf(ies, wps_ie); 485 wpabuf_free(wps_ie); 486 487 bands = wpas_get_bands(wpa_s, params->freqs); 488 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands); 489 490 params->p2p_probe = 1; 491 n = os_malloc(wpabuf_len(ies)); 492 if (n == NULL) { 493 wpabuf_free(ies); 494 goto fail; 495 } 496 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies)); 497 params->extra_ies = n; 498 params->extra_ies_len = wpabuf_len(ies); 499 wpabuf_free(ies); 500 501 radio_remove_works(wpa_s, "p2p-scan", 0); 502 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb, 503 params) < 0) 504 goto fail; 505 return 0; 506 507 fail: 508 wpa_scan_free_params(params); 509 return -1; 510 } 511 512 513 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface) 514 { 515 switch (p2p_group_interface) { 516 case P2P_GROUP_INTERFACE_PENDING: 517 return WPA_IF_P2P_GROUP; 518 case P2P_GROUP_INTERFACE_GO: 519 return WPA_IF_P2P_GO; 520 case P2P_GROUP_INTERFACE_CLIENT: 521 return WPA_IF_P2P_CLIENT; 522 } 523 524 return WPA_IF_P2P_GROUP; 525 } 526 527 528 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s, 529 const u8 *ssid, 530 size_t ssid_len, int *go) 531 { 532 struct wpa_ssid *s; 533 534 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 535 for (s = wpa_s->conf->ssid; s; s = s->next) { 536 if (s->disabled != 0 || !s->p2p_group || 537 s->ssid_len != ssid_len || 538 os_memcmp(ssid, s->ssid, ssid_len) != 0) 539 continue; 540 if (s->mode == WPAS_MODE_P2P_GO && 541 s != wpa_s->current_ssid) 542 continue; 543 if (go) 544 *go = s->mode == WPAS_MODE_P2P_GO; 545 return wpa_s; 546 } 547 } 548 549 return NULL; 550 } 551 552 553 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx) 554 { 555 struct wpa_supplicant *wpa_s = eloop_ctx; 556 wpa_printf(MSG_DEBUG, 557 "P2P: Complete previously requested removal of %s", 558 wpa_s->ifname); 559 wpas_p2p_disconnect(wpa_s); 560 } 561 562 563 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s, 564 struct wpa_supplicant *calling_wpa_s) 565 { 566 if (calling_wpa_s == wpa_s && wpa_s && 567 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { 568 /* 569 * The calling wpa_s instance is going to be removed. Do that 570 * from an eloop callback to keep the instance available until 571 * the caller has returned. This may be needed, e.g., to provide 572 * control interface responses on the per-interface socket. 573 */ 574 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect, 575 wpa_s, NULL) < 0) 576 return -1; 577 return 0; 578 } 579 580 return wpas_p2p_disconnect(wpa_s); 581 } 582 583 584 /* Determine total number of clients in active groups where we are the GO */ 585 static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s) 586 { 587 unsigned int count = 0; 588 struct wpa_ssid *s; 589 590 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 591 for (s = wpa_s->conf->ssid; s; s = s->next) { 592 wpa_printf(MSG_DEBUG, 593 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d", 594 wpa_s, s, s->disabled, s->p2p_group, 595 s->mode); 596 if (!s->disabled && s->p2p_group && 597 s->mode == WPAS_MODE_P2P_GO) { 598 count += p2p_get_group_num_members( 599 wpa_s->p2p_group); 600 } 601 } 602 } 603 604 return count; 605 } 606 607 608 static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s) 609 { 610 return !wpa_s->p2p_mgmt && wpa_s->current_ssid && 611 !wpa_s->current_ssid->disabled && 612 wpa_s->current_ssid->p2p_group && 613 wpa_s->current_ssid->p2p_persistent_group; 614 } 615 616 617 static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s) 618 { 619 return p2p_is_active_persistent_group(wpa_s) && 620 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO; 621 } 622 623 624 /* Find an interface for a P2P group where we are the GO */ 625 static struct wpa_supplicant * 626 wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s) 627 { 628 struct wpa_supplicant *save = NULL; 629 630 if (!wpa_s) 631 return NULL; 632 633 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 634 if (!p2p_is_active_persistent_go(wpa_s)) 635 continue; 636 637 /* Prefer a group with connected clients */ 638 if (p2p_get_group_num_members(wpa_s->p2p_group)) 639 return wpa_s; 640 save = wpa_s; 641 } 642 643 /* No group with connected clients, so pick the one without (if any) */ 644 return save; 645 } 646 647 648 static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s) 649 { 650 return p2p_is_active_persistent_group(wpa_s) && 651 wpa_s->current_ssid->mode == WPAS_MODE_INFRA; 652 } 653 654 655 /* Find an interface for a P2P group where we are the P2P Client */ 656 static struct wpa_supplicant * 657 wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s) 658 { 659 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 660 if (p2p_is_active_persistent_cli(wpa_s)) 661 return wpa_s; 662 } 663 664 return NULL; 665 } 666 667 668 /* Find a persistent group where we are the GO */ 669 static struct wpa_ssid * 670 wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s) 671 { 672 struct wpa_ssid *s; 673 674 for (s = wpa_s->conf->ssid; s; s = s->next) { 675 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO) 676 return s; 677 } 678 679 return NULL; 680 } 681 682 683 static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role, 684 unsigned int *force_freq, 685 unsigned int *pref_freq) 686 { 687 struct wpa_supplicant *wpa_s = ctx; 688 struct wpa_ssid *s; 689 u8 conncap = P2PS_SETUP_NONE; 690 unsigned int owned_members = 0; 691 struct wpa_supplicant *go_wpa_s, *cli_wpa_s; 692 struct wpa_ssid *persistent_go; 693 int p2p_no_group_iface; 694 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; 695 696 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role); 697 698 if (force_freq) 699 *force_freq = 0; 700 if (pref_freq) 701 *pref_freq = 0; 702 703 size = P2P_MAX_PREF_CHANNELS; 704 if (force_freq && pref_freq && 705 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq, 706 (int *) pref_freq, 0, pref_freq_list, &size)) 707 wpas_p2p_set_own_freq_preference(wpa_s, 708 *force_freq ? *force_freq : 709 *pref_freq); 710 711 /* 712 * For non-concurrent capable devices: 713 * If persistent_go, then no new. 714 * If GO, then no client. 715 * If client, then no GO. 716 */ 717 go_wpa_s = wpas_p2p_get_go_group(wpa_s); 718 if (go_wpa_s) 719 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group); 720 persistent_go = wpas_p2p_get_persistent_go(wpa_s); 721 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s); 722 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s); 723 724 wpa_printf(MSG_DEBUG, 725 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p", 726 go_wpa_s, owned_members, cli_wpa_s, persistent_go); 727 728 /* If not concurrent, restrict our choices */ 729 if (p2p_no_group_iface) { 730 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface"); 731 732 if (cli_wpa_s) 733 return P2PS_SETUP_NONE; 734 735 if (go_wpa_s) { 736 if (role == P2PS_SETUP_CLIENT || 737 incoming == P2PS_SETUP_GROUP_OWNER || 738 p2p_client_limit_reached(go_wpa_s->p2p_group)) 739 return P2PS_SETUP_NONE; 740 741 return P2PS_SETUP_GROUP_OWNER; 742 } 743 744 if (persistent_go) { 745 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) { 746 if (!incoming) 747 return P2PS_SETUP_GROUP_OWNER | 748 P2PS_SETUP_CLIENT; 749 if (incoming == P2PS_SETUP_NEW) { 750 u8 r; 751 752 if (os_get_random(&r, sizeof(r)) < 0 || 753 (r & 1)) 754 return P2PS_SETUP_CLIENT; 755 return P2PS_SETUP_GROUP_OWNER; 756 } 757 } 758 } 759 } 760 761 /* If a required role has been specified, handle it here */ 762 if (role && role != P2PS_SETUP_NEW) { 763 switch (incoming) { 764 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW: 765 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT: 766 /* 767 * Peer has an active GO, so if the role allows it and 768 * we do not have any active roles, become client. 769 */ 770 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s && 771 !cli_wpa_s) 772 return P2PS_SETUP_CLIENT; 773 774 /* fall through */ 775 776 case P2PS_SETUP_NONE: 777 case P2PS_SETUP_NEW: 778 conncap = role; 779 goto grp_owner; 780 781 case P2PS_SETUP_GROUP_OWNER: 782 /* 783 * Must be a complimentary role - cannot be a client to 784 * more than one peer. 785 */ 786 if (incoming == role || cli_wpa_s) 787 return P2PS_SETUP_NONE; 788 789 return P2PS_SETUP_CLIENT; 790 791 case P2PS_SETUP_CLIENT: 792 /* Must be a complimentary role */ 793 if (incoming != role) { 794 conncap = P2PS_SETUP_GROUP_OWNER; 795 goto grp_owner; 796 } 797 /* fall through */ 798 799 default: 800 return P2PS_SETUP_NONE; 801 } 802 } 803 804 /* 805 * For now, we only will support ownership of one group, and being a 806 * client of one group. Therefore, if we have either an existing GO 807 * group, or an existing client group, we will not do a new GO 808 * negotiation, but rather try to re-use the existing groups. 809 */ 810 switch (incoming) { 811 case P2PS_SETUP_NONE: 812 case P2PS_SETUP_NEW: 813 if (cli_wpa_s) 814 conncap = P2PS_SETUP_GROUP_OWNER; 815 else if (!owned_members) 816 conncap = P2PS_SETUP_NEW; 817 else if (incoming == P2PS_SETUP_NONE) 818 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT; 819 else 820 conncap = P2PS_SETUP_CLIENT; 821 break; 822 823 case P2PS_SETUP_CLIENT: 824 conncap = P2PS_SETUP_GROUP_OWNER; 825 break; 826 827 case P2PS_SETUP_GROUP_OWNER: 828 if (!cli_wpa_s) 829 conncap = P2PS_SETUP_CLIENT; 830 break; 831 832 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW: 833 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT: 834 if (cli_wpa_s) 835 conncap = P2PS_SETUP_GROUP_OWNER; 836 else { 837 u8 r; 838 839 if (os_get_random(&r, sizeof(r)) < 0 || 840 (r & 1)) 841 conncap = P2PS_SETUP_CLIENT; 842 else 843 conncap = P2PS_SETUP_GROUP_OWNER; 844 } 845 break; 846 847 default: 848 return P2PS_SETUP_NONE; 849 } 850 851 grp_owner: 852 if ((conncap & P2PS_SETUP_GROUP_OWNER) || 853 (!incoming && (conncap & P2PS_SETUP_NEW))) { 854 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group)) 855 conncap &= ~P2PS_SETUP_GROUP_OWNER; 856 857 s = wpas_p2p_get_persistent_go(wpa_s); 858 if (!s && !go_wpa_s && p2p_no_group_iface) { 859 p2p_set_intended_addr(wpa_s->global->p2p, 860 wpa_s->p2p_mgmt ? 861 wpa_s->parent->own_addr : 862 wpa_s->own_addr); 863 } else if (!s && !go_wpa_s) { 864 if (wpas_p2p_add_group_interface(wpa_s, 865 WPA_IF_P2P_GROUP) < 0) { 866 wpa_printf(MSG_ERROR, 867 "P2P: Failed to allocate a new interface for the group"); 868 return P2PS_SETUP_NONE; 869 } 870 wpa_s->global->pending_group_iface_for_p2ps = 1; 871 p2p_set_intended_addr(wpa_s->global->p2p, 872 wpa_s->pending_interface_addr); 873 } 874 } 875 876 return conncap; 877 } 878 879 880 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s, 881 enum p2p_group_removal_reason removal_reason) 882 { 883 struct wpa_ssid *ssid; 884 char *gtype; 885 const char *reason; 886 887 ssid = wpa_s->current_ssid; 888 if (ssid == NULL) { 889 /* 890 * The current SSID was not known, but there may still be a 891 * pending P2P group interface waiting for provisioning or a 892 * P2P group that is trying to reconnect. 893 */ 894 ssid = wpa_s->conf->ssid; 895 while (ssid) { 896 if (ssid->p2p_group && ssid->disabled != 2) 897 break; 898 ssid = ssid->next; 899 } 900 if (ssid == NULL && 901 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) 902 { 903 wpa_printf(MSG_ERROR, "P2P: P2P group interface " 904 "not found"); 905 return -1; 906 } 907 } 908 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO) 909 gtype = "GO"; 910 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT || 911 (ssid && ssid->mode == WPAS_MODE_INFRA)) { 912 wpa_s->reassociate = 0; 913 wpa_s->disconnected = 1; 914 gtype = "client"; 915 } else 916 gtype = "GO"; 917 918 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid) 919 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype); 920 921 if (os_strcmp(gtype, "client") == 0) { 922 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING); 923 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal, 924 wpa_s, NULL)) { 925 wpa_printf(MSG_DEBUG, 926 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal"); 927 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE; 928 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, 929 wpa_s, NULL); 930 } 931 } 932 933 if (wpa_s->cross_connect_in_use) { 934 wpa_s->cross_connect_in_use = 0; 935 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 936 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", 937 wpa_s->ifname, wpa_s->cross_connect_uplink); 938 } 939 switch (removal_reason) { 940 case P2P_GROUP_REMOVAL_REQUESTED: 941 reason = " reason=REQUESTED"; 942 break; 943 case P2P_GROUP_REMOVAL_FORMATION_FAILED: 944 reason = " reason=FORMATION_FAILED"; 945 break; 946 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT: 947 reason = " reason=IDLE"; 948 break; 949 case P2P_GROUP_REMOVAL_UNAVAILABLE: 950 reason = " reason=UNAVAILABLE"; 951 break; 952 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION: 953 reason = " reason=GO_ENDING_SESSION"; 954 break; 955 case P2P_GROUP_REMOVAL_PSK_FAILURE: 956 reason = " reason=PSK_FAILURE"; 957 break; 958 case P2P_GROUP_REMOVAL_FREQ_CONFLICT: 959 reason = " reason=FREQ_CONFLICT"; 960 break; 961 default: 962 reason = ""; 963 break; 964 } 965 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) { 966 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 967 P2P_EVENT_GROUP_REMOVED "%s %s%s", 968 wpa_s->ifname, gtype, reason); 969 } 970 971 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0) 972 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout"); 973 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) 974 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); 975 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 976 wpa_s->p2pdev, NULL) > 0) { 977 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation " 978 "timeout"); 979 wpa_s->p2p_in_provisioning = 0; 980 wpas_p2p_group_formation_failed(wpa_s, 1); 981 } 982 983 wpa_s->p2p_in_invitation = 0; 984 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL); 985 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL); 986 987 /* 988 * Make sure wait for the first client does not remain active after the 989 * group has been removed. 990 */ 991 wpa_s->global->p2p_go_wait_client.sec = 0; 992 993 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { 994 struct wpa_global *global; 995 char *ifname; 996 enum wpa_driver_if_type type; 997 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s", 998 wpa_s->ifname); 999 global = wpa_s->global; 1000 ifname = os_strdup(wpa_s->ifname); 1001 type = wpas_p2p_if_type(wpa_s->p2p_group_interface); 1002 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL); 1003 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0); 1004 wpa_s = global->ifaces; 1005 if (wpa_s && ifname) 1006 wpa_drv_if_remove(wpa_s, type, ifname); 1007 os_free(ifname); 1008 return 1; 1009 } 1010 1011 /* 1012 * The primary interface was used for P2P group operations, so 1013 * need to reset its p2pdev. 1014 */ 1015 wpa_s->p2pdev = wpa_s->parent; 1016 1017 if (!wpa_s->p2p_go_group_formation_completed) { 1018 wpa_s->global->p2p_group_formation = NULL; 1019 wpa_s->p2p_in_provisioning = 0; 1020 } 1021 1022 wpa_s->show_group_started = 0; 1023 os_free(wpa_s->go_params); 1024 wpa_s->go_params = NULL; 1025 1026 os_free(wpa_s->p2p_group_common_freqs); 1027 wpa_s->p2p_group_common_freqs = NULL; 1028 wpa_s->p2p_group_common_freqs_num = 0; 1029 wpa_s->p2p_go_do_acs = 0; 1030 1031 wpa_s->waiting_presence_resp = 0; 1032 1033 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network"); 1034 if (ssid && (ssid->p2p_group || 1035 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION || 1036 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) { 1037 int id = ssid->id; 1038 if (ssid == wpa_s->current_ssid) { 1039 wpa_sm_set_config(wpa_s->wpa, NULL); 1040 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL); 1041 wpa_s->current_ssid = NULL; 1042 } 1043 /* 1044 * Networks objects created during any P2P activities are not 1045 * exposed out as they might/will confuse certain non-P2P aware 1046 * applications since these network objects won't behave like 1047 * regular ones. 1048 * 1049 * Likewise, we don't send out network removed signals for such 1050 * network objects. 1051 */ 1052 wpa_config_remove_network(wpa_s->conf, id); 1053 wpa_supplicant_clear_status(wpa_s); 1054 wpa_supplicant_cancel_sched_scan(wpa_s); 1055 } else { 1056 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not " 1057 "found"); 1058 } 1059 if (wpa_s->ap_iface) 1060 wpa_supplicant_ap_deinit(wpa_s); 1061 else 1062 wpa_drv_deinit_p2p_cli(wpa_s); 1063 1064 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); 1065 1066 return 0; 1067 } 1068 1069 1070 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s, 1071 u8 *go_dev_addr, 1072 const u8 *ssid, size_t ssid_len) 1073 { 1074 struct wpa_bss *bss; 1075 const u8 *bssid; 1076 struct wpabuf *p2p; 1077 u8 group_capab; 1078 const u8 *addr; 1079 1080 if (wpa_s->go_params) 1081 bssid = wpa_s->go_params->peer_interface_addr; 1082 else 1083 bssid = wpa_s->bssid; 1084 1085 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len); 1086 if (bss == NULL && wpa_s->go_params && 1087 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr)) 1088 bss = wpa_bss_get_p2p_dev_addr( 1089 wpa_s, wpa_s->go_params->peer_device_addr); 1090 if (bss == NULL) { 1091 u8 iface_addr[ETH_ALEN]; 1092 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid, 1093 iface_addr) == 0) 1094 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len); 1095 } 1096 if (bss == NULL) { 1097 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " 1098 "group is persistent - BSS " MACSTR " not found", 1099 MAC2STR(bssid)); 1100 return 0; 1101 } 1102 1103 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); 1104 if (p2p == NULL) 1105 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss, 1106 P2P_IE_VENDOR_TYPE); 1107 if (p2p == NULL) { 1108 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether " 1109 "group is persistent - BSS " MACSTR 1110 " did not include P2P IE", MAC2STR(bssid)); 1111 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs", 1112 wpa_bss_ie_ptr(bss), bss->ie_len); 1113 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs", 1114 wpa_bss_ie_ptr(bss) + bss->ie_len, 1115 bss->beacon_ie_len); 1116 return 0; 1117 } 1118 1119 group_capab = p2p_get_group_capab(p2p); 1120 addr = p2p_get_go_dev_addr(p2p); 1121 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: " 1122 "group_capab=0x%x", group_capab); 1123 if (addr) { 1124 os_memcpy(go_dev_addr, addr, ETH_ALEN); 1125 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR, 1126 MAC2STR(addr)); 1127 } else 1128 os_memset(go_dev_addr, 0, ETH_ALEN); 1129 wpabuf_free(p2p); 1130 1131 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x " 1132 "go_dev_addr=" MACSTR, 1133 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr)); 1134 1135 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP); 1136 } 1137 1138 1139 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s, 1140 struct wpa_ssid *ssid, 1141 const u8 *go_dev_addr) 1142 { 1143 struct wpa_ssid *s; 1144 int changed = 0; 1145 1146 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent " 1147 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr)); 1148 for (s = wpa_s->conf->ssid; s; s = s->next) { 1149 if (s->disabled == 2 && 1150 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 && 1151 s->ssid_len == ssid->ssid_len && 1152 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0) 1153 break; 1154 } 1155 1156 if (s) { 1157 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group " 1158 "entry"); 1159 if (ssid->passphrase && !s->passphrase) 1160 changed = 1; 1161 else if (ssid->passphrase && s->passphrase && 1162 os_strcmp(ssid->passphrase, s->passphrase) != 0) 1163 changed = 1; 1164 } else { 1165 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group " 1166 "entry"); 1167 changed = 1; 1168 s = wpa_config_add_network(wpa_s->conf); 1169 if (s == NULL) 1170 return -1; 1171 1172 /* 1173 * Instead of network_added we emit persistent_group_added 1174 * notification. Also to keep the defense checks in 1175 * persistent_group obj registration method, we set the 1176 * relevant flags in s to designate it as a persistent group. 1177 */ 1178 s->p2p_group = 1; 1179 s->p2p_persistent_group = 1; 1180 wpas_notify_persistent_group_added(wpa_s, s); 1181 wpa_config_set_network_defaults(s); 1182 } 1183 1184 s->p2p_group = 1; 1185 s->p2p_persistent_group = 1; 1186 s->disabled = 2; 1187 s->bssid_set = 1; 1188 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN); 1189 s->mode = ssid->mode; 1190 s->auth_alg = WPA_AUTH_ALG_OPEN; 1191 s->key_mgmt = WPA_KEY_MGMT_PSK; 1192 s->proto = WPA_PROTO_RSN; 1193 s->pbss = ssid->pbss; 1194 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP; 1195 s->export_keys = 1; 1196 if (ssid->passphrase) { 1197 os_free(s->passphrase); 1198 s->passphrase = os_strdup(ssid->passphrase); 1199 } 1200 if (ssid->psk_set) { 1201 s->psk_set = 1; 1202 os_memcpy(s->psk, ssid->psk, 32); 1203 } 1204 if (s->passphrase && !s->psk_set) 1205 wpa_config_update_psk(s); 1206 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) { 1207 os_free(s->ssid); 1208 s->ssid = os_malloc(ssid->ssid_len); 1209 } 1210 if (s->ssid) { 1211 s->ssid_len = ssid->ssid_len; 1212 os_memcpy(s->ssid, ssid->ssid, s->ssid_len); 1213 } 1214 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) { 1215 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list); 1216 wpa_s->global->add_psk = NULL; 1217 changed = 1; 1218 } 1219 1220 if (changed && wpa_s->conf->update_config && 1221 wpa_config_write(wpa_s->confname, wpa_s->conf)) { 1222 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 1223 } 1224 1225 return s->id; 1226 } 1227 1228 1229 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s, 1230 const u8 *addr) 1231 { 1232 struct wpa_ssid *ssid, *s; 1233 u8 *n; 1234 size_t i; 1235 int found = 0; 1236 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s; 1237 1238 ssid = wpa_s->current_ssid; 1239 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || 1240 !ssid->p2p_persistent_group) 1241 return; 1242 1243 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) { 1244 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) 1245 continue; 1246 1247 if (s->ssid_len == ssid->ssid_len && 1248 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0) 1249 break; 1250 } 1251 1252 if (s == NULL) 1253 return; 1254 1255 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) { 1256 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr, 1257 ETH_ALEN) != 0) 1258 continue; 1259 1260 if (i == s->num_p2p_clients - 1) 1261 return; /* already the most recent entry */ 1262 1263 /* move the entry to mark it most recent */ 1264 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN, 1265 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN, 1266 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN); 1267 os_memcpy(s->p2p_client_list + 1268 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr, 1269 ETH_ALEN); 1270 os_memset(s->p2p_client_list + 1271 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN, 1272 0xff, ETH_ALEN); 1273 found = 1; 1274 break; 1275 } 1276 1277 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) { 1278 n = os_realloc_array(s->p2p_client_list, 1279 s->num_p2p_clients + 1, 2 * ETH_ALEN); 1280 if (n == NULL) 1281 return; 1282 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr, 1283 ETH_ALEN); 1284 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN, 1285 0xff, ETH_ALEN); 1286 s->p2p_client_list = n; 1287 s->num_p2p_clients++; 1288 } else if (!found && s->p2p_client_list) { 1289 /* Not enough room for an additional entry - drop the oldest 1290 * entry */ 1291 os_memmove(s->p2p_client_list, 1292 s->p2p_client_list + 2 * ETH_ALEN, 1293 (s->num_p2p_clients - 1) * 2 * ETH_ALEN); 1294 os_memcpy(s->p2p_client_list + 1295 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, 1296 addr, ETH_ALEN); 1297 os_memset(s->p2p_client_list + 1298 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN, 1299 0xff, ETH_ALEN); 1300 } 1301 1302 if (p2p_wpa_s->conf->update_config && 1303 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf)) 1304 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 1305 } 1306 1307 1308 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s, 1309 int go, struct wpa_ssid *ssid, int freq, 1310 const u8 *psk, const char *passphrase, 1311 const u8 *go_dev_addr, int persistent, 1312 const char *extra) 1313 { 1314 const char *ssid_txt; 1315 char psk_txt[65]; 1316 1317 if (psk) 1318 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32); 1319 else 1320 psk_txt[0] = '\0'; 1321 1322 if (ssid) 1323 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len); 1324 else 1325 ssid_txt = ""; 1326 1327 if (passphrase && passphrase[0] == '\0') 1328 passphrase = NULL; 1329 1330 /* 1331 * Include PSK/passphrase only in the control interface message and 1332 * leave it out from the debug log entry. 1333 */ 1334 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO, 1335 P2P_EVENT_GROUP_STARTED 1336 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr=" 1337 MACSTR "%s%s", 1338 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, 1339 psk ? " psk=" : "", psk_txt, 1340 passphrase ? " passphrase=\"" : "", 1341 passphrase ? passphrase : "", 1342 passphrase ? "\"" : "", 1343 MAC2STR(go_dev_addr), 1344 persistent ? " [PERSISTENT]" : "", extra); 1345 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED 1346 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s", 1347 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq, 1348 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "", 1349 extra); 1350 } 1351 1352 1353 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s, 1354 int success, int already_deleted) 1355 { 1356 struct wpa_ssid *ssid; 1357 int client; 1358 int persistent; 1359 u8 go_dev_addr[ETH_ALEN]; 1360 1361 /* 1362 * This callback is likely called for the main interface. Update wpa_s 1363 * to use the group interface if a new interface was created for the 1364 * group. 1365 */ 1366 if (wpa_s->global->p2p_group_formation) 1367 wpa_s = wpa_s->global->p2p_group_formation; 1368 if (wpa_s->p2p_go_group_formation_completed) { 1369 wpa_s->global->p2p_group_formation = NULL; 1370 wpa_s->p2p_in_provisioning = 0; 1371 } else if (wpa_s->p2p_in_provisioning && !success) { 1372 wpa_msg(wpa_s, MSG_DEBUG, 1373 "P2P: Stop provisioning state due to failure"); 1374 wpa_s->p2p_in_provisioning = 0; 1375 } 1376 wpa_s->p2p_in_invitation = 0; 1377 wpa_s->group_formation_reported = 1; 1378 1379 if (!success) { 1380 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 1381 P2P_EVENT_GROUP_FORMATION_FAILURE); 1382 wpas_notify_p2p_group_formation_failure(wpa_s, ""); 1383 if (already_deleted) 1384 return; 1385 wpas_p2p_group_delete(wpa_s, 1386 P2P_GROUP_REMOVAL_FORMATION_FAILED); 1387 return; 1388 } 1389 1390 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 1391 P2P_EVENT_GROUP_FORMATION_SUCCESS); 1392 1393 ssid = wpa_s->current_ssid; 1394 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { 1395 ssid->mode = WPAS_MODE_P2P_GO; 1396 p2p_group_notif_formation_done(wpa_s->p2p_group); 1397 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL); 1398 } 1399 1400 persistent = 0; 1401 if (ssid) { 1402 client = ssid->mode == WPAS_MODE_INFRA; 1403 if (ssid->mode == WPAS_MODE_P2P_GO) { 1404 persistent = ssid->p2p_persistent_group; 1405 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr, 1406 ETH_ALEN); 1407 } else 1408 persistent = wpas_p2p_persistent_group(wpa_s, 1409 go_dev_addr, 1410 ssid->ssid, 1411 ssid->ssid_len); 1412 } else { 1413 client = wpa_s->p2p_group_interface == 1414 P2P_GROUP_INTERFACE_CLIENT; 1415 os_memset(go_dev_addr, 0, ETH_ALEN); 1416 } 1417 1418 wpa_s->show_group_started = 0; 1419 if (client) { 1420 /* 1421 * Indicate event only after successfully completed 4-way 1422 * handshake, i.e., when the interface is ready for data 1423 * packets. 1424 */ 1425 wpa_s->show_group_started = 1; 1426 } else { 1427 wpas_p2p_group_started(wpa_s, 1, ssid, 1428 ssid ? ssid->frequency : 0, 1429 ssid && ssid->passphrase == NULL && 1430 ssid->psk_set ? ssid->psk : NULL, 1431 ssid ? ssid->passphrase : NULL, 1432 go_dev_addr, persistent, ""); 1433 wpas_p2p_cross_connect_setup(wpa_s); 1434 wpas_p2p_set_group_idle_timeout(wpa_s); 1435 } 1436 1437 if (persistent) 1438 wpas_p2p_store_persistent_group(wpa_s->p2pdev, 1439 ssid, go_dev_addr); 1440 else { 1441 os_free(wpa_s->global->add_psk); 1442 wpa_s->global->add_psk = NULL; 1443 } 1444 1445 if (!client) { 1446 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL); 1447 os_get_reltime(&wpa_s->global->p2p_go_wait_client); 1448 } 1449 } 1450 1451 1452 struct send_action_work { 1453 unsigned int freq; 1454 u8 dst[ETH_ALEN]; 1455 u8 src[ETH_ALEN]; 1456 u8 bssid[ETH_ALEN]; 1457 size_t len; 1458 unsigned int wait_time; 1459 u8 buf[0]; 1460 }; 1461 1462 1463 static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s) 1464 { 1465 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx; 1466 1467 wpa_printf(MSG_DEBUG, 1468 "P2P: Free Action frame radio work @%p (freq=%u dst=" 1469 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)", 1470 wpa_s->p2p_send_action_work, awork->freq, 1471 MAC2STR(awork->dst), MAC2STR(awork->src), 1472 MAC2STR(awork->bssid), awork->wait_time); 1473 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame", 1474 awork->buf, awork->len); 1475 os_free(awork); 1476 wpa_s->p2p_send_action_work->ctx = NULL; 1477 radio_work_done(wpa_s->p2p_send_action_work); 1478 wpa_s->p2p_send_action_work = NULL; 1479 } 1480 1481 1482 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx, 1483 void *timeout_ctx) 1484 { 1485 struct wpa_supplicant *wpa_s = eloop_ctx; 1486 1487 if (!wpa_s->p2p_send_action_work) 1488 return; 1489 1490 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out"); 1491 wpas_p2p_free_send_action_work(wpa_s); 1492 } 1493 1494 1495 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s) 1496 { 1497 if (wpa_s->p2p_send_action_work) { 1498 struct send_action_work *awork; 1499 1500 awork = wpa_s->p2p_send_action_work->ctx; 1501 wpa_printf(MSG_DEBUG, 1502 "P2P: Clear Action TX work @%p (wait_time=%u)", 1503 wpa_s->p2p_send_action_work, awork->wait_time); 1504 if (awork->wait_time == 0) { 1505 wpas_p2p_free_send_action_work(wpa_s); 1506 } else { 1507 /* 1508 * In theory, this should not be needed, but number of 1509 * places in the P2P code is still using non-zero wait 1510 * time for the last Action frame in the sequence and 1511 * some of these do not call send_action_done(). 1512 */ 1513 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, 1514 wpa_s, NULL); 1515 eloop_register_timeout( 1516 0, awork->wait_time * 1000, 1517 wpas_p2p_send_action_work_timeout, 1518 wpa_s, NULL); 1519 } 1520 } 1521 } 1522 1523 1524 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s, 1525 unsigned int freq, 1526 const u8 *dst, const u8 *src, 1527 const u8 *bssid, 1528 const u8 *data, size_t data_len, 1529 enum offchannel_send_action_result 1530 result) 1531 { 1532 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS; 1533 1534 wpas_p2p_action_tx_clear(wpa_s); 1535 1536 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) 1537 return; 1538 1539 switch (result) { 1540 case OFFCHANNEL_SEND_ACTION_SUCCESS: 1541 res = P2P_SEND_ACTION_SUCCESS; 1542 break; 1543 case OFFCHANNEL_SEND_ACTION_NO_ACK: 1544 res = P2P_SEND_ACTION_NO_ACK; 1545 break; 1546 case OFFCHANNEL_SEND_ACTION_FAILED: 1547 res = P2P_SEND_ACTION_FAILED; 1548 break; 1549 } 1550 1551 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res); 1552 1553 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS && 1554 wpa_s->pending_pd_before_join && 1555 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || 1556 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) && 1557 wpa_s->p2p_fallback_to_go_neg) { 1558 wpa_s->pending_pd_before_join = 0; 1559 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req " 1560 "during p2p_connect-auto"); 1561 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 1562 P2P_EVENT_FALLBACK_TO_GO_NEG 1563 "reason=no-ACK-to-PD-Req"); 1564 wpas_p2p_fallback_to_go_neg(wpa_s, 0); 1565 return; 1566 } 1567 } 1568 1569 1570 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit) 1571 { 1572 struct wpa_supplicant *wpa_s = work->wpa_s; 1573 struct send_action_work *awork = work->ctx; 1574 1575 if (deinit) { 1576 if (work->started) { 1577 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, 1578 wpa_s, NULL); 1579 wpa_s->p2p_send_action_work = NULL; 1580 offchannel_send_action_done(wpa_s); 1581 } 1582 os_free(awork); 1583 return; 1584 } 1585 1586 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src, 1587 awork->bssid, awork->buf, awork->len, 1588 awork->wait_time, 1589 wpas_p2p_send_action_tx_status, 1) < 0) { 1590 os_free(awork); 1591 radio_work_done(work); 1592 return; 1593 } 1594 wpa_s->p2p_send_action_work = work; 1595 } 1596 1597 1598 static int wpas_send_action_work(struct wpa_supplicant *wpa_s, 1599 unsigned int freq, const u8 *dst, 1600 const u8 *src, const u8 *bssid, const u8 *buf, 1601 size_t len, unsigned int wait_time) 1602 { 1603 struct send_action_work *awork; 1604 1605 if (radio_work_pending(wpa_s, "p2p-send-action")) { 1606 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending"); 1607 return -1; 1608 } 1609 1610 awork = os_zalloc(sizeof(*awork) + len); 1611 if (awork == NULL) 1612 return -1; 1613 1614 awork->freq = freq; 1615 os_memcpy(awork->dst, dst, ETH_ALEN); 1616 os_memcpy(awork->src, src, ETH_ALEN); 1617 os_memcpy(awork->bssid, bssid, ETH_ALEN); 1618 awork->len = len; 1619 awork->wait_time = wait_time; 1620 os_memcpy(awork->buf, buf, len); 1621 1622 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1, 1623 wpas_send_action_cb, awork) < 0) { 1624 os_free(awork); 1625 return -1; 1626 } 1627 1628 return 0; 1629 } 1630 1631 1632 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst, 1633 const u8 *src, const u8 *bssid, const u8 *buf, 1634 size_t len, unsigned int wait_time, int *scheduled) 1635 { 1636 struct wpa_supplicant *wpa_s = ctx; 1637 int listen_freq = -1, send_freq = -1; 1638 1639 if (scheduled) 1640 *scheduled = 0; 1641 if (wpa_s->p2p_listen_work) 1642 listen_freq = wpa_s->p2p_listen_work->freq; 1643 if (wpa_s->p2p_send_action_work) 1644 send_freq = wpa_s->p2p_send_action_work->freq; 1645 if (listen_freq != (int) freq && send_freq != (int) freq) { 1646 int res; 1647 1648 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)", 1649 listen_freq, send_freq, freq); 1650 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf, 1651 len, wait_time); 1652 if (res == 0 && scheduled) 1653 *scheduled = 1; 1654 return res; 1655 } 1656 1657 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX"); 1658 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len, 1659 wait_time, 1660 wpas_p2p_send_action_tx_status, 1); 1661 } 1662 1663 1664 static void wpas_send_action_done(void *ctx) 1665 { 1666 struct wpa_supplicant *wpa_s = ctx; 1667 1668 if (wpa_s->p2p_send_action_work) { 1669 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, 1670 wpa_s, NULL); 1671 os_free(wpa_s->p2p_send_action_work->ctx); 1672 radio_work_done(wpa_s->p2p_send_action_work); 1673 wpa_s->p2p_send_action_work = NULL; 1674 } 1675 1676 offchannel_send_action_done(wpa_s); 1677 } 1678 1679 1680 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s, 1681 struct p2p_go_neg_results *params) 1682 { 1683 if (wpa_s->go_params == NULL) { 1684 wpa_s->go_params = os_malloc(sizeof(*params)); 1685 if (wpa_s->go_params == NULL) 1686 return -1; 1687 } 1688 os_memcpy(wpa_s->go_params, params, sizeof(*params)); 1689 return 0; 1690 } 1691 1692 1693 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s, 1694 struct p2p_go_neg_results *res) 1695 { 1696 wpa_s->group_formation_reported = 0; 1697 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR 1698 " dev_addr " MACSTR " wps_method %d", 1699 MAC2STR(res->peer_interface_addr), 1700 MAC2STR(res->peer_device_addr), res->wps_method); 1701 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID", 1702 res->ssid, res->ssid_len); 1703 wpa_supplicant_ap_deinit(wpa_s); 1704 wpas_copy_go_neg_results(wpa_s, res); 1705 if (res->wps_method == WPS_PBC) { 1706 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0); 1707 #ifdef CONFIG_WPS_NFC 1708 } else if (res->wps_method == WPS_NFC) { 1709 wpas_wps_start_nfc(wpa_s, res->peer_device_addr, 1710 res->peer_interface_addr, 1711 wpa_s->p2pdev->p2p_oob_dev_pw, 1712 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1, 1713 wpa_s->p2pdev->p2p_oob_dev_pw_id == 1714 DEV_PW_NFC_CONNECTION_HANDOVER ? 1715 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : 1716 NULL, 1717 NULL, 0, 0); 1718 #endif /* CONFIG_WPS_NFC */ 1719 } else { 1720 u16 dev_pw_id = DEV_PW_DEFAULT; 1721 if (wpa_s->p2p_wps_method == WPS_P2PS) 1722 dev_pw_id = DEV_PW_P2PS_DEFAULT; 1723 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD) 1724 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED; 1725 wpas_wps_start_pin(wpa_s, res->peer_interface_addr, 1726 wpa_s->p2p_pin, 1, dev_pw_id); 1727 } 1728 } 1729 1730 1731 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s, 1732 struct wpa_ssid *ssid) 1733 { 1734 struct wpa_ssid *persistent; 1735 struct psk_list_entry *psk; 1736 struct hostapd_data *hapd; 1737 1738 if (!wpa_s->ap_iface) 1739 return; 1740 1741 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid, 1742 ssid->ssid_len); 1743 if (persistent == NULL) 1744 return; 1745 1746 hapd = wpa_s->ap_iface->bss[0]; 1747 1748 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry, 1749 list) { 1750 struct hostapd_wpa_psk *hpsk; 1751 1752 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for " 1753 MACSTR " psk=%d", 1754 MAC2STR(psk->addr), psk->p2p); 1755 hpsk = os_zalloc(sizeof(*hpsk)); 1756 if (hpsk == NULL) 1757 break; 1758 os_memcpy(hpsk->psk, psk->psk, PMK_LEN); 1759 if (psk->p2p) 1760 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN); 1761 else 1762 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN); 1763 hpsk->next = hapd->conf->ssid.wpa_psk; 1764 hapd->conf->ssid.wpa_psk = hpsk; 1765 } 1766 } 1767 1768 1769 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s) 1770 { 1771 char buf[20 + P2P_MAX_CHANNELS * 6]; 1772 char *pos, *end; 1773 unsigned int i; 1774 int res; 1775 1776 pos = buf; 1777 end = pos + sizeof(buf); 1778 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { 1779 res = os_snprintf(pos, end - pos, " %d", 1780 wpa_s->p2p_group_common_freqs[i]); 1781 if (os_snprintf_error(end - pos, res)) 1782 break; 1783 pos += res; 1784 } 1785 *pos = '\0'; 1786 1787 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf); 1788 } 1789 1790 1791 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s, 1792 struct p2p_go_neg_results *params) 1793 { 1794 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list); 1795 1796 wpa_s->p2p_group_common_freqs_num = 0; 1797 os_free(wpa_s->p2p_group_common_freqs); 1798 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int)); 1799 if (!wpa_s->p2p_group_common_freqs) 1800 return; 1801 1802 for (i = 0; i < len; i++) { 1803 if (!wpa_s->go_params->freq_list[i]) 1804 break; 1805 wpa_s->p2p_group_common_freqs[i] = 1806 wpa_s->go_params->freq_list[i]; 1807 } 1808 wpa_s->p2p_group_common_freqs_num = i; 1809 } 1810 1811 1812 static void p2p_config_write(struct wpa_supplicant *wpa_s) 1813 { 1814 #ifndef CONFIG_NO_CONFIG_WRITE 1815 if (wpa_s->p2pdev->conf->update_config && 1816 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf)) 1817 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 1818 #endif /* CONFIG_NO_CONFIG_WRITE */ 1819 } 1820 1821 1822 static void p2p_go_configured(void *ctx, void *data) 1823 { 1824 struct wpa_supplicant *wpa_s = ctx; 1825 struct p2p_go_neg_results *params = data; 1826 struct wpa_ssid *ssid; 1827 1828 wpa_s->ap_configured_cb = NULL; 1829 wpa_s->ap_configured_cb_ctx = NULL; 1830 wpa_s->ap_configured_cb_data = NULL; 1831 if (!wpa_s->go_params) { 1832 wpa_printf(MSG_ERROR, 1833 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL"); 1834 return; 1835 } 1836 1837 p2p_go_save_group_common_freqs(wpa_s, params); 1838 p2p_go_dump_common_freqs(wpa_s); 1839 1840 ssid = wpa_s->current_ssid; 1841 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) { 1842 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning"); 1843 if (wpa_s->global->p2p_group_formation == wpa_s) 1844 wpa_s->global->p2p_group_formation = NULL; 1845 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency, 1846 params->passphrase[0] == '\0' ? 1847 params->psk : NULL, 1848 params->passphrase, 1849 wpa_s->global->p2p_dev_addr, 1850 params->persistent_group, ""); 1851 wpa_s->group_formation_reported = 1; 1852 1853 if (wpa_s->p2pdev->p2ps_method_config_any) { 1854 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) { 1855 wpa_dbg(wpa_s, MSG_DEBUG, 1856 "P2PS: Setting default PIN for ANY"); 1857 wpa_supplicant_ap_wps_pin(wpa_s, NULL, 1858 "12345670", NULL, 0, 1859 0); 1860 } else { 1861 wpa_dbg(wpa_s, MSG_DEBUG, 1862 "P2PS: Setting default PIN for " MACSTR, 1863 MAC2STR(wpa_s->p2pdev->p2ps_join_addr)); 1864 wpa_supplicant_ap_wps_pin( 1865 wpa_s, wpa_s->p2pdev->p2ps_join_addr, 1866 "12345670", NULL, 0, 0); 1867 } 1868 wpa_s->p2pdev->p2ps_method_config_any = 0; 1869 } 1870 1871 os_get_reltime(&wpa_s->global->p2p_go_wait_client); 1872 if (params->persistent_group) { 1873 wpas_p2p_store_persistent_group( 1874 wpa_s->p2pdev, ssid, 1875 wpa_s->global->p2p_dev_addr); 1876 wpas_p2p_add_psk_list(wpa_s, ssid); 1877 } 1878 1879 wpas_notify_p2p_group_started(wpa_s, ssid, 1880 params->persistent_group, 0, 1881 NULL); 1882 wpas_p2p_cross_connect_setup(wpa_s); 1883 wpas_p2p_set_group_idle_timeout(wpa_s); 1884 1885 if (wpa_s->p2p_first_connection_timeout) { 1886 wpa_dbg(wpa_s, MSG_DEBUG, 1887 "P2P: Start group formation timeout of %d seconds until first data connection on GO", 1888 wpa_s->p2p_first_connection_timeout); 1889 wpa_s->p2p_go_group_formation_completed = 0; 1890 wpa_s->global->p2p_group_formation = wpa_s; 1891 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 1892 wpa_s->p2pdev, NULL); 1893 eloop_register_timeout( 1894 wpa_s->p2p_first_connection_timeout, 0, 1895 wpas_p2p_group_formation_timeout, 1896 wpa_s->p2pdev, NULL); 1897 } 1898 1899 return; 1900 } 1901 1902 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning"); 1903 if (wpa_supplicant_ap_mac_addr_filter(wpa_s, 1904 params->peer_interface_addr)) { 1905 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address " 1906 "filtering"); 1907 return; 1908 } 1909 if (params->wps_method == WPS_PBC) { 1910 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr, 1911 params->peer_device_addr); 1912 #ifdef CONFIG_WPS_NFC 1913 } else if (params->wps_method == WPS_NFC) { 1914 if (wpa_s->p2pdev->p2p_oob_dev_pw_id != 1915 DEV_PW_NFC_CONNECTION_HANDOVER && 1916 !wpa_s->p2pdev->p2p_oob_dev_pw) { 1917 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); 1918 return; 1919 } 1920 wpas_ap_wps_add_nfc_pw( 1921 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id, 1922 wpa_s->p2pdev->p2p_oob_dev_pw, 1923 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ? 1924 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL); 1925 #endif /* CONFIG_WPS_NFC */ 1926 } else if (wpa_s->p2p_pin[0]) 1927 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr, 1928 wpa_s->p2p_pin, NULL, 0, 0); 1929 os_free(wpa_s->go_params); 1930 wpa_s->go_params = NULL; 1931 } 1932 1933 1934 /** 1935 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel 1936 * @freq: Frequency (MHz) to convert 1937 * @op_class: Buffer for returning operating class 1938 * @op_edmg_channel: Buffer for returning channel number 1939 * Returns: 0 on success, -1 on failure 1940 * 1941 * This can be used to find the highest channel bonding which includes the 1942 * specified frequency. 1943 */ 1944 static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s, 1945 unsigned int freq, 1946 u8 *op_class, u8 *op_edmg_channel) 1947 { 1948 struct hostapd_hw_modes *hwmode; 1949 struct ieee80211_edmg_config edmg; 1950 unsigned int i; 1951 enum chan_width chanwidth[] = { 1952 CHAN_WIDTH_8640, 1953 CHAN_WIDTH_6480, 1954 CHAN_WIDTH_4320, 1955 }; 1956 1957 if (!wpa_s->hw.modes) 1958 return -1; 1959 1960 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, 1961 HOSTAPD_MODE_IEEE80211AD, false); 1962 if (!hwmode) { 1963 wpa_printf(MSG_ERROR, 1964 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD"); 1965 return -1; 1966 } 1967 1968 /* Find the highest EDMG channel bandwidth to start the P2P GO */ 1969 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) { 1970 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0, 1971 op_class, 1972 op_edmg_channel) < 0) 1973 continue; 1974 1975 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg); 1976 if (edmg.channels && 1977 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) { 1978 wpa_printf(MSG_DEBUG, 1979 "Freq %u to EDMG channel %u at opclass %u", 1980 freq, *op_edmg_channel, *op_class); 1981 return 0; 1982 } 1983 } 1984 1985 return -1; 1986 } 1987 1988 1989 int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s, 1990 struct p2p_go_neg_results *params) 1991 { 1992 u8 op_channel, op_class; 1993 int freq; 1994 1995 /* Try social channel as primary channel frequency */ 1996 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq; 1997 1998 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class, 1999 &op_channel) == 0) { 2000 wpa_printf(MSG_DEBUG, 2001 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)", 2002 freq, op_channel, op_class); 2003 params->freq = freq; 2004 return 0; 2005 } 2006 2007 return -1; 2008 } 2009 2010 2011 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s, 2012 struct p2p_go_neg_results *params, 2013 int group_formation) 2014 { 2015 struct wpa_ssid *ssid; 2016 2017 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO"); 2018 if (wpas_copy_go_neg_results(wpa_s, params) < 0) { 2019 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation " 2020 "results"); 2021 return; 2022 } 2023 2024 ssid = wpa_config_add_network(wpa_s->conf); 2025 if (ssid == NULL) { 2026 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO"); 2027 return; 2028 } 2029 2030 wpa_s->show_group_started = 0; 2031 wpa_s->p2p_go_group_formation_completed = 0; 2032 wpa_s->group_formation_reported = 0; 2033 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); 2034 2035 wpa_config_set_network_defaults(ssid); 2036 ssid->temporary = 1; 2037 ssid->p2p_group = 1; 2038 ssid->p2p_persistent_group = !!params->persistent_group; 2039 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION : 2040 WPAS_MODE_P2P_GO; 2041 ssid->frequency = params->freq; 2042 ssid->ht40 = params->ht40; 2043 ssid->vht = params->vht; 2044 ssid->max_oper_chwidth = params->max_oper_chwidth; 2045 ssid->vht_center_freq2 = params->vht_center_freq2; 2046 ssid->he = params->he; 2047 if (params->edmg) { 2048 u8 op_channel, op_class; 2049 2050 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq, 2051 &op_class, &op_channel)) { 2052 ssid->edmg_channel = op_channel; 2053 ssid->enable_edmg = params->edmg; 2054 } else { 2055 wpa_dbg(wpa_s, MSG_DEBUG, 2056 "P2P: Could not match EDMG channel, freq %d, for GO", 2057 params->freq); 2058 } 2059 } 2060 2061 ssid->ssid = os_zalloc(params->ssid_len + 1); 2062 if (ssid->ssid) { 2063 os_memcpy(ssid->ssid, params->ssid, params->ssid_len); 2064 ssid->ssid_len = params->ssid_len; 2065 } 2066 ssid->auth_alg = WPA_AUTH_ALG_OPEN; 2067 ssid->key_mgmt = WPA_KEY_MGMT_PSK; 2068 if (is_6ghz_freq(ssid->frequency) && 2069 is_p2p_6ghz_capable(wpa_s->global->p2p)) { 2070 ssid->auth_alg |= WPA_AUTH_ALG_SAE; 2071 ssid->key_mgmt = WPA_KEY_MGMT_SAE; 2072 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt"); 2073 } else { 2074 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false); 2075 } 2076 ssid->proto = WPA_PROTO_RSN; 2077 ssid->pairwise_cipher = WPA_CIPHER_CCMP; 2078 ssid->group_cipher = WPA_CIPHER_CCMP; 2079 if (params->freq > 56160) { 2080 /* 2081 * Enable GCMP instead of CCMP as pairwise_cipher and 2082 * group_cipher in 60 GHz. 2083 */ 2084 ssid->pairwise_cipher = WPA_CIPHER_GCMP; 2085 ssid->group_cipher = WPA_CIPHER_GCMP; 2086 /* P2P GO in 60 GHz is always a PCP (PBSS) */ 2087 ssid->pbss = 1; 2088 } 2089 if (os_strlen(params->passphrase) > 0) { 2090 ssid->passphrase = os_strdup(params->passphrase); 2091 if (ssid->passphrase == NULL) { 2092 wpa_msg_global(wpa_s, MSG_ERROR, 2093 "P2P: Failed to copy passphrase for GO"); 2094 wpa_config_remove_network(wpa_s->conf, ssid->id); 2095 return; 2096 } 2097 } else 2098 ssid->passphrase = NULL; 2099 ssid->psk_set = params->psk_set; 2100 if (ssid->psk_set) 2101 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk)); 2102 else if (ssid->passphrase) 2103 wpa_config_update_psk(ssid); 2104 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity; 2105 2106 wpa_s->ap_configured_cb = p2p_go_configured; 2107 wpa_s->ap_configured_cb_ctx = wpa_s; 2108 wpa_s->ap_configured_cb_data = wpa_s->go_params; 2109 wpa_s->scan_req = NORMAL_SCAN_REQ; 2110 wpa_s->connect_without_scan = ssid; 2111 wpa_s->reassociate = 1; 2112 wpa_s->disconnected = 0; 2113 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to " 2114 "start GO)"); 2115 wpa_supplicant_req_scan(wpa_s, 0, 0); 2116 } 2117 2118 2119 static void wpas_p2p_clone_config(struct wpa_supplicant *dst, 2120 const struct wpa_supplicant *src) 2121 { 2122 struct wpa_config *d; 2123 const struct wpa_config *s; 2124 2125 d = dst->conf; 2126 s = src->conf; 2127 2128 #define C(n) \ 2129 do { \ 2130 if (s->n && !d->n) \ 2131 d->n = os_strdup(s->n); \ 2132 } while (0) 2133 2134 C(device_name); 2135 C(manufacturer); 2136 C(model_name); 2137 C(model_number); 2138 C(serial_number); 2139 C(config_methods); 2140 #undef C 2141 2142 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN); 2143 os_memcpy(d->sec_device_type, s->sec_device_type, 2144 sizeof(d->sec_device_type)); 2145 d->num_sec_device_types = s->num_sec_device_types; 2146 2147 d->p2p_group_idle = s->p2p_group_idle; 2148 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy; 2149 d->p2p_intra_bss = s->p2p_intra_bss; 2150 d->persistent_reconnect = s->persistent_reconnect; 2151 d->max_num_sta = s->max_num_sta; 2152 d->pbc_in_m1 = s->pbc_in_m1; 2153 d->ignore_old_scan_res = s->ignore_old_scan_res; 2154 d->beacon_int = s->beacon_int; 2155 d->dtim_period = s->dtim_period; 2156 d->p2p_go_ctwindow = s->p2p_go_ctwindow; 2157 d->disassoc_low_ack = s->disassoc_low_ack; 2158 d->disable_scan_offload = s->disable_scan_offload; 2159 d->passive_scan = s->passive_scan; 2160 2161 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey && 2162 !d->wps_nfc_pw_from_config) { 2163 wpabuf_free(d->wps_nfc_dh_privkey); 2164 wpabuf_free(d->wps_nfc_dh_pubkey); 2165 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey); 2166 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey); 2167 } 2168 d->p2p_cli_probe = s->p2p_cli_probe; 2169 d->go_interworking = s->go_interworking; 2170 d->go_access_network_type = s->go_access_network_type; 2171 d->go_internet = s->go_internet; 2172 d->go_venue_group = s->go_venue_group; 2173 d->go_venue_type = s->go_venue_type; 2174 d->p2p_add_cli_chan = s->p2p_add_cli_chan; 2175 } 2176 2177 2178 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s, 2179 char *ifname, size_t len) 2180 { 2181 char *ifname_ptr = wpa_s->ifname; 2182 2183 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX, 2184 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { 2185 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1; 2186 } 2187 2188 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx); 2189 if (os_strlen(ifname) >= IFNAMSIZ && 2190 os_strlen(wpa_s->ifname) < IFNAMSIZ) { 2191 int res; 2192 2193 /* Try to avoid going over the IFNAMSIZ length limit */ 2194 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx); 2195 if (os_snprintf_error(len, res) && len) 2196 ifname[len - 1] = '\0'; 2197 } 2198 } 2199 2200 2201 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s, 2202 enum wpa_driver_if_type type) 2203 { 2204 char ifname[120], force_ifname[120]; 2205 2206 if (wpa_s->pending_interface_name[0]) { 2207 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists " 2208 "- skip creation of a new one"); 2209 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) { 2210 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address " 2211 "unknown?! ifname='%s'", 2212 wpa_s->pending_interface_name); 2213 return -1; 2214 } 2215 return 0; 2216 } 2217 2218 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname)); 2219 force_ifname[0] = '\0'; 2220 2221 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group", 2222 ifname); 2223 wpa_s->p2p_group_idx++; 2224 2225 wpa_s->pending_interface_type = type; 2226 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname, 2227 wpa_s->pending_interface_addr, NULL) < 0) { 2228 wpa_printf(MSG_ERROR, "P2P: Failed to create new group " 2229 "interface"); 2230 return -1; 2231 } 2232 2233 if (wpa_s->conf->p2p_interface_random_mac_addr) { 2234 random_mac_addr(wpa_s->pending_interface_addr); 2235 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR 2236 " for the group", 2237 MAC2STR(wpa_s->pending_interface_addr)); 2238 } 2239 2240 if (force_ifname[0]) { 2241 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s", 2242 force_ifname); 2243 os_strlcpy(wpa_s->pending_interface_name, force_ifname, 2244 sizeof(wpa_s->pending_interface_name)); 2245 } else 2246 os_strlcpy(wpa_s->pending_interface_name, ifname, 2247 sizeof(wpa_s->pending_interface_name)); 2248 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr " 2249 MACSTR, wpa_s->pending_interface_name, 2250 MAC2STR(wpa_s->pending_interface_addr)); 2251 2252 return 0; 2253 } 2254 2255 2256 static void wpas_p2p_remove_pending_group_interface( 2257 struct wpa_supplicant *wpa_s) 2258 { 2259 if (!wpa_s->pending_interface_name[0] || 2260 is_zero_ether_addr(wpa_s->pending_interface_addr)) 2261 return; /* No pending virtual interface */ 2262 2263 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s", 2264 wpa_s->pending_interface_name); 2265 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type, 2266 wpa_s->pending_interface_name); 2267 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); 2268 wpa_s->pending_interface_name[0] = '\0'; 2269 wpa_s->global->pending_group_iface_for_p2ps = 0; 2270 } 2271 2272 2273 static struct wpa_supplicant * 2274 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go) 2275 { 2276 struct wpa_interface iface; 2277 struct wpa_supplicant *group_wpa_s; 2278 2279 if (!wpa_s->pending_interface_name[0]) { 2280 wpa_printf(MSG_ERROR, "P2P: No pending group interface"); 2281 if (!wpas_p2p_create_iface(wpa_s)) 2282 return NULL; 2283 /* 2284 * Something has forced us to remove the pending interface; try 2285 * to create a new one and hope for the best that we will get 2286 * the same local address. 2287 */ 2288 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : 2289 WPA_IF_P2P_CLIENT) < 0) 2290 return NULL; 2291 } 2292 2293 os_memset(&iface, 0, sizeof(iface)); 2294 iface.ifname = wpa_s->pending_interface_name; 2295 iface.driver = wpa_s->driver->name; 2296 if (wpa_s->conf->ctrl_interface == NULL && 2297 wpa_s->parent != wpa_s && 2298 wpa_s->p2p_mgmt && 2299 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) 2300 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface; 2301 else 2302 iface.ctrl_interface = wpa_s->conf->ctrl_interface; 2303 iface.driver_param = wpa_s->conf->driver_param; 2304 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s); 2305 if (group_wpa_s == NULL) { 2306 wpa_printf(MSG_ERROR, "P2P: Failed to create new " 2307 "wpa_supplicant interface"); 2308 return NULL; 2309 } 2310 wpa_s->pending_interface_name[0] = '\0'; 2311 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO : 2312 P2P_GROUP_INTERFACE_CLIENT; 2313 wpa_s->global->p2p_group_formation = group_wpa_s; 2314 wpa_s->global->pending_group_iface_for_p2ps = 0; 2315 2316 wpas_p2p_clone_config(group_wpa_s, wpa_s); 2317 2318 if (wpa_s->conf->p2p_interface_random_mac_addr) { 2319 if (wpa_drv_set_mac_addr(group_wpa_s, 2320 wpa_s->pending_interface_addr) < 0) { 2321 wpa_msg(group_wpa_s, MSG_INFO, 2322 "Failed to set random MAC address"); 2323 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 2324 0); 2325 return NULL; 2326 } 2327 2328 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) { 2329 wpa_msg(group_wpa_s, MSG_INFO, 2330 "Could not update MAC address information"); 2331 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 2332 0); 2333 return NULL; 2334 } 2335 2336 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR 2337 " for the group", 2338 MAC2STR(wpa_s->pending_interface_addr)); 2339 } 2340 2341 return group_wpa_s; 2342 } 2343 2344 2345 static void wpas_p2p_group_formation_timeout(void *eloop_ctx, 2346 void *timeout_ctx) 2347 { 2348 struct wpa_supplicant *wpa_s = eloop_ctx; 2349 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out"); 2350 wpas_p2p_group_formation_failed(wpa_s, 0); 2351 } 2352 2353 2354 static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s, 2355 int already_deleted) 2356 { 2357 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 2358 wpa_s->p2pdev, NULL); 2359 if (wpa_s->global->p2p) 2360 p2p_group_formation_failed(wpa_s->global->p2p); 2361 wpas_group_formation_completed(wpa_s, 0, already_deleted); 2362 } 2363 2364 2365 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s) 2366 { 2367 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure"); 2368 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 2369 wpa_s->p2pdev, NULL); 2370 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, 2371 wpa_s->p2pdev, NULL); 2372 wpa_s->global->p2p_fail_on_wps_complete = 0; 2373 } 2374 2375 2376 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s) 2377 { 2378 if (wpa_s->global->p2p_group_formation != wpa_s) 2379 return; 2380 /* Speed up group formation timeout since this cannot succeed */ 2381 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 2382 wpa_s->p2pdev, NULL); 2383 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, 2384 wpa_s->p2pdev, NULL); 2385 } 2386 2387 2388 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res) 2389 { 2390 struct wpa_supplicant *wpa_s = ctx; 2391 struct wpa_supplicant *group_wpa_s; 2392 2393 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { 2394 wpa_drv_cancel_remain_on_channel(wpa_s); 2395 wpa_s->off_channel_freq = 0; 2396 wpa_s->roc_waiting_drv_freq = 0; 2397 } 2398 2399 if (res->status) { 2400 wpa_msg_global(wpa_s, MSG_INFO, 2401 P2P_EVENT_GO_NEG_FAILURE "status=%d", 2402 res->status); 2403 wpas_notify_p2p_go_neg_completed(wpa_s, res); 2404 wpas_p2p_remove_pending_group_interface(wpa_s); 2405 return; 2406 } 2407 2408 if (!res->role_go) { 2409 /* Inform driver of the operating channel of GO. */ 2410 wpa_drv_set_prob_oper_freq(wpa_s, res->freq); 2411 } 2412 2413 if (wpa_s->p2p_go_ht40) 2414 res->ht40 = 1; 2415 if (wpa_s->p2p_go_vht) 2416 res->vht = 1; 2417 if (wpa_s->p2p_go_he) 2418 res->he = 1; 2419 if (wpa_s->p2p_go_edmg) 2420 res->edmg = 1; 2421 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth; 2422 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2; 2423 2424 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s " 2425 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR 2426 " wps_method=%s", 2427 res->role_go ? "GO" : "client", res->freq, res->ht40, 2428 MAC2STR(res->peer_device_addr), 2429 MAC2STR(res->peer_interface_addr), 2430 p2p_wps_method_text(res->wps_method)); 2431 wpas_notify_p2p_go_neg_completed(wpa_s, res); 2432 2433 if (res->role_go && wpa_s->p2p_persistent_id >= 0) { 2434 struct wpa_ssid *ssid; 2435 ssid = wpa_config_get_network(wpa_s->conf, 2436 wpa_s->p2p_persistent_id); 2437 if (ssid && ssid->disabled == 2 && 2438 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) { 2439 size_t len = os_strlen(ssid->passphrase); 2440 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based " 2441 "on requested persistent group"); 2442 os_memcpy(res->passphrase, ssid->passphrase, len); 2443 res->passphrase[len] = '\0'; 2444 } 2445 } 2446 2447 if (wpa_s->create_p2p_iface) { 2448 group_wpa_s = 2449 wpas_p2p_init_group_interface(wpa_s, res->role_go); 2450 if (group_wpa_s == NULL) { 2451 wpas_p2p_remove_pending_group_interface(wpa_s); 2452 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, 2453 wpa_s, NULL); 2454 wpas_p2p_group_formation_failed(wpa_s, 1); 2455 return; 2456 } 2457 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN); 2458 wpa_s->pending_interface_name[0] = '\0'; 2459 } else { 2460 group_wpa_s = wpa_s->parent; 2461 wpa_s->global->p2p_group_formation = group_wpa_s; 2462 if (group_wpa_s != wpa_s) 2463 wpas_p2p_clone_config(group_wpa_s, wpa_s); 2464 } 2465 2466 group_wpa_s->p2p_in_provisioning = 1; 2467 group_wpa_s->p2pdev = wpa_s; 2468 if (group_wpa_s != wpa_s) { 2469 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin, 2470 sizeof(group_wpa_s->p2p_pin)); 2471 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method; 2472 } 2473 if (res->role_go) { 2474 wpas_start_wps_go(group_wpa_s, res, 1); 2475 } else { 2476 os_get_reltime(&group_wpa_s->scan_min_time); 2477 wpas_start_wps_enrollee(group_wpa_s, res); 2478 } 2479 2480 wpa_s->global->p2p_long_listen = 0; 2481 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 2482 2483 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); 2484 eloop_register_timeout(15 + res->peer_config_timeout / 100, 2485 (res->peer_config_timeout % 100) * 10000, 2486 wpas_p2p_group_formation_timeout, wpa_s, NULL); 2487 } 2488 2489 2490 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id, 2491 u8 go_intent) 2492 { 2493 struct wpa_supplicant *wpa_s = ctx; 2494 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR 2495 " dev_passwd_id=%u go_intent=%u", MAC2STR(src), 2496 dev_passwd_id, go_intent); 2497 2498 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent); 2499 } 2500 2501 2502 static void wpas_dev_found(void *ctx, const u8 *addr, 2503 const struct p2p_peer_info *info, 2504 int new_device) 2505 { 2506 #ifndef CONFIG_NO_STDOUT_DEBUG 2507 struct wpa_supplicant *wpa_s = ctx; 2508 char devtype[WPS_DEV_TYPE_BUFSIZE]; 2509 char *wfd_dev_info_hex = NULL; 2510 2511 #ifdef CONFIG_WIFI_DISPLAY 2512 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems, 2513 WFD_SUBELEM_DEVICE_INFO); 2514 #endif /* CONFIG_WIFI_DISPLAY */ 2515 2516 if (info->p2ps_instance) { 2517 char str[256]; 2518 const u8 *buf = wpabuf_head(info->p2ps_instance); 2519 size_t len = wpabuf_len(info->p2ps_instance); 2520 2521 while (len) { 2522 u32 id; 2523 u16 methods; 2524 u8 str_len; 2525 2526 if (len < 4 + 2 + 1) 2527 break; 2528 id = WPA_GET_LE32(buf); 2529 buf += sizeof(u32); 2530 methods = WPA_GET_BE16(buf); 2531 buf += sizeof(u16); 2532 str_len = *buf++; 2533 if (str_len > len - 4 - 2 - 1) 2534 break; 2535 os_memcpy(str, buf, str_len); 2536 str[str_len] = '\0'; 2537 buf += str_len; 2538 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8); 2539 2540 wpa_msg_global(wpa_s, MSG_INFO, 2541 P2P_EVENT_DEVICE_FOUND MACSTR 2542 " p2p_dev_addr=" MACSTR 2543 " pri_dev_type=%s name='%s'" 2544 " config_methods=0x%x" 2545 " dev_capab=0x%x" 2546 " group_capab=0x%x" 2547 " adv_id=%x asp_svc=%s%s", 2548 MAC2STR(addr), 2549 MAC2STR(info->p2p_device_addr), 2550 wps_dev_type_bin2str( 2551 info->pri_dev_type, 2552 devtype, sizeof(devtype)), 2553 info->device_name, methods, 2554 info->dev_capab, info->group_capab, 2555 id, str, 2556 info->vendor_elems ? 2557 " vendor_elems=1" : ""); 2558 } 2559 goto done; 2560 } 2561 2562 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR 2563 " p2p_dev_addr=" MACSTR 2564 " pri_dev_type=%s name='%s' config_methods=0x%x " 2565 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d", 2566 MAC2STR(addr), MAC2STR(info->p2p_device_addr), 2567 wps_dev_type_bin2str(info->pri_dev_type, devtype, 2568 sizeof(devtype)), 2569 info->device_name, info->config_methods, 2570 info->dev_capab, info->group_capab, 2571 wfd_dev_info_hex ? " wfd_dev_info=0x" : "", 2572 wfd_dev_info_hex ? wfd_dev_info_hex : "", 2573 info->vendor_elems ? " vendor_elems=1" : "", 2574 new_device); 2575 2576 done: 2577 os_free(wfd_dev_info_hex); 2578 #endif /* CONFIG_NO_STDOUT_DEBUG */ 2579 2580 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device); 2581 } 2582 2583 2584 static void wpas_dev_lost(void *ctx, const u8 *dev_addr) 2585 { 2586 struct wpa_supplicant *wpa_s = ctx; 2587 2588 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST 2589 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr)); 2590 2591 wpas_notify_p2p_device_lost(wpa_s, dev_addr); 2592 } 2593 2594 2595 static void wpas_find_stopped(void *ctx) 2596 { 2597 struct wpa_supplicant *wpa_s = ctx; 2598 2599 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0) 2600 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed"); 2601 2602 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED); 2603 wpas_notify_p2p_find_stopped(wpa_s); 2604 } 2605 2606 2607 struct wpas_p2p_listen_work { 2608 unsigned int freq; 2609 unsigned int duration; 2610 struct wpabuf *probe_resp_ie; 2611 }; 2612 2613 2614 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork) 2615 { 2616 if (lwork == NULL) 2617 return; 2618 wpabuf_free(lwork->probe_resp_ie); 2619 os_free(lwork); 2620 } 2621 2622 2623 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s) 2624 { 2625 struct wpas_p2p_listen_work *lwork; 2626 2627 if (!wpa_s->p2p_listen_work) 2628 return; 2629 2630 lwork = wpa_s->p2p_listen_work->ctx; 2631 wpas_p2p_listen_work_free(lwork); 2632 radio_work_done(wpa_s->p2p_listen_work); 2633 wpa_s->p2p_listen_work = NULL; 2634 } 2635 2636 2637 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit) 2638 { 2639 struct wpa_supplicant *wpa_s = work->wpa_s; 2640 struct wpas_p2p_listen_work *lwork = work->ctx; 2641 unsigned int duration; 2642 2643 if (deinit) { 2644 if (work->started) { 2645 wpa_s->p2p_listen_work = NULL; 2646 wpas_stop_listen(wpa_s); 2647 } 2648 wpas_p2p_listen_work_free(lwork); 2649 return; 2650 } 2651 2652 wpa_s->p2p_listen_work = work; 2653 2654 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL); 2655 2656 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) { 2657 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to " 2658 "report received Probe Request frames"); 2659 wpas_p2p_listen_work_done(wpa_s); 2660 return; 2661 } 2662 2663 wpa_s->pending_listen_freq = lwork->freq; 2664 wpa_s->pending_listen_duration = lwork->duration; 2665 2666 duration = lwork->duration; 2667 #ifdef CONFIG_TESTING_OPTIONS 2668 if (wpa_s->extra_roc_dur) { 2669 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u", 2670 duration, duration + wpa_s->extra_roc_dur); 2671 duration += wpa_s->extra_roc_dur; 2672 } 2673 #endif /* CONFIG_TESTING_OPTIONS */ 2674 2675 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) { 2676 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver " 2677 "to remain on channel (%u MHz) for Listen " 2678 "state", lwork->freq); 2679 wpas_p2p_listen_work_done(wpa_s); 2680 wpa_s->pending_listen_freq = 0; 2681 return; 2682 } 2683 wpa_s->off_channel_freq = 0; 2684 wpa_s->roc_waiting_drv_freq = lwork->freq; 2685 } 2686 2687 2688 static int wpas_start_listen(void *ctx, unsigned int freq, 2689 unsigned int duration, 2690 const struct wpabuf *probe_resp_ie) 2691 { 2692 struct wpa_supplicant *wpa_s = ctx; 2693 struct wpas_p2p_listen_work *lwork; 2694 2695 if (wpa_s->p2p_listen_work) { 2696 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists"); 2697 return -1; 2698 } 2699 2700 lwork = os_zalloc(sizeof(*lwork)); 2701 if (lwork == NULL) 2702 return -1; 2703 lwork->freq = freq; 2704 lwork->duration = duration; 2705 if (probe_resp_ie) { 2706 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie); 2707 if (lwork->probe_resp_ie == NULL) { 2708 wpas_p2p_listen_work_free(lwork); 2709 return -1; 2710 } 2711 } 2712 2713 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb, 2714 lwork) < 0) { 2715 wpas_p2p_listen_work_free(lwork); 2716 return -1; 2717 } 2718 2719 return 0; 2720 } 2721 2722 2723 static void wpas_stop_listen(void *ctx) 2724 { 2725 struct wpa_supplicant *wpa_s = ctx; 2726 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { 2727 wpa_drv_cancel_remain_on_channel(wpa_s); 2728 wpa_s->off_channel_freq = 0; 2729 wpa_s->roc_waiting_drv_freq = 0; 2730 } 2731 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL); 2732 2733 /* 2734 * Don't cancel Probe Request RX reporting for a connected P2P Client 2735 * handling Probe Request frames. 2736 */ 2737 if (!wpa_s->p2p_cli_probe) 2738 wpa_drv_probe_req_report(wpa_s, 0); 2739 2740 wpas_p2p_listen_work_done(wpa_s); 2741 } 2742 2743 2744 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf, 2745 unsigned int freq) 2746 { 2747 struct wpa_supplicant *wpa_s = ctx; 2748 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1, 2749 freq, 0); 2750 } 2751 2752 2753 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s, 2754 const u8 *peer, const char *params, 2755 unsigned int generated_pin) 2756 { 2757 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR 2758 " %08d%s", MAC2STR(peer), generated_pin, params); 2759 } 2760 2761 2762 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s, 2763 const u8 *peer, const char *params) 2764 { 2765 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR 2766 "%s", MAC2STR(peer), params); 2767 } 2768 2769 2770 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods, 2771 const u8 *dev_addr, const u8 *pri_dev_type, 2772 const char *dev_name, u16 supp_config_methods, 2773 u8 dev_capab, u8 group_capab, const u8 *group_id, 2774 size_t group_id_len) 2775 { 2776 struct wpa_supplicant *wpa_s = ctx; 2777 char devtype[WPS_DEV_TYPE_BUFSIZE]; 2778 char params[300]; 2779 u8 empty_dev_type[8]; 2780 unsigned int generated_pin = 0; 2781 struct wpa_supplicant *group = NULL; 2782 int res; 2783 2784 if (group_id) { 2785 for (group = wpa_s->global->ifaces; group; group = group->next) 2786 { 2787 struct wpa_ssid *s = group->current_ssid; 2788 if (s != NULL && 2789 s->mode == WPAS_MODE_P2P_GO && 2790 group_id_len - ETH_ALEN == s->ssid_len && 2791 os_memcmp(group_id + ETH_ALEN, s->ssid, 2792 s->ssid_len) == 0) 2793 break; 2794 } 2795 } 2796 2797 if (pri_dev_type == NULL) { 2798 os_memset(empty_dev_type, 0, sizeof(empty_dev_type)); 2799 pri_dev_type = empty_dev_type; 2800 } 2801 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR 2802 " pri_dev_type=%s name='%s' config_methods=0x%x " 2803 "dev_capab=0x%x group_capab=0x%x%s%s", 2804 MAC2STR(dev_addr), 2805 wps_dev_type_bin2str(pri_dev_type, devtype, 2806 sizeof(devtype)), 2807 dev_name, supp_config_methods, dev_capab, group_capab, 2808 group ? " group=" : "", 2809 group ? group->ifname : ""); 2810 if (os_snprintf_error(sizeof(params), res)) 2811 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated"); 2812 params[sizeof(params) - 1] = '\0'; 2813 2814 if (config_methods & WPS_CONFIG_DISPLAY) { 2815 if (wps_generate_pin(&generated_pin) < 0) { 2816 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN"); 2817 wpas_notify_p2p_provision_discovery( 2818 wpa_s, peer, 0 /* response */, 2819 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0); 2820 return; 2821 } 2822 wpas_prov_disc_local_display(wpa_s, peer, params, 2823 generated_pin); 2824 } else if (config_methods & WPS_CONFIG_KEYPAD) 2825 wpas_prov_disc_local_keypad(wpa_s, peer, params); 2826 else if (config_methods & WPS_CONFIG_PUSHBUTTON) 2827 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ 2828 MACSTR "%s", MAC2STR(peer), params); 2829 2830 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */, 2831 P2P_PROV_DISC_SUCCESS, 2832 config_methods, generated_pin); 2833 } 2834 2835 2836 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods) 2837 { 2838 struct wpa_supplicant *wpa_s = ctx; 2839 unsigned int generated_pin = 0; 2840 char params[20]; 2841 2842 if (wpa_s->pending_pd_before_join && 2843 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 || 2844 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) { 2845 wpa_s->pending_pd_before_join = 0; 2846 wpa_printf(MSG_DEBUG, "P2P: Starting pending " 2847 "join-existing-group operation"); 2848 wpas_p2p_join_start(wpa_s, 0, NULL, 0); 2849 return; 2850 } 2851 2852 if (wpa_s->pending_pd_use == AUTO_PD_JOIN || 2853 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) { 2854 int res; 2855 2856 res = os_snprintf(params, sizeof(params), " peer_go=%d", 2857 wpa_s->pending_pd_use == AUTO_PD_JOIN); 2858 if (os_snprintf_error(sizeof(params), res)) 2859 params[sizeof(params) - 1] = '\0'; 2860 } else 2861 params[0] = '\0'; 2862 2863 if (config_methods & WPS_CONFIG_DISPLAY) 2864 wpas_prov_disc_local_keypad(wpa_s, peer, params); 2865 else if (config_methods & WPS_CONFIG_KEYPAD) { 2866 if (wps_generate_pin(&generated_pin) < 0) { 2867 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN"); 2868 wpas_notify_p2p_provision_discovery( 2869 wpa_s, peer, 0 /* response */, 2870 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0); 2871 return; 2872 } 2873 wpas_prov_disc_local_display(wpa_s, peer, params, 2874 generated_pin); 2875 } else if (config_methods & WPS_CONFIG_PUSHBUTTON) 2876 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP 2877 MACSTR "%s", MAC2STR(peer), params); 2878 2879 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, 2880 P2P_PROV_DISC_SUCCESS, 2881 config_methods, generated_pin); 2882 } 2883 2884 2885 static void wpas_prov_disc_fail(void *ctx, const u8 *peer, 2886 enum p2p_prov_disc_status status, 2887 u32 adv_id, const u8 *adv_mac, 2888 const char *deferred_session_resp) 2889 { 2890 struct wpa_supplicant *wpa_s = ctx; 2891 2892 if (wpa_s->p2p_fallback_to_go_neg) { 2893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto " 2894 "failed - fall back to GO Negotiation"); 2895 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 2896 P2P_EVENT_FALLBACK_TO_GO_NEG 2897 "reason=PD-failed"); 2898 wpas_p2p_fallback_to_go_neg(wpa_s, 0); 2899 return; 2900 } 2901 2902 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) { 2903 wpa_s->pending_pd_before_join = 0; 2904 wpa_printf(MSG_DEBUG, "P2P: Starting pending " 2905 "join-existing-group operation (no ACK for PD " 2906 "Req attempts)"); 2907 wpas_p2p_join_start(wpa_s, 0, NULL, 0); 2908 return; 2909 } 2910 2911 if (adv_id && adv_mac && deferred_session_resp) { 2912 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE 2913 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x" 2914 " deferred_session_resp='%s'", 2915 MAC2STR(peer), status, adv_id, 2916 deferred_session_resp); 2917 } else if (adv_id && adv_mac) { 2918 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE 2919 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x", 2920 MAC2STR(peer), status, adv_id); 2921 } else { 2922 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE 2923 " p2p_dev_addr=" MACSTR " status=%d", 2924 MAC2STR(peer), status); 2925 } 2926 2927 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */, 2928 status, 0, 0); 2929 } 2930 2931 2932 static int freq_included(struct wpa_supplicant *wpa_s, 2933 const struct p2p_channels *channels, 2934 unsigned int freq) 2935 { 2936 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) && 2937 wpas_p2p_go_is_peer_freq(wpa_s, freq)) 2938 return 1; 2939 return 0; 2940 } 2941 2942 2943 static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s) 2944 { 2945 unsigned int num = P2P_MAX_CHANNELS; 2946 int *common_freqs; 2947 int ret; 2948 2949 p2p_go_dump_common_freqs(wpa_s); 2950 common_freqs = os_calloc(num, sizeof(int)); 2951 if (!common_freqs) 2952 return; 2953 2954 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num); 2955 if (ret < 0) { 2956 wpa_dbg(wpa_s, MSG_DEBUG, 2957 "P2P: Failed to get group common freqs"); 2958 os_free(common_freqs); 2959 return; 2960 } 2961 2962 os_free(wpa_s->p2p_group_common_freqs); 2963 wpa_s->p2p_group_common_freqs = common_freqs; 2964 wpa_s->p2p_group_common_freqs_num = num; 2965 p2p_go_dump_common_freqs(wpa_s); 2966 } 2967 2968 2969 /* 2970 * Check if the given frequency is one of the possible operating frequencies 2971 * set after the completion of the GO Negotiation. 2972 */ 2973 static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq) 2974 { 2975 unsigned int i; 2976 2977 p2p_go_dump_common_freqs(wpa_s); 2978 2979 /* assume no restrictions */ 2980 if (!wpa_s->p2p_group_common_freqs_num) 2981 return 1; 2982 2983 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { 2984 if (wpa_s->p2p_group_common_freqs[i] == freq) 2985 return 1; 2986 } 2987 return 0; 2988 } 2989 2990 2991 static int wpas_sta_check_ecsa(struct hostapd_data *hapd, 2992 struct sta_info *sta, void *ctx) 2993 { 2994 int *ecsa_support = ctx; 2995 2996 *ecsa_support &= sta->ecsa_supported; 2997 2998 return 0; 2999 } 3000 3001 3002 /* Check if all the peers support eCSA */ 3003 static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s) 3004 { 3005 int ecsa_support = 1; 3006 3007 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa, 3008 &ecsa_support); 3009 3010 return ecsa_support; 3011 } 3012 3013 3014 /** 3015 * Pick the best frequency to use from all the currently used frequencies. 3016 */ 3017 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s, 3018 struct wpa_used_freq_data *freqs, 3019 unsigned int num) 3020 { 3021 unsigned int i, c; 3022 3023 /* find a candidate freq that is supported by P2P */ 3024 for (c = 0; c < num; c++) 3025 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq)) 3026 break; 3027 3028 if (c == num) 3029 return 0; 3030 3031 /* once we have a candidate, try to find a 'better' one */ 3032 for (i = c + 1; i < num; i++) { 3033 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq)) 3034 continue; 3035 3036 /* 3037 * 1. Infrastructure station interfaces have higher preference. 3038 * 2. P2P Clients have higher preference. 3039 * 3. All others. 3040 */ 3041 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) { 3042 c = i; 3043 break; 3044 } 3045 3046 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT)) 3047 c = i; 3048 } 3049 return freqs[c].freq; 3050 } 3051 3052 3053 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid, 3054 const u8 *go_dev_addr, const u8 *ssid, 3055 size_t ssid_len, int *go, u8 *group_bssid, 3056 int *force_freq, int persistent_group, 3057 const struct p2p_channels *channels, 3058 int dev_pw_id) 3059 { 3060 struct wpa_supplicant *wpa_s = ctx; 3061 struct wpa_ssid *s; 3062 struct wpa_used_freq_data *freqs; 3063 struct wpa_supplicant *grp; 3064 int best_freq; 3065 3066 if (!persistent_group) { 3067 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR 3068 " to join an active group (SSID: %s)", 3069 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len)); 3070 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && 3071 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN) 3072 == 0 || 3073 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) { 3074 wpa_printf(MSG_DEBUG, "P2P: Accept previously " 3075 "authorized invitation"); 3076 goto accept_inv; 3077 } 3078 3079 #ifdef CONFIG_WPS_NFC 3080 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled && 3081 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) { 3082 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag"); 3083 wpa_s->p2p_wps_method = WPS_NFC; 3084 wpa_s->pending_join_wps_method = WPS_NFC; 3085 os_memcpy(wpa_s->pending_join_dev_addr, 3086 go_dev_addr, ETH_ALEN); 3087 os_memcpy(wpa_s->pending_join_iface_addr, 3088 bssid, ETH_ALEN); 3089 goto accept_inv; 3090 } 3091 #endif /* CONFIG_WPS_NFC */ 3092 3093 /* 3094 * Do not accept the invitation automatically; notify user and 3095 * request approval. 3096 */ 3097 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; 3098 } 3099 3100 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go); 3101 if (grp) { 3102 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already " 3103 "running persistent group"); 3104 if (*go) 3105 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN); 3106 goto accept_inv; 3107 } 3108 3109 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) && 3110 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) { 3111 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated " 3112 "invitation to re-invoke a persistent group"); 3113 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); 3114 } else if (!wpa_s->conf->persistent_reconnect) 3115 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE; 3116 3117 for (s = wpa_s->conf->ssid; s; s = s->next) { 3118 if (s->disabled == 2 && 3119 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 && 3120 s->ssid_len == ssid_len && 3121 os_memcmp(ssid, s->ssid, ssid_len) == 0) 3122 break; 3123 } 3124 3125 if (!s) { 3126 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR 3127 " requested reinvocation of an unknown group", 3128 MAC2STR(sa)); 3129 return P2P_SC_FAIL_UNKNOWN_GROUP; 3130 } 3131 3132 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) { 3133 *go = 1; 3134 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) { 3135 wpa_printf(MSG_DEBUG, "P2P: The only available " 3136 "interface is already in use - reject " 3137 "invitation"); 3138 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 3139 } 3140 if (wpa_s->p2p_mgmt) 3141 os_memcpy(group_bssid, wpa_s->parent->own_addr, 3142 ETH_ALEN); 3143 else 3144 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN); 3145 } else if (s->mode == WPAS_MODE_P2P_GO) { 3146 *go = 1; 3147 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0) 3148 { 3149 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " 3150 "interface address for the group"); 3151 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE; 3152 } 3153 os_memcpy(group_bssid, wpa_s->pending_interface_addr, 3154 ETH_ALEN); 3155 } 3156 3157 accept_inv: 3158 wpas_p2p_set_own_freq_preference(wpa_s, 0); 3159 3160 best_freq = 0; 3161 freqs = os_calloc(wpa_s->num_multichan_concurrent, 3162 sizeof(struct wpa_used_freq_data)); 3163 if (freqs) { 3164 int num_channels = wpa_s->num_multichan_concurrent; 3165 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels); 3166 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); 3167 os_free(freqs); 3168 } 3169 3170 /* Get one of the frequencies currently in use */ 3171 if (best_freq > 0) { 3172 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces"); 3173 wpas_p2p_set_own_freq_preference(wpa_s, best_freq); 3174 3175 if (wpa_s->num_multichan_concurrent < 2 || 3176 wpas_p2p_num_unused_channels(wpa_s) < 1) { 3177 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces"); 3178 *force_freq = best_freq; 3179 } 3180 } 3181 3182 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 && 3183 wpas_p2p_num_unused_channels(wpa_s) > 0) { 3184 if (*go == 0) { 3185 /* We are the client */ 3186 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be " 3187 "running a GO but we are capable of MCC, " 3188 "figure out the best channel to use"); 3189 *force_freq = 0; 3190 } else if (!freq_included(wpa_s, channels, *force_freq)) { 3191 /* We are the GO, and *force_freq is not in the 3192 * intersection */ 3193 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not " 3194 "in intersection but we are capable of MCC, " 3195 "figure out the best channel to use", 3196 *force_freq); 3197 *force_freq = 0; 3198 } 3199 } 3200 3201 return P2P_SC_SUCCESS; 3202 } 3203 3204 3205 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid, 3206 const u8 *ssid, size_t ssid_len, 3207 const u8 *go_dev_addr, u8 status, 3208 int op_freq) 3209 { 3210 struct wpa_supplicant *wpa_s = ctx; 3211 struct wpa_ssid *s; 3212 3213 for (s = wpa_s->conf->ssid; s; s = s->next) { 3214 if (s->disabled == 2 && 3215 s->ssid_len == ssid_len && 3216 os_memcmp(ssid, s->ssid, ssid_len) == 0) 3217 break; 3218 } 3219 3220 if (status == P2P_SC_SUCCESS) { 3221 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR 3222 " was accepted; op_freq=%d MHz, SSID=%s", 3223 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len)); 3224 if (s) { 3225 int go = s->mode == WPAS_MODE_P2P_GO; 3226 if (go) { 3227 wpa_msg_global(wpa_s, MSG_INFO, 3228 P2P_EVENT_INVITATION_ACCEPTED 3229 "sa=" MACSTR 3230 " persistent=%d freq=%d", 3231 MAC2STR(sa), s->id, op_freq); 3232 } else { 3233 wpa_msg_global(wpa_s, MSG_INFO, 3234 P2P_EVENT_INVITATION_ACCEPTED 3235 "sa=" MACSTR 3236 " persistent=%d", 3237 MAC2STR(sa), s->id); 3238 } 3239 wpas_p2p_group_add_persistent( 3240 wpa_s, s, go, 0, op_freq, 0, 3241 wpa_s->conf->p2p_go_ht40, 3242 wpa_s->conf->p2p_go_vht, 3243 0, 3244 wpa_s->conf->p2p_go_he, 3245 wpa_s->conf->p2p_go_edmg, NULL, 3246 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 3247 1, is_p2p_allow_6ghz(wpa_s->global->p2p)); 3248 } else if (bssid) { 3249 wpa_s->user_initiated_pd = 0; 3250 wpa_msg_global(wpa_s, MSG_INFO, 3251 P2P_EVENT_INVITATION_ACCEPTED 3252 "sa=" MACSTR " go_dev_addr=" MACSTR 3253 " bssid=" MACSTR " unknown-network", 3254 MAC2STR(sa), MAC2STR(go_dev_addr), 3255 MAC2STR(bssid)); 3256 wpas_p2p_join(wpa_s, bssid, go_dev_addr, 3257 wpa_s->p2p_wps_method, 0, op_freq, 3258 ssid, ssid_len); 3259 } 3260 return; 3261 } 3262 3263 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { 3264 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR 3265 " was rejected (status %u)", MAC2STR(sa), status); 3266 return; 3267 } 3268 3269 if (!s) { 3270 if (bssid) { 3271 wpa_msg_global(wpa_s, MSG_INFO, 3272 P2P_EVENT_INVITATION_RECEIVED 3273 "sa=" MACSTR " go_dev_addr=" MACSTR 3274 " bssid=" MACSTR " unknown-network", 3275 MAC2STR(sa), MAC2STR(go_dev_addr), 3276 MAC2STR(bssid)); 3277 } else { 3278 wpa_msg_global(wpa_s, MSG_INFO, 3279 P2P_EVENT_INVITATION_RECEIVED 3280 "sa=" MACSTR " go_dev_addr=" MACSTR 3281 " unknown-network", 3282 MAC2STR(sa), MAC2STR(go_dev_addr)); 3283 } 3284 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, 3285 bssid, 0, op_freq); 3286 return; 3287 } 3288 3289 if (s->mode == WPAS_MODE_P2P_GO && op_freq) { 3290 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED 3291 "sa=" MACSTR " persistent=%d freq=%d", 3292 MAC2STR(sa), s->id, op_freq); 3293 } else { 3294 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED 3295 "sa=" MACSTR " persistent=%d", 3296 MAC2STR(sa), s->id); 3297 } 3298 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid, 3299 s->id, op_freq); 3300 } 3301 3302 3303 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s, 3304 struct wpa_ssid *ssid, 3305 const u8 *peer, int inv) 3306 { 3307 size_t i; 3308 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s; 3309 3310 if (ssid == NULL) 3311 return; 3312 3313 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) { 3314 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer, 3315 ETH_ALEN) == 0) 3316 break; 3317 } 3318 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) { 3319 if (ssid->mode != WPAS_MODE_P2P_GO && 3320 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) { 3321 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d " 3322 "due to invitation result", ssid->id); 3323 wpas_notify_network_removed(wpa_s, ssid); 3324 wpa_config_remove_network(wpa_s->conf, ssid->id); 3325 return; 3326 } 3327 return; /* Peer not found in client list */ 3328 } 3329 3330 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent " 3331 "group %d client list%s", 3332 MAC2STR(peer), ssid->id, 3333 inv ? " due to invitation result" : ""); 3334 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN, 3335 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN, 3336 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN); 3337 ssid->num_p2p_clients--; 3338 if (p2p_wpa_s->conf->update_config && 3339 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf)) 3340 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 3341 } 3342 3343 3344 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s, 3345 const u8 *peer) 3346 { 3347 struct wpa_ssid *ssid; 3348 3349 wpa_s = wpa_s->global->p2p_invite_group; 3350 if (wpa_s == NULL) 3351 return; /* No known invitation group */ 3352 ssid = wpa_s->current_ssid; 3353 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO || 3354 !ssid->p2p_persistent_group) 3355 return; /* Not operating as a GO in persistent group */ 3356 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer, 3357 ssid->ssid, ssid->ssid_len); 3358 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); 3359 } 3360 3361 3362 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid, 3363 const struct p2p_channels *channels, 3364 const u8 *peer, int neg_freq, 3365 int peer_oper_freq) 3366 { 3367 struct wpa_supplicant *wpa_s = ctx; 3368 struct wpa_ssid *ssid; 3369 int freq; 3370 3371 if (bssid) { 3372 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT 3373 "status=%d " MACSTR, 3374 status, MAC2STR(bssid)); 3375 } else { 3376 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT 3377 "status=%d ", status); 3378 } 3379 wpas_notify_p2p_invitation_result(wpa_s, status, bssid); 3380 3381 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR, 3382 status, MAC2STR(peer)); 3383 if (wpa_s->pending_invite_ssid_id == -1) { 3384 struct wpa_supplicant *group_if = 3385 wpa_s->global->p2p_invite_group; 3386 3387 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) 3388 wpas_remove_persistent_client(wpa_s, peer); 3389 3390 /* 3391 * Invitation to an active group. If this is successful and we 3392 * are the GO, set the client wait to postpone some concurrent 3393 * operations and to allow provisioning and connection to happen 3394 * more quickly. 3395 */ 3396 if (status == P2P_SC_SUCCESS && 3397 group_if && group_if->current_ssid && 3398 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) { 3399 os_get_reltime(&wpa_s->global->p2p_go_wait_client); 3400 #ifdef CONFIG_TESTING_OPTIONS 3401 if (group_if->p2p_go_csa_on_inv) { 3402 wpa_printf(MSG_DEBUG, 3403 "Testing: force P2P GO CSA after invitation"); 3404 eloop_cancel_timeout( 3405 wpas_p2p_reconsider_moving_go, 3406 wpa_s, NULL); 3407 eloop_register_timeout( 3408 0, 50000, 3409 wpas_p2p_reconsider_moving_go, 3410 wpa_s, NULL); 3411 } 3412 #endif /* CONFIG_TESTING_OPTIONS */ 3413 } 3414 return; 3415 } 3416 3417 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) { 3418 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another " 3419 "invitation exchange to indicate readiness for " 3420 "re-invocation"); 3421 } 3422 3423 if (status != P2P_SC_SUCCESS) { 3424 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) { 3425 ssid = wpa_config_get_network( 3426 wpa_s->conf, wpa_s->pending_invite_ssid_id); 3427 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1); 3428 } 3429 wpas_p2p_remove_pending_group_interface(wpa_s); 3430 return; 3431 } 3432 3433 ssid = wpa_config_get_network(wpa_s->conf, 3434 wpa_s->pending_invite_ssid_id); 3435 if (ssid == NULL) { 3436 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group " 3437 "data matching with invitation"); 3438 return; 3439 } 3440 3441 /* 3442 * The peer could have missed our ctrl::ack frame for Invitation 3443 * Response and continue retransmitting the frame. To reduce the 3444 * likelihood of the peer not getting successful TX status for the 3445 * Invitation Response frame, wait a short time here before starting 3446 * the persistent group so that we will remain on the current channel to 3447 * acknowledge any possible retransmission from the peer. 3448 */ 3449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before " 3450 "starting persistent group"); 3451 os_sleep(0, 50000); 3452 3453 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO && 3454 freq_included(wpa_s, channels, neg_freq)) 3455 freq = neg_freq; 3456 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO && 3457 freq_included(wpa_s, channels, peer_oper_freq)) 3458 freq = peer_oper_freq; 3459 else 3460 freq = 0; 3461 3462 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s", 3463 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); 3464 wpas_p2p_group_add_persistent(wpa_s, ssid, 3465 ssid->mode == WPAS_MODE_P2P_GO, 3466 wpa_s->p2p_persistent_go_freq, 3467 freq, 3468 wpa_s->p2p_go_vht_center_freq2, 3469 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht, 3470 wpa_s->p2p_go_max_oper_chwidth, 3471 wpa_s->p2p_go_he, 3472 wpa_s->p2p_go_edmg, 3473 channels, 3474 ssid->mode == WPAS_MODE_P2P_GO ? 3475 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 3476 0, 1, 3477 is_p2p_allow_6ghz(wpa_s->global->p2p)); 3478 } 3479 3480 3481 static int wpas_p2p_disallowed_freq(struct wpa_global *global, 3482 unsigned int freq) 3483 { 3484 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq)) 3485 return 1; 3486 return freq_range_list_includes(&global->p2p_disallow_freq, freq); 3487 } 3488 3489 3490 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan) 3491 { 3492 reg->channel[reg->channels] = chan; 3493 reg->channels++; 3494 } 3495 3496 3497 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s, 3498 struct p2p_channels *chan, 3499 struct p2p_channels *cli_chan) 3500 { 3501 int i, cla = 0; 3502 3503 wpa_s->global->p2p_24ghz_social_channels = 1; 3504 3505 os_memset(cli_chan, 0, sizeof(*cli_chan)); 3506 3507 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz " 3508 "band"); 3509 3510 /* Operating class 81 - 2.4 GHz band channels 1..13 */ 3511 chan->reg_class[cla].reg_class = 81; 3512 chan->reg_class[cla].channels = 0; 3513 for (i = 0; i < 11; i++) { 3514 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5)) 3515 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1); 3516 } 3517 if (chan->reg_class[cla].channels) 3518 cla++; 3519 3520 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz " 3521 "band"); 3522 3523 /* Operating class 115 - 5 GHz, channels 36-48 */ 3524 chan->reg_class[cla].reg_class = 115; 3525 chan->reg_class[cla].channels = 0; 3526 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5)) 3527 wpas_p2p_add_chan(&chan->reg_class[cla], 36); 3528 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5)) 3529 wpas_p2p_add_chan(&chan->reg_class[cla], 40); 3530 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5)) 3531 wpas_p2p_add_chan(&chan->reg_class[cla], 44); 3532 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5)) 3533 wpas_p2p_add_chan(&chan->reg_class[cla], 48); 3534 if (chan->reg_class[cla].channels) 3535 cla++; 3536 3537 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz " 3538 "band"); 3539 3540 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */ 3541 chan->reg_class[cla].reg_class = 124; 3542 chan->reg_class[cla].channels = 0; 3543 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5)) 3544 wpas_p2p_add_chan(&chan->reg_class[cla], 149); 3545 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5)) 3546 wpas_p2p_add_chan(&chan->reg_class[cla], 153); 3547 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5)) 3548 wpas_p2p_add_chan(&chan->reg_class[cla], 157); 3549 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5)) 3550 wpas_p2p_add_chan(&chan->reg_class[cla], 161); 3551 if (chan->reg_class[cla].channels) 3552 cla++; 3553 3554 chan->reg_classes = cla; 3555 return 0; 3556 } 3557 3558 3559 static enum chan_allowed has_channel(struct wpa_global *global, 3560 struct hostapd_hw_modes *mode, u8 op_class, 3561 u8 chan, int *flags) 3562 { 3563 int i; 3564 unsigned int freq; 3565 3566 freq = ieee80211_chan_to_freq(NULL, op_class, chan); 3567 if (wpas_p2p_disallowed_freq(global, freq)) 3568 return NOT_ALLOWED; 3569 3570 for (i = 0; i < mode->num_channels; i++) { 3571 if ((unsigned int) mode->channels[i].freq == freq) { 3572 if (flags) 3573 *flags = mode->channels[i].flag; 3574 if (mode->channels[i].flag & 3575 (HOSTAPD_CHAN_DISABLED | 3576 HOSTAPD_CHAN_RADAR)) 3577 return NOT_ALLOWED; 3578 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR) 3579 return NO_IR; 3580 return ALLOWED; 3581 } 3582 } 3583 3584 return NOT_ALLOWED; 3585 } 3586 3587 3588 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s, 3589 struct hostapd_hw_modes *mode, 3590 u8 channel, const u8 *center_channels, 3591 size_t num_chan) 3592 { 3593 size_t i; 3594 3595 if (mode->mode != HOSTAPD_MODE_IEEE80211A) 3596 return 0; 3597 3598 for (i = 0; i < num_chan; i++) 3599 /* 3600 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48), 3601 * so the center channel is 6 channels away from the start/end. 3602 */ 3603 if (channel >= center_channels[i] - 6 && 3604 channel <= center_channels[i] + 6) 3605 return center_channels[i]; 3606 3607 return 0; 3608 } 3609 3610 3611 static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138, 3612 155, 171 }; 3613 static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103, 3614 119, 135, 151, 167, 183, 199, 3615 215 }; 3616 3617 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s, 3618 struct hostapd_hw_modes *mode, 3619 u8 op_class, u8 channel, u8 bw) 3620 { 3621 u8 center_chan; 3622 int i, flags; 3623 enum chan_allowed res, ret = ALLOWED; 3624 const u8 *chans; 3625 size_t num_chans; 3626 bool is_6ghz = is_6ghz_op_class(op_class); 3627 3628 if (is_6ghz) { 3629 chans = center_channels_6ghz_80mhz; 3630 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz); 3631 } else { 3632 chans = center_channels_5ghz_80mhz; 3633 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz); 3634 } 3635 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel, 3636 chans, num_chans); 3637 if (!center_chan) 3638 return NOT_ALLOWED; 3639 if (!is_6ghz && center_chan >= 58 && center_chan <= 138) 3640 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */ 3641 3642 /* check all the channels are available */ 3643 for (i = 0; i < 4; i++) { 3644 int adj_chan = center_chan - 6 + i * 4; 3645 3646 res = has_channel(wpa_s->global, mode, op_class, adj_chan, 3647 &flags); 3648 if (res == NOT_ALLOWED) 3649 return NOT_ALLOWED; 3650 if (res == NO_IR) 3651 ret = NO_IR; 3652 3653 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70)) 3654 return NOT_ALLOWED; 3655 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50)) 3656 return NOT_ALLOWED; 3657 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30)) 3658 return NOT_ALLOWED; 3659 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10)) 3660 return NOT_ALLOWED; 3661 } 3662 3663 return ret; 3664 } 3665 3666 3667 static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s, 3668 struct hostapd_hw_modes *mode, 3669 u8 channel, const u8 *center_channels, 3670 size_t num_chan) 3671 { 3672 unsigned int i; 3673 3674 if (mode->mode != HOSTAPD_MODE_IEEE80211A) 3675 return 0; 3676 3677 for (i = 0; i < num_chan; i++) 3678 /* 3679 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64), 3680 * so the center channel is 14 channels away from the start/end. 3681 */ 3682 if (channel >= center_channels[i] - 14 && 3683 channel <= center_channels[i] + 14) 3684 return center_channels[i]; 3685 3686 return 0; 3687 } 3688 3689 3690 static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 }; 3691 static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175, 3692 207 }; 3693 3694 static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s, 3695 struct hostapd_hw_modes *mode, 3696 u8 op_class, u8 channel, u8 bw) 3697 { 3698 u8 center_chan; 3699 int i, flags; 3700 enum chan_allowed res, ret = ALLOWED; 3701 const u8 *chans; 3702 size_t num_chans; 3703 3704 if (is_6ghz_op_class(op_class)) { 3705 chans = center_channels_6ghz_160mhz; 3706 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz); 3707 } else { 3708 chans = center_channels_5ghz_160mhz; 3709 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz); 3710 } 3711 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel, 3712 chans, num_chans); 3713 if (!center_chan) 3714 return NOT_ALLOWED; 3715 /* VHT 160 MHz uses DFS channels in most countries. */ 3716 3717 /* Check all the channels are available */ 3718 for (i = 0; i < 8; i++) { 3719 int adj_chan = center_chan - 14 + i * 4; 3720 3721 res = has_channel(wpa_s->global, mode, op_class, adj_chan, 3722 &flags); 3723 if (res == NOT_ALLOWED) 3724 return NOT_ALLOWED; 3725 3726 if (res == NO_IR) 3727 ret = NO_IR; 3728 3729 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150)) 3730 return NOT_ALLOWED; 3731 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130)) 3732 return NOT_ALLOWED; 3733 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110)) 3734 return NOT_ALLOWED; 3735 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90)) 3736 return NOT_ALLOWED; 3737 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70)) 3738 return NOT_ALLOWED; 3739 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50)) 3740 return NOT_ALLOWED; 3741 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30)) 3742 return NOT_ALLOWED; 3743 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10)) 3744 return NOT_ALLOWED; 3745 } 3746 3747 return ret; 3748 } 3749 3750 3751 static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s, 3752 struct hostapd_hw_modes *mode, 3753 u8 channel) 3754 { 3755 struct ieee80211_edmg_config edmg; 3756 3757 hostapd_encode_edmg_chan(1, channel, 0, &edmg); 3758 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg)) 3759 return ALLOWED; 3760 3761 return NOT_ALLOWED; 3762 } 3763 3764 3765 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s, 3766 struct hostapd_hw_modes *mode, 3767 u8 op_class, u8 channel, u8 bw) 3768 { 3769 int flag = 0; 3770 enum chan_allowed res, res2; 3771 3772 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag); 3773 if (bw == BW40MINUS) { 3774 if (!(flag & HOSTAPD_CHAN_HT40MINUS)) 3775 return NOT_ALLOWED; 3776 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4, 3777 NULL); 3778 } else if (bw == BW40PLUS) { 3779 if (!(flag & HOSTAPD_CHAN_HT40PLUS)) 3780 return NOT_ALLOWED; 3781 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4, 3782 NULL); 3783 } else if (bw == BW80) { 3784 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel, 3785 bw); 3786 } else if (bw == BW160) { 3787 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel, 3788 bw); 3789 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) { 3790 return wpas_p2p_verify_edmg(wpa_s, mode, channel); 3791 } 3792 3793 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED) 3794 return NOT_ALLOWED; 3795 if (res == NO_IR || res2 == NO_IR) 3796 return NO_IR; 3797 return res; 3798 } 3799 3800 3801 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s, 3802 struct p2p_channels *chan, 3803 struct p2p_channels *cli_chan, 3804 bool p2p_disable_6ghz) 3805 { 3806 struct hostapd_hw_modes *mode; 3807 int cla, op, cli_cla; 3808 3809 if (wpa_s->hw.modes == NULL) { 3810 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching " 3811 "of all supported channels; assume dualband " 3812 "support"); 3813 return wpas_p2p_default_channels(wpa_s, chan, cli_chan); 3814 } 3815 3816 cla = cli_cla = 0; 3817 3818 for (op = 0; global_op_class[op].op_class; op++) { 3819 const struct oper_class_map *o = &global_op_class[op]; 3820 u8 ch; 3821 struct p2p_reg_class *reg = NULL, *cli_reg = NULL; 3822 3823 if (o->p2p == NO_P2P_SUPP || 3824 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz)) 3825 continue; 3826 3827 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode, 3828 is_6ghz_op_class(o->op_class)); 3829 if (mode == NULL) 3830 continue; 3831 if (mode->mode == HOSTAPD_MODE_IEEE80211G) 3832 wpa_s->global->p2p_24ghz_social_channels = 1; 3833 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { 3834 enum chan_allowed res; 3835 3836 /* Check for non-continuous jump in channel index 3837 * incrementation */ 3838 if ((o->op_class >= 128 && o->op_class <= 130) && 3839 ch < 149 && ch + o->inc > 149) 3840 ch = 149; 3841 3842 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class, 3843 ch, o->bw); 3844 if (res == ALLOWED) { 3845 if (reg == NULL) { 3846 if (cla == P2P_MAX_REG_CLASSES) 3847 continue; 3848 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u", 3849 o->op_class); 3850 reg = &chan->reg_class[cla]; 3851 cla++; 3852 reg->reg_class = o->op_class; 3853 } 3854 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS) 3855 continue; 3856 reg->channel[reg->channels] = ch; 3857 reg->channels++; 3858 } else if (res == NO_IR && 3859 wpa_s->conf->p2p_add_cli_chan) { 3860 if (cli_reg == NULL) { 3861 if (cli_cla == P2P_MAX_REG_CLASSES) 3862 continue; 3863 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)", 3864 o->op_class); 3865 cli_reg = &cli_chan->reg_class[cli_cla]; 3866 cli_cla++; 3867 cli_reg->reg_class = o->op_class; 3868 } 3869 if (cli_reg->channels == 3870 P2P_MAX_REG_CLASS_CHANNELS) 3871 continue; 3872 cli_reg->channel[cli_reg->channels] = ch; 3873 cli_reg->channels++; 3874 } 3875 } 3876 if (reg) { 3877 wpa_hexdump(MSG_DEBUG, "P2P: Channels", 3878 reg->channel, reg->channels); 3879 } 3880 if (cli_reg) { 3881 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)", 3882 cli_reg->channel, cli_reg->channels); 3883 } 3884 } 3885 3886 chan->reg_classes = cla; 3887 cli_chan->reg_classes = cli_cla; 3888 3889 return 0; 3890 } 3891 3892 3893 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s, 3894 struct hostapd_hw_modes *mode, u8 channel) 3895 { 3896 int op; 3897 enum chan_allowed ret; 3898 3899 for (op = 0; global_op_class[op].op_class; op++) { 3900 const struct oper_class_map *o = &global_op_class[op]; 3901 u8 ch; 3902 3903 if (o->p2p == NO_P2P_SUPP || 3904 (is_6ghz_op_class(o->op_class) && 3905 wpa_s->conf->p2p_6ghz_disable)) 3906 continue; 3907 3908 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) { 3909 if (o->mode != HOSTAPD_MODE_IEEE80211A || 3910 (o->bw != BW40PLUS && o->bw != BW40MINUS) || 3911 ch != channel) 3912 continue; 3913 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class, 3914 ch, o->bw); 3915 if (ret == ALLOWED) 3916 return (o->bw == BW40MINUS) ? -1 : 1; 3917 } 3918 } 3919 return 0; 3920 } 3921 3922 3923 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s, 3924 struct hostapd_hw_modes *mode, u8 channel, 3925 u8 op_class) 3926 { 3927 const u8 *chans; 3928 size_t num_chans; 3929 3930 if (!wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80)) 3931 return 0; 3932 3933 if (is_6ghz_op_class(op_class)) { 3934 chans = center_channels_6ghz_80mhz; 3935 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz); 3936 } else { 3937 chans = center_channels_5ghz_80mhz; 3938 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz); 3939 } 3940 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel, 3941 chans, num_chans); 3942 } 3943 3944 3945 int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s, 3946 struct hostapd_hw_modes *mode, u8 channel, 3947 u8 op_class) 3948 { 3949 const u8 *chans; 3950 size_t num_chans; 3951 3952 if (!wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160)) 3953 return 0; 3954 if (is_6ghz_op_class(op_class)) { 3955 chans = center_channels_6ghz_160mhz; 3956 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz); 3957 } else { 3958 chans = center_channels_5ghz_160mhz; 3959 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz); 3960 } 3961 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel, 3962 chans, num_chans); 3963 } 3964 3965 3966 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf, 3967 size_t buf_len) 3968 { 3969 struct wpa_supplicant *wpa_s = ctx; 3970 3971 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 3972 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0) 3973 break; 3974 } 3975 if (wpa_s == NULL) 3976 return -1; 3977 3978 return wpa_drv_get_noa(wpa_s, buf, buf_len); 3979 } 3980 3981 3982 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s, 3983 const u8 *ssid, size_t ssid_len) 3984 { 3985 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 3986 struct wpa_ssid *s = wpa_s->current_ssid; 3987 if (s == NULL) 3988 continue; 3989 if (s->mode != WPAS_MODE_P2P_GO && 3990 s->mode != WPAS_MODE_AP && 3991 s->mode != WPAS_MODE_P2P_GROUP_FORMATION) 3992 continue; 3993 if (s->ssid_len != ssid_len || 3994 os_memcmp(ssid, s->ssid, ssid_len) != 0) 3995 continue; 3996 return wpa_s; 3997 } 3998 3999 return NULL; 4000 4001 } 4002 4003 4004 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s, 4005 const u8 *peer_dev_addr) 4006 { 4007 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 4008 struct wpa_ssid *ssid = wpa_s->current_ssid; 4009 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group)) 4010 continue; 4011 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0) 4012 return wpa_s; 4013 } 4014 4015 return NULL; 4016 } 4017 4018 4019 static int wpas_go_connected(void *ctx, const u8 *dev_addr) 4020 { 4021 struct wpa_supplicant *wpa_s = ctx; 4022 4023 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL; 4024 } 4025 4026 4027 static int wpas_is_concurrent_session_active(void *ctx) 4028 { 4029 struct wpa_supplicant *wpa_s = ctx; 4030 struct wpa_supplicant *ifs; 4031 4032 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) { 4033 if (ifs == wpa_s) 4034 continue; 4035 if (ifs->wpa_state > WPA_ASSOCIATED) 4036 return 1; 4037 } 4038 return 0; 4039 } 4040 4041 4042 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg) 4043 { 4044 struct wpa_supplicant *wpa_s = ctx; 4045 wpa_msg_global(wpa_s, level, "P2P: %s", msg); 4046 } 4047 4048 4049 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s, 4050 const char *conf_p2p_dev) 4051 { 4052 struct wpa_interface iface; 4053 struct wpa_supplicant *p2pdev_wpa_s; 4054 char ifname[100]; 4055 char force_name[100]; 4056 int ret; 4057 const u8 *if_addr = NULL; 4058 4059 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s", 4060 wpa_s->ifname); 4061 if (os_snprintf_error(sizeof(ifname), ret)) 4062 return -1; 4063 /* Cut length at the maximum size. Note that we don't need to ensure 4064 * collision free names here as the created interface is not a netdev. 4065 */ 4066 ifname[IFNAMSIZ - 1] = '\0'; 4067 force_name[0] = '\0'; 4068 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE; 4069 4070 if (wpa_s->conf->p2p_device_random_mac_addr == 2 && 4071 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr)) 4072 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr; 4073 4074 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL, 4075 force_name, wpa_s->pending_interface_addr, NULL); 4076 if (ret < 0) { 4077 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface"); 4078 return ret; 4079 } 4080 os_strlcpy(wpa_s->pending_interface_name, ifname, 4081 sizeof(wpa_s->pending_interface_name)); 4082 4083 os_memset(&iface, 0, sizeof(iface)); 4084 iface.p2p_mgmt = 1; 4085 iface.ifname = wpa_s->pending_interface_name; 4086 iface.driver = wpa_s->driver->name; 4087 iface.driver_param = wpa_s->conf->driver_param; 4088 4089 /* 4090 * If a P2P Device configuration file was given, use it as the interface 4091 * configuration file (instead of using parent's configuration file. 4092 */ 4093 if (conf_p2p_dev) { 4094 iface.confname = conf_p2p_dev; 4095 iface.ctrl_interface = NULL; 4096 } else { 4097 iface.confname = wpa_s->confname; 4098 iface.ctrl_interface = wpa_s->conf->ctrl_interface; 4099 } 4100 4101 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s); 4102 if (!p2pdev_wpa_s) { 4103 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface"); 4104 return -1; 4105 } 4106 4107 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s; 4108 wpa_s->pending_interface_name[0] = '\0'; 4109 return 0; 4110 } 4111 4112 4113 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status, 4114 const u8 *noa, size_t noa_len) 4115 { 4116 struct wpa_supplicant *wpa_s, *intf = ctx; 4117 char hex[100]; 4118 4119 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 4120 if (wpa_s->waiting_presence_resp) 4121 break; 4122 } 4123 if (!wpa_s) { 4124 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response"); 4125 return; 4126 } 4127 wpa_s->waiting_presence_resp = 0; 4128 4129 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len); 4130 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR 4131 " status=%u noa=%s", MAC2STR(src), status, hex); 4132 } 4133 4134 4135 static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid, 4136 size_t ssid_len, u8 *go_dev_addr, 4137 u8 *ret_ssid, size_t *ret_ssid_len, 4138 u8 *intended_iface_addr) 4139 { 4140 struct wpa_supplicant *wpa_s = ctx; 4141 struct wpa_ssid *s; 4142 4143 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len); 4144 if (s) { 4145 os_memcpy(ret_ssid, s->ssid, s->ssid_len); 4146 *ret_ssid_len = s->ssid_len; 4147 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN); 4148 4149 if (s->mode != WPAS_MODE_P2P_GO) { 4150 os_memset(intended_iface_addr, 0, ETH_ALEN); 4151 } else if (wpas_p2p_create_iface(wpa_s)) { 4152 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO)) 4153 return 0; 4154 4155 os_memcpy(intended_iface_addr, 4156 wpa_s->pending_interface_addr, ETH_ALEN); 4157 } else { 4158 os_memcpy(intended_iface_addr, wpa_s->own_addr, 4159 ETH_ALEN); 4160 } 4161 return 1; 4162 } 4163 4164 return 0; 4165 } 4166 4167 4168 static int wpas_get_go_info(void *ctx, u8 *intended_addr, 4169 u8 *ssid, size_t *ssid_len, int *group_iface, 4170 unsigned int *freq) 4171 { 4172 struct wpa_supplicant *wpa_s = ctx; 4173 struct wpa_supplicant *go; 4174 struct wpa_ssid *s; 4175 4176 /* 4177 * group_iface will be set to 1 only if a dedicated interface for P2P 4178 * role is required. First, we try to reuse an active GO. However, 4179 * if it is not present, we will try to reactivate an existing 4180 * persistent group and set group_iface to 1, so the caller will know 4181 * that the pending interface should be used. 4182 */ 4183 *group_iface = 0; 4184 4185 if (freq) 4186 *freq = 0; 4187 4188 go = wpas_p2p_get_go_group(wpa_s); 4189 if (!go) { 4190 s = wpas_p2p_get_persistent_go(wpa_s); 4191 *group_iface = wpas_p2p_create_iface(wpa_s); 4192 if (s) 4193 os_memcpy(intended_addr, s->bssid, ETH_ALEN); 4194 else 4195 return 0; 4196 } else { 4197 s = go->current_ssid; 4198 os_memcpy(intended_addr, go->own_addr, ETH_ALEN); 4199 if (freq) 4200 *freq = go->assoc_freq; 4201 } 4202 4203 os_memcpy(ssid, s->ssid, s->ssid_len); 4204 *ssid_len = s->ssid_len; 4205 4206 return 1; 4207 } 4208 4209 4210 static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go, 4211 const u8 *ssid, size_t ssid_len) 4212 { 4213 struct wpa_supplicant *wpa_s = ctx; 4214 struct wpa_ssid *s; 4215 int save_config = 0; 4216 size_t i; 4217 4218 /* Start with our first choice of Persistent Groups */ 4219 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) { 4220 if (go && ssid && ssid_len && 4221 s->ssid_len == ssid_len && 4222 os_memcmp(go, s->bssid, ETH_ALEN) == 0 && 4223 os_memcmp(ssid, s->ssid, ssid_len) == 0) 4224 break; 4225 4226 /* Remove stale persistent group */ 4227 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) { 4228 wpa_dbg(wpa_s, MSG_DEBUG, 4229 "P2P: Remove stale persistent group id=%d", 4230 s->id); 4231 wpas_notify_persistent_group_removed(wpa_s, s); 4232 wpa_config_remove_network(wpa_s->conf, s->id); 4233 save_config = 1; 4234 continue; 4235 } 4236 4237 for (i = 0; i < s->num_p2p_clients; i++) { 4238 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, 4239 peer, ETH_ALEN) != 0) 4240 continue; 4241 4242 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN, 4243 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN, 4244 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN); 4245 break; 4246 } 4247 s->num_p2p_clients--; 4248 save_config = 1; 4249 } 4250 4251 if (save_config) 4252 p2p_config_write(wpa_s); 4253 4254 /* Return TRUE if valid SSID remains */ 4255 return s != NULL; 4256 } 4257 4258 4259 static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len, 4260 const u8 *feat_cap, size_t feat_cap_len) 4261 { 4262 static const char pref[] = " feature_cap="; 4263 int ret; 4264 4265 buf[0] = '\0'; 4266 4267 /* 4268 * We expect a feature capability to contain at least one byte to be 4269 * reported. The string buffer provided by the caller function is 4270 * expected to be big enough to contain all bytes of the attribute for 4271 * known specifications. This function truncates the reported bytes if 4272 * the feature capability data exceeds the string buffer size. 4273 */ 4274 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2) 4275 return; 4276 4277 os_memcpy(buf, pref, sizeof(pref)); 4278 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1], 4279 buf_len - sizeof(pref) + 1, 4280 feat_cap, feat_cap_len); 4281 4282 if (ret != (2 * (int) feat_cap_len)) 4283 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated"); 4284 } 4285 4286 4287 static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev, 4288 const u8 *adv_mac, const u8 *ses_mac, 4289 const u8 *grp_mac, u32 adv_id, u32 ses_id, 4290 u8 conncap, int passwd_id, 4291 const u8 *persist_ssid, 4292 size_t persist_ssid_size, int response_done, 4293 int prov_start, const char *session_info, 4294 const u8 *feat_cap, size_t feat_cap_len, 4295 unsigned int freq, 4296 const u8 *group_ssid, size_t group_ssid_len) 4297 { 4298 struct wpa_supplicant *wpa_s = ctx; 4299 u8 mac[ETH_ALEN]; 4300 struct wpa_ssid *persistent_go, *stale, *s = NULL; 4301 int save_config = 0; 4302 struct wpa_supplicant *go_wpa_s; 4303 char feat_cap_str[256]; 4304 4305 if (!dev) 4306 return; 4307 4308 os_memset(mac, 0, ETH_ALEN); 4309 if (!adv_mac) 4310 adv_mac = mac; 4311 if (!ses_mac) 4312 ses_mac = mac; 4313 if (!grp_mac) 4314 grp_mac = mac; 4315 4316 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str), 4317 feat_cap, feat_cap_len); 4318 4319 if (prov_start) { 4320 if (session_info == NULL) { 4321 wpa_msg_global(wpa_s, MSG_INFO, 4322 P2P_EVENT_P2PS_PROVISION_START MACSTR 4323 " adv_id=%x conncap=%x" 4324 " adv_mac=" MACSTR 4325 " session=%x mac=" MACSTR 4326 " dev_passwd_id=%d%s", 4327 MAC2STR(dev), adv_id, conncap, 4328 MAC2STR(adv_mac), 4329 ses_id, MAC2STR(ses_mac), 4330 passwd_id, feat_cap_str); 4331 } else { 4332 wpa_msg_global(wpa_s, MSG_INFO, 4333 P2P_EVENT_P2PS_PROVISION_START MACSTR 4334 " adv_id=%x conncap=%x" 4335 " adv_mac=" MACSTR 4336 " session=%x mac=" MACSTR 4337 " dev_passwd_id=%d info='%s'%s", 4338 MAC2STR(dev), adv_id, conncap, 4339 MAC2STR(adv_mac), 4340 ses_id, MAC2STR(ses_mac), 4341 passwd_id, session_info, feat_cap_str); 4342 } 4343 return; 4344 } 4345 4346 go_wpa_s = wpas_p2p_get_go_group(wpa_s); 4347 persistent_go = wpas_p2p_get_persistent_go(wpa_s); 4348 4349 if (status && status != P2P_SC_SUCCESS_DEFERRED) { 4350 if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) 4351 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); 4352 4353 if (persistent_go && !persistent_go->num_p2p_clients) { 4354 /* remove empty persistent GO */ 4355 wpa_dbg(wpa_s, MSG_DEBUG, 4356 "P2P: Remove empty persistent group id=%d", 4357 persistent_go->id); 4358 wpas_notify_persistent_group_removed(wpa_s, 4359 persistent_go); 4360 wpa_config_remove_network(wpa_s->conf, 4361 persistent_go->id); 4362 } 4363 4364 wpa_msg_global(wpa_s, MSG_INFO, 4365 P2P_EVENT_P2PS_PROVISION_DONE MACSTR 4366 " status=%d" 4367 " adv_id=%x adv_mac=" MACSTR 4368 " session=%x mac=" MACSTR "%s", 4369 MAC2STR(dev), status, 4370 adv_id, MAC2STR(adv_mac), 4371 ses_id, MAC2STR(ses_mac), feat_cap_str); 4372 return; 4373 } 4374 4375 /* Clean up stale persistent groups with this device */ 4376 if (persist_ssid && persist_ssid_size) 4377 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid, 4378 persist_ssid_size); 4379 4380 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO && 4381 is_zero_ether_addr(grp_mac)) { 4382 wpa_dbg(wpa_s, MSG_ERROR, 4383 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address"); 4384 return; 4385 } 4386 4387 for (;;) { 4388 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0); 4389 if (!stale) 4390 break; 4391 4392 if (s && s->ssid_len == stale->ssid_len && 4393 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 && 4394 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0) 4395 break; 4396 4397 /* Remove stale persistent group */ 4398 if (stale->mode != WPAS_MODE_P2P_GO || 4399 stale->num_p2p_clients <= 1) { 4400 wpa_dbg(wpa_s, MSG_DEBUG, 4401 "P2P: Remove stale persistent group id=%d", 4402 stale->id); 4403 wpas_notify_persistent_group_removed(wpa_s, stale); 4404 wpa_config_remove_network(wpa_s->conf, stale->id); 4405 } else { 4406 size_t i; 4407 4408 for (i = 0; i < stale->num_p2p_clients; i++) { 4409 if (os_memcmp(stale->p2p_client_list + 4410 i * ETH_ALEN, 4411 dev, ETH_ALEN) == 0) { 4412 os_memmove(stale->p2p_client_list + 4413 i * ETH_ALEN, 4414 stale->p2p_client_list + 4415 (i + 1) * ETH_ALEN, 4416 (stale->num_p2p_clients - 4417 i - 1) * ETH_ALEN); 4418 break; 4419 } 4420 } 4421 stale->num_p2p_clients--; 4422 } 4423 save_config = 1; 4424 } 4425 4426 if (save_config) 4427 p2p_config_write(wpa_s); 4428 4429 if (s) { 4430 if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) 4431 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); 4432 4433 if (persistent_go && s != persistent_go && 4434 !persistent_go->num_p2p_clients) { 4435 /* remove empty persistent GO */ 4436 wpa_dbg(wpa_s, MSG_DEBUG, 4437 "P2P: Remove empty persistent group id=%d", 4438 persistent_go->id); 4439 wpas_notify_persistent_group_removed(wpa_s, 4440 persistent_go); 4441 wpa_config_remove_network(wpa_s->conf, 4442 persistent_go->id); 4443 /* Save config */ 4444 } 4445 4446 wpa_msg_global(wpa_s, MSG_INFO, 4447 P2P_EVENT_P2PS_PROVISION_DONE MACSTR 4448 " status=%d" 4449 " adv_id=%x adv_mac=" MACSTR 4450 " session=%x mac=" MACSTR 4451 " persist=%d%s", 4452 MAC2STR(dev), status, 4453 adv_id, MAC2STR(adv_mac), 4454 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str); 4455 return; 4456 } 4457 4458 wpa_s->global->pending_p2ps_group = 0; 4459 wpa_s->global->pending_p2ps_group_freq = 0; 4460 4461 if (conncap == P2PS_SETUP_GROUP_OWNER) { 4462 /* 4463 * We need to copy the interface name. Simply saving a 4464 * pointer isn't enough, since if we use pending_interface_name 4465 * it will be overwritten when the group is added. 4466 */ 4467 char go_ifname[100]; 4468 4469 go_ifname[0] = '\0'; 4470 if (!go_wpa_s) { 4471 if (!response_done) { 4472 wpa_s->global->pending_p2ps_group = 1; 4473 wpa_s->global->pending_p2ps_group_freq = freq; 4474 } 4475 4476 if (!wpas_p2p_create_iface(wpa_s)) 4477 os_memcpy(go_ifname, wpa_s->ifname, 4478 sizeof(go_ifname)); 4479 else if (wpa_s->pending_interface_name[0]) 4480 os_memcpy(go_ifname, 4481 wpa_s->pending_interface_name, 4482 sizeof(go_ifname)); 4483 4484 if (!go_ifname[0]) { 4485 wpas_p2ps_prov_complete( 4486 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP, 4487 dev, adv_mac, ses_mac, 4488 grp_mac, adv_id, ses_id, 0, 0, 4489 NULL, 0, 0, 0, NULL, NULL, 0, 0, 4490 NULL, 0); 4491 return; 4492 } 4493 4494 /* If PD Resp complete, start up the GO */ 4495 if (response_done && persistent_go) { 4496 wpas_p2p_group_add_persistent( 4497 wpa_s, persistent_go, 4498 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL, 4499 persistent_go->mode == 4500 WPAS_MODE_P2P_GO ? 4501 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 4502 0, 0, false); 4503 } else if (response_done) { 4504 wpas_p2p_group_add(wpa_s, 1, freq, 4505 0, 0, 0, 0, 0, 0, false); 4506 } 4507 4508 if (passwd_id == DEV_PW_P2PS_DEFAULT) { 4509 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, 4510 ETH_ALEN); 4511 wpa_s->p2ps_method_config_any = 1; 4512 } 4513 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) { 4514 os_memcpy(go_ifname, go_wpa_s->ifname, 4515 sizeof(go_ifname)); 4516 4517 if (is_zero_ether_addr(grp_mac)) { 4518 wpa_dbg(go_wpa_s, MSG_DEBUG, 4519 "P2P: Setting PIN-1 for ANY"); 4520 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL, 4521 "12345670", NULL, 0, 4522 0); 4523 } else { 4524 wpa_dbg(go_wpa_s, MSG_DEBUG, 4525 "P2P: Setting PIN-1 for " MACSTR, 4526 MAC2STR(grp_mac)); 4527 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac, 4528 "12345670", NULL, 0, 4529 0); 4530 } 4531 4532 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN); 4533 wpa_s->p2ps_method_config_any = 1; 4534 } 4535 4536 wpa_msg_global(wpa_s, MSG_INFO, 4537 P2P_EVENT_P2PS_PROVISION_DONE MACSTR 4538 " status=%d conncap=%x" 4539 " adv_id=%x adv_mac=" MACSTR 4540 " session=%x mac=" MACSTR 4541 " dev_passwd_id=%d go=%s%s", 4542 MAC2STR(dev), status, conncap, 4543 adv_id, MAC2STR(adv_mac), 4544 ses_id, MAC2STR(ses_mac), 4545 passwd_id, go_ifname, feat_cap_str); 4546 return; 4547 } 4548 4549 if (go_wpa_s && !p2p_group_go_member_count(wpa_s)) 4550 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname); 4551 4552 if (persistent_go && !persistent_go->num_p2p_clients) { 4553 /* remove empty persistent GO */ 4554 wpa_dbg(wpa_s, MSG_DEBUG, 4555 "P2P: Remove empty persistent group id=%d", 4556 persistent_go->id); 4557 wpas_notify_persistent_group_removed(wpa_s, persistent_go); 4558 wpa_config_remove_network(wpa_s->conf, persistent_go->id); 4559 } 4560 4561 if (conncap == P2PS_SETUP_CLIENT) { 4562 char ssid_hex[32 * 2 + 1]; 4563 4564 if (group_ssid) 4565 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex), 4566 group_ssid, group_ssid_len); 4567 else 4568 ssid_hex[0] = '\0'; 4569 wpa_msg_global(wpa_s, MSG_INFO, 4570 P2P_EVENT_P2PS_PROVISION_DONE MACSTR 4571 " status=%d conncap=%x" 4572 " adv_id=%x adv_mac=" MACSTR 4573 " session=%x mac=" MACSTR 4574 " dev_passwd_id=%d join=" MACSTR "%s%s%s", 4575 MAC2STR(dev), status, conncap, 4576 adv_id, MAC2STR(adv_mac), 4577 ses_id, MAC2STR(ses_mac), 4578 passwd_id, MAC2STR(grp_mac), feat_cap_str, 4579 group_ssid ? " group_ssid=" : "", ssid_hex); 4580 } else { 4581 wpa_msg_global(wpa_s, MSG_INFO, 4582 P2P_EVENT_P2PS_PROVISION_DONE MACSTR 4583 " status=%d conncap=%x" 4584 " adv_id=%x adv_mac=" MACSTR 4585 " session=%x mac=" MACSTR 4586 " dev_passwd_id=%d%s", 4587 MAC2STR(dev), status, conncap, 4588 adv_id, MAC2STR(adv_mac), 4589 ses_id, MAC2STR(ses_mac), 4590 passwd_id, feat_cap_str); 4591 } 4592 } 4593 4594 4595 static int _wpas_p2p_in_progress(void *ctx) 4596 { 4597 struct wpa_supplicant *wpa_s = ctx; 4598 return wpas_p2p_in_progress(wpa_s); 4599 } 4600 4601 4602 static int wpas_prov_disc_resp_cb(void *ctx) 4603 { 4604 struct wpa_supplicant *wpa_s = ctx; 4605 struct wpa_ssid *persistent_go; 4606 unsigned int freq; 4607 4608 if (!wpa_s->global->pending_p2ps_group) 4609 return 0; 4610 4611 freq = wpa_s->global->pending_p2ps_group_freq; 4612 wpa_s->global->pending_p2ps_group_freq = 0; 4613 wpa_s->global->pending_p2ps_group = 0; 4614 4615 if (wpas_p2p_get_go_group(wpa_s)) 4616 return 0; 4617 persistent_go = wpas_p2p_get_persistent_go(wpa_s); 4618 4619 if (persistent_go) { 4620 wpas_p2p_group_add_persistent( 4621 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4622 NULL, 4623 persistent_go->mode == WPAS_MODE_P2P_GO ? 4624 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0, 4625 is_p2p_allow_6ghz(wpa_s->global->p2p)); 4626 } else { 4627 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0, 4628 is_p2p_allow_6ghz(wpa_s->global->p2p)); 4629 } 4630 4631 return 1; 4632 } 4633 4634 4635 static int wpas_p2p_get_pref_freq_list(void *ctx, int go, 4636 unsigned int *len, 4637 unsigned int *freq_list) 4638 { 4639 struct wpa_supplicant *wpa_s = ctx; 4640 4641 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO : 4642 WPA_IF_P2P_CLIENT, len, freq_list); 4643 } 4644 4645 4646 int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s) 4647 { 4648 u8 addr[ETH_ALEN] = {0}; 4649 4650 if (wpa_s->conf->p2p_device_random_mac_addr == 0) 4651 return 0; 4652 4653 if (wpa_s->conf->p2p_device_random_mac_addr == 2) { 4654 if (is_zero_ether_addr( 4655 wpa_s->conf->p2p_device_persistent_mac_addr) && 4656 !is_zero_ether_addr(wpa_s->own_addr)) { 4657 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, 4658 wpa_s->own_addr, ETH_ALEN); 4659 } 4660 return 0; 4661 } 4662 4663 if (!wpa_s->conf->ssid) { 4664 if (random_mac_addr(addr) < 0) { 4665 wpa_msg(wpa_s, MSG_INFO, 4666 "Failed to generate random MAC address"); 4667 return -EINVAL; 4668 } 4669 4670 /* Store generated MAC address. */ 4671 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr, 4672 ETH_ALEN); 4673 } else { 4674 /* If there are existing saved groups, restore last MAC address. 4675 * if there is no last used MAC address, the last one is 4676 * factory MAC. */ 4677 if (is_zero_ether_addr( 4678 wpa_s->conf->p2p_device_persistent_mac_addr)) 4679 return 0; 4680 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr, 4681 ETH_ALEN); 4682 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address."); 4683 } 4684 4685 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) { 4686 wpa_msg(wpa_s, MSG_INFO, 4687 "Failed to set random MAC address"); 4688 return -EINVAL; 4689 } 4690 4691 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) { 4692 wpa_msg(wpa_s, MSG_INFO, 4693 "Could not update MAC address information"); 4694 return -EINVAL; 4695 } 4696 4697 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR, 4698 MAC2STR(addr)); 4699 4700 return 0; 4701 } 4702 4703 4704 /** 4705 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant 4706 * @global: Pointer to global data from wpa_supplicant_init() 4707 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 4708 * Returns: 0 on success, -1 on failure 4709 */ 4710 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s) 4711 { 4712 struct p2p_config p2p; 4713 int i; 4714 4715 if (wpa_s->conf->p2p_disabled) 4716 return 0; 4717 4718 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) 4719 return 0; 4720 4721 if (global->p2p) 4722 return 0; 4723 4724 if (wpas_p2p_mac_setup(wpa_s) < 0) { 4725 wpa_msg(wpa_s, MSG_ERROR, 4726 "Failed to initialize P2P random MAC address."); 4727 return -1; 4728 } 4729 4730 os_memset(&p2p, 0, sizeof(p2p)); 4731 p2p.cb_ctx = wpa_s; 4732 p2p.debug_print = wpas_p2p_debug_print; 4733 p2p.p2p_scan = wpas_p2p_scan; 4734 p2p.send_action = wpas_send_action; 4735 p2p.send_action_done = wpas_send_action_done; 4736 p2p.go_neg_completed = wpas_go_neg_completed; 4737 p2p.go_neg_req_rx = wpas_go_neg_req_rx; 4738 p2p.dev_found = wpas_dev_found; 4739 p2p.dev_lost = wpas_dev_lost; 4740 p2p.find_stopped = wpas_find_stopped; 4741 p2p.start_listen = wpas_start_listen; 4742 p2p.stop_listen = wpas_stop_listen; 4743 p2p.send_probe_resp = wpas_send_probe_resp; 4744 p2p.sd_request = wpas_sd_request; 4745 p2p.sd_response = wpas_sd_response; 4746 p2p.prov_disc_req = wpas_prov_disc_req; 4747 p2p.prov_disc_resp = wpas_prov_disc_resp; 4748 p2p.prov_disc_fail = wpas_prov_disc_fail; 4749 p2p.invitation_process = wpas_invitation_process; 4750 p2p.invitation_received = wpas_invitation_received; 4751 p2p.invitation_result = wpas_invitation_result; 4752 p2p.get_noa = wpas_get_noa; 4753 p2p.go_connected = wpas_go_connected; 4754 p2p.presence_resp = wpas_presence_resp; 4755 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active; 4756 p2p.is_p2p_in_progress = _wpas_p2p_in_progress; 4757 p2p.get_persistent_group = wpas_get_persistent_group; 4758 p2p.get_go_info = wpas_get_go_info; 4759 p2p.remove_stale_groups = wpas_remove_stale_groups; 4760 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete; 4761 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb; 4762 p2p.p2ps_group_capability = p2ps_group_capability; 4763 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list; 4764 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable; 4765 4766 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN); 4767 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN); 4768 p2p.dev_name = wpa_s->conf->device_name; 4769 p2p.manufacturer = wpa_s->conf->manufacturer; 4770 p2p.model_name = wpa_s->conf->model_name; 4771 p2p.model_number = wpa_s->conf->model_number; 4772 p2p.serial_number = wpa_s->conf->serial_number; 4773 if (wpa_s->wps) { 4774 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16); 4775 p2p.config_methods = wpa_s->wps->config_methods; 4776 } 4777 4778 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels, 4779 p2p.p2p_6ghz_disable)) { 4780 wpa_printf(MSG_ERROR, 4781 "P2P: Failed to configure supported channel list"); 4782 return -1; 4783 } 4784 4785 if (wpa_s->conf->p2p_listen_reg_class && 4786 wpa_s->conf->p2p_listen_channel) { 4787 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class; 4788 p2p.channel = wpa_s->conf->p2p_listen_channel; 4789 p2p.channel_forced = 1; 4790 } else { 4791 /* 4792 * Pick one of the social channels randomly as the listen 4793 * channel. 4794 */ 4795 if (p2p_config_get_random_social(&p2p, &p2p.reg_class, 4796 &p2p.channel, 4797 &global->p2p_go_avoid_freq, 4798 &global->p2p_disallow_freq) != 4799 0) { 4800 wpa_printf(MSG_INFO, 4801 "P2P: No social channels supported by the driver - do not enable P2P"); 4802 return 0; 4803 } 4804 p2p.channel_forced = 0; 4805 } 4806 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d", 4807 p2p.reg_class, p2p.channel); 4808 4809 if (wpa_s->conf->p2p_oper_reg_class && 4810 wpa_s->conf->p2p_oper_channel) { 4811 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class; 4812 p2p.op_channel = wpa_s->conf->p2p_oper_channel; 4813 p2p.cfg_op_channel = 1; 4814 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: " 4815 "%d:%d", p2p.op_reg_class, p2p.op_channel); 4816 4817 } else { 4818 /* 4819 * Use random operation channel from 2.4 GHz band social 4820 * channels (1, 6, 11) or band 60 GHz social channel (2) if no 4821 * other preference is indicated. 4822 */ 4823 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class, 4824 &p2p.op_channel, NULL, 4825 NULL) != 0) { 4826 wpa_printf(MSG_INFO, 4827 "P2P: Failed to select random social channel as operation channel"); 4828 p2p.op_reg_class = 0; 4829 p2p.op_channel = 0; 4830 /* This will be overridden during group setup in 4831 * p2p_prepare_channel(), so allow setup to continue. */ 4832 } 4833 p2p.cfg_op_channel = 0; 4834 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: " 4835 "%d:%d", p2p.op_reg_class, p2p.op_channel); 4836 } 4837 4838 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) { 4839 p2p.pref_chan = wpa_s->conf->p2p_pref_chan; 4840 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan; 4841 } 4842 4843 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) { 4844 os_memcpy(p2p.country, wpa_s->conf->country, 2); 4845 p2p.country[2] = 0x04; 4846 } else 4847 os_memcpy(p2p.country, "XX\x04", 3); 4848 4849 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type, 4850 WPS_DEV_TYPE_LEN); 4851 4852 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types; 4853 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type, 4854 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN); 4855 4856 p2p.concurrent_operations = !!(wpa_s->drv_flags & 4857 WPA_DRIVER_FLAGS_P2P_CONCURRENT); 4858 4859 p2p.max_peers = 100; 4860 4861 if (wpa_s->conf->p2p_ssid_postfix) { 4862 p2p.ssid_postfix_len = 4863 os_strlen(wpa_s->conf->p2p_ssid_postfix); 4864 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix)) 4865 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix); 4866 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix, 4867 p2p.ssid_postfix_len); 4868 } 4869 4870 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss; 4871 4872 p2p.max_listen = wpa_s->max_remain_on_chan; 4873 4874 if (wpa_s->conf->p2p_passphrase_len >= 8 && 4875 wpa_s->conf->p2p_passphrase_len <= 63) 4876 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len; 4877 else 4878 p2p.passphrase_len = 8; 4879 4880 global->p2p = p2p_init(&p2p); 4881 if (global->p2p == NULL) 4882 return -1; 4883 global->p2p_init_wpa_s = wpa_s; 4884 4885 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { 4886 if (wpa_s->conf->wps_vendor_ext[i] == NULL) 4887 continue; 4888 p2p_add_wps_vendor_extension( 4889 global->p2p, wpa_s->conf->wps_vendor_ext[i]); 4890 } 4891 4892 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq); 4893 4894 return 0; 4895 } 4896 4897 4898 /** 4899 * wpas_p2p_deinit - Deinitialize per-interface P2P data 4900 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 4901 * 4902 * This function deinitialize per-interface P2P data. 4903 */ 4904 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s) 4905 { 4906 if (wpa_s->driver && wpa_s->drv_priv) 4907 wpa_drv_probe_req_report(wpa_s, 0); 4908 4909 if (wpa_s->go_params) { 4910 /* Clear any stored provisioning info */ 4911 p2p_clear_provisioning_info( 4912 wpa_s->global->p2p, 4913 wpa_s->go_params->peer_device_addr); 4914 } 4915 4916 os_free(wpa_s->go_params); 4917 wpa_s->go_params = NULL; 4918 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL); 4919 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); 4920 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 4921 wpa_s->global->p2p_long_listen = 0; 4922 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 4923 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); 4924 wpas_p2p_remove_pending_group_interface(wpa_s); 4925 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL); 4926 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL); 4927 wpas_p2p_listen_work_done(wpa_s); 4928 if (wpa_s->p2p_send_action_work) { 4929 os_free(wpa_s->p2p_send_action_work->ctx); 4930 radio_work_done(wpa_s->p2p_send_action_work); 4931 wpa_s->p2p_send_action_work = NULL; 4932 } 4933 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL); 4934 4935 wpabuf_free(wpa_s->p2p_oob_dev_pw); 4936 wpa_s->p2p_oob_dev_pw = NULL; 4937 4938 os_free(wpa_s->p2p_group_common_freqs); 4939 wpa_s->p2p_group_common_freqs = NULL; 4940 wpa_s->p2p_group_common_freqs_num = 0; 4941 4942 /* TODO: remove group interface from the driver if this wpa_s instance 4943 * is on top of a P2P group interface */ 4944 } 4945 4946 4947 /** 4948 * wpas_p2p_deinit_global - Deinitialize global P2P module 4949 * @global: Pointer to global data from wpa_supplicant_init() 4950 * 4951 * This function deinitializes the global (per device) P2P module. 4952 */ 4953 static void wpas_p2p_deinit_global(struct wpa_global *global) 4954 { 4955 struct wpa_supplicant *wpa_s, *tmp; 4956 4957 wpa_s = global->ifaces; 4958 4959 wpas_p2p_service_flush(global->p2p_init_wpa_s); 4960 4961 /* Remove remaining P2P group interfaces */ 4962 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) 4963 wpa_s = wpa_s->next; 4964 while (wpa_s) { 4965 tmp = global->ifaces; 4966 while (tmp && 4967 (tmp == wpa_s || 4968 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) { 4969 tmp = tmp->next; 4970 } 4971 if (tmp == NULL) 4972 break; 4973 /* Disconnect from the P2P group and deinit the interface */ 4974 wpas_p2p_disconnect(tmp); 4975 } 4976 4977 /* 4978 * Deinit GO data on any possibly remaining interface (if main 4979 * interface is used as GO). 4980 */ 4981 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 4982 if (wpa_s->ap_iface) 4983 wpas_p2p_group_deinit(wpa_s); 4984 } 4985 4986 p2p_deinit(global->p2p); 4987 global->p2p = NULL; 4988 global->p2p_init_wpa_s = NULL; 4989 } 4990 4991 4992 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s) 4993 { 4994 if (wpa_s->conf->p2p_no_group_iface) 4995 return 0; /* separate interface disabled per configuration */ 4996 if (wpa_s->drv_flags & 4997 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE | 4998 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P)) 4999 return 1; /* P2P group requires a new interface in every case 5000 */ 5001 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)) 5002 return 0; /* driver does not support concurrent operations */ 5003 if (wpa_s->global->ifaces->next) 5004 return 1; /* more that one interface already in use */ 5005 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) 5006 return 1; /* this interface is already in use */ 5007 return 0; 5008 } 5009 5010 5011 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s, 5012 const u8 *peer_addr, 5013 enum p2p_wps_method wps_method, 5014 int go_intent, const u8 *own_interface_addr, 5015 unsigned int force_freq, int persistent_group, 5016 struct wpa_ssid *ssid, unsigned int pref_freq) 5017 { 5018 if (persistent_group && wpa_s->conf->persistent_reconnect) 5019 persistent_group = 2; 5020 5021 /* 5022 * Increase GO config timeout if HT40 is used since it takes some time 5023 * to scan channels for coex purposes before the BSS can be started. 5024 */ 5025 p2p_set_config_timeout(wpa_s->global->p2p, 5026 wpa_s->p2p_go_ht40 ? 255 : 100, 20); 5027 5028 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method, 5029 go_intent, own_interface_addr, force_freq, 5030 persistent_group, ssid ? ssid->ssid : NULL, 5031 ssid ? ssid->ssid_len : 0, 5032 wpa_s->p2p_pd_before_go_neg, pref_freq, 5033 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : 5034 0); 5035 } 5036 5037 5038 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s, 5039 const u8 *peer_addr, 5040 enum p2p_wps_method wps_method, 5041 int go_intent, const u8 *own_interface_addr, 5042 unsigned int force_freq, int persistent_group, 5043 struct wpa_ssid *ssid, unsigned int pref_freq) 5044 { 5045 if (persistent_group && wpa_s->conf->persistent_reconnect) 5046 persistent_group = 2; 5047 5048 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method, 5049 go_intent, own_interface_addr, force_freq, 5050 persistent_group, ssid ? ssid->ssid : NULL, 5051 ssid ? ssid->ssid_len : 0, pref_freq, 5052 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id : 5053 0); 5054 } 5055 5056 5057 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s) 5058 { 5059 wpa_s->p2p_join_scan_count++; 5060 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d", 5061 wpa_s->p2p_join_scan_count); 5062 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) { 5063 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR 5064 " for join operationg - stop join attempt", 5065 MAC2STR(wpa_s->pending_join_iface_addr)); 5066 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 5067 if (wpa_s->p2p_auto_pd) { 5068 wpa_s->p2p_auto_pd = 0; 5069 wpa_msg_global(wpa_s, MSG_INFO, 5070 P2P_EVENT_PROV_DISC_FAILURE 5071 " p2p_dev_addr=" MACSTR " status=N/A", 5072 MAC2STR(wpa_s->pending_join_dev_addr)); 5073 return; 5074 } 5075 if (wpa_s->p2p_fallback_to_go_neg) { 5076 wpa_dbg(wpa_s, MSG_DEBUG, 5077 "P2P: Join operation failed - fall back to GO Negotiation"); 5078 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 5079 P2P_EVENT_FALLBACK_TO_GO_NEG 5080 "reason=join-failed"); 5081 wpas_p2p_fallback_to_go_neg(wpa_s, 0); 5082 return; 5083 } 5084 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 5085 P2P_EVENT_GROUP_FORMATION_FAILURE); 5086 wpas_notify_p2p_group_formation_failure(wpa_s, ""); 5087 } 5088 } 5089 5090 5091 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq) 5092 { 5093 int res; 5094 unsigned int num, i; 5095 struct wpa_used_freq_data *freqs; 5096 5097 if (wpas_p2p_num_unused_channels(wpa_s) > 0) { 5098 /* Multiple channels are supported and not all are in use */ 5099 return 0; 5100 } 5101 5102 freqs = os_calloc(wpa_s->num_multichan_concurrent, 5103 sizeof(struct wpa_used_freq_data)); 5104 if (!freqs) 5105 return 1; 5106 5107 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, 5108 wpa_s->num_multichan_concurrent); 5109 5110 for (i = 0; i < num; i++) { 5111 if (freqs[i].freq == freq) { 5112 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used", 5113 freq); 5114 res = 0; 5115 goto exit_free; 5116 } 5117 } 5118 5119 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies"); 5120 res = 1; 5121 5122 exit_free: 5123 os_free(freqs); 5124 return res; 5125 } 5126 5127 5128 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s, 5129 const u8 *peer_dev_addr) 5130 { 5131 struct wpa_bss *bss; 5132 int updated; 5133 5134 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr); 5135 if (bss == NULL) 5136 return -1; 5137 if (bss->last_update_idx < wpa_s->bss_update_idx) { 5138 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the " 5139 "last scan"); 5140 return 0; 5141 } 5142 5143 updated = os_reltime_before(&wpa_s->p2p_auto_started, 5144 &bss->last_update); 5145 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at " 5146 "%ld.%06ld (%supdated in last scan)", 5147 bss->last_update.sec, bss->last_update.usec, 5148 updated ? "": "not "); 5149 5150 return updated; 5151 } 5152 5153 5154 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s, 5155 struct wpa_scan_results *scan_res) 5156 { 5157 struct wpa_bss *bss = NULL; 5158 int freq; 5159 u8 iface_addr[ETH_ALEN]; 5160 5161 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 5162 5163 if (wpa_s->global->p2p_disabled) 5164 return; 5165 5166 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin", 5167 scan_res ? (int) scan_res->num : -1, 5168 wpa_s->p2p_auto_join ? "auto_" : ""); 5169 5170 if (scan_res) 5171 wpas_p2p_scan_res_handler(wpa_s, scan_res); 5172 5173 if (wpa_s->p2p_auto_pd) { 5174 int join = wpas_p2p_peer_go(wpa_s, 5175 wpa_s->pending_join_dev_addr); 5176 if (join == 0 && 5177 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) { 5178 wpa_s->auto_pd_scan_retry++; 5179 bss = wpa_bss_get_bssid_latest( 5180 wpa_s, wpa_s->pending_join_dev_addr); 5181 if (bss) { 5182 freq = bss->freq; 5183 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for " 5184 "the peer " MACSTR " at %d MHz", 5185 wpa_s->auto_pd_scan_retry, 5186 MAC2STR(wpa_s-> 5187 pending_join_dev_addr), 5188 freq); 5189 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0); 5190 return; 5191 } 5192 } 5193 5194 if (join < 0) 5195 join = 0; 5196 5197 wpa_s->p2p_auto_pd = 0; 5198 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG; 5199 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d", 5200 MAC2STR(wpa_s->pending_join_dev_addr), join); 5201 if (p2p_prov_disc_req(wpa_s->global->p2p, 5202 wpa_s->pending_join_dev_addr, NULL, 5203 wpa_s->pending_pd_config_methods, join, 5204 0, wpa_s->user_initiated_pd) < 0) { 5205 wpa_s->p2p_auto_pd = 0; 5206 wpa_msg_global(wpa_s, MSG_INFO, 5207 P2P_EVENT_PROV_DISC_FAILURE 5208 " p2p_dev_addr=" MACSTR " status=N/A", 5209 MAC2STR(wpa_s->pending_join_dev_addr)); 5210 } 5211 return; 5212 } 5213 5214 if (wpa_s->p2p_auto_join) { 5215 int join = wpas_p2p_peer_go(wpa_s, 5216 wpa_s->pending_join_dev_addr); 5217 if (join < 0) { 5218 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be " 5219 "running a GO -> use GO Negotiation"); 5220 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 5221 P2P_EVENT_FALLBACK_TO_GO_NEG 5222 "reason=peer-not-running-GO"); 5223 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, 5224 wpa_s->p2p_pin, wpa_s->p2p_wps_method, 5225 wpa_s->p2p_persistent_group, 0, 0, 0, 5226 wpa_s->p2p_go_intent, 5227 wpa_s->p2p_connect_freq, 5228 wpa_s->p2p_go_vht_center_freq2, 5229 wpa_s->p2p_persistent_id, 5230 wpa_s->p2p_pd_before_go_neg, 5231 wpa_s->p2p_go_ht40, 5232 wpa_s->p2p_go_vht, 5233 wpa_s->p2p_go_max_oper_chwidth, 5234 wpa_s->p2p_go_he, 5235 wpa_s->p2p_go_edmg, 5236 NULL, 0, 5237 is_p2p_allow_6ghz(wpa_s->global->p2p)); 5238 return; 5239 } 5240 5241 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> " 5242 "try to join the group", join ? "" : 5243 " in older scan"); 5244 if (!join) { 5245 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 5246 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED); 5247 wpa_s->p2p_fallback_to_go_neg = 1; 5248 } 5249 } 5250 5251 freq = p2p_get_oper_freq(wpa_s->global->p2p, 5252 wpa_s->pending_join_iface_addr); 5253 if (freq < 0 && 5254 p2p_get_interface_addr(wpa_s->global->p2p, 5255 wpa_s->pending_join_dev_addr, 5256 iface_addr) == 0 && 5257 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0 5258 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) { 5259 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface " 5260 "address for join from " MACSTR " to " MACSTR 5261 " based on newly discovered P2P peer entry", 5262 MAC2STR(wpa_s->pending_join_iface_addr), 5263 MAC2STR(iface_addr)); 5264 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, 5265 ETH_ALEN); 5266 5267 freq = p2p_get_oper_freq(wpa_s->global->p2p, 5268 wpa_s->pending_join_iface_addr); 5269 } 5270 if (freq >= 0) { 5271 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " 5272 "from P2P peer table: %d MHz", freq); 5273 } 5274 if (wpa_s->p2p_join_ssid_len) { 5275 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " 5276 MACSTR " and SSID %s", 5277 MAC2STR(wpa_s->pending_join_iface_addr), 5278 wpa_ssid_txt(wpa_s->p2p_join_ssid, 5279 wpa_s->p2p_join_ssid_len)); 5280 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, 5281 wpa_s->p2p_join_ssid, 5282 wpa_s->p2p_join_ssid_len); 5283 } else if (!bss) { 5284 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID " 5285 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr)); 5286 bss = wpa_bss_get_bssid_latest(wpa_s, 5287 wpa_s->pending_join_iface_addr); 5288 } 5289 if (bss) { 5290 u8 dev_addr[ETH_ALEN]; 5291 5292 freq = bss->freq; 5293 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency " 5294 "from BSS table: %d MHz (SSID %s)", freq, 5295 wpa_ssid_txt(bss->ssid, bss->ssid_len)); 5296 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, 5297 dev_addr) == 0 && 5298 os_memcmp(wpa_s->pending_join_dev_addr, 5299 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 && 5300 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr, 5301 ETH_ALEN) != 0) { 5302 wpa_printf(MSG_DEBUG, 5303 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")", 5304 MAC2STR(dev_addr), 5305 MAC2STR(wpa_s->pending_join_dev_addr)); 5306 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, 5307 ETH_ALEN); 5308 } 5309 } 5310 if (freq > 0) { 5311 u16 method; 5312 5313 if (wpas_check_freq_conflict(wpa_s, freq) > 0) { 5314 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 5315 P2P_EVENT_GROUP_FORMATION_FAILURE 5316 "reason=FREQ_CONFLICT"); 5317 wpas_notify_p2p_group_formation_failure( 5318 wpa_s, "FREQ_CONFLICT"); 5319 return; 5320 } 5321 5322 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request " 5323 "prior to joining an existing group (GO " MACSTR 5324 " freq=%u MHz)", 5325 MAC2STR(wpa_s->pending_join_dev_addr), freq); 5326 wpa_s->pending_pd_before_join = 1; 5327 5328 switch (wpa_s->pending_join_wps_method) { 5329 case WPS_PIN_DISPLAY: 5330 method = WPS_CONFIG_KEYPAD; 5331 break; 5332 case WPS_PIN_KEYPAD: 5333 method = WPS_CONFIG_DISPLAY; 5334 break; 5335 case WPS_PBC: 5336 method = WPS_CONFIG_PUSHBUTTON; 5337 break; 5338 case WPS_P2PS: 5339 method = WPS_CONFIG_P2PS; 5340 break; 5341 default: 5342 method = 0; 5343 break; 5344 } 5345 5346 if ((p2p_get_provisioning_info(wpa_s->global->p2p, 5347 wpa_s->pending_join_dev_addr) == 5348 method)) { 5349 /* 5350 * We have already performed provision discovery for 5351 * joining the group. Proceed directly to join 5352 * operation without duplicated provision discovery. */ 5353 wpa_printf(MSG_DEBUG, "P2P: Provision discovery " 5354 "with " MACSTR " already done - proceed to " 5355 "join", 5356 MAC2STR(wpa_s->pending_join_dev_addr)); 5357 wpa_s->pending_pd_before_join = 0; 5358 goto start; 5359 } 5360 5361 if (p2p_prov_disc_req(wpa_s->global->p2p, 5362 wpa_s->pending_join_dev_addr, 5363 NULL, method, 1, 5364 freq, wpa_s->user_initiated_pd) < 0) { 5365 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision " 5366 "Discovery Request before joining an " 5367 "existing group"); 5368 wpa_s->pending_pd_before_join = 0; 5369 goto start; 5370 } 5371 return; 5372 } 5373 5374 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later"); 5375 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 5376 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); 5377 wpas_p2p_check_join_scan_limit(wpa_s); 5378 return; 5379 5380 start: 5381 /* Start join operation immediately */ 5382 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid, 5383 wpa_s->p2p_join_ssid_len); 5384 } 5385 5386 5387 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq, 5388 const u8 *ssid, size_t ssid_len) 5389 { 5390 int ret; 5391 struct wpa_driver_scan_params params; 5392 struct wpabuf *wps_ie, *ies; 5393 size_t ielen; 5394 int freqs[2] = { 0, 0 }; 5395 unsigned int bands; 5396 5397 os_memset(¶ms, 0, sizeof(params)); 5398 5399 /* P2P Wildcard SSID */ 5400 params.num_ssids = 1; 5401 if (ssid && ssid_len) { 5402 params.ssids[0].ssid = ssid; 5403 params.ssids[0].ssid_len = ssid_len; 5404 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len); 5405 wpa_s->p2p_join_ssid_len = ssid_len; 5406 } else { 5407 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID; 5408 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN; 5409 wpa_s->p2p_join_ssid_len = 0; 5410 } 5411 5412 wpa_s->wps->dev.p2p = 1; 5413 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev, 5414 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0, 5415 NULL); 5416 if (wps_ie == NULL) { 5417 wpas_p2p_scan_res_join(wpa_s, NULL); 5418 return; 5419 } 5420 5421 if (!freq) { 5422 int oper_freq; 5423 /* 5424 * If freq is not provided, check the operating freq of the GO 5425 * and use a single channel scan on if possible. 5426 */ 5427 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p, 5428 wpa_s->pending_join_iface_addr); 5429 if (oper_freq > 0) 5430 freq = oper_freq; 5431 } 5432 if (freq > 0) { 5433 freqs[0] = freq; 5434 params.freqs = freqs; 5435 } else if (wpa_s->conf->p2p_6ghz_disable || 5436 !is_p2p_allow_6ghz(wpa_s->global->p2p)) { 5437 wpa_printf(MSG_DEBUG, 5438 "P2P: 6 GHz disabled - update the scan frequency list"); 5439 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, ¶ms, 5440 0); 5441 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, ¶ms, 5442 0); 5443 } 5444 5445 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); 5446 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen); 5447 if (ies == NULL) { 5448 wpabuf_free(wps_ie); 5449 wpas_p2p_scan_res_join(wpa_s, NULL); 5450 return; 5451 } 5452 wpabuf_put_buf(ies, wps_ie); 5453 wpabuf_free(wps_ie); 5454 5455 bands = wpas_get_bands(wpa_s, freqs); 5456 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands); 5457 5458 params.p2p_probe = 1; 5459 params.extra_ies = wpabuf_head(ies); 5460 params.extra_ies_len = wpabuf_len(ies); 5461 5462 if (wpa_s->clear_driver_scan_cache) { 5463 wpa_printf(MSG_DEBUG, 5464 "Request driver to clear scan cache due to local BSS flush"); 5465 params.only_new_results = 1; 5466 } 5467 5468 /* 5469 * Run a scan to update BSS table and start Provision Discovery once 5470 * the new scan results become available. 5471 */ 5472 ret = wpa_drv_scan(wpa_s, ¶ms); 5473 if (params.freqs != freqs) 5474 os_free(params.freqs); 5475 if (!ret) { 5476 os_get_reltime(&wpa_s->scan_trigger_time); 5477 wpa_s->scan_res_handler = wpas_p2p_scan_res_join; 5478 wpa_s->own_scan_requested = 1; 5479 wpa_s->clear_driver_scan_cache = 0; 5480 } 5481 5482 wpabuf_free(ies); 5483 5484 if (ret) { 5485 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - " 5486 "try again later"); 5487 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 5488 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL); 5489 wpas_p2p_check_join_scan_limit(wpa_s); 5490 } 5491 } 5492 5493 5494 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx) 5495 { 5496 struct wpa_supplicant *wpa_s = eloop_ctx; 5497 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0); 5498 } 5499 5500 5501 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr, 5502 const u8 *dev_addr, enum p2p_wps_method wps_method, 5503 int auto_join, int op_freq, 5504 const u8 *ssid, size_t ssid_len) 5505 { 5506 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface " 5507 MACSTR " dev " MACSTR " op_freq=%d)%s", 5508 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq, 5509 auto_join ? " (auto_join)" : ""); 5510 if (ssid && ssid_len) { 5511 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s", 5512 wpa_ssid_txt(ssid, ssid_len)); 5513 } 5514 5515 wpa_s->p2p_auto_pd = 0; 5516 wpa_s->p2p_auto_join = !!auto_join; 5517 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN); 5518 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN); 5519 wpa_s->pending_join_wps_method = wps_method; 5520 5521 /* Make sure we are not running find during connection establishment */ 5522 wpas_p2p_stop_find(wpa_s); 5523 5524 wpa_s->p2p_join_scan_count = 0; 5525 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len); 5526 return 0; 5527 } 5528 5529 5530 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq, 5531 const u8 *ssid, size_t ssid_len) 5532 { 5533 struct wpa_supplicant *group; 5534 struct p2p_go_neg_results res; 5535 struct wpa_bss *bss; 5536 5537 group = wpas_p2p_get_group_iface(wpa_s, 0, 0); 5538 if (group == NULL) 5539 return -1; 5540 if (group != wpa_s) { 5541 os_memcpy(group->p2p_pin, wpa_s->p2p_pin, 5542 sizeof(group->p2p_pin)); 5543 group->p2p_wps_method = wpa_s->p2p_wps_method; 5544 } 5545 5546 /* 5547 * Need to mark the current interface for p2p_group_formation 5548 * when a separate group interface is not used. This is needed 5549 * to allow p2p_cancel stop a pending p2p_connect-join. 5550 * wpas_p2p_init_group_interface() addresses this for the case 5551 * where a separate group interface is used. 5552 */ 5553 if (group == wpa_s->parent) 5554 wpa_s->global->p2p_group_formation = group; 5555 5556 group->p2p_in_provisioning = 1; 5557 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg; 5558 5559 os_memset(&res, 0, sizeof(res)); 5560 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN); 5561 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr, 5562 ETH_ALEN); 5563 res.wps_method = wpa_s->pending_join_wps_method; 5564 if (freq && ssid && ssid_len) { 5565 res.freq = freq; 5566 res.ssid_len = ssid_len; 5567 os_memcpy(res.ssid, ssid, ssid_len); 5568 } else { 5569 if (ssid && ssid_len) { 5570 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr, 5571 ssid, ssid_len); 5572 } else { 5573 bss = wpa_bss_get_bssid_latest( 5574 wpa_s, wpa_s->pending_join_iface_addr); 5575 } 5576 if (bss) { 5577 res.freq = bss->freq; 5578 res.ssid_len = bss->ssid_len; 5579 os_memcpy(res.ssid, bss->ssid, bss->ssid_len); 5580 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)", 5581 bss->freq, 5582 wpa_ssid_txt(bss->ssid, bss->ssid_len)); 5583 } else if (ssid && ssid_len) { 5584 res.ssid_len = ssid_len; 5585 os_memcpy(res.ssid, ssid, ssid_len); 5586 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)", 5587 wpa_ssid_txt(ssid, ssid_len)); 5588 } 5589 } 5590 5591 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) { 5592 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to " 5593 "starting client"); 5594 wpa_drv_cancel_remain_on_channel(wpa_s); 5595 wpa_s->off_channel_freq = 0; 5596 wpa_s->roc_waiting_drv_freq = 0; 5597 } 5598 wpas_start_wps_enrollee(group, &res); 5599 5600 /* 5601 * Allow a longer timeout for join-a-running-group than normal 15 5602 * second group formation timeout since the GO may not have authorized 5603 * our connection yet. 5604 */ 5605 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL); 5606 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout, 5607 wpa_s, NULL); 5608 5609 return 0; 5610 } 5611 5612 5613 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq, 5614 int *force_freq, int *pref_freq, int go, 5615 unsigned int *pref_freq_list, 5616 unsigned int *num_pref_freq) 5617 { 5618 struct wpa_used_freq_data *freqs; 5619 int res, best_freq, num_unused; 5620 unsigned int freq_in_use = 0, num, i, max_pref_freq; 5621 5622 max_pref_freq = *num_pref_freq; 5623 *num_pref_freq = 0; 5624 5625 freqs = os_calloc(wpa_s->num_multichan_concurrent, 5626 sizeof(struct wpa_used_freq_data)); 5627 if (!freqs) 5628 return -1; 5629 5630 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, 5631 wpa_s->num_multichan_concurrent); 5632 5633 /* 5634 * It is possible that the total number of used frequencies is bigger 5635 * than the number of frequencies used for P2P, so get the system wide 5636 * number of unused frequencies. 5637 */ 5638 num_unused = wpas_p2p_num_unused_channels(wpa_s); 5639 5640 wpa_printf(MSG_DEBUG, 5641 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d", 5642 freq, wpa_s->num_multichan_concurrent, num, num_unused); 5643 5644 if (freq > 0) { 5645 int ret; 5646 if (go) 5647 ret = p2p_supported_freq(wpa_s->global->p2p, freq); 5648 else 5649 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq); 5650 if (!ret) { 5651 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && 5652 ieee80211_is_dfs(freq, wpa_s->hw.modes, 5653 wpa_s->hw.num_modes)) { 5654 /* 5655 * If freq is a DFS channel and DFS is offloaded 5656 * to the driver, allow P2P GO to use it. 5657 */ 5658 wpa_printf(MSG_DEBUG, 5659 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver", 5660 freq); 5661 } else { 5662 wpa_printf(MSG_DEBUG, 5663 "P2P: The forced channel (%u MHz) is not supported for P2P uses", 5664 freq); 5665 res = -3; 5666 goto exit_free; 5667 } 5668 } 5669 5670 for (i = 0; i < num; i++) { 5671 if (freqs[i].freq == freq) 5672 freq_in_use = 1; 5673 } 5674 5675 if (num_unused <= 0 && !freq_in_use) { 5676 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels", 5677 freq); 5678 res = -2; 5679 goto exit_free; 5680 } 5681 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the " 5682 "requested channel (%u MHz)", freq); 5683 *force_freq = freq; 5684 goto exit_ok; 5685 } 5686 5687 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); 5688 5689 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) { 5690 enum wpa_driver_if_type iface_type; 5691 5692 if (go) 5693 iface_type = WPA_IF_P2P_GO; 5694 else 5695 iface_type = WPA_IF_P2P_CLIENT; 5696 5697 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d", 5698 best_freq, go); 5699 5700 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, 5701 &max_pref_freq, 5702 pref_freq_list); 5703 if (!is_p2p_allow_6ghz(wpa_s->global->p2p)) 5704 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list, 5705 max_pref_freq); 5706 5707 if (!res && max_pref_freq > 0) { 5708 *num_pref_freq = max_pref_freq; 5709 i = 0; 5710 while (i < *num_pref_freq && 5711 (!p2p_supported_freq(wpa_s->global->p2p, 5712 pref_freq_list[i]) || 5713 wpas_p2p_disallowed_freq(wpa_s->global, 5714 pref_freq_list[i]))) { 5715 wpa_printf(MSG_DEBUG, 5716 "P2P: preferred_freq_list[%d]=%d is disallowed", 5717 i, pref_freq_list[i]); 5718 i++; 5719 } 5720 if (i != *num_pref_freq) { 5721 best_freq = pref_freq_list[i]; 5722 wpa_printf(MSG_DEBUG, 5723 "P2P: Using preferred_freq_list[%d]=%d", 5724 i, best_freq); 5725 } else { 5726 wpa_printf(MSG_DEBUG, 5727 "P2P: All driver preferred frequencies are disallowed for P2P use"); 5728 *num_pref_freq = 0; 5729 } 5730 } else { 5731 wpa_printf(MSG_DEBUG, 5732 "P2P: No preferred frequency list available"); 5733 } 5734 } 5735 5736 /* We have a candidate frequency to use */ 5737 if (best_freq > 0) { 5738 if (*pref_freq == 0 && num_unused > 0) { 5739 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using", 5740 best_freq); 5741 *pref_freq = best_freq; 5742 } else { 5743 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use", 5744 best_freq); 5745 *force_freq = best_freq; 5746 } 5747 } else if (num_unused > 0) { 5748 wpa_printf(MSG_DEBUG, 5749 "P2P: Current operating channels are not available for P2P. Try to use another channel"); 5750 *force_freq = 0; 5751 } else { 5752 wpa_printf(MSG_DEBUG, 5753 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group"); 5754 res = -2; 5755 goto exit_free; 5756 } 5757 5758 exit_ok: 5759 res = 0; 5760 exit_free: 5761 os_free(freqs); 5762 return res; 5763 } 5764 5765 5766 static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s, 5767 const u8 *peer_addr) 5768 { 5769 if (wpa_s->conf->p2p_6ghz_disable || 5770 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, 5771 HOSTAPD_MODE_IEEE80211A, true)) 5772 return false; 5773 5774 if (!p2p_wfd_enabled(wpa_s->global->p2p)) 5775 return false; 5776 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr)) 5777 return false; 5778 5779 return true; 5780 } 5781 5782 5783 static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s, 5784 const u8 *peer_addr, bool allow_6ghz, int freq) 5785 { 5786 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) { 5787 wpa_printf(MSG_DEBUG, 5788 "P2P: Allow connection on 6 GHz channels"); 5789 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true); 5790 } else { 5791 if (is_6ghz_freq(freq)) 5792 return -2; 5793 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false); 5794 } 5795 5796 return 0; 5797 } 5798 5799 5800 /** 5801 * wpas_p2p_connect - Request P2P Group Formation to be started 5802 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 5803 * @peer_addr: Address of the peer P2P Device 5804 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode 5805 * @persistent_group: Whether to create a persistent group 5806 * @auto_join: Whether to select join vs. GO Negotiation automatically 5807 * @join: Whether to join an existing group (as a client) instead of starting 5808 * Group Owner negotiation; @peer_addr is BSSID in that case 5809 * @auth: Whether to only authorize the connection instead of doing that and 5810 * initiating Group Owner negotiation 5811 * @go_intent: GO Intent or -1 to use default 5812 * @freq: Frequency for the group or 0 for auto-selection 5813 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode 5814 * @persistent_id: Persistent group credentials to use for forcing GO 5815 * parameters or -1 to generate new values (SSID/passphrase) 5816 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an 5817 * interoperability workaround when initiating group formation 5818 * @ht40: Start GO with 40 MHz channel width 5819 * @vht: Start GO with VHT support 5820 * @vht_chwidth: Channel width supported by GO operating with VHT support 5821 * (CHANWIDTH_*). 5822 * @group_ssid: Specific Group SSID for join or %NULL if not set 5823 * @group_ssid_len: Length of @group_ssid in octets 5824 * @allow_6ghz: Allow P2P connection on 6 GHz channels 5825 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified 5826 * failure, -2 on failure due to channel not currently available, 5827 * -3 if forced channel is not supported 5828 */ 5829 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 5830 const char *pin, enum p2p_wps_method wps_method, 5831 int persistent_group, int auto_join, int join, int auth, 5832 int go_intent, int freq, unsigned int vht_center_freq2, 5833 int persistent_id, int pd, int ht40, int vht, 5834 unsigned int vht_chwidth, int he, int edmg, 5835 const u8 *group_ssid, size_t group_ssid_len, 5836 bool allow_6ghz) 5837 { 5838 int force_freq = 0, pref_freq = 0; 5839 int ret = 0, res; 5840 enum wpa_driver_if_type iftype; 5841 const u8 *if_addr; 5842 struct wpa_ssid *ssid = NULL; 5843 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; 5844 5845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 5846 return -1; 5847 5848 if (persistent_id >= 0) { 5849 ssid = wpa_config_get_network(wpa_s->conf, persistent_id); 5850 if (ssid == NULL || ssid->disabled != 2 || 5851 ssid->mode != WPAS_MODE_P2P_GO) 5852 return -1; 5853 } 5854 5855 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq)) 5856 return -2; 5857 5858 os_free(wpa_s->global->add_psk); 5859 wpa_s->global->add_psk = NULL; 5860 5861 wpa_s->global->p2p_fail_on_wps_complete = 0; 5862 wpa_s->global->pending_p2ps_group = 0; 5863 wpa_s->global->pending_p2ps_group_freq = 0; 5864 wpa_s->p2ps_method_config_any = 0; 5865 5866 if (go_intent < 0) 5867 go_intent = wpa_s->conf->p2p_go_intent; 5868 5869 if (!auth) 5870 wpa_s->global->p2p_long_listen = 0; 5871 5872 wpa_s->p2p_wps_method = wps_method; 5873 wpa_s->p2p_persistent_group = !!persistent_group; 5874 wpa_s->p2p_persistent_id = persistent_id; 5875 wpa_s->p2p_go_intent = go_intent; 5876 wpa_s->p2p_connect_freq = freq; 5877 wpa_s->p2p_fallback_to_go_neg = 0; 5878 wpa_s->p2p_pd_before_go_neg = !!pd; 5879 wpa_s->p2p_go_ht40 = !!ht40; 5880 wpa_s->p2p_go_vht = !!vht; 5881 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2; 5882 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth; 5883 wpa_s->p2p_go_he = !!he; 5884 wpa_s->p2p_go_edmg = !!edmg; 5885 5886 if (pin) 5887 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin)); 5888 else if (wps_method == WPS_PIN_DISPLAY) { 5889 if (wps_generate_pin((unsigned int *) &ret) < 0) 5890 return -1; 5891 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), 5892 "%08d", ret); 5893 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res)) 5894 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0'; 5895 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s", 5896 wpa_s->p2p_pin); 5897 } else if (wps_method == WPS_P2PS) { 5898 /* Force the P2Ps default PIN to be used */ 5899 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin)); 5900 } else 5901 wpa_s->p2p_pin[0] = '\0'; 5902 5903 if (join || auto_join) { 5904 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN]; 5905 if (auth) { 5906 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to " 5907 "connect a running group from " MACSTR, 5908 MAC2STR(peer_addr)); 5909 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); 5910 return ret; 5911 } 5912 os_memcpy(dev_addr, peer_addr, ETH_ALEN); 5913 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr, 5914 iface_addr) < 0) { 5915 os_memcpy(iface_addr, peer_addr, ETH_ALEN); 5916 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr, 5917 dev_addr); 5918 } 5919 if (auto_join) { 5920 os_get_reltime(&wpa_s->p2p_auto_started); 5921 wpa_printf(MSG_DEBUG, "P2P: Auto join started at " 5922 "%ld.%06ld", 5923 wpa_s->p2p_auto_started.sec, 5924 wpa_s->p2p_auto_started.usec); 5925 } 5926 wpa_s->user_initiated_pd = 1; 5927 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method, 5928 auto_join, freq, 5929 group_ssid, group_ssid_len) < 0) 5930 return -1; 5931 return ret; 5932 } 5933 5934 size = P2P_MAX_PREF_CHANNELS; 5935 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, 5936 go_intent == 15, pref_freq_list, &size); 5937 if (res) 5938 return res; 5939 wpas_p2p_set_own_freq_preference(wpa_s, 5940 force_freq ? force_freq : pref_freq); 5941 5942 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size); 5943 5944 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); 5945 5946 if (wpa_s->create_p2p_iface) { 5947 /* Prepare to add a new interface for the group */ 5948 iftype = WPA_IF_P2P_GROUP; 5949 if (go_intent == 15) 5950 iftype = WPA_IF_P2P_GO; 5951 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { 5952 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " 5953 "interface for the group"); 5954 return -1; 5955 } 5956 5957 if_addr = wpa_s->pending_interface_addr; 5958 } else { 5959 if (wpa_s->p2p_mgmt) 5960 if_addr = wpa_s->parent->own_addr; 5961 else 5962 if_addr = wpa_s->own_addr; 5963 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); 5964 } 5965 5966 if (auth) { 5967 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method, 5968 go_intent, if_addr, 5969 force_freq, persistent_group, ssid, 5970 pref_freq) < 0) 5971 return -1; 5972 return ret; 5973 } 5974 5975 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method, 5976 go_intent, if_addr, force_freq, 5977 persistent_group, ssid, pref_freq) < 0) { 5978 if (wpa_s->create_p2p_iface) 5979 wpas_p2p_remove_pending_group_interface(wpa_s); 5980 return -1; 5981 } 5982 return ret; 5983 } 5984 5985 5986 /** 5987 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start 5988 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 5989 * @freq: Frequency of the channel in MHz 5990 * @duration: Duration of the stay on the channel in milliseconds 5991 * 5992 * This callback is called when the driver indicates that it has started the 5993 * requested remain-on-channel duration. 5994 */ 5995 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s, 5996 unsigned int freq, unsigned int duration) 5997 { 5998 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 5999 return; 6000 wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)", 6001 wpa_s->off_channel_freq, wpa_s->pending_listen_freq, 6002 wpa_s->roc_waiting_drv_freq, freq, duration); 6003 if (wpa_s->off_channel_freq && 6004 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) { 6005 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq, 6006 wpa_s->pending_listen_duration); 6007 wpa_s->pending_listen_freq = 0; 6008 } else { 6009 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)", 6010 wpa_s->off_channel_freq, wpa_s->pending_listen_freq, 6011 freq, duration); 6012 } 6013 } 6014 6015 6016 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout) 6017 { 6018 /* Limit maximum Listen state time based on driver limitation. */ 6019 if (timeout > wpa_s->max_remain_on_chan) 6020 timeout = wpa_s->max_remain_on_chan; 6021 6022 return p2p_listen(wpa_s->global->p2p, timeout); 6023 } 6024 6025 6026 /** 6027 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout 6028 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 6029 * @freq: Frequency of the channel in MHz 6030 * 6031 * This callback is called when the driver indicates that a remain-on-channel 6032 * operation has been completed, i.e., the duration on the requested channel 6033 * has timed out. 6034 */ 6035 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s, 6036 unsigned int freq) 6037 { 6038 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback " 6039 "(p2p_long_listen=%d ms pending_action_tx=%p)", 6040 wpa_s->global->p2p_long_listen, 6041 offchannel_pending_action_tx(wpa_s)); 6042 wpas_p2p_listen_work_done(wpa_s); 6043 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 6044 return; 6045 if (wpa_s->global->p2p_long_listen > 0) 6046 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan; 6047 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0) 6048 return; /* P2P module started a new operation */ 6049 if (offchannel_pending_action_tx(wpa_s)) 6050 return; 6051 if (wpa_s->global->p2p_long_listen > 0) { 6052 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state"); 6053 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen); 6054 } else { 6055 /* 6056 * When listen duration is over, stop listen & update p2p_state 6057 * to IDLE. 6058 */ 6059 p2p_stop_listen(wpa_s->global->p2p); 6060 } 6061 } 6062 6063 6064 /** 6065 * wpas_p2p_group_remove - Remove a P2P group 6066 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 6067 * @ifname: Network interface name of the group interface or "*" to remove all 6068 * groups 6069 * Returns: 0 on success, -1 on failure 6070 * 6071 * This function is used to remove a P2P group. This can be used to disconnect 6072 * from a group in which the local end is a P2P Client or to end a P2P Group in 6073 * case the local end is the Group Owner. If a virtual network interface was 6074 * created for this group, that interface will be removed. Otherwise, only the 6075 * configured P2P group network will be removed from the interface. 6076 */ 6077 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname) 6078 { 6079 struct wpa_global *global = wpa_s->global; 6080 struct wpa_supplicant *calling_wpa_s = wpa_s; 6081 6082 if (os_strcmp(ifname, "*") == 0) { 6083 struct wpa_supplicant *prev; 6084 bool calling_wpa_s_group_removed = false; 6085 6086 wpa_s = global->ifaces; 6087 while (wpa_s) { 6088 prev = wpa_s; 6089 wpa_s = wpa_s->next; 6090 if (prev->p2p_group_interface != 6091 NOT_P2P_GROUP_INTERFACE || 6092 (prev->current_ssid && 6093 prev->current_ssid->p2p_group)) { 6094 wpas_p2p_disconnect_safely(prev, calling_wpa_s); 6095 if (prev == calling_wpa_s) 6096 calling_wpa_s_group_removed = true; 6097 } 6098 } 6099 6100 if (!calling_wpa_s_group_removed && 6101 (calling_wpa_s->p2p_group_interface != 6102 NOT_P2P_GROUP_INTERFACE || 6103 (calling_wpa_s->current_ssid && 6104 calling_wpa_s->current_ssid->p2p_group))) { 6105 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group"); 6106 wpas_p2p_disconnect_safely(calling_wpa_s, 6107 calling_wpa_s); 6108 } 6109 6110 return 0; 6111 } 6112 6113 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 6114 if (os_strcmp(wpa_s->ifname, ifname) == 0) 6115 break; 6116 } 6117 6118 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s); 6119 } 6120 6121 6122 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq) 6123 { 6124 unsigned int r; 6125 6126 if (!wpa_s->conf->num_p2p_pref_chan && !freq) { 6127 unsigned int i, size = P2P_MAX_PREF_CHANNELS; 6128 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS]; 6129 int res; 6130 6131 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO, 6132 &size, pref_freq_list); 6133 if (!is_p2p_allow_6ghz(wpa_s->global->p2p)) 6134 size = p2p_remove_6ghz_channels(pref_freq_list, size); 6135 6136 if (!res && size > 0) { 6137 i = 0; 6138 while (i < size && 6139 (!p2p_supported_freq(wpa_s->global->p2p, 6140 pref_freq_list[i]) || 6141 wpas_p2p_disallowed_freq(wpa_s->global, 6142 pref_freq_list[i]))) { 6143 wpa_printf(MSG_DEBUG, 6144 "P2P: preferred_freq_list[%d]=%d is disallowed", 6145 i, pref_freq_list[i]); 6146 i++; 6147 } 6148 if (i != size) { 6149 freq = pref_freq_list[i]; 6150 wpa_printf(MSG_DEBUG, 6151 "P2P: Using preferred_freq_list[%d]=%d", 6152 i, freq); 6153 } else { 6154 wpa_printf(MSG_DEBUG, 6155 "P2P: All driver preferred frequencies are disallowed for P2P use"); 6156 } 6157 } else { 6158 wpa_printf(MSG_DEBUG, 6159 "P2P: No preferred frequency list available"); 6160 } 6161 } 6162 6163 if (freq == 2) { 6164 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz " 6165 "band"); 6166 if (wpa_s->best_24_freq > 0 && 6167 p2p_supported_freq_go(wpa_s->global->p2p, 6168 wpa_s->best_24_freq)) { 6169 freq = wpa_s->best_24_freq; 6170 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band " 6171 "channel: %d MHz", freq); 6172 } else { 6173 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 6174 return -1; 6175 freq = 2412 + (r % 3) * 25; 6176 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band " 6177 "channel: %d MHz", freq); 6178 } 6179 } 6180 6181 if (freq == 5) { 6182 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz " 6183 "band"); 6184 if (wpa_s->best_5_freq > 0 && 6185 p2p_supported_freq_go(wpa_s->global->p2p, 6186 wpa_s->best_5_freq)) { 6187 freq = wpa_s->best_5_freq; 6188 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band " 6189 "channel: %d MHz", freq); 6190 } else { 6191 const int freqs[] = { 6192 /* operating class 115 */ 6193 5180, 5200, 5220, 5240, 6194 /* operating class 124 */ 6195 5745, 5765, 5785, 5805, 6196 }; 6197 unsigned int i, num_freqs = ARRAY_SIZE(freqs); 6198 6199 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 6200 return -1; 6201 6202 /* 6203 * Most of the 5 GHz channels require DFS. Only 6204 * operating classes 115 and 124 are available possibly 6205 * without that requirement. Check these for 6206 * availability starting from a randomly picked 6207 * position. 6208 */ 6209 for (i = 0; i < num_freqs; i++, r++) { 6210 freq = freqs[r % num_freqs]; 6211 if (p2p_supported_freq_go(wpa_s->global->p2p, 6212 freq)) 6213 break; 6214 } 6215 6216 if (i >= num_freqs) { 6217 wpa_printf(MSG_DEBUG, "P2P: Could not select " 6218 "5 GHz channel for P2P group"); 6219 return -1; 6220 } 6221 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band " 6222 "channel: %d MHz", freq); 6223 } 6224 } 6225 6226 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) { 6227 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && 6228 ieee80211_is_dfs(freq, wpa_s->hw.modes, 6229 wpa_s->hw.num_modes)) { 6230 /* 6231 * If freq is a DFS channel and DFS is offloaded to the 6232 * driver, allow P2P GO to use it. 6233 */ 6234 wpa_printf(MSG_DEBUG, "P2P: " 6235 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded", 6236 __func__, freq); 6237 return freq; 6238 } 6239 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO " 6240 "(%u MHz) is not supported for P2P uses", 6241 freq); 6242 return -1; 6243 } 6244 6245 return freq; 6246 } 6247 6248 6249 static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s, 6250 const struct p2p_channels *channels, 6251 int freq) 6252 { 6253 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) && 6254 p2p_supported_freq_go(wpa_s->global->p2p, freq) && 6255 freq_included(wpa_s, channels, freq)) 6256 return 1; 6257 return 0; 6258 } 6259 6260 6261 static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s, 6262 struct p2p_go_neg_results *params, 6263 const struct p2p_channels *channels) 6264 { 6265 unsigned int i, r; 6266 6267 /* try all channels in operating class 115 */ 6268 for (i = 0; i < 4; i++) { 6269 params->freq = 5180 + i * 20; 6270 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && 6271 freq_included(wpa_s, channels, params->freq) && 6272 p2p_supported_freq(wpa_s->global->p2p, params->freq)) 6273 goto out; 6274 } 6275 6276 /* try all channels in operating class 124 */ 6277 for (i = 0; i < 4; i++) { 6278 params->freq = 5745 + i * 20; 6279 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && 6280 freq_included(wpa_s, channels, params->freq) && 6281 p2p_supported_freq(wpa_s->global->p2p, params->freq)) 6282 goto out; 6283 } 6284 6285 /* try social channel class 180 channel 2 */ 6286 params->freq = 58320 + 1 * 2160; 6287 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && 6288 freq_included(wpa_s, channels, params->freq) && 6289 p2p_supported_freq(wpa_s->global->p2p, params->freq)) 6290 goto out; 6291 6292 /* try all channels in reg. class 180 */ 6293 for (i = 0; i < 4; i++) { 6294 params->freq = 58320 + i * 2160; 6295 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) && 6296 freq_included(wpa_s, channels, params->freq) && 6297 p2p_supported_freq(wpa_s->global->p2p, params->freq)) 6298 goto out; 6299 } 6300 6301 /* try some random selection of the social channels */ 6302 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 6303 return; 6304 6305 for (i = 0; i < 3; i++) { 6306 params->freq = 2412 + ((r + i) % 3) * 25; 6307 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq)) 6308 goto out; 6309 } 6310 6311 /* try all other channels in operating class 81 */ 6312 for (i = 0; i < 11; i++) { 6313 params->freq = 2412 + i * 5; 6314 6315 /* skip social channels; covered in the previous loop */ 6316 if (params->freq == 2412 || 6317 params->freq == 2437 || 6318 params->freq == 2462) 6319 continue; 6320 6321 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq)) 6322 goto out; 6323 } 6324 6325 params->freq = 0; 6326 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed"); 6327 return; 6328 out: 6329 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)", 6330 params->freq); 6331 } 6332 6333 6334 static int wpas_same_band(int freq1, int freq2) 6335 { 6336 enum hostapd_hw_mode mode1, mode2; 6337 u8 chan1, chan2; 6338 6339 mode1 = ieee80211_freq_to_chan(freq1, &chan1); 6340 mode2 = ieee80211_freq_to_chan(freq2, &chan2); 6341 if (mode1 == NUM_HOSTAPD_MODES) 6342 return 0; 6343 return mode1 == mode2; 6344 } 6345 6346 6347 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s, 6348 struct p2p_go_neg_results *params, 6349 int freq, int vht_center_freq2, int ht40, 6350 int vht, int max_oper_chwidth, int he, 6351 int edmg, 6352 const struct p2p_channels *channels) 6353 { 6354 struct wpa_used_freq_data *freqs; 6355 unsigned int cand; 6356 unsigned int num, i; 6357 int ignore_no_freqs = 0; 6358 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0; 6359 6360 os_memset(params, 0, sizeof(*params)); 6361 params->role_go = 1; 6362 params->ht40 = ht40; 6363 params->vht = vht; 6364 params->he = he; 6365 params->max_oper_chwidth = max_oper_chwidth; 6366 params->vht_center_freq2 = vht_center_freq2; 6367 params->edmg = edmg; 6368 6369 freqs = os_calloc(wpa_s->num_multichan_concurrent, 6370 sizeof(struct wpa_used_freq_data)); 6371 if (!freqs) 6372 return -1; 6373 6374 num = get_shared_radio_freqs_data(wpa_s, freqs, 6375 wpa_s->num_multichan_concurrent); 6376 6377 if (wpa_s->current_ssid && 6378 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO && 6379 wpa_s->wpa_state == WPA_COMPLETED) { 6380 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO", 6381 __func__); 6382 6383 /* 6384 * If the frequency selection is done for an active P2P GO that 6385 * is not sharing a frequency, allow to select a new frequency 6386 * even if there are no unused frequencies as we are about to 6387 * move the P2P GO so its frequency can be re-used. 6388 */ 6389 for (i = 0; i < num; i++) { 6390 if (freqs[i].freq == wpa_s->current_ssid->frequency && 6391 freqs[i].flags == 0) { 6392 ignore_no_freqs = 1; 6393 break; 6394 } 6395 } 6396 } 6397 6398 /* Try to use EDMG channel */ 6399 if (params->edmg) { 6400 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0) 6401 goto success; 6402 params->edmg = 0; 6403 } 6404 6405 /* try using the forced freq */ 6406 if (freq) { 6407 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) || 6408 !freq_included(wpa_s, channels, freq)) { 6409 wpa_printf(MSG_DEBUG, 6410 "P2P: Forced GO freq %d MHz disallowed", 6411 freq); 6412 goto fail; 6413 } 6414 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) { 6415 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && 6416 ieee80211_is_dfs(freq, wpa_s->hw.modes, 6417 wpa_s->hw.num_modes)) { 6418 /* 6419 * If freq is a DFS channel and DFS is offloaded 6420 * to the driver, allow P2P GO to use it. 6421 */ 6422 wpa_printf(MSG_DEBUG, 6423 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded", 6424 __func__, freq); 6425 } else { 6426 wpa_printf(MSG_DEBUG, 6427 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses", 6428 freq); 6429 goto fail; 6430 } 6431 } 6432 6433 for (i = 0; i < num; i++) { 6434 if (freqs[i].freq == freq) { 6435 wpa_printf(MSG_DEBUG, 6436 "P2P: forced freq (%d MHz) is also shared", 6437 freq); 6438 params->freq = freq; 6439 goto success; 6440 } 6441 } 6442 6443 if (!ignore_no_freqs && !unused_channels) { 6444 wpa_printf(MSG_DEBUG, 6445 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use", 6446 freq); 6447 goto fail; 6448 } 6449 6450 wpa_printf(MSG_DEBUG, 6451 "P2P: force GO freq (%d MHz) on a free channel", 6452 freq); 6453 params->freq = freq; 6454 goto success; 6455 } 6456 6457 /* consider using one of the shared frequencies */ 6458 if (num && 6459 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) { 6460 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num); 6461 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { 6462 wpa_printf(MSG_DEBUG, 6463 "P2P: Use shared freq (%d MHz) for GO", 6464 cand); 6465 params->freq = cand; 6466 goto success; 6467 } 6468 6469 /* try using one of the shared freqs */ 6470 for (i = 0; i < num; i++) { 6471 if (wpas_p2p_supported_freq_go(wpa_s, channels, 6472 freqs[i].freq)) { 6473 wpa_printf(MSG_DEBUG, 6474 "P2P: Use shared freq (%d MHz) for GO", 6475 freqs[i].freq); 6476 params->freq = freqs[i].freq; 6477 goto success; 6478 } 6479 } 6480 } 6481 6482 if (!ignore_no_freqs && !unused_channels) { 6483 wpa_printf(MSG_DEBUG, 6484 "P2P: Cannot force GO on any of the channels we are already using"); 6485 goto fail; 6486 } 6487 6488 /* try using the setting from the configuration file */ 6489 if (wpa_s->conf->p2p_oper_reg_class == 81 && 6490 wpa_s->conf->p2p_oper_channel >= 1 && 6491 wpa_s->conf->p2p_oper_channel <= 11 && 6492 wpas_p2p_supported_freq_go( 6493 wpa_s, channels, 6494 2407 + 5 * wpa_s->conf->p2p_oper_channel)) { 6495 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel; 6496 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " 6497 "frequency %d MHz", params->freq); 6498 goto success; 6499 } 6500 6501 if ((wpa_s->conf->p2p_oper_reg_class == 115 || 6502 wpa_s->conf->p2p_oper_reg_class == 116 || 6503 wpa_s->conf->p2p_oper_reg_class == 117 || 6504 wpa_s->conf->p2p_oper_reg_class == 124 || 6505 wpa_s->conf->p2p_oper_reg_class == 125 || 6506 wpa_s->conf->p2p_oper_reg_class == 126 || 6507 wpa_s->conf->p2p_oper_reg_class == 127) && 6508 wpas_p2p_supported_freq_go(wpa_s, channels, 6509 5000 + 6510 5 * wpa_s->conf->p2p_oper_channel)) { 6511 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel; 6512 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured " 6513 "frequency %d MHz", params->freq); 6514 goto success; 6515 } 6516 6517 /* Try using best channels */ 6518 if (wpa_s->conf->p2p_oper_channel == 0 && 6519 wpa_s->best_overall_freq > 0 && 6520 wpas_p2p_supported_freq_go(wpa_s, channels, 6521 wpa_s->best_overall_freq)) { 6522 params->freq = wpa_s->best_overall_freq; 6523 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall " 6524 "channel %d MHz", params->freq); 6525 goto success; 6526 } 6527 6528 if (wpa_s->conf->p2p_oper_channel == 0 && 6529 wpa_s->best_24_freq > 0 && 6530 wpas_p2p_supported_freq_go(wpa_s, channels, 6531 wpa_s->best_24_freq)) { 6532 params->freq = wpa_s->best_24_freq; 6533 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz " 6534 "channel %d MHz", params->freq); 6535 goto success; 6536 } 6537 6538 if (wpa_s->conf->p2p_oper_channel == 0 && 6539 wpa_s->best_5_freq > 0 && 6540 wpas_p2p_supported_freq_go(wpa_s, channels, 6541 wpa_s->best_5_freq)) { 6542 params->freq = wpa_s->best_5_freq; 6543 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz " 6544 "channel %d MHz", params->freq); 6545 goto success; 6546 } 6547 6548 /* try using preferred channels */ 6549 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels); 6550 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { 6551 params->freq = cand; 6552 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred " 6553 "channels", params->freq); 6554 goto success; 6555 } 6556 6557 /* Try using a channel that allows VHT to be used with 80 MHz */ 6558 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) { 6559 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { 6560 enum hostapd_hw_mode mode; 6561 struct hostapd_hw_modes *hwmode; 6562 u8 chan; 6563 u8 op_class; 6564 6565 cand = wpa_s->p2p_group_common_freqs[i]; 6566 op_class = is_6ghz_freq(cand) ? 133 : 128; 6567 mode = ieee80211_freq_to_chan(cand, &chan); 6568 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, 6569 mode, is_6ghz_freq(cand)); 6570 if (!hwmode || 6571 wpas_p2p_verify_channel(wpa_s, hwmode, op_class, 6572 chan, BW80) != ALLOWED) 6573 continue; 6574 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { 6575 params->freq = cand; 6576 wpa_printf(MSG_DEBUG, 6577 "P2P: Use freq %d MHz common with the peer and allowing VHT80", 6578 params->freq); 6579 goto success; 6580 } 6581 } 6582 } 6583 6584 /* Try using a channel that allows HT to be used with 40 MHz on the same 6585 * band so that CSA can be used */ 6586 if (wpa_s->current_ssid && wpa_s->hw.modes && 6587 wpa_s->p2p_group_common_freqs) { 6588 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { 6589 enum hostapd_hw_mode mode; 6590 struct hostapd_hw_modes *hwmode; 6591 u8 chan, op_class; 6592 bool is_6ghz, supported = false; 6593 6594 is_6ghz = is_6ghz_freq(cand); 6595 cand = wpa_s->p2p_group_common_freqs[i]; 6596 mode = ieee80211_freq_to_chan(cand, &chan); 6597 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, 6598 mode, is_6ghz); 6599 if (!wpas_same_band(wpa_s->current_ssid->frequency, 6600 cand) || 6601 !hwmode) 6602 continue; 6603 if (is_6ghz && 6604 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan, 6605 BW40) == ALLOWED) 6606 supported = true; 6607 6608 if (!is_6ghz && 6609 ieee80211_freq_to_channel_ext( 6610 cand, -1, CHANWIDTH_USE_HT, &op_class, 6611 &chan) != NUM_HOSTAPD_MODES && 6612 wpas_p2p_verify_channel( 6613 wpa_s, hwmode, op_class, chan, 6614 BW40MINUS) == ALLOWED) 6615 supported = true; 6616 6617 if (!supported && !is_6ghz && 6618 ieee80211_freq_to_channel_ext( 6619 cand, 1, CHANWIDTH_USE_HT, &op_class, 6620 &chan) != NUM_HOSTAPD_MODES && 6621 wpas_p2p_verify_channel( 6622 wpa_s, hwmode, op_class, chan, 6623 BW40PLUS) == ALLOWED) 6624 supported = true; 6625 6626 if (!supported) 6627 continue; 6628 6629 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { 6630 params->freq = cand; 6631 wpa_printf(MSG_DEBUG, 6632 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band", 6633 params->freq); 6634 goto success; 6635 } 6636 } 6637 } 6638 6639 /* Try using one of the group common freqs on the same band so that CSA 6640 * can be used */ 6641 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) { 6642 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { 6643 cand = wpa_s->p2p_group_common_freqs[i]; 6644 if (!wpas_same_band(wpa_s->current_ssid->frequency, 6645 cand)) 6646 continue; 6647 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { 6648 params->freq = cand; 6649 wpa_printf(MSG_DEBUG, 6650 "P2P: Use freq %d MHz common with the peer and maintaining same band", 6651 params->freq); 6652 goto success; 6653 } 6654 } 6655 } 6656 6657 /* Try using one of the group common freqs */ 6658 if (wpa_s->p2p_group_common_freqs) { 6659 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) { 6660 cand = wpa_s->p2p_group_common_freqs[i]; 6661 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) { 6662 params->freq = cand; 6663 wpa_printf(MSG_DEBUG, 6664 "P2P: Use freq %d MHz common with the peer", 6665 params->freq); 6666 goto success; 6667 } 6668 } 6669 } 6670 6671 /* no preference, select some channel */ 6672 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels); 6673 6674 if (params->freq == 0) { 6675 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use"); 6676 goto fail; 6677 } 6678 6679 success: 6680 os_free(freqs); 6681 return 0; 6682 fail: 6683 os_free(freqs); 6684 return -1; 6685 } 6686 6687 6688 static struct wpa_supplicant * 6689 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated, 6690 int go) 6691 { 6692 struct wpa_supplicant *group_wpa_s; 6693 6694 if (!wpas_p2p_create_iface(wpa_s)) { 6695 if (wpa_s->p2p_mgmt) { 6696 /* 6697 * We may be called on the p2p_dev interface which 6698 * cannot be used for group operations, so always use 6699 * the primary interface. 6700 */ 6701 wpa_s->parent->p2pdev = wpa_s; 6702 wpa_s = wpa_s->parent; 6703 } 6704 wpa_dbg(wpa_s, MSG_DEBUG, 6705 "P2P: Use primary interface for group operations"); 6706 wpa_s->p2p_first_connection_timeout = 0; 6707 if (wpa_s != wpa_s->p2pdev) 6708 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev); 6709 return wpa_s; 6710 } 6711 6712 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO : 6713 WPA_IF_P2P_CLIENT) < 0) { 6714 wpa_msg_global(wpa_s, MSG_ERROR, 6715 "P2P: Failed to add group interface"); 6716 return NULL; 6717 } 6718 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go); 6719 if (group_wpa_s == NULL) { 6720 wpa_msg_global(wpa_s, MSG_ERROR, 6721 "P2P: Failed to initialize group interface"); 6722 wpas_p2p_remove_pending_group_interface(wpa_s); 6723 return NULL; 6724 } 6725 6726 if (go && wpa_s->p2p_go_do_acs) { 6727 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs; 6728 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band; 6729 wpa_s->p2p_go_do_acs = 0; 6730 } 6731 6732 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s", 6733 group_wpa_s->ifname); 6734 group_wpa_s->p2p_first_connection_timeout = 0; 6735 return group_wpa_s; 6736 } 6737 6738 6739 /** 6740 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner 6741 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface() 6742 * @persistent_group: Whether to create a persistent group 6743 * @freq: Frequency for the group or 0 to indicate no hardcoding 6744 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80 6745 * @ht40: Start GO with 40 MHz channel width 6746 * @vht: Start GO with VHT support 6747 * @vht_chwidth: channel bandwidth for GO operating with VHT support 6748 * @edmg: Start GO with EDMG support 6749 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel 6750 * Returns: 0 on success, -1 on failure 6751 * 6752 * This function creates a new P2P group with the local end as the Group Owner, 6753 * i.e., without using Group Owner Negotiation. 6754 */ 6755 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group, 6756 int freq, int vht_center_freq2, int ht40, int vht, 6757 int max_oper_chwidth, int he, int edmg, 6758 bool allow_6ghz) 6759 { 6760 struct p2p_go_neg_results params; 6761 6762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 6763 return -1; 6764 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq)) 6765 return -1; 6766 6767 os_free(wpa_s->global->add_psk); 6768 wpa_s->global->add_psk = NULL; 6769 6770 /* Make sure we are not running find during connection establishment */ 6771 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND"); 6772 wpas_p2p_stop_find_oper(wpa_s); 6773 6774 if (!wpa_s->p2p_go_do_acs) { 6775 freq = wpas_p2p_select_go_freq(wpa_s, freq); 6776 if (freq < 0) 6777 return -1; 6778 } 6779 6780 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, vht_center_freq2, 6781 ht40, vht, max_oper_chwidth, he, edmg, 6782 NULL)) 6783 return -1; 6784 6785 p2p_go_params(wpa_s->global->p2p, ¶ms); 6786 params.persistent_group = persistent_group; 6787 6788 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1); 6789 if (wpa_s == NULL) 6790 return -1; 6791 wpas_start_wps_go(wpa_s, ¶ms, 0); 6792 6793 return 0; 6794 } 6795 6796 6797 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s, 6798 struct wpa_ssid *params, int addr_allocated, 6799 int freq, int force_scan) 6800 { 6801 struct wpa_ssid *ssid; 6802 6803 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0); 6804 if (wpa_s == NULL) 6805 return -1; 6806 if (force_scan) 6807 os_get_reltime(&wpa_s->scan_min_time); 6808 wpa_s->p2p_last_4way_hs_fail = NULL; 6809 6810 wpa_supplicant_ap_deinit(wpa_s); 6811 6812 ssid = wpa_config_add_network(wpa_s->conf); 6813 if (ssid == NULL) 6814 return -1; 6815 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN); 6816 wpa_config_set_network_defaults(ssid); 6817 ssid->temporary = 1; 6818 ssid->proto = WPA_PROTO_RSN; 6819 ssid->pbss = params->pbss; 6820 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP : 6821 WPA_CIPHER_CCMP; 6822 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP; 6823 ssid->key_mgmt = WPA_KEY_MGMT_PSK; 6824 ssid->ssid = os_malloc(params->ssid_len); 6825 if (ssid->ssid == NULL) { 6826 wpa_config_remove_network(wpa_s->conf, ssid->id); 6827 return -1; 6828 } 6829 os_memcpy(ssid->ssid, params->ssid, params->ssid_len); 6830 ssid->ssid_len = params->ssid_len; 6831 ssid->p2p_group = 1; 6832 ssid->export_keys = 1; 6833 if (params->psk_set) { 6834 os_memcpy(ssid->psk, params->psk, 32); 6835 ssid->psk_set = 1; 6836 } 6837 if (params->passphrase) 6838 ssid->passphrase = os_strdup(params->passphrase); 6839 6840 wpa_s->show_group_started = 1; 6841 wpa_s->p2p_in_invitation = 1; 6842 wpa_s->p2p_invite_go_freq = freq; 6843 wpa_s->p2p_go_group_formation_completed = 0; 6844 wpa_s->global->p2p_group_formation = wpa_s; 6845 6846 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev, 6847 NULL); 6848 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, 6849 wpas_p2p_group_formation_timeout, 6850 wpa_s->p2pdev, NULL); 6851 wpa_supplicant_select_network(wpa_s, ssid); 6852 6853 return 0; 6854 } 6855 6856 6857 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s, 6858 struct wpa_ssid *ssid, int addr_allocated, 6859 int force_freq, int neg_freq, 6860 int vht_center_freq2, int ht40, 6861 int vht, int max_oper_chwidth, int he, 6862 int edmg, 6863 const struct p2p_channels *channels, 6864 int connection_timeout, int force_scan, 6865 bool allow_6ghz) 6866 { 6867 struct p2p_go_neg_results params; 6868 int go = 0, freq; 6869 6870 if (ssid->disabled != 2 || ssid->ssid == NULL) 6871 return -1; 6872 6873 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) && 6874 go == (ssid->mode == WPAS_MODE_P2P_GO)) { 6875 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is " 6876 "already running"); 6877 if (go == 0 && 6878 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 6879 wpa_s->p2pdev, NULL)) { 6880 /* 6881 * This can happen if Invitation Response frame was lost 6882 * and the peer (GO of a persistent group) tries to 6883 * invite us again. Reschedule the timeout to avoid 6884 * terminating the wait for the connection too early 6885 * since we now know that the peer is still trying to 6886 * invite us instead of having already started the GO. 6887 */ 6888 wpa_printf(MSG_DEBUG, 6889 "P2P: Reschedule group formation timeout since peer is still trying to invite us"); 6890 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, 6891 wpas_p2p_group_formation_timeout, 6892 wpa_s->p2pdev, NULL); 6893 } 6894 return 0; 6895 } 6896 6897 os_free(wpa_s->global->add_psk); 6898 wpa_s->global->add_psk = NULL; 6899 6900 /* Make sure we are not running find during connection establishment */ 6901 wpas_p2p_stop_find_oper(wpa_s); 6902 6903 wpa_s->p2p_fallback_to_go_neg = 0; 6904 6905 if (ssid->mode == WPAS_MODE_P2P_GO) { 6906 if (force_freq > 0) { 6907 freq = wpas_p2p_select_go_freq(wpa_s, force_freq); 6908 if (freq < 0) 6909 return -1; 6910 } else { 6911 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq); 6912 if (freq < 0 || 6913 (freq > 0 && !freq_included(wpa_s, channels, freq))) 6914 freq = 0; 6915 } 6916 } else if (ssid->mode == WPAS_MODE_INFRA) { 6917 freq = neg_freq; 6918 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) { 6919 struct os_reltime now; 6920 struct wpa_bss *bss = 6921 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid); 6922 6923 os_get_reltime(&now); 6924 if (bss && 6925 !os_reltime_expired(&now, &bss->last_update, 5) && 6926 freq_included(wpa_s, channels, bss->freq)) 6927 freq = bss->freq; 6928 else 6929 freq = 0; 6930 } 6931 6932 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq, 6933 force_scan); 6934 } else { 6935 return -1; 6936 } 6937 6938 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, vht_center_freq2, 6939 ht40, vht, max_oper_chwidth, he, edmg, 6940 channels)) 6941 return -1; 6942 6943 params.role_go = 1; 6944 params.psk_set = ssid->psk_set; 6945 if (params.psk_set) 6946 os_memcpy(params.psk, ssid->psk, sizeof(params.psk)); 6947 if (ssid->passphrase) { 6948 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) { 6949 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in " 6950 "persistent group"); 6951 return -1; 6952 } 6953 os_strlcpy(params.passphrase, ssid->passphrase, 6954 sizeof(params.passphrase)); 6955 } 6956 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len); 6957 params.ssid_len = ssid->ssid_len; 6958 params.persistent_group = 1; 6959 6960 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1); 6961 if (wpa_s == NULL) 6962 return -1; 6963 6964 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS); 6965 6966 wpa_s->p2p_first_connection_timeout = connection_timeout; 6967 wpas_start_wps_go(wpa_s, ¶ms, 0); 6968 6969 return 0; 6970 } 6971 6972 6973 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies, 6974 struct wpabuf *proberesp_ies) 6975 { 6976 struct wpa_supplicant *wpa_s = ctx; 6977 if (wpa_s->ap_iface) { 6978 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0]; 6979 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) { 6980 wpabuf_free(beacon_ies); 6981 wpabuf_free(proberesp_ies); 6982 return; 6983 } 6984 if (beacon_ies) { 6985 wpabuf_free(hapd->p2p_beacon_ie); 6986 hapd->p2p_beacon_ie = beacon_ies; 6987 } 6988 wpabuf_free(hapd->p2p_probe_resp_ie); 6989 hapd->p2p_probe_resp_ie = proberesp_ies; 6990 } else { 6991 wpabuf_free(beacon_ies); 6992 wpabuf_free(proberesp_ies); 6993 } 6994 wpa_supplicant_ap_update_beacon(wpa_s); 6995 } 6996 6997 6998 static void wpas_p2p_idle_update(void *ctx, int idle) 6999 { 7000 struct wpa_supplicant *wpa_s = ctx; 7001 if (!wpa_s->ap_iface) 7002 return; 7003 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not "); 7004 if (idle) { 7005 if (wpa_s->global->p2p_fail_on_wps_complete && 7006 wpa_s->p2p_in_provisioning) { 7007 wpas_p2p_grpform_fail_after_wps(wpa_s); 7008 return; 7009 } 7010 wpas_p2p_set_group_idle_timeout(wpa_s); 7011 } else 7012 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL); 7013 } 7014 7015 7016 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s, 7017 struct wpa_ssid *ssid) 7018 { 7019 struct p2p_group *group; 7020 struct p2p_group_config *cfg; 7021 7022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || 7023 !ssid->p2p_group) 7024 return NULL; 7025 7026 cfg = os_zalloc(sizeof(*cfg)); 7027 if (cfg == NULL) 7028 return NULL; 7029 7030 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect) 7031 cfg->persistent_group = 2; 7032 else if (ssid->p2p_persistent_group) 7033 cfg->persistent_group = 1; 7034 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN); 7035 if (wpa_s->max_stations && 7036 wpa_s->max_stations < wpa_s->conf->max_num_sta) 7037 cfg->max_clients = wpa_s->max_stations; 7038 else 7039 cfg->max_clients = wpa_s->conf->max_num_sta; 7040 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len); 7041 cfg->ssid_len = ssid->ssid_len; 7042 cfg->freq = ssid->frequency; 7043 cfg->cb_ctx = wpa_s; 7044 cfg->ie_update = wpas_p2p_ie_update; 7045 cfg->idle_update = wpas_p2p_idle_update; 7046 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start) 7047 != 0; 7048 7049 group = p2p_group_init(wpa_s->global->p2p, cfg); 7050 if (group == NULL) 7051 os_free(cfg); 7052 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) 7053 p2p_group_notif_formation_done(group); 7054 wpa_s->p2p_group = group; 7055 return group; 7056 } 7057 7058 7059 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 7060 int registrar) 7061 { 7062 struct wpa_ssid *ssid = wpa_s->current_ssid; 7063 7064 if (!wpa_s->p2p_in_provisioning) { 7065 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P " 7066 "provisioning not in progress"); 7067 return; 7068 } 7069 7070 if (ssid && ssid->mode == WPAS_MODE_INFRA) { 7071 u8 go_dev_addr[ETH_ALEN]; 7072 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN); 7073 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, 7074 ssid->ssid_len); 7075 /* Clear any stored provisioning info */ 7076 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr); 7077 } 7078 7079 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev, 7080 NULL); 7081 wpa_s->p2p_go_group_formation_completed = 1; 7082 if (ssid && ssid->mode == WPAS_MODE_INFRA) { 7083 /* 7084 * Use a separate timeout for initial data connection to 7085 * complete to allow the group to be removed automatically if 7086 * something goes wrong in this step before the P2P group idle 7087 * timeout mechanism is taken into use. 7088 */ 7089 wpa_dbg(wpa_s, MSG_DEBUG, 7090 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection", 7091 P2P_MAX_INITIAL_CONN_WAIT); 7092 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0, 7093 wpas_p2p_group_formation_timeout, 7094 wpa_s->p2pdev, NULL); 7095 /* Complete group formation on successful data connection. */ 7096 wpa_s->p2p_go_group_formation_completed = 0; 7097 } else if (ssid) { 7098 /* 7099 * Use a separate timeout for initial data connection to 7100 * complete to allow the group to be removed automatically if 7101 * the client does not complete data connection successfully. 7102 */ 7103 wpa_dbg(wpa_s, MSG_DEBUG, 7104 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection", 7105 P2P_MAX_INITIAL_CONN_WAIT_GO); 7106 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0, 7107 wpas_p2p_group_formation_timeout, 7108 wpa_s->p2pdev, NULL); 7109 /* 7110 * Complete group formation on first successful data connection 7111 */ 7112 wpa_s->p2p_go_group_formation_completed = 0; 7113 } 7114 if (wpa_s->global->p2p) 7115 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr); 7116 wpas_group_formation_completed(wpa_s, 1, 0); 7117 } 7118 7119 7120 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s, 7121 struct wps_event_fail *fail) 7122 { 7123 if (!wpa_s->p2p_in_provisioning) { 7124 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P " 7125 "provisioning not in progress"); 7126 return; 7127 } 7128 7129 if (wpa_s->go_params) { 7130 p2p_clear_provisioning_info( 7131 wpa_s->global->p2p, 7132 wpa_s->go_params->peer_device_addr); 7133 } 7134 7135 wpas_notify_p2p_wps_failed(wpa_s, fail); 7136 7137 if (wpa_s == wpa_s->global->p2p_group_formation) { 7138 /* 7139 * Allow some time for the failed WPS negotiation exchange to 7140 * complete, but remove the group since group formation cannot 7141 * succeed after provisioning failure. 7142 */ 7143 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout"); 7144 wpa_s->global->p2p_fail_on_wps_complete = 1; 7145 eloop_deplete_timeout(0, 50000, 7146 wpas_p2p_group_formation_timeout, 7147 wpa_s->p2pdev, NULL); 7148 } 7149 } 7150 7151 7152 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s) 7153 { 7154 if (!wpa_s->global->p2p_fail_on_wps_complete || 7155 !wpa_s->p2p_in_provisioning) 7156 return 0; 7157 7158 wpas_p2p_grpform_fail_after_wps(wpa_s); 7159 7160 return 1; 7161 } 7162 7163 7164 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 7165 const char *config_method, 7166 enum wpas_p2p_prov_disc_use use, 7167 struct p2ps_provision *p2ps_prov) 7168 { 7169 u16 config_methods; 7170 7171 wpa_s->global->pending_p2ps_group = 0; 7172 wpa_s->global->pending_p2ps_group_freq = 0; 7173 wpa_s->p2p_fallback_to_go_neg = 0; 7174 wpa_s->pending_pd_use = NORMAL_PD; 7175 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) { 7176 p2ps_prov->conncap = p2ps_group_capability( 7177 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role, 7178 &p2ps_prov->force_freq, &p2ps_prov->pref_freq); 7179 7180 wpa_printf(MSG_DEBUG, 7181 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s", 7182 __func__, p2ps_prov->conncap, 7183 p2ps_prov->adv_id, p2ps_prov->conncap, 7184 p2ps_prov->status, p2ps_prov->info); 7185 7186 config_methods = 0; 7187 } else if (os_strncmp(config_method, "display", 7) == 0) 7188 config_methods = WPS_CONFIG_DISPLAY; 7189 else if (os_strncmp(config_method, "keypad", 6) == 0) 7190 config_methods = WPS_CONFIG_KEYPAD; 7191 else if (os_strncmp(config_method, "pbc", 3) == 0 || 7192 os_strncmp(config_method, "pushbutton", 10) == 0) 7193 config_methods = WPS_CONFIG_PUSHBUTTON; 7194 else { 7195 wpa_printf(MSG_DEBUG, "P2P: Unknown config method"); 7196 os_free(p2ps_prov); 7197 return -1; 7198 } 7199 7200 if (use == WPAS_P2P_PD_AUTO) { 7201 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN); 7202 wpa_s->pending_pd_config_methods = config_methods; 7203 wpa_s->p2p_auto_pd = 1; 7204 wpa_s->p2p_auto_join = 0; 7205 wpa_s->pending_pd_before_join = 0; 7206 wpa_s->auto_pd_scan_retry = 0; 7207 wpas_p2p_stop_find(wpa_s); 7208 wpa_s->p2p_join_scan_count = 0; 7209 os_get_reltime(&wpa_s->p2p_auto_started); 7210 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld", 7211 wpa_s->p2p_auto_started.sec, 7212 wpa_s->p2p_auto_started.usec); 7213 wpas_p2p_join_scan(wpa_s, NULL); 7214 return 0; 7215 } 7216 7217 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) { 7218 os_free(p2ps_prov); 7219 return -1; 7220 } 7221 7222 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov, 7223 config_methods, use == WPAS_P2P_PD_FOR_JOIN, 7224 0, 1); 7225 } 7226 7227 7228 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, 7229 char *end) 7230 { 7231 return p2p_scan_result_text(ies, ies_len, buf, end); 7232 } 7233 7234 7235 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s) 7236 { 7237 if (!offchannel_pending_action_tx(wpa_s)) 7238 return; 7239 7240 if (wpa_s->p2p_send_action_work) { 7241 wpas_p2p_free_send_action_work(wpa_s); 7242 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, 7243 wpa_s, NULL); 7244 offchannel_send_action_done(wpa_s); 7245 } 7246 7247 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new " 7248 "operation request"); 7249 offchannel_clear_pending_action_tx(wpa_s); 7250 } 7251 7252 7253 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout, 7254 enum p2p_discovery_type type, 7255 unsigned int num_req_dev_types, const u8 *req_dev_types, 7256 const u8 *dev_id, unsigned int search_delay, 7257 u8 seek_cnt, const char **seek_string, int freq, 7258 bool include_6ghz) 7259 { 7260 wpas_p2p_clear_pending_action_tx(wpa_s); 7261 wpa_s->global->p2p_long_listen = 0; 7262 7263 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL || 7264 wpa_s->p2p_in_provisioning) { 7265 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s", 7266 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ? 7267 " (P2P disabled)" : "", 7268 wpa_s->p2p_in_provisioning ? 7269 " (p2p_in_provisioning)" : ""); 7270 return -1; 7271 } 7272 7273 wpa_supplicant_cancel_sched_scan(wpa_s); 7274 7275 return p2p_find(wpa_s->global->p2p, timeout, type, 7276 num_req_dev_types, req_dev_types, dev_id, 7277 search_delay, seek_cnt, seek_string, freq, 7278 include_6ghz); 7279 } 7280 7281 7282 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s, 7283 struct wpa_scan_results *scan_res) 7284 { 7285 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); 7286 7287 if (wpa_s->p2p_scan_work) { 7288 struct wpa_radio_work *work = wpa_s->p2p_scan_work; 7289 wpa_s->p2p_scan_work = NULL; 7290 radio_work_done(work); 7291 } 7292 7293 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7294 return; 7295 7296 /* 7297 * Indicate that results have been processed so that the P2P module can 7298 * continue pending tasks. 7299 */ 7300 wpas_p2p_scan_res_handled(wpa_s); 7301 } 7302 7303 7304 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s) 7305 { 7306 wpas_p2p_clear_pending_action_tx(wpa_s); 7307 wpa_s->global->p2p_long_listen = 0; 7308 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 7309 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL); 7310 7311 if (wpa_s->global->p2p) 7312 p2p_stop_find(wpa_s->global->p2p); 7313 7314 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) { 7315 wpa_printf(MSG_DEBUG, 7316 "P2P: Do not consider the scan results after stop_find"); 7317 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search; 7318 } 7319 } 7320 7321 7322 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s) 7323 { 7324 wpas_p2p_stop_find_oper(wpa_s); 7325 if (!wpa_s->global->pending_group_iface_for_p2ps) 7326 wpas_p2p_remove_pending_group_interface(wpa_s); 7327 } 7328 7329 7330 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx) 7331 { 7332 struct wpa_supplicant *wpa_s = eloop_ctx; 7333 wpa_s->global->p2p_long_listen = 0; 7334 } 7335 7336 7337 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout) 7338 { 7339 int res; 7340 7341 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7342 return -1; 7343 7344 if (wpa_s->p2p_lo_started) { 7345 wpa_printf(MSG_DEBUG, 7346 "P2P: Cannot start P2P listen, it is offloaded"); 7347 return -1; 7348 } 7349 7350 wpa_supplicant_cancel_sched_scan(wpa_s); 7351 wpas_p2p_clear_pending_action_tx(wpa_s); 7352 7353 if (timeout == 0) { 7354 /* 7355 * This is a request for unlimited Listen state. However, at 7356 * least for now, this is mapped to a Listen state for one 7357 * hour. 7358 */ 7359 timeout = 3600; 7360 } 7361 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL); 7362 wpa_s->global->p2p_long_listen = 0; 7363 7364 /* 7365 * Stop previous find/listen operation to avoid trying to request a new 7366 * remain-on-channel operation while the driver is still running the 7367 * previous one. 7368 */ 7369 if (wpa_s->global->p2p) 7370 p2p_stop_find(wpa_s->global->p2p); 7371 7372 res = wpas_p2p_listen_start(wpa_s, timeout * 1000); 7373 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) { 7374 wpa_s->global->p2p_long_listen = timeout * 1000; 7375 eloop_register_timeout(timeout, 0, 7376 wpas_p2p_long_listen_timeout, 7377 wpa_s, NULL); 7378 } 7379 7380 return res; 7381 } 7382 7383 7384 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, 7385 u8 *buf, size_t len, int p2p_group) 7386 { 7387 struct wpabuf *p2p_ie; 7388 int ret; 7389 7390 if (wpa_s->global->p2p_disabled) 7391 return -1; 7392 /* 7393 * Advertize mandatory cross connection capability even on 7394 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP. 7395 */ 7396 if (wpa_s->conf->p2p_disabled && p2p_group) 7397 return -1; 7398 if (wpa_s->global->p2p == NULL) 7399 return -1; 7400 if (bss == NULL) 7401 return -1; 7402 7403 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE); 7404 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len, 7405 p2p_group, p2p_ie); 7406 wpabuf_free(p2p_ie); 7407 7408 return ret; 7409 } 7410 7411 7412 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr, 7413 const u8 *dst, const u8 *bssid, 7414 const u8 *ie, size_t ie_len, 7415 unsigned int rx_freq, int ssi_signal) 7416 { 7417 if (wpa_s->global->p2p_disabled) 7418 return 0; 7419 if (wpa_s->global->p2p == NULL) 7420 return 0; 7421 7422 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid, 7423 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) { 7424 case P2P_PREQ_NOT_P2P: 7425 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len, 7426 ssi_signal); 7427 /* fall through */ 7428 case P2P_PREQ_MALFORMED: 7429 case P2P_PREQ_NOT_LISTEN: 7430 case P2P_PREQ_NOT_PROCESSED: 7431 default: /* make gcc happy */ 7432 return 0; 7433 case P2P_PREQ_PROCESSED: 7434 return 1; 7435 } 7436 } 7437 7438 7439 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da, 7440 const u8 *sa, const u8 *bssid, 7441 u8 category, const u8 *data, size_t len, int freq) 7442 { 7443 if (wpa_s->global->p2p_disabled) 7444 return; 7445 if (wpa_s->global->p2p == NULL) 7446 return; 7447 7448 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len, 7449 freq); 7450 } 7451 7452 7453 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies) 7454 { 7455 unsigned int bands; 7456 7457 if (wpa_s->global->p2p_disabled) 7458 return; 7459 if (wpa_s->global->p2p == NULL) 7460 return; 7461 7462 bands = wpas_get_bands(wpa_s, NULL); 7463 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands); 7464 } 7465 7466 7467 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s) 7468 { 7469 p2p_group_deinit(wpa_s->p2p_group); 7470 wpa_s->p2p_group = NULL; 7471 7472 wpa_s->ap_configured_cb = NULL; 7473 wpa_s->ap_configured_cb_ctx = NULL; 7474 wpa_s->ap_configured_cb_data = NULL; 7475 wpa_s->connect_without_scan = NULL; 7476 } 7477 7478 7479 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr) 7480 { 7481 wpa_s->global->p2p_long_listen = 0; 7482 7483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7484 return -1; 7485 7486 return p2p_reject(wpa_s->global->p2p, addr); 7487 } 7488 7489 7490 /* Invite to reinvoke a persistent group */ 7491 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr, 7492 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq, 7493 int vht_center_freq2, int ht40, int vht, int max_chwidth, 7494 int pref_freq, int he, int edmg, bool allow_6ghz) 7495 { 7496 enum p2p_invite_role role; 7497 u8 *bssid = NULL; 7498 int force_freq = 0; 7499 int res; 7500 int no_pref_freq_given = pref_freq == 0; 7501 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; 7502 7503 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq)) 7504 return -1; 7505 7506 wpa_s->global->p2p_invite_group = NULL; 7507 if (peer_addr) 7508 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN); 7509 else 7510 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN); 7511 7512 wpa_s->p2p_persistent_go_freq = freq; 7513 wpa_s->p2p_go_ht40 = !!ht40; 7514 wpa_s->p2p_go_vht = !!vht; 7515 wpa_s->p2p_go_he = !!he; 7516 wpa_s->p2p_go_max_oper_chwidth = max_chwidth; 7517 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2; 7518 wpa_s->p2p_go_edmg = !!edmg; 7519 if (ssid->mode == WPAS_MODE_P2P_GO) { 7520 role = P2P_INVITE_ROLE_GO; 7521 if (peer_addr == NULL) { 7522 wpa_printf(MSG_DEBUG, "P2P: Missing peer " 7523 "address in invitation command"); 7524 return -1; 7525 } 7526 if (wpas_p2p_create_iface(wpa_s)) { 7527 if (wpas_p2p_add_group_interface(wpa_s, 7528 WPA_IF_P2P_GO) < 0) { 7529 wpa_printf(MSG_ERROR, "P2P: Failed to " 7530 "allocate a new interface for the " 7531 "group"); 7532 return -1; 7533 } 7534 bssid = wpa_s->pending_interface_addr; 7535 } else if (wpa_s->p2p_mgmt) 7536 bssid = wpa_s->parent->own_addr; 7537 else 7538 bssid = wpa_s->own_addr; 7539 } else { 7540 role = P2P_INVITE_ROLE_CLIENT; 7541 peer_addr = ssid->bssid; 7542 } 7543 wpa_s->pending_invite_ssid_id = ssid->id; 7544 7545 size = P2P_MAX_PREF_CHANNELS; 7546 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, 7547 role == P2P_INVITE_ROLE_GO, 7548 pref_freq_list, &size); 7549 if (res) 7550 return res; 7551 7552 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7553 return -1; 7554 7555 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size); 7556 7557 if (wpa_s->parent->conf->p2p_ignore_shared_freq && 7558 no_pref_freq_given && pref_freq > 0 && 7559 wpa_s->num_multichan_concurrent > 1 && 7560 wpas_p2p_num_unused_channels(wpa_s) > 0) { 7561 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration", 7562 pref_freq); 7563 pref_freq = 0; 7564 } 7565 7566 /* 7567 * Stop any find/listen operations before invitation and possibly 7568 * connection establishment. 7569 */ 7570 wpas_p2p_stop_find_oper(wpa_s); 7571 7572 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, 7573 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr, 7574 1, pref_freq, -1); 7575 } 7576 7577 7578 /* Invite to join an active group */ 7579 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname, 7580 const u8 *peer_addr, const u8 *go_dev_addr, 7581 bool allow_6ghz) 7582 { 7583 struct wpa_global *global = wpa_s->global; 7584 enum p2p_invite_role role; 7585 u8 *bssid = NULL; 7586 struct wpa_ssid *ssid; 7587 int persistent; 7588 int freq = 0, force_freq = 0, pref_freq = 0; 7589 int res; 7590 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size; 7591 7592 wpa_s->p2p_persistent_go_freq = 0; 7593 wpa_s->p2p_go_ht40 = 0; 7594 wpa_s->p2p_go_vht = 0; 7595 wpa_s->p2p_go_vht_center_freq2 = 0; 7596 wpa_s->p2p_go_max_oper_chwidth = 0; 7597 wpa_s->p2p_go_edmg = 0; 7598 7599 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 7600 if (os_strcmp(wpa_s->ifname, ifname) == 0) 7601 break; 7602 } 7603 if (wpa_s == NULL) { 7604 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname); 7605 return -1; 7606 } 7607 7608 ssid = wpa_s->current_ssid; 7609 if (ssid == NULL) { 7610 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for " 7611 "invitation"); 7612 return -1; 7613 } 7614 7615 wpa_s->global->p2p_invite_group = wpa_s; 7616 persistent = ssid->p2p_persistent_group && 7617 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr, 7618 ssid->ssid, ssid->ssid_len); 7619 7620 if (ssid->mode == WPAS_MODE_P2P_GO) { 7621 role = P2P_INVITE_ROLE_ACTIVE_GO; 7622 bssid = wpa_s->own_addr; 7623 if (go_dev_addr == NULL) 7624 go_dev_addr = wpa_s->global->p2p_dev_addr; 7625 freq = ssid->frequency; 7626 } else { 7627 role = P2P_INVITE_ROLE_CLIENT; 7628 if (wpa_s->wpa_state < WPA_ASSOCIATED) { 7629 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot " 7630 "invite to current group"); 7631 return -1; 7632 } 7633 bssid = wpa_s->bssid; 7634 if (go_dev_addr == NULL && 7635 !is_zero_ether_addr(wpa_s->go_dev_addr)) 7636 go_dev_addr = wpa_s->go_dev_addr; 7637 freq = wpa_s->current_bss ? wpa_s->current_bss->freq : 7638 (int) wpa_s->assoc_freq; 7639 } 7640 wpa_s->p2pdev->pending_invite_ssid_id = -1; 7641 7642 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7643 return -1; 7644 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq)) 7645 return -1; 7646 7647 size = P2P_MAX_PREF_CHANNELS; 7648 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq, 7649 role == P2P_INVITE_ROLE_ACTIVE_GO, 7650 pref_freq_list, &size); 7651 if (res) 7652 return res; 7653 wpas_p2p_set_own_freq_preference(wpa_s, force_freq); 7654 7655 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid, 7656 ssid->ssid, ssid->ssid_len, force_freq, 7657 go_dev_addr, persistent, pref_freq, -1); 7658 } 7659 7660 7661 void wpas_p2p_completed(struct wpa_supplicant *wpa_s) 7662 { 7663 struct wpa_ssid *ssid = wpa_s->current_ssid; 7664 u8 go_dev_addr[ETH_ALEN]; 7665 int persistent; 7666 int freq; 7667 u8 ip[3 * 4], *ip_ptr = NULL; 7668 char ip_addr[100]; 7669 7670 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) { 7671 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 7672 wpa_s->p2pdev, NULL); 7673 } 7674 7675 if (!wpa_s->show_group_started || !ssid) 7676 return; 7677 7678 wpa_s->show_group_started = 0; 7679 if (!wpa_s->p2p_go_group_formation_completed && 7680 wpa_s->global->p2p_group_formation == wpa_s) { 7681 wpa_dbg(wpa_s, MSG_DEBUG, 7682 "P2P: Marking group formation completed on client on data connection"); 7683 wpa_s->p2p_go_group_formation_completed = 1; 7684 wpa_s->global->p2p_group_formation = NULL; 7685 wpa_s->p2p_in_provisioning = 0; 7686 wpa_s->p2p_in_invitation = 0; 7687 } 7688 7689 os_memset(go_dev_addr, 0, ETH_ALEN); 7690 if (ssid->bssid_set) 7691 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN); 7692 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid, 7693 ssid->ssid_len); 7694 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN); 7695 7696 if (wpa_s->global->p2p_group_formation == wpa_s) 7697 wpa_s->global->p2p_group_formation = NULL; 7698 7699 freq = wpa_s->current_bss ? wpa_s->current_bss->freq : 7700 (int) wpa_s->assoc_freq; 7701 7702 ip_addr[0] = '\0'; 7703 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) { 7704 int res; 7705 7706 res = os_snprintf(ip_addr, sizeof(ip_addr), 7707 " ip_addr=%u.%u.%u.%u " 7708 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u", 7709 ip[0], ip[1], ip[2], ip[3], 7710 ip[4], ip[5], ip[6], ip[7], 7711 ip[8], ip[9], ip[10], ip[11]); 7712 if (os_snprintf_error(sizeof(ip_addr), res)) 7713 ip_addr[0] = '\0'; 7714 ip_ptr = ip; 7715 } 7716 7717 wpas_p2p_group_started(wpa_s, 0, ssid, freq, 7718 ssid->passphrase == NULL && ssid->psk_set ? 7719 ssid->psk : NULL, 7720 ssid->passphrase, go_dev_addr, persistent, 7721 ip_addr); 7722 7723 if (persistent) 7724 wpas_p2p_store_persistent_group(wpa_s->p2pdev, 7725 ssid, go_dev_addr); 7726 7727 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr); 7728 } 7729 7730 7731 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1, 7732 u32 interval1, u32 duration2, u32 interval2) 7733 { 7734 int ret; 7735 7736 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7737 return -1; 7738 7739 if (wpa_s->wpa_state < WPA_ASSOCIATED || 7740 wpa_s->current_ssid == NULL || 7741 wpa_s->current_ssid->mode != WPAS_MODE_INFRA) 7742 return -1; 7743 7744 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid, 7745 wpa_s->own_addr, wpa_s->assoc_freq, 7746 duration1, interval1, duration2, interval2); 7747 if (ret == 0) 7748 wpa_s->waiting_presence_resp = 1; 7749 7750 return ret; 7751 } 7752 7753 7754 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period, 7755 unsigned int interval) 7756 { 7757 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7758 return -1; 7759 7760 return p2p_ext_listen(wpa_s->global->p2p, period, interval); 7761 } 7762 7763 7764 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s) 7765 { 7766 if (wpa_s->current_ssid == NULL) { 7767 /* 7768 * current_ssid can be cleared when P2P client interface gets 7769 * disconnected, so assume this interface was used as P2P 7770 * client. 7771 */ 7772 return 1; 7773 } 7774 return wpa_s->current_ssid->p2p_group && 7775 wpa_s->current_ssid->mode == WPAS_MODE_INFRA; 7776 } 7777 7778 7779 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx) 7780 { 7781 struct wpa_supplicant *wpa_s = eloop_ctx; 7782 7783 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) { 7784 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - " 7785 "disabled"); 7786 return; 7787 } 7788 7789 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate " 7790 "group"); 7791 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT); 7792 } 7793 7794 7795 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s) 7796 { 7797 int timeout; 7798 7799 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) 7800 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); 7801 7802 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) 7803 return; 7804 7805 timeout = wpa_s->conf->p2p_group_idle; 7806 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA && 7807 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE)) 7808 timeout = P2P_MAX_CLIENT_IDLE; 7809 7810 if (timeout == 0) 7811 return; 7812 7813 if (timeout < 0) { 7814 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA) 7815 timeout = 0; /* special client mode no-timeout */ 7816 else 7817 return; 7818 } 7819 7820 if (wpa_s->p2p_in_provisioning) { 7821 /* 7822 * Use the normal group formation timeout during the 7823 * provisioning phase to avoid terminating this process too 7824 * early due to group idle timeout. 7825 */ 7826 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " 7827 "during provisioning"); 7828 return; 7829 } 7830 7831 if (wpa_s->show_group_started) { 7832 /* 7833 * Use the normal group formation timeout between the end of 7834 * the provisioning phase and completion of 4-way handshake to 7835 * avoid terminating this process too early due to group idle 7836 * timeout. 7837 */ 7838 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout " 7839 "while waiting for initial 4-way handshake to " 7840 "complete"); 7841 return; 7842 } 7843 7844 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds", 7845 timeout); 7846 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout, 7847 wpa_s, NULL); 7848 } 7849 7850 7851 /* Returns 1 if the interface was removed */ 7852 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, 7853 u16 reason_code, const u8 *ie, size_t ie_len, 7854 int locally_generated) 7855 { 7856 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7857 return 0; 7858 7859 if (!locally_generated) 7860 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie, 7861 ie_len); 7862 7863 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated && 7864 wpa_s->current_ssid && 7865 wpa_s->current_ssid->p2p_group && 7866 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) { 7867 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group " 7868 "session is ending"); 7869 if (wpas_p2p_group_delete(wpa_s, 7870 P2P_GROUP_REMOVAL_GO_ENDING_SESSION) 7871 > 0) 7872 return 1; 7873 } 7874 7875 return 0; 7876 } 7877 7878 7879 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid, 7880 u16 reason_code, const u8 *ie, size_t ie_len, 7881 int locally_generated) 7882 { 7883 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 7884 return; 7885 7886 if (!locally_generated) 7887 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie, 7888 ie_len); 7889 } 7890 7891 7892 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s) 7893 { 7894 struct p2p_data *p2p = wpa_s->global->p2p; 7895 7896 if (p2p == NULL) 7897 return; 7898 7899 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)) 7900 return; 7901 7902 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME) 7903 p2p_set_dev_name(p2p, wpa_s->conf->device_name); 7904 7905 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE) 7906 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type); 7907 7908 if (wpa_s->wps && 7909 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS)) 7910 p2p_set_config_methods(p2p, wpa_s->wps->config_methods); 7911 7912 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID)) 7913 p2p_set_uuid(p2p, wpa_s->wps->uuid); 7914 7915 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) { 7916 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer); 7917 p2p_set_model_name(p2p, wpa_s->conf->model_name); 7918 p2p_set_model_number(p2p, wpa_s->conf->model_number); 7919 p2p_set_serial_number(p2p, wpa_s->conf->serial_number); 7920 } 7921 7922 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE) 7923 p2p_set_sec_dev_types(p2p, 7924 (void *) wpa_s->conf->sec_device_type, 7925 wpa_s->conf->num_sec_device_types); 7926 7927 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) { 7928 int i; 7929 p2p_remove_wps_vendor_extensions(p2p); 7930 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) { 7931 if (wpa_s->conf->wps_vendor_ext[i] == NULL) 7932 continue; 7933 p2p_add_wps_vendor_extension( 7934 p2p, wpa_s->conf->wps_vendor_ext[i]); 7935 } 7936 } 7937 7938 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) && 7939 wpa_s->conf->country[0] && wpa_s->conf->country[1]) { 7940 char country[3]; 7941 country[0] = wpa_s->conf->country[0]; 7942 country[1] = wpa_s->conf->country[1]; 7943 country[2] = 0x04; 7944 p2p_set_country(p2p, country); 7945 } 7946 7947 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) { 7948 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix, 7949 wpa_s->conf->p2p_ssid_postfix ? 7950 os_strlen(wpa_s->conf->p2p_ssid_postfix) : 7951 0); 7952 } 7953 7954 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS) 7955 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss); 7956 7957 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) { 7958 u8 reg_class, channel; 7959 int ret; 7960 unsigned int r; 7961 u8 channel_forced; 7962 7963 if (wpa_s->conf->p2p_listen_reg_class && 7964 wpa_s->conf->p2p_listen_channel) { 7965 reg_class = wpa_s->conf->p2p_listen_reg_class; 7966 channel = wpa_s->conf->p2p_listen_channel; 7967 channel_forced = 1; 7968 } else { 7969 reg_class = 81; 7970 /* 7971 * Pick one of the social channels randomly as the 7972 * listen channel. 7973 */ 7974 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 7975 channel = 1; 7976 else 7977 channel = 1 + (r % 3) * 5; 7978 channel_forced = 0; 7979 } 7980 ret = p2p_set_listen_channel(p2p, reg_class, channel, 7981 channel_forced); 7982 if (ret) 7983 wpa_printf(MSG_ERROR, "P2P: Own listen channel update " 7984 "failed: %d", ret); 7985 } 7986 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) { 7987 u8 op_reg_class, op_channel, cfg_op_channel; 7988 int ret = 0; 7989 unsigned int r; 7990 if (wpa_s->conf->p2p_oper_reg_class && 7991 wpa_s->conf->p2p_oper_channel) { 7992 op_reg_class = wpa_s->conf->p2p_oper_reg_class; 7993 op_channel = wpa_s->conf->p2p_oper_channel; 7994 cfg_op_channel = 1; 7995 } else { 7996 op_reg_class = 81; 7997 /* 7998 * Use random operation channel from (1, 6, 11) 7999 *if no other preference is indicated. 8000 */ 8001 if (os_get_random((u8 *) &r, sizeof(r)) < 0) 8002 op_channel = 1; 8003 else 8004 op_channel = 1 + (r % 3) * 5; 8005 cfg_op_channel = 0; 8006 } 8007 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel, 8008 cfg_op_channel); 8009 if (ret) 8010 wpa_printf(MSG_ERROR, "P2P: Own oper channel update " 8011 "failed: %d", ret); 8012 } 8013 8014 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) { 8015 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan, 8016 wpa_s->conf->p2p_pref_chan) < 0) { 8017 wpa_printf(MSG_ERROR, "P2P: Preferred channel list " 8018 "update failed"); 8019 } 8020 8021 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) { 8022 wpa_printf(MSG_ERROR, "P2P: No GO channel list " 8023 "update failed"); 8024 } 8025 } 8026 8027 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN) 8028 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len); 8029 } 8030 8031 8032 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start, 8033 int duration) 8034 { 8035 if (!wpa_s->ap_iface) 8036 return -1; 8037 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start, 8038 duration); 8039 } 8040 8041 8042 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled) 8043 { 8044 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 8045 return -1; 8046 8047 wpa_s->global->cross_connection = enabled; 8048 p2p_set_cross_connect(wpa_s->global->p2p, enabled); 8049 8050 if (!enabled) { 8051 struct wpa_supplicant *iface; 8052 8053 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) 8054 { 8055 if (iface->cross_connect_enabled == 0) 8056 continue; 8057 8058 iface->cross_connect_enabled = 0; 8059 iface->cross_connect_in_use = 0; 8060 wpa_msg_global(iface->p2pdev, MSG_INFO, 8061 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", 8062 iface->ifname, 8063 iface->cross_connect_uplink); 8064 } 8065 } 8066 8067 return 0; 8068 } 8069 8070 8071 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink) 8072 { 8073 struct wpa_supplicant *iface; 8074 8075 if (!uplink->global->cross_connection) 8076 return; 8077 8078 for (iface = uplink->global->ifaces; iface; iface = iface->next) { 8079 if (!iface->cross_connect_enabled) 8080 continue; 8081 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != 8082 0) 8083 continue; 8084 if (iface->ap_iface == NULL) 8085 continue; 8086 if (iface->cross_connect_in_use) 8087 continue; 8088 8089 iface->cross_connect_in_use = 1; 8090 wpa_msg_global(iface->p2pdev, MSG_INFO, 8091 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", 8092 iface->ifname, iface->cross_connect_uplink); 8093 } 8094 } 8095 8096 8097 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink) 8098 { 8099 struct wpa_supplicant *iface; 8100 8101 for (iface = uplink->global->ifaces; iface; iface = iface->next) { 8102 if (!iface->cross_connect_enabled) 8103 continue; 8104 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) != 8105 0) 8106 continue; 8107 if (!iface->cross_connect_in_use) 8108 continue; 8109 8110 wpa_msg_global(iface->p2pdev, MSG_INFO, 8111 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s", 8112 iface->ifname, iface->cross_connect_uplink); 8113 iface->cross_connect_in_use = 0; 8114 } 8115 } 8116 8117 8118 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s) 8119 { 8120 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL || 8121 wpa_s->current_ssid->mode != WPAS_MODE_INFRA || 8122 wpa_s->cross_connect_disallowed) 8123 wpas_p2p_disable_cross_connect(wpa_s); 8124 else 8125 wpas_p2p_enable_cross_connect(wpa_s); 8126 if (!wpa_s->ap_iface && 8127 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0) 8128 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout"); 8129 } 8130 8131 8132 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s) 8133 { 8134 wpas_p2p_disable_cross_connect(wpa_s); 8135 if (!wpa_s->ap_iface && 8136 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout, 8137 wpa_s, NULL)) 8138 wpas_p2p_set_group_idle_timeout(wpa_s); 8139 } 8140 8141 8142 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s) 8143 { 8144 struct wpa_supplicant *iface; 8145 8146 if (!wpa_s->global->cross_connection) 8147 return; 8148 8149 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 8150 if (iface == wpa_s) 8151 continue; 8152 if (iface->drv_flags & 8153 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE) 8154 continue; 8155 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) && 8156 iface != wpa_s->parent) 8157 continue; 8158 8159 wpa_s->cross_connect_enabled = 1; 8160 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname, 8161 sizeof(wpa_s->cross_connect_uplink)); 8162 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from " 8163 "%s to %s whenever uplink is available", 8164 wpa_s->ifname, wpa_s->cross_connect_uplink); 8165 8166 if (iface->ap_iface || iface->current_ssid == NULL || 8167 iface->current_ssid->mode != WPAS_MODE_INFRA || 8168 iface->cross_connect_disallowed || 8169 iface->wpa_state != WPA_COMPLETED) 8170 break; 8171 8172 wpa_s->cross_connect_in_use = 1; 8173 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 8174 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s", 8175 wpa_s->ifname, wpa_s->cross_connect_uplink); 8176 break; 8177 } 8178 } 8179 8180 8181 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s) 8182 { 8183 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT && 8184 !wpa_s->p2p_in_provisioning) 8185 return 0; /* not P2P client operation */ 8186 8187 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC " 8188 "session overlap"); 8189 if (wpa_s != wpa_s->p2pdev) 8190 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP); 8191 wpas_p2p_group_formation_failed(wpa_s, 0); 8192 return 1; 8193 } 8194 8195 8196 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx) 8197 { 8198 struct wpa_supplicant *wpa_s = eloop_ctx; 8199 wpas_p2p_notif_pbc_overlap(wpa_s); 8200 } 8201 8202 8203 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s, 8204 enum wpas_p2p_channel_update_trig trig) 8205 { 8206 struct p2p_channels chan, cli_chan; 8207 struct wpa_used_freq_data *freqs = NULL; 8208 unsigned int num = wpa_s->num_multichan_concurrent; 8209 8210 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL) 8211 return; 8212 8213 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data)); 8214 if (!freqs) 8215 return; 8216 8217 num = get_shared_radio_freqs_data(wpa_s, freqs, num); 8218 8219 os_memset(&chan, 0, sizeof(chan)); 8220 os_memset(&cli_chan, 0, sizeof(cli_chan)); 8221 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan, 8222 is_p2p_6ghz_disabled(wpa_s->global->p2p))) { 8223 wpa_printf(MSG_ERROR, "P2P: Failed to update supported " 8224 "channel list"); 8225 return; 8226 } 8227 8228 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan); 8229 8230 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num); 8231 8232 /* 8233 * The used frequencies map changed, so it is possible that a GO is 8234 * using a channel that is no longer valid for P2P use. It is also 8235 * possible that due to policy consideration, it would be preferable to 8236 * move it to a frequency already used by other station interfaces. 8237 */ 8238 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig); 8239 8240 os_free(freqs); 8241 } 8242 8243 8244 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s, 8245 struct wpa_scan_results *scan_res) 8246 { 8247 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results"); 8248 } 8249 8250 8251 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s) 8252 { 8253 struct wpa_global *global = wpa_s->global; 8254 int found = 0; 8255 const u8 *peer; 8256 8257 if (global->p2p == NULL) 8258 return -1; 8259 8260 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation"); 8261 8262 if (wpa_s->pending_interface_name[0] && 8263 !is_zero_ether_addr(wpa_s->pending_interface_addr)) 8264 found = 1; 8265 8266 peer = p2p_get_go_neg_peer(global->p2p); 8267 if (peer) { 8268 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer " 8269 MACSTR, MAC2STR(peer)); 8270 p2p_unauthorize(global->p2p, peer); 8271 found = 1; 8272 } 8273 8274 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) { 8275 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join"); 8276 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore; 8277 found = 1; 8278 } 8279 8280 if (wpa_s->pending_pd_before_join) { 8281 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join"); 8282 wpa_s->pending_pd_before_join = 0; 8283 found = 1; 8284 } 8285 8286 wpas_p2p_stop_find(wpa_s); 8287 8288 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) { 8289 if (wpa_s == global->p2p_group_formation && 8290 (wpa_s->p2p_in_provisioning || 8291 wpa_s->parent->pending_interface_type == 8292 WPA_IF_P2P_CLIENT)) { 8293 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group " 8294 "formation found - cancelling", 8295 wpa_s->ifname); 8296 found = 1; 8297 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 8298 wpa_s->p2pdev, NULL); 8299 if (wpa_s->p2p_in_provisioning) { 8300 wpas_group_formation_completed(wpa_s, 0, 0); 8301 break; 8302 } 8303 wpas_p2p_group_delete(wpa_s, 8304 P2P_GROUP_REMOVAL_REQUESTED); 8305 break; 8306 } else if (wpa_s->p2p_in_invitation) { 8307 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling", 8308 wpa_s->ifname); 8309 found = 1; 8310 wpas_p2p_group_formation_failed(wpa_s, 0); 8311 break; 8312 } 8313 } 8314 8315 if (!found) { 8316 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found"); 8317 return -1; 8318 } 8319 8320 return 0; 8321 } 8322 8323 8324 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s) 8325 { 8326 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group) 8327 return; 8328 8329 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not " 8330 "being available anymore"); 8331 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE); 8332 } 8333 8334 8335 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s, 8336 int freq_24, int freq_5, int freq_overall) 8337 { 8338 struct p2p_data *p2p = wpa_s->global->p2p; 8339 if (p2p == NULL) 8340 return; 8341 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall); 8342 } 8343 8344 8345 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr) 8346 { 8347 u8 peer[ETH_ALEN]; 8348 struct p2p_data *p2p = wpa_s->global->p2p; 8349 8350 if (p2p == NULL) 8351 return -1; 8352 8353 if (hwaddr_aton(addr, peer)) 8354 return -1; 8355 8356 return p2p_unauthorize(p2p, peer); 8357 } 8358 8359 8360 /** 8361 * wpas_p2p_disconnect - Disconnect from a P2P Group 8362 * @wpa_s: Pointer to wpa_supplicant data 8363 * Returns: 0 on success, -1 on failure 8364 * 8365 * This can be used to disconnect from a group in which the local end is a P2P 8366 * Client or to end a P2P Group in case the local end is the Group Owner. If a 8367 * virtual network interface was created for this group, that interface will be 8368 * removed. Otherwise, only the configured P2P group network will be removed 8369 * from the interface. 8370 */ 8371 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s) 8372 { 8373 8374 if (wpa_s == NULL) 8375 return -1; 8376 8377 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ? 8378 -1 : 0; 8379 } 8380 8381 8382 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s) 8383 { 8384 int ret; 8385 8386 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 8387 return 0; 8388 8389 ret = p2p_in_progress(wpa_s->global->p2p); 8390 if (ret == 0) { 8391 /* 8392 * Check whether there is an ongoing WPS provisioning step (or 8393 * other parts of group formation) on another interface since 8394 * p2p_in_progress() does not report this to avoid issues for 8395 * scans during such provisioning step. 8396 */ 8397 if (wpa_s->global->p2p_group_formation && 8398 wpa_s->global->p2p_group_formation != wpa_s) { 8399 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) " 8400 "in group formation", 8401 wpa_s->global->p2p_group_formation->ifname); 8402 ret = 1; 8403 } 8404 } 8405 8406 if (!ret && wpa_s->global->p2p_go_wait_client.sec) { 8407 struct os_reltime now; 8408 os_get_reltime(&now); 8409 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client, 8410 P2P_MAX_INITIAL_CONN_WAIT_GO)) { 8411 /* Wait for the first client has expired */ 8412 wpa_s->global->p2p_go_wait_client.sec = 0; 8413 } else { 8414 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation"); 8415 ret = 1; 8416 } 8417 } 8418 8419 return ret; 8420 } 8421 8422 8423 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s, 8424 struct wpa_ssid *ssid) 8425 { 8426 if (wpa_s->p2p_in_provisioning && ssid->p2p_group && 8427 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 8428 wpa_s->p2pdev, NULL) > 0) { 8429 /** 8430 * Remove the network by scheduling the group formation 8431 * timeout to happen immediately. The teardown code 8432 * needs to be scheduled to run asynch later so that we 8433 * don't delete data from under ourselves unexpectedly. 8434 * Calling wpas_p2p_group_formation_timeout directly 8435 * causes a series of crashes in WPS failure scenarios. 8436 */ 8437 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to " 8438 "P2P group network getting removed"); 8439 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout, 8440 wpa_s->p2pdev, NULL); 8441 } 8442 } 8443 8444 8445 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s, 8446 const u8 *addr, const u8 *ssid, 8447 size_t ssid_len) 8448 { 8449 struct wpa_ssid *s; 8450 size_t i; 8451 8452 for (s = wpa_s->conf->ssid; s; s = s->next) { 8453 if (s->disabled != 2) 8454 continue; 8455 if (ssid && 8456 (ssid_len != s->ssid_len || 8457 os_memcmp(ssid, s->ssid, ssid_len) != 0)) 8458 continue; 8459 if (addr == NULL) { 8460 if (s->mode == WPAS_MODE_P2P_GO) 8461 return s; 8462 continue; 8463 } 8464 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0) 8465 return s; /* peer is GO in the persistent group */ 8466 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL) 8467 continue; 8468 for (i = 0; i < s->num_p2p_clients; i++) { 8469 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, 8470 addr, ETH_ALEN) == 0) 8471 return s; /* peer is P2P client in persistent 8472 * group */ 8473 } 8474 } 8475 8476 return NULL; 8477 } 8478 8479 8480 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s, 8481 const u8 *addr) 8482 { 8483 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout, 8484 wpa_s->p2pdev, NULL) > 0) { 8485 /* 8486 * This can happen if WPS provisioning step is not terminated 8487 * cleanly (e.g., P2P Client does not send WSC_Done). Since the 8488 * peer was able to connect, there is no need to time out group 8489 * formation after this, though. In addition, this is used with 8490 * the initial connection wait on the GO as a separate formation 8491 * timeout and as such, expected to be hit after the initial WPS 8492 * provisioning step. 8493 */ 8494 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection"); 8495 8496 if (!wpa_s->p2p_go_group_formation_completed && 8497 !wpa_s->group_formation_reported) { 8498 /* 8499 * GO has not yet notified group formation success since 8500 * the WPS step was not completed cleanly. Do that 8501 * notification now since the P2P Client was able to 8502 * connect and as such, must have received the 8503 * credential from the WPS step. 8504 */ 8505 if (wpa_s->global->p2p) 8506 p2p_wps_success_cb(wpa_s->global->p2p, addr); 8507 wpas_group_formation_completed(wpa_s, 1, 0); 8508 } 8509 } 8510 if (!wpa_s->p2p_go_group_formation_completed) { 8511 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection"); 8512 wpa_s->p2p_go_group_formation_completed = 1; 8513 wpa_s->global->p2p_group_formation = NULL; 8514 wpa_s->p2p_in_provisioning = 0; 8515 wpa_s->p2p_in_invitation = 0; 8516 } 8517 wpa_s->global->p2p_go_wait_client.sec = 0; 8518 if (addr == NULL) 8519 return; 8520 wpas_p2p_add_persistent_group_client(wpa_s, addr); 8521 } 8522 8523 8524 static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s, 8525 int group_added) 8526 { 8527 struct wpa_supplicant *group = wpa_s; 8528 int ret = 0; 8529 8530 if (wpa_s->global->p2p_group_formation) 8531 group = wpa_s->global->p2p_group_formation; 8532 wpa_s = wpa_s->global->p2p_init_wpa_s; 8533 offchannel_send_action_done(wpa_s); 8534 if (group_added) 8535 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT); 8536 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation"); 8537 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin, 8538 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0, 8539 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq, 8540 wpa_s->p2p_go_vht_center_freq2, 8541 wpa_s->p2p_persistent_id, 8542 wpa_s->p2p_pd_before_go_neg, 8543 wpa_s->p2p_go_ht40, 8544 wpa_s->p2p_go_vht, 8545 wpa_s->p2p_go_max_oper_chwidth, 8546 wpa_s->p2p_go_he, 8547 wpa_s->p2p_go_edmg, 8548 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p)); 8549 return ret; 8550 } 8551 8552 8553 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s) 8554 { 8555 int res; 8556 8557 if (!wpa_s->p2p_fallback_to_go_neg || 8558 wpa_s->p2p_in_provisioning <= 5) 8559 return 0; 8560 8561 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0) 8562 return 0; /* peer operating as a GO */ 8563 8564 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - " 8565 "fallback to GO Negotiation"); 8566 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG 8567 "reason=GO-not-found"); 8568 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1); 8569 8570 return res == 1 ? 2 : 1; 8571 } 8572 8573 8574 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s) 8575 { 8576 struct wpa_supplicant *ifs; 8577 8578 if (wpa_s->wpa_state > WPA_SCANNING) { 8579 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to " 8580 "concurrent operation", 8581 wpa_s->conf->p2p_search_delay); 8582 return wpa_s->conf->p2p_search_delay; 8583 } 8584 8585 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, 8586 radio_list) { 8587 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) { 8588 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search " 8589 "delay due to concurrent operation on " 8590 "interface %s", 8591 wpa_s->conf->p2p_search_delay, 8592 ifs->ifname); 8593 return wpa_s->conf->p2p_search_delay; 8594 } 8595 } 8596 8597 return 0; 8598 } 8599 8600 8601 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s, 8602 struct wpa_ssid *s, const u8 *addr, 8603 int iface_addr) 8604 { 8605 struct psk_list_entry *psk, *tmp; 8606 int changed = 0; 8607 8608 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry, 8609 list) { 8610 if ((iface_addr && !psk->p2p && 8611 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) || 8612 (!iface_addr && psk->p2p && 8613 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) { 8614 wpa_dbg(wpa_s, MSG_DEBUG, 8615 "P2P: Remove persistent group PSK list entry for " 8616 MACSTR " p2p=%u", 8617 MAC2STR(psk->addr), psk->p2p); 8618 dl_list_del(&psk->list); 8619 os_free(psk); 8620 changed++; 8621 } 8622 } 8623 8624 return changed; 8625 } 8626 8627 8628 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr, 8629 const u8 *p2p_dev_addr, 8630 const u8 *psk, size_t psk_len) 8631 { 8632 struct wpa_ssid *ssid = wpa_s->current_ssid; 8633 struct wpa_ssid *persistent; 8634 struct psk_list_entry *p, *last; 8635 8636 if (psk_len != sizeof(p->psk)) 8637 return; 8638 8639 if (p2p_dev_addr) { 8640 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR 8641 " p2p_dev_addr=" MACSTR, 8642 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr)); 8643 if (is_zero_ether_addr(p2p_dev_addr)) 8644 p2p_dev_addr = NULL; 8645 } else { 8646 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR, 8647 MAC2STR(mac_addr)); 8648 } 8649 8650 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) { 8651 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation"); 8652 /* To be added to persistent group once created */ 8653 if (wpa_s->global->add_psk == NULL) { 8654 wpa_s->global->add_psk = os_zalloc(sizeof(*p)); 8655 if (wpa_s->global->add_psk == NULL) 8656 return; 8657 } 8658 p = wpa_s->global->add_psk; 8659 if (p2p_dev_addr) { 8660 p->p2p = 1; 8661 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); 8662 } else { 8663 p->p2p = 0; 8664 os_memcpy(p->addr, mac_addr, ETH_ALEN); 8665 } 8666 os_memcpy(p->psk, psk, psk_len); 8667 return; 8668 } 8669 8670 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) { 8671 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO"); 8672 return; 8673 } 8674 8675 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid, 8676 ssid->ssid_len); 8677 if (!persistent) { 8678 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK"); 8679 return; 8680 } 8681 8682 p = os_zalloc(sizeof(*p)); 8683 if (p == NULL) 8684 return; 8685 if (p2p_dev_addr) { 8686 p->p2p = 1; 8687 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN); 8688 } else { 8689 p->p2p = 0; 8690 os_memcpy(p->addr, mac_addr, ETH_ALEN); 8691 } 8692 os_memcpy(p->psk, psk, psk_len); 8693 8694 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS && 8695 (last = dl_list_last(&persistent->psk_list, 8696 struct psk_list_entry, list))) { 8697 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for " 8698 MACSTR " (p2p=%u) to make room for a new one", 8699 MAC2STR(last->addr), last->p2p); 8700 dl_list_del(&last->list); 8701 os_free(last); 8702 } 8703 8704 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent, 8705 p2p_dev_addr ? p2p_dev_addr : mac_addr, 8706 p2p_dev_addr == NULL); 8707 if (p2p_dev_addr) { 8708 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr=" 8709 MACSTR, MAC2STR(p2p_dev_addr)); 8710 } else { 8711 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR, 8712 MAC2STR(mac_addr)); 8713 } 8714 dl_list_add(&persistent->psk_list, &p->list); 8715 8716 if (wpa_s->p2pdev->conf->update_config && 8717 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf)) 8718 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration"); 8719 } 8720 8721 8722 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s, 8723 struct wpa_ssid *s, const u8 *addr, 8724 int iface_addr) 8725 { 8726 int res; 8727 8728 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr); 8729 if (res > 0 && wpa_s->conf->update_config && 8730 wpa_config_write(wpa_s->confname, wpa_s->conf)) 8731 wpa_dbg(wpa_s, MSG_DEBUG, 8732 "P2P: Failed to update configuration"); 8733 } 8734 8735 8736 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s, 8737 const u8 *peer, int iface_addr) 8738 { 8739 struct hostapd_data *hapd; 8740 struct hostapd_wpa_psk *psk, *prev, *rem; 8741 struct sta_info *sta; 8742 8743 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL || 8744 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO) 8745 return; 8746 8747 /* Remove per-station PSK entry */ 8748 hapd = wpa_s->ap_iface->bss[0]; 8749 prev = NULL; 8750 psk = hapd->conf->ssid.wpa_psk; 8751 while (psk) { 8752 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) || 8753 (!iface_addr && 8754 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) { 8755 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for " 8756 MACSTR " iface_addr=%d", 8757 MAC2STR(peer), iface_addr); 8758 if (prev) 8759 prev->next = psk->next; 8760 else 8761 hapd->conf->ssid.wpa_psk = psk->next; 8762 rem = psk; 8763 psk = psk->next; 8764 os_free(rem); 8765 } else { 8766 prev = psk; 8767 psk = psk->next; 8768 } 8769 } 8770 8771 /* Disconnect from group */ 8772 if (iface_addr) 8773 sta = ap_get_sta(hapd, peer); 8774 else 8775 sta = ap_get_sta_p2p(hapd, peer); 8776 if (sta) { 8777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR 8778 " (iface_addr=%d) from group", 8779 MAC2STR(peer), iface_addr); 8780 hostapd_drv_sta_deauth(hapd, sta->addr, 8781 WLAN_REASON_DEAUTH_LEAVING); 8782 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING); 8783 } 8784 } 8785 8786 8787 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer, 8788 int iface_addr) 8789 { 8790 struct wpa_ssid *s; 8791 struct wpa_supplicant *w; 8792 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s; 8793 8794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer)); 8795 8796 /* Remove from any persistent group */ 8797 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) { 8798 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO) 8799 continue; 8800 if (!iface_addr) 8801 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0); 8802 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr); 8803 } 8804 8805 /* Remove from any operating group */ 8806 for (w = wpa_s->global->ifaces; w; w = w->next) 8807 wpas_p2p_remove_client_go(w, peer, iface_addr); 8808 } 8809 8810 8811 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx) 8812 { 8813 struct wpa_supplicant *wpa_s = eloop_ctx; 8814 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE); 8815 } 8816 8817 8818 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx) 8819 { 8820 struct wpa_supplicant *wpa_s = eloop_ctx; 8821 8822 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group"); 8823 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT); 8824 } 8825 8826 8827 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq, 8828 struct wpa_ssid *ssid) 8829 { 8830 struct wpa_supplicant *iface; 8831 8832 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 8833 if (!iface->current_ssid || 8834 iface->current_ssid->frequency == freq || 8835 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && 8836 !iface->current_ssid->p2p_group)) 8837 continue; 8838 8839 /* Remove the connection with least priority */ 8840 if (!wpas_is_p2p_prioritized(iface)) { 8841 /* STA connection has priority over existing 8842 * P2P connection, so remove the interface. */ 8843 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict"); 8844 eloop_register_timeout(0, 0, 8845 wpas_p2p_group_freq_conflict, 8846 iface, NULL); 8847 /* If connection in progress is P2P connection, do not 8848 * proceed for the connection. */ 8849 if (wpa_s == iface) 8850 return -1; 8851 else 8852 return 0; 8853 } else { 8854 /* P2P connection has priority, disable the STA network 8855 */ 8856 wpa_supplicant_disable_network(wpa_s->global->ifaces, 8857 ssid); 8858 wpa_msg(wpa_s->global->ifaces, MSG_INFO, 8859 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id); 8860 os_memset(wpa_s->global->ifaces->pending_bssid, 0, 8861 ETH_ALEN); 8862 /* If P2P connection is in progress, continue 8863 * connecting...*/ 8864 if (wpa_s == iface) 8865 return 0; 8866 else 8867 return -1; 8868 } 8869 } 8870 8871 return 0; 8872 } 8873 8874 8875 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s) 8876 { 8877 struct wpa_ssid *ssid = wpa_s->current_ssid; 8878 8879 if (ssid == NULL || !ssid->p2p_group) 8880 return 0; 8881 8882 if (wpa_s->p2p_last_4way_hs_fail && 8883 wpa_s->p2p_last_4way_hs_fail == ssid) { 8884 u8 go_dev_addr[ETH_ALEN]; 8885 struct wpa_ssid *persistent; 8886 8887 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr, 8888 ssid->ssid, 8889 ssid->ssid_len) <= 0) { 8890 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group"); 8891 goto disconnect; 8892 } 8893 8894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr=" 8895 MACSTR, MAC2STR(go_dev_addr)); 8896 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr, 8897 ssid->ssid, 8898 ssid->ssid_len); 8899 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) { 8900 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored"); 8901 goto disconnect; 8902 } 8903 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, 8904 P2P_EVENT_PERSISTENT_PSK_FAIL "%d", 8905 persistent->id); 8906 disconnect: 8907 wpa_s->p2p_last_4way_hs_fail = NULL; 8908 /* 8909 * Remove the group from a timeout to avoid issues with caller 8910 * continuing to use the interface if this is on a P2P group 8911 * interface. 8912 */ 8913 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal, 8914 wpa_s, NULL); 8915 return 1; 8916 } 8917 8918 wpa_s->p2p_last_4way_hs_fail = ssid; 8919 return 0; 8920 } 8921 8922 8923 #ifdef CONFIG_WPS_NFC 8924 8925 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc, 8926 struct wpabuf *p2p) 8927 { 8928 struct wpabuf *ret; 8929 size_t wsc_len; 8930 8931 if (p2p == NULL) { 8932 wpabuf_free(wsc); 8933 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover"); 8934 return NULL; 8935 } 8936 8937 wsc_len = wsc ? wpabuf_len(wsc) : 0; 8938 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p)); 8939 if (ret == NULL) { 8940 wpabuf_free(wsc); 8941 wpabuf_free(p2p); 8942 return NULL; 8943 } 8944 8945 wpabuf_put_be16(ret, wsc_len); 8946 if (wsc) 8947 wpabuf_put_buf(ret, wsc); 8948 wpabuf_put_be16(ret, wpabuf_len(p2p)); 8949 wpabuf_put_buf(ret, p2p); 8950 8951 wpabuf_free(wsc); 8952 wpabuf_free(p2p); 8953 wpa_hexdump_buf(MSG_DEBUG, 8954 "P2P: Generated NFC connection handover message", ret); 8955 8956 if (ndef && ret) { 8957 struct wpabuf *tmp; 8958 tmp = ndef_build_p2p(ret); 8959 wpabuf_free(ret); 8960 if (tmp == NULL) { 8961 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request"); 8962 return NULL; 8963 } 8964 ret = tmp; 8965 } 8966 8967 return ret; 8968 } 8969 8970 8971 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s, 8972 struct wpa_ssid **ssid, u8 *go_dev_addr) 8973 { 8974 struct wpa_supplicant *iface; 8975 8976 if (go_dev_addr) 8977 os_memset(go_dev_addr, 0, ETH_ALEN); 8978 if (ssid) 8979 *ssid = NULL; 8980 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 8981 if (iface->wpa_state < WPA_ASSOCIATING || 8982 iface->current_ssid == NULL || iface->assoc_freq == 0 || 8983 !iface->current_ssid->p2p_group || 8984 iface->current_ssid->mode != WPAS_MODE_INFRA) 8985 continue; 8986 if (ssid) 8987 *ssid = iface->current_ssid; 8988 if (go_dev_addr) 8989 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN); 8990 return iface->assoc_freq; 8991 } 8992 return 0; 8993 } 8994 8995 8996 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s, 8997 int ndef) 8998 { 8999 struct wpabuf *wsc, *p2p; 9000 struct wpa_ssid *ssid; 9001 u8 go_dev_addr[ETH_ALEN]; 9002 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); 9003 9004 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) { 9005 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request"); 9006 return NULL; 9007 } 9008 9009 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && 9010 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, 9011 &wpa_s->conf->wps_nfc_dh_privkey) < 0) { 9012 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request"); 9013 return NULL; 9014 } 9015 9016 if (cli_freq == 0) { 9017 wsc = wps_build_nfc_handover_req_p2p( 9018 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey); 9019 } else 9020 wsc = NULL; 9021 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq, 9022 go_dev_addr, ssid ? ssid->ssid : NULL, 9023 ssid ? ssid->ssid_len : 0); 9024 9025 return wpas_p2p_nfc_handover(ndef, wsc, p2p); 9026 } 9027 9028 9029 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s, 9030 int ndef, int tag) 9031 { 9032 struct wpabuf *wsc, *p2p; 9033 struct wpa_ssid *ssid; 9034 u8 go_dev_addr[ETH_ALEN]; 9035 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr); 9036 9037 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 9038 return NULL; 9039 9040 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL && 9041 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, 9042 &wpa_s->conf->wps_nfc_dh_privkey) < 0) 9043 return NULL; 9044 9045 if (cli_freq == 0) { 9046 wsc = wps_build_nfc_handover_sel_p2p( 9047 wpa_s->parent->wps, 9048 tag ? wpa_s->conf->wps_nfc_dev_pw_id : 9049 DEV_PW_NFC_CONNECTION_HANDOVER, 9050 wpa_s->conf->wps_nfc_dh_pubkey, 9051 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL); 9052 } else 9053 wsc = NULL; 9054 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq, 9055 go_dev_addr, ssid ? ssid->ssid : NULL, 9056 ssid ? ssid->ssid_len : 0); 9057 9058 return wpas_p2p_nfc_handover(ndef, wsc, p2p); 9059 } 9060 9061 9062 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s, 9063 struct p2p_nfc_params *params) 9064 { 9065 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC " 9066 "connection handover (freq=%d)", 9067 params->go_freq); 9068 9069 if (params->go_freq && params->go_ssid_len) { 9070 wpa_s->p2p_wps_method = WPS_NFC; 9071 wpa_s->pending_join_wps_method = WPS_NFC; 9072 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN); 9073 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr, 9074 ETH_ALEN); 9075 return wpas_p2p_join_start(wpa_s, params->go_freq, 9076 params->go_ssid, 9077 params->go_ssid_len); 9078 } 9079 9080 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, 9081 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent, 9082 params->go_freq, wpa_s->p2p_go_vht_center_freq2, 9083 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth, 9084 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg, 9085 params->go_ssid_len ? params->go_ssid : NULL, 9086 params->go_ssid_len, false); 9087 } 9088 9089 9090 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s, 9091 struct p2p_nfc_params *params, int tag) 9092 { 9093 int res, persistent; 9094 struct wpa_ssid *ssid; 9095 9096 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC " 9097 "connection handover"); 9098 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) { 9099 ssid = wpa_s->current_ssid; 9100 if (ssid == NULL) 9101 continue; 9102 if (ssid->mode != WPAS_MODE_P2P_GO) 9103 continue; 9104 if (wpa_s->ap_iface == NULL) 9105 continue; 9106 break; 9107 } 9108 if (wpa_s == NULL) { 9109 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface"); 9110 return -1; 9111 } 9112 9113 if (wpa_s->p2pdev->p2p_oob_dev_pw_id != 9114 DEV_PW_NFC_CONNECTION_HANDOVER && 9115 !wpa_s->p2pdev->p2p_oob_dev_pw) { 9116 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known"); 9117 return -1; 9118 } 9119 res = wpas_ap_wps_add_nfc_pw( 9120 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id, 9121 wpa_s->p2pdev->p2p_oob_dev_pw, 9122 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ? 9123 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL); 9124 if (res) 9125 return res; 9126 9127 if (!tag) { 9128 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation"); 9129 return 0; 9130 } 9131 9132 if (!params->peer || 9133 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE)) 9134 return 0; 9135 9136 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR 9137 " to join", MAC2STR(params->peer->p2p_device_addr)); 9138 9139 wpa_s->global->p2p_invite_group = wpa_s; 9140 persistent = ssid->p2p_persistent_group && 9141 wpas_p2p_get_persistent(wpa_s->p2pdev, 9142 params->peer->p2p_device_addr, 9143 ssid->ssid, ssid->ssid_len); 9144 wpa_s->p2pdev->pending_invite_ssid_id = -1; 9145 9146 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr, 9147 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr, 9148 ssid->ssid, ssid->ssid_len, ssid->frequency, 9149 wpa_s->global->p2p_dev_addr, persistent, 0, 9150 wpa_s->p2pdev->p2p_oob_dev_pw_id); 9151 } 9152 9153 9154 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s, 9155 struct p2p_nfc_params *params, 9156 int forced_freq) 9157 { 9158 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC " 9159 "connection handover"); 9160 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, 9161 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent, 9162 forced_freq, wpa_s->p2p_go_vht_center_freq2, 9163 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth, 9164 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg, 9165 NULL, 0, false); 9166 } 9167 9168 9169 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s, 9170 struct p2p_nfc_params *params, 9171 int forced_freq) 9172 { 9173 int res; 9174 9175 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC " 9176 "connection handover"); 9177 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL, 9178 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent, 9179 forced_freq, wpa_s->p2p_go_vht_center_freq2, 9180 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth, 9181 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg, 9182 NULL, 0, false); 9183 if (res) 9184 return res; 9185 9186 res = wpas_p2p_listen(wpa_s, 60); 9187 if (res) { 9188 p2p_unauthorize(wpa_s->global->p2p, 9189 params->peer->p2p_device_addr); 9190 } 9191 9192 return res; 9193 } 9194 9195 9196 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s, 9197 const struct wpabuf *data, 9198 int sel, int tag, int forced_freq) 9199 { 9200 const u8 *pos, *end; 9201 u16 len, id; 9202 struct p2p_nfc_params params; 9203 int res; 9204 9205 os_memset(¶ms, 0, sizeof(params)); 9206 params.sel = sel; 9207 9208 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data); 9209 9210 pos = wpabuf_head(data); 9211 end = pos + wpabuf_len(data); 9212 9213 if (end - pos < 2) { 9214 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC " 9215 "attributes"); 9216 return -1; 9217 } 9218 len = WPA_GET_BE16(pos); 9219 pos += 2; 9220 if (len > end - pos) { 9221 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC " 9222 "attributes"); 9223 return -1; 9224 } 9225 params.wsc_attr = pos; 9226 params.wsc_len = len; 9227 pos += len; 9228 9229 if (end - pos < 2) { 9230 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P " 9231 "attributes"); 9232 return -1; 9233 } 9234 len = WPA_GET_BE16(pos); 9235 pos += 2; 9236 if (len > end - pos) { 9237 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P " 9238 "attributes"); 9239 return -1; 9240 } 9241 params.p2p_attr = pos; 9242 params.p2p_len = len; 9243 pos += len; 9244 9245 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes", 9246 params.wsc_attr, params.wsc_len); 9247 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes", 9248 params.p2p_attr, params.p2p_len); 9249 if (pos < end) { 9250 wpa_hexdump(MSG_DEBUG, 9251 "P2P: Ignored extra data after P2P attributes", 9252 pos, end - pos); 9253 } 9254 9255 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms); 9256 if (res) 9257 return res; 9258 9259 if (params.next_step == NO_ACTION) 9260 return 0; 9261 9262 if (params.next_step == BOTH_GO) { 9263 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR, 9264 MAC2STR(params.peer->p2p_device_addr)); 9265 return 0; 9266 } 9267 9268 if (params.next_step == PEER_CLIENT) { 9269 if (!is_zero_ether_addr(params.go_dev_addr)) { 9270 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT 9271 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR 9272 " ssid=\"%s\"", 9273 MAC2STR(params.peer->p2p_device_addr), 9274 params.go_freq, 9275 MAC2STR(params.go_dev_addr), 9276 wpa_ssid_txt(params.go_ssid, 9277 params.go_ssid_len)); 9278 } else { 9279 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT 9280 "peer=" MACSTR " freq=%d", 9281 MAC2STR(params.peer->p2p_device_addr), 9282 params.go_freq); 9283 } 9284 return 0; 9285 } 9286 9287 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) { 9288 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer=" 9289 MACSTR, MAC2STR(params.peer->p2p_device_addr)); 9290 return 0; 9291 } 9292 9293 wpabuf_free(wpa_s->p2p_oob_dev_pw); 9294 wpa_s->p2p_oob_dev_pw = NULL; 9295 9296 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) { 9297 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw " 9298 "received"); 9299 return -1; 9300 } 9301 9302 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN); 9303 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id); 9304 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash", 9305 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); 9306 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash, 9307 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN); 9308 wpa_s->p2p_peer_oob_pk_hash_known = 1; 9309 9310 if (tag) { 9311 if (id < 0x10) { 9312 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid " 9313 "peer OOB Device Password Id %u", id); 9314 return -1; 9315 } 9316 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB " 9317 "Device Password Id %u", id); 9318 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password", 9319 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, 9320 params.oob_dev_pw_len - 9321 WPS_OOB_PUBKEY_HASH_LEN - 2); 9322 wpa_s->p2p_oob_dev_pw_id = id; 9323 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy( 9324 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2, 9325 params.oob_dev_pw_len - 9326 WPS_OOB_PUBKEY_HASH_LEN - 2); 9327 if (wpa_s->p2p_oob_dev_pw == NULL) 9328 return -1; 9329 9330 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL && 9331 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey, 9332 &wpa_s->conf->wps_nfc_dh_privkey) < 0) 9333 return -1; 9334 } else { 9335 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake " 9336 "without Device Password"); 9337 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER; 9338 } 9339 9340 switch (params.next_step) { 9341 case NO_ACTION: 9342 case BOTH_GO: 9343 case PEER_CLIENT: 9344 /* already covered above */ 9345 return 0; 9346 case JOIN_GROUP: 9347 return wpas_p2p_nfc_join_group(wpa_s, ¶ms); 9348 case AUTH_JOIN: 9349 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag); 9350 case INIT_GO_NEG: 9351 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq); 9352 case RESP_GO_NEG: 9353 /* TODO: use own OOB Dev Pw */ 9354 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq); 9355 } 9356 9357 return -1; 9358 } 9359 9360 9361 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s, 9362 const struct wpabuf *data, int forced_freq) 9363 { 9364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 9365 return -1; 9366 9367 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq); 9368 } 9369 9370 9371 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init, 9372 const struct wpabuf *req, 9373 const struct wpabuf *sel, int forced_freq) 9374 { 9375 struct wpabuf *tmp; 9376 int ret; 9377 9378 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 9379 return -1; 9380 9381 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported"); 9382 9383 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req", 9384 wpabuf_head(req), wpabuf_len(req)); 9385 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel", 9386 wpabuf_head(sel), wpabuf_len(sel)); 9387 if (forced_freq) 9388 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq); 9389 tmp = ndef_parse_p2p(init ? sel : req); 9390 if (tmp == NULL) { 9391 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF"); 9392 return -1; 9393 } 9394 9395 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0, 9396 forced_freq); 9397 wpabuf_free(tmp); 9398 9399 return ret; 9400 } 9401 9402 9403 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled) 9404 { 9405 const u8 *if_addr; 9406 int go_intent = wpa_s->conf->p2p_go_intent; 9407 struct wpa_supplicant *iface; 9408 9409 if (wpa_s->global->p2p == NULL) 9410 return -1; 9411 9412 if (!enabled) { 9413 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag"); 9414 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) 9415 { 9416 if (!iface->ap_iface) 9417 continue; 9418 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]); 9419 } 9420 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0, 9421 0, NULL); 9422 if (wpa_s->p2p_nfc_tag_enabled) 9423 wpas_p2p_remove_pending_group_interface(wpa_s); 9424 wpa_s->p2p_nfc_tag_enabled = 0; 9425 return 0; 9426 } 9427 9428 if (wpa_s->global->p2p_disabled) 9429 return -1; 9430 9431 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL || 9432 wpa_s->conf->wps_nfc_dh_privkey == NULL || 9433 wpa_s->conf->wps_nfc_dev_pw == NULL || 9434 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) { 9435 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured " 9436 "to allow static handover cases"); 9437 return -1; 9438 } 9439 9440 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag"); 9441 9442 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; 9443 wpabuf_free(wpa_s->p2p_oob_dev_pw); 9444 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); 9445 if (wpa_s->p2p_oob_dev_pw == NULL) 9446 return -1; 9447 wpa_s->p2p_peer_oob_pk_hash_known = 0; 9448 9449 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO || 9450 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) { 9451 /* 9452 * P2P Group Interface present and the command came on group 9453 * interface, so enable the token for the current interface. 9454 */ 9455 wpa_s->create_p2p_iface = 0; 9456 } else { 9457 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s); 9458 } 9459 9460 if (wpa_s->create_p2p_iface) { 9461 enum wpa_driver_if_type iftype; 9462 /* Prepare to add a new interface for the group */ 9463 iftype = WPA_IF_P2P_GROUP; 9464 if (go_intent == 15) 9465 iftype = WPA_IF_P2P_GO; 9466 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) { 9467 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new " 9468 "interface for the group"); 9469 return -1; 9470 } 9471 9472 if_addr = wpa_s->pending_interface_addr; 9473 } else if (wpa_s->p2p_mgmt) 9474 if_addr = wpa_s->parent->own_addr; 9475 else 9476 if_addr = wpa_s->own_addr; 9477 9478 wpa_s->p2p_nfc_tag_enabled = enabled; 9479 9480 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) { 9481 struct hostapd_data *hapd; 9482 if (iface->ap_iface == NULL) 9483 continue; 9484 hapd = iface->ap_iface->bss[0]; 9485 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey); 9486 hapd->conf->wps_nfc_dh_pubkey = 9487 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey); 9488 wpabuf_free(hapd->conf->wps_nfc_dh_privkey); 9489 hapd->conf->wps_nfc_dh_privkey = 9490 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey); 9491 wpabuf_free(hapd->conf->wps_nfc_dev_pw); 9492 hapd->conf->wps_nfc_dev_pw = 9493 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw); 9494 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id; 9495 9496 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) { 9497 wpa_dbg(iface, MSG_DEBUG, 9498 "P2P: Failed to enable NFC Tag for GO"); 9499 } 9500 } 9501 p2p_set_authorized_oob_dev_pw_id( 9502 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent, 9503 if_addr); 9504 9505 return 0; 9506 } 9507 9508 #endif /* CONFIG_WPS_NFC */ 9509 9510 9511 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s, 9512 struct wpa_used_freq_data *freqs, 9513 unsigned int num) 9514 { 9515 u8 curr_chan, cand, chan; 9516 unsigned int i; 9517 9518 /* 9519 * If possible, optimize the Listen channel to be a channel that is 9520 * already used by one of the other interfaces. 9521 */ 9522 if (!wpa_s->conf->p2p_optimize_listen_chan) 9523 return; 9524 9525 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED) 9526 return; 9527 9528 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p); 9529 for (i = 0, cand = 0; i < num; i++) { 9530 ieee80211_freq_to_chan(freqs[i].freq, &chan); 9531 if (curr_chan == chan) { 9532 cand = 0; 9533 break; 9534 } 9535 9536 if (chan == 1 || chan == 6 || chan == 11) 9537 cand = chan; 9538 } 9539 9540 if (cand) { 9541 wpa_dbg(wpa_s, MSG_DEBUG, 9542 "P2P: Update Listen channel to %u based on operating channel", 9543 cand); 9544 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0); 9545 } 9546 } 9547 9548 9549 static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s) 9550 { 9551 struct hostapd_config *conf; 9552 struct p2p_go_neg_results params; 9553 struct csa_settings csa_settings; 9554 struct wpa_ssid *current_ssid = wpa_s->current_ssid; 9555 int old_freq = current_ssid->frequency; 9556 int ret; 9557 9558 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) { 9559 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled"); 9560 return -1; 9561 } 9562 9563 /* 9564 * TODO: This function may not always work correctly. For example, 9565 * when we have a running GO and a BSS on a DFS channel. 9566 */ 9567 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, 0, 0, 0, 0, 9568 NULL)) { 9569 wpa_dbg(wpa_s, MSG_DEBUG, 9570 "P2P CSA: Failed to select new frequency for GO"); 9571 return -1; 9572 } 9573 9574 if (current_ssid->frequency == params.freq) { 9575 wpa_dbg(wpa_s, MSG_DEBUG, 9576 "P2P CSA: Selected same frequency - not moving GO"); 9577 return 0; 9578 } 9579 9580 conf = hostapd_config_defaults(); 9581 if (!conf) { 9582 wpa_dbg(wpa_s, MSG_DEBUG, 9583 "P2P CSA: Failed to allocate default config"); 9584 return -1; 9585 } 9586 9587 current_ssid->frequency = params.freq; 9588 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) { 9589 wpa_dbg(wpa_s, MSG_DEBUG, 9590 "P2P CSA: Failed to create new GO config"); 9591 ret = -1; 9592 goto out; 9593 } 9594 9595 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) { 9596 wpa_dbg(wpa_s, MSG_DEBUG, 9597 "P2P CSA: CSA to a different band is not supported"); 9598 ret = -1; 9599 goto out; 9600 } 9601 9602 os_memset(&csa_settings, 0, sizeof(csa_settings)); 9603 csa_settings.cs_count = P2P_GO_CSA_COUNT; 9604 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX; 9605 csa_settings.freq_params.freq = params.freq; 9606 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel; 9607 csa_settings.freq_params.ht_enabled = conf->ieee80211n; 9608 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20; 9609 9610 if (conf->ieee80211ac) { 9611 int freq1 = 0, freq2 = 0; 9612 u8 chan, opclass; 9613 9614 if (ieee80211_freq_to_channel_ext(params.freq, 9615 conf->secondary_channel, 9616 conf->vht_oper_chwidth, 9617 &opclass, &chan) == 9618 NUM_HOSTAPD_MODES) { 9619 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq"); 9620 ret = -1; 9621 goto out; 9622 } 9623 9624 if (conf->vht_oper_centr_freq_seg0_idx) 9625 freq1 = ieee80211_chan_to_freq( 9626 NULL, opclass, 9627 conf->vht_oper_centr_freq_seg0_idx); 9628 9629 if (conf->vht_oper_centr_freq_seg1_idx) 9630 freq2 = ieee80211_chan_to_freq( 9631 NULL, opclass, 9632 conf->vht_oper_centr_freq_seg1_idx); 9633 9634 if (freq1 < 0 || freq2 < 0) { 9635 wpa_dbg(wpa_s, MSG_DEBUG, 9636 "P2P CSA: Selected invalid VHT center freqs"); 9637 ret = -1; 9638 goto out; 9639 } 9640 9641 csa_settings.freq_params.vht_enabled = conf->ieee80211ac; 9642 csa_settings.freq_params.center_freq1 = freq1; 9643 csa_settings.freq_params.center_freq2 = freq2; 9644 9645 switch (conf->vht_oper_chwidth) { 9646 case CHANWIDTH_80MHZ: 9647 case CHANWIDTH_80P80MHZ: 9648 csa_settings.freq_params.bandwidth = 80; 9649 break; 9650 case CHANWIDTH_160MHZ: 9651 csa_settings.freq_params.bandwidth = 160; 9652 break; 9653 } 9654 } 9655 9656 ret = ap_switch_channel(wpa_s, &csa_settings); 9657 out: 9658 current_ssid->frequency = old_freq; 9659 hostapd_config_free(conf); 9660 return ret; 9661 } 9662 9663 9664 static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s) 9665 { 9666 struct p2p_go_neg_results params; 9667 struct wpa_ssid *current_ssid = wpa_s->current_ssid; 9668 void (*ap_configured_cb)(void *ctx, void *data); 9669 void *ap_configured_cb_ctx, *ap_configured_cb_data; 9670 9671 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP); 9672 9673 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz", 9674 current_ssid->frequency); 9675 9676 /* Stop the AP functionality */ 9677 /* TODO: Should do this in a way that does not indicated to possible 9678 * P2P Clients in the group that the group is terminated. */ 9679 /* If this action occurs before a group is started, the callback should 9680 * be preserved, or GROUP-STARTED event would be lost. If this action 9681 * occurs after a group is started, these pointers are all NULL and 9682 * harmless. */ 9683 ap_configured_cb = wpa_s->ap_configured_cb; 9684 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx; 9685 ap_configured_cb_data = wpa_s->ap_configured_cb_data; 9686 wpa_supplicant_ap_deinit(wpa_s); 9687 9688 /* Reselect the GO frequency */ 9689 if (wpas_p2p_init_go_params(wpa_s, ¶ms, 0, 0, 0, 0, 0, 0, 0, 9690 NULL)) { 9691 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq"); 9692 wpas_p2p_group_delete(wpa_s, 9693 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL); 9694 return; 9695 } 9696 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)", 9697 params.freq); 9698 9699 if (params.freq && 9700 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) { 9701 wpa_printf(MSG_DEBUG, 9702 "P2P: Selected freq (%u MHz) is not valid for P2P", 9703 params.freq); 9704 wpas_p2p_group_delete(wpa_s, 9705 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL); 9706 return; 9707 } 9708 9709 /* Restore preserved callback parameters */ 9710 wpa_s->ap_configured_cb = ap_configured_cb; 9711 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx; 9712 wpa_s->ap_configured_cb_data = ap_configured_cb_data; 9713 9714 /* Update the frequency */ 9715 current_ssid->frequency = params.freq; 9716 wpa_s->connect_without_scan = current_ssid; 9717 wpa_s->reassociate = 1; 9718 wpa_s->disconnected = 0; 9719 wpa_supplicant_req_scan(wpa_s, 0, 0); 9720 } 9721 9722 9723 static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx) 9724 { 9725 struct wpa_supplicant *wpa_s = eloop_ctx; 9726 9727 if (!wpa_s->ap_iface || !wpa_s->current_ssid) 9728 return; 9729 9730 wpas_p2p_go_update_common_freqs(wpa_s); 9731 9732 /* Do not move GO in the middle of a CSA */ 9733 if (hostapd_csa_in_progress(wpa_s->ap_iface)) { 9734 wpa_printf(MSG_DEBUG, 9735 "P2P: CSA is in progress - not moving GO"); 9736 return; 9737 } 9738 9739 /* 9740 * First, try a channel switch flow. If it is not supported or fails, 9741 * take down the GO and bring it up again. 9742 */ 9743 if (wpas_p2p_move_go_csa(wpa_s) < 0) 9744 wpas_p2p_move_go_no_csa(wpa_s); 9745 } 9746 9747 9748 static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx) 9749 { 9750 struct wpa_supplicant *wpa_s = eloop_ctx; 9751 struct wpa_used_freq_data *freqs = NULL; 9752 unsigned int num = wpa_s->num_multichan_concurrent; 9753 9754 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data)); 9755 if (!freqs) 9756 return; 9757 9758 num = get_shared_radio_freqs_data(wpa_s, freqs, num); 9759 9760 /* Previous attempt to move a GO was not possible -- try again. */ 9761 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, 9762 WPAS_P2P_CHANNEL_UPDATE_ANY); 9763 9764 os_free(freqs); 9765 } 9766 9767 9768 /* 9769 * Consider moving a GO from its currently used frequency: 9770 * 1. It is possible that due to regulatory consideration the frequency 9771 * can no longer be used and there is a need to evacuate the GO. 9772 * 2. It is possible that due to MCC considerations, it would be preferable 9773 * to move the GO to a channel that is currently used by some other 9774 * station interface. 9775 * 9776 * In case a frequency that became invalid is once again valid, cancel a 9777 * previously initiated GO frequency change. 9778 */ 9779 static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s, 9780 struct wpa_used_freq_data *freqs, 9781 unsigned int num) 9782 { 9783 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0; 9784 unsigned int timeout; 9785 int freq; 9786 int dfs_offload; 9787 9788 wpas_p2p_go_update_common_freqs(wpa_s); 9789 9790 freq = wpa_s->current_ssid->frequency; 9791 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) && 9792 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes); 9793 for (i = 0, invalid_freq = 0; i < num; i++) { 9794 if (freqs[i].freq == freq) { 9795 flags = freqs[i].flags; 9796 9797 /* The channel is invalid, must change it */ 9798 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) && 9799 !dfs_offload) { 9800 wpa_dbg(wpa_s, MSG_DEBUG, 9801 "P2P: Freq=%d MHz no longer valid for GO", 9802 freq); 9803 invalid_freq = 1; 9804 } 9805 } else if (freqs[i].flags == 0) { 9806 /* Freq is not used by any other station interface */ 9807 continue; 9808 } else if (!p2p_supported_freq(wpa_s->global->p2p, 9809 freqs[i].freq) && !dfs_offload) { 9810 /* Freq is not valid for P2P use cases */ 9811 continue; 9812 } else if (wpa_s->conf->p2p_go_freq_change_policy == 9813 P2P_GO_FREQ_MOVE_SCM) { 9814 policy_move = 1; 9815 } else if (wpa_s->conf->p2p_go_freq_change_policy == 9816 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS && 9817 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) { 9818 policy_move = 1; 9819 } else if ((wpa_s->conf->p2p_go_freq_change_policy == 9820 P2P_GO_FREQ_MOVE_SCM_ECSA) && 9821 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) { 9822 if (!p2p_get_group_num_members(wpa_s->p2p_group)) { 9823 policy_move = 1; 9824 } else if ((wpa_s->drv_flags & 9825 WPA_DRIVER_FLAGS_AP_CSA) && 9826 wpas_p2p_go_clients_support_ecsa(wpa_s)) { 9827 u8 chan; 9828 9829 /* 9830 * We do not support CSA between bands, so move 9831 * GO only within the same band. 9832 */ 9833 if (wpa_s->ap_iface->current_mode->mode == 9834 ieee80211_freq_to_chan(freqs[i].freq, 9835 &chan)) 9836 policy_move = 1; 9837 } 9838 } 9839 } 9840 9841 wpa_dbg(wpa_s, MSG_DEBUG, 9842 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X", 9843 invalid_freq, policy_move, flags); 9844 9845 /* 9846 * The channel is valid, or we are going to have a policy move, so 9847 * cancel timeout. 9848 */ 9849 if (!invalid_freq || policy_move) { 9850 wpa_dbg(wpa_s, MSG_DEBUG, 9851 "P2P: Cancel a GO move from freq=%d MHz", freq); 9852 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL); 9853 9854 if (wpas_p2p_in_progress(wpa_s)) { 9855 wpa_dbg(wpa_s, MSG_DEBUG, 9856 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move"); 9857 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, 9858 wpa_s, NULL); 9859 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0, 9860 wpas_p2p_reconsider_moving_go, 9861 wpa_s, NULL); 9862 return; 9863 } 9864 } 9865 9866 if (!invalid_freq && (!policy_move || flags != 0)) { 9867 wpa_dbg(wpa_s, MSG_DEBUG, 9868 "P2P: Not initiating a GO frequency change"); 9869 return; 9870 } 9871 9872 /* 9873 * Do not consider moving GO if it is in the middle of a CSA. When the 9874 * CSA is finished this flow should be retriggered. 9875 */ 9876 if (hostapd_csa_in_progress(wpa_s->ap_iface)) { 9877 wpa_dbg(wpa_s, MSG_DEBUG, 9878 "P2P: Not initiating a GO frequency change - CSA is in progress"); 9879 return; 9880 } 9881 9882 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq)) 9883 timeout = P2P_GO_FREQ_CHANGE_TIME; 9884 else 9885 timeout = 0; 9886 9887 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs", 9888 freq, timeout); 9889 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL); 9890 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL); 9891 } 9892 9893 9894 static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s, 9895 struct wpa_used_freq_data *freqs, 9896 unsigned int num, 9897 enum wpas_p2p_channel_update_trig trig) 9898 { 9899 struct wpa_supplicant *ifs; 9900 9901 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX, 9902 NULL); 9903 9904 /* 9905 * Travers all the radio interfaces, and for each GO interface, check 9906 * if there is a need to move the GO from the frequency it is using, 9907 * or in case the frequency is valid again, cancel the evacuation flow. 9908 */ 9909 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant, 9910 radio_list) { 9911 if (ifs->current_ssid == NULL || 9912 ifs->current_ssid->mode != WPAS_MODE_P2P_GO) 9913 continue; 9914 9915 /* 9916 * The GO was just started or completed channel switch, no need 9917 * to move it. 9918 */ 9919 if (wpa_s == ifs && 9920 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE || 9921 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) { 9922 wpa_dbg(wpa_s, MSG_DEBUG, 9923 "P2P: GO move - schedule re-consideration"); 9924 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0, 9925 wpas_p2p_reconsider_moving_go, 9926 wpa_s, NULL); 9927 continue; 9928 } 9929 9930 wpas_p2p_consider_moving_one_go(ifs, freqs, num); 9931 } 9932 } 9933 9934 9935 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s) 9936 { 9937 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) 9938 return; 9939 9940 wpas_p2p_update_channel_list(wpa_s, 9941 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE); 9942 } 9943 9944 9945 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s) 9946 { 9947 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) { 9948 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing " 9949 "the management interface is being removed"); 9950 wpas_p2p_deinit_global(wpa_s->global); 9951 } 9952 } 9953 9954 9955 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s) 9956 { 9957 if (wpa_s->ap_iface->bss) 9958 wpa_s->ap_iface->bss[0]->p2p_group = NULL; 9959 wpas_p2p_group_deinit(wpa_s); 9960 } 9961 9962 9963 int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq, 9964 unsigned int period, unsigned int interval, 9965 unsigned int count) 9966 { 9967 struct p2p_data *p2p = wpa_s->global->p2p; 9968 u8 *device_types; 9969 size_t dev_types_len; 9970 struct wpabuf *buf; 9971 int ret; 9972 9973 if (wpa_s->p2p_lo_started) { 9974 wpa_dbg(wpa_s, MSG_DEBUG, 9975 "P2P Listen offload is already started"); 9976 return 0; 9977 } 9978 9979 if (wpa_s->global->p2p == NULL || 9980 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) { 9981 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported"); 9982 return -1; 9983 } 9984 9985 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) { 9986 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u", 9987 freq); 9988 return -1; 9989 } 9990 9991 /* Get device type */ 9992 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) * 9993 WPS_DEV_TYPE_LEN; 9994 device_types = os_malloc(dev_types_len); 9995 if (!device_types) 9996 return -1; 9997 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN); 9998 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type, 9999 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN); 10000 10001 /* Get Probe Response IE(s) */ 10002 buf = p2p_build_probe_resp_template(p2p, freq); 10003 if (!buf) { 10004 os_free(device_types); 10005 return -1; 10006 } 10007 10008 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count, 10009 device_types, dev_types_len, 10010 wpabuf_mhead_u8(buf), wpabuf_len(buf)); 10011 if (ret < 0) 10012 wpa_dbg(wpa_s, MSG_DEBUG, 10013 "P2P: Failed to start P2P listen offload"); 10014 10015 os_free(device_types); 10016 wpabuf_free(buf); 10017 10018 if (ret == 0) { 10019 wpa_s->p2p_lo_started = 1; 10020 10021 /* Stop current P2P listen if any */ 10022 wpas_stop_listen(wpa_s); 10023 } 10024 10025 return ret; 10026 } 10027 10028 10029 int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s) 10030 { 10031 int ret; 10032 10033 if (!wpa_s->p2p_lo_started) 10034 return 0; 10035 10036 ret = wpa_drv_p2p_lo_stop(wpa_s); 10037 if (ret < 0) 10038 wpa_dbg(wpa_s, MSG_DEBUG, 10039 "P2P: Failed to stop P2P listen offload"); 10040 10041 wpa_s->p2p_lo_started = 0; 10042 return ret; 10043 } 10044