blob: 0c88e1734c582b831c6370dbc406797bcb9d95e2 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
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"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000026#include "ap/wpa_auth.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070027#include "eapol_supp/eapol_supp_sm.h"
28#include "rsn_supp/wpa.h"
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000029#include "rsn_supp/pmksa_cache.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "wpa_supplicant_i.h"
31#include "driver_i.h"
32#include "ap.h"
33#include "config_ssid.h"
34#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "notify.h"
36#include "scan.h"
37#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "wps_supplicant.h"
40#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080041#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
43
44/*
45 * How many times to try to scan to find the GO before giving up on join
46 * request.
47 */
48#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
49
Dmitry Shmidt04949592012-07-19 12:16:46 -070050#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
51
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080052/**
53 * Defines time interval in seconds when a GO needs to evacuate a frequency that
54 * it is currently using, but is no longer valid for P2P use cases.
55 */
56#define P2P_GO_FREQ_CHANGE_TIME 5
57
58/**
59 * Defines CSA parameters which are used when GO evacuates the no longer valid
60 * channel (and if the driver supports channel switch).
61 */
62#define P2P_GO_CSA_COUNT 7
63#define P2P_GO_CSA_BLOCK_TX 0
64
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080065#ifndef P2P_MAX_CLIENT_IDLE
66/*
67 * How many seconds to try to reconnect to the GO when connection in P2P client
68 * role has been lost.
69 */
70#define P2P_MAX_CLIENT_IDLE 10
71#endif /* P2P_MAX_CLIENT_IDLE */
72
Dmitry Shmidt04949592012-07-19 12:16:46 -070073#ifndef P2P_MAX_INITIAL_CONN_WAIT
74/*
75 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070076 * WPS provisioning step or after the re-invocation of a persistent group on a
77 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070078 */
79#define P2P_MAX_INITIAL_CONN_WAIT 10
80#endif /* P2P_MAX_INITIAL_CONN_WAIT */
81
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070082#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
83/*
84 * How many seconds to wait for initial 4-way handshake to get completed after
85 * WPS provisioning step on the GO. This controls the extra time the P2P
86 * operation is considered to be in progress (e.g., to delay other scans) after
87 * WPS provisioning has been completed on the GO during group formation.
88 */
89#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
90#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
91
Dmitry Shmidt56052862013-10-04 10:23:25 -070092#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
93/*
94 * How many seconds to wait for initial 4-way handshake to get completed after
95 * re-invocation of a persistent group on the GO when the client is expected
96 * to connect automatically (no user interaction).
97 */
98#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
99#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
100
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700101#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800103/*
104 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
105 * was not possible.
106 */
107#define P2P_RECONSIDER_GO_MOVE_DELAY 30
108
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530109/* Check if frequency is 2GHz */
110#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
111
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700112enum p2p_group_removal_reason {
113 P2P_GROUP_REMOVAL_UNKNOWN,
114 P2P_GROUP_REMOVAL_SILENT,
115 P2P_GROUP_REMOVAL_FORMATION_FAILED,
116 P2P_GROUP_REMOVAL_REQUESTED,
117 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
118 P2P_GROUP_REMOVAL_UNAVAILABLE,
119 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800120 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800121 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
122 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700123};
124
Jouni Malinendc7b7132012-09-14 12:53:47 -0700125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
127static struct wpa_supplicant *
128wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
129 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800130static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
131 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800132static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
133 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000134 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800135 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800136static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
139static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700140 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800141 int auto_join, int freq,
142 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
144static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
145static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
146static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800147static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
148 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800149static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800150static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
151 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800152static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800153static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700154static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700155static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800156static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
157 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700158static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
159 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800160static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
161 struct wpa_used_freq_data *freqs,
162 unsigned int num);
163static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
164static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
165static void
166wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
167 struct wpa_used_freq_data *freqs, unsigned int num,
168 enum wpas_p2p_channel_update_trig trig);
169static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Sunil Ravi036cec52023-03-29 11:35:17 -0700170static int wpas_p2p_disallowed_freq(struct wpa_global *global,
171 unsigned int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172
173
Hai Shalomc1a21442022-02-04 13:43:00 -0800174static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
175{
176 int he_capab = 0;
177
178 if (mode)
179 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
180 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
181 return he_capab;
182}
183
184
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700185/*
186 * Get the number of concurrent channels that the HW can operate, but that are
187 * currently not in use by any of the wpa_supplicant interfaces.
188 */
189static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
190{
191 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800192 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700193
194 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
195 if (!freqs)
196 return -1;
197
198 num = get_shared_radio_freqs(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000199 wpa_s->num_multichan_concurrent, false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 os_free(freqs);
201
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800202 unused = wpa_s->num_multichan_concurrent - num;
203 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
204 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700205}
206
207
208/*
209 * Get the frequencies that are currently in use by one or more of the virtual
210 * interfaces, and that are also valid for P2P operation.
211 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700212static unsigned int
213wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
214 struct wpa_used_freq_data *p2p_freqs,
215 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700216{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700217 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700218 unsigned int num, i, j;
219
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700220 freqs = os_calloc(wpa_s->num_multichan_concurrent,
221 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700222 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700223 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700224
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700225 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000226 wpa_s->num_multichan_concurrent,
227 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700229 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700230
231 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700232 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700233 p2p_freqs[j++] = freqs[i];
234 }
235
236 os_free(freqs);
237
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700238 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800239
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700240 return j;
241}
242
243
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700244static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
245 int freq)
246{
247 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
248 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700249
250 /* Use the wpa_s used to control the P2P Device operation */
251 wpa_s = wpa_s->global->p2p_init_wpa_s;
252
253 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800254 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
255 wpas_p2p_num_unused_channels(wpa_s) > 0) {
256 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
257 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700258 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800259 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700260 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
261}
262
263
Hai Shalom60840252021-02-19 19:02:11 -0800264static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
265{
266 unsigned int delay = wpas_p2p_search_delay(wpa_s);
267
268 /* In case of concurrent P2P and external scans, delay P2P search. */
269 if (external_scan_running(wpa_s->radio)) {
270 delay = wpa_s->conf->p2p_search_delay;
271 wpa_printf(MSG_DEBUG,
272 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
273 delay);
274 }
275
276 p2p_scan_res_handled(wpa_s->global->p2p, delay);
277}
278
279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
281 struct wpa_scan_results *scan_res)
282{
283 size_t i;
284
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800285 if (wpa_s->p2p_scan_work) {
286 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
287 wpa_s->p2p_scan_work = NULL;
288 radio_work_done(work);
289 }
290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
292 return;
293
294 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
295 (int) scan_res->num);
296
297 for (i = 0; i < scan_res->num; i++) {
298 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700300 const u8 *ies;
301 size_t ies_len;
302
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800303 time_tmp_age.sec = bss->age / 1000;
304 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800305 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700306
307 ies = (const u8 *) (bss + 1);
308 ies_len = bss->ie_len;
309 if (bss->beacon_ie_len > 0 &&
310 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
311 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
312 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
313 MACSTR, MAC2STR(bss->bssid));
314 ies = ies + ies_len;
315 ies_len = bss->beacon_ie_len;
316 }
317
318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800320 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700321 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 break;
323 }
324
Hai Shalom60840252021-02-19 19:02:11 -0800325 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326}
327
328
Hai Shalom60840252021-02-19 19:02:11 -0800329static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700330{
Hai Shalom60840252021-02-19 19:02:11 -0800331 if (wpa_s->p2p_scan_work) {
332 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700333
Hai Shalom60840252021-02-19 19:02:11 -0800334 wpa_s->p2p_scan_work = NULL;
335 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700336 }
337
Hai Shalom60840252021-02-19 19:02:11 -0800338 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
339 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700340
Hai Shalom60840252021-02-19 19:02:11 -0800341 wpa_dbg(wpa_s, MSG_DEBUG,
342 "P2P: Failed to get scan results - try to continue");
343 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700344}
345
346
Sunil Ravi77d572f2023-01-17 23:58:31 +0000347void wpas_p2p_scan_freqs(struct wpa_supplicant *wpa_s,
348 struct wpa_driver_scan_params *params,
349 bool include_6ghz)
350{
351 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
352 params, false, false, false);
353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
354 params, false, false, false);
355 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD,
356 params, false, false, false);
357 if (!wpa_s->conf->p2p_6ghz_disable &&
358 is_p2p_allow_6ghz(wpa_s->global->p2p) && include_6ghz)
359 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
360 params, true, true, false);
361}
362
363
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800364static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
365{
366 struct wpa_supplicant *wpa_s = work->wpa_s;
367 struct wpa_driver_scan_params *params = work->ctx;
368 int ret;
369
370 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800371 if (!work->started) {
372 wpa_scan_free_params(params);
373 return;
374 }
375
376 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800377 return;
378 }
379
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800380 if (wpa_s->clear_driver_scan_cache) {
381 wpa_printf(MSG_DEBUG,
382 "Request driver to clear scan cache due to local BSS flush");
383 params->only_new_results = 1;
384 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700385
Sunil Ravi77d572f2023-01-17 23:58:31 +0000386 if (!params->freqs)
387 wpas_p2p_scan_freqs(wpa_s, params, params->p2p_include_6ghz);
388
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800389 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800390 if (ret == 0)
391 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800392 wpa_scan_free_params(params);
393 work->ctx = NULL;
394 if (ret) {
395 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800396 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800397 return;
398 }
399
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800400 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800401 os_get_reltime(&wpa_s->scan_trigger_time);
402 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800403 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800404 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800405 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800406 wpa_s->p2p_scan_work = work;
407}
408
409
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800410static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
411 int freq)
412{
413 if (wpa_s->global->p2p_24ghz_social_channels &&
414 (freq == 2412 || freq == 2437 || freq == 2462)) {
415 /*
416 * Search all social channels regardless of whether these have
417 * been disabled for P2P operating channel use to avoid missing
418 * peers.
419 */
420 return 1;
421 }
422 return p2p_supported_freq(wpa_s->global->p2p, freq);
423}
424
425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
427 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800428 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
429 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430{
431 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800432 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700434 unsigned int num_channels = 0;
435 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800436 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700437 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800438 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439
440 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
441 return -1;
442
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800443 if (wpa_s->p2p_scan_work) {
444 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
445 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700446 }
447
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800448 params = os_zalloc(sizeof(*params));
449 if (params == NULL)
450 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451
452 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800453 params->num_ssids = 1;
454 n = os_malloc(P2P_WILDCARD_SSID_LEN);
455 if (n == NULL)
456 goto fail;
457 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
458 params->ssids[0].ssid = n;
459 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
461 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700462 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
463 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 num_req_dev_types, req_dev_types);
465 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800466 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000467
468 /*
469 * In case 6 GHz channels are requested as part of the P2P scan, only
470 * the PSCs would be included as P2P GOs are not expected to be
471 * collocated, i.e., they would not be announced in the RNR element of
472 * other APs.
473 */
Hai Shalomc1a21442022-02-04 13:43:00 -0800474 if (!wpa_s->conf->p2p_6ghz_disable)
475 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 switch (type) {
477 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700478 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
479 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800480 if (params->freqs == NULL)
481 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700482 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800483 if (wpas_p2p_search_social_channel(
484 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700485 params->freqs[num_channels++] =
486 social_channels_freq[i];
487 }
488 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 break;
490 case P2P_SCAN_FULL:
491 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800492 case P2P_SCAN_SPECIFIC:
493 params->freqs = os_calloc(2, sizeof(int));
494 if (params->freqs == NULL)
495 goto fail;
496 params->freqs[0] = freq;
497 params->freqs[1] = 0;
498 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700499 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700500 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
501 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800502 if (params->freqs == NULL)
503 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700504 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800505 if (wpas_p2p_search_social_channel(
506 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700507 params->freqs[num_channels++] =
508 social_channels_freq[i];
509 }
510 if (p2p_supported_freq(wpa_s->global->p2p, freq))
511 params->freqs[num_channels++] = freq;
512 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 break;
514 }
515
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800516 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
517 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
518 if (ies == NULL) {
519 wpabuf_free(wps_ie);
520 goto fail;
521 }
522 wpabuf_put_buf(ies, wps_ie);
523 wpabuf_free(wps_ie);
524
525 bands = wpas_get_bands(wpa_s, params->freqs);
526 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
527
528 params->p2p_probe = 1;
529 n = os_malloc(wpabuf_len(ies));
530 if (n == NULL) {
531 wpabuf_free(ies);
532 goto fail;
533 }
534 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
535 params->extra_ies = n;
536 params->extra_ies_len = wpabuf_len(ies);
537 wpabuf_free(ies);
538
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800539 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800540 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
541 params) < 0)
542 goto fail;
543 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800545fail:
546 wpa_scan_free_params(params);
547 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548}
549
550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
552{
553 switch (p2p_group_interface) {
554 case P2P_GROUP_INTERFACE_PENDING:
555 return WPA_IF_P2P_GROUP;
556 case P2P_GROUP_INTERFACE_GO:
557 return WPA_IF_P2P_GO;
558 case P2P_GROUP_INTERFACE_CLIENT:
559 return WPA_IF_P2P_CLIENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000560 default:
561 return WPA_IF_P2P_GROUP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563}
564
565
566static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
567 const u8 *ssid,
568 size_t ssid_len, int *go)
569{
570 struct wpa_ssid *s;
571
572 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
573 for (s = wpa_s->conf->ssid; s; s = s->next) {
574 if (s->disabled != 0 || !s->p2p_group ||
575 s->ssid_len != ssid_len ||
576 os_memcmp(ssid, s->ssid, ssid_len) != 0)
577 continue;
578 if (s->mode == WPAS_MODE_P2P_GO &&
579 s != wpa_s->current_ssid)
580 continue;
581 if (go)
582 *go = s->mode == WPAS_MODE_P2P_GO;
583 return wpa_s;
584 }
585 }
586
587 return NULL;
588}
589
590
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800591static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
592{
593 struct wpa_supplicant *wpa_s = eloop_ctx;
594 wpa_printf(MSG_DEBUG,
595 "P2P: Complete previously requested removal of %s",
596 wpa_s->ifname);
597 wpas_p2p_disconnect(wpa_s);
598}
599
600
601static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
602 struct wpa_supplicant *calling_wpa_s)
603{
604 if (calling_wpa_s == wpa_s && wpa_s &&
605 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
606 /*
607 * The calling wpa_s instance is going to be removed. Do that
608 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700609 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800610 * control interface responses on the per-interface socket.
611 */
612 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
613 wpa_s, NULL) < 0)
614 return -1;
615 return 0;
616 }
617
618 return wpas_p2p_disconnect(wpa_s);
619}
620
621
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800622/* Determine total number of clients in active groups where we are the GO */
623static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
624{
625 unsigned int count = 0;
626 struct wpa_ssid *s;
627
628 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
629 for (s = wpa_s->conf->ssid; s; s = s->next) {
630 wpa_printf(MSG_DEBUG,
631 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
632 wpa_s, s, s->disabled, s->p2p_group,
633 s->mode);
634 if (!s->disabled && s->p2p_group &&
635 s->mode == WPAS_MODE_P2P_GO) {
636 count += p2p_get_group_num_members(
637 wpa_s->p2p_group);
638 }
639 }
640 }
641
642 return count;
643}
644
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
647{
648 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
649 !wpa_s->current_ssid->disabled &&
650 wpa_s->current_ssid->p2p_group &&
651 wpa_s->current_ssid->p2p_persistent_group;
652}
653
654
655static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
656{
657 return p2p_is_active_persistent_group(wpa_s) &&
658 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
659}
660
661
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800662/* Find an interface for a P2P group where we are the GO */
663static struct wpa_supplicant *
664wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
665{
666 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800667
668 if (!wpa_s)
669 return NULL;
670
671 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 if (!p2p_is_active_persistent_go(wpa_s))
673 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800674
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800675 /* Prefer a group with connected clients */
676 if (p2p_get_group_num_members(wpa_s->p2p_group))
677 return wpa_s;
678 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800679 }
680
681 /* No group with connected clients, so pick the one without (if any) */
682 return save;
683}
684
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800688 return p2p_is_active_persistent_group(wpa_s) &&
689 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
690}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800691
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800692
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800693/* Find an interface for a P2P group where we are the P2P Client */
694static struct wpa_supplicant *
695wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
696{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800697 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800698 if (p2p_is_active_persistent_cli(wpa_s))
699 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800700 }
701
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800702 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800703}
704
705
706/* Find a persistent group where we are the GO */
707static struct wpa_ssid *
708wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
709{
710 struct wpa_ssid *s;
711
712 for (s = wpa_s->conf->ssid; s; s = s->next) {
713 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
714 return s;
715 }
716
717 return NULL;
718}
719
720
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800721static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
722 unsigned int *force_freq,
723 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800725 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 struct wpa_ssid *s;
727 u8 conncap = P2PS_SETUP_NONE;
728 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800729 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800730 struct wpa_ssid *persistent_go;
731 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000732 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
733 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800734
735 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
736
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800737 if (force_freq)
738 *force_freq = 0;
739 if (pref_freq)
740 *pref_freq = 0;
741
742 size = P2P_MAX_PREF_CHANNELS;
743 if (force_freq && pref_freq &&
744 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
745 (int *) pref_freq, 0, pref_freq_list, &size))
746 wpas_p2p_set_own_freq_preference(wpa_s,
747 *force_freq ? *force_freq :
748 *pref_freq);
749
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800750 /*
751 * For non-concurrent capable devices:
752 * If persistent_go, then no new.
753 * If GO, then no client.
754 * If client, then no GO.
755 */
756 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800757 if (go_wpa_s)
758 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800759 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800760 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
761 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800762
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800763 wpa_printf(MSG_DEBUG,
764 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
765 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800766
767 /* If not concurrent, restrict our choices */
768 if (p2p_no_group_iface) {
769 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
770
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800771 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800772 return P2PS_SETUP_NONE;
773
774 if (go_wpa_s) {
775 if (role == P2PS_SETUP_CLIENT ||
776 incoming == P2PS_SETUP_GROUP_OWNER ||
777 p2p_client_limit_reached(go_wpa_s->p2p_group))
778 return P2PS_SETUP_NONE;
779
780 return P2PS_SETUP_GROUP_OWNER;
781 }
782
783 if (persistent_go) {
784 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
785 if (!incoming)
786 return P2PS_SETUP_GROUP_OWNER |
787 P2PS_SETUP_CLIENT;
788 if (incoming == P2PS_SETUP_NEW) {
789 u8 r;
790
791 if (os_get_random(&r, sizeof(r)) < 0 ||
792 (r & 1))
793 return P2PS_SETUP_CLIENT;
794 return P2PS_SETUP_GROUP_OWNER;
795 }
796 }
797 }
798 }
799
800 /* If a required role has been specified, handle it here */
801 if (role && role != P2PS_SETUP_NEW) {
802 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800803 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
804 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
805 /*
806 * Peer has an active GO, so if the role allows it and
807 * we do not have any active roles, become client.
808 */
809 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
810 !cli_wpa_s)
811 return P2PS_SETUP_CLIENT;
812
813 /* fall through */
814
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 case P2PS_SETUP_NONE:
816 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800817 conncap = role;
818 goto grp_owner;
819
820 case P2PS_SETUP_GROUP_OWNER:
821 /*
822 * Must be a complimentary role - cannot be a client to
823 * more than one peer.
824 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800825 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800826 return P2PS_SETUP_NONE;
827
828 return P2PS_SETUP_CLIENT;
829
830 case P2PS_SETUP_CLIENT:
831 /* Must be a complimentary role */
832 if (incoming != role) {
833 conncap = P2PS_SETUP_GROUP_OWNER;
834 goto grp_owner;
835 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700836 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800837
838 default:
839 return P2PS_SETUP_NONE;
840 }
841 }
842
843 /*
844 * For now, we only will support ownership of one group, and being a
845 * client of one group. Therefore, if we have either an existing GO
846 * group, or an existing client group, we will not do a new GO
847 * negotiation, but rather try to re-use the existing groups.
848 */
849 switch (incoming) {
850 case P2PS_SETUP_NONE:
851 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800852 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800853 conncap = P2PS_SETUP_GROUP_OWNER;
854 else if (!owned_members)
855 conncap = P2PS_SETUP_NEW;
856 else if (incoming == P2PS_SETUP_NONE)
857 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
858 else
859 conncap = P2PS_SETUP_CLIENT;
860 break;
861
862 case P2PS_SETUP_CLIENT:
863 conncap = P2PS_SETUP_GROUP_OWNER;
864 break;
865
866 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800867 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800868 conncap = P2PS_SETUP_CLIENT;
869 break;
870
871 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
872 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800873 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800874 conncap = P2PS_SETUP_GROUP_OWNER;
875 else {
876 u8 r;
877
878 if (os_get_random(&r, sizeof(r)) < 0 ||
879 (r & 1))
880 conncap = P2PS_SETUP_CLIENT;
881 else
882 conncap = P2PS_SETUP_GROUP_OWNER;
883 }
884 break;
885
886 default:
887 return P2PS_SETUP_NONE;
888 }
889
890grp_owner:
891 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
892 (!incoming && (conncap & P2PS_SETUP_NEW))) {
893 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
894 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800895
896 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800897 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800898 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800899 wpa_s->p2p_mgmt ?
900 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800901 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800902 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800903 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800904 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800905 wpa_printf(MSG_ERROR,
906 "P2P: Failed to allocate a new interface for the group");
907 return P2PS_SETUP_NONE;
908 }
909 wpa_s->global->pending_group_iface_for_p2ps = 1;
910 p2p_set_intended_addr(wpa_s->global->p2p,
911 wpa_s->pending_interface_addr);
912 }
913 }
914
915 return conncap;
916}
917
918
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700919static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
920 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921{
922 struct wpa_ssid *ssid;
923 char *gtype;
924 const char *reason;
925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 ssid = wpa_s->current_ssid;
927 if (ssid == NULL) {
928 /*
929 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700930 * pending P2P group interface waiting for provisioning or a
931 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 */
933 ssid = wpa_s->conf->ssid;
934 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700935 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936 break;
937 ssid = ssid->next;
938 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300939 if (ssid == NULL &&
940 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
941 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700942 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
943 "not found");
944 return -1;
945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 }
947 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
948 gtype = "GO";
949 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
950 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
951 wpa_s->reassociate = 0;
952 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 gtype = "client";
954 } else
955 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700956
957 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
958 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
959
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700961 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800962 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
963 wpa_s, NULL)) {
964 wpa_printf(MSG_DEBUG,
965 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
966 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
967 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
968 wpa_s, NULL);
969 }
970 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 if (wpa_s->cross_connect_in_use) {
973 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800974 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700975 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
976 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700978 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 case P2P_GROUP_REMOVAL_REQUESTED:
980 reason = " reason=REQUESTED";
981 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700982 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
983 reason = " reason=FORMATION_FAILED";
984 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
986 reason = " reason=IDLE";
987 break;
988 case P2P_GROUP_REMOVAL_UNAVAILABLE:
989 reason = " reason=UNAVAILABLE";
990 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700991 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
992 reason = " reason=GO_ENDING_SESSION";
993 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700994 case P2P_GROUP_REMOVAL_PSK_FAILURE:
995 reason = " reason=PSK_FAILURE";
996 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800997 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
998 reason = " reason=FREQ_CONFLICT";
999 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 default:
1001 reason = "";
1002 break;
1003 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001004 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001005 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001006 P2P_EVENT_GROUP_REMOVED "%s %s%s",
1007 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001008 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001010 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
1011 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001012 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
1013 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001014 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001015 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001016 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
1017 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001018 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001019 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001020 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001021
Dmitry Shmidt15907092014-03-25 10:42:57 -07001022 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001023 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001024 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1025 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001026
Dmitry Shmidt96571392013-10-14 12:54:46 -07001027 /*
1028 * Make sure wait for the first client does not remain active after the
1029 * group has been removed.
1030 */
1031 wpa_s->global->p2p_go_wait_client.sec = 0;
1032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1034 struct wpa_global *global;
1035 char *ifname;
1036 enum wpa_driver_if_type type;
1037 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1038 wpa_s->ifname);
1039 global = wpa_s->global;
1040 ifname = os_strdup(wpa_s->ifname);
1041 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001042 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001043 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 wpa_s = global->ifaces;
1045 if (wpa_s && ifname)
1046 wpa_drv_if_remove(wpa_s, type, ifname);
1047 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001048 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 }
1050
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001051 /*
1052 * The primary interface was used for P2P group operations, so
1053 * need to reset its p2pdev.
1054 */
1055 wpa_s->p2pdev = wpa_s->parent;
1056
Dmitry Shmidt96571392013-10-14 12:54:46 -07001057 if (!wpa_s->p2p_go_group_formation_completed) {
1058 wpa_s->global->p2p_group_formation = NULL;
1059 wpa_s->p2p_in_provisioning = 0;
1060 }
1061
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001062 wpa_s->show_group_started = 0;
1063 os_free(wpa_s->go_params);
1064 wpa_s->go_params = NULL;
1065
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001066 os_free(wpa_s->p2p_group_common_freqs);
1067 wpa_s->p2p_group_common_freqs = NULL;
1068 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001069 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001070 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001071
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001072 wpa_s->waiting_presence_resp = 0;
1073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1075 if (ssid && (ssid->p2p_group ||
1076 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1077 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1078 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001079 if (ssid == wpa_s->current_ssid) {
1080 wpa_sm_set_config(wpa_s->wpa, NULL);
1081 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001083 }
1084 /*
1085 * Networks objects created during any P2P activities are not
1086 * exposed out as they might/will confuse certain non-P2P aware
1087 * applications since these network objects won't behave like
1088 * regular ones.
1089 *
1090 * Likewise, we don't send out network removed signals for such
1091 * network objects.
1092 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001093 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 wpa_config_remove_network(wpa_s->conf, id);
1095 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001096 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 } else {
1098 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1099 "found");
1100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 if (wpa_s->ap_iface)
1102 wpa_supplicant_ap_deinit(wpa_s);
1103 else
1104 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001105
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001106 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1107
Sunil Ravi99c035e2024-07-12 01:42:03 +00001108 wpa_s->p2p_go_no_pri_sec_switch = 0;
1109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001110 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111}
1112
1113
1114static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1115 u8 *go_dev_addr,
1116 const u8 *ssid, size_t ssid_len)
1117{
1118 struct wpa_bss *bss;
1119 const u8 *bssid;
1120 struct wpabuf *p2p;
1121 u8 group_capab;
1122 const u8 *addr;
1123
1124 if (wpa_s->go_params)
1125 bssid = wpa_s->go_params->peer_interface_addr;
1126 else
1127 bssid = wpa_s->bssid;
1128
1129 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001130 if (bss == NULL && wpa_s->go_params &&
1131 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1132 bss = wpa_bss_get_p2p_dev_addr(
1133 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 if (bss == NULL) {
1135 u8 iface_addr[ETH_ALEN];
1136 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1137 iface_addr) == 0)
1138 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1139 }
1140 if (bss == NULL) {
1141 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1142 "group is persistent - BSS " MACSTR " not found",
1143 MAC2STR(bssid));
1144 return 0;
1145 }
1146
1147 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001148 if (p2p == NULL)
1149 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1150 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 if (p2p == NULL) {
1152 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1153 "group is persistent - BSS " MACSTR
1154 " did not include P2P IE", MAC2STR(bssid));
1155 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001156 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001158 wpa_bss_ie_ptr(bss) + bss->ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 bss->beacon_ie_len);
1160 return 0;
1161 }
1162
1163 group_capab = p2p_get_group_capab(p2p);
1164 addr = p2p_get_go_dev_addr(p2p);
1165 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1166 "group_capab=0x%x", group_capab);
1167 if (addr) {
1168 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1169 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1170 MAC2STR(addr));
1171 } else
1172 os_memset(go_dev_addr, 0, ETH_ALEN);
1173 wpabuf_free(p2p);
1174
1175 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1176 "go_dev_addr=" MACSTR,
1177 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1178
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001179 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180}
1181
1182
Jouni Malinen75ecf522011-06-27 15:19:46 -07001183static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1184 struct wpa_ssid *ssid,
1185 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186{
1187 struct wpa_ssid *s;
1188 int changed = 0;
1189
1190 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1191 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1192 for (s = wpa_s->conf->ssid; s; s = s->next) {
1193 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001194 ether_addr_equal(go_dev_addr, s->bssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 s->ssid_len == ssid->ssid_len &&
1196 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1197 break;
1198 }
1199
1200 if (s) {
1201 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1202 "entry");
1203 if (ssid->passphrase && !s->passphrase)
1204 changed = 1;
1205 else if (ssid->passphrase && s->passphrase &&
1206 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1207 changed = 1;
1208 } else {
1209 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1210 "entry");
1211 changed = 1;
1212 s = wpa_config_add_network(wpa_s->conf);
1213 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001214 return -1;
1215
1216 /*
1217 * Instead of network_added we emit persistent_group_added
1218 * notification. Also to keep the defense checks in
1219 * persistent_group obj registration method, we set the
1220 * relevant flags in s to designate it as a persistent group.
1221 */
1222 s->p2p_group = 1;
1223 s->p2p_persistent_group = 1;
1224 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 wpa_config_set_network_defaults(s);
1226 }
1227
1228 s->p2p_group = 1;
1229 s->p2p_persistent_group = 1;
1230 s->disabled = 2;
1231 s->bssid_set = 1;
1232 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1233 s->mode = ssid->mode;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001234 s->auth_alg = ssid->auth_alg;
1235 s->key_mgmt = ssid->key_mgmt;
1236 s->proto = ssid->proto;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001237 s->pbss = ssid->pbss;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001238 s->pmk_valid = ssid->pmk_valid;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001239 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 s->export_keys = 1;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001241
1242 if (ssid->sae_password) {
1243 os_free(s->sae_password);
1244 s->sae_password = os_strdup(ssid->sae_password);
1245 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 if (ssid->passphrase) {
1247 os_free(s->passphrase);
1248 s->passphrase = os_strdup(ssid->passphrase);
1249 }
1250 if (ssid->psk_set) {
1251 s->psk_set = 1;
1252 os_memcpy(s->psk, ssid->psk, 32);
1253 }
1254 if (s->passphrase && !s->psk_set)
1255 wpa_config_update_psk(s);
1256 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1257 os_free(s->ssid);
1258 s->ssid = os_malloc(ssid->ssid_len);
1259 }
1260 if (s->ssid) {
1261 s->ssid_len = ssid->ssid_len;
1262 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1263 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001264 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1265 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1266 wpa_s->global->add_psk = NULL;
1267 changed = 1;
1268 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 if (changed && wpa_s->conf->update_config &&
1271 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1272 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1273 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001274
1275 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276}
1277
1278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1280 const u8 *addr)
1281{
1282 struct wpa_ssid *ssid, *s;
1283 u8 *n;
1284 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001285 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001286 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001287
1288 ssid = wpa_s->current_ssid;
1289 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1290 !ssid->p2p_persistent_group)
1291 return;
1292
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001293 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001294 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1295 continue;
1296
1297 if (s->ssid_len == ssid->ssid_len &&
1298 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1299 break;
1300 }
1301
1302 if (s == NULL)
1303 return;
1304
1305 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001306 if (!ether_addr_equal(s->p2p_client_list + i * 2 * ETH_ALEN,
1307 addr))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001308 continue;
1309
1310 if (i == s->num_p2p_clients - 1)
1311 return; /* already the most recent entry */
1312
1313 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001314 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1315 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1316 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001317 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001318 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1319 ETH_ALEN);
1320 os_memset(s->p2p_client_list +
1321 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1322 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001323 found = 1;
1324 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001325 }
1326
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001327 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1328 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001329 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001330 if (n == NULL)
1331 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001332 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1333 ETH_ALEN);
1334 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1335 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 s->p2p_client_list = n;
1337 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001338 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001339 /* Not enough room for an additional entry - drop the oldest
1340 * entry */
1341 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001342 s->p2p_client_list + 2 * ETH_ALEN,
1343 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001344 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001345 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001346 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001347 os_memset(s->p2p_client_list +
1348 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1349 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001350 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001351
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001352 if (p2p_wpa_s->conf->update_config &&
1353 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355}
1356
1357
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001358static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1359 int go, struct wpa_ssid *ssid, int freq,
1360 const u8 *psk, const char *passphrase,
1361 const u8 *go_dev_addr, int persistent,
1362 const char *extra)
1363{
1364 const char *ssid_txt;
1365 char psk_txt[65];
1366
1367 if (psk)
1368 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1369 else
1370 psk_txt[0] = '\0';
1371
1372 if (ssid)
1373 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1374 else
1375 ssid_txt = "";
1376
1377 if (passphrase && passphrase[0] == '\0')
1378 passphrase = NULL;
1379
1380 /*
1381 * Include PSK/passphrase only in the control interface message and
1382 * leave it out from the debug log entry.
1383 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001384 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001385 P2P_EVENT_GROUP_STARTED
1386 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1387 MACSTR "%s%s",
1388 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1389 psk ? " psk=" : "", psk_txt,
1390 passphrase ? " passphrase=\"" : "",
1391 passphrase ? passphrase : "",
1392 passphrase ? "\"" : "",
1393 MAC2STR(go_dev_addr),
1394 persistent ? " [PERSISTENT]" : "", extra);
1395 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1396 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1397 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1398 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1399 extra);
1400}
1401
1402
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001403int wpas_p2p_remove_all_identity(struct wpa_supplicant *wpa_s)
1404{
1405 struct wpa_dev_ik *ik;
1406
1407 for (ik = wpa_s->conf->identity; ik; ik = ik->next)
1408 wpa_config_remove_identity(wpa_s->conf, ik->id);
1409
1410 if (wpa_s->conf->update_config &&
1411 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1412 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1413 return -1;
1414 }
1415 return 0;
1416}
1417
1418
1419static void wpas_p2p_store_identity(struct wpa_supplicant *wpa_s, u8 cipher,
1420 const u8 *dik_data, size_t dik_len,
1421 const u8 *pmk, size_t pmk_len,
1422 const u8 *pmkid)
1423{
1424 struct wpa_dev_ik *ik;
1425
1426 for (ik = wpa_s->conf->identity; ik; ik = ik->next) {
1427 if (dik_len == wpabuf_len(ik->dik) &&
1428 os_memcmp(dik_data, wpabuf_head(ik->dik), dik_len) == 0) {
1429 wpa_printf(MSG_DEBUG,
1430 "P2P: Remove previous device identity entry for matching DIK");
1431 wpa_config_remove_identity(wpa_s->conf, ik->id);
1432 break;
1433 }
1434 }
1435
1436 wpa_printf(MSG_DEBUG, "P2P: Create a new device identity entry");
1437 ik = wpa_config_add_identity(wpa_s->conf);
1438 if (!ik)
1439 return;
1440
1441 ik->dik = wpabuf_alloc_copy(dik_data, dik_len);
1442 if (!ik->dik)
1443 goto fail;
1444 ik->pmk = wpabuf_alloc_copy(pmk, pmk_len);
1445 if (!ik->pmk)
1446 goto fail;
1447 ik->pmkid = wpabuf_alloc_copy(pmkid, PMKID_LEN);
1448 if (!ik->pmkid)
1449 goto fail;
1450
1451 ik->dik_cipher = cipher;
1452
1453 if (wpa_s->conf->update_config &&
1454 wpa_config_write(wpa_s->confname, wpa_s->conf))
1455 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1456 return;
1457
1458fail:
1459 wpa_config_remove_identity(wpa_s->conf, ik->id);
1460}
1461
1462
1463static void wpas_p2p_store_go_identity(struct wpa_supplicant *wpa_s,
1464 const u8 *go_dev_addr, const u8 *bssid)
1465{
1466 int ret;
1467 u8 cipher;
1468 const u8 *dik_data, *pmk, *pmkid;
1469 size_t dik_len, pmk_len;
1470 u8 iface_addr[ETH_ALEN];
1471 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
1472
1473 if (!wpa_s->p2p2)
1474 return;
1475
1476 ret = p2p_get_dev_identity_key(p2p_wpa_s->global->p2p, go_dev_addr,
1477 &dik_data, &dik_len, &cipher);
1478 if (ret)
1479 return;
1480
1481 ret = p2p_get_interface_addr(p2p_wpa_s->global->p2p, go_dev_addr,
1482 iface_addr);
1483 if (ret) {
1484 wpa_printf(MSG_DEBUG,
1485 "P2P: Fetch PMK for GO BSSID " MACSTR,
1486 MAC2STR(bssid));
1487 os_memcpy(iface_addr, bssid, ETH_ALEN);
1488 }
1489 ret = wpa_sm_pmksa_get_pmk(wpa_s->wpa, iface_addr, &pmk, &pmk_len,
1490 &pmkid);
1491 if (ret)
1492 return;
1493
1494 wpa_printf(MSG_DEBUG,
1495 "P2P: Storing Device identity of GO (Interface Addr " MACSTR
1496 ")",
1497 MAC2STR(iface_addr));
1498 wpas_p2p_store_identity(p2p_wpa_s, cipher, dik_data, dik_len, pmk,
1499 pmk_len, pmkid);
1500}
1501
1502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001504 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505{
1506 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 int client;
1508 int persistent;
1509 u8 go_dev_addr[ETH_ALEN];
1510
1511 /*
1512 * This callback is likely called for the main interface. Update wpa_s
1513 * to use the group interface if a new interface was created for the
1514 * group.
1515 */
1516 if (wpa_s->global->p2p_group_formation)
1517 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001518 if (wpa_s->p2p_go_group_formation_completed) {
1519 wpa_s->global->p2p_group_formation = NULL;
1520 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001521 } else if (wpa_s->p2p_in_provisioning && !success) {
1522 wpa_msg(wpa_s, MSG_DEBUG,
1523 "P2P: Stop provisioning state due to failure");
1524 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001525 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001526 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001527 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001528 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529
1530 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001531 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001532 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001533 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001534 if (already_deleted)
1535 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001536 wpas_p2p_group_delete(wpa_s,
1537 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 return;
1539 }
1540
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001541 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001542 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543
1544 ssid = wpa_s->current_ssid;
1545 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1546 ssid->mode = WPAS_MODE_P2P_GO;
1547 p2p_group_notif_formation_done(wpa_s->p2p_group);
1548 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1549 }
1550
1551 persistent = 0;
1552 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 client = ssid->mode == WPAS_MODE_INFRA;
1554 if (ssid->mode == WPAS_MODE_P2P_GO) {
1555 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001556 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1557 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001558 } else {
1559 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 persistent = wpas_p2p_persistent_group(wpa_s,
1561 go_dev_addr,
1562 ssid->ssid,
1563 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001564 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 client = wpa_s->p2p_group_interface ==
1567 P2P_GROUP_INTERFACE_CLIENT;
1568 os_memset(go_dev_addr, 0, ETH_ALEN);
1569 }
1570
1571 wpa_s->show_group_started = 0;
1572 if (client) {
1573 /*
1574 * Indicate event only after successfully completed 4-way
1575 * handshake, i.e., when the interface is ready for data
1576 * packets.
1577 */
1578 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001580 wpas_p2p_group_started(wpa_s, 1, ssid,
1581 ssid ? ssid->frequency : 0,
1582 ssid && ssid->passphrase == NULL &&
1583 ssid->psk_set ? ssid->psk : NULL,
1584 ssid ? ssid->passphrase : NULL,
1585 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 wpas_p2p_cross_connect_setup(wpa_s);
1587 wpas_p2p_set_group_idle_timeout(wpa_s);
1588 }
1589
1590 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001591 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1592 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001593 else {
1594 os_free(wpa_s->global->add_psk);
1595 wpa_s->global->add_psk = NULL;
1596 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001597
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001598 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001599 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001600 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001601 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602}
1603
1604
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001605struct send_action_work {
1606 unsigned int freq;
1607 u8 dst[ETH_ALEN];
1608 u8 src[ETH_ALEN];
1609 u8 bssid[ETH_ALEN];
1610 size_t len;
1611 unsigned int wait_time;
1612 u8 buf[0];
1613};
1614
1615
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001616static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1617{
1618 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1619
1620 wpa_printf(MSG_DEBUG,
1621 "P2P: Free Action frame radio work @%p (freq=%u dst="
1622 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1623 wpa_s->p2p_send_action_work, awork->freq,
1624 MAC2STR(awork->dst), MAC2STR(awork->src),
1625 MAC2STR(awork->bssid), awork->wait_time);
1626 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1627 awork->buf, awork->len);
1628 os_free(awork);
1629 wpa_s->p2p_send_action_work->ctx = NULL;
1630 radio_work_done(wpa_s->p2p_send_action_work);
1631 wpa_s->p2p_send_action_work = NULL;
1632}
1633
1634
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001635static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1636 void *timeout_ctx)
1637{
1638 struct wpa_supplicant *wpa_s = eloop_ctx;
1639
1640 if (!wpa_s->p2p_send_action_work)
1641 return;
1642
1643 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001644 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001645}
1646
1647
Dmitry Shmidt03658832014-08-13 11:03:49 -07001648static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001650 if (wpa_s->p2p_send_action_work) {
1651 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001652
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001653 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001654 wpa_printf(MSG_DEBUG,
1655 "P2P: Clear Action TX work @%p (wait_time=%u)",
1656 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001657 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001658 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001659 } else {
1660 /*
1661 * In theory, this should not be needed, but number of
1662 * places in the P2P code is still using non-zero wait
1663 * time for the last Action frame in the sequence and
1664 * some of these do not call send_action_done().
1665 */
1666 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1667 wpa_s, NULL);
1668 eloop_register_timeout(
1669 0, awork->wait_time * 1000,
1670 wpas_p2p_send_action_work_timeout,
1671 wpa_s, NULL);
1672 }
1673 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001674}
1675
1676
1677static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1678 unsigned int freq,
1679 const u8 *dst, const u8 *src,
1680 const u8 *bssid,
1681 const u8 *data, size_t data_len,
1682 enum offchannel_send_action_result
1683 result)
1684{
1685 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1686
1687 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1690 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001692 switch (result) {
1693 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1694 res = P2P_SEND_ACTION_SUCCESS;
1695 break;
1696 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1697 res = P2P_SEND_ACTION_NO_ACK;
1698 break;
1699 case OFFCHANNEL_SEND_ACTION_FAILED:
1700 res = P2P_SEND_ACTION_FAILED;
1701 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 }
1703
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001704 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001706 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1707 wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001708 (ether_addr_equal(dst, wpa_s->pending_join_dev_addr) ||
1709 ether_addr_equal(dst, wpa_s->pending_join_iface_addr)) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001710 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001712 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1713 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001714 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001715 P2P_EVENT_FALLBACK_TO_GO_NEG
1716 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001717 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1718 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 }
1720}
1721
1722
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001723static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1724{
1725 struct wpa_supplicant *wpa_s = work->wpa_s;
1726 struct send_action_work *awork = work->ctx;
1727
1728 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001729 if (work->started) {
1730 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1731 wpa_s, NULL);
1732 wpa_s->p2p_send_action_work = NULL;
1733 offchannel_send_action_done(wpa_s);
1734 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001735 os_free(awork);
1736 return;
1737 }
1738
1739 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1740 awork->bssid, awork->buf, awork->len,
1741 awork->wait_time,
1742 wpas_p2p_send_action_tx_status, 1) < 0) {
1743 os_free(awork);
1744 radio_work_done(work);
1745 return;
1746 }
1747 wpa_s->p2p_send_action_work = work;
1748}
1749
1750
1751static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1752 unsigned int freq, const u8 *dst,
1753 const u8 *src, const u8 *bssid, const u8 *buf,
1754 size_t len, unsigned int wait_time)
1755{
1756 struct send_action_work *awork;
1757
Hai Shalom81f62d82019-07-22 12:10:00 -07001758 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001759 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1760 return -1;
1761 }
1762
1763 awork = os_zalloc(sizeof(*awork) + len);
1764 if (awork == NULL)
1765 return -1;
1766
1767 awork->freq = freq;
1768 os_memcpy(awork->dst, dst, ETH_ALEN);
1769 os_memcpy(awork->src, src, ETH_ALEN);
1770 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1771 awork->len = len;
1772 awork->wait_time = wait_time;
1773 os_memcpy(awork->buf, buf, len);
1774
Hai Shalom81f62d82019-07-22 12:10:00 -07001775 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001776 wpas_send_action_cb, awork) < 0) {
1777 os_free(awork);
1778 return -1;
1779 }
1780
1781 return 0;
1782}
1783
1784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1786 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001787 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788{
1789 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001790 int listen_freq = -1, send_freq = -1;
1791
Hai Shalom021b0b52019-04-10 11:17:58 -07001792 if (scheduled)
1793 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001794 if (wpa_s->p2p_listen_work)
1795 listen_freq = wpa_s->p2p_listen_work->freq;
1796 if (wpa_s->p2p_send_action_work)
1797 send_freq = wpa_s->p2p_send_action_work->freq;
1798 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001799 int res;
1800
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001801 wpa_printf(MSG_DEBUG,
1802 "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u dst="
1803 MACSTR " src=" MACSTR " bssid=" MACSTR,
1804 listen_freq, send_freq, freq, MAC2STR(dst),
1805 MAC2STR(src), MAC2STR(bssid));
Hai Shalom021b0b52019-04-10 11:17:58 -07001806 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1807 len, wait_time);
1808 if (res == 0 && scheduled)
1809 *scheduled = 1;
1810 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001811 }
1812
1813 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001814 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1815 wait_time,
1816 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817}
1818
1819
1820static void wpas_send_action_done(void *ctx)
1821{
1822 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001823
1824 if (wpa_s->p2p_send_action_work) {
1825 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1826 wpa_s, NULL);
1827 os_free(wpa_s->p2p_send_action_work->ctx);
1828 radio_work_done(wpa_s->p2p_send_action_work);
1829 wpa_s->p2p_send_action_work = NULL;
1830 }
1831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833}
1834
1835
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001836#ifdef CONFIG_PASN
1837
1838struct wpa_p2p_pasn_auth_work {
1839 u8 peer_addr[ETH_ALEN];
1840 int freq;
1841 bool verify;
1842 int force_freq;
1843 int pref_freq;
1844 enum p2p_invite_role role;
1845 u8 *ssid;
1846 size_t ssid_len;
1847 u8 bssid[ETH_ALEN];
1848 u8 go_dev_addr[ETH_ALEN];
1849};
1850
1851
1852static void wpas_p2p_pasn_free_auth_work(struct wpa_p2p_pasn_auth_work *awork)
1853{
1854 if (!awork)
1855 return;
1856 os_free(awork->ssid);
1857 os_free(awork);
1858}
1859
1860
1861static void wpas_p2p_pasn_cancel_auth_work(struct wpa_supplicant *wpa_s)
1862{
1863 wpa_printf(MSG_DEBUG, "P2P PASN: Cancel p2p-pasn-start-auth work");
1864
1865 /* Remove pending/started work */
1866 radio_remove_works(wpa_s, "p2p-pasn-start-auth", 0);
1867}
1868
1869
1870static void wpas_p2p_pasn_auth_start_cb(struct wpa_radio_work *work, int deinit)
1871{
1872 int ret;
1873 struct wpa_supplicant *wpa_s = work->wpa_s;
1874 struct wpa_p2p_pasn_auth_work *awork = work->ctx;
1875 struct p2p_data *p2p = wpa_s->global->p2p;
1876 const u8 *peer_addr = NULL;
1877 const u8 *bssid = NULL;
1878 const u8 *go_dev_addr = NULL;
1879
1880 if (deinit) {
1881 if (!work->started) {
1882 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1883 wpa_s->p2pdev, NULL);
1884 }
1885 wpas_p2p_pasn_free_auth_work(awork);
1886 return;
1887 }
1888
1889 if (!is_zero_ether_addr(awork->peer_addr))
1890 peer_addr = awork->peer_addr;
1891 if (!is_zero_ether_addr(awork->bssid))
1892 bssid = awork->bssid;
1893 if (!is_zero_ether_addr(awork->go_dev_addr))
1894 go_dev_addr = awork->go_dev_addr;
1895
1896
1897 if (awork->verify)
1898 ret = p2p_initiate_pasn_verify(p2p, peer_addr, awork->freq,
1899 awork->role, bssid, awork->ssid,
1900 awork->ssid_len,
1901 awork->force_freq, go_dev_addr,
1902 awork->pref_freq);
1903 else
1904 ret = p2p_initiate_pasn_auth(p2p, peer_addr, awork->freq);
1905
1906 if (ret) {
1907 wpa_printf(MSG_DEBUG,
1908 "P2P PASN: Failed to start PASN authentication");
1909 goto fail;
1910 }
1911 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1912 wpa_s->p2pdev, NULL);
1913 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
1914 wpas_p2p_group_formation_timeout,
1915 wpa_s->p2pdev, NULL);
1916 wpa_s->p2p_pasn_auth_work = work;
1917 return;
1918
1919fail:
1920 wpas_p2p_pasn_free_auth_work(awork);
1921 work->ctx = NULL;
1922 radio_work_done(work);
1923}
1924
1925
1926static int wpas_p2p_initiate_pasn_auth(struct wpa_supplicant *wpa_s,
1927 const u8 *peer_addr, int freq)
1928{
1929 struct wpa_p2p_pasn_auth_work *awork;
1930
1931 wpas_p2p_pasn_cancel_auth_work(wpa_s);
1932 wpa_s->p2p_pasn_auth_work = NULL;
1933
1934 awork = os_zalloc(sizeof(*awork));
1935 if (!awork)
1936 return -1;
1937
1938 awork->freq = freq;
1939 os_memcpy(awork->peer_addr, peer_addr, ETH_ALEN);
1940
1941 if (radio_add_work(wpa_s, freq, "p2p-pasn-start-auth", 1,
1942 wpas_p2p_pasn_auth_start_cb, awork) < 0) {
1943 wpas_p2p_pasn_free_auth_work(awork);
1944 return -1;
1945 }
1946
1947 wpa_printf(MSG_DEBUG,
1948 "P2P PASN: Authentication work successfully added");
1949 return 0;
1950}
1951
1952#endif /* CONFIG_PASN */
1953
1954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1956 struct p2p_go_neg_results *params)
1957{
1958 if (wpa_s->go_params == NULL) {
1959 wpa_s->go_params = os_malloc(sizeof(*params));
1960 if (wpa_s->go_params == NULL)
1961 return -1;
1962 }
1963 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1964 return 0;
1965}
1966
1967
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001968static void wpas_start_gc(struct wpa_supplicant *wpa_s,
1969 struct p2p_go_neg_results *res)
1970{
1971 struct os_reltime now;
1972 struct wpa_ssid *ssid;
1973 struct rsn_pmksa_cache_entry *entry;
1974
1975 if (!res->ssid_len) {
1976 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: SSID info not present");
1977 return;
1978 }
1979
1980 wpa_s->group_formation_reported = 0;
1981 wpa_printf(MSG_DEBUG, "P2P: Start connect for peer " MACSTR
1982 " dev_addr " MACSTR,
1983 MAC2STR(res->peer_interface_addr),
1984 MAC2STR(res->peer_device_addr));
1985 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start connect for SSID",
1986 res->ssid, res->ssid_len);
1987 wpa_supplicant_ap_deinit(wpa_s);
1988 wpas_copy_go_neg_results(wpa_s, res);
1989
1990 ssid = wpa_config_add_network(wpa_s->conf);
1991 if (!ssid) {
1992 wpa_dbg(wpa_s, MSG_DEBUG,
1993 "P2P: Could not add network for client");
1994 return;
1995 }
1996 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1997 wpa_config_set_network_defaults(ssid);
1998 ssid->temporary = 1;
1999 ssid->p2p_group = 1;
2000
2001 ssid->ssid = os_memdup(res->ssid, res->ssid_len);
2002 if (!ssid->ssid)
2003 return;
2004 ssid->ssid_len = res->ssid_len;
2005
2006 os_memcpy(ssid->bssid, res->peer_interface_addr, ETH_ALEN);
2007
2008 if (res->akmp == WPA_KEY_MGMT_PASN && res->sae_password[0]) {
2009 ssid->auth_alg = WPA_AUTH_ALG_SAE;
2010 ssid->sae_password = os_strdup(res->sae_password);
2011 if (!ssid->sae_password)
2012 return;
2013 } else if (res->akmp == WPA_KEY_MGMT_SAE && res->pmk_len) {
2014 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2015 entry = os_zalloc(sizeof(*entry));
2016 if (!entry)
2017 return;
2018 os_memcpy(entry->aa, res->peer_interface_addr, ETH_ALEN);
2019 os_memcpy(entry->pmkid, res->pmkid, PMKID_LEN);
2020 entry->pmk_len = res->pmk_len;
2021 os_memcpy(entry->pmk, res->pmk, res->pmk_len);
2022 entry->akmp = res->akmp;
2023 os_get_reltime(&now);
2024 entry->expiration = now.sec + 43200;
2025 entry->reauth_time = now.sec + 43200 * 70 / 100;
2026 entry->network_ctx = ssid;
2027 os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
2028
2029 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
2030 ssid->pmk_valid = true;
2031 } else if (res->akmp == WPA_KEY_MGMT_SAE && res->sae_password[0]) {
2032 ssid->auth_alg = WPA_AUTH_ALG_SAE;
2033 ssid->sae_password = os_strdup(res->sae_password);
2034 if (!ssid->sae_password)
2035 return;
2036 }
2037
2038 if (res->psk_set) {
2039 os_memcpy(ssid->psk, res->psk, 32);
2040 ssid->psk_set = 1;
2041 }
2042 ssid->proto = WPA_PROTO_RSN;
2043 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2044 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
2045 ssid->group_cipher = WPA_CIPHER_CCMP;
2046 if (res->cipher)
2047 ssid->pairwise_cipher |= res->cipher;
2048 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
2049 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2050 ssid->disabled = 0;
2051 wpa_s->show_group_started = 1;
2052 wpa_s->p2p_in_invitation = 1;
2053 wpa_s->p2p_go_group_formation_completed = 0;
2054 wpa_s->global->p2p_group_formation = wpa_s;
2055 ssid->rsn_overriding = RSN_OVERRIDING_ENABLED;
2056
2057 wpa_s->current_ssid = ssid;
2058 wpa_supplicant_update_scan_results(wpa_s, res->peer_interface_addr);
2059 wpa_supplicant_select_network(wpa_s, ssid);
2060}
2061
2062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
2064 struct p2p_go_neg_results *res)
2065{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002066 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002067 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
2068 " dev_addr " MACSTR " wps_method %d",
2069 MAC2STR(res->peer_interface_addr),
2070 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
2072 res->ssid, res->ssid_len);
2073 wpa_supplicant_ap_deinit(wpa_s);
2074 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002075 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002076 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002077#ifdef CONFIG_WPS_NFC
2078 } else if (res->wps_method == WPS_NFC) {
2079 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
2080 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002081 wpa_s->p2pdev->p2p_oob_dev_pw,
2082 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
2083 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002084 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002085 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002086 NULL,
2087 NULL, 0, 0);
2088#endif /* CONFIG_WPS_NFC */
2089 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002091 if (wpa_s->p2p_wps_method == WPS_P2PS)
2092 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
2094 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
2095 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
2096 wpa_s->p2p_pin, 1, dev_pw_id);
2097 }
2098}
2099
2100
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002101static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
2102 struct wpa_ssid *ssid)
2103{
2104 struct wpa_ssid *persistent;
2105 struct psk_list_entry *psk;
2106 struct hostapd_data *hapd;
2107
2108 if (!wpa_s->ap_iface)
2109 return;
2110
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002111 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002112 ssid->ssid_len);
2113 if (persistent == NULL)
2114 return;
2115
2116 hapd = wpa_s->ap_iface->bss[0];
2117
2118 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
2119 list) {
2120 struct hostapd_wpa_psk *hpsk;
2121
2122 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
2123 MACSTR " psk=%d",
2124 MAC2STR(psk->addr), psk->p2p);
2125 hpsk = os_zalloc(sizeof(*hpsk));
2126 if (hpsk == NULL)
2127 break;
2128 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
2129 if (psk->p2p)
2130 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
2131 else
2132 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
2133 hpsk->next = hapd->conf->ssid.wpa_psk;
2134 hapd->conf->ssid.wpa_psk = hpsk;
2135 }
2136}
2137
2138
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002139static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
2140{
Roshan Pius3a1667e2018-07-03 15:17:14 -07002141 char buf[20 + P2P_MAX_CHANNELS * 6];
2142 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002143 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002144 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002145
Roshan Pius3a1667e2018-07-03 15:17:14 -07002146 pos = buf;
2147 end = pos + sizeof(buf);
2148 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2149 res = os_snprintf(pos, end - pos, " %d",
2150 wpa_s->p2p_group_common_freqs[i]);
2151 if (os_snprintf_error(end - pos, res))
2152 break;
2153 pos += res;
2154 }
2155 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002156
Roshan Pius3a1667e2018-07-03 15:17:14 -07002157 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002158}
2159
2160
2161static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
2162 struct p2p_go_neg_results *params)
2163{
2164 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
2165
2166 wpa_s->p2p_group_common_freqs_num = 0;
2167 os_free(wpa_s->p2p_group_common_freqs);
2168 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
2169 if (!wpa_s->p2p_group_common_freqs)
2170 return;
2171
2172 for (i = 0; i < len; i++) {
2173 if (!wpa_s->go_params->freq_list[i])
2174 break;
2175 wpa_s->p2p_group_common_freqs[i] =
2176 wpa_s->go_params->freq_list[i];
2177 }
2178 wpa_s->p2p_group_common_freqs_num = i;
2179}
2180
2181
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002182static void p2p_config_write(struct wpa_supplicant *wpa_s)
2183{
2184#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002185 if (wpa_s->p2pdev->conf->update_config &&
2186 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002187 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2188#endif /* CONFIG_NO_CONFIG_WRITE */
2189}
2190
2191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192static void p2p_go_configured(void *ctx, void *data)
2193{
2194 struct wpa_supplicant *wpa_s = ctx;
2195 struct p2p_go_neg_results *params = data;
2196 struct wpa_ssid *ssid;
2197
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002198 wpa_s->ap_configured_cb = NULL;
2199 wpa_s->ap_configured_cb_ctx = NULL;
2200 wpa_s->ap_configured_cb_data = NULL;
2201 if (!wpa_s->go_params) {
2202 wpa_printf(MSG_ERROR,
2203 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
2204 return;
2205 }
2206
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002207 if (wpa_s->ap_iface && params->p2p2 &&
2208 params->akmp == WPA_KEY_MGMT_SAE) {
2209 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
2210
2211 wpa_auth_pmksa_add_sae(hapd->wpa_auth,
2212 params->peer_device_addr,
2213 params->pmk, params->pmk_len,
2214 params->pmkid, WPA_KEY_MGMT_SAE);
2215 hostapd_add_pmkid(hapd, params->peer_device_addr,
2216 params->pmk, params->pmk_len,
2217 params->pmkid, WPA_KEY_MGMT_SAE);
2218 }
2219
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002220 p2p_go_save_group_common_freqs(wpa_s, params);
2221 p2p_go_dump_common_freqs(wpa_s);
2222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 ssid = wpa_s->current_ssid;
2224 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
2225 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
2226 if (wpa_s->global->p2p_group_formation == wpa_s)
2227 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002228 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
2229 params->passphrase[0] == '\0' ?
2230 params->psk : NULL,
2231 params->passphrase,
2232 wpa_s->global->p2p_dev_addr,
2233 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002234 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002236 if (wpa_s->p2pdev->p2ps_method_config_any) {
2237 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002238 wpa_dbg(wpa_s, MSG_DEBUG,
2239 "P2PS: Setting default PIN for ANY");
2240 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
2241 "12345670", NULL, 0,
2242 0);
2243 } else {
2244 wpa_dbg(wpa_s, MSG_DEBUG,
2245 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002246 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002247 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002248 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002249 "12345670", NULL, 0, 0);
2250 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002251 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002252 }
2253
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002254 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002255 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002256 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002257 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07002258 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002259 wpas_p2p_add_psk_list(wpa_s, ssid);
2260 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002261
2262 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002263 params->persistent_group, 0,
2264 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 wpas_p2p_cross_connect_setup(wpa_s);
2266 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07002267
2268 if (wpa_s->p2p_first_connection_timeout) {
2269 wpa_dbg(wpa_s, MSG_DEBUG,
2270 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
2271 wpa_s->p2p_first_connection_timeout);
2272 wpa_s->p2p_go_group_formation_completed = 0;
2273 wpa_s->global->p2p_group_formation = wpa_s;
2274 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002275 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07002276 eloop_register_timeout(
2277 wpa_s->p2p_first_connection_timeout, 0,
2278 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002279 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07002280 }
2281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282 return;
2283 }
2284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
2286 params->peer_interface_addr)) {
2287 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
2288 "filtering");
2289 return;
2290 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002291
2292 if (params->p2p2) {
2293 wpas_group_formation_completed(wpa_s, 1, 0);
2294 wpa_printf(MSG_DEBUG,
2295 "P2P2: Group formation completed - first connection in progress");
2296 goto out;
2297 }
2298
2299 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002300 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002302 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002303#ifdef CONFIG_WPS_NFC
2304 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002305 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002306 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002307 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002308 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
2309 return;
2310 }
2311 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002312 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
2313 wpa_s->p2pdev->p2p_oob_dev_pw,
2314 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
2315 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002316#endif /* CONFIG_WPS_NFC */
2317 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002319 wpa_s->p2p_pin, NULL, 0, 0);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002320out:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 os_free(wpa_s->go_params);
2322 wpa_s->go_params = NULL;
2323}
2324
2325
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002326/**
2327 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
2328 * @freq: Frequency (MHz) to convert
2329 * @op_class: Buffer for returning operating class
2330 * @op_edmg_channel: Buffer for returning channel number
2331 * Returns: 0 on success, -1 on failure
2332 *
2333 * This can be used to find the highest channel bonding which includes the
2334 * specified frequency.
2335 */
2336static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
2337 unsigned int freq,
2338 u8 *op_class, u8 *op_edmg_channel)
2339{
2340 struct hostapd_hw_modes *hwmode;
2341 struct ieee80211_edmg_config edmg;
2342 unsigned int i;
2343 enum chan_width chanwidth[] = {
2344 CHAN_WIDTH_8640,
2345 CHAN_WIDTH_6480,
2346 CHAN_WIDTH_4320,
2347 };
2348
2349 if (!wpa_s->hw.modes)
2350 return -1;
2351
2352 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002353 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002354 if (!hwmode) {
2355 wpa_printf(MSG_ERROR,
2356 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2357 return -1;
2358 }
2359
2360 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2361 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2362 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2363 op_class,
2364 op_edmg_channel) < 0)
2365 continue;
2366
2367 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2368 if (edmg.channels &&
2369 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2370 wpa_printf(MSG_DEBUG,
2371 "Freq %u to EDMG channel %u at opclass %u",
2372 freq, *op_edmg_channel, *op_class);
2373 return 0;
2374 }
2375 }
2376
2377 return -1;
2378}
2379
2380
2381int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2382 struct p2p_go_neg_results *params)
2383{
2384 u8 op_channel, op_class;
2385 int freq;
2386
2387 /* Try social channel as primary channel frequency */
2388 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2389
2390 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2391 &op_channel) == 0) {
2392 wpa_printf(MSG_DEBUG,
2393 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2394 freq, op_channel, op_class);
2395 params->freq = freq;
2396 return 0;
2397 }
2398
2399 return -1;
2400}
2401
2402
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002403static void wpas_start_go(struct wpa_supplicant *wpa_s,
2404 struct p2p_go_neg_results *params,
2405 int group_formation, enum wpa_p2p_mode p2p_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406{
2407 struct wpa_ssid *ssid;
2408
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002409 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2410 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2411 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2412 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415
2416 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002417 if (ssid == NULL) {
2418 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002422 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002423 wpa_s->p2p_go_group_formation_completed = 0;
2424 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002425 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 wpa_config_set_network_defaults(ssid);
2428 ssid->temporary = 1;
2429 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002430 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2432 WPAS_MODE_P2P_GO;
2433 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002434 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002435 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002436 ssid->max_oper_chwidth = params->max_oper_chwidth;
2437 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002438 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002439 if (params->edmg) {
2440 u8 op_channel, op_class;
2441
2442 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2443 &op_class, &op_channel)) {
2444 ssid->edmg_channel = op_channel;
2445 ssid->enable_edmg = params->edmg;
2446 } else {
2447 wpa_dbg(wpa_s, MSG_DEBUG,
2448 "P2P: Could not match EDMG channel, freq %d, for GO",
2449 params->freq);
2450 }
2451 }
2452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 ssid->ssid = os_zalloc(params->ssid_len + 1);
2454 if (ssid->ssid) {
2455 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2456 ssid->ssid_len = params->ssid_len;
2457 }
2458 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2459 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002460 if (is_6ghz_freq(ssid->frequency) &&
2461 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2462 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2463 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2464 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002465 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002466 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2467 } else {
2468 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470 ssid->proto = WPA_PROTO_RSN;
2471 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002472 ssid->group_cipher = WPA_CIPHER_CCMP;
2473 if (params->freq > 56160) {
2474 /*
2475 * Enable GCMP instead of CCMP as pairwise_cipher and
2476 * group_cipher in 60 GHz.
2477 */
2478 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2479 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002480 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2481 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002482 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002483 if (os_strlen(params->passphrase) > 0) {
2484 ssid->passphrase = os_strdup(params->passphrase);
2485 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002486 wpa_msg_global(wpa_s, MSG_ERROR,
2487 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002488 wpa_config_remove_network(wpa_s->conf, ssid->id);
2489 return;
2490 }
2491 } else
2492 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002493 ssid->psk_set = params->psk_set;
2494 if (ssid->psk_set)
2495 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002496 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002497 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002498 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002500 ssid->p2p_mode = p2p_mode;
2501 if (params->p2p2) {
2502 if (params->akmp == WPA_KEY_MGMT_SAE)
2503 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2504 else
2505 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2506
2507 ssid->key_mgmt = WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PASN;
2508 ssid->sae_password = os_strdup(params->sae_password);
2509 /* In PCC, RSNE indicates PMF to be disabled while RSNOE/RSNO2E
2510 * requires PMF for SAE. */
2511 if (ssid->p2p_mode != WPA_P2P_MODE_WFD_PCC)
2512 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
2513 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
2514 if (params->cipher)
2515 ssid->pairwise_cipher |= params->cipher;
2516 }
2517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 wpa_s->ap_configured_cb = p2p_go_configured;
2519 wpa_s->ap_configured_cb_ctx = wpa_s;
2520 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002521 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002522 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 wpa_s->reassociate = 1;
2524 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2526 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 wpa_supplicant_req_scan(wpa_s, 0, 0);
2528}
2529
2530
2531static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2532 const struct wpa_supplicant *src)
2533{
2534 struct wpa_config *d;
2535 const struct wpa_config *s;
2536
2537 d = dst->conf;
2538 s = src->conf;
2539
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002540#define C(n) \
2541do { \
2542 if (s->n && !d->n) \
2543 d->n = os_strdup(s->n); \
2544} while (0)
2545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 C(device_name);
2547 C(manufacturer);
2548 C(model_name);
2549 C(model_number);
2550 C(serial_number);
2551 C(config_methods);
2552#undef C
2553
2554 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2555 os_memcpy(d->sec_device_type, s->sec_device_type,
2556 sizeof(d->sec_device_type));
2557 d->num_sec_device_types = s->num_sec_device_types;
2558
2559 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002560 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002562 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002563 d->max_num_sta = s->max_num_sta;
2564 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002565 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002566 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002567 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002568 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002569 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002570 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002571 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002572 d->pmf = s->pmf;
2573 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002574 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002575
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002576 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2577 !d->wps_nfc_pw_from_config) {
2578 wpabuf_free(d->wps_nfc_dh_privkey);
2579 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002580 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2581 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2582 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002583 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002584 d->go_interworking = s->go_interworking;
2585 d->go_access_network_type = s->go_access_network_type;
2586 d->go_internet = s->go_internet;
2587 d->go_venue_group = s->go_venue_group;
2588 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002589 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590}
2591
2592
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002593static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2594 char *ifname, size_t len)
2595{
2596 char *ifname_ptr = wpa_s->ifname;
2597
2598 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2599 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2600 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2601 }
2602
2603 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2604 if (os_strlen(ifname) >= IFNAMSIZ &&
2605 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002606 int res;
2607
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002608 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002609 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2610 if (os_snprintf_error(len, res) && len)
2611 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002612 }
2613}
2614
2615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2617 enum wpa_driver_if_type type)
2618{
2619 char ifname[120], force_ifname[120];
2620
2621 if (wpa_s->pending_interface_name[0]) {
2622 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2623 "- skip creation of a new one");
2624 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2625 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2626 "unknown?! ifname='%s'",
2627 wpa_s->pending_interface_name);
2628 return -1;
2629 }
2630 return 0;
2631 }
2632
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002633 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 force_ifname[0] = '\0';
2635
2636 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2637 ifname);
2638 wpa_s->p2p_group_idx++;
2639
2640 wpa_s->pending_interface_type = type;
2641 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2642 wpa_s->pending_interface_addr, NULL) < 0) {
2643 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2644 "interface");
2645 return -1;
2646 }
2647
Jimmy Chen36c21992018-11-29 16:46:43 +08002648 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002649 random_mac_addr(wpa_s->pending_interface_addr);
2650 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2651 " for the group",
2652 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002653 }
2654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655 if (force_ifname[0]) {
2656 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2657 force_ifname);
2658 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2659 sizeof(wpa_s->pending_interface_name));
2660 } else
2661 os_strlcpy(wpa_s->pending_interface_name, ifname,
2662 sizeof(wpa_s->pending_interface_name));
2663 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2664 MACSTR, wpa_s->pending_interface_name,
2665 MAC2STR(wpa_s->pending_interface_addr));
2666
2667 return 0;
2668}
2669
2670
2671static void wpas_p2p_remove_pending_group_interface(
2672 struct wpa_supplicant *wpa_s)
2673{
2674 if (!wpa_s->pending_interface_name[0] ||
2675 is_zero_ether_addr(wpa_s->pending_interface_addr))
2676 return; /* No pending virtual interface */
2677
2678 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2679 wpa_s->pending_interface_name);
2680 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2681 wpa_s->pending_interface_name);
2682 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2683 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002684 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685}
2686
2687
2688static struct wpa_supplicant *
2689wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2690{
2691 struct wpa_interface iface;
2692 struct wpa_supplicant *group_wpa_s;
2693
2694 if (!wpa_s->pending_interface_name[0]) {
2695 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2696 if (!wpas_p2p_create_iface(wpa_s))
2697 return NULL;
2698 /*
2699 * Something has forced us to remove the pending interface; try
2700 * to create a new one and hope for the best that we will get
2701 * the same local address.
2702 */
2703 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2704 WPA_IF_P2P_CLIENT) < 0)
2705 return NULL;
2706 }
2707
2708 os_memset(&iface, 0, sizeof(iface));
2709 iface.ifname = wpa_s->pending_interface_name;
2710 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002711 if (wpa_s->conf->ctrl_interface == NULL &&
2712 wpa_s->parent != wpa_s &&
2713 wpa_s->p2p_mgmt &&
2714 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2715 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2716 else
2717 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002718 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002719 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720 if (group_wpa_s == NULL) {
2721 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2722 "wpa_supplicant interface");
2723 return NULL;
2724 }
2725 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2727 P2P_GROUP_INTERFACE_CLIENT;
2728 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002729 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730
2731 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002732 group_wpa_s->p2p2 = wpa_s->p2p2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733
Jimmy Chen36c21992018-11-29 16:46:43 +08002734 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002735 if (wpa_drv_set_mac_addr(group_wpa_s,
2736 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002737 wpa_msg(group_wpa_s, MSG_INFO,
2738 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002739 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2740 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002741 return NULL;
2742 }
2743
2744 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2745 wpa_msg(group_wpa_s, MSG_INFO,
2746 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002747 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2748 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002749 return NULL;
2750 }
2751
Hai Shalom74f70d42019-02-11 14:42:39 -08002752 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2753 " for the group",
2754 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002755 }
2756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 return group_wpa_s;
2758}
2759
2760
2761static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2762 void *timeout_ctx)
2763{
2764 struct wpa_supplicant *wpa_s = eloop_ctx;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002765
2766#ifdef CONFIG_PASN
2767 if (wpa_s->p2p_pasn_auth_work) {
2768 wpas_p2p_pasn_cancel_auth_work(wpa_s);
2769 wpa_s->p2p_pasn_auth_work = NULL;
2770 }
2771#endif /* CONFIG_PASN */
2772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002774 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002775}
2776
2777
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002778static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2779 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002780{
2781 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002782 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 if (wpa_s->global->p2p)
2784 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002785 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786}
2787
2788
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002789static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2790{
2791 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2792 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002793 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002794 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002795 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002796 wpa_s->global->p2p_fail_on_wps_complete = 0;
2797}
2798
2799
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002800void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2801{
2802 if (wpa_s->global->p2p_group_formation != wpa_s)
2803 return;
2804 /* Speed up group formation timeout since this cannot succeed */
2805 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002806 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002807 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002808 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002809}
2810
2811
Matthew Wang06b42472022-11-10 06:56:31 +00002812bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2813{
2814 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002815 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002816 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002817
Matthew Wang06b42472022-11-10 06:56:31 +00002818 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2819 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2820 wpa_s->p2pdev, NULL);
2821 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2822 wpa_s->p2pdev, NULL);
2823 return true;
2824}
2825
2826
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002827static void wpas_set_go_security_config(void *ctx,
2828 struct p2p_go_neg_results *params)
2829{
2830 struct wpa_supplicant *wpa_s = ctx;
2831 struct wpa_supplicant *tmp, *ifs = NULL;
2832 struct hostapd_data *hapd;
2833
2834 if (!params->p2p2)
2835 return;
2836
2837 dl_list_for_each(tmp, &wpa_s->radio->ifaces, struct wpa_supplicant,
2838 radio_list) {
2839 struct wpa_ssid *ssid = tmp->current_ssid;
2840
2841 if (ssid && ssid->mode == WPAS_MODE_P2P_GO &&
2842 ssid->ssid && ssid->ssid_len == params->ssid_len &&
2843 os_memcmp(ssid->ssid, params->ssid, params->ssid_len) == 0)
2844 {
2845 ifs = tmp;
2846 break;
2847 }
2848 }
2849
2850 if (!ifs || !ifs->ap_iface)
2851 return;
2852
2853 hapd = ifs->ap_iface->bss[0];
2854 hapd->conf->wps_state = 0;
2855
2856 if (params->akmp == WPA_KEY_MGMT_SAE) {
2857 wpa_printf(MSG_DEBUG, "P2P: Adding PMK for peer: " MACSTR,
2858 MAC2STR(params->peer_device_addr));
2859 wpa_auth_pmksa_add_sae(hapd->wpa_auth,
2860 params->peer_device_addr,
2861 params->pmk, params->pmk_len,
2862 params->pmkid, WPA_KEY_MGMT_SAE);
2863 hostapd_add_pmkid(hapd, params->peer_device_addr,
2864 params->pmk, params->pmk_len,
2865 params->pmkid, WPA_KEY_MGMT_SAE);
2866 }
2867}
2868
2869
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002870static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871{
2872 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002873 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874
2875 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2876 wpa_drv_cancel_remain_on_channel(wpa_s);
2877 wpa_s->off_channel_freq = 0;
2878 wpa_s->roc_waiting_drv_freq = 0;
2879 }
2880
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002881#ifdef CONFIG_PASN
2882 if (wpa_s->p2p_pasn_auth_work) {
2883 wpas_p2p_pasn_cancel_auth_work(wpa_s);
2884 wpa_s->p2p_pasn_auth_work = NULL;
2885 }
2886#endif /* CONFIG_PASN */
2887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002889 wpa_msg_global(wpa_s, MSG_INFO,
2890 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2891 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 wpas_p2p_remove_pending_group_interface(wpa_s);
2894 return;
2895 }
2896
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002897 if (!res->role_go) {
2898 /* Inform driver of the operating channel of GO. */
2899 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2900 }
2901
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002902 if (wpa_s->p2p_go_ht40)
2903 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002904 if (wpa_s->p2p_go_vht)
2905 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002906 if (wpa_s->p2p_go_he)
2907 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002908 if (wpa_s->p2p_go_edmg)
2909 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002910 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2911 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002912
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002913 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2914 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2915 " wps_method=%s",
2916 res->role_go ? "GO" : "client", res->freq, res->ht40,
2917 MAC2STR(res->peer_device_addr),
2918 MAC2STR(res->peer_interface_addr),
2919 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002920 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921
Dmitry Shmidt04949592012-07-19 12:16:46 -07002922 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2923 struct wpa_ssid *ssid;
2924 ssid = wpa_config_get_network(wpa_s->conf,
2925 wpa_s->p2p_persistent_id);
2926 if (ssid && ssid->disabled == 2 &&
2927 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2928 size_t len = os_strlen(ssid->passphrase);
2929 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2930 "on requested persistent group");
2931 os_memcpy(res->passphrase, ssid->passphrase, len);
2932 res->passphrase[len] = '\0';
2933 }
2934 }
2935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002937 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2939 if (group_wpa_s == NULL) {
2940 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002941 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2942 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002943 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944 return;
2945 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2947 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002949 group_wpa_s = wpa_s->parent;
2950 wpa_s->global->p2p_group_formation = group_wpa_s;
2951 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002952 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002955 group_wpa_s->p2p_in_provisioning = 1;
2956 group_wpa_s->p2pdev = wpa_s;
2957 if (group_wpa_s != wpa_s) {
2958 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2959 sizeof(group_wpa_s->p2p_pin));
2960 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002961 group_wpa_s->p2p2 = res->p2p2;
2962 group_wpa_s->p2p_bootstrap = wpa_s->p2p_bootstrap;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002963 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002964
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002965 if (res->role_go) {
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002966 wpas_start_go(group_wpa_s, res, 1, group_wpa_s->p2p_mode);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002967 } else {
2968 os_get_reltime(&group_wpa_s->scan_min_time);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00002969 if (res->p2p2)
2970 wpas_start_gc(group_wpa_s, res);
2971 else
2972 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973 }
2974
Hai Shalomfdcde762020-04-02 11:19:20 -07002975 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2977
2978 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2979 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2980 (res->peer_config_timeout % 100) * 10000,
2981 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2982}
2983
2984
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002985static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2986 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987{
2988 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002989 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002990 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2991 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002993 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994}
2995
2996
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002997static void wpas_dev_found(void *ctx, const u8 *addr,
2998 const struct p2p_peer_info *info,
2999 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000{
Roshan Piusfd2fd662017-01-23 13:41:57 -08003001 u8 *wfd_dev_info = NULL;
3002 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08003003 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08003004 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003005#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 struct wpa_supplicant *wpa_s = ctx;
3007 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003008 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08003009 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003010
Irfan Sheriff8367dc92012-09-09 17:08:19 -07003011#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003012 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
3013 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08003014 if (wfd_dev_info_hex) {
3015 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
3016 wfd_dev_info = os_zalloc(wfd_dev_info_len);
3017 // Only used for notification, so not handling error.
3018 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
3019 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08003020
3021 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
3022 WFD_SUBELEM_R2_DEVICE_INFO);
3023 if (wfd_r2_dev_info_hex) {
3024 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
3025 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
3026 // Only used for notification, so not handling error.
3027 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
3028 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07003029#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003030
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003031 if (info->p2ps_instance) {
3032 char str[256];
3033 const u8 *buf = wpabuf_head(info->p2ps_instance);
3034 size_t len = wpabuf_len(info->p2ps_instance);
3035
3036 while (len) {
3037 u32 id;
3038 u16 methods;
3039 u8 str_len;
3040
3041 if (len < 4 + 2 + 1)
3042 break;
3043 id = WPA_GET_LE32(buf);
3044 buf += sizeof(u32);
3045 methods = WPA_GET_BE16(buf);
3046 buf += sizeof(u16);
3047 str_len = *buf++;
3048 if (str_len > len - 4 - 2 - 1)
3049 break;
3050 os_memcpy(str, buf, str_len);
3051 str[str_len] = '\0';
3052 buf += str_len;
3053 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
3054
3055 wpa_msg_global(wpa_s, MSG_INFO,
3056 P2P_EVENT_DEVICE_FOUND MACSTR
3057 " p2p_dev_addr=" MACSTR
3058 " pri_dev_type=%s name='%s'"
3059 " config_methods=0x%x"
3060 " dev_capab=0x%x"
3061 " group_capab=0x%x"
3062 " adv_id=%x asp_svc=%s%s",
3063 MAC2STR(addr),
3064 MAC2STR(info->p2p_device_addr),
3065 wps_dev_type_bin2str(
3066 info->pri_dev_type,
3067 devtype, sizeof(devtype)),
3068 info->device_name, methods,
3069 info->dev_capab, info->group_capab,
3070 id, str,
3071 info->vendor_elems ?
3072 " vendor_elems=1" : "");
3073 }
3074 goto done;
3075 }
3076
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003077 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
3078 " p2p_dev_addr=" MACSTR
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003079 " pri_dev_type=%s name='%s' config_methods=0x%x dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d pcea_cap_info=0x%x bootstrap_methods=0x%x pasn_type=0x%x",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003080 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
3081 wps_dev_type_bin2str(info->pri_dev_type, devtype,
3082 sizeof(devtype)),
3083 info->device_name, info->config_methods,
3084 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003085 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003086 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08003087 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
3088 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003089 info->vendor_elems ? " vendor_elems=1" : "",
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003090 new_device, info->pcea_cap_info,
3091 info->pairing_config.bootstrap_methods,
3092 info->pairing_config.pasn_type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003094done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003095 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08003096 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08003097#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003098
Roshan Piusfd2fd662017-01-23 13:41:57 -08003099 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08003100 wfd_dev_info_len, wfd_r2_dev_info,
3101 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08003102 os_free(wfd_dev_info);
jiangpingpinga96a5e82023-11-16 15:02:17 +08003103 os_free(wfd_r2_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104}
3105
3106
3107static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
3108{
3109 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003110
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003111 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
3112 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
3115}
3116
3117
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003118static void wpas_find_stopped(void *ctx)
3119{
3120 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003121
3122 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
3123 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
3124
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003125 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003126 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003127}
3128
3129
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003130struct wpas_p2p_listen_work {
3131 unsigned int freq;
3132 unsigned int duration;
3133 struct wpabuf *probe_resp_ie;
3134};
3135
3136
3137static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
3138{
3139 if (lwork == NULL)
3140 return;
3141 wpabuf_free(lwork->probe_resp_ie);
3142 os_free(lwork);
3143}
3144
3145
3146static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
3147{
3148 struct wpas_p2p_listen_work *lwork;
3149
3150 if (!wpa_s->p2p_listen_work)
3151 return;
3152
3153 lwork = wpa_s->p2p_listen_work->ctx;
3154 wpas_p2p_listen_work_free(lwork);
3155 radio_work_done(wpa_s->p2p_listen_work);
3156 wpa_s->p2p_listen_work = NULL;
3157}
3158
3159
3160static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
3161{
3162 struct wpa_supplicant *wpa_s = work->wpa_s;
3163 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003164 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003165
3166 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003167 if (work->started) {
3168 wpa_s->p2p_listen_work = NULL;
3169 wpas_stop_listen(wpa_s);
3170 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003171 wpas_p2p_listen_work_free(lwork);
3172 return;
3173 }
3174
3175 wpa_s->p2p_listen_work = work;
3176
3177 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
3178
3179 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
3180 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
3181 "report received Probe Request frames");
3182 wpas_p2p_listen_work_done(wpa_s);
3183 return;
3184 }
3185
3186 wpa_s->pending_listen_freq = lwork->freq;
3187 wpa_s->pending_listen_duration = lwork->duration;
3188
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003189 duration = lwork->duration;
3190#ifdef CONFIG_TESTING_OPTIONS
3191 if (wpa_s->extra_roc_dur) {
3192 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
3193 duration, duration + wpa_s->extra_roc_dur);
3194 duration += wpa_s->extra_roc_dur;
3195 }
3196#endif /* CONFIG_TESTING_OPTIONS */
3197
3198 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003199 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
3200 "to remain on channel (%u MHz) for Listen "
3201 "state", lwork->freq);
3202 wpas_p2p_listen_work_done(wpa_s);
3203 wpa_s->pending_listen_freq = 0;
3204 return;
3205 }
3206 wpa_s->off_channel_freq = 0;
3207 wpa_s->roc_waiting_drv_freq = lwork->freq;
3208}
3209
3210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211static int wpas_start_listen(void *ctx, unsigned int freq,
3212 unsigned int duration,
3213 const struct wpabuf *probe_resp_ie)
3214{
3215 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003216 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003218 if (wpa_s->p2p_listen_work) {
3219 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 return -1;
3221 }
3222
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003223 lwork = os_zalloc(sizeof(*lwork));
3224 if (lwork == NULL)
3225 return -1;
3226 lwork->freq = freq;
3227 lwork->duration = duration;
3228 if (probe_resp_ie) {
3229 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
3230 if (lwork->probe_resp_ie == NULL) {
3231 wpas_p2p_listen_work_free(lwork);
3232 return -1;
3233 }
3234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003236 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
3237 lwork) < 0) {
3238 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 return -1;
3240 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241
3242 return 0;
3243}
3244
3245
3246static void wpas_stop_listen(void *ctx)
3247{
3248 struct wpa_supplicant *wpa_s = ctx;
3249 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3250 wpa_drv_cancel_remain_on_channel(wpa_s);
3251 wpa_s->off_channel_freq = 0;
3252 wpa_s->roc_waiting_drv_freq = 0;
3253 }
3254 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003255
3256 /*
3257 * Don't cancel Probe Request RX reporting for a connected P2P Client
3258 * handling Probe Request frames.
3259 */
3260 if (!wpa_s->p2p_cli_probe)
3261 wpa_drv_probe_req_report(wpa_s, 0);
3262
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003263 wpas_p2p_listen_work_done(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003264
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003265 if (!wpa_s->p2p_removing_listen_work &&
3266 radio_work_pending(wpa_s, "p2p-listen")) {
3267 wpa_s->p2p_removing_listen_work = true;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003268 wpa_printf(MSG_DEBUG,
3269 "P2P: p2p-listen is still pending - remove it");
3270 radio_remove_works(wpa_s, "p2p-listen", 0);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003271 wpa_s->p2p_removing_listen_work = false;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003272 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003273}
3274
3275
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003276static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
3277 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278{
3279 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003280 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07003281 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282}
3283
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3286 const u8 *peer, const char *params,
3287 unsigned int generated_pin)
3288{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003289 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3290 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291}
3292
3293
3294static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3295 const u8 *peer, const char *params)
3296{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003297 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3298 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299}
3300
3301
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003302static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3303 const u8 *dev_addr, const u8 *pri_dev_type,
3304 const char *dev_name, u16 supp_config_methods,
3305 u8 dev_capab, u8 group_capab, const u8 *group_id,
3306 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307{
3308 struct wpa_supplicant *wpa_s = ctx;
3309 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 u8 empty_dev_type[8];
3312 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003313 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003314 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003315
3316 if (group_id) {
3317 for (group = wpa_s->global->ifaces; group; group = group->next)
3318 {
3319 struct wpa_ssid *s = group->current_ssid;
3320 if (s != NULL &&
3321 s->mode == WPAS_MODE_P2P_GO &&
3322 group_id_len - ETH_ALEN == s->ssid_len &&
3323 os_memcmp(group_id + ETH_ALEN, s->ssid,
3324 s->ssid_len) == 0)
3325 break;
3326 }
3327 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003328
3329 if (pri_dev_type == NULL) {
3330 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3331 pri_dev_type = empty_dev_type;
3332 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003333 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3334 " pri_dev_type=%s name='%s' config_methods=0x%x "
3335 "dev_capab=0x%x group_capab=0x%x%s%s",
3336 MAC2STR(dev_addr),
3337 wps_dev_type_bin2str(pri_dev_type, devtype,
3338 sizeof(devtype)),
3339 dev_name, supp_config_methods, dev_capab, group_capab,
3340 group ? " group=" : "",
3341 group ? group->ifname : "");
3342 if (os_snprintf_error(sizeof(params), res))
3343 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003347 if (wps_generate_pin(&generated_pin) < 0) {
3348 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
3349 wpas_notify_p2p_provision_discovery(
3350 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003351 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003352 return;
3353 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 wpas_prov_disc_local_display(wpa_s, peer, params,
3355 generated_pin);
3356 } else if (config_methods & WPS_CONFIG_KEYPAD)
3357 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3358 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003359 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3360 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003361
3362 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3363 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003364 config_methods, generated_pin,
3365 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366}
3367
3368
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003369static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370{
3371 struct wpa_supplicant *wpa_s = ctx;
3372 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003373 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003375 if (wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003376 (ether_addr_equal(peer, wpa_s->pending_join_dev_addr) ||
3377 ether_addr_equal(peer, wpa_s->pending_join_iface_addr))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003378 wpa_s->pending_pd_before_join = 0;
3379 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3380 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003381 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003382 return;
3383 }
3384
Dmitry Shmidt04949592012-07-19 12:16:46 -07003385 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003386 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3387 int res;
3388
3389 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3390 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3391 if (os_snprintf_error(sizeof(params), res))
3392 params[sizeof(params) - 1] = '\0';
3393 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07003394 params[0] = '\0';
3395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003397 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003399 if (wps_generate_pin(&generated_pin) < 0) {
3400 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
3401 wpas_notify_p2p_provision_discovery(
3402 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003403 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003404 return;
3405 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 wpas_prov_disc_local_display(wpa_s, peer, params,
3407 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003409 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3410 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411
Jouni Malinen75ecf522011-06-27 15:19:46 -07003412 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3413 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003414 config_methods, generated_pin,
3415 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416}
3417
3418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003419static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003420 enum p2p_prov_disc_status status,
3421 u32 adv_id, const u8 *adv_mac,
3422 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003423{
3424 struct wpa_supplicant *wpa_s = ctx;
3425
Dmitry Shmidt04949592012-07-19 12:16:46 -07003426 if (wpa_s->p2p_fallback_to_go_neg) {
3427 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3428 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003429 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003430 P2P_EVENT_FALLBACK_TO_GO_NEG
3431 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003432 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3433 return;
3434 }
3435
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003436 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003437 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003438 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3439 "join-existing-group operation (no ACK for PD "
3440 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003441 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003442 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003443 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003444
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003445 if (adv_id && adv_mac && deferred_session_resp) {
3446 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3447 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3448 " deferred_session_resp='%s'",
3449 MAC2STR(peer), status, adv_id,
3450 deferred_session_resp);
3451 } else if (adv_id && adv_mac) {
3452 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3453 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3454 MAC2STR(peer), status, adv_id);
3455 } else {
3456 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3457 " p2p_dev_addr=" MACSTR " status=%d",
3458 MAC2STR(peer), status);
3459 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003460
Jouni Malinen75ecf522011-06-27 15:19:46 -07003461 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003462 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003463}
3464
3465
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003466static int freq_included(struct wpa_supplicant *wpa_s,
3467 const struct p2p_channels *channels,
3468 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003469{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003470 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3471 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3472 return 1;
3473 return 0;
3474}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003475
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003476
3477static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3478{
3479 unsigned int num = P2P_MAX_CHANNELS;
3480 int *common_freqs;
3481 int ret;
3482
3483 p2p_go_dump_common_freqs(wpa_s);
3484 common_freqs = os_calloc(num, sizeof(int));
3485 if (!common_freqs)
3486 return;
3487
3488 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3489 if (ret < 0) {
3490 wpa_dbg(wpa_s, MSG_DEBUG,
3491 "P2P: Failed to get group common freqs");
3492 os_free(common_freqs);
3493 return;
3494 }
3495
3496 os_free(wpa_s->p2p_group_common_freqs);
3497 wpa_s->p2p_group_common_freqs = common_freqs;
3498 wpa_s->p2p_group_common_freqs_num = num;
3499 p2p_go_dump_common_freqs(wpa_s);
3500}
3501
3502
3503/*
3504 * Check if the given frequency is one of the possible operating frequencies
3505 * set after the completion of the GO Negotiation.
3506 */
3507static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3508{
3509 unsigned int i;
3510
3511 p2p_go_dump_common_freqs(wpa_s);
3512
3513 /* assume no restrictions */
3514 if (!wpa_s->p2p_group_common_freqs_num)
3515 return 1;
3516
3517 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3518 if (wpa_s->p2p_group_common_freqs[i] == freq)
3519 return 1;
3520 }
3521 return 0;
3522}
3523
3524
3525static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3526 struct sta_info *sta, void *ctx)
3527{
3528 int *ecsa_support = ctx;
3529
3530 *ecsa_support &= sta->ecsa_supported;
3531
3532 return 0;
3533}
3534
3535
3536/* Check if all the peers support eCSA */
3537static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3538{
3539 int ecsa_support = 1;
3540
3541 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3542 &ecsa_support);
3543
3544 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003545}
3546
3547
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003548/**
3549 * Pick the best frequency to use from all the currently used frequencies.
3550 */
3551static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3552 struct wpa_used_freq_data *freqs,
3553 unsigned int num)
3554{
3555 unsigned int i, c;
3556
3557 /* find a candidate freq that is supported by P2P */
3558 for (c = 0; c < num; c++)
3559 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3560 break;
3561
3562 if (c == num)
3563 return 0;
3564
3565 /* once we have a candidate, try to find a 'better' one */
3566 for (i = c + 1; i < num; i++) {
3567 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3568 continue;
3569
3570 /*
3571 * 1. Infrastructure station interfaces have higher preference.
3572 * 2. P2P Clients have higher preference.
3573 * 3. All others.
3574 */
3575 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3576 c = i;
3577 break;
3578 }
3579
3580 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3581 c = i;
3582 }
3583 return freqs[c].freq;
3584}
3585
3586
Sunil Ravi036cec52023-03-29 11:35:17 -07003587/**
3588 * Pick the best frequency the driver suggests.
3589 *
3590 * num_pref_freq is used as both input and output
3591 * - input: the max size of pref_freq_list,
3592 * - output: the valid size of pref_freq_list filled with data.
3593 */
3594static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3595 struct weighted_pcl *pref_freq_list,
3596 unsigned int *num_pref_freq)
3597{
3598 int best_freq = 0;
3599 unsigned int max_pref_freq, i;
3600 int res;
3601 enum wpa_driver_if_type iface_type;
3602
3603 max_pref_freq = *num_pref_freq;
3604 *num_pref_freq = 0;
3605
3606 if (go)
3607 iface_type = WPA_IF_P2P_GO;
3608 else
3609 iface_type = WPA_IF_P2P_CLIENT;
3610
3611 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3612 pref_freq_list);
3613 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3614 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3615 max_pref_freq);
3616 if (res || !max_pref_freq) {
3617 wpa_printf(MSG_DEBUG,
3618 "P2P: No preferred frequency list available");
3619 return 0;
3620 }
3621
3622 *num_pref_freq = max_pref_freq;
3623 i = 0;
3624 while (i < *num_pref_freq &&
3625 (!p2p_supported_freq(wpa_s->global->p2p,
3626 pref_freq_list[i].freq) ||
3627 wpas_p2p_disallowed_freq(wpa_s->global,
3628 pref_freq_list[i].freq) ||
3629 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3630 wpa_printf(MSG_DEBUG,
3631 "P2P: preferred_freq_list[%d]=%d is disallowed",
3632 i, pref_freq_list[i].freq);
3633 i++;
3634 }
3635 if (i != *num_pref_freq) {
3636 best_freq = pref_freq_list[i].freq;
3637 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3638 i, best_freq);
3639 } else {
3640 wpa_printf(MSG_DEBUG,
3641 "P2P: All driver preferred frequencies are disallowed for P2P use");
3642 *num_pref_freq = 0;
3643 }
3644
3645 return best_freq;
3646}
3647
3648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3650 const u8 *go_dev_addr, const u8 *ssid,
3651 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003652 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003653 const struct p2p_channels *channels,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003654 int dev_pw_id, bool p2p2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655{
3656 struct wpa_supplicant *wpa_s = ctx;
3657 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003658 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003660 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003661 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3662 unsigned int num_pref_freq;
3663 int res;
3664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665
3666 if (!persistent_group) {
3667 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003668 " to join an active group (SSID: %s)",
3669 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003671 (ether_addr_equal(go_dev_addr, wpa_s->p2p_auth_invite) ||
3672 ether_addr_equal(sa, wpa_s->p2p_auth_invite))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3674 "authorized invitation");
3675 goto accept_inv;
3676 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003677
3678#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003679 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3680 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003681 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003682 wpa_s->p2p_wps_method = WPS_NFC;
3683 wpa_s->pending_join_wps_method = WPS_NFC;
3684 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003685 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003686 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003687 bssid, ETH_ALEN);
3688 goto accept_inv;
3689 }
3690#endif /* CONFIG_WPS_NFC */
3691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 /*
3693 * Do not accept the invitation automatically; notify user and
3694 * request approval.
3695 */
3696 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3697 }
3698
3699 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3700 if (grp) {
3701 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3702 "running persistent group");
3703 if (*go)
3704 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3705 goto accept_inv;
3706 }
3707
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003708 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003709 ether_addr_equal(sa, wpa_s->p2p_auth_invite)) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003710 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3711 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003712 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003713 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003714 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3715
3716 for (s = wpa_s->conf->ssid; s; s = s->next) {
3717 if (s->disabled == 2 &&
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003718 (p2p2 || ether_addr_equal(s->bssid, go_dev_addr)) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 s->ssid_len == ssid_len &&
3720 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3721 break;
3722 }
3723
3724 if (!s) {
3725 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3726 " requested reinvocation of an unknown group",
3727 MAC2STR(sa));
3728 return P2P_SC_FAIL_UNKNOWN_GROUP;
3729 }
3730
3731 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3732 *go = 1;
3733 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3734 wpa_printf(MSG_DEBUG, "P2P: The only available "
3735 "interface is already in use - reject "
3736 "invitation");
3737 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3738 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003739 if (wpa_s->p2p_mgmt)
3740 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3741 ETH_ALEN);
3742 else
3743 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 } else if (s->mode == WPAS_MODE_P2P_GO) {
3745 *go = 1;
3746 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3747 {
3748 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3749 "interface address for the group");
3750 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3751 }
3752 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3753 ETH_ALEN);
3754 }
3755
3756accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003757 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3758
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003759 best_freq = 0;
3760 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3761 sizeof(struct wpa_used_freq_data));
3762 if (freqs) {
3763 int num_channels = wpa_s->num_multichan_concurrent;
3764 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3765 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3766 os_free(freqs);
3767 }
3768
Sunil Ravi036cec52023-03-29 11:35:17 -07003769 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3770 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3771 &num_pref_freq);
3772 if (res > 0)
3773 best_freq = res;
3774
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003775 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003776 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003777 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003778 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003779
3780 if (wpa_s->num_multichan_concurrent < 2 ||
3781 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3782 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003783 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003784 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 }
3786
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003787 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3788 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003789 if (*go == 0) {
3790 /* We are the client */
3791 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3792 "running a GO but we are capable of MCC, "
3793 "figure out the best channel to use");
3794 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003795 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003796 /* We are the GO, and *force_freq is not in the
3797 * intersection */
3798 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3799 "in intersection but we are capable of MCC, "
3800 "figure out the best channel to use",
3801 *force_freq);
3802 *force_freq = 0;
3803 }
3804 }
3805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 return P2P_SC_SUCCESS;
3807}
3808
3809
3810static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3811 const u8 *ssid, size_t ssid_len,
3812 const u8 *go_dev_addr, u8 status,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003813 int op_freq, const u8 *pmkid,
3814 const u8 *pmk, size_t pmk_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003815{
3816 struct wpa_supplicant *wpa_s = ctx;
3817 struct wpa_ssid *s;
3818
3819 for (s = wpa_s->conf->ssid; s; s = s->next) {
3820 if (s->disabled == 2 &&
3821 s->ssid_len == ssid_len &&
3822 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3823 break;
3824 }
3825
3826 if (status == P2P_SC_SUCCESS) {
3827 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003828 " was accepted; op_freq=%d MHz, SSID=%s",
3829 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003831 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003832 if (go) {
3833 wpa_msg_global(wpa_s, MSG_INFO,
3834 P2P_EVENT_INVITATION_ACCEPTED
3835 "sa=" MACSTR
3836 " persistent=%d freq=%d",
3837 MAC2STR(sa), s->id, op_freq);
3838 } else {
3839 wpa_msg_global(wpa_s, MSG_INFO,
3840 P2P_EVENT_INVITATION_ACCEPTED
3841 "sa=" MACSTR
3842 " persistent=%d",
3843 MAC2STR(sa), s->id);
3844 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003845 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003846 wpa_s, s, go, 0, op_freq, 0,
3847 wpa_s->conf->p2p_go_ht40,
3848 wpa_s->conf->p2p_go_vht,
3849 0,
3850 wpa_s->conf->p2p_go_he,
3851 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003852 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003853 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003854 bssid, sa, pmkid, pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003855 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003856 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003857 wpa_msg_global(wpa_s, MSG_INFO,
3858 P2P_EVENT_INVITATION_ACCEPTED
3859 "sa=" MACSTR " go_dev_addr=" MACSTR
3860 " bssid=" MACSTR " unknown-network",
3861 MAC2STR(sa), MAC2STR(go_dev_addr),
3862 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003864 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003865 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 }
3867 return;
3868 }
3869
3870 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3871 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3872 " was rejected (status %u)", MAC2STR(sa), status);
3873 return;
3874 }
3875
3876 if (!s) {
3877 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003878 wpa_msg_global(wpa_s, MSG_INFO,
3879 P2P_EVENT_INVITATION_RECEIVED
3880 "sa=" MACSTR " go_dev_addr=" MACSTR
3881 " bssid=" MACSTR " unknown-network",
3882 MAC2STR(sa), MAC2STR(go_dev_addr),
3883 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003885 wpa_msg_global(wpa_s, MSG_INFO,
3886 P2P_EVENT_INVITATION_RECEIVED
3887 "sa=" MACSTR " go_dev_addr=" MACSTR
3888 " unknown-network",
3889 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003890 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003891 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3892 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893 return;
3894 }
3895
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003896 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003897 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3898 "sa=" MACSTR " persistent=%d freq=%d",
3899 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003900 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003901 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3902 "sa=" MACSTR " persistent=%d",
3903 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003904 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003905 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3906 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907}
3908
3909
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003910static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3911 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003912 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003913{
3914 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003915 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003916
3917 if (ssid == NULL)
3918 return;
3919
3920 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003921 if (ether_addr_equal(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3922 peer))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003923 break;
3924 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003925 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003926 if (ssid->mode != WPAS_MODE_P2P_GO &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003927 ether_addr_equal(ssid->bssid, peer)) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003928 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3929 "due to invitation result", ssid->id);
3930 wpas_notify_network_removed(wpa_s, ssid);
3931 wpa_config_remove_network(wpa_s->conf, ssid->id);
3932 return;
3933 }
3934 return; /* Peer not found in client list */
3935 }
3936
3937 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003938 "group %d client list%s",
3939 MAC2STR(peer), ssid->id,
3940 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003941 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3942 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3943 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003944 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003945 if (p2p_wpa_s->conf->update_config &&
3946 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003947 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003948}
3949
3950
3951static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3952 const u8 *peer)
3953{
3954 struct wpa_ssid *ssid;
3955
3956 wpa_s = wpa_s->global->p2p_invite_group;
3957 if (wpa_s == NULL)
3958 return; /* No known invitation group */
3959 ssid = wpa_s->current_ssid;
3960 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3961 !ssid->p2p_persistent_group)
3962 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003963 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003964 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003965 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003966}
3967
3968
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003969static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003970 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003971 const u8 *peer, int neg_freq,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003972 int peer_oper_freq, const u8 *pmkid,
3973 const u8 *pmk, size_t pmk_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003974{
3975 struct wpa_supplicant *wpa_s = ctx;
3976 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003977 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00003979#ifdef CONFIG_PASN
3980 if (wpa_s->p2p_pasn_auth_work) {
3981 wpas_p2p_pasn_cancel_auth_work(wpa_s);
3982 wpa_s->p2p_pasn_auth_work = NULL;
3983 }
3984#endif /* CONFIG_PASN */
3985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003987 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3988 "status=%d " MACSTR,
3989 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003991 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3992 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 }
3994 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3995
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003996 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3997 status, MAC2STR(peer));
3998 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003999 struct wpa_supplicant *group_if =
4000 wpa_s->global->p2p_invite_group;
4001
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004002 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
4003 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004004
4005 /*
4006 * Invitation to an active group. If this is successful and we
4007 * are the GO, set the client wait to postpone some concurrent
4008 * operations and to allow provisioning and connection to happen
4009 * more quickly.
4010 */
4011 if (status == P2P_SC_SUCCESS &&
4012 group_if && group_if->current_ssid &&
4013 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
4014 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
4015#ifdef CONFIG_TESTING_OPTIONS
4016 if (group_if->p2p_go_csa_on_inv) {
4017 wpa_printf(MSG_DEBUG,
4018 "Testing: force P2P GO CSA after invitation");
4019 eloop_cancel_timeout(
4020 wpas_p2p_reconsider_moving_go,
4021 wpa_s, NULL);
4022 eloop_register_timeout(
4023 0, 50000,
4024 wpas_p2p_reconsider_moving_go,
4025 wpa_s, NULL);
4026 }
4027#endif /* CONFIG_TESTING_OPTIONS */
4028 }
4029 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004032 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4033 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
4034 "invitation exchange to indicate readiness for "
4035 "re-invocation");
4036 }
4037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004039 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
4040 ssid = wpa_config_get_network(
4041 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004042 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 wpas_p2p_remove_pending_group_interface(wpa_s);
4045 return;
4046 }
4047
4048 ssid = wpa_config_get_network(wpa_s->conf,
4049 wpa_s->pending_invite_ssid_id);
4050 if (ssid == NULL) {
4051 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
4052 "data matching with invitation");
4053 return;
4054 }
4055
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07004056 /*
4057 * The peer could have missed our ctrl::ack frame for Invitation
4058 * Response and continue retransmitting the frame. To reduce the
4059 * likelihood of the peer not getting successful TX status for the
4060 * Invitation Response frame, wait a short time here before starting
4061 * the persistent group so that we will remain on the current channel to
4062 * acknowledge any possible retransmission from the peer.
4063 */
4064 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
4065 "starting persistent group");
4066 os_sleep(0, 50000);
4067
Dmitry Shmidt15907092014-03-25 10:42:57 -07004068 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004069 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07004070 freq = neg_freq;
4071 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004072 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07004073 freq = peer_oper_freq;
4074 else
4075 freq = 0;
4076
4077 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4078 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004080 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004081 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004082 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004083 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004084 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004085 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08004086 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004087 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004088 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004089 ssid->mode == WPAS_MODE_P2P_GO ?
4090 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08004091 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00004092 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00004093 bssid, peer, pmkid, pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094}
4095
4096
Dmitry Shmidt04949592012-07-19 12:16:46 -07004097static int wpas_p2p_disallowed_freq(struct wpa_global *global,
4098 unsigned int freq)
4099{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004100 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
4101 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004102 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004103}
4104
4105
4106static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
4107{
4108 reg->channel[reg->channels] = chan;
4109 reg->channels++;
4110}
4111
4112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004114 struct p2p_channels *chan,
4115 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116{
4117 int i, cla = 0;
4118
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004119 wpa_s->global->p2p_24ghz_social_channels = 1;
4120
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004121 os_memset(cli_chan, 0, sizeof(*cli_chan));
4122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
4124 "band");
4125
4126 /* Operating class 81 - 2.4 GHz band channels 1..13 */
4127 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004128 chan->reg_class[cla].channels = 0;
4129 for (i = 0; i < 11; i++) {
4130 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
4131 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
4132 }
4133 if (chan->reg_class[cla].channels)
4134 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135
4136 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
4137 "band");
4138
4139 /* Operating class 115 - 5 GHz, channels 36-48 */
4140 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004141 chan->reg_class[cla].channels = 0;
4142 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
4143 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
4144 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
4145 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
4146 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
4147 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
4148 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
4149 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
4150 if (chan->reg_class[cla].channels)
4151 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004152
4153 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
4154 "band");
4155
4156 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4157 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004158 chan->reg_class[cla].channels = 0;
4159 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
4160 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
4161 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
4162 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
4163 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
4164 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
4165 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
4166 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
4167 if (chan->reg_class[cla].channels)
4168 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169
4170 chan->reg_classes = cla;
4171 return 0;
4172}
4173
4174
Hai Shalomc1a21442022-02-04 13:43:00 -08004175static enum chan_allowed has_channel(struct wpa_global *global,
4176 struct hostapd_hw_modes *mode, u8 op_class,
4177 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178{
4179 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 unsigned int freq;
4181
Hai Shalomc1a21442022-02-04 13:43:00 -08004182 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004184 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185
4186 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004187 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188 if (flags)
4189 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08004190 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004191 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004192 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
4193 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08004194 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
4195 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004196 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 }
4198 }
4199
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004200 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201}
4202
4203
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004204static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
4205 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08004206 u8 channel, const u8 *center_channels,
4207 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004208{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004209 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004210
4211 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4212 return 0;
4213
Hai Shalomc1a21442022-02-04 13:43:00 -08004214 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004215 /*
4216 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4217 * so the center channel is 6 channels away from the start/end.
4218 */
4219 if (channel >= center_channels[i] - 6 &&
4220 channel <= center_channels[i] + 6)
4221 return center_channels[i];
4222
4223 return 0;
4224}
4225
4226
Hai Shalomc1a21442022-02-04 13:43:00 -08004227static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
4228 155, 171 };
4229static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
4230 119, 135, 151, 167, 183, 199,
4231 215 };
4232
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004233static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
4234 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08004235 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004236{
4237 u8 center_chan;
4238 int i, flags;
4239 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004240 const u8 *chans;
4241 size_t num_chans;
4242 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004243
Hai Shalomc1a21442022-02-04 13:43:00 -08004244 if (is_6ghz) {
4245 chans = center_channels_6ghz_80mhz;
4246 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4247 } else {
4248 chans = center_channels_5ghz_80mhz;
4249 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4250 }
4251 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4252 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004253 if (!center_chan)
4254 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004255 if (!wpa_s->p2p_go_allow_dfs &&
4256 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004257 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
4258
4259 /* check all the channels are available */
4260 for (i = 0; i < 4; i++) {
4261 int adj_chan = center_chan - 6 + i * 4;
4262
Hai Shalomc1a21442022-02-04 13:43:00 -08004263 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
4264 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004265 if (res == NOT_ALLOWED)
4266 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004267 if (res == RADAR)
4268 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004269 if (res == NO_IR)
4270 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08004271 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07004272 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08004273 return NOT_ALLOWED;
4274 } else if (is_6ghz &&
4275 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
4276 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004277 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004278 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004279 }
4280
4281 return ret;
4282}
4283
4284
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004285static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
4286 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08004287 u8 channel, const u8 *center_channels,
4288 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004289{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004290 unsigned int i;
4291
4292 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4293 return 0;
4294
Hai Shalomc1a21442022-02-04 13:43:00 -08004295 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004296 /*
4297 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
4298 * so the center channel is 14 channels away from the start/end.
4299 */
4300 if (channel >= center_channels[i] - 14 &&
4301 channel <= center_channels[i] + 14)
4302 return center_channels[i];
4303
4304 return 0;
4305}
4306
4307
Hai Shalomc1a21442022-02-04 13:43:00 -08004308static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
4309static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
4310 207 };
4311
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004312static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
4313 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08004314 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004315{
4316 u8 center_chan;
4317 int i, flags;
4318 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004319 const u8 *chans;
4320 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004321
Hai Shalomc1a21442022-02-04 13:43:00 -08004322 if (is_6ghz_op_class(op_class)) {
4323 chans = center_channels_6ghz_160mhz;
4324 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4325 } else {
4326 chans = center_channels_5ghz_160mhz;
4327 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4328 }
4329 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4330 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004331 if (!center_chan)
4332 return NOT_ALLOWED;
4333 /* VHT 160 MHz uses DFS channels in most countries. */
4334
4335 /* Check all the channels are available */
4336 for (i = 0; i < 8; i++) {
4337 int adj_chan = center_chan - 14 + i * 4;
4338
Hai Shalomc1a21442022-02-04 13:43:00 -08004339 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
4340 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004341 if (res == NOT_ALLOWED)
4342 return NOT_ALLOWED;
4343
Hai Shalomc1a21442022-02-04 13:43:00 -08004344 if (res == RADAR)
4345 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004346 if (res == NO_IR)
4347 ret = NO_IR;
4348
Hai Shalomc1a21442022-02-04 13:43:00 -08004349 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07004350 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
4351 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08004352 return NOT_ALLOWED;
4353 } else if (is_6ghz_op_class(op_class) &&
4354 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
4355 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004356 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004357 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004358 }
4359
4360 return ret;
4361}
4362
4363
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004364static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
4365 struct hostapd_hw_modes *mode,
4366 u8 channel)
4367{
4368 struct ieee80211_edmg_config edmg;
4369
4370 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
4371 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
4372 return ALLOWED;
4373
4374 return NOT_ALLOWED;
4375}
4376
4377
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004378static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
4379 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08004380 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004381{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004382 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004383 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004384
Sunil Ravi89eba102022-09-13 21:04:37 -07004385 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
4386 p2p_channel_to_freq(op_class, channel)))
4387 return NOT_ALLOWED;
4388
Hai Shalomc1a21442022-02-04 13:43:00 -08004389 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004390 if (bw == BW40MINUS) {
4391 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
4392 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004393 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
4394 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004395 } else if (bw == BW40PLUS) {
4396 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
4397 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08004398 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
4399 NULL);
4400 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
4401 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4402 return NOT_ALLOWED;
4403 if (get_6ghz_sec_channel(channel) < 0)
4404 res2 = has_channel(wpa_s->global, mode, op_class,
4405 channel - 4, NULL);
4406 else
4407 res2 = has_channel(wpa_s->global, mode, op_class,
4408 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004409 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004410 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
4411 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004412 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004413 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
4414 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004415 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
4416 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004417 }
4418
4419 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
4420 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004421 if (res == NO_IR || res2 == NO_IR)
4422 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08004423 if (res == RADAR || res2 == RADAR)
4424 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004425 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004426}
4427
4428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004429static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004430 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08004431 struct p2p_channels *cli_chan,
4432 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004434 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004435 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004437 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
4439 "of all supported channels; assume dualband "
4440 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004441 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 }
4443
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004444 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004446 for (op = 0; global_op_class[op].op_class; op++) {
4447 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08004448 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004449 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07004450 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08004451 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452
Shuibing Dai64a8a892023-03-08 10:26:22 -08004453 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004454 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004455 continue;
4456
Hai Shalomfdcde762020-04-02 11:19:20 -07004457 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4458 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459 if (mode == NULL)
4460 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004461 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4462 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004463 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004464 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004465
4466 /* Check for non-continuous jump in channel index
4467 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004468 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004469 ch < 149 && ch + o->inc > 149)
4470 ch = 149;
4471
Hai Shalomc1a21442022-02-04 13:43:00 -08004472 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4473 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004474 if (res == ALLOWED) {
4475 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004476 if (cla == P2P_MAX_REG_CLASSES)
4477 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004478 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4479 o->op_class);
4480 reg = &chan->reg_class[cla];
4481 cla++;
4482 reg->reg_class = o->op_class;
4483 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004484 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4485 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004486 reg->channel[reg->channels] = ch;
4487 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004488 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004489 wpa_s->conf->p2p_add_cli_chan) {
4490 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004491 if (cli_cla == P2P_MAX_REG_CLASSES)
4492 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004493 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4494 o->op_class);
4495 cli_reg = &cli_chan->reg_class[cli_cla];
4496 cli_cla++;
4497 cli_reg->reg_class = o->op_class;
4498 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004499 if (cli_reg->channels ==
4500 P2P_MAX_REG_CLASS_CHANNELS)
4501 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004502 cli_reg->channel[cli_reg->channels] = ch;
4503 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 }
4506 if (reg) {
4507 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4508 reg->channel, reg->channels);
4509 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004510 if (cli_reg) {
4511 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4512 cli_reg->channel, cli_reg->channels);
4513 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514 }
4515
4516 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004517 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 return 0;
4520}
4521
4522
Hai Shalomc1a21442022-02-04 13:43:00 -08004523int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4524 struct hostapd_hw_modes *mode,
4525 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004526{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004527 int op;
4528 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004529
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004530 for (op = 0; global_op_class[op].op_class; op++) {
4531 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004532 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004533
Hai Shalomc1a21442022-02-04 13:43:00 -08004534 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4535 * driver offloaded DFS. */
4536 if ((o->p2p == NO_P2P_SUPP &&
4537 (!is_dfs_global_op_class(o->op_class) ||
4538 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004539 (is_6ghz_op_class(o->op_class) &&
4540 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004541 continue;
4542
Sunil Ravi77d572f2023-01-17 23:58:31 +00004543 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4544 * set up the BSS with a primary 20 MHz channel that coincides
4545 * with a preferred scanning channel (PSC)."
4546 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4547 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4548 * match the PSC.
4549 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004550 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4551 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004552 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004553
Sunil Ravi77d572f2023-01-17 23:58:31 +00004554 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004555 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004556 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4557 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004558 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004559 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004560 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4561 ch, o->bw);
4562 if (ret == ALLOWED) {
4563 if (is_6ghz_op_class(o->op_class) &&
4564 o->bw == BW40)
4565 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004566 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004567 }
4568 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4569 /* Allow RADAR channels used for driver
4570 * offloaded DFS */
4571 return (o->bw == BW40MINUS) ? -1 : 1;
4572 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004573 }
4574 }
4575 return 0;
4576}
4577
4578
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004579int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004580 struct hostapd_hw_modes *mode, u8 channel,
4581 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004582{
Hai Shalomc1a21442022-02-04 13:43:00 -08004583 const u8 *chans;
4584 size_t num_chans;
4585 enum chan_allowed ret;
4586
4587 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4588 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004589 return 0;
4590
Hai Shalomc1a21442022-02-04 13:43:00 -08004591 if (is_6ghz_op_class(op_class)) {
4592 chans = center_channels_6ghz_80mhz;
4593 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4594 } else {
4595 chans = center_channels_5ghz_80mhz;
4596 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4597 }
4598 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4599 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004600}
4601
4602
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004603int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004604 struct hostapd_hw_modes *mode, u8 channel,
4605 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004606{
Hai Shalomc1a21442022-02-04 13:43:00 -08004607 const u8 *chans;
4608 size_t num_chans;
4609 enum chan_allowed ret;
4610
4611 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4612 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004613 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004614 if (is_6ghz_op_class(op_class)) {
4615 chans = center_channels_6ghz_160mhz;
4616 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4617 } else {
4618 chans = center_channels_5ghz_160mhz;
4619 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4620 }
4621 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4622 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004623}
4624
4625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4627 size_t buf_len)
4628{
4629 struct wpa_supplicant *wpa_s = ctx;
4630
4631 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004632 if (ether_addr_equal(wpa_s->own_addr, interface_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 break;
4634 }
4635 if (wpa_s == NULL)
4636 return -1;
4637
4638 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4639}
4640
4641
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004642struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4643 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004644{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004645 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4646 struct wpa_ssid *s = wpa_s->current_ssid;
4647 if (s == NULL)
4648 continue;
4649 if (s->mode != WPAS_MODE_P2P_GO &&
4650 s->mode != WPAS_MODE_AP &&
4651 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4652 continue;
4653 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004654 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004655 continue;
4656 return wpa_s;
4657 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004658
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004659 return NULL;
4660
4661}
4662
4663
4664struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4665 const u8 *peer_dev_addr)
4666{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004667 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4668 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004669 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004670 continue;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004671 if (ether_addr_equal(wpa_s->go_dev_addr, peer_dev_addr))
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004672 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004673 }
4674
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004675 return NULL;
4676}
4677
4678
4679static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4680{
4681 struct wpa_supplicant *wpa_s = ctx;
4682
4683 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004684}
4685
4686
Dmitry Shmidt18463232014-01-24 12:29:41 -08004687static int wpas_is_concurrent_session_active(void *ctx)
4688{
4689 struct wpa_supplicant *wpa_s = ctx;
4690 struct wpa_supplicant *ifs;
4691
4692 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4693 if (ifs == wpa_s)
4694 continue;
4695 if (ifs->wpa_state > WPA_ASSOCIATED)
4696 return 1;
4697 }
4698 return 0;
4699}
4700
4701
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004702static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4703{
4704 struct wpa_supplicant *wpa_s = ctx;
4705 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4706}
4707
4708
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004709int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4710 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004711{
4712 struct wpa_interface iface;
4713 struct wpa_supplicant *p2pdev_wpa_s;
4714 char ifname[100];
4715 char force_name[100];
4716 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004717 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004719 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4720 wpa_s->ifname);
4721 if (os_snprintf_error(sizeof(ifname), ret))
4722 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004723 /* Cut length at the maximum size. Note that we don't need to ensure
4724 * collision free names here as the created interface is not a netdev.
4725 */
4726 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004727 force_name[0] = '\0';
4728 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004729
4730 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4731 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4732 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4733
4734 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004735 force_name, wpa_s->pending_interface_addr, NULL);
4736 if (ret < 0) {
4737 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4738 return ret;
4739 }
4740 os_strlcpy(wpa_s->pending_interface_name, ifname,
4741 sizeof(wpa_s->pending_interface_name));
4742
4743 os_memset(&iface, 0, sizeof(iface));
4744 iface.p2p_mgmt = 1;
4745 iface.ifname = wpa_s->pending_interface_name;
4746 iface.driver = wpa_s->driver->name;
4747 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004748
4749 /*
4750 * If a P2P Device configuration file was given, use it as the interface
4751 * configuration file (instead of using parent's configuration file.
4752 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004753 if (conf_p2p_dev) {
4754 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004755 iface.ctrl_interface = NULL;
4756 } else {
4757 iface.confname = wpa_s->confname;
4758 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4759 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004760
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004761 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004762 if (!p2pdev_wpa_s) {
4763 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4764 return -1;
4765 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004766
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004767 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004768 wpa_s->pending_interface_name[0] = '\0';
4769 return 0;
4770}
4771
4772
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004773static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4774 const u8 *noa, size_t noa_len)
4775{
4776 struct wpa_supplicant *wpa_s, *intf = ctx;
4777 char hex[100];
4778
4779 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4780 if (wpa_s->waiting_presence_resp)
4781 break;
4782 }
4783 if (!wpa_s) {
4784 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4785 return;
4786 }
4787 wpa_s->waiting_presence_resp = 0;
4788
4789 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4790 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4791 " status=%u noa=%s", MAC2STR(src), status, hex);
4792}
4793
4794
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004795static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4796 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004797 u8 *ret_ssid, size_t *ret_ssid_len,
4798 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004799{
4800 struct wpa_supplicant *wpa_s = ctx;
4801 struct wpa_ssid *s;
4802
4803 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4804 if (s) {
4805 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4806 *ret_ssid_len = s->ssid_len;
4807 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004808
4809 if (s->mode != WPAS_MODE_P2P_GO) {
4810 os_memset(intended_iface_addr, 0, ETH_ALEN);
4811 } else if (wpas_p2p_create_iface(wpa_s)) {
4812 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4813 return 0;
4814
4815 os_memcpy(intended_iface_addr,
4816 wpa_s->pending_interface_addr, ETH_ALEN);
4817 } else {
4818 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4819 ETH_ALEN);
4820 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004821 return 1;
4822 }
4823
4824 return 0;
4825}
4826
4827
4828static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004829 u8 *ssid, size_t *ssid_len, int *group_iface,
4830 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004831{
4832 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004833 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004834 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004835
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004836 /*
4837 * group_iface will be set to 1 only if a dedicated interface for P2P
4838 * role is required. First, we try to reuse an active GO. However,
4839 * if it is not present, we will try to reactivate an existing
4840 * persistent group and set group_iface to 1, so the caller will know
4841 * that the pending interface should be used.
4842 */
4843 *group_iface = 0;
4844
4845 if (freq)
4846 *freq = 0;
4847
4848 go = wpas_p2p_get_go_group(wpa_s);
4849 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004850 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004851 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004852 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004853 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4854 else
4855 return 0;
4856 } else {
4857 s = go->current_ssid;
4858 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4859 if (freq)
4860 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004861 }
4862
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004863 os_memcpy(ssid, s->ssid, s->ssid_len);
4864 *ssid_len = s->ssid_len;
4865
4866 return 1;
4867}
4868
4869
4870static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4871 const u8 *ssid, size_t ssid_len)
4872{
4873 struct wpa_supplicant *wpa_s = ctx;
4874 struct wpa_ssid *s;
4875 int save_config = 0;
4876 size_t i;
4877
4878 /* Start with our first choice of Persistent Groups */
4879 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4880 if (go && ssid && ssid_len &&
4881 s->ssid_len == ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004882 ether_addr_equal(go, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004883 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4884 break;
4885
4886 /* Remove stale persistent group */
4887 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004888 wpa_dbg(wpa_s, MSG_DEBUG,
4889 "P2P: Remove stale persistent group id=%d",
4890 s->id);
4891 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004892 wpa_config_remove_network(wpa_s->conf, s->id);
4893 save_config = 1;
4894 continue;
4895 }
4896
4897 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004898 if (!ether_addr_equal(s->p2p_client_list +
4899 i * 2 * ETH_ALEN, peer))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004900 continue;
4901
4902 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4903 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4904 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4905 break;
4906 }
4907 s->num_p2p_clients--;
4908 save_config = 1;
4909 }
4910
4911 if (save_config)
4912 p2p_config_write(wpa_s);
4913
4914 /* Return TRUE if valid SSID remains */
4915 return s != NULL;
4916}
4917
4918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004919static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4920 const u8 *feat_cap, size_t feat_cap_len)
4921{
4922 static const char pref[] = " feature_cap=";
4923 int ret;
4924
4925 buf[0] = '\0';
4926
4927 /*
4928 * We expect a feature capability to contain at least one byte to be
4929 * reported. The string buffer provided by the caller function is
4930 * expected to be big enough to contain all bytes of the attribute for
4931 * known specifications. This function truncates the reported bytes if
4932 * the feature capability data exceeds the string buffer size.
4933 */
4934 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4935 return;
4936
4937 os_memcpy(buf, pref, sizeof(pref));
4938 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4939 buf_len - sizeof(pref) + 1,
4940 feat_cap, feat_cap_len);
4941
4942 if (ret != (2 * (int) feat_cap_len))
4943 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4944}
4945
4946
Sunil Ravic0f5d412024-09-11 22:12:49 +00004947static void wpas_p2ps_prov_complete(void *ctx, enum p2p_status_code status,
4948 const u8 *dev,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004949 const u8 *adv_mac, const u8 *ses_mac,
4950 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4951 u8 conncap, int passwd_id,
4952 const u8 *persist_ssid,
4953 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004954 int prov_start, const char *session_info,
4955 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004956 unsigned int freq,
4957 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004958{
4959 struct wpa_supplicant *wpa_s = ctx;
4960 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004961 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004962 int save_config = 0;
4963 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004964 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004965
4966 if (!dev)
4967 return;
4968
4969 os_memset(mac, 0, ETH_ALEN);
4970 if (!adv_mac)
4971 adv_mac = mac;
4972 if (!ses_mac)
4973 ses_mac = mac;
4974 if (!grp_mac)
4975 grp_mac = mac;
4976
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004977 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4978 feat_cap, feat_cap_len);
4979
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004980 if (prov_start) {
4981 if (session_info == NULL) {
4982 wpa_msg_global(wpa_s, MSG_INFO,
4983 P2P_EVENT_P2PS_PROVISION_START MACSTR
4984 " adv_id=%x conncap=%x"
4985 " adv_mac=" MACSTR
4986 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004987 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004988 MAC2STR(dev), adv_id, conncap,
4989 MAC2STR(adv_mac),
4990 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004991 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004992 } else {
4993 wpa_msg_global(wpa_s, MSG_INFO,
4994 P2P_EVENT_P2PS_PROVISION_START MACSTR
4995 " adv_id=%x conncap=%x"
4996 " adv_mac=" MACSTR
4997 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004998 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004999 MAC2STR(dev), adv_id, conncap,
5000 MAC2STR(adv_mac),
5001 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005002 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005003 }
5004 return;
5005 }
5006
5007 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
5008 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5009
5010 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
5011 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5012 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5013
5014 if (persistent_go && !persistent_go->num_p2p_clients) {
5015 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08005016 wpa_dbg(wpa_s, MSG_DEBUG,
5017 "P2P: Remove empty persistent group id=%d",
5018 persistent_go->id);
5019 wpas_notify_persistent_group_removed(wpa_s,
5020 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005021 wpa_config_remove_network(wpa_s->conf,
5022 persistent_go->id);
5023 }
5024
5025 wpa_msg_global(wpa_s, MSG_INFO,
5026 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5027 " status=%d"
5028 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005029 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005030 MAC2STR(dev), status,
5031 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005032 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005033 return;
5034 }
5035
5036 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005037 if (persist_ssid && persist_ssid_size)
5038 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
5039 persist_ssid_size);
5040
5041 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
5042 is_zero_ether_addr(grp_mac)) {
5043 wpa_dbg(wpa_s, MSG_ERROR,
5044 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
5045 return;
5046 }
5047
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005048 for (;;) {
5049 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
5050 if (!stale)
5051 break;
5052
5053 if (s && s->ssid_len == stale->ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005054 ether_addr_equal(stale->bssid, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005055 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
5056 break;
5057
5058 /* Remove stale persistent group */
5059 if (stale->mode != WPAS_MODE_P2P_GO ||
5060 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08005061 wpa_dbg(wpa_s, MSG_DEBUG,
5062 "P2P: Remove stale persistent group id=%d",
5063 stale->id);
5064 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005065 wpa_config_remove_network(wpa_s->conf, stale->id);
5066 } else {
5067 size_t i;
5068
5069 for (i = 0; i < stale->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005070 if (ether_addr_equal(stale->p2p_client_list +
5071 i * ETH_ALEN, dev)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005072 os_memmove(stale->p2p_client_list +
5073 i * ETH_ALEN,
5074 stale->p2p_client_list +
5075 (i + 1) * ETH_ALEN,
5076 (stale->num_p2p_clients -
5077 i - 1) * ETH_ALEN);
5078 break;
5079 }
5080 }
5081 stale->num_p2p_clients--;
5082 }
5083 save_config = 1;
5084 }
5085
5086 if (save_config)
5087 p2p_config_write(wpa_s);
5088
5089 if (s) {
5090 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5091 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5092
5093 if (persistent_go && s != persistent_go &&
5094 !persistent_go->num_p2p_clients) {
5095 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08005096 wpa_dbg(wpa_s, MSG_DEBUG,
5097 "P2P: Remove empty persistent group id=%d",
5098 persistent_go->id);
5099 wpas_notify_persistent_group_removed(wpa_s,
5100 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005101 wpa_config_remove_network(wpa_s->conf,
5102 persistent_go->id);
5103 /* Save config */
5104 }
5105
5106 wpa_msg_global(wpa_s, MSG_INFO,
5107 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5108 " status=%d"
5109 " adv_id=%x adv_mac=" MACSTR
5110 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005111 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005112 MAC2STR(dev), status,
5113 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005114 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005115 return;
5116 }
5117
Hai Shalom5f92bc92019-04-18 11:54:11 -07005118 wpa_s->global->pending_p2ps_group = 0;
5119 wpa_s->global->pending_p2ps_group_freq = 0;
5120
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005121 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005122 /*
5123 * We need to copy the interface name. Simply saving a
5124 * pointer isn't enough, since if we use pending_interface_name
5125 * it will be overwritten when the group is added.
5126 */
5127 char go_ifname[100];
5128
5129 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005130 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07005131 if (!response_done) {
5132 wpa_s->global->pending_p2ps_group = 1;
5133 wpa_s->global->pending_p2ps_group_freq = freq;
5134 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005135
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005136 if (!wpas_p2p_create_iface(wpa_s))
5137 os_memcpy(go_ifname, wpa_s->ifname,
5138 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005139 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005140 os_memcpy(go_ifname,
5141 wpa_s->pending_interface_name,
5142 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005143
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005144 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005145 wpas_p2ps_prov_complete(
5146 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
5147 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005148 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005149 NULL, 0, 0, 0, NULL, NULL, 0, 0,
5150 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005151 return;
5152 }
5153
5154 /* If PD Resp complete, start up the GO */
5155 if (response_done && persistent_go) {
5156 wpas_p2p_group_add_persistent(
5157 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005158 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005159 persistent_go->mode ==
5160 WPAS_MODE_P2P_GO ?
5161 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005162 0, 0, false, 0, NULL, NULL, NULL, NULL,
5163 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005164 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08005165 wpas_p2p_group_add(wpa_s, 1, freq,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005166 0, 0, 0, 0, 0, 0, false,
5167 wpa_s->p2p2,
5168 WPA_P2P_MODE_WFD_R1);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005169 }
5170
5171 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005172 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
5173 ETH_ALEN);
5174 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005175 }
5176 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005177 os_memcpy(go_ifname, go_wpa_s->ifname,
5178 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005179
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005180 if (is_zero_ether_addr(grp_mac)) {
5181 wpa_dbg(go_wpa_s, MSG_DEBUG,
5182 "P2P: Setting PIN-1 for ANY");
5183 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
5184 "12345670", NULL, 0,
5185 0);
5186 } else {
5187 wpa_dbg(go_wpa_s, MSG_DEBUG,
5188 "P2P: Setting PIN-1 for " MACSTR,
5189 MAC2STR(grp_mac));
5190 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
5191 "12345670", NULL, 0,
5192 0);
5193 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005194
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005195 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
5196 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005197 }
5198
5199 wpa_msg_global(wpa_s, MSG_INFO,
5200 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5201 " status=%d conncap=%x"
5202 " adv_id=%x adv_mac=" MACSTR
5203 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005204 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005205 MAC2STR(dev), status, conncap,
5206 adv_id, MAC2STR(adv_mac),
5207 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005208 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005209 return;
5210 }
5211
5212 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
5213 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
5214
5215 if (persistent_go && !persistent_go->num_p2p_clients) {
5216 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08005217 wpa_dbg(wpa_s, MSG_DEBUG,
5218 "P2P: Remove empty persistent group id=%d",
5219 persistent_go->id);
5220 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005221 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
5222 }
5223
5224 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005225 char ssid_hex[32 * 2 + 1];
5226
5227 if (group_ssid)
5228 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
5229 group_ssid, group_ssid_len);
5230 else
5231 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005232 wpa_msg_global(wpa_s, MSG_INFO,
5233 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5234 " status=%d conncap=%x"
5235 " adv_id=%x adv_mac=" MACSTR
5236 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005237 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005238 MAC2STR(dev), status, conncap,
5239 adv_id, MAC2STR(adv_mac),
5240 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005241 passwd_id, MAC2STR(grp_mac), feat_cap_str,
5242 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005243 } else {
5244 wpa_msg_global(wpa_s, MSG_INFO,
5245 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5246 " status=%d conncap=%x"
5247 " adv_id=%x adv_mac=" MACSTR
5248 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005249 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005250 MAC2STR(dev), status, conncap,
5251 adv_id, MAC2STR(adv_mac),
5252 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005253 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005254 }
5255}
5256
5257
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005258static int _wpas_p2p_in_progress(void *ctx)
5259{
5260 struct wpa_supplicant *wpa_s = ctx;
5261 return wpas_p2p_in_progress(wpa_s);
5262}
5263
5264
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005265static int wpas_prov_disc_resp_cb(void *ctx)
5266{
5267 struct wpa_supplicant *wpa_s = ctx;
5268 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005269 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005270
5271 if (!wpa_s->global->pending_p2ps_group)
5272 return 0;
5273
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005274 freq = wpa_s->global->pending_p2ps_group_freq;
5275 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005276 wpa_s->global->pending_p2ps_group = 0;
5277
5278 if (wpas_p2p_get_go_group(wpa_s))
5279 return 0;
5280 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5281
5282 if (persistent_go) {
5283 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005284 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5285 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005286 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08005287 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005288 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL, NULL,
5289 NULL, NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005290 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08005291 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005292 is_p2p_allow_6ghz(wpa_s->global->p2p),
5293 wpa_s->p2p2, WPA_P2P_MODE_WFD_R1);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005294 }
5295
5296 return 1;
5297}
5298
5299
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005300static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
5301 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00005302 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005303{
5304 struct wpa_supplicant *wpa_s = ctx;
5305
5306 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
5307 WPA_IF_P2P_CLIENT, len, freq_list);
5308}
5309
Sunil Ravic0f5d412024-09-11 22:12:49 +00005310
5311static void wpas_p2p_send_bootstrap_comeback(void *eloop_ctx, void *timeout_ctx)
5312{
5313 struct wpa_supplicant *wpa_s = eloop_ctx;
5314
5315 wpa_printf(MSG_DEBUG, "P2P2: Send bootstrapping comeback PD Request");
5316 wpas_p2p_connect(wpa_s, wpa_s->p2p_bootstrap_dev_addr, wpa_s->p2p_pin,
5317 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5318 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5319 wpa_s->p2p_go_vht_center_freq2,
5320 wpa_s->p2p_persistent_id,
5321 wpa_s->p2p_pd_before_go_neg,
5322 wpa_s->p2p_go_ht40,
5323 wpa_s->p2p_go_vht,
5324 wpa_s->p2p_go_max_oper_chwidth,
5325 wpa_s->p2p_go_he,
5326 wpa_s->p2p_go_edmg,
5327 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p),
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005328 wpa_s->p2p2, wpa_s->p2p_bootstrap, NULL, false);
Sunil Ravic0f5d412024-09-11 22:12:49 +00005329}
5330
5331
5332static void wpas_p2p_register_bootstrap_comeback(void *ctx, const u8 *addr,
5333 u16 comeback_after)
5334{
5335 unsigned int timeout_us;
5336 struct wpa_supplicant *wpa_s = ctx;
5337
5338 timeout_us = comeback_after * 1024;
5339 os_memcpy(wpa_s->p2p_bootstrap_dev_addr, addr, ETH_ALEN);
5340
5341 eloop_cancel_timeout(wpas_p2p_send_bootstrap_comeback, wpa_s, NULL);
5342 eloop_register_timeout(0, timeout_us, wpas_p2p_send_bootstrap_comeback,
5343 wpa_s, NULL);
5344}
5345
5346
5347static void wpas_bootstrap_req_rx(void *ctx, const u8 *addr,
5348 u16 bootstrap_method)
5349{
5350 struct wpa_supplicant *wpa_s = ctx;
5351
5352 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_BOOTSTRAP_REQUEST MACSTR
5353 " bootstrap_method=%u", MAC2STR(addr), bootstrap_method);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005354
5355 wpas_notify_p2p_bootstrap_req(wpa_s, addr, bootstrap_method);
Sunil Ravic0f5d412024-09-11 22:12:49 +00005356}
5357
5358
5359static void wpas_bootstrap_completed(void *ctx, const u8 *addr,
5360 enum p2p_status_code status, int freq)
5361{
5362 struct wpa_supplicant *wpa_s = ctx;
5363
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005364 wpas_notify_p2p_bootstrap_completed(wpa_s, addr, status);
5365
Sunil Ravic0f5d412024-09-11 22:12:49 +00005366 if (status) {
5367 wpa_msg_global(wpa_s, MSG_INFO,
5368 P2P_EVENT_BOOTSTRAP_FAILURE MACSTR " status=%d",
5369 MAC2STR(addr), status);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005370 return;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005371 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005372
5373 wpa_msg_global(wpa_s, MSG_INFO,
5374 P2P_EVENT_BOOTSTRAP_SUCCESS MACSTR " status=%d",
5375 MAC2STR(addr), status);
5376
5377#ifdef CONFIG_PASN
5378 wpas_p2p_initiate_pasn_auth(wpa_s, addr, freq);
5379#endif /* CONFIG_PASN */
Sunil Ravic0f5d412024-09-11 22:12:49 +00005380}
5381
5382
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005383static void wpas_validate_dira(void *ctx, const u8 *peer_addr,
5384 const u8 *dira, size_t dira_len)
5385{
5386 struct wpa_supplicant *wpa_s = ctx;
5387 int ret;
5388 u8 tag[DEVICE_MAX_HASH_LEN];
5389 struct wpa_dev_ik *ik;
5390 const u8 *addr[3];
5391 size_t len[3];
5392 const char *label = "DIR";
5393
5394 if (dira_len < 1 || dira[0] != DIRA_CIPHER_VERSION_128) {
5395 wpa_printf(MSG_ERROR,
5396 "P2P2: Unsupported DIRA cipher version %d", dira[0]);
5397 return;
5398 }
5399
5400 if (dira_len < 1 + DEVICE_IDENTITY_NONCE_LEN + DEVICE_IDENTITY_TAG_LEN)
5401 {
5402 wpa_printf(MSG_INFO, "P2P2: Truncated DIRA (length %zu)",
5403 dira_len);
5404 return;
5405 }
5406
5407 addr[0] = (const u8 *) label;
5408 len[0] = DIR_STR_LEN;
5409 addr[1] = peer_addr;
5410 len[1] = ETH_ALEN;
5411 addr[2] = &dira[1];
5412 len[2] = DEVICE_IDENTITY_NONCE_LEN;
5413
5414 for (ik = wpa_s->conf->identity; ik; ik = ik->next) {
5415 if (wpabuf_len(ik->dik) != DEVICE_IDENTITY_KEY_LEN ||
5416 ik->dik_cipher != DIRA_CIPHER_VERSION_128)
5417 continue;
5418
5419 ret = hmac_sha256_vector(wpabuf_head(ik->dik),
5420 DEVICE_IDENTITY_KEY_LEN,
5421 3, addr, len, tag);
5422 if (ret < 0) {
5423 wpa_printf(MSG_ERROR,
5424 "P2P2: Failed to derive DIRA Tag");
5425 return;
5426 }
5427
5428 if (os_memcmp(tag, &dira[1 + DEVICE_IDENTITY_NONCE_LEN],
5429 DEVICE_IDENTITY_TAG_LEN) == 0) {
5430 wpa_printf(MSG_DEBUG, "P2P2: DIRA Tag matched");
5431 break;
5432 }
5433 }
5434
5435 if (!ik)
5436 return;
5437
5438#ifdef CONFIG_PASN
5439 p2p_pasn_pmksa_set_pmk(wpa_s->global->p2p, wpa_s->global->p2p_dev_addr,
5440 peer_addr,
5441 wpabuf_head(ik->pmk), wpabuf_len(ik->pmk),
5442 wpabuf_head(ik->pmkid));
5443#endif /* CONFIG_PASN */
5444}
5445
5446
5447#ifdef CONFIG_PASN
5448
5449static int wpas_p2p_initiate_pasn_verify(struct wpa_supplicant *wpa_s,
5450 const u8 *peer,
5451 enum p2p_invite_role role,
5452 const u8 *bssid, const u8 *ssid,
5453 size_t ssid_len,
5454 unsigned int force_freq,
5455 const u8 *go_dev_addr,
5456 unsigned int pref_freq)
5457{
5458 int freq;
5459 struct wpa_p2p_pasn_auth_work *awork;
5460
5461 wpas_p2p_pasn_cancel_auth_work(wpa_s);
5462 wpa_s->p2p_pasn_auth_work = NULL;
5463
5464 freq = p2p_get_listen_freq(wpa_s->global->p2p, peer);
5465 if (freq == -1)
5466 return -1;
5467
5468 awork = os_zalloc(sizeof(*awork));
5469 if (!awork)
5470 return -1;
5471
5472 awork->verify = 1;
5473 awork->role = role;
5474 awork->freq = freq;
5475 awork->force_freq = force_freq;
5476 awork->pref_freq = pref_freq;
5477 os_memcpy(awork->peer_addr, peer, ETH_ALEN);
5478 if (go_dev_addr)
5479 os_memcpy(awork->go_dev_addr, go_dev_addr, ETH_ALEN);
5480 if (bssid)
5481 os_memcpy(awork->bssid, bssid, ETH_ALEN);
5482 if (ssid_len) {
5483 awork->ssid = os_zalloc(ssid_len);
5484 if (!awork->ssid) {
5485 os_free(awork);
5486 return -1;
5487 }
5488 os_memcpy(awork->ssid, ssid, ssid_len);
5489 awork->ssid_len = ssid_len;
5490 }
5491
5492 if (radio_add_work(wpa_s, freq, "p2p-pasn-start-auth", 1,
5493 wpas_p2p_pasn_auth_start_cb, awork) < 0) {
5494 wpas_p2p_pasn_free_auth_work(awork);
5495 return -1;
5496 }
5497
5498 wpa_printf(MSG_DEBUG, "P2P PASN: Auth work successfully added");
5499 return 0;
5500}
5501
5502
5503static int wpas_p2p_pasn_send_mgmt(void *ctx, const u8 *data, size_t data_len,
5504 int noack, unsigned int freq,
5505 unsigned int wait)
5506{
5507 struct wpa_supplicant *wpa_s = ctx;
5508
5509 return wpa_drv_send_mlme(wpa_s, data, data_len, noack, freq, wait);
5510}
5511
5512
5513static int wpas_p2p_prepare_data_element(void *ctx, const u8 *peer_addr)
5514{
5515 struct wpa_supplicant *wpa_s = ctx;
5516 struct p2p_data *p2p = wpa_s->global->p2p;
5517
5518 return p2p_prepare_data_element(p2p, peer_addr);
5519}
5520
5521
5522static int wpas_p2p_parse_data_element(void *ctx, const u8 *data, size_t len)
5523{
5524 struct wpa_supplicant *wpa_s = ctx;
5525 struct p2p_data *p2p = wpa_s->global->p2p;
5526
5527 return p2p_parse_data_element(p2p, data, len);
5528}
5529
5530#endif /* CONFIG_PASN */
5531
5532
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005533int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
5534{
JaeMan Park9de97322022-07-11 15:36:43 +09005535 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005536 u8 addr[ETH_ALEN] = {0};
5537
5538 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
5539 return 0;
5540
Hai Shalom60840252021-02-19 19:02:11 -08005541 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
5542 if (is_zero_ether_addr(
5543 wpa_s->conf->p2p_device_persistent_mac_addr) &&
5544 !is_zero_ether_addr(wpa_s->own_addr)) {
5545 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
5546 wpa_s->own_addr, ETH_ALEN);
5547 }
5548 return 0;
5549 }
5550
5551 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005552 if (random_mac_addr(addr) < 0) {
5553 wpa_msg(wpa_s, MSG_INFO,
5554 "Failed to generate random MAC address");
5555 return -EINVAL;
5556 }
5557
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005558 /* Store generated MAC address. */
5559 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
5560 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005561 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005562 /* If there are existing saved groups, restore last MAC address.
5563 * if there is no last used MAC address, the last one is
5564 * factory MAC. */
5565 if (is_zero_ether_addr(
5566 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005567 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005568 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
5569 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005570 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
5571 }
5572
JaeMan Park9de97322022-07-11 15:36:43 +09005573 ret = wpa_drv_set_mac_addr(wpa_s, addr);
5574
5575 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005576 wpa_msg(wpa_s, MSG_INFO,
5577 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09005578 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005579 }
5580
JaeMan Park9de97322022-07-11 15:36:43 +09005581 ret = wpa_supplicant_update_mac_addr(wpa_s);
5582
5583 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005584 wpa_msg(wpa_s, MSG_INFO,
5585 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09005586 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005587 }
5588
5589 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
5590 MAC2STR(addr));
5591
5592 return 0;
5593}
5594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595/**
5596 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5597 * @global: Pointer to global data from wpa_supplicant_init()
5598 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5599 * Returns: 0 on success, -1 on failure
5600 */
5601int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
5602{
5603 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005604 int i;
5605
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005606 if (wpa_s->conf->p2p_disabled)
5607 return 0;
5608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5610 return 0;
5611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 if (global->p2p)
5613 return 0;
5614
Jimmy Chenf887c7b2018-11-13 15:19:57 +08005615 if (wpas_p2p_mac_setup(wpa_s) < 0) {
5616 wpa_msg(wpa_s, MSG_ERROR,
5617 "Failed to initialize P2P random MAC address.");
5618 return -1;
5619 }
5620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005623 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624 p2p.p2p_scan = wpas_p2p_scan;
5625 p2p.send_action = wpas_send_action;
5626 p2p.send_action_done = wpas_send_action_done;
5627 p2p.go_neg_completed = wpas_go_neg_completed;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005628 p2p.set_go_security_config = wpas_set_go_security_config;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5630 p2p.dev_found = wpas_dev_found;
5631 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005632 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633 p2p.start_listen = wpas_start_listen;
5634 p2p.stop_listen = wpas_stop_listen;
5635 p2p.send_probe_resp = wpas_send_probe_resp;
5636 p2p.sd_request = wpas_sd_request;
5637 p2p.sd_response = wpas_sd_response;
5638 p2p.prov_disc_req = wpas_prov_disc_req;
5639 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005640 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 p2p.invitation_process = wpas_invitation_process;
5642 p2p.invitation_received = wpas_invitation_received;
5643 p2p.invitation_result = wpas_invitation_result;
5644 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005645 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005646 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08005647 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005648 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005649 p2p.get_persistent_group = wpas_get_persistent_group;
5650 p2p.get_go_info = wpas_get_go_info;
5651 p2p.remove_stale_groups = wpas_remove_stale_groups;
5652 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5653 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5654 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005655 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08005656 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07005657 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Sunil Ravic0f5d412024-09-11 22:12:49 +00005658 p2p.register_bootstrap_comeback = wpas_p2p_register_bootstrap_comeback;
5659 p2p.bootstrap_req_rx = wpas_bootstrap_req_rx;
5660 p2p.bootstrap_completed = wpas_bootstrap_completed;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005661 p2p.validate_dira = wpas_validate_dira;
5662#ifdef CONFIG_PASN
5663 p2p.pasn_send_mgmt = wpas_p2p_pasn_send_mgmt;
5664 p2p.prepare_data_element = wpas_p2p_prepare_data_element;
5665 p2p.parse_data_element = wpas_p2p_parse_data_element;
5666#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667
5668 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005669 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670 p2p.dev_name = wpa_s->conf->device_name;
5671 p2p.manufacturer = wpa_s->conf->manufacturer;
5672 p2p.model_name = wpa_s->conf->model_name;
5673 p2p.model_number = wpa_s->conf->model_number;
5674 p2p.serial_number = wpa_s->conf->serial_number;
5675 if (wpa_s->wps) {
5676 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5677 p2p.config_methods = wpa_s->wps->config_methods;
5678 }
5679
Hai Shalom60840252021-02-19 19:02:11 -08005680 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
5681 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005682 wpa_printf(MSG_ERROR,
5683 "P2P: Failed to configure supported channel list");
5684 return -1;
5685 }
5686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687 if (wpa_s->conf->p2p_listen_reg_class &&
5688 wpa_s->conf->p2p_listen_channel) {
5689 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5690 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005691 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005692 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693 /*
5694 * Pick one of the social channels randomly as the listen
5695 * channel.
5696 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005697 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005698 &p2p.channel,
5699 &global->p2p_go_avoid_freq,
5700 &global->p2p_disallow_freq) !=
5701 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005702 wpa_printf(MSG_INFO,
5703 "P2P: No social channels supported by the driver - do not enable P2P");
5704 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005705 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005706 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005708 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5709 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005710
5711 if (wpa_s->conf->p2p_oper_reg_class &&
5712 wpa_s->conf->p2p_oper_channel) {
5713 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5714 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5715 p2p.cfg_op_channel = 1;
5716 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5717 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5718
5719 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005721 * Use random operation channel from 2.4 GHz band social
5722 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5723 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005725 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005726 &p2p.op_channel, NULL,
5727 NULL) != 0) {
5728 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005729 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005730 p2p.op_reg_class = 0;
5731 p2p.op_channel = 0;
5732 /* This will be overridden during group setup in
5733 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005735 p2p.cfg_op_channel = 0;
5736 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5737 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5738 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005739
5740 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5741 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5742 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5743 }
5744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5746 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5747 p2p.country[2] = 0x04;
5748 } else
5749 os_memcpy(p2p.country, "XX\x04", 3);
5750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5752 WPS_DEV_TYPE_LEN);
5753
5754 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5755 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5756 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5757
5758 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5759 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5760
5761 p2p.max_peers = 100;
5762
5763 if (wpa_s->conf->p2p_ssid_postfix) {
5764 p2p.ssid_postfix_len =
5765 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5766 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5767 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5768 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5769 p2p.ssid_postfix_len);
5770 }
5771
5772 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5773
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005774 p2p.max_listen = wpa_s->max_remain_on_chan;
5775
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005776 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5777 wpa_s->conf->p2p_passphrase_len <= 63)
5778 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5779 else
5780 p2p.passphrase_len = 8;
5781
Sunil Ravic0f5d412024-09-11 22:12:49 +00005782 if (wpa_s->conf->dik &&
5783 wpabuf_len(wpa_s->conf->dik) <= DEVICE_IDENTITY_KEY_MAX_LEN) {
5784 p2p.pairing_config.dik_cipher = wpa_s->conf->dik_cipher;
5785 p2p.pairing_config.dik_len = wpabuf_len(wpa_s->conf->dik);
5786 os_memcpy(p2p.pairing_config.dik_data,
5787 wpabuf_head(wpa_s->conf->dik),
5788 p2p.pairing_config.dik_len);
5789 } else {
5790 p2p.pairing_config.dik_cipher = DIRA_CIPHER_VERSION_128;
5791 p2p.pairing_config.dik_len = DEVICE_IDENTITY_KEY_LEN;
5792 if (os_get_random(p2p.pairing_config.dik_data,
5793 p2p.pairing_config.dik_len) < 0)
5794 return -1;
5795
5796 wpa_s->conf->dik =
5797 wpabuf_alloc_copy(p2p.pairing_config.dik_data,
5798 p2p.pairing_config.dik_len);
5799 if (!wpa_s->conf->dik)
5800 return -1;
5801
5802 wpa_s->conf->dik_cipher = p2p.pairing_config.dik_cipher;
5803
5804 if (wpa_s->conf->update_config &&
5805 wpa_config_write(wpa_s->confname, wpa_s->conf))
5806 wpa_printf(MSG_DEBUG,
5807 "P2P: Failed to update configuration");
5808 }
5809
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005810 p2p.pairing_config.enable_pairing_setup =
5811 wpa_s->conf->p2p_pairing_setup;
5812 p2p.pairing_config.enable_pairing_cache =
5813 wpa_s->conf->p2p_pairing_cache;
5814 p2p.pairing_config.bootstrap_methods =
5815 wpa_s->conf->p2p_bootstrap_methods;
5816 p2p.pairing_config.pasn_type = wpa_s->conf->p2p_pasn_type;
5817 p2p.comeback_after = wpa_s->conf->p2p_comeback_after;
5818 p2p.reg_info = wpa_s->conf->p2p_reg_info;
5819 p2p.twt_power_mgmt = wpa_s->conf->p2p_twt_power_mgmt;
5820 p2p.chan_switch_req_enable = wpa_s->conf->p2p_chan_switch_req_enable;
5821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 global->p2p = p2p_init(&p2p);
5823 if (global->p2p == NULL)
5824 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005825 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005826
5827 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5828 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5829 continue;
5830 p2p_add_wps_vendor_extension(
5831 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5832 }
5833
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005834 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836 return 0;
5837}
5838
5839
5840/**
5841 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5842 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5843 *
5844 * This function deinitialize per-interface P2P data.
5845 */
5846void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5847{
5848 if (wpa_s->driver && wpa_s->drv_priv)
5849 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005850
5851 if (wpa_s->go_params) {
5852 /* Clear any stored provisioning info */
5853 p2p_clear_provisioning_info(
5854 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005855 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005856 }
5857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005858 os_free(wpa_s->go_params);
5859 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005860 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5862 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005863 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5865 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5866 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005867 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005868 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005869 wpas_p2p_listen_work_done(wpa_s);
5870 if (wpa_s->p2p_send_action_work) {
5871 os_free(wpa_s->p2p_send_action_work->ctx);
5872 radio_work_done(wpa_s->p2p_send_action_work);
5873 wpa_s->p2p_send_action_work = NULL;
5874 }
5875 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Sunil Ravic0f5d412024-09-11 22:12:49 +00005876 eloop_cancel_timeout(wpas_p2p_send_bootstrap_comeback, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005878 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5879 wpa_s->p2p_oob_dev_pw = NULL;
5880
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005881 os_free(wpa_s->p2p_group_common_freqs);
5882 wpa_s->p2p_group_common_freqs = NULL;
5883 wpa_s->p2p_group_common_freqs_num = 0;
5884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005885 /* TODO: remove group interface from the driver if this wpa_s instance
5886 * is on top of a P2P group interface */
5887}
5888
5889
5890/**
5891 * wpas_p2p_deinit_global - Deinitialize global P2P module
5892 * @global: Pointer to global data from wpa_supplicant_init()
5893 *
5894 * This function deinitializes the global (per device) P2P module.
5895 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005896static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897{
5898 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005900 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005901
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005902 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903
5904 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5906 wpa_s = wpa_s->next;
5907 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005908 tmp = global->ifaces;
5909 while (tmp &&
5910 (tmp == wpa_s ||
5911 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5912 tmp = tmp->next;
5913 }
5914 if (tmp == NULL)
5915 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005916 /* Disconnect from the P2P group and deinit the interface */
5917 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918 }
5919
5920 /*
5921 * Deinit GO data on any possibly remaining interface (if main
5922 * interface is used as GO).
5923 */
5924 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5925 if (wpa_s->ap_iface)
5926 wpas_p2p_group_deinit(wpa_s);
5927 }
5928
5929 p2p_deinit(global->p2p);
5930 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005931 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005932}
5933
5934
5935static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5936{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005937 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005938 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005939 if (wpa_s->drv_flags &
5940 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5941 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5942 return 1; /* P2P group requires a new interface in every case
5943 */
5944 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5945 return 0; /* driver does not support concurrent operations */
5946 if (wpa_s->global->ifaces->next)
5947 return 1; /* more that one interface already in use */
5948 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5949 return 1; /* this interface is already in use */
5950 return 0;
5951}
5952
5953
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00005954#ifdef CONFIG_PASN
5955static int wpas_p2p_config_sae_password(struct wpa_supplicant *wpa_s,
5956 struct wpa_ssid *ssid)
5957{
5958 struct p2p_data *p2p = wpa_s->global->p2p;
5959
5960 if (wpa_s->global->p2p_disabled || !p2p || !ssid->sae_password)
5961 return -2;
5962
5963 return p2p_config_sae_password(p2p, ssid->sae_password);
5964}
5965#endif /* CONFIG_PASN */
5966
5967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005968static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5969 const u8 *peer_addr,
5970 enum p2p_wps_method wps_method,
5971 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005972 unsigned int force_freq, int persistent_group,
Sunil Ravic0f5d412024-09-11 22:12:49 +00005973 struct wpa_ssid *ssid, unsigned int pref_freq,
5974 bool p2p2, u16 bootstrap, const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005976 if (persistent_group && wpa_s->conf->persistent_reconnect)
5977 persistent_group = 2;
5978
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005979 /*
5980 * Increase GO config timeout if HT40 is used since it takes some time
5981 * to scan channels for coex purposes before the BSS can be started.
5982 */
5983 p2p_set_config_timeout(wpa_s->global->p2p,
5984 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005986 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5987 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005988 persistent_group, ssid ? ssid->ssid : NULL,
5989 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005990 wpa_s->p2p_pd_before_go_neg, pref_freq,
5991 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
Sunil Ravic0f5d412024-09-11 22:12:49 +00005992 0, p2p2, bootstrap, password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005993}
5994
5995
5996static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5997 const u8 *peer_addr,
5998 enum p2p_wps_method wps_method,
5999 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006000 unsigned int force_freq, int persistent_group,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006001 struct wpa_ssid *ssid, unsigned int pref_freq,
6002 u16 bootstrap, const char *password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006003{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006004 if (persistent_group && wpa_s->conf->persistent_reconnect)
6005 persistent_group = 2;
6006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
6008 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006009 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006010 ssid ? ssid->ssid_len : 0, pref_freq,
6011 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
Sunil Ravic0f5d412024-09-11 22:12:49 +00006012 0, bootstrap, password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013}
6014
6015
6016static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
6017{
6018 wpa_s->p2p_join_scan_count++;
6019 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
6020 wpa_s->p2p_join_scan_count);
6021 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
6022 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
6023 " for join operationg - stop join attempt",
6024 MAC2STR(wpa_s->pending_join_iface_addr));
6025 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006026 if (wpa_s->p2p_auto_pd) {
6027 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006028 wpa_msg_global(wpa_s, MSG_INFO,
6029 P2P_EVENT_PROV_DISC_FAILURE
6030 " p2p_dev_addr=" MACSTR " status=N/A",
6031 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07006032 return;
6033 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08006034 if (wpa_s->p2p_fallback_to_go_neg) {
6035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
6036 "failed - fall back to GO Negotiation");
6037 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6038 P2P_EVENT_FALLBACK_TO_GO_NEG
6039 "reason=join-failed");
6040 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
6041 return;
6042 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006043 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006044 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006045 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 }
6047}
6048
6049
Dmitry Shmidt04949592012-07-19 12:16:46 -07006050static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
6051{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006052 int res;
6053 unsigned int num, i;
6054 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006055
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006056 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
6057 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07006058 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006059 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006060
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006061 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6062 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006063 if (!freqs)
6064 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006065
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006066 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6067 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006068
6069 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006070 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006071 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
6072 freq);
6073 res = 0;
6074 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006075 }
6076 }
6077
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006078 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006079 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006080
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006081exit_free:
6082 os_free(freqs);
6083 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006084}
6085
6086
6087static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
6088 const u8 *peer_dev_addr)
6089{
6090 struct wpa_bss *bss;
6091 int updated;
6092
6093 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
6094 if (bss == NULL)
6095 return -1;
6096 if (bss->last_update_idx < wpa_s->bss_update_idx) {
6097 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
6098 "last scan");
6099 return 0;
6100 }
6101
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006102 updated = os_reltime_before(&wpa_s->p2p_auto_started,
6103 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006104 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
6105 "%ld.%06ld (%supdated in last scan)",
6106 bss->last_update.sec, bss->last_update.usec,
6107 updated ? "": "not ");
6108
6109 return updated;
6110}
6111
6112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
6114 struct wpa_scan_results *scan_res)
6115{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006116 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117 int freq;
6118 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07006119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6121
6122 if (wpa_s->global->p2p_disabled)
6123 return;
6124
Dmitry Shmidt04949592012-07-19 12:16:46 -07006125 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
6126 scan_res ? (int) scan_res->num : -1,
6127 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006128
6129 if (scan_res)
6130 wpas_p2p_scan_res_handler(wpa_s, scan_res);
6131
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006132 if (!wpa_s->p2p2 && wpa_s->p2p_auto_pd) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006133 int join = wpas_p2p_peer_go(wpa_s,
6134 wpa_s->pending_join_dev_addr);
6135 if (join == 0 &&
6136 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
6137 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006138 bss = wpa_bss_get_bssid_latest(
6139 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006140 if (bss) {
6141 freq = bss->freq;
6142 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
6143 "the peer " MACSTR " at %d MHz",
6144 wpa_s->auto_pd_scan_retry,
6145 MAC2STR(wpa_s->
6146 pending_join_dev_addr),
6147 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006148 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006149 return;
6150 }
6151 }
6152
6153 if (join < 0)
6154 join = 0;
6155
6156 wpa_s->p2p_auto_pd = 0;
6157 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
6158 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
6159 MAC2STR(wpa_s->pending_join_dev_addr), join);
6160 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006161 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006162 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006163 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006164 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006165 wpa_msg_global(wpa_s, MSG_INFO,
6166 P2P_EVENT_PROV_DISC_FAILURE
6167 " p2p_dev_addr=" MACSTR " status=N/A",
6168 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07006169 }
6170 return;
6171 }
6172
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006173 if (wpa_s->p2p2 || wpa_s->p2p_auto_join) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006174 int join = wpas_p2p_peer_go(wpa_s,
6175 wpa_s->pending_join_dev_addr);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006176
6177 if (wpa_s->p2p2 || join < 0) {
6178 if (join < 0) {
6179 wpa_printf(MSG_DEBUG,
6180 "P2P: Peer was not found to be running a GO -> use GO Negotiation");
6181 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
6182 P2P_EVENT_FALLBACK_TO_GO_NEG
6183 "reason=peer-not-running-GO");
6184 }
6185
6186 if (wpa_s->p2p2)
6187 wpa_printf(MSG_DEBUG,
6188 "P2P2: Initiate GO negotiation and provisioning using PASN Authentication");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006189 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
6190 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
6191 wpa_s->p2p_persistent_group, 0, 0, 0,
6192 wpa_s->p2p_go_intent,
6193 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006194 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006195 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006196 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006197 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006198 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006199 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08006200 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006201 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006202 NULL, 0,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006203 is_p2p_allow_6ghz(wpa_s->global->p2p),
6204 wpa_s->p2p2, wpa_s->p2p_bootstrap,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006205 wpa_s->pending_join_password[0] ?
6206 wpa_s->pending_join_password : NULL,
6207 false);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006208 return;
6209 }
6210
6211 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
6212 "try to join the group", join ? "" :
6213 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006214 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006215 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006216 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006217 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08006218 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006219 }
6220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 freq = p2p_get_oper_freq(wpa_s->global->p2p,
6222 wpa_s->pending_join_iface_addr);
6223 if (freq < 0 &&
6224 p2p_get_interface_addr(wpa_s->global->p2p,
6225 wpa_s->pending_join_dev_addr,
6226 iface_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006227 !ether_addr_equal(iface_addr, wpa_s->pending_join_dev_addr) &&
6228 !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
6230 "address for join from " MACSTR " to " MACSTR
6231 " based on newly discovered P2P peer entry",
6232 MAC2STR(wpa_s->pending_join_iface_addr),
6233 MAC2STR(iface_addr));
6234 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
6235 ETH_ALEN);
6236
6237 freq = p2p_get_oper_freq(wpa_s->global->p2p,
6238 wpa_s->pending_join_iface_addr);
6239 }
6240 if (freq >= 0) {
6241 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
6242 "from P2P peer table: %d MHz", freq);
6243 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006244 if (wpa_s->p2p_join_ssid_len) {
6245 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
6246 MACSTR " and SSID %s",
6247 MAC2STR(wpa_s->pending_join_iface_addr),
6248 wpa_ssid_txt(wpa_s->p2p_join_ssid,
6249 wpa_s->p2p_join_ssid_len));
6250 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
6251 wpa_s->p2p_join_ssid,
6252 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006253 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006254 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
6255 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
6256 bss = wpa_bss_get_bssid_latest(wpa_s,
6257 wpa_s->pending_join_iface_addr);
6258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006260 u8 dev_addr[ETH_ALEN];
6261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006262 freq = bss->freq;
6263 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07006264 "from BSS table: %d MHz (SSID %s)", freq,
6265 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08006266 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006267 dev_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00006268 ether_addr_equal(wpa_s->pending_join_dev_addr,
6269 wpa_s->pending_join_iface_addr) &&
6270 !ether_addr_equal(dev_addr, wpa_s->pending_join_dev_addr)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006271 wpa_printf(MSG_DEBUG,
6272 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
6273 MAC2STR(dev_addr),
6274 MAC2STR(wpa_s->pending_join_dev_addr));
6275 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
6276 ETH_ALEN);
6277 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006278 }
6279 if (freq > 0) {
6280 u16 method;
6281
Dmitry Shmidt04949592012-07-19 12:16:46 -07006282 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006283 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006284 P2P_EVENT_GROUP_FORMATION_FAILURE
6285 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006286 wpas_notify_p2p_group_formation_failure(
6287 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006288 return;
6289 }
6290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
6292 "prior to joining an existing group (GO " MACSTR
6293 " freq=%u MHz)",
6294 MAC2STR(wpa_s->pending_join_dev_addr), freq);
6295 wpa_s->pending_pd_before_join = 1;
6296
6297 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298 case WPS_PIN_DISPLAY:
6299 method = WPS_CONFIG_KEYPAD;
6300 break;
6301 case WPS_PIN_KEYPAD:
6302 method = WPS_CONFIG_DISPLAY;
6303 break;
6304 case WPS_PBC:
6305 method = WPS_CONFIG_PUSHBUTTON;
6306 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006307 case WPS_P2PS:
6308 method = WPS_CONFIG_P2PS;
6309 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006310 default:
6311 method = 0;
6312 break;
6313 }
6314
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006315 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
6316 wpa_s->pending_join_dev_addr) ==
6317 method)) {
6318 /*
6319 * We have already performed provision discovery for
6320 * joining the group. Proceed directly to join
6321 * operation without duplicated provision discovery. */
6322 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
6323 "with " MACSTR " already done - proceed to "
6324 "join",
6325 MAC2STR(wpa_s->pending_join_dev_addr));
6326 wpa_s->pending_pd_before_join = 0;
6327 goto start;
6328 }
6329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006331 wpa_s->pending_join_dev_addr,
6332 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006333 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
6335 "Discovery Request before joining an "
6336 "existing group");
6337 wpa_s->pending_pd_before_join = 0;
6338 goto start;
6339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006340 return;
6341 }
6342
6343 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
6344 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6345 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
6346 wpas_p2p_check_join_scan_limit(wpa_s);
6347 return;
6348
6349start:
6350 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006351 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
6352 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353}
6354
6355
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006356static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
6357 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006358{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359 int ret;
6360 struct wpa_driver_scan_params params;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006361 struct wpabuf *wps_ie = NULL, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006362 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006363 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006364 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006365
6366 os_memset(&params, 0, sizeof(params));
6367
6368 /* P2P Wildcard SSID */
6369 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006370 if (ssid && ssid_len) {
6371 params.ssids[0].ssid = ssid;
6372 params.ssids[0].ssid_len = ssid_len;
6373 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
6374 wpa_s->p2p_join_ssid_len = ssid_len;
6375 } else {
6376 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
6377 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
6378 wpa_s->p2p_join_ssid_len = 0;
6379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006381 if (!wpa_s->p2p2) {
6382 wpa_s->wps->dev.p2p = 1;
6383 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT,
6384 &wpa_s->wps->dev,
6385 wpa_s->wps->uuid,
6386 WPS_REQ_ENROLLEE, 0, NULL);
6387 if (!wps_ie) {
6388 wpas_p2p_scan_res_join(wpa_s, NULL);
6389 return;
6390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006391 }
6392
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08006393 if (!freq) {
6394 int oper_freq;
6395 /*
6396 * If freq is not provided, check the operating freq of the GO
6397 * and use a single channel scan on if possible.
6398 */
6399 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
6400 wpa_s->pending_join_iface_addr);
6401 if (oper_freq > 0)
6402 freq = oper_freq;
6403 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006404 if (freq > 0) {
6405 freqs[0] = freq;
6406 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00006407 } else {
6408 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006410
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006411 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006412
6413 if (wps_ie)
6414 ielen += wpabuf_len(wps_ie);
6415
6416 ies = wpabuf_alloc(ielen);
6417 if (!ies) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006418 wpabuf_free(wps_ie);
6419 wpas_p2p_scan_res_join(wpa_s, NULL);
6420 return;
6421 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006422
6423 if (wps_ie) {
6424 wpabuf_put_buf(ies, wps_ie);
6425 wpabuf_free(wps_ie);
6426 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006427
6428 bands = wpas_get_bands(wpa_s, freqs);
6429 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
6430
6431 params.p2p_probe = 1;
6432 params.extra_ies = wpabuf_head(ies);
6433 params.extra_ies_len = wpabuf_len(ies);
6434
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006435 if (wpa_s->clear_driver_scan_cache) {
6436 wpa_printf(MSG_DEBUG,
6437 "Request driver to clear scan cache due to local BSS flush");
6438 params.only_new_results = 1;
6439 }
6440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441 /*
6442 * Run a scan to update BSS table and start Provision Discovery once
6443 * the new scan results become available.
6444 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006445 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08006446 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07006447 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006448 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006449 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006450 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006451 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006452 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07006453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454
6455 wpabuf_free(ies);
6456
6457 if (ret) {
6458 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
6459 "try again later");
6460 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6461 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
6462 wpas_p2p_check_join_scan_limit(wpa_s);
6463 }
6464}
6465
6466
Dmitry Shmidt04949592012-07-19 12:16:46 -07006467static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
6468{
6469 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006470 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006471}
6472
6473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006475 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006476 int auto_join, int op_freq,
6477 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478{
6479 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006480 MACSTR " dev " MACSTR " op_freq=%d)%s",
6481 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006482 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08006483 if (ssid && ssid_len) {
6484 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
6485 wpa_ssid_txt(ssid, ssid_len));
6486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487
Dmitry Shmidt04949592012-07-19 12:16:46 -07006488 wpa_s->p2p_auto_pd = 0;
6489 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006490 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
6491 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
6492 wpa_s->pending_join_wps_method = wps_method;
6493
6494 /* Make sure we are not running find during connection establishment */
6495 wpas_p2p_stop_find(wpa_s);
6496
6497 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006498 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499 return 0;
6500}
6501
6502
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006503static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
6504 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505{
6506 struct wpa_supplicant *group;
6507 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006508 struct wpa_bss *bss;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006509 const u8 *iface_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510
6511 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
6512 if (group == NULL)
6513 return -1;
6514 if (group != wpa_s) {
6515 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
6516 sizeof(group->p2p_pin));
6517 group->p2p_wps_method = wpa_s->p2p_wps_method;
6518 }
6519
Hai Shalom74f70d42019-02-11 14:42:39 -08006520 /*
6521 * Need to mark the current interface for p2p_group_formation
6522 * when a separate group interface is not used. This is needed
6523 * to allow p2p_cancel stop a pending p2p_connect-join.
6524 * wpas_p2p_init_group_interface() addresses this for the case
6525 * where a separate group interface is used.
6526 */
6527 if (group == wpa_s->parent)
6528 wpa_s->global->p2p_group_formation = group;
6529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006530 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006531 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532
6533 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006534 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
6536 ETH_ALEN);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006537 if (!is_zero_ether_addr(wpa_s->pending_join_iface_addr))
6538 iface_addr = wpa_s->pending_join_iface_addr;
6539
6540 if (wpa_s->pending_join_password[0]) {
6541 res.akmp = WPA_KEY_MGMT_SAE;
6542 os_strlcpy(res.sae_password, wpa_s->pending_join_password,
6543 sizeof(res.sae_password));
6544 os_memset(wpa_s->pending_join_password, 0,
6545 sizeof(wpa_s->pending_join_password));
6546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006547 res.wps_method = wpa_s->pending_join_wps_method;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006548 res.p2p2 = wpa_s->p2p2;
6549 res.cipher = WPA_CIPHER_CCMP;
6550
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006551 if (freq && ssid && ssid_len) {
6552 res.freq = freq;
6553 res.ssid_len = ssid_len;
6554 os_memcpy(res.ssid, ssid, ssid_len);
6555 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006556 if (ssid && ssid_len) {
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006557 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006558 } else {
6559 bss = wpa_bss_get_bssid_latest(
6560 wpa_s, wpa_s->pending_join_iface_addr);
6561 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006562 if (bss) {
6563 res.freq = bss->freq;
6564 res.ssid_len = bss->ssid_len;
6565 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006566 os_memcpy(res.peer_interface_addr, bss->bssid,
6567 ETH_ALEN);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006568 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
6569 bss->freq,
6570 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006571 } else if (ssid && ssid_len) {
6572 res.ssid_len = ssid_len;
6573 os_memcpy(res.ssid, ssid, ssid_len);
6574 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
6575 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006576 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006577 }
6578
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006579 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
6580 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
6581 "starting client");
6582 wpa_drv_cancel_remain_on_channel(wpa_s);
6583 wpa_s->off_channel_freq = 0;
6584 wpa_s->roc_waiting_drv_freq = 0;
6585 }
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006586 if (res.p2p2)
6587 wpas_start_gc(group, &res);
6588 else
6589 wpas_start_wps_enrollee(group, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590
6591 /*
6592 * Allow a longer timeout for join-a-running-group than normal 15
6593 * second group formation timeout since the GO may not have authorized
6594 * our connection yet.
6595 */
6596 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
6597 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
6598 wpa_s, NULL);
6599
6600 return 0;
6601}
6602
6603
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006604static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006605 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00006606 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006607 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006608{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006609 struct wpa_used_freq_data *freqs;
6610 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006611 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306612 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006613
6614 max_pref_freq = *num_pref_freq;
6615 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006616
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006617 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6618 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006619 if (!freqs)
6620 return -1;
6621
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006622 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
6623 wpa_s->num_multichan_concurrent);
6624
6625 /*
6626 * It is possible that the total number of used frequencies is bigger
6627 * than the number of frequencies used for P2P, so get the system wide
6628 * number of unused frequencies.
6629 */
6630 num_unused = wpas_p2p_num_unused_channels(wpa_s);
6631
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006632 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006633 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
6634 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006635
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006636 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006637 int ret;
6638 if (go)
6639 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
6640 else
6641 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
6642 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006643 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006644 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6645 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006646 /*
6647 * If freq is a DFS channel and DFS is offloaded
6648 * to the driver, allow P2P GO to use it.
6649 */
6650 wpa_printf(MSG_DEBUG,
6651 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
6652 freq);
6653 } else {
6654 wpa_printf(MSG_DEBUG,
6655 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
6656 freq);
6657 res = -3;
6658 goto exit_free;
6659 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006660 }
6661
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006662 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006663 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006664 freq_in_use = 1;
6665 }
6666
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006667 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006668 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
6669 freq);
6670 res = -2;
6671 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006672 }
6673 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
6674 "requested channel (%u MHz)", freq);
6675 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006676 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006677 }
6678
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006679 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006680
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306681 if (*pref_freq == 0) {
6682 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006683 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306684 while (i < wpa_s->conf->num_p2p_pref_chan) {
6685 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
6686 wpa_s->conf->p2p_pref_chan[i].op_class,
6687 wpa_s->conf->p2p_pref_chan[i].chan);
6688
6689 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
6690 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
6691 best_freq = p2p_pref_freq;
6692 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
6693 "from P2P preferred channel list", best_freq);
6694 break;
6695 } else {
6696 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
6697 "frequency (%u MHz) ", p2p_pref_freq);
6698 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006699 i++;
6700 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05306701 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306702 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07006703 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05306704
Sunil Ravi036cec52023-03-29 11:35:17 -07006705 *num_pref_freq = max_pref_freq;
6706 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
6707 num_pref_freq);
6708 if (res > 0)
6709 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006710 }
6711 }
6712
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006713 /* We have a candidate frequency to use */
6714 if (best_freq > 0) {
6715 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006716 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006717 best_freq);
6718 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07006719 } else {
6720 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006721 best_freq);
6722 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006723 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006724 } else if (num_unused > 0) {
6725 wpa_printf(MSG_DEBUG,
6726 "P2P: Current operating channels are not available for P2P. Try to use another channel");
6727 *force_freq = 0;
6728 } else {
6729 wpa_printf(MSG_DEBUG,
6730 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
6731 res = -2;
6732 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006733 }
6734
6735exit_ok:
6736 res = 0;
6737exit_free:
6738 os_free(freqs);
6739 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006740}
6741
6742
Hai Shalomc1a21442022-02-04 13:43:00 -08006743static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
6744 const u8 *peer_addr)
6745{
6746 if (wpa_s->conf->p2p_6ghz_disable ||
6747 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6748 HOSTAPD_MODE_IEEE80211A, true))
6749 return false;
6750
Sunil Ravic0f5d412024-09-11 22:12:49 +00006751 if (wpa_s->p2p2)
6752 return true;
6753
Hai Shalomc1a21442022-02-04 13:43:00 -08006754 if (!p2p_wfd_enabled(wpa_s->global->p2p))
6755 return false;
6756 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
6757 return false;
6758
6759 return true;
6760}
6761
6762
6763static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
6764 const u8 *peer_addr, bool allow_6ghz, int freq)
6765{
6766 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
6767 wpa_printf(MSG_DEBUG,
6768 "P2P: Allow connection on 6 GHz channels");
6769 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
6770 } else {
6771 if (is_6ghz_freq(freq))
6772 return -2;
6773 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
6774 }
6775
6776 return 0;
6777}
6778
6779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006780/**
6781 * wpas_p2p_connect - Request P2P Group Formation to be started
6782 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6783 * @peer_addr: Address of the peer P2P Device
6784 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6785 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07006786 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006787 * @join: Whether to join an existing group (as a client) instead of starting
6788 * Group Owner negotiation; @peer_addr is BSSID in that case
6789 * @auth: Whether to only authorize the connection instead of doing that and
6790 * initiating Group Owner negotiation
6791 * @go_intent: GO Intent or -1 to use default
6792 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006793 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006794 * @persistent_id: Persistent group credentials to use for forcing GO
6795 * parameters or -1 to generate new values (SSID/passphrase)
6796 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6797 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006798 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006799 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006800 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006801 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006802 * @group_ssid: Specific Group SSID for join or %NULL if not set
6803 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006804 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Sunil Ravic0f5d412024-09-11 22:12:49 +00006805 * @p2p2: Whether device is in P2P R2 mode
6806 * @bootstrap: Requested bootstrap method for pairing in P2P2
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006807 * @password: Password for pairing setup or NULL for opportunistic method
Sunil Ravic0f5d412024-09-11 22:12:49 +00006808 * in P2P2
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006809 * @skip_prov: Connect without provisioning
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006810 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6811 * failure, -2 on failure due to channel not currently available,
6812 * -3 if forced channel is not supported
6813 */
6814int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6815 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006816 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006817 int go_intent, int freq, unsigned int vht_center_freq2,
6818 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006819 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006820 const u8 *group_ssid, size_t group_ssid_len,
Sunil Ravic0f5d412024-09-11 22:12:49 +00006821 bool allow_6ghz, bool p2p2, u16 bootstrap,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006822 const char *password, bool skip_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006824 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006825 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006826 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006827 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006828 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006829 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6830 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006831
6832 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6833 return -1;
6834
Dmitry Shmidt04949592012-07-19 12:16:46 -07006835 if (persistent_id >= 0) {
6836 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6837 if (ssid == NULL || ssid->disabled != 2 ||
6838 ssid->mode != WPAS_MODE_P2P_GO)
6839 return -1;
6840 }
6841
Sunil Ravic0f5d412024-09-11 22:12:49 +00006842 wpa_s->p2p2 = p2p2;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006843 wpa_s->p2p_mode = p2p2 ? WPA_P2P_MODE_WFD_R2 : WPA_P2P_MODE_WFD_R1;
Sunil Ravic0f5d412024-09-11 22:12:49 +00006844
Hai Shalomc1a21442022-02-04 13:43:00 -08006845 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006846 return -2;
6847
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006848 os_free(wpa_s->global->add_psk);
6849 wpa_s->global->add_psk = NULL;
6850
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006851 p2p_set_go_role(wpa_s->global->p2p, false);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006852 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006853 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006854 wpa_s->global->pending_p2ps_group_freq = 0;
6855 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857 if (go_intent < 0)
6858 go_intent = wpa_s->conf->p2p_go_intent;
6859
6860 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006861 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862
6863 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006864 wpa_s->p2p_persistent_group = !!persistent_group;
6865 wpa_s->p2p_persistent_id = persistent_id;
6866 wpa_s->p2p_go_intent = go_intent;
6867 wpa_s->p2p_connect_freq = freq;
6868 wpa_s->p2p_fallback_to_go_neg = 0;
6869 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006870 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006871 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006872 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6873 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006874 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006875 wpa_s->p2p_go_edmg = !!edmg;
Sunil Ravic0f5d412024-09-11 22:12:49 +00006876 wpa_s->p2p_bootstrap = bootstrap;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006877
6878 if (pin)
6879 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6880 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006881 if (wps_generate_pin((unsigned int *) &ret) < 0)
6882 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006883 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6884 "%08d", ret);
6885 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6886 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006887 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6888 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006889 } else if (wps_method == WPS_P2PS) {
6890 /* Force the P2Ps default PIN to be used */
6891 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006892 } else
6893 wpa_s->p2p_pin[0] = '\0';
6894
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006895 if (!password)
6896 os_memset(wpa_s->pending_join_password, 0,
6897 sizeof(wpa_s->pending_join_password));
6898
Dmitry Shmidt04949592012-07-19 12:16:46 -07006899 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6901 if (auth) {
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006902#ifdef CONFIG_PASN
6903 struct wpa_supplicant *ifs;
6904#endif /* CONFIG_PASN */
6905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006906 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6907 "connect a running group from " MACSTR,
6908 MAC2STR(peer_addr));
6909 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006910
6911#ifdef CONFIG_PASN
6912 if (!wpa_s->p2p2)
6913 return ret;
6914
6915 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6916 if (wpa_s->create_p2p_iface) {
6917 if_addr = wpa_s->pending_interface_addr;
6918 } else {
6919 if (wpa_s->p2p_mgmt)
6920 if_addr = wpa_s->parent->own_addr;
6921 else
6922 if_addr = wpa_s->own_addr;
6923 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6924 }
6925
6926 dl_list_for_each(ifs, &wpa_s->radio->ifaces,
6927 struct wpa_supplicant, radio_list) {
6928 if (!ifs->current_ssid ||
6929 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
6930 continue;
6931
6932 ssid = ifs->current_ssid;
6933
6934 if (bootstrap == P2P_PBMA_OPPORTUNISTIC &&
6935 wpas_p2p_config_sae_password(wpa_s, ssid)) {
6936 ssid = NULL;
6937 continue;
6938 }
6939
6940 force_freq = ifs->ap_iface->freq;
6941 break;
6942 }
6943 p2p_set_go_role(wpa_s->global->p2p, true);
6944 return wpas_p2p_auth_go_neg(wpa_s, peer_addr,
6945 wps_method, 15, if_addr,
6946 force_freq,
6947 persistent_group, ssid,
6948 pref_freq, bootstrap,
6949 password);
6950#else /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006951 return ret;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006952#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006953 }
6954 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6955 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6956 iface_addr) < 0) {
6957 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6958 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6959 dev_addr);
6960 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006961 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006962 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006963 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6964 "%ld.%06ld",
6965 wpa_s->p2p_auto_started.sec,
6966 wpa_s->p2p_auto_started.usec);
6967 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006968 wpa_s->user_initiated_pd = 1;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00006969 if (password)
6970 os_strlcpy(wpa_s->pending_join_password, password,
6971 sizeof(wpa_s->pending_join_password));
6972
6973 if (skip_prov) {
6974 if (!wpa_s->p2p2) {
6975 wpa_printf(MSG_DEBUG,
6976 "P2P: Join without provisioning not supported");
6977 return -1;
6978 }
6979 /* Start join operation immediately */
6980 return wpas_p2p_join_start(wpa_s, 0, group_ssid,
6981 group_ssid_len);
6982 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006983 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006984 auto_join, freq,
6985 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986 return -1;
6987 return ret;
6988 }
6989
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006990 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006991 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006992 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006993 if (res)
6994 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006995 wpas_p2p_set_own_freq_preference(wpa_s,
6996 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006997
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006998 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007002 if (wpa_s->create_p2p_iface) {
7003 /* Prepare to add a new interface for the group */
7004 iftype = WPA_IF_P2P_GROUP;
7005 if (go_intent == 15)
7006 iftype = WPA_IF_P2P_GO;
7007 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7008 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7009 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007010 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007013 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007014 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007015 if (wpa_s->p2p_mgmt)
7016 if_addr = wpa_s->parent->own_addr;
7017 else
7018 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007019 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
7020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021
7022 if (auth) {
7023 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007024 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007025 force_freq, persistent_group, ssid,
Sunil Ravic0f5d412024-09-11 22:12:49 +00007026 pref_freq, bootstrap, password) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007027 return -1;
7028 return ret;
7029 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007030
7031 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
7032 go_intent, if_addr, force_freq,
Sunil Ravic0f5d412024-09-11 22:12:49 +00007033 persistent_group, ssid, pref_freq, p2p2,
7034 bootstrap, password) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007035 if (wpa_s->create_p2p_iface)
7036 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037 return -1;
7038 }
7039 return ret;
7040}
7041
7042
7043/**
7044 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
7045 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7046 * @freq: Frequency of the channel in MHz
7047 * @duration: Duration of the stay on the channel in milliseconds
7048 *
7049 * This callback is called when the driver indicates that it has started the
7050 * requested remain-on-channel duration.
7051 */
7052void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
7053 unsigned int freq, unsigned int duration)
7054{
7055 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7056 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007057 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)",
7058 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
7059 wpa_s->roc_waiting_drv_freq, freq, duration);
7060 if (wpa_s->off_channel_freq &&
7061 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007062 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
7063 wpa_s->pending_listen_duration);
7064 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08007065 } else {
7066 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
7067 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
7068 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007069 }
7070}
7071
7072
Jithu Jance57cea1a2014-08-20 10:22:04 -07007073int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074{
7075 /* Limit maximum Listen state time based on driver limitation. */
7076 if (timeout > wpa_s->max_remain_on_chan)
7077 timeout = wpa_s->max_remain_on_chan;
7078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007079 return p2p_listen(wpa_s->global->p2p, timeout);
7080}
7081
7082
7083/**
7084 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
7085 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7086 * @freq: Frequency of the channel in MHz
7087 *
7088 * This callback is called when the driver indicates that a remain-on-channel
7089 * operation has been completed, i.e., the duration on the requested channel
7090 * has timed out.
7091 */
7092void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
7093 unsigned int freq)
7094{
7095 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
7096 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07007097 wpa_s->global->p2p_long_listen,
7098 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007099 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7101 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07007102 if (wpa_s->global->p2p_long_listen > 0)
7103 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007104 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
7105 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007106 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07007108 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07007110 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007111 } else {
7112 /*
7113 * When listen duration is over, stop listen & update p2p_state
7114 * to IDLE.
7115 */
7116 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007117 }
7118}
7119
7120
7121/**
7122 * wpas_p2p_group_remove - Remove a P2P group
7123 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7124 * @ifname: Network interface name of the group interface or "*" to remove all
7125 * groups
7126 * Returns: 0 on success, -1 on failure
7127 *
7128 * This function is used to remove a P2P group. This can be used to disconnect
7129 * from a group in which the local end is a P2P Client or to end a P2P Group in
7130 * case the local end is the Group Owner. If a virtual network interface was
7131 * created for this group, that interface will be removed. Otherwise, only the
7132 * configured P2P group network will be removed from the interface.
7133 */
7134int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
7135{
7136 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007137 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007138
7139 if (os_strcmp(ifname, "*") == 0) {
7140 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07007141 bool calling_wpa_s_group_removed = false;
7142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007143 wpa_s = global->ifaces;
7144 while (wpa_s) {
7145 prev = wpa_s;
7146 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007147 if (prev->p2p_group_interface !=
7148 NOT_P2P_GROUP_INTERFACE ||
7149 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07007150 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007151 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07007152 if (prev == calling_wpa_s)
7153 calling_wpa_s_group_removed = true;
7154 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007155 }
Hai Shalom899fcc72020-10-19 14:38:18 -07007156
7157 if (!calling_wpa_s_group_removed &&
7158 (calling_wpa_s->p2p_group_interface !=
7159 NOT_P2P_GROUP_INTERFACE ||
7160 (calling_wpa_s->current_ssid &&
7161 calling_wpa_s->current_ssid->p2p_group))) {
7162 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
7163 wpas_p2p_disconnect_safely(calling_wpa_s,
7164 calling_wpa_s);
7165 }
7166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167 return 0;
7168 }
7169
7170 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7171 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7172 break;
7173 }
7174
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007175 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007176}
7177
7178
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007179static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
7180{
7181 unsigned int r;
7182
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007183 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
7184 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00007185 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007186 int res;
7187
7188 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
7189 &size, pref_freq_list);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00007190 if (!res && size > 0 && !is_p2p_allow_6ghz(wpa_s->global->p2p))
Hai Shalomc1a21442022-02-04 13:43:00 -08007191 size = p2p_remove_6ghz_channels(pref_freq_list, size);
7192
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007193 if (!res && size > 0) {
7194 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08007195 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007196 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00007197 pref_freq_list[i].freq) ||
7198 wpas_p2p_disallowed_freq(
7199 wpa_s->global,
7200 pref_freq_list[i].freq) ||
7201 !p2p_pref_freq_allowed(&pref_freq_list[i],
7202 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007203 wpa_printf(MSG_DEBUG,
7204 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00007205 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007206 i++;
7207 }
7208 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00007209 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007210 wpa_printf(MSG_DEBUG,
7211 "P2P: Using preferred_freq_list[%d]=%d",
7212 i, freq);
7213 } else {
7214 wpa_printf(MSG_DEBUG,
7215 "P2P: All driver preferred frequencies are disallowed for P2P use");
7216 }
7217 } else {
7218 wpa_printf(MSG_DEBUG,
7219 "P2P: No preferred frequency list available");
7220 }
7221 }
7222
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007223 if (freq == 2) {
7224 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
7225 "band");
7226 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007227 p2p_supported_freq_go(wpa_s->global->p2p,
7228 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007229 freq = wpa_s->best_24_freq;
7230 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
7231 "channel: %d MHz", freq);
7232 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007233 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7234 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08007235 int possible_2g_freqs[] = {
7236 /* operating class 81 */
7237 2412, 2437, 2462,
7238 };
7239 int possible_2g_freqs_num =
7240 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
7241 int i;
7242 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
7243 freq = possible_2g_freqs[r % possible_2g_freqs_num];
7244 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
7245 break;
7246 }
7247 }
7248
7249 if (i >= possible_2g_freqs_num) {
7250 wpa_printf(MSG_DEBUG, "P2P: Could not select "
7251 "2.4 GHz channel for P2P group");
7252 return -1;
7253 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007254 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
7255 "channel: %d MHz", freq);
7256 }
7257 }
7258
7259 if (freq == 5) {
7260 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
7261 "band");
7262 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007263 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007264 wpa_s->best_5_freq)) {
7265 freq = wpa_s->best_5_freq;
7266 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
7267 "channel: %d MHz", freq);
7268 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08007269 const int freqs[] = {
7270 /* operating class 115 */
7271 5180, 5200, 5220, 5240,
7272 /* operating class 124 */
7273 5745, 5765, 5785, 5805,
7274 };
7275 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
7276
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007277 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7278 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08007279
7280 /*
7281 * most of 5G channels are DFS, only operating class 115 and 124
7282 * are available possibly, randomly pick a start to check them.
7283 */
7284 int possible_5g_freqs[] = {
7285 /* operating class 115 */
7286 5180, 5200, 5220, 5240,
7287 /* operating class 124 */
7288 5745, 5765, 5785, 5805,
7289 };
7290 int possible_5g_freqs_num =
7291 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
7292
Jimmy Chen64b90632020-09-29 17:27:34 +08007293 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
7294 if (p2p_supported_freq_go(
7295 wpa_s->global->p2p,
7296 possible_5g_freqs[r % possible_5g_freqs_num])) {
7297 freq = possible_5g_freqs[r % possible_5g_freqs_num];
7298 break;
7299 }
7300 }
7301
7302 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007303 wpa_printf(MSG_DEBUG, "P2P: Could not select "
7304 "5 GHz channel for P2P group");
7305 return -1;
7306 }
7307 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
7308 "channel: %d MHz", freq);
7309 }
7310 }
7311
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007312 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007313 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07007314 ieee80211_is_dfs(freq, wpa_s->hw.modes,
7315 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007316 /*
7317 * If freq is a DFS channel and DFS is offloaded to the
7318 * driver, allow P2P GO to use it.
7319 */
7320 wpa_printf(MSG_DEBUG, "P2P: "
7321 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
7322 __func__, freq);
7323 return freq;
7324 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007325 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
7326 "(%u MHz) is not supported for P2P uses",
7327 freq);
7328 return -1;
7329 }
7330
7331 return freq;
7332}
7333
7334
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007335static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
7336 const struct p2p_channels *channels,
7337 int freq)
7338{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007339 if (is_6ghz_freq(freq) &&
7340 !is_p2p_6ghz_capable(wpa_s->global->p2p))
7341 return 0;
7342
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007343 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
7344 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
7345 freq_included(wpa_s, channels, freq))
7346 return 1;
7347 return 0;
7348}
7349
7350
7351static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
7352 struct p2p_go_neg_results *params,
7353 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007354{
7355 unsigned int i, r;
7356
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007357 /* try all channels in operating class 115 */
7358 for (i = 0; i < 4; i++) {
7359 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08007360 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007361 goto out;
7362 }
7363
7364 /* try all channels in operating class 124 */
7365 for (i = 0; i < 4; i++) {
7366 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08007367 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007368 goto out;
7369 }
7370
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007371 /* try social channel class 180 channel 2 */
7372 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08007373 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007374 goto out;
7375
7376 /* try all channels in reg. class 180 */
7377 for (i = 0; i < 4; i++) {
7378 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08007379 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007380 goto out;
7381 }
7382
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007383 /* try some random selection of the social channels */
7384 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7385 return;
7386
7387 for (i = 0; i < 3; i++) {
7388 params->freq = 2412 + ((r + i) % 3) * 25;
7389 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7390 goto out;
7391 }
7392
7393 /* try all other channels in operating class 81 */
7394 for (i = 0; i < 11; i++) {
7395 params->freq = 2412 + i * 5;
7396
7397 /* skip social channels; covered in the previous loop */
7398 if (params->freq == 2412 ||
7399 params->freq == 2437 ||
7400 params->freq == 2462)
7401 continue;
7402
7403 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
7404 goto out;
7405 }
7406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007407 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08007408 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007409 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007410out:
7411 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
7412 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007413}
7414
7415
Roshan Pius3a1667e2018-07-03 15:17:14 -07007416static int wpas_same_band(int freq1, int freq2)
7417{
7418 enum hostapd_hw_mode mode1, mode2;
7419 u8 chan1, chan2;
7420
7421 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
7422 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
7423 if (mode1 == NUM_HOSTAPD_MODES)
7424 return 0;
7425 return mode1 == mode2;
7426}
7427
7428
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007429static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
7430 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007431 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007432 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007433 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007434 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007435{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007436 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007437 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007438 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007439 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007440 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007441
7442 os_memset(params, 0, sizeof(*params));
7443 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007444 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007445 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007446 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007447 params->max_oper_chwidth = max_oper_chwidth;
7448 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007449 params->edmg = edmg;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007450 params->p2p2 = wpa_s->p2p2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007451
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007452 freqs = os_calloc(wpa_s->num_multichan_concurrent,
7453 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007454 if (!freqs)
7455 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007456
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007457 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00007458 wpa_s->num_multichan_concurrent,
7459 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007460
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007461 if (wpa_s->current_ssid &&
7462 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
7463 wpa_s->wpa_state == WPA_COMPLETED) {
7464 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
7465 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007466
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007467 /*
7468 * If the frequency selection is done for an active P2P GO that
7469 * is not sharing a frequency, allow to select a new frequency
7470 * even if there are no unused frequencies as we are about to
7471 * move the P2P GO so its frequency can be re-used.
7472 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007473 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007474 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
7475 freqs[i].flags == 0) {
7476 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007477 break;
7478 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007479 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08007480 }
7481
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007482 /* Try to use EDMG channel */
7483 if (params->edmg) {
7484 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
7485 goto success;
7486 params->edmg = 0;
7487 }
7488
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007489 /* try using the forced freq */
7490 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007491 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
7492 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007493 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007494 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007495 freq);
7496 goto fail;
7497 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007498 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
7499 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07007500 ieee80211_is_dfs(freq, wpa_s->hw.modes,
7501 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007502 /*
7503 * If freq is a DFS channel and DFS is offloaded
7504 * to the driver, allow P2P GO to use it.
7505 */
7506 wpa_printf(MSG_DEBUG,
7507 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
7508 __func__, freq);
7509 } else {
7510 wpa_printf(MSG_DEBUG,
7511 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
7512 freq);
7513 goto fail;
7514 }
7515 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007516
7517 for (i = 0; i < num; i++) {
7518 if (freqs[i].freq == freq) {
7519 wpa_printf(MSG_DEBUG,
7520 "P2P: forced freq (%d MHz) is also shared",
7521 freq);
7522 params->freq = freq;
7523 goto success;
7524 }
7525 }
7526
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007527 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007528 wpa_printf(MSG_DEBUG,
7529 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
7530 freq);
7531 goto fail;
7532 }
7533
7534 wpa_printf(MSG_DEBUG,
7535 "P2P: force GO freq (%d MHz) on a free channel",
7536 freq);
7537 params->freq = freq;
7538 goto success;
7539 }
7540
7541 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007542 if (num &&
7543 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007544 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
7545 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7546 wpa_printf(MSG_DEBUG,
7547 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007548 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007549 params->freq = cand;
7550 goto success;
7551 }
7552
7553 /* try using one of the shared freqs */
7554 for (i = 0; i < num; i++) {
7555 if (wpas_p2p_supported_freq_go(wpa_s, channels,
7556 freqs[i].freq)) {
7557 wpa_printf(MSG_DEBUG,
7558 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007559 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007560 params->freq = freqs[i].freq;
7561 goto success;
7562 }
7563 }
7564 }
7565
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08007566 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007567 wpa_printf(MSG_DEBUG,
7568 "P2P: Cannot force GO on any of the channels we are already using");
7569 goto fail;
7570 }
7571
7572 /* try using the setting from the configuration file */
7573 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
7574 wpa_s->conf->p2p_oper_channel >= 1 &&
7575 wpa_s->conf->p2p_oper_channel <= 11 &&
7576 wpas_p2p_supported_freq_go(
7577 wpa_s, channels,
7578 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
7579 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
7580 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
7581 "frequency %d MHz", params->freq);
7582 goto success;
7583 }
7584
7585 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
7586 wpa_s->conf->p2p_oper_reg_class == 116 ||
7587 wpa_s->conf->p2p_oper_reg_class == 117 ||
7588 wpa_s->conf->p2p_oper_reg_class == 124 ||
7589 wpa_s->conf->p2p_oper_reg_class == 125 ||
7590 wpa_s->conf->p2p_oper_reg_class == 126 ||
7591 wpa_s->conf->p2p_oper_reg_class == 127) &&
7592 wpas_p2p_supported_freq_go(wpa_s, channels,
7593 5000 +
7594 5 * wpa_s->conf->p2p_oper_channel)) {
7595 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
7596 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
7597 "frequency %d MHz", params->freq);
7598 goto success;
7599 }
7600
7601 /* Try using best channels */
7602 if (wpa_s->conf->p2p_oper_channel == 0 &&
7603 wpa_s->best_overall_freq > 0 &&
7604 wpas_p2p_supported_freq_go(wpa_s, channels,
7605 wpa_s->best_overall_freq)) {
7606 params->freq = wpa_s->best_overall_freq;
7607 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
7608 "channel %d MHz", params->freq);
7609 goto success;
7610 }
7611
7612 if (wpa_s->conf->p2p_oper_channel == 0 &&
7613 wpa_s->best_24_freq > 0 &&
7614 wpas_p2p_supported_freq_go(wpa_s, channels,
7615 wpa_s->best_24_freq)) {
7616 params->freq = wpa_s->best_24_freq;
7617 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
7618 "channel %d MHz", params->freq);
7619 goto success;
7620 }
7621
7622 if (wpa_s->conf->p2p_oper_channel == 0 &&
7623 wpa_s->best_5_freq > 0 &&
7624 wpas_p2p_supported_freq_go(wpa_s, channels,
7625 wpa_s->best_5_freq)) {
7626 params->freq = wpa_s->best_5_freq;
7627 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
7628 "channel %d MHz", params->freq);
7629 goto success;
7630 }
7631
7632 /* try using preferred channels */
7633 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
7634 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7635 params->freq = cand;
7636 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
7637 "channels", params->freq);
7638 goto success;
7639 }
7640
Roshan Pius3a1667e2018-07-03 15:17:14 -07007641 /* Try using a channel that allows VHT to be used with 80 MHz */
7642 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
7643 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7644 enum hostapd_hw_mode mode;
7645 struct hostapd_hw_modes *hwmode;
7646 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08007647 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007648
7649 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08007650 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007651 mode = ieee80211_freq_to_chan(cand, &chan);
7652 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07007653 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07007654 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08007655 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
7656 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007657 continue;
7658 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7659 params->freq = cand;
7660 wpa_printf(MSG_DEBUG,
7661 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
7662 params->freq);
7663 goto success;
7664 }
7665 }
7666 }
7667
7668 /* Try using a channel that allows HT to be used with 40 MHz on the same
7669 * band so that CSA can be used */
7670 if (wpa_s->current_ssid && wpa_s->hw.modes &&
7671 wpa_s->p2p_group_common_freqs) {
7672 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7673 enum hostapd_hw_mode mode;
7674 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08007675 u8 chan, op_class;
7676 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07007677
Hai Shalomc1a21442022-02-04 13:43:00 -08007678 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007679 cand = wpa_s->p2p_group_common_freqs[i];
7680 mode = ieee80211_freq_to_chan(cand, &chan);
7681 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08007682 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07007683 if (!wpas_same_band(wpa_s->current_ssid->frequency,
7684 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08007685 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007686 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08007687 if (is_6ghz &&
7688 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
7689 BW40) == ALLOWED)
7690 supported = true;
7691
7692 if (!is_6ghz &&
7693 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00007694 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
7695 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08007696 wpas_p2p_verify_channel(
7697 wpa_s, hwmode, op_class, chan,
7698 BW40MINUS) == ALLOWED)
7699 supported = true;
7700
7701 if (!supported && !is_6ghz &&
7702 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00007703 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
7704 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08007705 wpas_p2p_verify_channel(
7706 wpa_s, hwmode, op_class, chan,
7707 BW40PLUS) == ALLOWED)
7708 supported = true;
7709
7710 if (!supported)
7711 continue;
7712
Roshan Pius3a1667e2018-07-03 15:17:14 -07007713 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7714 params->freq = cand;
7715 wpa_printf(MSG_DEBUG,
7716 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
7717 params->freq);
7718 goto success;
7719 }
7720 }
7721 }
7722
7723 /* Try using one of the group common freqs on the same band so that CSA
7724 * can be used */
7725 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
7726 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7727 cand = wpa_s->p2p_group_common_freqs[i];
7728 if (!wpas_same_band(wpa_s->current_ssid->frequency,
7729 cand))
7730 continue;
7731 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7732 params->freq = cand;
7733 wpa_printf(MSG_DEBUG,
7734 "P2P: Use freq %d MHz common with the peer and maintaining same band",
7735 params->freq);
7736 goto success;
7737 }
7738 }
7739 }
7740
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007741 /* Try using one of the group common freqs */
7742 if (wpa_s->p2p_group_common_freqs) {
7743 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
7744 cand = wpa_s->p2p_group_common_freqs[i];
7745 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
7746 params->freq = cand;
7747 wpa_printf(MSG_DEBUG,
7748 "P2P: Use freq %d MHz common with the peer",
7749 params->freq);
7750 goto success;
7751 }
7752 }
7753 }
7754
7755 /* no preference, select some channel */
7756 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
7757
7758 if (params->freq == 0) {
7759 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
7760 goto fail;
7761 }
7762
7763success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007764 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007765 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007766fail:
7767 os_free(freqs);
7768 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769}
7770
7771
7772static struct wpa_supplicant *
7773wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
7774 int go)
7775{
7776 struct wpa_supplicant *group_wpa_s;
7777
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007778 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007779 if (wpa_s->p2p_mgmt) {
7780 /*
7781 * We may be called on the p2p_dev interface which
7782 * cannot be used for group operations, so always use
7783 * the primary interface.
7784 */
7785 wpa_s->parent->p2pdev = wpa_s;
7786 wpa_s = wpa_s->parent;
7787 }
7788 wpa_dbg(wpa_s, MSG_DEBUG,
7789 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07007790 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007791 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007792 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007793 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007795
7796 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007797 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007798 wpa_msg_global(wpa_s, MSG_ERROR,
7799 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007800 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007801 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007802 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
7803 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007804 wpa_msg_global(wpa_s, MSG_ERROR,
7805 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007806 wpas_p2p_remove_pending_group_interface(wpa_s);
7807 return NULL;
7808 }
7809
Roshan Pius3a1667e2018-07-03 15:17:14 -07007810 if (go && wpa_s->p2p_go_do_acs) {
7811 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
7812 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
7813 wpa_s->p2p_go_do_acs = 0;
7814 }
7815
Hai Shalomc1a21442022-02-04 13:43:00 -08007816 if (go && wpa_s->p2p_go_allow_dfs) {
7817 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
7818 wpa_s->p2p_go_allow_dfs = 0;
7819 }
7820
Dmitry Shmidtaa532512012-09-24 10:35:31 -07007821 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
7822 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007823 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007824 return group_wpa_s;
7825}
7826
7827
7828/**
7829 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
7830 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
7831 * @persistent_group: Whether to create a persistent group
7832 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007833 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007834 * @ht40: Start GO with 40 MHz channel width
7835 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007836 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007837 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08007838 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007839 * @p2p_mode: Operation mode for GO (R1/R2/PCC)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007840 * Returns: 0 on success, -1 on failure
7841 *
7842 * This function creates a new P2P group with the local end as the Group Owner,
7843 * i.e., without using Group Owner Negotiation.
7844 */
7845int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007846 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08007847 int max_oper_chwidth, int he, int edmg,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007848 bool allow_6ghz, bool p2p2, enum wpa_p2p_mode p2p_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007849{
7850 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007851 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852
7853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7854 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007855 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7856 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007857
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007858 os_free(wpa_s->global->add_psk);
7859 wpa_s->global->add_psk = NULL;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007860 wpa_s->p2p2 = p2p2;
7861 wpa_s->p2p_mode = p2p_mode;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007862
Dmitry Shmidtdca39792011-09-06 11:17:33 -07007863 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007864 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007865 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07007866
Roshan Pius3a1667e2018-07-03 15:17:14 -07007867 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00007868 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
7869 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007870 return -1;
7871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007872
Sunil Ravi99c035e2024-07-12 01:42:03 +00007873 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq,
7874 vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007875 ht40, vht, max_oper_chwidth, he, edmg,
7876 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007877 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007879 p2p_go_params(wpa_s->global->p2p, &params);
7880 params.persistent_group = persistent_group;
7881
7882 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7883 if (wpa_s == NULL)
7884 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007885 if (freq > 0)
7886 wpa_s->p2p_go_no_pri_sec_switch = 1;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007887 params.p2p2 = wpa_s->p2p2;
7888 wpas_start_go(wpa_s, &params, 0, p2p_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007889
7890 return 0;
7891}
7892
7893
7894static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007895 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007896 int freq, int force_scan, int retry_limit,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007897 const u8 *go_bssid, bool p2p2, const u8 *pmkid,
7898 const u8 *pmk, size_t pmk_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007899{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007900 struct os_reltime now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007901 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007902 int other_iface_found = 0;
7903 struct wpa_supplicant *ifs;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007904 struct rsn_pmksa_cache_entry *entry;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007905
7906 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7907 if (wpa_s == NULL)
7908 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007909 if (force_scan)
7910 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007911 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007912
7913 wpa_supplicant_ap_deinit(wpa_s);
7914
7915 ssid = wpa_config_add_network(wpa_s->conf);
7916 if (ssid == NULL)
7917 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007918 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007919 wpa_config_set_network_defaults(ssid);
7920 ssid->temporary = 1;
7921 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007922 ssid->pbss = params->pbss;
7923 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7924 WPA_CIPHER_CCMP;
7925 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007926 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007927 if (is_6ghz_freq(freq) &&
7928 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7929 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7930 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7931 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7932 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7933 wpa_dbg(wpa_s, MSG_DEBUG,
7934 "P2P: Enable SAE auth_alg and key_mgmt");
7935 }
7936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007937 ssid->ssid = os_malloc(params->ssid_len);
7938 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007939 wpa_config_remove_network(wpa_s->conf, ssid->id);
7940 return -1;
7941 }
7942 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7943 ssid->ssid_len = params->ssid_len;
7944 ssid->p2p_group = 1;
7945 ssid->export_keys = 1;
7946 if (params->psk_set) {
7947 os_memcpy(ssid->psk, params->psk, 32);
7948 ssid->psk_set = 1;
7949 }
7950 if (params->passphrase)
7951 ssid->passphrase = os_strdup(params->passphrase);
7952
Sunil Ravi036cec52023-03-29 11:35:17 -07007953 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007954 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007955 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007956 }
7957
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00007958 if (p2p2) {
7959 ssid->key_mgmt = WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PASN;
7960 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
7961 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7962 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
7963 ssid->disabled = 0;
7964
7965 if (pmk && pmk_len && pmkid) {
7966 entry = os_zalloc(sizeof(*entry));
7967 if (!entry)
7968 return -1;
7969 os_memcpy(entry->aa, ssid->bssid, ETH_ALEN);
7970 os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
7971 entry->pmk_len = pmk_len;
7972 os_memcpy(entry->pmk, pmk, pmk_len);
7973 entry->akmp = WPA_KEY_MGMT_SAE;
7974 os_get_reltime(&now);
7975 entry->expiration = now.sec + 43200;
7976 entry->reauth_time = now.sec + 43200 * 70 / 100;
7977 entry->network_ctx = ssid;
7978 os_memcpy(entry->spa, wpa_s->own_addr, ETH_ALEN);
7979
7980 wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
7981 ssid->pmk_valid = true;
7982 }
7983 wpa_s->current_ssid = ssid;
7984 }
7985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007986 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007987 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007988 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007989 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007990 wpa_s->p2p_go_group_formation_completed = 0;
7991 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007992
Matthew Wang9ae940b2022-09-14 21:25:34 -07007993 /*
7994 * Get latest scan results from driver in case cached scan results from
7995 * interfaces on the same wiphy allow us to skip the next scan by fast
7996 * associating. Also update the scan time to the most recent scan result
7997 * fetch time on the same radio so it reflects the actual time the last
7998 * scan result event occurred.
7999 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00008000 wpa_supplicant_update_scan_results(wpa_s, go_bssid);
Matthew Wang9ae940b2022-09-14 21:25:34 -07008001 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8002 radio_list) {
8003 if (ifs == wpa_s)
8004 continue;
8005 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
8006 &ifs->last_scan)) {
8007 other_iface_found = 1;
8008 wpa_s->last_scan.sec = ifs->last_scan.sec;
8009 wpa_s->last_scan.usec = ifs->last_scan.usec;
8010 }
8011 }
8012
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008013 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07008014 NULL);
8015 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
8016 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008017 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08008018 wpa_supplicant_select_network(wpa_s, ssid);
8019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008020 return 0;
8021}
8022
8023
8024int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
8025 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008026 int force_freq, int neg_freq,
8027 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08008028 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008029 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008030 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08008031 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00008032 bool allow_6ghz, int retry_limit,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008033 const u8 *go_bssid, const u8 *dev_addr,
8034 const u8 *pmkid, const u8 *pmk,
8035 size_t pmk_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008036{
8037 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08008038 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008039
8040 if (ssid->disabled != 2 || ssid->ssid == NULL)
8041 return -1;
8042
8043 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
8044 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
8045 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
8046 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008047 if (go == 0 &&
8048 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008049 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008050 /*
8051 * This can happen if Invitation Response frame was lost
8052 * and the peer (GO of a persistent group) tries to
8053 * invite us again. Reschedule the timeout to avoid
8054 * terminating the wait for the connection too early
8055 * since we now know that the peer is still trying to
8056 * invite us instead of having already started the GO.
8057 */
8058 wpa_printf(MSG_DEBUG,
8059 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
8060 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
8061 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008062 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07008063 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008064 return 0;
8065 }
8066
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008067 os_free(wpa_s->global->add_psk);
8068 wpa_s->global->add_psk = NULL;
8069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008070 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008071 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008072
Dmitry Shmidt04949592012-07-19 12:16:46 -07008073 wpa_s->p2p_fallback_to_go_neg = 0;
8074
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008075 if (ssid->mode == WPAS_MODE_P2P_GO) {
8076 if (force_freq > 0) {
8077 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
8078 if (freq < 0)
8079 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00008080 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008081 } else {
8082 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
8083 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008084 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008085 freq = 0;
8086 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008087 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008088 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008089 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008090 struct os_reltime now;
8091 struct wpa_bss *bss =
8092 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07008093
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008094 os_get_reltime(&now);
8095 if (bss &&
8096 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008097 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008098 freq = bss->freq;
8099 else
8100 freq = 0;
8101 }
8102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008103 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008104 force_scan, retry_limit, go_bssid,
8105 wpa_s->p2p2, pmkid, pmk, pmk_len);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008106 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07008107 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07008108 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07008109
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008110 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008111 ht40, vht, max_oper_chwidth, he, edmg,
8112 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008113 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008114
8115 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008116 params.psk_set = ssid->psk_set;
8117 if (params.psk_set)
8118 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008119 if (ssid->passphrase) {
8120 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
8121 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
8122 "persistent group");
8123 return -1;
8124 }
8125 os_strlcpy(params.passphrase, ssid->passphrase,
8126 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008128 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
8129 params.ssid_len = ssid->ssid_len;
8130 params.persistent_group = 1;
8131
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008132 if (wpa_s->p2p2 && pmk_len && pmk && pmkid) {
8133 os_memcpy(params.peer_device_addr, dev_addr, ETH_ALEN);
8134 os_memcpy(params.pmkid, pmkid, PMKID_LEN);
8135 os_memcpy(params.pmk, pmk, pmk_len);
8136 params.pmk_len = pmk_len;
8137 params.akmp = WPA_KEY_MGMT_SAE;
8138 params.p2p2 = true;
8139 }
8140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008141 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
8142 if (wpa_s == NULL)
8143 return -1;
8144
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008145 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
8146
Dmitry Shmidt56052862013-10-04 10:23:25 -07008147 wpa_s->p2p_first_connection_timeout = connection_timeout;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008148 params.p2p2 = wpa_s->p2p2;
8149 wpas_start_go(wpa_s, &params, 0, wpa_s->p2p_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008150
8151 return 0;
8152}
8153
8154
8155static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
8156 struct wpabuf *proberesp_ies)
8157{
8158 struct wpa_supplicant *wpa_s = ctx;
8159 if (wpa_s->ap_iface) {
8160 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008161 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
8162 wpabuf_free(beacon_ies);
8163 wpabuf_free(proberesp_ies);
8164 return;
8165 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008166 if (beacon_ies) {
8167 wpabuf_free(hapd->p2p_beacon_ie);
8168 hapd->p2p_beacon_ie = beacon_ies;
8169 }
8170 wpabuf_free(hapd->p2p_probe_resp_ie);
8171 hapd->p2p_probe_resp_ie = proberesp_ies;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008172
8173 if (wpa_s->p2p2) {
8174 hapd->iconf->peer_to_peer_twt = true;
8175 hapd->iconf->channel_usage = true;
8176 }
8177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008178 } else {
8179 wpabuf_free(beacon_ies);
8180 wpabuf_free(proberesp_ies);
8181 }
8182 wpa_supplicant_ap_update_beacon(wpa_s);
8183}
8184
8185
8186static void wpas_p2p_idle_update(void *ctx, int idle)
8187{
8188 struct wpa_supplicant *wpa_s = ctx;
8189 if (!wpa_s->ap_iface)
8190 return;
8191 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008192 if (idle) {
8193 if (wpa_s->global->p2p_fail_on_wps_complete &&
8194 wpa_s->p2p_in_provisioning) {
8195 wpas_p2p_grpform_fail_after_wps(wpa_s);
8196 return;
8197 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008198 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008199 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008200 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
8201}
8202
8203
8204struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008205 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008206{
8207 struct p2p_group *group;
8208 struct p2p_group_config *cfg;
8209
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008210 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
8211 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008212 return NULL;
8213
8214 cfg = os_zalloc(sizeof(*cfg));
8215 if (cfg == NULL)
8216 return NULL;
8217
Dmitry Shmidt04949592012-07-19 12:16:46 -07008218 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008219 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008220 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008221 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008222 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
8223 if (wpa_s->max_stations &&
8224 wpa_s->max_stations < wpa_s->conf->max_num_sta)
8225 cfg->max_clients = wpa_s->max_stations;
8226 else
8227 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008228 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
8229 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008230 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008231 cfg->cb_ctx = wpa_s;
8232 cfg->ie_update = wpas_p2p_ie_update;
8233 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07008234 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
8235 != 0;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008236 cfg->p2p2 = wpa_s->p2p2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008237
8238 group = p2p_group_init(wpa_s->global->p2p, cfg);
8239 if (group == NULL)
8240 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008241 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008242 p2p_group_notif_formation_done(group);
8243 wpa_s->p2p_group = group;
8244 return group;
8245}
8246
8247
8248void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
8249 int registrar)
8250{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008251 struct wpa_ssid *ssid = wpa_s->current_ssid;
8252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008253 if (!wpa_s->p2p_in_provisioning) {
8254 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
8255 "provisioning not in progress");
8256 return;
8257 }
8258
Dmitry Shmidt04949592012-07-19 12:16:46 -07008259 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
8260 u8 go_dev_addr[ETH_ALEN];
8261 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
8262 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
8263 ssid->ssid_len);
8264 /* Clear any stored provisioning info */
8265 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
8266 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008267
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008268 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008269 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07008270 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008271 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
8272 /*
8273 * Use a separate timeout for initial data connection to
8274 * complete to allow the group to be removed automatically if
8275 * something goes wrong in this step before the P2P group idle
8276 * timeout mechanism is taken into use.
8277 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07008278 wpa_dbg(wpa_s, MSG_DEBUG,
8279 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
8280 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008281 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
8282 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008283 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008284 /* Complete group formation on successful data connection. */
8285 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008286 } else if (ssid) {
8287 /*
8288 * Use a separate timeout for initial data connection to
8289 * complete to allow the group to be removed automatically if
8290 * the client does not complete data connection successfully.
8291 */
8292 wpa_dbg(wpa_s, MSG_DEBUG,
8293 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
8294 P2P_MAX_INITIAL_CONN_WAIT_GO);
8295 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
8296 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008297 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07008298 /*
8299 * Complete group formation on first successful data connection
8300 */
8301 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008303 if (wpa_s->global->p2p)
8304 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008305 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008306}
8307
8308
Jouni Malinen75ecf522011-06-27 15:19:46 -07008309void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
8310 struct wps_event_fail *fail)
8311{
8312 if (!wpa_s->p2p_in_provisioning) {
8313 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
8314 "provisioning not in progress");
8315 return;
8316 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008317
8318 if (wpa_s->go_params) {
8319 p2p_clear_provisioning_info(
8320 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008321 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008322 }
8323
Jouni Malinen75ecf522011-06-27 15:19:46 -07008324 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008325
8326 if (wpa_s == wpa_s->global->p2p_group_formation) {
8327 /*
8328 * Allow some time for the failed WPS negotiation exchange to
8329 * complete, but remove the group since group formation cannot
8330 * succeed after provisioning failure.
8331 */
8332 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
8333 wpa_s->global->p2p_fail_on_wps_complete = 1;
8334 eloop_deplete_timeout(0, 50000,
8335 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008336 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008337 }
8338}
8339
8340
8341int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
8342{
8343 if (!wpa_s->global->p2p_fail_on_wps_complete ||
8344 !wpa_s->p2p_in_provisioning)
8345 return 0;
8346
8347 wpas_p2p_grpform_fail_after_wps(wpa_s);
8348
8349 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008350}
8351
8352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008353int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008354 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008355 enum wpas_p2p_prov_disc_use use,
8356 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008357{
8358 u16 config_methods;
8359
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008360 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008361 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008362 wpa_s->p2p_fallback_to_go_neg = 0;
8363 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008364 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
8365 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008366 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
8367 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
8368
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008369 wpa_printf(MSG_DEBUG,
8370 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
8371 __func__, p2ps_prov->conncap,
8372 p2ps_prov->adv_id, p2ps_prov->conncap,
8373 p2ps_prov->status, p2ps_prov->info);
8374
8375 config_methods = 0;
8376 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008377 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008378 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008379 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008380 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
8381 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008382 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008383 else {
8384 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008385 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008386 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008387 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388
Dmitry Shmidt04949592012-07-19 12:16:46 -07008389 if (use == WPAS_P2P_PD_AUTO) {
8390 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
8391 wpa_s->pending_pd_config_methods = config_methods;
8392 wpa_s->p2p_auto_pd = 1;
8393 wpa_s->p2p_auto_join = 0;
8394 wpa_s->pending_pd_before_join = 0;
8395 wpa_s->auto_pd_scan_retry = 0;
8396 wpas_p2p_stop_find(wpa_s);
8397 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008398 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008399 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
8400 wpa_s->p2p_auto_started.sec,
8401 wpa_s->p2p_auto_started.usec);
8402 wpas_p2p_join_scan(wpa_s, NULL);
8403 return 0;
8404 }
8405
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008406 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
8407 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008408 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008410
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008411 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008412 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008413 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008414}
8415
8416
8417int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
8418 char *end)
8419{
8420 return p2p_scan_result_text(ies, ies_len, buf, end);
8421}
8422
8423
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008424static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s,
8425 bool force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008426{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008427 if (!offchannel_pending_action_tx(wpa_s) && !force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008428 return;
8429
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008430 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008431 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008432 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
8433 wpa_s, NULL);
8434 offchannel_send_action_done(wpa_s);
8435 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07008436
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008437 if (!offchannel_pending_action_tx(wpa_s))
8438 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008439 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
8440 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008441 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008442}
8443
8444
8445int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
8446 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008447 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008448 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08008449 u8 seek_cnt, const char **seek_string, int freq,
8450 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008451{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008452 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07008453 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008454
Dmitry Shmidt04949592012-07-19 12:16:46 -07008455 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008456 wpa_s->p2p_in_provisioning) {
8457 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
8458 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
8459 " (P2P disabled)" : "",
8460 wpa_s->p2p_in_provisioning ?
8461 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008462 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07008463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008464
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008465 wpa_supplicant_cancel_sched_scan(wpa_s);
8466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008467 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008468 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08008469 search_delay, seek_cnt, seek_string, freq,
8470 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008471}
8472
8473
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008474static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
8475 struct wpa_scan_results *scan_res)
8476{
8477 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8478
8479 if (wpa_s->p2p_scan_work) {
8480 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
8481 wpa_s->p2p_scan_work = NULL;
8482 radio_work_done(work);
8483 }
8484
8485 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8486 return;
8487
8488 /*
8489 * Indicate that results have been processed so that the P2P module can
8490 * continue pending tasks.
8491 */
Hai Shalom60840252021-02-19 19:02:11 -08008492 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008493}
8494
8495
8496static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008497{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008498 wpas_p2p_clear_pending_action_tx(wpa_s, true);
Hai Shalomfdcde762020-04-02 11:19:20 -07008499 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008500 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
8501 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008502
8503 if (wpa_s->global->p2p)
8504 p2p_stop_find(wpa_s->global->p2p);
8505
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008506 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
8507 wpa_printf(MSG_DEBUG,
8508 "P2P: Do not consider the scan results after stop_find");
8509 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
8510 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008511}
8512
8513
8514void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
8515{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008516 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008517 if (!wpa_s->global->pending_group_iface_for_p2ps)
8518 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008519}
8520
8521
8522static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
8523{
8524 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07008525 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008526}
8527
8528
8529int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
8530{
8531 int res;
8532
8533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8534 return -1;
8535
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008536 if (wpa_s->p2p_lo_started) {
8537 wpa_printf(MSG_DEBUG,
8538 "P2P: Cannot start P2P listen, it is offloaded");
8539 return -1;
8540 }
8541
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008542 wpa_supplicant_cancel_sched_scan(wpa_s);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008543 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008544
8545 if (timeout == 0) {
8546 /*
8547 * This is a request for unlimited Listen state. However, at
8548 * least for now, this is mapped to a Listen state for one
8549 * hour.
8550 */
8551 timeout = 3600;
8552 }
8553 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07008554 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008556 /*
8557 * Stop previous find/listen operation to avoid trying to request a new
8558 * remain-on-channel operation while the driver is still running the
8559 * previous one.
8560 */
8561 if (wpa_s->global->p2p)
8562 p2p_stop_find(wpa_s->global->p2p);
8563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008564 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
8565 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07008566 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008567 eloop_register_timeout(timeout, 0,
8568 wpas_p2p_long_listen_timeout,
8569 wpa_s, NULL);
8570 }
8571
8572 return res;
8573}
8574
8575
8576int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
8577 u8 *buf, size_t len, int p2p_group)
8578{
8579 struct wpabuf *p2p_ie;
8580 int ret;
8581
8582 if (wpa_s->global->p2p_disabled)
8583 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008584 /*
8585 * Advertize mandatory cross connection capability even on
8586 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
8587 */
8588 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008589 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008590 if (wpa_s->global->p2p == NULL)
8591 return -1;
8592 if (bss == NULL)
8593 return -1;
8594
8595 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
8596 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
8597 p2p_group, p2p_ie);
8598 wpabuf_free(p2p_ie);
8599
8600 return ret;
8601}
8602
8603
8604int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008605 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008606 const u8 *ie, size_t ie_len,
8607 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008608{
8609 if (wpa_s->global->p2p_disabled)
8610 return 0;
8611 if (wpa_s->global->p2p == NULL)
8612 return 0;
8613
Dmitry Shmidt04949592012-07-19 12:16:46 -07008614 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008615 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008616 case P2P_PREQ_NOT_P2P:
8617 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
8618 ssi_signal);
8619 /* fall through */
8620 case P2P_PREQ_MALFORMED:
8621 case P2P_PREQ_NOT_LISTEN:
8622 case P2P_PREQ_NOT_PROCESSED:
8623 default: /* make gcc happy */
8624 return 0;
8625 case P2P_PREQ_PROCESSED:
8626 return 1;
8627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008628}
8629
8630
8631void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
8632 const u8 *sa, const u8 *bssid,
8633 u8 category, const u8 *data, size_t len, int freq)
8634{
8635 if (wpa_s->global->p2p_disabled)
8636 return;
8637 if (wpa_s->global->p2p == NULL)
8638 return;
8639
8640 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
8641 freq);
8642}
8643
8644
8645void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
8646{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008647 unsigned int bands;
8648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008649 if (wpa_s->global->p2p_disabled)
8650 return;
8651 if (wpa_s->global->p2p == NULL)
8652 return;
8653
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008654 bands = wpas_get_bands(wpa_s, NULL);
8655 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008656}
8657
8658
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008659static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008660{
8661 p2p_group_deinit(wpa_s->p2p_group);
8662 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008663
8664 wpa_s->ap_configured_cb = NULL;
8665 wpa_s->ap_configured_cb_ctx = NULL;
8666 wpa_s->ap_configured_cb_data = NULL;
8667 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008668}
8669
8670
8671int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
8672{
Hai Shalomfdcde762020-04-02 11:19:20 -07008673 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008675 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8676 return -1;
8677
8678 return p2p_reject(wpa_s->global->p2p, addr);
8679}
8680
8681
8682/* Invite to reinvoke a persistent group */
8683int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03008684 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008685 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008686 int pref_freq, int he, int edmg, bool allow_6ghz, bool p2p2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008687{
8688 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008689 u8 *bssid = NULL;
8690 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008691 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08008692 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00008693 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
8694 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008695
Hai Shalomc1a21442022-02-04 13:43:00 -08008696 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
8697 return -1;
8698
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008699 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008700 if (peer_addr)
8701 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
8702 else
8703 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008704
Jouni Malinen31be0a42012-08-31 21:20:51 +03008705 wpa_s->p2p_persistent_go_freq = freq;
8706 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008707 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08008708 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008709 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
8710 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008711 wpa_s->p2p_go_edmg = !!edmg;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008712 wpa_s->p2p2 = p2p2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008713 if (ssid->mode == WPAS_MODE_P2P_GO) {
8714 role = P2P_INVITE_ROLE_GO;
8715 if (peer_addr == NULL) {
8716 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
8717 "address in invitation command");
8718 return -1;
8719 }
8720 if (wpas_p2p_create_iface(wpa_s)) {
8721 if (wpas_p2p_add_group_interface(wpa_s,
8722 WPA_IF_P2P_GO) < 0) {
8723 wpa_printf(MSG_ERROR, "P2P: Failed to "
8724 "allocate a new interface for the "
8725 "group");
8726 return -1;
8727 }
8728 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008729 } else if (wpa_s->p2p_mgmt)
8730 bssid = wpa_s->parent->own_addr;
8731 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008732 bssid = wpa_s->own_addr;
8733 } else {
8734 role = P2P_INVITE_ROLE_CLIENT;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008735 if (!wpa_s->p2p2)
8736 peer_addr = ssid->bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008737 }
8738 wpa_s->pending_invite_ssid_id = ssid->id;
8739
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008740 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008741 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008742 role == P2P_INVITE_ROLE_GO,
8743 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008744 if (res)
8745 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07008746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008747 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8748 return -1;
8749
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08008750 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
8751
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08008752 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
8753 no_pref_freq_given && pref_freq > 0 &&
8754 wpa_s->num_multichan_concurrent > 1 &&
8755 wpas_p2p_num_unused_channels(wpa_s) > 0) {
8756 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
8757 pref_freq);
8758 pref_freq = 0;
8759 }
8760
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008761 /*
8762 * Stop any find/listen operations before invitation and possibly
8763 * connection establishment.
8764 */
8765 wpas_p2p_stop_find_oper(wpa_s);
8766
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008767#ifdef CONFIG_PASN
8768 if (p2p2) {
8769 if (wpas_p2p_initiate_pasn_verify(wpa_s, peer_addr, role, bssid,
8770 ssid->ssid, ssid->ssid_len,
8771 force_freq, go_dev_addr,
8772 pref_freq) < 0) {
8773 if (wpa_s->create_p2p_iface)
8774 wpas_p2p_remove_pending_group_interface(wpa_s);
8775 return -1;
8776 }
8777 return 0;
8778 }
8779#endif /* CONFIG_PASN */
8780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008781 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008782 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008783 1, pref_freq, -1, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008784}
8785
8786
8787/* Invite to join an active group */
8788int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08008789 const u8 *peer_addr, const u8 *go_dev_addr,
8790 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008791{
8792 struct wpa_global *global = wpa_s->global;
8793 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008794 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008795 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008796 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008797 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008798 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00008799 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
8800 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008801
Jouni Malinen31be0a42012-08-31 21:20:51 +03008802 wpa_s->p2p_persistent_go_freq = 0;
8803 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008804 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008805 wpa_s->p2p_go_vht_center_freq2 = 0;
8806 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008807 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03008808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008809 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8810 if (os_strcmp(wpa_s->ifname, ifname) == 0)
8811 break;
8812 }
8813 if (wpa_s == NULL) {
8814 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
8815 return -1;
8816 }
8817
8818 ssid = wpa_s->current_ssid;
8819 if (ssid == NULL) {
8820 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
8821 "invitation");
8822 return -1;
8823 }
8824
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008825 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008826 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008827 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008828 ssid->ssid, ssid->ssid_len);
8829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008830 if (ssid->mode == WPAS_MODE_P2P_GO) {
8831 role = P2P_INVITE_ROLE_ACTIVE_GO;
8832 bssid = wpa_s->own_addr;
8833 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008834 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008835 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008836 } else {
8837 role = P2P_INVITE_ROLE_CLIENT;
8838 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
8839 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
8840 "invite to current group");
8841 return -1;
8842 }
8843 bssid = wpa_s->bssid;
8844 if (go_dev_addr == NULL &&
8845 !is_zero_ether_addr(wpa_s->go_dev_addr))
8846 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07008847 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
8848 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008849 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008850 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008852 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8853 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08008854 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
8855 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008856
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008857 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008858 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008859 role == P2P_INVITE_ROLE_ACTIVE_GO,
8860 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008861 if (res)
8862 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07008863 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008865 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08008866 ssid->ssid, ssid->ssid_len, force_freq,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008867 go_dev_addr, persistent, pref_freq, -1, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008868}
8869
8870
8871void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
8872{
8873 struct wpa_ssid *ssid = wpa_s->current_ssid;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008874 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008875 u8 go_dev_addr[ETH_ALEN];
8876 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008877 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07008878 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008879 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008880
Dmitry Shmidt04949592012-07-19 12:16:46 -07008881 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
8882 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008883 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008884 }
8885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008886 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008887 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008888
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008889 if (wpa_s->go_params)
8890 bssid = wpa_s->go_params->peer_interface_addr;
8891 else
8892 bssid = wpa_s->bssid;
8893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008894 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008895 if (!wpa_s->p2p_go_group_formation_completed &&
8896 wpa_s->global->p2p_group_formation == wpa_s) {
8897 wpa_dbg(wpa_s, MSG_DEBUG,
8898 "P2P: Marking group formation completed on client on data connection");
8899 wpa_s->p2p_go_group_formation_completed = 1;
8900 wpa_s->global->p2p_group_formation = NULL;
8901 wpa_s->p2p_in_provisioning = 0;
8902 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008903 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008906 os_memset(go_dev_addr, 0, ETH_ALEN);
8907 if (ssid->bssid_set)
8908 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
8909 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
8910 ssid->ssid_len);
8911 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
8912
8913 if (wpa_s->global->p2p_group_formation == wpa_s)
8914 wpa_s->global->p2p_group_formation = NULL;
8915
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008916 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
8917 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008918
8919 ip_addr[0] = '\0';
8920 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008921 int res;
8922
8923 res = os_snprintf(ip_addr, sizeof(ip_addr),
8924 " ip_addr=%u.%u.%u.%u "
8925 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
8926 ip[0], ip[1], ip[2], ip[3],
8927 ip[4], ip[5], ip[6], ip[7],
8928 ip[8], ip[9], ip[10], ip[11]);
8929 if (os_snprintf_error(sizeof(ip_addr), res))
8930 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07008931 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008932 }
8933
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07008934 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
8935 ssid->passphrase == NULL && ssid->psk_set ?
8936 ssid->psk : NULL,
8937 ssid->passphrase, go_dev_addr, persistent,
8938 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008939
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008940 if (persistent) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008941 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
8942 ssid, go_dev_addr);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00008943 wpas_p2p_store_go_identity(wpa_s, go_dev_addr, bssid);
8944 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008945
Hai Shalom5f92bc92019-04-18 11:54:11 -07008946 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008947}
8948
8949
8950int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
8951 u32 interval1, u32 duration2, u32 interval2)
8952{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008953 int ret;
8954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008955 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8956 return -1;
8957
8958 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8959 wpa_s->current_ssid == NULL ||
8960 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8961 return -1;
8962
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008963 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8964 wpa_s->own_addr, wpa_s->assoc_freq,
8965 duration1, interval1, duration2, interval2);
8966 if (ret == 0)
8967 wpa_s->waiting_presence_resp = 1;
8968
8969 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008970}
8971
8972
8973int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8974 unsigned int interval)
8975{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008976 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8977 return -1;
8978
8979 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8980}
8981
8982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008983static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8984{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008985 if (wpa_s->current_ssid == NULL) {
8986 /*
8987 * current_ssid can be cleared when P2P client interface gets
8988 * disconnected, so assume this interface was used as P2P
8989 * client.
8990 */
8991 return 1;
8992 }
8993 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008994 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8995}
8996
8997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008998static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8999{
9000 struct wpa_supplicant *wpa_s = eloop_ctx;
9001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009002 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009003 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
9004 "disabled");
9005 return;
9006 }
9007
Dmitry Shmidt04949592012-07-19 12:16:46 -07009008 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
9009 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009010 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009011}
9012
9013
9014static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
9015{
Dmitry Shmidt04949592012-07-19 12:16:46 -07009016 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009017
Dmitry Shmidt04949592012-07-19 12:16:46 -07009018 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
9019 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
9020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009021 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
9022 return;
9023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009024 timeout = wpa_s->conf->p2p_group_idle;
9025 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
9026 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
9027 timeout = P2P_MAX_CLIENT_IDLE;
9028
9029 if (timeout == 0)
9030 return;
9031
Dmitry Shmidt04949592012-07-19 12:16:46 -07009032 if (timeout < 0) {
9033 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
9034 timeout = 0; /* special client mode no-timeout */
9035 else
9036 return;
9037 }
9038
9039 if (wpa_s->p2p_in_provisioning) {
9040 /*
9041 * Use the normal group formation timeout during the
9042 * provisioning phase to avoid terminating this process too
9043 * early due to group idle timeout.
9044 */
9045 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
9046 "during provisioning");
9047 return;
9048 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05309049
Dmitry Shmidt04949592012-07-19 12:16:46 -07009050 if (wpa_s->show_group_started) {
9051 /*
9052 * Use the normal group formation timeout between the end of
9053 * the provisioning phase and completion of 4-way handshake to
9054 * avoid terminating this process too early due to group idle
9055 * timeout.
9056 */
9057 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
9058 "while waiting for initial 4-way handshake to "
9059 "complete");
9060 return;
9061 }
9062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009063 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009064 timeout);
9065 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
9066 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009067}
9068
9069
Jouni Malinen2b89da82012-08-31 22:04:41 +03009070/* Returns 1 if the interface was removed */
9071int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
9072 u16 reason_code, const u8 *ie, size_t ie_len,
9073 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009074{
9075 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03009076 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009077
Dmitry Shmidt04949592012-07-19 12:16:46 -07009078 if (!locally_generated)
9079 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
9080 ie_len);
9081
9082 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
9083 wpa_s->current_ssid &&
9084 wpa_s->current_ssid->p2p_group &&
9085 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
9086 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
9087 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03009088 if (wpas_p2p_group_delete(wpa_s,
9089 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
9090 > 0)
9091 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009092 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03009093
9094 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009095}
9096
9097
9098void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009099 u16 reason_code, const u8 *ie, size_t ie_len,
9100 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009101{
9102 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9103 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009104
Dmitry Shmidt04949592012-07-19 12:16:46 -07009105 if (!locally_generated)
9106 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
9107 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009108}
9109
9110
9111void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
9112{
9113 struct p2p_data *p2p = wpa_s->global->p2p;
9114
9115 if (p2p == NULL)
9116 return;
9117
9118 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
9119 return;
9120
9121 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
9122 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
9123
9124 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
9125 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
9126
9127 if (wpa_s->wps &&
9128 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
9129 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
9130
9131 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
9132 p2p_set_uuid(p2p, wpa_s->wps->uuid);
9133
9134 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
9135 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
9136 p2p_set_model_name(p2p, wpa_s->conf->model_name);
9137 p2p_set_model_number(p2p, wpa_s->conf->model_number);
9138 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
9139 }
9140
9141 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
9142 p2p_set_sec_dev_types(p2p,
9143 (void *) wpa_s->conf->sec_device_type,
9144 wpa_s->conf->num_sec_device_types);
9145
9146 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
9147 int i;
9148 p2p_remove_wps_vendor_extensions(p2p);
9149 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
9150 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
9151 continue;
9152 p2p_add_wps_vendor_extension(
9153 p2p, wpa_s->conf->wps_vendor_ext[i]);
9154 }
9155 }
9156
9157 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
9158 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
9159 char country[3];
9160 country[0] = wpa_s->conf->country[0];
9161 country[1] = wpa_s->conf->country[1];
9162 country[2] = 0x04;
9163 p2p_set_country(p2p, country);
9164 }
9165
9166 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
9167 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
9168 wpa_s->conf->p2p_ssid_postfix ?
9169 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
9170 0);
9171 }
9172
9173 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
9174 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009175
9176 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
9177 u8 reg_class, channel;
9178 int ret;
9179 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009180 u8 channel_forced;
9181
Jouni Malinen75ecf522011-06-27 15:19:46 -07009182 if (wpa_s->conf->p2p_listen_reg_class &&
9183 wpa_s->conf->p2p_listen_channel) {
9184 reg_class = wpa_s->conf->p2p_listen_reg_class;
9185 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009186 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009187 } else {
9188 reg_class = 81;
9189 /*
9190 * Pick one of the social channels randomly as the
9191 * listen channel.
9192 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009193 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
9194 channel = 1;
9195 else
9196 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009197 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009198 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009199 ret = p2p_set_listen_channel(p2p, reg_class, channel,
9200 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07009201 if (ret)
9202 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
9203 "failed: %d", ret);
9204 }
9205 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
9206 u8 op_reg_class, op_channel, cfg_op_channel;
9207 int ret = 0;
9208 unsigned int r;
9209 if (wpa_s->conf->p2p_oper_reg_class &&
9210 wpa_s->conf->p2p_oper_channel) {
9211 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
9212 op_channel = wpa_s->conf->p2p_oper_channel;
9213 cfg_op_channel = 1;
9214 } else {
9215 op_reg_class = 81;
9216 /*
9217 * Use random operation channel from (1, 6, 11)
9218 *if no other preference is indicated.
9219 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009220 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
9221 op_channel = 1;
9222 else
9223 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07009224 cfg_op_channel = 0;
9225 }
9226 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
9227 cfg_op_channel);
9228 if (ret)
9229 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
9230 "failed: %d", ret);
9231 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07009232
9233 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
9234 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
9235 wpa_s->conf->p2p_pref_chan) < 0) {
9236 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
9237 "update failed");
9238 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009239
9240 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
9241 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
9242 "update failed");
9243 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07009244 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07009245
9246 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
9247 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009248}
9249
9250
9251int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
9252 int duration)
9253{
9254 if (!wpa_s->ap_iface)
9255 return -1;
9256 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
9257 duration);
9258}
9259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009260
9261int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
9262{
9263 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9264 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009265
9266 wpa_s->global->cross_connection = enabled;
9267 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
9268
9269 if (!enabled) {
9270 struct wpa_supplicant *iface;
9271
9272 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9273 {
9274 if (iface->cross_connect_enabled == 0)
9275 continue;
9276
9277 iface->cross_connect_enabled = 0;
9278 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009279 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009280 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
9281 iface->ifname,
9282 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009283 }
9284 }
9285
9286 return 0;
9287}
9288
9289
9290static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
9291{
9292 struct wpa_supplicant *iface;
9293
9294 if (!uplink->global->cross_connection)
9295 return;
9296
9297 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
9298 if (!iface->cross_connect_enabled)
9299 continue;
9300 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
9301 0)
9302 continue;
9303 if (iface->ap_iface == NULL)
9304 continue;
9305 if (iface->cross_connect_in_use)
9306 continue;
9307
9308 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009309 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009310 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
9311 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009312 }
9313}
9314
9315
9316static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
9317{
9318 struct wpa_supplicant *iface;
9319
9320 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
9321 if (!iface->cross_connect_enabled)
9322 continue;
9323 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
9324 0)
9325 continue;
9326 if (!iface->cross_connect_in_use)
9327 continue;
9328
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009329 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009330 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
9331 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009332 iface->cross_connect_in_use = 0;
9333 }
9334}
9335
9336
9337void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
9338{
9339 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
9340 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
9341 wpa_s->cross_connect_disallowed)
9342 wpas_p2p_disable_cross_connect(wpa_s);
9343 else
9344 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009345 if (!wpa_s->ap_iface &&
9346 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
9347 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009348}
9349
9350
9351void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
9352{
9353 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009354 if (!wpa_s->ap_iface &&
9355 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
9356 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009357 wpas_p2p_set_group_idle_timeout(wpa_s);
9358}
9359
9360
9361static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
9362{
9363 struct wpa_supplicant *iface;
9364
9365 if (!wpa_s->global->cross_connection)
9366 return;
9367
9368 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9369 if (iface == wpa_s)
9370 continue;
9371 if (iface->drv_flags &
9372 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
9373 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009374 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
9375 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009376 continue;
9377
9378 wpa_s->cross_connect_enabled = 1;
9379 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
9380 sizeof(wpa_s->cross_connect_uplink));
9381 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
9382 "%s to %s whenever uplink is available",
9383 wpa_s->ifname, wpa_s->cross_connect_uplink);
9384
9385 if (iface->ap_iface || iface->current_ssid == NULL ||
9386 iface->current_ssid->mode != WPAS_MODE_INFRA ||
9387 iface->cross_connect_disallowed ||
9388 iface->wpa_state != WPA_COMPLETED)
9389 break;
9390
9391 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009392 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07009393 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
9394 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009395 break;
9396 }
9397}
9398
9399
Sunil Ravi036cec52023-03-29 11:35:17 -07009400static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009401{
9402 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
9403 !wpa_s->p2p_in_provisioning)
9404 return 0; /* not P2P client operation */
9405
9406 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
9407 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009408 if (wpa_s != wpa_s->p2pdev)
9409 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009410 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009411 return 1;
9412}
9413
9414
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07009415void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
9416{
9417 struct wpa_supplicant *wpa_s = eloop_ctx;
9418 wpas_p2p_notif_pbc_overlap(wpa_s);
9419}
9420
9421
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009422void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
9423 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009424{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009425 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009426 struct wpa_used_freq_data *freqs = NULL;
9427 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009428
9429 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
9430 return;
9431
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009432 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9433 if (!freqs)
9434 return;
9435
Sunil Ravi77d572f2023-01-17 23:58:31 +00009436 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009438 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009439 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08009440 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
9441 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009442 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
9443 "channel list");
9444 return;
9445 }
9446
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009447 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07009448
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009449 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07009450
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009451 /*
9452 * The used frequencies map changed, so it is possible that a GO is
9453 * using a channel that is no longer valid for P2P use. It is also
9454 * possible that due to policy consideration, it would be preferable to
9455 * move it to a frequency already used by other station interfaces.
9456 */
9457 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
9458
9459 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009460}
9461
9462
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009463static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
9464 struct wpa_scan_results *scan_res)
9465{
9466 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
9467}
9468
9469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009470int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
9471{
9472 struct wpa_global *global = wpa_s->global;
9473 int found = 0;
9474 const u8 *peer;
9475
9476 if (global->p2p == NULL)
9477 return -1;
9478
9479 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
9480
9481 if (wpa_s->pending_interface_name[0] &&
9482 !is_zero_ether_addr(wpa_s->pending_interface_addr))
9483 found = 1;
9484
9485 peer = p2p_get_go_neg_peer(global->p2p);
9486 if (peer) {
9487 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
9488 MACSTR, MAC2STR(peer));
9489 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009490 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009491 }
9492
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08009493 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
9494 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
9495 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
9496 found = 1;
9497 }
9498
9499 if (wpa_s->pending_pd_before_join) {
9500 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
9501 wpa_s->pending_pd_before_join = 0;
9502 found = 1;
9503 }
9504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009505 wpas_p2p_stop_find(wpa_s);
9506
9507 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9508 if (wpa_s == global->p2p_group_formation &&
9509 (wpa_s->p2p_in_provisioning ||
9510 wpa_s->parent->pending_interface_type ==
9511 WPA_IF_P2P_CLIENT)) {
9512 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
9513 "formation found - cancelling",
9514 wpa_s->ifname);
9515 found = 1;
9516 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009517 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07009518 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009519 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07009520 break;
9521 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009522 wpas_p2p_group_delete(wpa_s,
9523 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009524 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07009525 } else if (wpa_s->p2p_in_invitation) {
9526 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
9527 wpa_s->ifname);
9528 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009529 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009530 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009531 }
9532 }
9533
9534 if (!found) {
9535 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
9536 return -1;
9537 }
9538
9539 return 0;
9540}
9541
9542
9543void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
9544{
9545 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
9546 return;
9547
9548 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
9549 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009550 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009551}
9552
9553
9554void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
9555 int freq_24, int freq_5, int freq_overall)
9556{
9557 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009558 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009559 return;
9560 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
9561}
9562
9563
9564int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
9565{
9566 u8 peer[ETH_ALEN];
9567 struct p2p_data *p2p = wpa_s->global->p2p;
9568
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009569 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009570 return -1;
9571
9572 if (hwaddr_aton(addr, peer))
9573 return -1;
9574
9575 return p2p_unauthorize(p2p, peer);
9576}
9577
9578
9579/**
9580 * wpas_p2p_disconnect - Disconnect from a P2P Group
9581 * @wpa_s: Pointer to wpa_supplicant data
9582 * Returns: 0 on success, -1 on failure
9583 *
9584 * This can be used to disconnect from a group in which the local end is a P2P
9585 * Client or to end a P2P Group in case the local end is the Group Owner. If a
9586 * virtual network interface was created for this group, that interface will be
9587 * removed. Otherwise, only the configured P2P group network will be removed
9588 * from the interface.
9589 */
9590int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
9591{
9592
9593 if (wpa_s == NULL)
9594 return -1;
9595
Jouni Malinen2b89da82012-08-31 22:04:41 +03009596 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
9597 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009598}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009599
9600
9601int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
9602{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08009603 int ret;
9604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009605 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9606 return 0;
9607
Dmitry Shmidtf8623282013-02-20 14:34:59 -08009608 ret = p2p_in_progress(wpa_s->global->p2p);
9609 if (ret == 0) {
9610 /*
9611 * Check whether there is an ongoing WPS provisioning step (or
9612 * other parts of group formation) on another interface since
9613 * p2p_in_progress() does not report this to avoid issues for
9614 * scans during such provisioning step.
9615 */
9616 if (wpa_s->global->p2p_group_formation &&
9617 wpa_s->global->p2p_group_formation != wpa_s) {
9618 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
9619 "in group formation",
9620 wpa_s->global->p2p_group_formation->ifname);
9621 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00009622 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
9623 wpa_dbg(wpa_s, MSG_DEBUG,
9624 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
9625 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08009626 }
9627 }
9628
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07009629 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009630 struct os_reltime now;
9631 os_get_reltime(&now);
9632 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
9633 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07009634 /* Wait for the first client has expired */
9635 wpa_s->global->p2p_go_wait_client.sec = 0;
9636 } else {
9637 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
9638 ret = 1;
9639 }
9640 }
9641
Dmitry Shmidtf8623282013-02-20 14:34:59 -08009642 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009643}
9644
9645
9646void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
9647 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009648{
9649 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
9650 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009651 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07009652 /**
9653 * Remove the network by scheduling the group formation
9654 * timeout to happen immediately. The teardown code
9655 * needs to be scheduled to run asynch later so that we
9656 * don't delete data from under ourselves unexpectedly.
9657 * Calling wpas_p2p_group_formation_timeout directly
9658 * causes a series of crashes in WPS failure scenarios.
9659 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009660 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
9661 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07009662 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009663 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009664 }
9665}
9666
9667
9668struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009669 const u8 *addr, const u8 *ssid,
9670 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009671{
9672 struct wpa_ssid *s;
9673 size_t i;
9674
9675 for (s = wpa_s->conf->ssid; s; s = s->next) {
9676 if (s->disabled != 2)
9677 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009678 if (ssid &&
9679 (ssid_len != s->ssid_len ||
9680 os_memcmp(ssid, s->ssid, ssid_len) != 0))
9681 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009682 if (addr == NULL) {
9683 if (s->mode == WPAS_MODE_P2P_GO)
9684 return s;
9685 continue;
9686 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009687 if (ether_addr_equal(s->bssid, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009688 return s; /* peer is GO in the persistent group */
9689 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
9690 continue;
9691 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009692 if (ether_addr_equal(s->p2p_client_list +
9693 i * 2 * ETH_ALEN, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009694 return s; /* peer is P2P client in persistent
9695 * group */
9696 }
9697 }
9698
9699 return NULL;
9700}
9701
9702
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009703static void wpas_p2p_store_client_identity(struct wpa_supplicant *wpa_s,
9704 const u8 *addr)
9705{
9706 u8 cipher;
9707 size_t dik_len;
9708 const u8 *dik_data;
9709 const u8 *pmk, *pmkid;
9710 size_t pmk_len;
9711 u8 iface_addr[ETH_ALEN];
9712 struct hostapd_data *hapd;
9713 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
9714
9715 if (!wpa_s->p2p2 || !wpa_s->ap_iface)
9716 return;
9717
9718 hapd = wpa_s->ap_iface->bss[0];
9719 if (!hapd)
9720 return;
9721
9722 if (p2p_get_dev_identity_key(p2p_wpa_s->global->p2p, addr,
9723 &dik_data, &dik_len, &cipher))
9724 return;
9725
9726 wpa_printf(MSG_DEBUG, "P2P: Fetch PMK from client (Device Addr " MACSTR
9727 ")", MAC2STR(addr));
9728 if (wpa_auth_pmksa_get_pmk(hapd->wpa_auth, addr, &pmk, &pmk_len,
9729 &pmkid)) {
9730 if (p2p_get_interface_addr(p2p_wpa_s->global->p2p, addr,
9731 iface_addr))
9732 return;
9733 wpa_printf(MSG_DEBUG,
9734 "P2P: Fetch PMK from client (Interface Addr " MACSTR
9735 ")", MAC2STR(iface_addr));
9736 if (wpa_auth_pmksa_get_pmk(hapd->wpa_auth, iface_addr, &pmk,
9737 &pmk_len, &pmkid))
9738 return;
9739 }
9740
9741 wpa_printf(MSG_DEBUG,
9742 "P2P: Storing device identity of client (Device Addr "
9743 MACSTR ")", MAC2STR(addr));
9744 wpas_p2p_store_identity(p2p_wpa_s, cipher, dik_data, dik_len, pmk,
9745 pmk_len, pmkid);
9746}
9747
9748
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009749void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
9750 const u8 *addr)
9751{
Dmitry Shmidt56052862013-10-04 10:23:25 -07009752 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009753 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07009754 /*
9755 * This can happen if WPS provisioning step is not terminated
9756 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
9757 * peer was able to connect, there is no need to time out group
9758 * formation after this, though. In addition, this is used with
9759 * the initial connection wait on the GO as a separate formation
9760 * timeout and as such, expected to be hit after the initial WPS
9761 * provisioning step.
9762 */
9763 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009764
9765 if (!wpa_s->p2p_go_group_formation_completed &&
9766 !wpa_s->group_formation_reported) {
9767 /*
9768 * GO has not yet notified group formation success since
9769 * the WPS step was not completed cleanly. Do that
9770 * notification now since the P2P Client was able to
9771 * connect and as such, must have received the
9772 * credential from the WPS step.
9773 */
9774 if (wpa_s->global->p2p)
9775 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009776 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009777 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07009778 }
9779 if (!wpa_s->p2p_go_group_formation_completed) {
9780 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
9781 wpa_s->p2p_go_group_formation_completed = 1;
9782 wpa_s->global->p2p_group_formation = NULL;
9783 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07009784 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00009785 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07009786 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07009787 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009788 if (addr == NULL)
9789 return;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009790
9791 wpas_p2p_store_client_identity(wpa_s, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009792 wpas_p2p_add_persistent_group_client(wpa_s, addr);
9793}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009794
Dmitry Shmidt04949592012-07-19 12:16:46 -07009795
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009796static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
9797 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07009798{
9799 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009800 int ret = 0;
9801
Dmitry Shmidt04949592012-07-19 12:16:46 -07009802 if (wpa_s->global->p2p_group_formation)
9803 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07009804 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009805 offchannel_send_action_done(wpa_s);
9806 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009807 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009808 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
9809 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
9810 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
9811 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009812 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009813 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009814 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07009815 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009816 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08009817 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009818 wpa_s->p2p_go_he,
9819 wpa_s->p2p_go_edmg,
Sunil Ravic0f5d412024-09-11 22:12:49 +00009820 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p),
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00009821 wpa_s->p2p2, wpa_s->p2p_bootstrap, NULL, false);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009822 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009823}
9824
9825
9826int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
9827{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009828 int res;
9829
Dmitry Shmidt04949592012-07-19 12:16:46 -07009830 if (!wpa_s->p2p_fallback_to_go_neg ||
9831 wpa_s->p2p_in_provisioning <= 5)
9832 return 0;
9833
9834 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
9835 return 0; /* peer operating as a GO */
9836
9837 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
9838 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009839 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009840 "reason=GO-not-found");
9841 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07009842
Dmitry Shmidt7f656022015-02-25 14:36:37 -08009843 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07009844}
9845
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009846
9847unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
9848{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009849 struct wpa_supplicant *ifs;
9850
9851 if (wpa_s->wpa_state > WPA_SCANNING) {
9852 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
9853 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07009854 wpa_s->conf->p2p_search_delay);
9855 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009856 }
9857
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08009858 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9859 radio_list) {
9860 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
9862 "delay due to concurrent operation on "
9863 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07009864 wpa_s->conf->p2p_search_delay,
9865 ifs->ifname);
9866 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07009867 }
9868 }
9869
9870 return 0;
9871}
9872
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07009873
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009874static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
9875 struct wpa_ssid *s, const u8 *addr,
9876 int iface_addr)
9877{
9878 struct psk_list_entry *psk, *tmp;
9879 int changed = 0;
9880
9881 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
9882 list) {
9883 if ((iface_addr && !psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009884 ether_addr_equal(addr, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009885 (!iface_addr && psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009886 ether_addr_equal(addr, psk->addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009887 wpa_dbg(wpa_s, MSG_DEBUG,
9888 "P2P: Remove persistent group PSK list entry for "
9889 MACSTR " p2p=%u",
9890 MAC2STR(psk->addr), psk->p2p);
9891 dl_list_del(&psk->list);
9892 os_free(psk);
9893 changed++;
9894 }
9895 }
9896
9897 return changed;
9898}
9899
9900
9901void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
9902 const u8 *p2p_dev_addr,
9903 const u8 *psk, size_t psk_len)
9904{
9905 struct wpa_ssid *ssid = wpa_s->current_ssid;
9906 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009907 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009908
9909 if (psk_len != sizeof(p->psk))
9910 return;
9911
9912 if (p2p_dev_addr) {
9913 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
9914 " p2p_dev_addr=" MACSTR,
9915 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
9916 if (is_zero_ether_addr(p2p_dev_addr))
9917 p2p_dev_addr = NULL;
9918 } else {
9919 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
9920 MAC2STR(mac_addr));
9921 }
9922
9923 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
9924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
9925 /* To be added to persistent group once created */
9926 if (wpa_s->global->add_psk == NULL) {
9927 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
9928 if (wpa_s->global->add_psk == NULL)
9929 return;
9930 }
9931 p = wpa_s->global->add_psk;
9932 if (p2p_dev_addr) {
9933 p->p2p = 1;
9934 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
9935 } else {
9936 p->p2p = 0;
9937 os_memcpy(p->addr, mac_addr, ETH_ALEN);
9938 }
9939 os_memcpy(p->psk, psk, psk_len);
9940 return;
9941 }
9942
9943 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
9944 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
9945 return;
9946 }
9947
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009948 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009949 ssid->ssid_len);
9950 if (!persistent) {
9951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
9952 return;
9953 }
9954
9955 p = os_zalloc(sizeof(*p));
9956 if (p == NULL)
9957 return;
9958 if (p2p_dev_addr) {
9959 p->p2p = 1;
9960 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
9961 } else {
9962 p->p2p = 0;
9963 os_memcpy(p->addr, mac_addr, ETH_ALEN);
9964 }
9965 os_memcpy(p->psk, psk, psk_len);
9966
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07009967 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
9968 (last = dl_list_last(&persistent->psk_list,
9969 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
9971 MACSTR " (p2p=%u) to make room for a new one",
9972 MAC2STR(last->addr), last->p2p);
9973 dl_list_del(&last->list);
9974 os_free(last);
9975 }
9976
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009977 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009978 p2p_dev_addr ? p2p_dev_addr : mac_addr,
9979 p2p_dev_addr == NULL);
9980 if (p2p_dev_addr) {
9981 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
9982 MACSTR, MAC2STR(p2p_dev_addr));
9983 } else {
9984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
9985 MAC2STR(mac_addr));
9986 }
9987 dl_list_add(&persistent->psk_list, &p->list);
9988
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009989 if (wpa_s->p2pdev->conf->update_config &&
9990 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009991 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009992}
9993
9994
9995static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
9996 struct wpa_ssid *s, const u8 *addr,
9997 int iface_addr)
9998{
9999 int res;
10000
10001 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080010002 if (res > 0 && wpa_s->conf->update_config &&
10003 wpa_config_write(wpa_s->confname, wpa_s->conf))
10004 wpa_dbg(wpa_s, MSG_DEBUG,
10005 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010006}
10007
10008
10009static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
10010 const u8 *peer, int iface_addr)
10011{
10012 struct hostapd_data *hapd;
10013 struct hostapd_wpa_psk *psk, *prev, *rem;
10014 struct sta_info *sta;
10015
10016 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
10017 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
10018 return;
10019
10020 /* Remove per-station PSK entry */
10021 hapd = wpa_s->ap_iface->bss[0];
10022 prev = NULL;
10023 psk = hapd->conf->ssid.wpa_psk;
10024 while (psk) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010025 if ((iface_addr && ether_addr_equal(peer, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010026 (!iface_addr &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010027 ether_addr_equal(peer, psk->p2p_dev_addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010028 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
10029 MACSTR " iface_addr=%d",
10030 MAC2STR(peer), iface_addr);
10031 if (prev)
10032 prev->next = psk->next;
10033 else
10034 hapd->conf->ssid.wpa_psk = psk->next;
10035 rem = psk;
10036 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -070010037 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010038 } else {
10039 prev = psk;
10040 psk = psk->next;
10041 }
10042 }
10043
10044 /* Disconnect from group */
10045 if (iface_addr)
10046 sta = ap_get_sta(hapd, peer);
10047 else
10048 sta = ap_get_sta_p2p(hapd, peer);
10049 if (sta) {
10050 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
10051 " (iface_addr=%d) from group",
10052 MAC2STR(peer), iface_addr);
10053 hostapd_drv_sta_deauth(hapd, sta->addr,
10054 WLAN_REASON_DEAUTH_LEAVING);
10055 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
10056 }
10057}
10058
10059
10060void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
10061 int iface_addr)
10062{
10063 struct wpa_ssid *s;
10064 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -070010065 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010066
10067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
10068
10069 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -070010070 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010071 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
10072 continue;
10073 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -070010074 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
10075 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010076 }
10077
10078 /* Remove from any operating group */
10079 for (w = wpa_s->global->ifaces; w; w = w->next)
10080 wpas_p2p_remove_client_go(w, peer, iface_addr);
10081}
10082
10083
10084static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
10085{
10086 struct wpa_supplicant *wpa_s = eloop_ctx;
10087 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
10088}
10089
10090
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080010091static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
10092{
10093 struct wpa_supplicant *wpa_s = eloop_ctx;
10094
10095 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
10096 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
10097}
10098
10099
10100int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
10101 struct wpa_ssid *ssid)
10102{
10103 struct wpa_supplicant *iface;
10104
10105 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
10106 if (!iface->current_ssid ||
10107 iface->current_ssid->frequency == freq ||
10108 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
10109 !iface->current_ssid->p2p_group))
10110 continue;
10111
10112 /* Remove the connection with least priority */
10113 if (!wpas_is_p2p_prioritized(iface)) {
10114 /* STA connection has priority over existing
10115 * P2P connection, so remove the interface. */
10116 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
10117 eloop_register_timeout(0, 0,
10118 wpas_p2p_group_freq_conflict,
10119 iface, NULL);
10120 /* If connection in progress is P2P connection, do not
10121 * proceed for the connection. */
10122 if (wpa_s == iface)
10123 return -1;
10124 else
10125 return 0;
10126 } else {
10127 /* P2P connection has priority, disable the STA network
10128 */
10129 wpa_supplicant_disable_network(wpa_s->global->ifaces,
10130 ssid);
10131 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
10132 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
10133 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
10134 ETH_ALEN);
10135 /* If P2P connection is in progress, continue
10136 * connecting...*/
10137 if (wpa_s == iface)
10138 return 0;
10139 else
10140 return -1;
10141 }
10142 }
10143
10144 return 0;
10145}
10146
10147
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010148int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
10149{
10150 struct wpa_ssid *ssid = wpa_s->current_ssid;
10151
10152 if (ssid == NULL || !ssid->p2p_group)
10153 return 0;
10154
10155 if (wpa_s->p2p_last_4way_hs_fail &&
10156 wpa_s->p2p_last_4way_hs_fail == ssid) {
10157 u8 go_dev_addr[ETH_ALEN];
10158 struct wpa_ssid *persistent;
10159
10160 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
10161 ssid->ssid,
10162 ssid->ssid_len) <= 0) {
10163 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
10164 goto disconnect;
10165 }
10166
10167 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
10168 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -080010169 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010170 ssid->ssid,
10171 ssid->ssid_len);
10172 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
10173 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
10174 goto disconnect;
10175 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -080010176 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070010177 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
10178 persistent->id);
10179 disconnect:
10180 wpa_s->p2p_last_4way_hs_fail = NULL;
10181 /*
10182 * Remove the group from a timeout to avoid issues with caller
10183 * continuing to use the interface if this is on a P2P group
10184 * interface.
10185 */
10186 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
10187 wpa_s, NULL);
10188 return 1;
10189 }
10190
10191 wpa_s->p2p_last_4way_hs_fail = ssid;
10192 return 0;
10193}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010194
10195
10196#ifdef CONFIG_WPS_NFC
10197
10198static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
10199 struct wpabuf *p2p)
10200{
10201 struct wpabuf *ret;
10202 size_t wsc_len;
10203
10204 if (p2p == NULL) {
10205 wpabuf_free(wsc);
10206 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
10207 return NULL;
10208 }
10209
10210 wsc_len = wsc ? wpabuf_len(wsc) : 0;
10211 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
10212 if (ret == NULL) {
10213 wpabuf_free(wsc);
10214 wpabuf_free(p2p);
10215 return NULL;
10216 }
10217
10218 wpabuf_put_be16(ret, wsc_len);
10219 if (wsc)
10220 wpabuf_put_buf(ret, wsc);
10221 wpabuf_put_be16(ret, wpabuf_len(p2p));
10222 wpabuf_put_buf(ret, p2p);
10223
10224 wpabuf_free(wsc);
10225 wpabuf_free(p2p);
10226 wpa_hexdump_buf(MSG_DEBUG,
10227 "P2P: Generated NFC connection handover message", ret);
10228
10229 if (ndef && ret) {
10230 struct wpabuf *tmp;
10231 tmp = ndef_build_p2p(ret);
10232 wpabuf_free(ret);
10233 if (tmp == NULL) {
10234 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
10235 return NULL;
10236 }
10237 ret = tmp;
10238 }
10239
10240 return ret;
10241}
10242
10243
10244static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
10245 struct wpa_ssid **ssid, u8 *go_dev_addr)
10246{
10247 struct wpa_supplicant *iface;
10248
10249 if (go_dev_addr)
10250 os_memset(go_dev_addr, 0, ETH_ALEN);
10251 if (ssid)
10252 *ssid = NULL;
10253 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
10254 if (iface->wpa_state < WPA_ASSOCIATING ||
10255 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
10256 !iface->current_ssid->p2p_group ||
10257 iface->current_ssid->mode != WPAS_MODE_INFRA)
10258 continue;
10259 if (ssid)
10260 *ssid = iface->current_ssid;
10261 if (go_dev_addr)
10262 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
10263 return iface->assoc_freq;
10264 }
10265 return 0;
10266}
10267
10268
10269struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
10270 int ndef)
10271{
10272 struct wpabuf *wsc, *p2p;
10273 struct wpa_ssid *ssid;
10274 u8 go_dev_addr[ETH_ALEN];
10275 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
10276
10277 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
10278 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
10279 return NULL;
10280 }
10281
10282 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
10283 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
10284 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
10285 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
10286 return NULL;
10287 }
10288
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010289 wpa_s->p2p2 = false;
10290
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010291 if (cli_freq == 0) {
10292 wsc = wps_build_nfc_handover_req_p2p(
10293 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
10294 } else
10295 wsc = NULL;
10296 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
10297 go_dev_addr, ssid ? ssid->ssid : NULL,
10298 ssid ? ssid->ssid_len : 0);
10299
10300 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
10301}
10302
10303
10304struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
10305 int ndef, int tag)
10306{
10307 struct wpabuf *wsc, *p2p;
10308 struct wpa_ssid *ssid;
10309 u8 go_dev_addr[ETH_ALEN];
10310 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
10311
10312 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10313 return NULL;
10314
10315 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
10316 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
10317 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
10318 return NULL;
10319
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010320 wpa_s->p2p2 = false;
10321
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010322 if (cli_freq == 0) {
10323 wsc = wps_build_nfc_handover_sel_p2p(
10324 wpa_s->parent->wps,
10325 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
10326 DEV_PW_NFC_CONNECTION_HANDOVER,
10327 wpa_s->conf->wps_nfc_dh_pubkey,
10328 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
10329 } else
10330 wsc = NULL;
10331 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
10332 go_dev_addr, ssid ? ssid->ssid : NULL,
10333 ssid ? ssid->ssid_len : 0);
10334
10335 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
10336}
10337
10338
10339static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
10340 struct p2p_nfc_params *params)
10341{
10342 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
10343 "connection handover (freq=%d)",
10344 params->go_freq);
10345
10346 if (params->go_freq && params->go_ssid_len) {
10347 wpa_s->p2p_wps_method = WPS_NFC;
10348 wpa_s->pending_join_wps_method = WPS_NFC;
10349 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
10350 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
10351 ETH_ALEN);
10352 return wpas_p2p_join_start(wpa_s, params->go_freq,
10353 params->go_ssid,
10354 params->go_ssid_len);
10355 }
10356
10357 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
10358 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080010359 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -080010360 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010361 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -080010362 params->go_ssid_len ? params->go_ssid : NULL,
Sunil Ravic0f5d412024-09-11 22:12:49 +000010363 params->go_ssid_len, false, wpa_s->p2p2,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010364 wpa_s->p2p_bootstrap, NULL, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010365}
10366
10367
10368static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
10369 struct p2p_nfc_params *params, int tag)
10370{
10371 int res, persistent;
10372 struct wpa_ssid *ssid;
10373
10374 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
10375 "connection handover");
10376 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
10377 ssid = wpa_s->current_ssid;
10378 if (ssid == NULL)
10379 continue;
10380 if (ssid->mode != WPAS_MODE_P2P_GO)
10381 continue;
10382 if (wpa_s->ap_iface == NULL)
10383 continue;
10384 break;
10385 }
10386 if (wpa_s == NULL) {
10387 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
10388 return -1;
10389 }
10390
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010391 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010392 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010393 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010394 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
10395 return -1;
10396 }
10397 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010398 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
10399 wpa_s->p2pdev->p2p_oob_dev_pw,
10400 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
10401 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010402 if (res)
10403 return res;
10404
10405 if (!tag) {
10406 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
10407 return 0;
10408 }
10409
10410 if (!params->peer ||
10411 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
10412 return 0;
10413
10414 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
10415 " to join", MAC2STR(params->peer->p2p_device_addr));
10416
10417 wpa_s->global->p2p_invite_group = wpa_s;
10418 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010419 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010420 params->peer->p2p_device_addr,
10421 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -070010422 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010423
10424 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
10425 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
10426 ssid->ssid, ssid->ssid_len, ssid->frequency,
10427 wpa_s->global->p2p_dev_addr, persistent, 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010428 wpa_s->p2pdev->p2p_oob_dev_pw_id, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010429}
10430
10431
10432static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
10433 struct p2p_nfc_params *params,
10434 int forced_freq)
10435{
10436 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
10437 "connection handover");
10438 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
10439 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080010440 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -080010441 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010442 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Sunil Ravic0f5d412024-09-11 22:12:49 +000010443 NULL, 0, false, wpa_s->p2p2,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010444 wpa_s->p2p_bootstrap, NULL, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010445}
10446
10447
10448static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
10449 struct p2p_nfc_params *params,
10450 int forced_freq)
10451{
10452 int res;
10453
10454 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
10455 "connection handover");
10456 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
10457 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080010458 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -080010459 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010460 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Sunil Ravic0f5d412024-09-11 22:12:49 +000010461 NULL, 0, false, wpa_s->p2p2,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010462 wpa_s->p2p_bootstrap, NULL, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010463 if (res)
10464 return res;
10465
10466 res = wpas_p2p_listen(wpa_s, 60);
10467 if (res) {
10468 p2p_unauthorize(wpa_s->global->p2p,
10469 params->peer->p2p_device_addr);
10470 }
10471
10472 return res;
10473}
10474
10475
10476static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
10477 const struct wpabuf *data,
10478 int sel, int tag, int forced_freq)
10479{
10480 const u8 *pos, *end;
10481 u16 len, id;
10482 struct p2p_nfc_params params;
10483 int res;
10484
10485 os_memset(&params, 0, sizeof(params));
10486 params.sel = sel;
10487
10488 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
10489
10490 pos = wpabuf_head(data);
10491 end = pos + wpabuf_len(data);
10492
10493 if (end - pos < 2) {
10494 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
10495 "attributes");
10496 return -1;
10497 }
10498 len = WPA_GET_BE16(pos);
10499 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010500 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010501 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
10502 "attributes");
10503 return -1;
10504 }
10505 params.wsc_attr = pos;
10506 params.wsc_len = len;
10507 pos += len;
10508
10509 if (end - pos < 2) {
10510 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
10511 "attributes");
10512 return -1;
10513 }
10514 len = WPA_GET_BE16(pos);
10515 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080010516 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010517 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
10518 "attributes");
10519 return -1;
10520 }
10521 params.p2p_attr = pos;
10522 params.p2p_len = len;
10523 pos += len;
10524
10525 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
10526 params.wsc_attr, params.wsc_len);
10527 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
10528 params.p2p_attr, params.p2p_len);
10529 if (pos < end) {
10530 wpa_hexdump(MSG_DEBUG,
10531 "P2P: Ignored extra data after P2P attributes",
10532 pos, end - pos);
10533 }
10534
10535 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
10536 if (res)
10537 return res;
10538
10539 if (params.next_step == NO_ACTION)
10540 return 0;
10541
10542 if (params.next_step == BOTH_GO) {
10543 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
10544 MAC2STR(params.peer->p2p_device_addr));
10545 return 0;
10546 }
10547
10548 if (params.next_step == PEER_CLIENT) {
10549 if (!is_zero_ether_addr(params.go_dev_addr)) {
10550 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
10551 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
10552 " ssid=\"%s\"",
10553 MAC2STR(params.peer->p2p_device_addr),
10554 params.go_freq,
10555 MAC2STR(params.go_dev_addr),
10556 wpa_ssid_txt(params.go_ssid,
10557 params.go_ssid_len));
10558 } else {
10559 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
10560 "peer=" MACSTR " freq=%d",
10561 MAC2STR(params.peer->p2p_device_addr),
10562 params.go_freq);
10563 }
10564 return 0;
10565 }
10566
10567 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
10568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
10569 MACSTR, MAC2STR(params.peer->p2p_device_addr));
10570 return 0;
10571 }
10572
10573 wpabuf_free(wpa_s->p2p_oob_dev_pw);
10574 wpa_s->p2p_oob_dev_pw = NULL;
10575
10576 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
10577 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
10578 "received");
10579 return -1;
10580 }
10581
10582 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
10583 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
10584 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
10585 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
10586 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
10587 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
10588 wpa_s->p2p_peer_oob_pk_hash_known = 1;
10589
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000010590 wpa_s->p2p2 = false;
10591
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010592 if (tag) {
10593 if (id < 0x10) {
10594 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
10595 "peer OOB Device Password Id %u", id);
10596 return -1;
10597 }
10598 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
10599 "Device Password Id %u", id);
10600 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
10601 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
10602 params.oob_dev_pw_len -
10603 WPS_OOB_PUBKEY_HASH_LEN - 2);
10604 wpa_s->p2p_oob_dev_pw_id = id;
10605 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
10606 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
10607 params.oob_dev_pw_len -
10608 WPS_OOB_PUBKEY_HASH_LEN - 2);
10609 if (wpa_s->p2p_oob_dev_pw == NULL)
10610 return -1;
10611
10612 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
10613 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
10614 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
10615 return -1;
10616 } else {
10617 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
10618 "without Device Password");
10619 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
10620 }
10621
10622 switch (params.next_step) {
10623 case NO_ACTION:
10624 case BOTH_GO:
10625 case PEER_CLIENT:
10626 /* already covered above */
10627 return 0;
10628 case JOIN_GROUP:
10629 return wpas_p2p_nfc_join_group(wpa_s, &params);
10630 case AUTH_JOIN:
10631 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
10632 case INIT_GO_NEG:
10633 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
10634 case RESP_GO_NEG:
10635 /* TODO: use own OOB Dev Pw */
10636 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
10637 }
10638
10639 return -1;
10640}
10641
10642
10643int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
10644 const struct wpabuf *data, int forced_freq)
10645{
10646 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10647 return -1;
10648
10649 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
10650}
10651
10652
10653int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
10654 const struct wpabuf *req,
10655 const struct wpabuf *sel, int forced_freq)
10656{
10657 struct wpabuf *tmp;
10658 int ret;
10659
10660 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10661 return -1;
10662
10663 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
10664
10665 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
10666 wpabuf_head(req), wpabuf_len(req));
10667 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
10668 wpabuf_head(sel), wpabuf_len(sel));
10669 if (forced_freq)
10670 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
10671 tmp = ndef_parse_p2p(init ? sel : req);
10672 if (tmp == NULL) {
10673 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
10674 return -1;
10675 }
10676
10677 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
10678 forced_freq);
10679 wpabuf_free(tmp);
10680
10681 return ret;
10682}
10683
10684
10685int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
10686{
10687 const u8 *if_addr;
10688 int go_intent = wpa_s->conf->p2p_go_intent;
10689 struct wpa_supplicant *iface;
10690
10691 if (wpa_s->global->p2p == NULL)
10692 return -1;
10693
10694 if (!enabled) {
10695 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
10696 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
10697 {
10698 if (!iface->ap_iface)
10699 continue;
10700 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
10701 }
10702 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
10703 0, NULL);
10704 if (wpa_s->p2p_nfc_tag_enabled)
10705 wpas_p2p_remove_pending_group_interface(wpa_s);
10706 wpa_s->p2p_nfc_tag_enabled = 0;
10707 return 0;
10708 }
10709
10710 if (wpa_s->global->p2p_disabled)
10711 return -1;
10712
10713 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
10714 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
10715 wpa_s->conf->wps_nfc_dev_pw == NULL ||
10716 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
10717 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
10718 "to allow static handover cases");
10719 return -1;
10720 }
10721
10722 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
10723
10724 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
10725 wpabuf_free(wpa_s->p2p_oob_dev_pw);
10726 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
10727 if (wpa_s->p2p_oob_dev_pw == NULL)
10728 return -1;
10729 wpa_s->p2p_peer_oob_pk_hash_known = 0;
10730
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -070010731 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
10732 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
10733 /*
10734 * P2P Group Interface present and the command came on group
10735 * interface, so enable the token for the current interface.
10736 */
10737 wpa_s->create_p2p_iface = 0;
10738 } else {
10739 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
10740 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010741
10742 if (wpa_s->create_p2p_iface) {
10743 enum wpa_driver_if_type iftype;
10744 /* Prepare to add a new interface for the group */
10745 iftype = WPA_IF_P2P_GROUP;
10746 if (go_intent == 15)
10747 iftype = WPA_IF_P2P_GO;
10748 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
10749 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
10750 "interface for the group");
10751 return -1;
10752 }
10753
10754 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -080010755 } else if (wpa_s->p2p_mgmt)
10756 if_addr = wpa_s->parent->own_addr;
10757 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080010758 if_addr = wpa_s->own_addr;
10759
10760 wpa_s->p2p_nfc_tag_enabled = enabled;
10761
10762 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
10763 struct hostapd_data *hapd;
10764 if (iface->ap_iface == NULL)
10765 continue;
10766 hapd = iface->ap_iface->bss[0];
10767 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
10768 hapd->conf->wps_nfc_dh_pubkey =
10769 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
10770 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
10771 hapd->conf->wps_nfc_dh_privkey =
10772 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
10773 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
10774 hapd->conf->wps_nfc_dev_pw =
10775 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
10776 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
10777
10778 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
10779 wpa_dbg(iface, MSG_DEBUG,
10780 "P2P: Failed to enable NFC Tag for GO");
10781 }
10782 }
10783 p2p_set_authorized_oob_dev_pw_id(
10784 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
10785 if_addr);
10786
10787 return 0;
10788}
10789
10790#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010791
10792
10793static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
10794 struct wpa_used_freq_data *freqs,
10795 unsigned int num)
10796{
10797 u8 curr_chan, cand, chan;
10798 unsigned int i;
10799
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010800 /*
10801 * If possible, optimize the Listen channel to be a channel that is
10802 * already used by one of the other interfaces.
10803 */
10804 if (!wpa_s->conf->p2p_optimize_listen_chan)
10805 return;
10806
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010807 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
10808 for (i = 0, cand = 0; i < num; i++) {
10809 ieee80211_freq_to_chan(freqs[i].freq, &chan);
10810 if (curr_chan == chan) {
10811 cand = 0;
10812 break;
10813 }
10814
10815 if (chan == 1 || chan == 6 || chan == 11)
10816 cand = chan;
10817 }
10818
10819 if (cand) {
10820 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -080010821 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010822 cand);
10823 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
10824 }
10825}
10826
10827
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010828static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010829{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010830 struct hostapd_config *conf;
10831 struct p2p_go_neg_results params;
10832 struct csa_settings csa_settings;
10833 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
10834 int old_freq = current_ssid->frequency;
10835 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010836
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010837 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
10838 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
10839 return -1;
10840 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010841
10842 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010843 * TODO: This function may not always work correctly. For example,
10844 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010845 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010846 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
10847 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010848 wpa_dbg(wpa_s, MSG_DEBUG,
10849 "P2P CSA: Failed to select new frequency for GO");
10850 return -1;
10851 }
10852
10853 if (current_ssid->frequency == params.freq) {
10854 wpa_dbg(wpa_s, MSG_DEBUG,
10855 "P2P CSA: Selected same frequency - not moving GO");
10856 return 0;
10857 }
10858
10859 conf = hostapd_config_defaults();
10860 if (!conf) {
10861 wpa_dbg(wpa_s, MSG_DEBUG,
10862 "P2P CSA: Failed to allocate default config");
10863 return -1;
10864 }
10865
10866 current_ssid->frequency = params.freq;
10867 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
10868 wpa_dbg(wpa_s, MSG_DEBUG,
10869 "P2P CSA: Failed to create new GO config");
10870 ret = -1;
10871 goto out;
10872 }
10873
Sunil4a3f9f52022-07-19 22:04:39 +000010874 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
10875 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -070010876 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +000010877 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
10878 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -070010879 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
10880 wpa_s->ap_iface->current_mode->mode,
10881 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
10882 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010883 ret = -1;
10884 goto out;
10885 }
10886
10887 os_memset(&csa_settings, 0, sizeof(csa_settings));
10888 csa_settings.cs_count = P2P_GO_CSA_COUNT;
10889 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
Sunil Ravib0ac25f2024-07-12 01:42:03 +000010890 csa_settings.link_id = -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010891 csa_settings.freq_params.freq = params.freq;
10892 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
10893 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
10894 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
10895
10896 if (conf->ieee80211ac) {
10897 int freq1 = 0, freq2 = 0;
10898 u8 chan, opclass;
10899
10900 if (ieee80211_freq_to_channel_ext(params.freq,
10901 conf->secondary_channel,
10902 conf->vht_oper_chwidth,
10903 &opclass, &chan) ==
10904 NUM_HOSTAPD_MODES) {
10905 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
10906 ret = -1;
10907 goto out;
10908 }
10909
10910 if (conf->vht_oper_centr_freq_seg0_idx)
10911 freq1 = ieee80211_chan_to_freq(
10912 NULL, opclass,
10913 conf->vht_oper_centr_freq_seg0_idx);
10914
10915 if (conf->vht_oper_centr_freq_seg1_idx)
10916 freq2 = ieee80211_chan_to_freq(
10917 NULL, opclass,
10918 conf->vht_oper_centr_freq_seg1_idx);
10919
10920 if (freq1 < 0 || freq2 < 0) {
10921 wpa_dbg(wpa_s, MSG_DEBUG,
10922 "P2P CSA: Selected invalid VHT center freqs");
10923 ret = -1;
10924 goto out;
10925 }
10926
10927 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
10928 csa_settings.freq_params.center_freq1 = freq1;
10929 csa_settings.freq_params.center_freq2 = freq2;
10930
10931 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +000010932 case CONF_OPER_CHWIDTH_80MHZ:
10933 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010934 csa_settings.freq_params.bandwidth = 80;
10935 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000010936 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010937 csa_settings.freq_params.bandwidth = 160;
10938 break;
Sunil8cd6f4d2022-06-28 18:40:46 +000010939 default:
10940 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010941 }
10942 }
10943
10944 ret = ap_switch_channel(wpa_s, &csa_settings);
10945out:
10946 current_ssid->frequency = old_freq;
10947 hostapd_config_free(conf);
10948 return ret;
10949}
10950
10951
10952static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
10953{
10954 struct p2p_go_neg_results params;
10955 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -070010956 void (*ap_configured_cb)(void *ctx, void *data);
10957 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010958
10959 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
10960
10961 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
10962 current_ssid->frequency);
10963
10964 /* Stop the AP functionality */
10965 /* TODO: Should do this in a way that does not indicated to possible
10966 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -070010967 /* If this action occurs before a group is started, the callback should
10968 * be preserved, or GROUP-STARTED event would be lost. If this action
10969 * occurs after a group is started, these pointers are all NULL and
10970 * harmless. */
10971 ap_configured_cb = wpa_s->ap_configured_cb;
10972 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
10973 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010974 wpa_supplicant_ap_deinit(wpa_s);
10975
10976 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080010977 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
10978 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010979 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
10980 wpas_p2p_group_delete(wpa_s,
10981 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
10982 return;
10983 }
10984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
10985 params.freq);
10986
10987 if (params.freq &&
10988 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
10989 wpa_printf(MSG_DEBUG,
10990 "P2P: Selected freq (%u MHz) is not valid for P2P",
10991 params.freq);
10992 wpas_p2p_group_delete(wpa_s,
10993 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
10994 return;
10995 }
10996
Hai Shalom899fcc72020-10-19 14:38:18 -070010997 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +080010998 wpa_s->ap_configured_cb = ap_configured_cb;
10999 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
11000 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -070011001
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011002 /* Update the frequency */
11003 current_ssid->frequency = params.freq;
11004 wpa_s->connect_without_scan = current_ssid;
11005 wpa_s->reassociate = 1;
11006 wpa_s->disconnected = 0;
11007 wpa_supplicant_req_scan(wpa_s, 0, 0);
11008}
11009
11010
11011static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
11012{
11013 struct wpa_supplicant *wpa_s = eloop_ctx;
11014
11015 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011016 return;
11017
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011018 wpas_p2p_go_update_common_freqs(wpa_s);
11019
11020 /* Do not move GO in the middle of a CSA */
11021 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
11022 wpa_printf(MSG_DEBUG,
11023 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011024 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011025 }
11026
11027 /*
11028 * First, try a channel switch flow. If it is not supported or fails,
11029 * take down the GO and bring it up again.
11030 */
11031 if (wpas_p2p_move_go_csa(wpa_s) < 0)
11032 wpas_p2p_move_go_no_csa(wpa_s);
11033}
11034
11035
11036static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
11037{
11038 struct wpa_supplicant *wpa_s = eloop_ctx;
11039 struct wpa_used_freq_data *freqs = NULL;
11040 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011041
11042 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
11043 if (!freqs)
11044 return;
11045
Sunil Ravi77d572f2023-01-17 23:58:31 +000011046 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011047
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011048 /* Previous attempt to move a GO was not possible -- try again. */
11049 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
11050 WPAS_P2P_CHANNEL_UPDATE_ANY);
11051
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011052 os_free(freqs);
11053}
11054
11055
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011056/*
11057 * Consider moving a GO from its currently used frequency:
11058 * 1. It is possible that due to regulatory consideration the frequency
11059 * can no longer be used and there is a need to evacuate the GO.
11060 * 2. It is possible that due to MCC considerations, it would be preferable
11061 * to move the GO to a channel that is currently used by some other
11062 * station interface.
11063 *
11064 * In case a frequency that became invalid is once again valid, cancel a
11065 * previously initiated GO frequency change.
11066 */
11067static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
11068 struct wpa_used_freq_data *freqs,
11069 unsigned int num)
11070{
11071 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
11072 unsigned int timeout;
11073 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011074 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011075
11076 wpas_p2p_go_update_common_freqs(wpa_s);
11077
11078 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011079 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070011080 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011081 for (i = 0, invalid_freq = 0; i < num; i++) {
11082 if (freqs[i].freq == freq) {
11083 flags = freqs[i].flags;
11084
11085 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011086 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
11087 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011088 wpa_dbg(wpa_s, MSG_DEBUG,
11089 "P2P: Freq=%d MHz no longer valid for GO",
11090 freq);
11091 invalid_freq = 1;
11092 }
11093 } else if (freqs[i].flags == 0) {
11094 /* Freq is not used by any other station interface */
11095 continue;
11096 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070011097 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080011098 /* Freq is not valid for P2P use cases */
11099 continue;
11100 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
11101 P2P_GO_FREQ_MOVE_SCM) {
11102 policy_move = 1;
11103 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
11104 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
11105 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
11106 policy_move = 1;
11107 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
11108 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
11109 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
11110 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
11111 policy_move = 1;
11112 } else if ((wpa_s->drv_flags &
11113 WPA_DRIVER_FLAGS_AP_CSA) &&
11114 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
11115 u8 chan;
11116
11117 /*
11118 * We do not support CSA between bands, so move
11119 * GO only within the same band.
11120 */
11121 if (wpa_s->ap_iface->current_mode->mode ==
11122 ieee80211_freq_to_chan(freqs[i].freq,
11123 &chan))
11124 policy_move = 1;
11125 }
11126 }
11127 }
11128
11129 wpa_dbg(wpa_s, MSG_DEBUG,
11130 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
11131 invalid_freq, policy_move, flags);
11132
11133 /*
11134 * The channel is valid, or we are going to have a policy move, so
11135 * cancel timeout.
11136 */
11137 if (!invalid_freq || policy_move) {
11138 wpa_dbg(wpa_s, MSG_DEBUG,
11139 "P2P: Cancel a GO move from freq=%d MHz", freq);
11140 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
11141
11142 if (wpas_p2p_in_progress(wpa_s)) {
11143 wpa_dbg(wpa_s, MSG_DEBUG,
11144 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
11145 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
11146 wpa_s, NULL);
11147 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
11148 wpas_p2p_reconsider_moving_go,
11149 wpa_s, NULL);
11150 return;
11151 }
11152 }
11153
11154 if (!invalid_freq && (!policy_move || flags != 0)) {
11155 wpa_dbg(wpa_s, MSG_DEBUG,
11156 "P2P: Not initiating a GO frequency change");
11157 return;
11158 }
11159
11160 /*
11161 * Do not consider moving GO if it is in the middle of a CSA. When the
11162 * CSA is finished this flow should be retriggered.
11163 */
11164 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
11165 wpa_dbg(wpa_s, MSG_DEBUG,
11166 "P2P: Not initiating a GO frequency change - CSA is in progress");
11167 return;
11168 }
11169
11170 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
11171 timeout = P2P_GO_FREQ_CHANGE_TIME;
11172 else
11173 timeout = 0;
11174
11175 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
11176 freq, timeout);
11177 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
11178 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
11179}
11180
11181
11182static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
11183 struct wpa_used_freq_data *freqs,
11184 unsigned int num,
11185 enum wpas_p2p_channel_update_trig trig)
11186{
11187 struct wpa_supplicant *ifs;
11188
11189 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
11190 NULL);
11191
11192 /*
11193 * Travers all the radio interfaces, and for each GO interface, check
11194 * if there is a need to move the GO from the frequency it is using,
11195 * or in case the frequency is valid again, cancel the evacuation flow.
11196 */
11197 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
11198 radio_list) {
11199 if (ifs->current_ssid == NULL ||
11200 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
11201 continue;
11202
11203 /*
11204 * The GO was just started or completed channel switch, no need
11205 * to move it.
11206 */
11207 if (wpa_s == ifs &&
11208 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
11209 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
11210 wpa_dbg(wpa_s, MSG_DEBUG,
11211 "P2P: GO move - schedule re-consideration");
11212 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
11213 wpas_p2p_reconsider_moving_go,
11214 wpa_s, NULL);
11215 continue;
11216 }
11217
11218 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
11219 }
11220}
11221
11222
11223void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
11224{
11225 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
11226 return;
11227
11228 wpas_p2p_update_channel_list(wpa_s,
11229 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
11230}
11231
11232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011233void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
11234{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070011235 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
11236 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
11237 "the management interface is being removed");
11238 wpas_p2p_deinit_global(wpa_s->global);
11239 }
11240}
11241
11242
11243void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
11244{
11245 if (wpa_s->ap_iface->bss)
11246 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
11247 wpas_p2p_group_deinit(wpa_s);
11248}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070011249
11250
11251int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
11252 unsigned int period, unsigned int interval,
11253 unsigned int count)
11254{
11255 struct p2p_data *p2p = wpa_s->global->p2p;
11256 u8 *device_types;
11257 size_t dev_types_len;
11258 struct wpabuf *buf;
11259 int ret;
11260
11261 if (wpa_s->p2p_lo_started) {
11262 wpa_dbg(wpa_s, MSG_DEBUG,
11263 "P2P Listen offload is already started");
11264 return 0;
11265 }
11266
11267 if (wpa_s->global->p2p == NULL ||
11268 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
11269 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
11270 return -1;
11271 }
11272
11273 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
11274 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
11275 freq);
11276 return -1;
11277 }
11278
11279 /* Get device type */
11280 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
11281 WPS_DEV_TYPE_LEN;
11282 device_types = os_malloc(dev_types_len);
11283 if (!device_types)
11284 return -1;
11285 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
11286 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
11287 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
11288
11289 /* Get Probe Response IE(s) */
11290 buf = p2p_build_probe_resp_template(p2p, freq);
11291 if (!buf) {
11292 os_free(device_types);
11293 return -1;
11294 }
11295
11296 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
11297 device_types, dev_types_len,
11298 wpabuf_mhead_u8(buf), wpabuf_len(buf));
11299 if (ret < 0)
11300 wpa_dbg(wpa_s, MSG_DEBUG,
11301 "P2P: Failed to start P2P listen offload");
11302
11303 os_free(device_types);
11304 wpabuf_free(buf);
11305
11306 if (ret == 0) {
11307 wpa_s->p2p_lo_started = 1;
11308
11309 /* Stop current P2P listen if any */
11310 wpas_stop_listen(wpa_s);
11311 }
11312
11313 return ret;
11314}
11315
11316
11317int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
11318{
11319 int ret;
11320
11321 if (!wpa_s->p2p_lo_started)
11322 return 0;
11323
11324 ret = wpa_drv_p2p_lo_stop(wpa_s);
11325 if (ret < 0)
11326 wpa_dbg(wpa_s, MSG_DEBUG,
11327 "P2P: Failed to stop P2P listen offload");
11328
11329 wpa_s->p2p_lo_started = 0;
11330 return ret;
11331}
Sunil Ravic0f5d412024-09-11 22:12:49 +000011332
11333
11334struct wpabuf * wpas_p2p_usd_elems(struct wpa_supplicant *wpa_s)
11335{
11336 struct p2p_data *p2p = wpa_s->global->p2p;
11337
11338 if (wpa_s->global->p2p_disabled || !p2p)
11339 return NULL;
11340 return p2p_usd_elems(p2p);
11341}
11342
11343
11344void wpas_p2p_process_usd_elems(struct wpa_supplicant *wpa_s, const u8 *buf,
11345 u16 buf_len, const u8 *peer_addr,
11346 unsigned int freq)
11347{
11348 struct p2p_data *p2p = wpa_s->global->p2p;
11349
11350 if (wpa_s->global->p2p_disabled || !p2p)
11351 return;
11352 p2p_process_usd_elems(p2p, buf, buf_len, peer_addr, freq);
11353}
Sunil Ravi79e6c4f2025-01-04 00:47:06 +000011354
11355
11356#ifdef CONFIG_PASN
11357
11358int wpas_p2p_pasn_auth_rx(struct wpa_supplicant *wpa_s,
11359 const struct ieee80211_mgmt *mgmt, size_t len,
11360 int freq)
11361{
11362 struct p2p_data *p2p = wpa_s->global->p2p;
11363
11364 if (wpa_s->global->p2p_disabled || !p2p)
11365 return -2;
11366 return p2p_pasn_auth_rx(p2p, mgmt, len, freq);
11367}
11368
11369
11370int wpas_p2p_pasn_auth_tx_status(struct wpa_supplicant *wpa_s, const u8 *data,
11371 size_t data_len, bool acked)
11372{
11373 struct p2p_data *p2p = wpa_s->global->p2p;
11374 struct wpa_p2p_pasn_auth_work *awork;
11375
11376 if (!wpa_s->p2p_pasn_auth_work)
11377 return -1;
11378 awork = wpa_s->p2p_pasn_auth_work->ctx;
11379
11380 return p2p_pasn_auth_tx_status(p2p, data, data_len, acked,
11381 awork->verify);
11382}
11383
11384
11385#ifdef CONFIG_TESTING_OPTIONS
11386int wpas_p2p_get_pasn_ptk(struct wpa_supplicant *wpa_s, const u8 **ptk,
11387 size_t *ptk_len)
11388{
11389 struct p2p_data *p2p = wpa_s->global->p2p;
11390
11391 if (wpa_s->global->p2p_disabled || !p2p)
11392 return -2;
11393 return p2p_pasn_get_ptk(p2p, ptk, ptk_len);
11394}
11395#endif /* CONFIG_TESTING_OPTIONS */
11396
11397#endif /* CONFIG_PASN */
11398
11399
11400void wpas_p2p_update_dev_addr(struct wpa_supplicant *wpa_s)
11401{
11402 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
11403 p2p_set_dev_addr(wpa_s->global->p2p, wpa_s->own_addr);
11404}