blob: 9c20ee50995c56a2b8531ae2c023e4e0bfccbd7a [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"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Vinayak Yadawad8db34572021-08-30 21:28:05 +0530107/* Check if frequency is 2GHz */
108#define IS_2GHZ(n) (n >= 2412 && n <= 2484)
109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110enum p2p_group_removal_reason {
111 P2P_GROUP_REMOVAL_UNKNOWN,
112 P2P_GROUP_REMOVAL_SILENT,
113 P2P_GROUP_REMOVAL_FORMATION_FAILED,
114 P2P_GROUP_REMOVAL_REQUESTED,
115 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
116 P2P_GROUP_REMOVAL_UNAVAILABLE,
117 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800119 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
120 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121};
122
Jouni Malinendc7b7132012-09-14 12:53:47 -0700123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
125static struct wpa_supplicant *
126wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
127 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
129 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800130static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
131 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +0000132 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800133 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800134static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
135 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700136static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
137static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700138 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800139 int auto_join, int freq,
140 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700141static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
142static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
143static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
144static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800145static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
146 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800147static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800148static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
149 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800150static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800151static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700152static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700153static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800154static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
155 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700156static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
157 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *freqs,
160 unsigned int num);
161static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
162static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
163static void
164wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
165 struct wpa_used_freq_data *freqs, unsigned int num,
166 enum wpas_p2p_channel_update_trig trig);
167static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Sunil Ravi036cec52023-03-29 11:35:17 -0700168static int wpas_p2p_disallowed_freq(struct wpa_global *global,
169 unsigned int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170
171
Hai Shalomc1a21442022-02-04 13:43:00 -0800172static int wpas_get_6ghz_he_chwidth_capab(struct hostapd_hw_modes *mode)
173{
174 int he_capab = 0;
175
176 if (mode)
177 he_capab = mode->he_capab[WPAS_MODE_INFRA].phy_cap[
178 HE_PHYCAP_CHANNEL_WIDTH_SET_IDX];
179 return he_capab;
180}
181
182
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700183/*
184 * Get the number of concurrent channels that the HW can operate, but that are
185 * currently not in use by any of the wpa_supplicant interfaces.
186 */
187static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
188{
189 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800190 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700191
192 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
193 if (!freqs)
194 return -1;
195
196 num = get_shared_radio_freqs(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000197 wpa_s->num_multichan_concurrent, false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198 os_free(freqs);
199
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800200 unused = wpa_s->num_multichan_concurrent - num;
201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
202 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700203}
204
205
206/*
207 * Get the frequencies that are currently in use by one or more of the virtual
208 * interfaces, and that are also valid for P2P operation.
209 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210static unsigned int
211wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
212 struct wpa_used_freq_data *p2p_freqs,
213 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700215 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700216 unsigned int num, i, j;
217
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700218 freqs = os_calloc(wpa_s->num_multichan_concurrent,
219 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700220 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700221 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700222
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700223 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +0000224 wpa_s->num_multichan_concurrent,
225 false);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700226
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700227 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700228
229 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700230 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700231 p2p_freqs[j++] = freqs[i];
232 }
233
234 os_free(freqs);
235
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700236 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800237
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700238 return j;
239}
240
241
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700242static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
243 int freq)
244{
245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
246 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700247
248 /* Use the wpa_s used to control the P2P Device operation */
249 wpa_s = wpa_s->global->p2p_init_wpa_s;
250
251 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800252 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
253 wpas_p2p_num_unused_channels(wpa_s) > 0) {
254 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
255 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700256 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800257 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700258 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
259}
260
261
Hai Shalom60840252021-02-19 19:02:11 -0800262static void wpas_p2p_scan_res_handled(struct wpa_supplicant *wpa_s)
263{
264 unsigned int delay = wpas_p2p_search_delay(wpa_s);
265
266 /* In case of concurrent P2P and external scans, delay P2P search. */
267 if (external_scan_running(wpa_s->radio)) {
268 delay = wpa_s->conf->p2p_search_delay;
269 wpa_printf(MSG_DEBUG,
270 "P2P: Delay next P2P search by %d ms to let externally triggered scan complete",
271 delay);
272 }
273
274 p2p_scan_res_handled(wpa_s->global->p2p, delay);
275}
276
277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
279 struct wpa_scan_results *scan_res)
280{
281 size_t i;
282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 if (wpa_s->p2p_scan_work) {
284 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
285 wpa_s->p2p_scan_work = NULL;
286 radio_work_done(work);
287 }
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
290 return;
291
292 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
293 (int) scan_res->num);
294
295 for (i = 0; i < scan_res->num; i++) {
296 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700298 const u8 *ies;
299 size_t ies_len;
300
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800301 time_tmp_age.sec = bss->age / 1000;
302 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700304
305 ies = (const u8 *) (bss + 1);
306 ies_len = bss->ie_len;
307 if (bss->beacon_ie_len > 0 &&
308 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
309 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
310 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
311 MACSTR, MAC2STR(bss->bssid));
312 ies = ies + ies_len;
313 ies_len = bss->beacon_ie_len;
314 }
315
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800318 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700319 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 break;
321 }
322
Hai Shalom60840252021-02-19 19:02:11 -0800323 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324}
325
326
Hai Shalom60840252021-02-19 19:02:11 -0800327static void wpas_p2p_scan_res_fail_handler(struct wpa_supplicant *wpa_s)
Hai Shalom899fcc72020-10-19 14:38:18 -0700328{
Hai Shalom60840252021-02-19 19:02:11 -0800329 if (wpa_s->p2p_scan_work) {
330 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
Hai Shalom899fcc72020-10-19 14:38:18 -0700331
Hai Shalom60840252021-02-19 19:02:11 -0800332 wpa_s->p2p_scan_work = NULL;
333 radio_work_done(work);
Hai Shalom899fcc72020-10-19 14:38:18 -0700334 }
335
Hai Shalom60840252021-02-19 19:02:11 -0800336 if (wpa_s->global->p2p_disabled || !wpa_s->global->p2p)
337 return;
Hai Shalom899fcc72020-10-19 14:38:18 -0700338
Hai Shalom60840252021-02-19 19:02:11 -0800339 wpa_dbg(wpa_s, MSG_DEBUG,
340 "P2P: Failed to get scan results - try to continue");
341 wpas_p2p_scan_res_handled(wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -0700342}
343
344
Sunil Ravi77d572f2023-01-17 23:58:31 +0000345void wpas_p2p_scan_freqs(struct wpa_supplicant *wpa_s,
346 struct wpa_driver_scan_params *params,
347 bool include_6ghz)
348{
349 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
350 params, false, false, false);
351 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
352 params, false, false, false);
353 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211AD,
354 params, false, false, false);
355 if (!wpa_s->conf->p2p_6ghz_disable &&
356 is_p2p_allow_6ghz(wpa_s->global->p2p) && include_6ghz)
357 wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
358 params, true, true, false);
359}
360
361
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800362static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
363{
364 struct wpa_supplicant *wpa_s = work->wpa_s;
365 struct wpa_driver_scan_params *params = work->ctx;
366 int ret;
367
368 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800369 if (!work->started) {
370 wpa_scan_free_params(params);
371 return;
372 }
373
374 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 return;
376 }
377
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800378 if (wpa_s->clear_driver_scan_cache) {
379 wpa_printf(MSG_DEBUG,
380 "Request driver to clear scan cache due to local BSS flush");
381 params->only_new_results = 1;
382 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700383
Sunil Ravi77d572f2023-01-17 23:58:31 +0000384 if (!params->freqs)
385 wpas_p2p_scan_freqs(wpa_s, params, params->p2p_include_6ghz);
386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800387 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800388 if (ret == 0)
389 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 wpa_scan_free_params(params);
391 work->ctx = NULL;
392 if (ret) {
393 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800394 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800395 return;
396 }
397
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800399 os_get_reltime(&wpa_s->scan_trigger_time);
400 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Hai Shalom60840252021-02-19 19:02:11 -0800401 wpa_s->scan_res_fail_handler = wpas_p2p_scan_res_fail_handler;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800402 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800403 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800404 wpa_s->p2p_scan_work = work;
405}
406
407
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800408static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
409 int freq)
410{
411 if (wpa_s->global->p2p_24ghz_social_channels &&
412 (freq == 2412 || freq == 2437 || freq == 2462)) {
413 /*
414 * Search all social channels regardless of whether these have
415 * been disabled for P2P operating channel use to avoid missing
416 * peers.
417 */
418 return 1;
419 }
420 return p2p_supported_freq(wpa_s->global->p2p, freq);
421}
422
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
425 unsigned int num_req_dev_types,
Hai Shalomc1a21442022-02-04 13:43:00 -0800426 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id,
427 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428{
429 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800430 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700432 unsigned int num_channels = 0;
433 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700435 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800436 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
439 return -1;
440
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800441 if (wpa_s->p2p_scan_work) {
442 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
443 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700444 }
445
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800446 params = os_zalloc(sizeof(*params));
447 if (params == NULL)
448 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449
450 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800451 params->num_ssids = 1;
452 n = os_malloc(P2P_WILDCARD_SSID_LEN);
453 if (n == NULL)
454 goto fail;
455 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
456 params->ssids[0].ssid = n;
457 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458
459 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700460 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
461 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 num_req_dev_types, req_dev_types);
463 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800464 goto fail;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000465
466 /*
467 * In case 6 GHz channels are requested as part of the P2P scan, only
468 * the PSCs would be included as P2P GOs are not expected to be
469 * collocated, i.e., they would not be announced in the RNR element of
470 * other APs.
471 */
Hai Shalomc1a21442022-02-04 13:43:00 -0800472 if (!wpa_s->conf->p2p_6ghz_disable)
473 params->p2p_include_6ghz = include_6ghz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 switch (type) {
475 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700476 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
477 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800478 if (params->freqs == NULL)
479 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700480 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800481 if (wpas_p2p_search_social_channel(
482 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700483 params->freqs[num_channels++] =
484 social_channels_freq[i];
485 }
486 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 break;
488 case P2P_SCAN_FULL:
489 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800490 case P2P_SCAN_SPECIFIC:
491 params->freqs = os_calloc(2, sizeof(int));
492 if (params->freqs == NULL)
493 goto fail;
494 params->freqs[0] = freq;
495 params->freqs[1] = 0;
496 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700498 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
499 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800500 if (params->freqs == NULL)
501 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700502 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800503 if (wpas_p2p_search_social_channel(
504 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700505 params->freqs[num_channels++] =
506 social_channels_freq[i];
507 }
508 if (p2p_supported_freq(wpa_s->global->p2p, freq))
509 params->freqs[num_channels++] = freq;
510 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 break;
512 }
513
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800514 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
515 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
516 if (ies == NULL) {
517 wpabuf_free(wps_ie);
518 goto fail;
519 }
520 wpabuf_put_buf(ies, wps_ie);
521 wpabuf_free(wps_ie);
522
523 bands = wpas_get_bands(wpa_s, params->freqs);
524 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
525
526 params->p2p_probe = 1;
527 n = os_malloc(wpabuf_len(ies));
528 if (n == NULL) {
529 wpabuf_free(ies);
530 goto fail;
531 }
532 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
533 params->extra_ies = n;
534 params->extra_ies_len = wpabuf_len(ies);
535 wpabuf_free(ies);
536
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800537 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800538 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
539 params) < 0)
540 goto fail;
541 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543fail:
544 wpa_scan_free_params(params);
545 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546}
547
548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
550{
551 switch (p2p_group_interface) {
552 case P2P_GROUP_INTERFACE_PENDING:
553 return WPA_IF_P2P_GROUP;
554 case P2P_GROUP_INTERFACE_GO:
555 return WPA_IF_P2P_GO;
556 case P2P_GROUP_INTERFACE_CLIENT:
557 return WPA_IF_P2P_CLIENT;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000558 default:
559 return WPA_IF_P2P_GROUP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561}
562
563
564static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
565 const u8 *ssid,
566 size_t ssid_len, int *go)
567{
568 struct wpa_ssid *s;
569
570 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
571 for (s = wpa_s->conf->ssid; s; s = s->next) {
572 if (s->disabled != 0 || !s->p2p_group ||
573 s->ssid_len != ssid_len ||
574 os_memcmp(ssid, s->ssid, ssid_len) != 0)
575 continue;
576 if (s->mode == WPAS_MODE_P2P_GO &&
577 s != wpa_s->current_ssid)
578 continue;
579 if (go)
580 *go = s->mode == WPAS_MODE_P2P_GO;
581 return wpa_s;
582 }
583 }
584
585 return NULL;
586}
587
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
590{
591 struct wpa_supplicant *wpa_s = eloop_ctx;
592 wpa_printf(MSG_DEBUG,
593 "P2P: Complete previously requested removal of %s",
594 wpa_s->ifname);
595 wpas_p2p_disconnect(wpa_s);
596}
597
598
599static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
600 struct wpa_supplicant *calling_wpa_s)
601{
602 if (calling_wpa_s == wpa_s && wpa_s &&
603 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
604 /*
605 * The calling wpa_s instance is going to be removed. Do that
606 * from an eloop callback to keep the instance available until
Hai Shalom899fcc72020-10-19 14:38:18 -0700607 * the caller has returned. This may be needed, e.g., to provide
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 * control interface responses on the per-interface socket.
609 */
610 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
611 wpa_s, NULL) < 0)
612 return -1;
613 return 0;
614 }
615
616 return wpas_p2p_disconnect(wpa_s);
617}
618
619
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800620/* Determine total number of clients in active groups where we are the GO */
621static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
622{
623 unsigned int count = 0;
624 struct wpa_ssid *s;
625
626 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
627 for (s = wpa_s->conf->ssid; s; s = s->next) {
628 wpa_printf(MSG_DEBUG,
629 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
630 wpa_s, s, s->disabled, s->p2p_group,
631 s->mode);
632 if (!s->disabled && s->p2p_group &&
633 s->mode == WPAS_MODE_P2P_GO) {
634 count += p2p_get_group_num_members(
635 wpa_s->p2p_group);
636 }
637 }
638 }
639
640 return count;
641}
642
643
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800644static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
645{
646 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
647 !wpa_s->current_ssid->disabled &&
648 wpa_s->current_ssid->p2p_group &&
649 wpa_s->current_ssid->p2p_persistent_group;
650}
651
652
653static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
654{
655 return p2p_is_active_persistent_group(wpa_s) &&
656 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
657}
658
659
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800660/* Find an interface for a P2P group where we are the GO */
661static struct wpa_supplicant *
662wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
663{
664 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665
666 if (!wpa_s)
667 return NULL;
668
669 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800670 if (!p2p_is_active_persistent_go(wpa_s))
671 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800672
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800673 /* Prefer a group with connected clients */
674 if (p2p_get_group_num_members(wpa_s->p2p_group))
675 return wpa_s;
676 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677 }
678
679 /* No group with connected clients, so pick the one without (if any) */
680 return save;
681}
682
683
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800684static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800685{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 return p2p_is_active_persistent_group(wpa_s) &&
687 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
688}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800689
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800691/* Find an interface for a P2P group where we are the P2P Client */
692static struct wpa_supplicant *
693wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
694{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800695 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (p2p_is_active_persistent_cli(wpa_s))
697 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800698 }
699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800700 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800701}
702
703
704/* Find a persistent group where we are the GO */
705static struct wpa_ssid *
706wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
707{
708 struct wpa_ssid *s;
709
710 for (s = wpa_s->conf->ssid; s; s = s->next) {
711 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
712 return s;
713 }
714
715 return NULL;
716}
717
718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800719static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
720 unsigned int *force_freq,
721 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800722{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800723 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 struct wpa_ssid *s;
725 u8 conncap = P2PS_SETUP_NONE;
726 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800727 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800728 struct wpa_ssid *persistent_go;
729 int p2p_no_group_iface;
Sunil8cd6f4d2022-06-28 18:40:46 +0000730 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
731 unsigned int size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732
733 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
734
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800735 if (force_freq)
736 *force_freq = 0;
737 if (pref_freq)
738 *pref_freq = 0;
739
740 size = P2P_MAX_PREF_CHANNELS;
741 if (force_freq && pref_freq &&
742 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
743 (int *) pref_freq, 0, pref_freq_list, &size))
744 wpas_p2p_set_own_freq_preference(wpa_s,
745 *force_freq ? *force_freq :
746 *pref_freq);
747
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800748 /*
749 * For non-concurrent capable devices:
750 * If persistent_go, then no new.
751 * If GO, then no client.
752 * If client, then no GO.
753 */
754 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800755 if (go_wpa_s)
756 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800757 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800758 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
759 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800760
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800761 wpa_printf(MSG_DEBUG,
762 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
763 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800764
765 /* If not concurrent, restrict our choices */
766 if (p2p_no_group_iface) {
767 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
768
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800769 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800770 return P2PS_SETUP_NONE;
771
772 if (go_wpa_s) {
773 if (role == P2PS_SETUP_CLIENT ||
774 incoming == P2PS_SETUP_GROUP_OWNER ||
775 p2p_client_limit_reached(go_wpa_s->p2p_group))
776 return P2PS_SETUP_NONE;
777
778 return P2PS_SETUP_GROUP_OWNER;
779 }
780
781 if (persistent_go) {
782 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
783 if (!incoming)
784 return P2PS_SETUP_GROUP_OWNER |
785 P2PS_SETUP_CLIENT;
786 if (incoming == P2PS_SETUP_NEW) {
787 u8 r;
788
789 if (os_get_random(&r, sizeof(r)) < 0 ||
790 (r & 1))
791 return P2PS_SETUP_CLIENT;
792 return P2PS_SETUP_GROUP_OWNER;
793 }
794 }
795 }
796 }
797
798 /* If a required role has been specified, handle it here */
799 if (role && role != P2PS_SETUP_NEW) {
800 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800801 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
802 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
803 /*
804 * Peer has an active GO, so if the role allows it and
805 * we do not have any active roles, become client.
806 */
807 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
808 !cli_wpa_s)
809 return P2PS_SETUP_CLIENT;
810
811 /* fall through */
812
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 case P2PS_SETUP_NONE:
814 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 conncap = role;
816 goto grp_owner;
817
818 case P2PS_SETUP_GROUP_OWNER:
819 /*
820 * Must be a complimentary role - cannot be a client to
821 * more than one peer.
822 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800823 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800824 return P2PS_SETUP_NONE;
825
826 return P2PS_SETUP_CLIENT;
827
828 case P2PS_SETUP_CLIENT:
829 /* Must be a complimentary role */
830 if (incoming != role) {
831 conncap = P2PS_SETUP_GROUP_OWNER;
832 goto grp_owner;
833 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700834 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800835
836 default:
837 return P2PS_SETUP_NONE;
838 }
839 }
840
841 /*
842 * For now, we only will support ownership of one group, and being a
843 * client of one group. Therefore, if we have either an existing GO
844 * group, or an existing client group, we will not do a new GO
845 * negotiation, but rather try to re-use the existing groups.
846 */
847 switch (incoming) {
848 case P2PS_SETUP_NONE:
849 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800851 conncap = P2PS_SETUP_GROUP_OWNER;
852 else if (!owned_members)
853 conncap = P2PS_SETUP_NEW;
854 else if (incoming == P2PS_SETUP_NONE)
855 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
856 else
857 conncap = P2PS_SETUP_CLIENT;
858 break;
859
860 case P2PS_SETUP_CLIENT:
861 conncap = P2PS_SETUP_GROUP_OWNER;
862 break;
863
864 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800866 conncap = P2PS_SETUP_CLIENT;
867 break;
868
869 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
870 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800871 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800872 conncap = P2PS_SETUP_GROUP_OWNER;
873 else {
874 u8 r;
875
876 if (os_get_random(&r, sizeof(r)) < 0 ||
877 (r & 1))
878 conncap = P2PS_SETUP_CLIENT;
879 else
880 conncap = P2PS_SETUP_GROUP_OWNER;
881 }
882 break;
883
884 default:
885 return P2PS_SETUP_NONE;
886 }
887
888grp_owner:
889 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
890 (!incoming && (conncap & P2PS_SETUP_NEW))) {
891 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
892 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800893
894 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800895 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800896 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800897 wpa_s->p2p_mgmt ?
898 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800899 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800900 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800901 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800902 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800903 wpa_printf(MSG_ERROR,
904 "P2P: Failed to allocate a new interface for the group");
905 return P2PS_SETUP_NONE;
906 }
907 wpa_s->global->pending_group_iface_for_p2ps = 1;
908 p2p_set_intended_addr(wpa_s->global->p2p,
909 wpa_s->pending_interface_addr);
910 }
911 }
912
913 return conncap;
914}
915
916
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
918 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919{
920 struct wpa_ssid *ssid;
921 char *gtype;
922 const char *reason;
923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 ssid = wpa_s->current_ssid;
925 if (ssid == NULL) {
926 /*
927 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700928 * pending P2P group interface waiting for provisioning or a
929 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 */
931 ssid = wpa_s->conf->ssid;
932 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700933 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 break;
935 ssid = ssid->next;
936 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300937 if (ssid == NULL &&
938 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
939 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
941 "not found");
942 return -1;
943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 }
945 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
946 gtype = "GO";
947 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
948 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
949 wpa_s->reassociate = 0;
950 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 gtype = "client";
952 } else
953 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700954
955 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
956 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800958 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700959 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800960 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
961 wpa_s, NULL)) {
962 wpa_printf(MSG_DEBUG,
963 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
964 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
965 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
966 wpa_s, NULL);
967 }
968 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 if (wpa_s->cross_connect_in_use) {
971 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800972 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700973 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
974 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700976 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 case P2P_GROUP_REMOVAL_REQUESTED:
978 reason = " reason=REQUESTED";
979 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
981 reason = " reason=FORMATION_FAILED";
982 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
984 reason = " reason=IDLE";
985 break;
986 case P2P_GROUP_REMOVAL_UNAVAILABLE:
987 reason = " reason=UNAVAILABLE";
988 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
990 reason = " reason=GO_ENDING_SESSION";
991 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700992 case P2P_GROUP_REMOVAL_PSK_FAILURE:
993 reason = " reason=PSK_FAILURE";
994 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800995 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
996 reason = " reason=FREQ_CONFLICT";
997 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 default:
999 reason = "";
1000 break;
1001 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001002 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001003 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001004 P2P_EVENT_GROUP_REMOVED "%s %s%s",
1005 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001006 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001008 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
1009 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001010 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
1011 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001012 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001013 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001014 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
1015 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001016 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001018 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001019
Dmitry Shmidt15907092014-03-25 10:42:57 -07001020 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001021 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001022 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
1023 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -07001024
Dmitry Shmidt96571392013-10-14 12:54:46 -07001025 /*
1026 * Make sure wait for the first client does not remain active after the
1027 * group has been removed.
1028 */
1029 wpa_s->global->p2p_go_wait_client.sec = 0;
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
1032 struct wpa_global *global;
1033 char *ifname;
1034 enum wpa_driver_if_type type;
1035 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
1036 wpa_s->ifname);
1037 global = wpa_s->global;
1038 ifname = os_strdup(wpa_s->ifname);
1039 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07001041 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s = global->ifaces;
1043 if (wpa_s && ifname)
1044 wpa_drv_if_remove(wpa_s, type, ifname);
1045 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +03001046 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 }
1048
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001049 /*
1050 * The primary interface was used for P2P group operations, so
1051 * need to reset its p2pdev.
1052 */
1053 wpa_s->p2pdev = wpa_s->parent;
1054
Dmitry Shmidt96571392013-10-14 12:54:46 -07001055 if (!wpa_s->p2p_go_group_formation_completed) {
1056 wpa_s->global->p2p_group_formation = NULL;
1057 wpa_s->p2p_in_provisioning = 0;
1058 }
1059
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001060 wpa_s->show_group_started = 0;
1061 os_free(wpa_s->go_params);
1062 wpa_s->go_params = NULL;
1063
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001064 os_free(wpa_s->p2p_group_common_freqs);
1065 wpa_s->p2p_group_common_freqs = NULL;
1066 wpa_s->p2p_group_common_freqs_num = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -07001067 wpa_s->p2p_go_do_acs = 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08001068 wpa_s->p2p_go_allow_dfs = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001069
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001070 wpa_s->waiting_presence_resp = 0;
1071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
1073 if (ssid && (ssid->p2p_group ||
1074 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
1075 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
1076 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001077 if (ssid == wpa_s->current_ssid) {
1078 wpa_sm_set_config(wpa_s->wpa, NULL);
1079 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001081 }
1082 /*
1083 * Networks objects created during any P2P activities are not
1084 * exposed out as they might/will confuse certain non-P2P aware
1085 * applications since these network objects won't behave like
1086 * regular ones.
1087 *
1088 * Likewise, we don't send out network removed signals for such
1089 * network objects.
1090 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001091 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 wpa_config_remove_network(wpa_s->conf, id);
1093 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001094 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 } else {
1096 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1097 "found");
1098 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001099 if (wpa_s->ap_iface)
1100 wpa_supplicant_ap_deinit(wpa_s);
1101 else
1102 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001103
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001104 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1105
Sunil Ravi99c035e2024-07-12 01:42:03 +00001106 wpa_s->p2p_go_no_pri_sec_switch = 0;
1107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001108 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109}
1110
1111
1112static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1113 u8 *go_dev_addr,
1114 const u8 *ssid, size_t ssid_len)
1115{
1116 struct wpa_bss *bss;
1117 const u8 *bssid;
1118 struct wpabuf *p2p;
1119 u8 group_capab;
1120 const u8 *addr;
1121
1122 if (wpa_s->go_params)
1123 bssid = wpa_s->go_params->peer_interface_addr;
1124 else
1125 bssid = wpa_s->bssid;
1126
1127 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001128 if (bss == NULL && wpa_s->go_params &&
1129 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1130 bss = wpa_bss_get_p2p_dev_addr(
1131 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 if (bss == NULL) {
1133 u8 iface_addr[ETH_ALEN];
1134 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1135 iface_addr) == 0)
1136 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1137 }
1138 if (bss == NULL) {
1139 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1140 "group is persistent - BSS " MACSTR " not found",
1141 MAC2STR(bssid));
1142 return 0;
1143 }
1144
1145 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001146 if (p2p == NULL)
1147 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1148 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 if (p2p == NULL) {
1150 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1151 "group is persistent - BSS " MACSTR
1152 " did not include P2P IE", MAC2STR(bssid));
1153 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001154 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 wpa_hexdump(MSG_DEBUG, "P2P: Beacon 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 bss->beacon_ie_len);
1158 return 0;
1159 }
1160
1161 group_capab = p2p_get_group_capab(p2p);
1162 addr = p2p_get_go_dev_addr(p2p);
1163 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1164 "group_capab=0x%x", group_capab);
1165 if (addr) {
1166 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1167 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1168 MAC2STR(addr));
1169 } else
1170 os_memset(go_dev_addr, 0, ETH_ALEN);
1171 wpabuf_free(p2p);
1172
1173 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1174 "go_dev_addr=" MACSTR,
1175 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1176
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001177 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178}
1179
1180
Jouni Malinen75ecf522011-06-27 15:19:46 -07001181static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1182 struct wpa_ssid *ssid,
1183 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184{
1185 struct wpa_ssid *s;
1186 int changed = 0;
1187
1188 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1189 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1190 for (s = wpa_s->conf->ssid; s; s = s->next) {
1191 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001192 ether_addr_equal(go_dev_addr, s->bssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 s->ssid_len == ssid->ssid_len &&
1194 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1195 break;
1196 }
1197
1198 if (s) {
1199 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1200 "entry");
1201 if (ssid->passphrase && !s->passphrase)
1202 changed = 1;
1203 else if (ssid->passphrase && s->passphrase &&
1204 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1205 changed = 1;
1206 } else {
1207 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1208 "entry");
1209 changed = 1;
1210 s = wpa_config_add_network(wpa_s->conf);
1211 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001212 return -1;
1213
1214 /*
1215 * Instead of network_added we emit persistent_group_added
1216 * notification. Also to keep the defense checks in
1217 * persistent_group obj registration method, we set the
1218 * relevant flags in s to designate it as a persistent group.
1219 */
1220 s->p2p_group = 1;
1221 s->p2p_persistent_group = 1;
1222 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 wpa_config_set_network_defaults(s);
1224 }
1225
1226 s->p2p_group = 1;
1227 s->p2p_persistent_group = 1;
1228 s->disabled = 2;
1229 s->bssid_set = 1;
1230 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1231 s->mode = ssid->mode;
1232 s->auth_alg = WPA_AUTH_ALG_OPEN;
1233 s->key_mgmt = WPA_KEY_MGMT_PSK;
1234 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001235 s->pbss = ssid->pbss;
1236 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 s->export_keys = 1;
1238 if (ssid->passphrase) {
1239 os_free(s->passphrase);
1240 s->passphrase = os_strdup(ssid->passphrase);
1241 }
1242 if (ssid->psk_set) {
1243 s->psk_set = 1;
1244 os_memcpy(s->psk, ssid->psk, 32);
1245 }
1246 if (s->passphrase && !s->psk_set)
1247 wpa_config_update_psk(s);
1248 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1249 os_free(s->ssid);
1250 s->ssid = os_malloc(ssid->ssid_len);
1251 }
1252 if (s->ssid) {
1253 s->ssid_len = ssid->ssid_len;
1254 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1255 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001256 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1257 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1258 wpa_s->global->add_psk = NULL;
1259 changed = 1;
1260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 if (changed && wpa_s->conf->update_config &&
1263 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1264 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1265 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001266
1267 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268}
1269
1270
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1272 const u8 *addr)
1273{
1274 struct wpa_ssid *ssid, *s;
1275 u8 *n;
1276 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001277 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001278 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001279
1280 ssid = wpa_s->current_ssid;
1281 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1282 !ssid->p2p_persistent_group)
1283 return;
1284
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001285 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001286 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1287 continue;
1288
1289 if (s->ssid_len == ssid->ssid_len &&
1290 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1291 break;
1292 }
1293
1294 if (s == NULL)
1295 return;
1296
1297 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001298 if (!ether_addr_equal(s->p2p_client_list + i * 2 * ETH_ALEN,
1299 addr))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001300 continue;
1301
1302 if (i == s->num_p2p_clients - 1)
1303 return; /* already the most recent entry */
1304
1305 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001306 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1307 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1308 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001309 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001310 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1311 ETH_ALEN);
1312 os_memset(s->p2p_client_list +
1313 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1314 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001315 found = 1;
1316 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001317 }
1318
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001319 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1320 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001321 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001322 if (n == NULL)
1323 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001324 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1325 ETH_ALEN);
1326 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1327 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001328 s->p2p_client_list = n;
1329 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001330 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001331 /* Not enough room for an additional entry - drop the oldest
1332 * entry */
1333 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001334 s->p2p_client_list + 2 * ETH_ALEN,
1335 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001337 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001339 os_memset(s->p2p_client_list +
1340 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1341 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001342 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001343
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001344 if (p2p_wpa_s->conf->update_config &&
1345 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001347}
1348
1349
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001350static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1351 int go, struct wpa_ssid *ssid, int freq,
1352 const u8 *psk, const char *passphrase,
1353 const u8 *go_dev_addr, int persistent,
1354 const char *extra)
1355{
1356 const char *ssid_txt;
1357 char psk_txt[65];
1358
1359 if (psk)
1360 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1361 else
1362 psk_txt[0] = '\0';
1363
1364 if (ssid)
1365 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1366 else
1367 ssid_txt = "";
1368
1369 if (passphrase && passphrase[0] == '\0')
1370 passphrase = NULL;
1371
1372 /*
1373 * Include PSK/passphrase only in the control interface message and
1374 * leave it out from the debug log entry.
1375 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001376 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001377 P2P_EVENT_GROUP_STARTED
1378 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1379 MACSTR "%s%s",
1380 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1381 psk ? " psk=" : "", psk_txt,
1382 passphrase ? " passphrase=\"" : "",
1383 passphrase ? passphrase : "",
1384 passphrase ? "\"" : "",
1385 MAC2STR(go_dev_addr),
1386 persistent ? " [PERSISTENT]" : "", extra);
1387 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1388 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1389 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1390 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1391 extra);
1392}
1393
1394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001396 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397{
1398 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 int client;
1400 int persistent;
1401 u8 go_dev_addr[ETH_ALEN];
1402
1403 /*
1404 * This callback is likely called for the main interface. Update wpa_s
1405 * to use the group interface if a new interface was created for the
1406 * group.
1407 */
1408 if (wpa_s->global->p2p_group_formation)
1409 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001410 if (wpa_s->p2p_go_group_formation_completed) {
1411 wpa_s->global->p2p_group_formation = NULL;
1412 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001413 } else if (wpa_s->p2p_in_provisioning && !success) {
1414 wpa_msg(wpa_s, MSG_DEBUG,
1415 "P2P: Stop provisioning state due to failure");
1416 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001417 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001418 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001419 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001420 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
1422 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001423 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001424 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001425 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001426 if (already_deleted)
1427 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001428 wpas_p2p_group_delete(wpa_s,
1429 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 return;
1431 }
1432
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001433 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001434 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435
1436 ssid = wpa_s->current_ssid;
1437 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1438 ssid->mode = WPAS_MODE_P2P_GO;
1439 p2p_group_notif_formation_done(wpa_s->p2p_group);
1440 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1441 }
1442
1443 persistent = 0;
1444 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 client = ssid->mode == WPAS_MODE_INFRA;
1446 if (ssid->mode == WPAS_MODE_P2P_GO) {
1447 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001448 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1449 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001450 } else {
1451 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 persistent = wpas_p2p_persistent_group(wpa_s,
1453 go_dev_addr,
1454 ssid->ssid,
1455 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 client = wpa_s->p2p_group_interface ==
1459 P2P_GROUP_INTERFACE_CLIENT;
1460 os_memset(go_dev_addr, 0, ETH_ALEN);
1461 }
1462
1463 wpa_s->show_group_started = 0;
1464 if (client) {
1465 /*
1466 * Indicate event only after successfully completed 4-way
1467 * handshake, i.e., when the interface is ready for data
1468 * packets.
1469 */
1470 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001472 wpas_p2p_group_started(wpa_s, 1, ssid,
1473 ssid ? ssid->frequency : 0,
1474 ssid && ssid->passphrase == NULL &&
1475 ssid->psk_set ? ssid->psk : NULL,
1476 ssid ? ssid->passphrase : NULL,
1477 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 wpas_p2p_cross_connect_setup(wpa_s);
1479 wpas_p2p_set_group_idle_timeout(wpa_s);
1480 }
1481
1482 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001483 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1484 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001485 else {
1486 os_free(wpa_s->global->add_psk);
1487 wpa_s->global->add_psk = NULL;
1488 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001489
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001490 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001491 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001492 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494}
1495
1496
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001497struct send_action_work {
1498 unsigned int freq;
1499 u8 dst[ETH_ALEN];
1500 u8 src[ETH_ALEN];
1501 u8 bssid[ETH_ALEN];
1502 size_t len;
1503 unsigned int wait_time;
1504 u8 buf[0];
1505};
1506
1507
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001508static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1509{
1510 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1511
1512 wpa_printf(MSG_DEBUG,
1513 "P2P: Free Action frame radio work @%p (freq=%u dst="
1514 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1515 wpa_s->p2p_send_action_work, awork->freq,
1516 MAC2STR(awork->dst), MAC2STR(awork->src),
1517 MAC2STR(awork->bssid), awork->wait_time);
1518 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1519 awork->buf, awork->len);
1520 os_free(awork);
1521 wpa_s->p2p_send_action_work->ctx = NULL;
1522 radio_work_done(wpa_s->p2p_send_action_work);
1523 wpa_s->p2p_send_action_work = NULL;
1524}
1525
1526
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001527static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1528 void *timeout_ctx)
1529{
1530 struct wpa_supplicant *wpa_s = eloop_ctx;
1531
1532 if (!wpa_s->p2p_send_action_work)
1533 return;
1534
1535 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001536 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001537}
1538
1539
Dmitry Shmidt03658832014-08-13 11:03:49 -07001540static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001542 if (wpa_s->p2p_send_action_work) {
1543 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001544
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001545 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001546 wpa_printf(MSG_DEBUG,
1547 "P2P: Clear Action TX work @%p (wait_time=%u)",
1548 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001549 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001550 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001551 } else {
1552 /*
1553 * In theory, this should not be needed, but number of
1554 * places in the P2P code is still using non-zero wait
1555 * time for the last Action frame in the sequence and
1556 * some of these do not call send_action_done().
1557 */
1558 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1559 wpa_s, NULL);
1560 eloop_register_timeout(
1561 0, awork->wait_time * 1000,
1562 wpas_p2p_send_action_work_timeout,
1563 wpa_s, NULL);
1564 }
1565 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001566}
1567
1568
1569static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1570 unsigned int freq,
1571 const u8 *dst, const u8 *src,
1572 const u8 *bssid,
1573 const u8 *data, size_t data_len,
1574 enum offchannel_send_action_result
1575 result)
1576{
1577 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1578
1579 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1582 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 switch (result) {
1585 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1586 res = P2P_SEND_ACTION_SUCCESS;
1587 break;
1588 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1589 res = P2P_SEND_ACTION_NO_ACK;
1590 break;
1591 case OFFCHANNEL_SEND_ACTION_FAILED:
1592 res = P2P_SEND_ACTION_FAILED;
1593 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 }
1595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001596 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001598 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1599 wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001600 (ether_addr_equal(dst, wpa_s->pending_join_dev_addr) ||
1601 ether_addr_equal(dst, wpa_s->pending_join_iface_addr)) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001602 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001604 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1605 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001606 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001607 P2P_EVENT_FALLBACK_TO_GO_NEG
1608 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001609 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1610 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 }
1612}
1613
1614
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001615static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1616{
1617 struct wpa_supplicant *wpa_s = work->wpa_s;
1618 struct send_action_work *awork = work->ctx;
1619
1620 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001621 if (work->started) {
1622 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1623 wpa_s, NULL);
1624 wpa_s->p2p_send_action_work = NULL;
1625 offchannel_send_action_done(wpa_s);
1626 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001627 os_free(awork);
1628 return;
1629 }
1630
1631 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1632 awork->bssid, awork->buf, awork->len,
1633 awork->wait_time,
1634 wpas_p2p_send_action_tx_status, 1) < 0) {
1635 os_free(awork);
1636 radio_work_done(work);
1637 return;
1638 }
1639 wpa_s->p2p_send_action_work = work;
1640}
1641
1642
1643static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1644 unsigned int freq, const u8 *dst,
1645 const u8 *src, const u8 *bssid, const u8 *buf,
1646 size_t len, unsigned int wait_time)
1647{
1648 struct send_action_work *awork;
1649
Hai Shalom81f62d82019-07-22 12:10:00 -07001650 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001651 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1652 return -1;
1653 }
1654
1655 awork = os_zalloc(sizeof(*awork) + len);
1656 if (awork == NULL)
1657 return -1;
1658
1659 awork->freq = freq;
1660 os_memcpy(awork->dst, dst, ETH_ALEN);
1661 os_memcpy(awork->src, src, ETH_ALEN);
1662 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1663 awork->len = len;
1664 awork->wait_time = wait_time;
1665 os_memcpy(awork->buf, buf, len);
1666
Hai Shalom81f62d82019-07-22 12:10:00 -07001667 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001668 wpas_send_action_cb, awork) < 0) {
1669 os_free(awork);
1670 return -1;
1671 }
1672
1673 return 0;
1674}
1675
1676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1678 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001679 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680{
1681 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001682 int listen_freq = -1, send_freq = -1;
1683
Hai Shalom021b0b52019-04-10 11:17:58 -07001684 if (scheduled)
1685 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001686 if (wpa_s->p2p_listen_work)
1687 listen_freq = wpa_s->p2p_listen_work->freq;
1688 if (wpa_s->p2p_send_action_work)
1689 send_freq = wpa_s->p2p_send_action_work->freq;
1690 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001691 int res;
1692
1693 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1694 listen_freq, send_freq, freq);
1695 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1696 len, wait_time);
1697 if (res == 0 && scheduled)
1698 *scheduled = 1;
1699 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001700 }
1701
1702 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1704 wait_time,
1705 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706}
1707
1708
1709static void wpas_send_action_done(void *ctx)
1710{
1711 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001712
1713 if (wpa_s->p2p_send_action_work) {
1714 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1715 wpa_s, NULL);
1716 os_free(wpa_s->p2p_send_action_work->ctx);
1717 radio_work_done(wpa_s->p2p_send_action_work);
1718 wpa_s->p2p_send_action_work = NULL;
1719 }
1720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722}
1723
1724
1725static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1726 struct p2p_go_neg_results *params)
1727{
1728 if (wpa_s->go_params == NULL) {
1729 wpa_s->go_params = os_malloc(sizeof(*params));
1730 if (wpa_s->go_params == NULL)
1731 return -1;
1732 }
1733 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1734 return 0;
1735}
1736
1737
1738static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1739 struct p2p_go_neg_results *res)
1740{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001741 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001742 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1743 " dev_addr " MACSTR " wps_method %d",
1744 MAC2STR(res->peer_interface_addr),
1745 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1747 res->ssid, res->ssid_len);
1748 wpa_supplicant_ap_deinit(wpa_s);
1749 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001750 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001751 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001752#ifdef CONFIG_WPS_NFC
1753 } else if (res->wps_method == WPS_NFC) {
1754 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1755 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001756 wpa_s->p2pdev->p2p_oob_dev_pw,
1757 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1758 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001759 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001760 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001761 NULL,
1762 NULL, 0, 0);
1763#endif /* CONFIG_WPS_NFC */
1764 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001766 if (wpa_s->p2p_wps_method == WPS_P2PS)
1767 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1769 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1770 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1771 wpa_s->p2p_pin, 1, dev_pw_id);
1772 }
1773}
1774
1775
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001776static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1777 struct wpa_ssid *ssid)
1778{
1779 struct wpa_ssid *persistent;
1780 struct psk_list_entry *psk;
1781 struct hostapd_data *hapd;
1782
1783 if (!wpa_s->ap_iface)
1784 return;
1785
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001786 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001787 ssid->ssid_len);
1788 if (persistent == NULL)
1789 return;
1790
1791 hapd = wpa_s->ap_iface->bss[0];
1792
1793 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1794 list) {
1795 struct hostapd_wpa_psk *hpsk;
1796
1797 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1798 MACSTR " psk=%d",
1799 MAC2STR(psk->addr), psk->p2p);
1800 hpsk = os_zalloc(sizeof(*hpsk));
1801 if (hpsk == NULL)
1802 break;
1803 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1804 if (psk->p2p)
1805 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1806 else
1807 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1808 hpsk->next = hapd->conf->ssid.wpa_psk;
1809 hapd->conf->ssid.wpa_psk = hpsk;
1810 }
1811}
1812
1813
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001814static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1815{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001816 char buf[20 + P2P_MAX_CHANNELS * 6];
1817 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001818 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001819 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001820
Roshan Pius3a1667e2018-07-03 15:17:14 -07001821 pos = buf;
1822 end = pos + sizeof(buf);
1823 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1824 res = os_snprintf(pos, end - pos, " %d",
1825 wpa_s->p2p_group_common_freqs[i]);
1826 if (os_snprintf_error(end - pos, res))
1827 break;
1828 pos += res;
1829 }
1830 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831
Roshan Pius3a1667e2018-07-03 15:17:14 -07001832 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001833}
1834
1835
1836static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1837 struct p2p_go_neg_results *params)
1838{
1839 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1840
1841 wpa_s->p2p_group_common_freqs_num = 0;
1842 os_free(wpa_s->p2p_group_common_freqs);
1843 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1844 if (!wpa_s->p2p_group_common_freqs)
1845 return;
1846
1847 for (i = 0; i < len; i++) {
1848 if (!wpa_s->go_params->freq_list[i])
1849 break;
1850 wpa_s->p2p_group_common_freqs[i] =
1851 wpa_s->go_params->freq_list[i];
1852 }
1853 wpa_s->p2p_group_common_freqs_num = i;
1854}
1855
1856
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001857static void p2p_config_write(struct wpa_supplicant *wpa_s)
1858{
1859#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001860 if (wpa_s->p2pdev->conf->update_config &&
1861 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001862 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1863#endif /* CONFIG_NO_CONFIG_WRITE */
1864}
1865
1866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867static void p2p_go_configured(void *ctx, void *data)
1868{
1869 struct wpa_supplicant *wpa_s = ctx;
1870 struct p2p_go_neg_results *params = data;
1871 struct wpa_ssid *ssid;
1872
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001873 wpa_s->ap_configured_cb = NULL;
1874 wpa_s->ap_configured_cb_ctx = NULL;
1875 wpa_s->ap_configured_cb_data = NULL;
1876 if (!wpa_s->go_params) {
1877 wpa_printf(MSG_ERROR,
1878 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1879 return;
1880 }
1881
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001882 p2p_go_save_group_common_freqs(wpa_s, params);
1883 p2p_go_dump_common_freqs(wpa_s);
1884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 ssid = wpa_s->current_ssid;
1886 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1887 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1888 if (wpa_s->global->p2p_group_formation == wpa_s)
1889 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001890 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1891 params->passphrase[0] == '\0' ?
1892 params->psk : NULL,
1893 params->passphrase,
1894 wpa_s->global->p2p_dev_addr,
1895 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001896 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001897
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001898 if (wpa_s->p2pdev->p2ps_method_config_any) {
1899 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001900 wpa_dbg(wpa_s, MSG_DEBUG,
1901 "P2PS: Setting default PIN for ANY");
1902 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1903 "12345670", NULL, 0,
1904 0);
1905 } else {
1906 wpa_dbg(wpa_s, MSG_DEBUG,
1907 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001908 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001909 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001910 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001911 "12345670", NULL, 0, 0);
1912 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001913 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001914 }
1915
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001916 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001917 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001918 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001919 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001920 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001921 wpas_p2p_add_psk_list(wpa_s, ssid);
1922 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001923
1924 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001925 params->persistent_group, 0,
1926 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 wpas_p2p_cross_connect_setup(wpa_s);
1928 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001929
1930 if (wpa_s->p2p_first_connection_timeout) {
1931 wpa_dbg(wpa_s, MSG_DEBUG,
1932 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1933 wpa_s->p2p_first_connection_timeout);
1934 wpa_s->p2p_go_group_formation_completed = 0;
1935 wpa_s->global->p2p_group_formation = wpa_s;
1936 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001937 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001938 eloop_register_timeout(
1939 wpa_s->p2p_first_connection_timeout, 0,
1940 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001941 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001942 }
1943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 return;
1945 }
1946
1947 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1948 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1949 params->peer_interface_addr)) {
1950 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1951 "filtering");
1952 return;
1953 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001954 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001956 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001957#ifdef CONFIG_WPS_NFC
1958 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001959 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001960 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001961 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001962 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1963 return;
1964 }
1965 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001966 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1967 wpa_s->p2pdev->p2p_oob_dev_pw,
1968 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1969 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001970#endif /* CONFIG_WPS_NFC */
1971 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001973 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 os_free(wpa_s->go_params);
1975 wpa_s->go_params = NULL;
1976}
1977
1978
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001979/**
1980 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1981 * @freq: Frequency (MHz) to convert
1982 * @op_class: Buffer for returning operating class
1983 * @op_edmg_channel: Buffer for returning channel number
1984 * Returns: 0 on success, -1 on failure
1985 *
1986 * This can be used to find the highest channel bonding which includes the
1987 * specified frequency.
1988 */
1989static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1990 unsigned int freq,
1991 u8 *op_class, u8 *op_edmg_channel)
1992{
1993 struct hostapd_hw_modes *hwmode;
1994 struct ieee80211_edmg_config edmg;
1995 unsigned int i;
1996 enum chan_width chanwidth[] = {
1997 CHAN_WIDTH_8640,
1998 CHAN_WIDTH_6480,
1999 CHAN_WIDTH_4320,
2000 };
2001
2002 if (!wpa_s->hw.modes)
2003 return -1;
2004
2005 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002006 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002007 if (!hwmode) {
2008 wpa_printf(MSG_ERROR,
2009 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2010 return -1;
2011 }
2012
2013 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2014 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2015 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2016 op_class,
2017 op_edmg_channel) < 0)
2018 continue;
2019
2020 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2021 if (edmg.channels &&
2022 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2023 wpa_printf(MSG_DEBUG,
2024 "Freq %u to EDMG channel %u at opclass %u",
2025 freq, *op_edmg_channel, *op_class);
2026 return 0;
2027 }
2028 }
2029
2030 return -1;
2031}
2032
2033
2034int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2035 struct p2p_go_neg_results *params)
2036{
2037 u8 op_channel, op_class;
2038 int freq;
2039
2040 /* Try social channel as primary channel frequency */
2041 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2042
2043 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2044 &op_channel) == 0) {
2045 wpa_printf(MSG_DEBUG,
2046 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2047 freq, op_channel, op_class);
2048 params->freq = freq;
2049 return 0;
2050 }
2051
2052 return -1;
2053}
2054
2055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2057 struct p2p_go_neg_results *params,
2058 int group_formation)
2059{
2060 struct wpa_ssid *ssid;
2061
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002062 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2063 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2064 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2065 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068
2069 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002070 if (ssid == NULL) {
2071 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002075 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002076 wpa_s->p2p_go_group_formation_completed = 0;
2077 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002078 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 wpa_config_set_network_defaults(ssid);
2081 ssid->temporary = 1;
2082 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002083 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2085 WPAS_MODE_P2P_GO;
2086 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002087 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002088 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002089 ssid->max_oper_chwidth = params->max_oper_chwidth;
2090 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002091 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002092 if (params->edmg) {
2093 u8 op_channel, op_class;
2094
2095 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2096 &op_class, &op_channel)) {
2097 ssid->edmg_channel = op_channel;
2098 ssid->enable_edmg = params->edmg;
2099 } else {
2100 wpa_dbg(wpa_s, MSG_DEBUG,
2101 "P2P: Could not match EDMG channel, freq %d, for GO",
2102 params->freq);
2103 }
2104 }
2105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 ssid->ssid = os_zalloc(params->ssid_len + 1);
2107 if (ssid->ssid) {
2108 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2109 ssid->ssid_len = params->ssid_len;
2110 }
2111 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2112 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002113 if (is_6ghz_freq(ssid->frequency) &&
2114 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2115 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2116 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2117 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002118 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2120 } else {
2121 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2122 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123 ssid->proto = WPA_PROTO_RSN;
2124 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002125 ssid->group_cipher = WPA_CIPHER_CCMP;
2126 if (params->freq > 56160) {
2127 /*
2128 * Enable GCMP instead of CCMP as pairwise_cipher and
2129 * group_cipher in 60 GHz.
2130 */
2131 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2132 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002133 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2134 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002135 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002136 if (os_strlen(params->passphrase) > 0) {
2137 ssid->passphrase = os_strdup(params->passphrase);
2138 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002139 wpa_msg_global(wpa_s, MSG_ERROR,
2140 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002141 wpa_config_remove_network(wpa_s->conf, ssid->id);
2142 return;
2143 }
2144 } else
2145 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002146 ssid->psk_set = params->psk_set;
2147 if (ssid->psk_set)
2148 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002149 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002150 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002151 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152
2153 wpa_s->ap_configured_cb = p2p_go_configured;
2154 wpa_s->ap_configured_cb_ctx = wpa_s;
2155 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002156 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002157 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 wpa_s->reassociate = 1;
2159 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2161 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 wpa_supplicant_req_scan(wpa_s, 0, 0);
2163}
2164
2165
2166static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2167 const struct wpa_supplicant *src)
2168{
2169 struct wpa_config *d;
2170 const struct wpa_config *s;
2171
2172 d = dst->conf;
2173 s = src->conf;
2174
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002175#define C(n) \
2176do { \
2177 if (s->n && !d->n) \
2178 d->n = os_strdup(s->n); \
2179} while (0)
2180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 C(device_name);
2182 C(manufacturer);
2183 C(model_name);
2184 C(model_number);
2185 C(serial_number);
2186 C(config_methods);
2187#undef C
2188
2189 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2190 os_memcpy(d->sec_device_type, s->sec_device_type,
2191 sizeof(d->sec_device_type));
2192 d->num_sec_device_types = s->num_sec_device_types;
2193
2194 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002195 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002197 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002198 d->max_num_sta = s->max_num_sta;
2199 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002200 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002201 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002202 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002203 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002204 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002205 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002206 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002207 d->pmf = s->pmf;
2208 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002209 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002210
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002211 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2212 !d->wps_nfc_pw_from_config) {
2213 wpabuf_free(d->wps_nfc_dh_privkey);
2214 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002215 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2216 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2217 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002218 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002219 d->go_interworking = s->go_interworking;
2220 d->go_access_network_type = s->go_access_network_type;
2221 d->go_internet = s->go_internet;
2222 d->go_venue_group = s->go_venue_group;
2223 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002224 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225}
2226
2227
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002228static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2229 char *ifname, size_t len)
2230{
2231 char *ifname_ptr = wpa_s->ifname;
2232
2233 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2234 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2235 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2236 }
2237
2238 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2239 if (os_strlen(ifname) >= IFNAMSIZ &&
2240 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002241 int res;
2242
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002243 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002244 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2245 if (os_snprintf_error(len, res) && len)
2246 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002247 }
2248}
2249
2250
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2252 enum wpa_driver_if_type type)
2253{
2254 char ifname[120], force_ifname[120];
2255
2256 if (wpa_s->pending_interface_name[0]) {
2257 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2258 "- skip creation of a new one");
2259 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2260 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2261 "unknown?! ifname='%s'",
2262 wpa_s->pending_interface_name);
2263 return -1;
2264 }
2265 return 0;
2266 }
2267
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002268 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 force_ifname[0] = '\0';
2270
2271 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2272 ifname);
2273 wpa_s->p2p_group_idx++;
2274
2275 wpa_s->pending_interface_type = type;
2276 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2277 wpa_s->pending_interface_addr, NULL) < 0) {
2278 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2279 "interface");
2280 return -1;
2281 }
2282
Jimmy Chen36c21992018-11-29 16:46:43 +08002283 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002284 random_mac_addr(wpa_s->pending_interface_addr);
2285 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2286 " for the group",
2287 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002288 }
2289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 if (force_ifname[0]) {
2291 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2292 force_ifname);
2293 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2294 sizeof(wpa_s->pending_interface_name));
2295 } else
2296 os_strlcpy(wpa_s->pending_interface_name, ifname,
2297 sizeof(wpa_s->pending_interface_name));
2298 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2299 MACSTR, wpa_s->pending_interface_name,
2300 MAC2STR(wpa_s->pending_interface_addr));
2301
2302 return 0;
2303}
2304
2305
2306static void wpas_p2p_remove_pending_group_interface(
2307 struct wpa_supplicant *wpa_s)
2308{
2309 if (!wpa_s->pending_interface_name[0] ||
2310 is_zero_ether_addr(wpa_s->pending_interface_addr))
2311 return; /* No pending virtual interface */
2312
2313 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2314 wpa_s->pending_interface_name);
2315 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2316 wpa_s->pending_interface_name);
2317 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2318 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002319 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320}
2321
2322
2323static struct wpa_supplicant *
2324wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2325{
2326 struct wpa_interface iface;
2327 struct wpa_supplicant *group_wpa_s;
2328
2329 if (!wpa_s->pending_interface_name[0]) {
2330 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2331 if (!wpas_p2p_create_iface(wpa_s))
2332 return NULL;
2333 /*
2334 * Something has forced us to remove the pending interface; try
2335 * to create a new one and hope for the best that we will get
2336 * the same local address.
2337 */
2338 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2339 WPA_IF_P2P_CLIENT) < 0)
2340 return NULL;
2341 }
2342
2343 os_memset(&iface, 0, sizeof(iface));
2344 iface.ifname = wpa_s->pending_interface_name;
2345 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002346 if (wpa_s->conf->ctrl_interface == NULL &&
2347 wpa_s->parent != wpa_s &&
2348 wpa_s->p2p_mgmt &&
2349 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2350 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2351 else
2352 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002354 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355 if (group_wpa_s == NULL) {
2356 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2357 "wpa_supplicant interface");
2358 return NULL;
2359 }
2360 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2362 P2P_GROUP_INTERFACE_CLIENT;
2363 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002364 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365
2366 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2367
Jimmy Chen36c21992018-11-29 16:46:43 +08002368 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002369 if (wpa_drv_set_mac_addr(group_wpa_s,
2370 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002371 wpa_msg(group_wpa_s, MSG_INFO,
2372 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002373 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2374 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002375 return NULL;
2376 }
2377
2378 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2379 wpa_msg(group_wpa_s, MSG_INFO,
2380 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002381 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2382 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002383 return NULL;
2384 }
2385
Hai Shalom74f70d42019-02-11 14:42:39 -08002386 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2387 " for the group",
2388 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002389 }
2390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 return group_wpa_s;
2392}
2393
2394
2395static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2396 void *timeout_ctx)
2397{
2398 struct wpa_supplicant *wpa_s = eloop_ctx;
2399 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002400 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002401}
2402
2403
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002404static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2405 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002406{
2407 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002408 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409 if (wpa_s->global->p2p)
2410 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002411 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412}
2413
2414
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002415static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2416{
2417 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2418 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002419 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002420 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002421 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002422 wpa_s->global->p2p_fail_on_wps_complete = 0;
2423}
2424
2425
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002426void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2427{
2428 if (wpa_s->global->p2p_group_formation != wpa_s)
2429 return;
2430 /* Speed up group formation timeout since this cannot succeed */
2431 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002432 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002433 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002434 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002435}
2436
2437
Matthew Wang06b42472022-11-10 06:56:31 +00002438bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2439{
2440 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002441 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002442 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002443
Matthew Wang06b42472022-11-10 06:56:31 +00002444 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2445 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2446 wpa_s->p2pdev, NULL);
2447 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2448 wpa_s->p2pdev, NULL);
2449 return true;
2450}
2451
2452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002453static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454{
2455 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002456 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457
2458 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2459 wpa_drv_cancel_remain_on_channel(wpa_s);
2460 wpa_s->off_channel_freq = 0;
2461 wpa_s->roc_waiting_drv_freq = 0;
2462 }
2463
2464 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002465 wpa_msg_global(wpa_s, MSG_INFO,
2466 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2467 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002468 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 wpas_p2p_remove_pending_group_interface(wpa_s);
2470 return;
2471 }
2472
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002473 if (!res->role_go) {
2474 /* Inform driver of the operating channel of GO. */
2475 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2476 }
2477
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002478 if (wpa_s->p2p_go_ht40)
2479 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002480 if (wpa_s->p2p_go_vht)
2481 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002482 if (wpa_s->p2p_go_he)
2483 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002484 if (wpa_s->p2p_go_edmg)
2485 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002486 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2487 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002488
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002489 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2490 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2491 " wps_method=%s",
2492 res->role_go ? "GO" : "client", res->freq, res->ht40,
2493 MAC2STR(res->peer_device_addr),
2494 MAC2STR(res->peer_interface_addr),
2495 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002496 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497
Dmitry Shmidt04949592012-07-19 12:16:46 -07002498 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2499 struct wpa_ssid *ssid;
2500 ssid = wpa_config_get_network(wpa_s->conf,
2501 wpa_s->p2p_persistent_id);
2502 if (ssid && ssid->disabled == 2 &&
2503 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2504 size_t len = os_strlen(ssid->passphrase);
2505 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2506 "on requested persistent group");
2507 os_memcpy(res->passphrase, ssid->passphrase, len);
2508 res->passphrase[len] = '\0';
2509 }
2510 }
2511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002513 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2515 if (group_wpa_s == NULL) {
2516 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002517 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2518 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002519 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 return;
2521 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2523 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002525 group_wpa_s = wpa_s->parent;
2526 wpa_s->global->p2p_group_formation = group_wpa_s;
2527 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002528 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002529 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002531 group_wpa_s->p2p_in_provisioning = 1;
2532 group_wpa_s->p2pdev = wpa_s;
2533 if (group_wpa_s != wpa_s) {
2534 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2535 sizeof(group_wpa_s->p2p_pin));
2536 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2537 }
2538 if (res->role_go) {
2539 wpas_start_wps_go(group_wpa_s, res, 1);
2540 } else {
2541 os_get_reltime(&group_wpa_s->scan_min_time);
2542 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 }
2544
Hai Shalomfdcde762020-04-02 11:19:20 -07002545 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2547
2548 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2549 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2550 (res->peer_config_timeout % 100) * 10000,
2551 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2552}
2553
2554
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002555static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2556 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557{
2558 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002559 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002560 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2561 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002563 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564}
2565
2566
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002567static void wpas_dev_found(void *ctx, const u8 *addr,
2568 const struct p2p_peer_info *info,
2569 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002571 u8 *wfd_dev_info = NULL;
2572 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002573 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002574 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002575#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 struct wpa_supplicant *wpa_s = ctx;
2577 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002578 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002579 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002580
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002581#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002582 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2583 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002584 if (wfd_dev_info_hex) {
2585 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2586 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2587 // Only used for notification, so not handling error.
2588 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2589 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002590
2591 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2592 WFD_SUBELEM_R2_DEVICE_INFO);
2593 if (wfd_r2_dev_info_hex) {
2594 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2595 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2596 // Only used for notification, so not handling error.
2597 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2598 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002599#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002600
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002601 if (info->p2ps_instance) {
2602 char str[256];
2603 const u8 *buf = wpabuf_head(info->p2ps_instance);
2604 size_t len = wpabuf_len(info->p2ps_instance);
2605
2606 while (len) {
2607 u32 id;
2608 u16 methods;
2609 u8 str_len;
2610
2611 if (len < 4 + 2 + 1)
2612 break;
2613 id = WPA_GET_LE32(buf);
2614 buf += sizeof(u32);
2615 methods = WPA_GET_BE16(buf);
2616 buf += sizeof(u16);
2617 str_len = *buf++;
2618 if (str_len > len - 4 - 2 - 1)
2619 break;
2620 os_memcpy(str, buf, str_len);
2621 str[str_len] = '\0';
2622 buf += str_len;
2623 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2624
2625 wpa_msg_global(wpa_s, MSG_INFO,
2626 P2P_EVENT_DEVICE_FOUND MACSTR
2627 " p2p_dev_addr=" MACSTR
2628 " pri_dev_type=%s name='%s'"
2629 " config_methods=0x%x"
2630 " dev_capab=0x%x"
2631 " group_capab=0x%x"
2632 " adv_id=%x asp_svc=%s%s",
2633 MAC2STR(addr),
2634 MAC2STR(info->p2p_device_addr),
2635 wps_dev_type_bin2str(
2636 info->pri_dev_type,
2637 devtype, sizeof(devtype)),
2638 info->device_name, methods,
2639 info->dev_capab, info->group_capab,
2640 id, str,
2641 info->vendor_elems ?
2642 " vendor_elems=1" : "");
2643 }
2644 goto done;
2645 }
2646
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002647 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2648 " p2p_dev_addr=" MACSTR
2649 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002650 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002651 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2652 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2653 sizeof(devtype)),
2654 info->device_name, info->config_methods,
2655 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002656 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002657 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002658 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2659 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002660 info->vendor_elems ? " vendor_elems=1" : "",
2661 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002663done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002664 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002665 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002666#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002667
Roshan Piusfd2fd662017-01-23 13:41:57 -08002668 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002669 wfd_dev_info_len, wfd_r2_dev_info,
2670 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002671 os_free(wfd_dev_info);
jiangpingpinga96a5e82023-11-16 15:02:17 +08002672 os_free(wfd_r2_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673}
2674
2675
2676static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2677{
2678 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002679
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002680 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2681 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2684}
2685
2686
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002687static void wpas_find_stopped(void *ctx)
2688{
2689 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002690
2691 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2692 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2693
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002694 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002695 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002696}
2697
2698
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002699struct wpas_p2p_listen_work {
2700 unsigned int freq;
2701 unsigned int duration;
2702 struct wpabuf *probe_resp_ie;
2703};
2704
2705
2706static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2707{
2708 if (lwork == NULL)
2709 return;
2710 wpabuf_free(lwork->probe_resp_ie);
2711 os_free(lwork);
2712}
2713
2714
2715static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2716{
2717 struct wpas_p2p_listen_work *lwork;
2718
2719 if (!wpa_s->p2p_listen_work)
2720 return;
2721
2722 lwork = wpa_s->p2p_listen_work->ctx;
2723 wpas_p2p_listen_work_free(lwork);
2724 radio_work_done(wpa_s->p2p_listen_work);
2725 wpa_s->p2p_listen_work = NULL;
2726}
2727
2728
2729static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2730{
2731 struct wpa_supplicant *wpa_s = work->wpa_s;
2732 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002733 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002734
2735 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002736 if (work->started) {
2737 wpa_s->p2p_listen_work = NULL;
2738 wpas_stop_listen(wpa_s);
2739 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002740 wpas_p2p_listen_work_free(lwork);
2741 return;
2742 }
2743
2744 wpa_s->p2p_listen_work = work;
2745
2746 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2747
2748 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2749 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2750 "report received Probe Request frames");
2751 wpas_p2p_listen_work_done(wpa_s);
2752 return;
2753 }
2754
2755 wpa_s->pending_listen_freq = lwork->freq;
2756 wpa_s->pending_listen_duration = lwork->duration;
2757
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002758 duration = lwork->duration;
2759#ifdef CONFIG_TESTING_OPTIONS
2760 if (wpa_s->extra_roc_dur) {
2761 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2762 duration, duration + wpa_s->extra_roc_dur);
2763 duration += wpa_s->extra_roc_dur;
2764 }
2765#endif /* CONFIG_TESTING_OPTIONS */
2766
2767 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002768 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2769 "to remain on channel (%u MHz) for Listen "
2770 "state", lwork->freq);
2771 wpas_p2p_listen_work_done(wpa_s);
2772 wpa_s->pending_listen_freq = 0;
2773 return;
2774 }
2775 wpa_s->off_channel_freq = 0;
2776 wpa_s->roc_waiting_drv_freq = lwork->freq;
2777}
2778
2779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780static int wpas_start_listen(void *ctx, unsigned int freq,
2781 unsigned int duration,
2782 const struct wpabuf *probe_resp_ie)
2783{
2784 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002785 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002787 if (wpa_s->p2p_listen_work) {
2788 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 return -1;
2790 }
2791
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002792 lwork = os_zalloc(sizeof(*lwork));
2793 if (lwork == NULL)
2794 return -1;
2795 lwork->freq = freq;
2796 lwork->duration = duration;
2797 if (probe_resp_ie) {
2798 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2799 if (lwork->probe_resp_ie == NULL) {
2800 wpas_p2p_listen_work_free(lwork);
2801 return -1;
2802 }
2803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002805 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2806 lwork) < 0) {
2807 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 return -1;
2809 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 return 0;
2812}
2813
2814
2815static void wpas_stop_listen(void *ctx)
2816{
2817 struct wpa_supplicant *wpa_s = ctx;
2818 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2819 wpa_drv_cancel_remain_on_channel(wpa_s);
2820 wpa_s->off_channel_freq = 0;
2821 wpa_s->roc_waiting_drv_freq = 0;
2822 }
2823 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002824
2825 /*
2826 * Don't cancel Probe Request RX reporting for a connected P2P Client
2827 * handling Probe Request frames.
2828 */
2829 if (!wpa_s->p2p_cli_probe)
2830 wpa_drv_probe_req_report(wpa_s, 0);
2831
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002832 wpas_p2p_listen_work_done(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002833
2834 if (radio_work_pending(wpa_s, "p2p-listen")) {
2835 wpa_printf(MSG_DEBUG,
2836 "P2P: p2p-listen is still pending - remove it");
2837 radio_remove_works(wpa_s, "p2p-listen", 0);
2838 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839}
2840
2841
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002842static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2843 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844{
2845 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002846 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002847 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848}
2849
2850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2852 const u8 *peer, const char *params,
2853 unsigned int generated_pin)
2854{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002855 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2856 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857}
2858
2859
2860static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2861 const u8 *peer, const char *params)
2862{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002863 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2864 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865}
2866
2867
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002868static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2869 const u8 *dev_addr, const u8 *pri_dev_type,
2870 const char *dev_name, u16 supp_config_methods,
2871 u8 dev_capab, u8 group_capab, const u8 *group_id,
2872 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873{
2874 struct wpa_supplicant *wpa_s = ctx;
2875 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002876 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 u8 empty_dev_type[8];
2878 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002880 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002881
2882 if (group_id) {
2883 for (group = wpa_s->global->ifaces; group; group = group->next)
2884 {
2885 struct wpa_ssid *s = group->current_ssid;
2886 if (s != NULL &&
2887 s->mode == WPAS_MODE_P2P_GO &&
2888 group_id_len - ETH_ALEN == s->ssid_len &&
2889 os_memcmp(group_id + ETH_ALEN, s->ssid,
2890 s->ssid_len) == 0)
2891 break;
2892 }
2893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894
2895 if (pri_dev_type == NULL) {
2896 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2897 pri_dev_type = empty_dev_type;
2898 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002899 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2900 " pri_dev_type=%s name='%s' config_methods=0x%x "
2901 "dev_capab=0x%x group_capab=0x%x%s%s",
2902 MAC2STR(dev_addr),
2903 wps_dev_type_bin2str(pri_dev_type, devtype,
2904 sizeof(devtype)),
2905 dev_name, supp_config_methods, dev_capab, group_capab,
2906 group ? " group=" : "",
2907 group ? group->ifname : "");
2908 if (os_snprintf_error(sizeof(params), res))
2909 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002913 if (wps_generate_pin(&generated_pin) < 0) {
2914 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2915 wpas_notify_p2p_provision_discovery(
2916 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002917 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002918 return;
2919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 wpas_prov_disc_local_display(wpa_s, peer, params,
2921 generated_pin);
2922 } else if (config_methods & WPS_CONFIG_KEYPAD)
2923 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2924 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002925 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2926 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002927
2928 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2929 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002930 config_methods, generated_pin,
2931 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932}
2933
2934
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002935static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936{
2937 struct wpa_supplicant *wpa_s = ctx;
2938 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002939 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002941 if (wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002942 (ether_addr_equal(peer, wpa_s->pending_join_dev_addr) ||
2943 ether_addr_equal(peer, wpa_s->pending_join_iface_addr))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002944 wpa_s->pending_pd_before_join = 0;
2945 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2946 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002947 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002948 return;
2949 }
2950
Dmitry Shmidt04949592012-07-19 12:16:46 -07002951 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002952 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2953 int res;
2954
2955 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2956 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2957 if (os_snprintf_error(sizeof(params), res))
2958 params[sizeof(params) - 1] = '\0';
2959 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002960 params[0] = '\0';
2961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002963 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002965 if (wps_generate_pin(&generated_pin) < 0) {
2966 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2967 wpas_notify_p2p_provision_discovery(
2968 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002969 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002970 return;
2971 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002972 wpas_prov_disc_local_display(wpa_s, peer, params,
2973 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002975 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2976 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977
Jouni Malinen75ecf522011-06-27 15:19:46 -07002978 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2979 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002980 config_methods, generated_pin,
2981 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982}
2983
2984
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002986 enum p2p_prov_disc_status status,
2987 u32 adv_id, const u8 *adv_mac,
2988 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002989{
2990 struct wpa_supplicant *wpa_s = ctx;
2991
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992 if (wpa_s->p2p_fallback_to_go_neg) {
2993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2994 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002995 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002996 P2P_EVENT_FALLBACK_TO_GO_NEG
2997 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002998 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2999 return;
3000 }
3001
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003002 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003003 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003004 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3005 "join-existing-group operation (no ACK for PD "
3006 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003007 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003008 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003009 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003010
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003011 if (adv_id && adv_mac && deferred_session_resp) {
3012 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3013 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3014 " deferred_session_resp='%s'",
3015 MAC2STR(peer), status, adv_id,
3016 deferred_session_resp);
3017 } else if (adv_id && adv_mac) {
3018 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3019 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3020 MAC2STR(peer), status, adv_id);
3021 } else {
3022 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3023 " p2p_dev_addr=" MACSTR " status=%d",
3024 MAC2STR(peer), status);
3025 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003026
Jouni Malinen75ecf522011-06-27 15:19:46 -07003027 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003028 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003029}
3030
3031
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003032static int freq_included(struct wpa_supplicant *wpa_s,
3033 const struct p2p_channels *channels,
3034 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003035{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003036 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3037 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3038 return 1;
3039 return 0;
3040}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003041
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003042
3043static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3044{
3045 unsigned int num = P2P_MAX_CHANNELS;
3046 int *common_freqs;
3047 int ret;
3048
3049 p2p_go_dump_common_freqs(wpa_s);
3050 common_freqs = os_calloc(num, sizeof(int));
3051 if (!common_freqs)
3052 return;
3053
3054 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3055 if (ret < 0) {
3056 wpa_dbg(wpa_s, MSG_DEBUG,
3057 "P2P: Failed to get group common freqs");
3058 os_free(common_freqs);
3059 return;
3060 }
3061
3062 os_free(wpa_s->p2p_group_common_freqs);
3063 wpa_s->p2p_group_common_freqs = common_freqs;
3064 wpa_s->p2p_group_common_freqs_num = num;
3065 p2p_go_dump_common_freqs(wpa_s);
3066}
3067
3068
3069/*
3070 * Check if the given frequency is one of the possible operating frequencies
3071 * set after the completion of the GO Negotiation.
3072 */
3073static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3074{
3075 unsigned int i;
3076
3077 p2p_go_dump_common_freqs(wpa_s);
3078
3079 /* assume no restrictions */
3080 if (!wpa_s->p2p_group_common_freqs_num)
3081 return 1;
3082
3083 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3084 if (wpa_s->p2p_group_common_freqs[i] == freq)
3085 return 1;
3086 }
3087 return 0;
3088}
3089
3090
3091static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3092 struct sta_info *sta, void *ctx)
3093{
3094 int *ecsa_support = ctx;
3095
3096 *ecsa_support &= sta->ecsa_supported;
3097
3098 return 0;
3099}
3100
3101
3102/* Check if all the peers support eCSA */
3103static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3104{
3105 int ecsa_support = 1;
3106
3107 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3108 &ecsa_support);
3109
3110 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003111}
3112
3113
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003114/**
3115 * Pick the best frequency to use from all the currently used frequencies.
3116 */
3117static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3118 struct wpa_used_freq_data *freqs,
3119 unsigned int num)
3120{
3121 unsigned int i, c;
3122
3123 /* find a candidate freq that is supported by P2P */
3124 for (c = 0; c < num; c++)
3125 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3126 break;
3127
3128 if (c == num)
3129 return 0;
3130
3131 /* once we have a candidate, try to find a 'better' one */
3132 for (i = c + 1; i < num; i++) {
3133 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3134 continue;
3135
3136 /*
3137 * 1. Infrastructure station interfaces have higher preference.
3138 * 2. P2P Clients have higher preference.
3139 * 3. All others.
3140 */
3141 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3142 c = i;
3143 break;
3144 }
3145
3146 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3147 c = i;
3148 }
3149 return freqs[c].freq;
3150}
3151
3152
Sunil Ravi036cec52023-03-29 11:35:17 -07003153/**
3154 * Pick the best frequency the driver suggests.
3155 *
3156 * num_pref_freq is used as both input and output
3157 * - input: the max size of pref_freq_list,
3158 * - output: the valid size of pref_freq_list filled with data.
3159 */
3160static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3161 struct weighted_pcl *pref_freq_list,
3162 unsigned int *num_pref_freq)
3163{
3164 int best_freq = 0;
3165 unsigned int max_pref_freq, i;
3166 int res;
3167 enum wpa_driver_if_type iface_type;
3168
3169 max_pref_freq = *num_pref_freq;
3170 *num_pref_freq = 0;
3171
3172 if (go)
3173 iface_type = WPA_IF_P2P_GO;
3174 else
3175 iface_type = WPA_IF_P2P_CLIENT;
3176
3177 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3178 pref_freq_list);
3179 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3180 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3181 max_pref_freq);
3182 if (res || !max_pref_freq) {
3183 wpa_printf(MSG_DEBUG,
3184 "P2P: No preferred frequency list available");
3185 return 0;
3186 }
3187
3188 *num_pref_freq = max_pref_freq;
3189 i = 0;
3190 while (i < *num_pref_freq &&
3191 (!p2p_supported_freq(wpa_s->global->p2p,
3192 pref_freq_list[i].freq) ||
3193 wpas_p2p_disallowed_freq(wpa_s->global,
3194 pref_freq_list[i].freq) ||
3195 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3196 wpa_printf(MSG_DEBUG,
3197 "P2P: preferred_freq_list[%d]=%d is disallowed",
3198 i, pref_freq_list[i].freq);
3199 i++;
3200 }
3201 if (i != *num_pref_freq) {
3202 best_freq = pref_freq_list[i].freq;
3203 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3204 i, best_freq);
3205 } else {
3206 wpa_printf(MSG_DEBUG,
3207 "P2P: All driver preferred frequencies are disallowed for P2P use");
3208 *num_pref_freq = 0;
3209 }
3210
3211 return best_freq;
3212}
3213
3214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3216 const u8 *go_dev_addr, const u8 *ssid,
3217 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003218 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003219 const struct p2p_channels *channels,
3220 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221{
3222 struct wpa_supplicant *wpa_s = ctx;
3223 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003224 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003226 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003227 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3228 unsigned int num_pref_freq;
3229 int res;
3230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231
3232 if (!persistent_group) {
3233 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003234 " to join an active group (SSID: %s)",
3235 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003237 (ether_addr_equal(go_dev_addr, wpa_s->p2p_auth_invite) ||
3238 ether_addr_equal(sa, wpa_s->p2p_auth_invite))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3240 "authorized invitation");
3241 goto accept_inv;
3242 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003243
3244#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003245 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3246 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003247 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003248 wpa_s->p2p_wps_method = WPS_NFC;
3249 wpa_s->pending_join_wps_method = WPS_NFC;
3250 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003251 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003252 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003253 bssid, ETH_ALEN);
3254 goto accept_inv;
3255 }
3256#endif /* CONFIG_WPS_NFC */
3257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 /*
3259 * Do not accept the invitation automatically; notify user and
3260 * request approval.
3261 */
3262 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3263 }
3264
3265 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3266 if (grp) {
3267 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3268 "running persistent group");
3269 if (*go)
3270 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3271 goto accept_inv;
3272 }
3273
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003274 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003275 ether_addr_equal(sa, wpa_s->p2p_auth_invite)) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003276 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3277 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003278 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003279 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3281
3282 for (s = wpa_s->conf->ssid; s; s = s->next) {
3283 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003284 ether_addr_equal(s->bssid, go_dev_addr) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 s->ssid_len == ssid_len &&
3286 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3287 break;
3288 }
3289
3290 if (!s) {
3291 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3292 " requested reinvocation of an unknown group",
3293 MAC2STR(sa));
3294 return P2P_SC_FAIL_UNKNOWN_GROUP;
3295 }
3296
3297 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3298 *go = 1;
3299 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3300 wpa_printf(MSG_DEBUG, "P2P: The only available "
3301 "interface is already in use - reject "
3302 "invitation");
3303 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3304 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003305 if (wpa_s->p2p_mgmt)
3306 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3307 ETH_ALEN);
3308 else
3309 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 } else if (s->mode == WPAS_MODE_P2P_GO) {
3311 *go = 1;
3312 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3313 {
3314 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3315 "interface address for the group");
3316 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3317 }
3318 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3319 ETH_ALEN);
3320 }
3321
3322accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003323 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3324
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003325 best_freq = 0;
3326 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3327 sizeof(struct wpa_used_freq_data));
3328 if (freqs) {
3329 int num_channels = wpa_s->num_multichan_concurrent;
3330 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3331 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3332 os_free(freqs);
3333 }
3334
Sunil Ravi036cec52023-03-29 11:35:17 -07003335 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3336 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3337 &num_pref_freq);
3338 if (res > 0)
3339 best_freq = res;
3340
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003341 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003342 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003343 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003344 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003345
3346 if (wpa_s->num_multichan_concurrent < 2 ||
3347 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3348 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 -07003349 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003350 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351 }
3352
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003353 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3354 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003355 if (*go == 0) {
3356 /* We are the client */
3357 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3358 "running a GO but we are capable of MCC, "
3359 "figure out the best channel to use");
3360 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003361 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003362 /* We are the GO, and *force_freq is not in the
3363 * intersection */
3364 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3365 "in intersection but we are capable of MCC, "
3366 "figure out the best channel to use",
3367 *force_freq);
3368 *force_freq = 0;
3369 }
3370 }
3371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 return P2P_SC_SUCCESS;
3373}
3374
3375
3376static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3377 const u8 *ssid, size_t ssid_len,
3378 const u8 *go_dev_addr, u8 status,
3379 int op_freq)
3380{
3381 struct wpa_supplicant *wpa_s = ctx;
3382 struct wpa_ssid *s;
3383
3384 for (s = wpa_s->conf->ssid; s; s = s->next) {
3385 if (s->disabled == 2 &&
3386 s->ssid_len == ssid_len &&
3387 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3388 break;
3389 }
3390
3391 if (status == P2P_SC_SUCCESS) {
3392 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003393 " was accepted; op_freq=%d MHz, SSID=%s",
3394 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003396 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003397 if (go) {
3398 wpa_msg_global(wpa_s, MSG_INFO,
3399 P2P_EVENT_INVITATION_ACCEPTED
3400 "sa=" MACSTR
3401 " persistent=%d freq=%d",
3402 MAC2STR(sa), s->id, op_freq);
3403 } else {
3404 wpa_msg_global(wpa_s, MSG_INFO,
3405 P2P_EVENT_INVITATION_ACCEPTED
3406 "sa=" MACSTR
3407 " persistent=%d",
3408 MAC2STR(sa), s->id);
3409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003410 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003411 wpa_s, s, go, 0, op_freq, 0,
3412 wpa_s->conf->p2p_go_ht40,
3413 wpa_s->conf->p2p_go_vht,
3414 0,
3415 wpa_s->conf->p2p_go_he,
3416 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003417 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003418 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003419 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003421 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003422 wpa_msg_global(wpa_s, MSG_INFO,
3423 P2P_EVENT_INVITATION_ACCEPTED
3424 "sa=" MACSTR " go_dev_addr=" MACSTR
3425 " bssid=" MACSTR " unknown-network",
3426 MAC2STR(sa), MAC2STR(go_dev_addr),
3427 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003429 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003430 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 }
3432 return;
3433 }
3434
3435 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3436 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3437 " was rejected (status %u)", MAC2STR(sa), status);
3438 return;
3439 }
3440
3441 if (!s) {
3442 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003443 wpa_msg_global(wpa_s, MSG_INFO,
3444 P2P_EVENT_INVITATION_RECEIVED
3445 "sa=" MACSTR " go_dev_addr=" MACSTR
3446 " bssid=" MACSTR " unknown-network",
3447 MAC2STR(sa), MAC2STR(go_dev_addr),
3448 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003450 wpa_msg_global(wpa_s, MSG_INFO,
3451 P2P_EVENT_INVITATION_RECEIVED
3452 "sa=" MACSTR " go_dev_addr=" MACSTR
3453 " unknown-network",
3454 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003456 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3457 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458 return;
3459 }
3460
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003461 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003462 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3463 "sa=" MACSTR " persistent=%d freq=%d",
3464 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003465 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003466 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3467 "sa=" MACSTR " persistent=%d",
3468 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003469 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003470 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3471 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472}
3473
3474
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003475static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3476 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003477 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003478{
3479 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003480 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003481
3482 if (ssid == NULL)
3483 return;
3484
3485 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003486 if (ether_addr_equal(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3487 peer))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003488 break;
3489 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003490 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003491 if (ssid->mode != WPAS_MODE_P2P_GO &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003492 ether_addr_equal(ssid->bssid, peer)) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003493 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3494 "due to invitation result", ssid->id);
3495 wpas_notify_network_removed(wpa_s, ssid);
3496 wpa_config_remove_network(wpa_s->conf, ssid->id);
3497 return;
3498 }
3499 return; /* Peer not found in client list */
3500 }
3501
3502 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003503 "group %d client list%s",
3504 MAC2STR(peer), ssid->id,
3505 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003506 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3507 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3508 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003509 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003510 if (p2p_wpa_s->conf->update_config &&
3511 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003512 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003513}
3514
3515
3516static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3517 const u8 *peer)
3518{
3519 struct wpa_ssid *ssid;
3520
3521 wpa_s = wpa_s->global->p2p_invite_group;
3522 if (wpa_s == NULL)
3523 return; /* No known invitation group */
3524 ssid = wpa_s->current_ssid;
3525 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3526 !ssid->p2p_persistent_group)
3527 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003528 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003529 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003530 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003531}
3532
3533
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003534static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003535 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003536 const u8 *peer, int neg_freq,
3537 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538{
3539 struct wpa_supplicant *wpa_s = ctx;
3540 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003541 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542
3543 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003544 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3545 "status=%d " MACSTR,
3546 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003548 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3549 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550 }
3551 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3552
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003553 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3554 status, MAC2STR(peer));
3555 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003556 struct wpa_supplicant *group_if =
3557 wpa_s->global->p2p_invite_group;
3558
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003559 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3560 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003561
3562 /*
3563 * Invitation to an active group. If this is successful and we
3564 * are the GO, set the client wait to postpone some concurrent
3565 * operations and to allow provisioning and connection to happen
3566 * more quickly.
3567 */
3568 if (status == P2P_SC_SUCCESS &&
3569 group_if && group_if->current_ssid &&
3570 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3571 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3572#ifdef CONFIG_TESTING_OPTIONS
3573 if (group_if->p2p_go_csa_on_inv) {
3574 wpa_printf(MSG_DEBUG,
3575 "Testing: force P2P GO CSA after invitation");
3576 eloop_cancel_timeout(
3577 wpas_p2p_reconsider_moving_go,
3578 wpa_s, NULL);
3579 eloop_register_timeout(
3580 0, 50000,
3581 wpas_p2p_reconsider_moving_go,
3582 wpa_s, NULL);
3583 }
3584#endif /* CONFIG_TESTING_OPTIONS */
3585 }
3586 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003587 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003588
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003589 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3590 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3591 "invitation exchange to indicate readiness for "
3592 "re-invocation");
3593 }
3594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003596 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3597 ssid = wpa_config_get_network(
3598 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003599 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 wpas_p2p_remove_pending_group_interface(wpa_s);
3602 return;
3603 }
3604
3605 ssid = wpa_config_get_network(wpa_s->conf,
3606 wpa_s->pending_invite_ssid_id);
3607 if (ssid == NULL) {
3608 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3609 "data matching with invitation");
3610 return;
3611 }
3612
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003613 /*
3614 * The peer could have missed our ctrl::ack frame for Invitation
3615 * Response and continue retransmitting the frame. To reduce the
3616 * likelihood of the peer not getting successful TX status for the
3617 * Invitation Response frame, wait a short time here before starting
3618 * the persistent group so that we will remain on the current channel to
3619 * acknowledge any possible retransmission from the peer.
3620 */
3621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3622 "starting persistent group");
3623 os_sleep(0, 50000);
3624
Dmitry Shmidt15907092014-03-25 10:42:57 -07003625 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003626 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003627 freq = neg_freq;
3628 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003629 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003630 freq = peer_oper_freq;
3631 else
3632 freq = 0;
3633
3634 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3635 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003637 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003638 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003639 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003640 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003641 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003642 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003643 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003644 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003645 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003646 ssid->mode == WPAS_MODE_P2P_GO ?
3647 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003648 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003649 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003650 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651}
3652
3653
Dmitry Shmidt04949592012-07-19 12:16:46 -07003654static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3655 unsigned int freq)
3656{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003657 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3658 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003659 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003660}
3661
3662
3663static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3664{
3665 reg->channel[reg->channels] = chan;
3666 reg->channels++;
3667}
3668
3669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003671 struct p2p_channels *chan,
3672 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673{
3674 int i, cla = 0;
3675
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003676 wpa_s->global->p2p_24ghz_social_channels = 1;
3677
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003678 os_memset(cli_chan, 0, sizeof(*cli_chan));
3679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3681 "band");
3682
3683 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3684 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003685 chan->reg_class[cla].channels = 0;
3686 for (i = 0; i < 11; i++) {
3687 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3688 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3689 }
3690 if (chan->reg_class[cla].channels)
3691 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692
3693 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3694 "band");
3695
3696 /* Operating class 115 - 5 GHz, channels 36-48 */
3697 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 chan->reg_class[cla].channels = 0;
3699 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3700 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3701 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3702 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3703 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3704 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3705 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3706 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3707 if (chan->reg_class[cla].channels)
3708 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709
3710 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3711 "band");
3712
3713 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3714 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003715 chan->reg_class[cla].channels = 0;
3716 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3717 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3718 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3719 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3720 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3721 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3722 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3723 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3724 if (chan->reg_class[cla].channels)
3725 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726
3727 chan->reg_classes = cla;
3728 return 0;
3729}
3730
3731
Hai Shalomc1a21442022-02-04 13:43:00 -08003732static enum chan_allowed has_channel(struct wpa_global *global,
3733 struct hostapd_hw_modes *mode, u8 op_class,
3734 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003735{
3736 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737 unsigned int freq;
3738
Hai Shalomc1a21442022-02-04 13:43:00 -08003739 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003740 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003741 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742
3743 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003744 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 if (flags)
3746 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003747 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003748 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003749 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3750 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003751 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3752 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003753 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003754 }
3755 }
3756
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003757 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758}
3759
3760
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003761static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3762 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003763 u8 channel, const u8 *center_channels,
3764 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003765{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003766 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003767
3768 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3769 return 0;
3770
Hai Shalomc1a21442022-02-04 13:43:00 -08003771 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003772 /*
3773 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3774 * so the center channel is 6 channels away from the start/end.
3775 */
3776 if (channel >= center_channels[i] - 6 &&
3777 channel <= center_channels[i] + 6)
3778 return center_channels[i];
3779
3780 return 0;
3781}
3782
3783
Hai Shalomc1a21442022-02-04 13:43:00 -08003784static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3785 155, 171 };
3786static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3787 119, 135, 151, 167, 183, 199,
3788 215 };
3789
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003790static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3791 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003792 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003793{
3794 u8 center_chan;
3795 int i, flags;
3796 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003797 const u8 *chans;
3798 size_t num_chans;
3799 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003800
Hai Shalomc1a21442022-02-04 13:43:00 -08003801 if (is_6ghz) {
3802 chans = center_channels_6ghz_80mhz;
3803 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3804 } else {
3805 chans = center_channels_5ghz_80mhz;
3806 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3807 }
3808 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3809 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003810 if (!center_chan)
3811 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003812 if (!wpa_s->p2p_go_allow_dfs &&
3813 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003814 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3815
3816 /* check all the channels are available */
3817 for (i = 0; i < 4; i++) {
3818 int adj_chan = center_chan - 6 + i * 4;
3819
Hai Shalomc1a21442022-02-04 13:43:00 -08003820 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3821 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003822 if (res == NOT_ALLOWED)
3823 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003824 if (res == RADAR)
3825 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003826 if (res == NO_IR)
3827 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003828 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003829 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003830 return NOT_ALLOWED;
3831 } else if (is_6ghz &&
3832 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3833 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003834 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003835 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003836 }
3837
3838 return ret;
3839}
3840
3841
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003842static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3843 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003844 u8 channel, const u8 *center_channels,
3845 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003846{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003847 unsigned int i;
3848
3849 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3850 return 0;
3851
Hai Shalomc1a21442022-02-04 13:43:00 -08003852 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003853 /*
3854 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3855 * so the center channel is 14 channels away from the start/end.
3856 */
3857 if (channel >= center_channels[i] - 14 &&
3858 channel <= center_channels[i] + 14)
3859 return center_channels[i];
3860
3861 return 0;
3862}
3863
3864
Hai Shalomc1a21442022-02-04 13:43:00 -08003865static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3866static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3867 207 };
3868
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003869static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3870 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003871 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003872{
3873 u8 center_chan;
3874 int i, flags;
3875 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003876 const u8 *chans;
3877 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003878
Hai Shalomc1a21442022-02-04 13:43:00 -08003879 if (is_6ghz_op_class(op_class)) {
3880 chans = center_channels_6ghz_160mhz;
3881 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3882 } else {
3883 chans = center_channels_5ghz_160mhz;
3884 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3885 }
3886 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3887 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003888 if (!center_chan)
3889 return NOT_ALLOWED;
3890 /* VHT 160 MHz uses DFS channels in most countries. */
3891
3892 /* Check all the channels are available */
3893 for (i = 0; i < 8; i++) {
3894 int adj_chan = center_chan - 14 + i * 4;
3895
Hai Shalomc1a21442022-02-04 13:43:00 -08003896 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3897 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003898 if (res == NOT_ALLOWED)
3899 return NOT_ALLOWED;
3900
Hai Shalomc1a21442022-02-04 13:43:00 -08003901 if (res == RADAR)
3902 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003903 if (res == NO_IR)
3904 ret = NO_IR;
3905
Hai Shalomc1a21442022-02-04 13:43:00 -08003906 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003907 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
3908 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003909 return NOT_ALLOWED;
3910 } else if (is_6ghz_op_class(op_class) &&
3911 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3912 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003913 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003914 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003915 }
3916
3917 return ret;
3918}
3919
3920
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003921static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3922 struct hostapd_hw_modes *mode,
3923 u8 channel)
3924{
3925 struct ieee80211_edmg_config edmg;
3926
3927 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3928 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3929 return ALLOWED;
3930
3931 return NOT_ALLOWED;
3932}
3933
3934
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003935static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3936 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003937 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003938{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003939 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003940 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003941
Sunil Ravi89eba102022-09-13 21:04:37 -07003942 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3943 p2p_channel_to_freq(op_class, channel)))
3944 return NOT_ALLOWED;
3945
Hai Shalomc1a21442022-02-04 13:43:00 -08003946 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003947 if (bw == BW40MINUS) {
3948 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3949 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003950 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3951 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003952 } else if (bw == BW40PLUS) {
3953 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3954 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003955 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3956 NULL);
3957 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3958 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3959 return NOT_ALLOWED;
3960 if (get_6ghz_sec_channel(channel) < 0)
3961 res2 = has_channel(wpa_s->global, mode, op_class,
3962 channel - 4, NULL);
3963 else
3964 res2 = has_channel(wpa_s->global, mode, op_class,
3965 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003966 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003967 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3968 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003969 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003970 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3971 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003972 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3973 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003974 }
3975
3976 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3977 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003978 if (res == NO_IR || res2 == NO_IR)
3979 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003980 if (res == RADAR || res2 == RADAR)
3981 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003982 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003983}
3984
3985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003986static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003987 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003988 struct p2p_channels *cli_chan,
3989 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003991 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003992 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003994 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3996 "of all supported channels; assume dualband "
3997 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003998 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 }
4000
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004001 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004002
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004003 for (op = 0; global_op_class[op].op_class; op++) {
4004 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08004005 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004006 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07004007 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08004008 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009
Shuibing Dai64a8a892023-03-08 10:26:22 -08004010 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004011 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004012 continue;
4013
Hai Shalomfdcde762020-04-02 11:19:20 -07004014 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4015 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 if (mode == NULL)
4017 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004018 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4019 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004021 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004022
4023 /* Check for non-continuous jump in channel index
4024 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004025 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004026 ch < 149 && ch + o->inc > 149)
4027 ch = 149;
4028
Hai Shalomc1a21442022-02-04 13:43:00 -08004029 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4030 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004031 if (res == ALLOWED) {
4032 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004033 if (cla == P2P_MAX_REG_CLASSES)
4034 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004035 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4036 o->op_class);
4037 reg = &chan->reg_class[cla];
4038 cla++;
4039 reg->reg_class = o->op_class;
4040 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004041 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4042 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004043 reg->channel[reg->channels] = ch;
4044 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004045 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004046 wpa_s->conf->p2p_add_cli_chan) {
4047 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004048 if (cli_cla == P2P_MAX_REG_CLASSES)
4049 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004050 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4051 o->op_class);
4052 cli_reg = &cli_chan->reg_class[cli_cla];
4053 cli_cla++;
4054 cli_reg->reg_class = o->op_class;
4055 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004056 if (cli_reg->channels ==
4057 P2P_MAX_REG_CLASS_CHANNELS)
4058 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004059 cli_reg->channel[cli_reg->channels] = ch;
4060 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 }
4063 if (reg) {
4064 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4065 reg->channel, reg->channels);
4066 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004067 if (cli_reg) {
4068 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4069 cli_reg->channel, cli_reg->channels);
4070 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 }
4072
4073 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004074 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004076 return 0;
4077}
4078
4079
Hai Shalomc1a21442022-02-04 13:43:00 -08004080int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4081 struct hostapd_hw_modes *mode,
4082 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004083{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004084 int op;
4085 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004086
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004087 for (op = 0; global_op_class[op].op_class; op++) {
4088 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004089 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004090
Hai Shalomc1a21442022-02-04 13:43:00 -08004091 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4092 * driver offloaded DFS. */
4093 if ((o->p2p == NO_P2P_SUPP &&
4094 (!is_dfs_global_op_class(o->op_class) ||
4095 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004096 (is_6ghz_op_class(o->op_class) &&
4097 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004098 continue;
4099
Sunil Ravi77d572f2023-01-17 23:58:31 +00004100 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4101 * set up the BSS with a primary 20 MHz channel that coincides
4102 * with a preferred scanning channel (PSC)."
4103 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4104 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4105 * match the PSC.
4106 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004107 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4108 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004109 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004110
Sunil Ravi77d572f2023-01-17 23:58:31 +00004111 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004112 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004113 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4114 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004115 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004116 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004117 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4118 ch, o->bw);
4119 if (ret == ALLOWED) {
4120 if (is_6ghz_op_class(o->op_class) &&
4121 o->bw == BW40)
4122 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004123 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004124 }
4125 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4126 /* Allow RADAR channels used for driver
4127 * offloaded DFS */
4128 return (o->bw == BW40MINUS) ? -1 : 1;
4129 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004130 }
4131 }
4132 return 0;
4133}
4134
4135
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004136int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004137 struct hostapd_hw_modes *mode, u8 channel,
4138 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004139{
Hai Shalomc1a21442022-02-04 13:43:00 -08004140 const u8 *chans;
4141 size_t num_chans;
4142 enum chan_allowed ret;
4143
4144 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4145 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004146 return 0;
4147
Hai Shalomc1a21442022-02-04 13:43:00 -08004148 if (is_6ghz_op_class(op_class)) {
4149 chans = center_channels_6ghz_80mhz;
4150 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4151 } else {
4152 chans = center_channels_5ghz_80mhz;
4153 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4154 }
4155 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4156 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004157}
4158
4159
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004160int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004161 struct hostapd_hw_modes *mode, u8 channel,
4162 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004163{
Hai Shalomc1a21442022-02-04 13:43:00 -08004164 const u8 *chans;
4165 size_t num_chans;
4166 enum chan_allowed ret;
4167
4168 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4169 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004170 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004171 if (is_6ghz_op_class(op_class)) {
4172 chans = center_channels_6ghz_160mhz;
4173 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4174 } else {
4175 chans = center_channels_5ghz_160mhz;
4176 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4177 }
4178 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4179 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004180}
4181
4182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4184 size_t buf_len)
4185{
4186 struct wpa_supplicant *wpa_s = ctx;
4187
4188 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004189 if (ether_addr_equal(wpa_s->own_addr, interface_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190 break;
4191 }
4192 if (wpa_s == NULL)
4193 return -1;
4194
4195 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4196}
4197
4198
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004199struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4200 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004201{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004202 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4203 struct wpa_ssid *s = wpa_s->current_ssid;
4204 if (s == NULL)
4205 continue;
4206 if (s->mode != WPAS_MODE_P2P_GO &&
4207 s->mode != WPAS_MODE_AP &&
4208 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4209 continue;
4210 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004211 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004212 continue;
4213 return wpa_s;
4214 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004215
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004216 return NULL;
4217
4218}
4219
4220
4221struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4222 const u8 *peer_dev_addr)
4223{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004224 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4225 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004226 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004227 continue;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004228 if (ether_addr_equal(wpa_s->go_dev_addr, peer_dev_addr))
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004229 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004230 }
4231
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004232 return NULL;
4233}
4234
4235
4236static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4237{
4238 struct wpa_supplicant *wpa_s = ctx;
4239
4240 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004241}
4242
4243
Dmitry Shmidt18463232014-01-24 12:29:41 -08004244static int wpas_is_concurrent_session_active(void *ctx)
4245{
4246 struct wpa_supplicant *wpa_s = ctx;
4247 struct wpa_supplicant *ifs;
4248
4249 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4250 if (ifs == wpa_s)
4251 continue;
4252 if (ifs->wpa_state > WPA_ASSOCIATED)
4253 return 1;
4254 }
4255 return 0;
4256}
4257
4258
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004259static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4260{
4261 struct wpa_supplicant *wpa_s = ctx;
4262 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4263}
4264
4265
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004266int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4267 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004268{
4269 struct wpa_interface iface;
4270 struct wpa_supplicant *p2pdev_wpa_s;
4271 char ifname[100];
4272 char force_name[100];
4273 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004274 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004275
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004276 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4277 wpa_s->ifname);
4278 if (os_snprintf_error(sizeof(ifname), ret))
4279 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004280 /* Cut length at the maximum size. Note that we don't need to ensure
4281 * collision free names here as the created interface is not a netdev.
4282 */
4283 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004284 force_name[0] = '\0';
4285 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004286
4287 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4288 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4289 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4290
4291 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004292 force_name, wpa_s->pending_interface_addr, NULL);
4293 if (ret < 0) {
4294 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4295 return ret;
4296 }
4297 os_strlcpy(wpa_s->pending_interface_name, ifname,
4298 sizeof(wpa_s->pending_interface_name));
4299
4300 os_memset(&iface, 0, sizeof(iface));
4301 iface.p2p_mgmt = 1;
4302 iface.ifname = wpa_s->pending_interface_name;
4303 iface.driver = wpa_s->driver->name;
4304 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004305
4306 /*
4307 * If a P2P Device configuration file was given, use it as the interface
4308 * configuration file (instead of using parent's configuration file.
4309 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004310 if (conf_p2p_dev) {
4311 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004312 iface.ctrl_interface = NULL;
4313 } else {
4314 iface.confname = wpa_s->confname;
4315 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4316 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004317
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004318 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004319 if (!p2pdev_wpa_s) {
4320 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4321 return -1;
4322 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004323
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004324 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004325 wpa_s->pending_interface_name[0] = '\0';
4326 return 0;
4327}
4328
4329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004330static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4331 const u8 *noa, size_t noa_len)
4332{
4333 struct wpa_supplicant *wpa_s, *intf = ctx;
4334 char hex[100];
4335
4336 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4337 if (wpa_s->waiting_presence_resp)
4338 break;
4339 }
4340 if (!wpa_s) {
4341 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4342 return;
4343 }
4344 wpa_s->waiting_presence_resp = 0;
4345
4346 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4347 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4348 " status=%u noa=%s", MAC2STR(src), status, hex);
4349}
4350
4351
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004352static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4353 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004354 u8 *ret_ssid, size_t *ret_ssid_len,
4355 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004356{
4357 struct wpa_supplicant *wpa_s = ctx;
4358 struct wpa_ssid *s;
4359
4360 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4361 if (s) {
4362 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4363 *ret_ssid_len = s->ssid_len;
4364 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004365
4366 if (s->mode != WPAS_MODE_P2P_GO) {
4367 os_memset(intended_iface_addr, 0, ETH_ALEN);
4368 } else if (wpas_p2p_create_iface(wpa_s)) {
4369 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4370 return 0;
4371
4372 os_memcpy(intended_iface_addr,
4373 wpa_s->pending_interface_addr, ETH_ALEN);
4374 } else {
4375 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4376 ETH_ALEN);
4377 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004378 return 1;
4379 }
4380
4381 return 0;
4382}
4383
4384
4385static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004386 u8 *ssid, size_t *ssid_len, int *group_iface,
4387 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004388{
4389 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004390 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004391 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004392
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004393 /*
4394 * group_iface will be set to 1 only if a dedicated interface for P2P
4395 * role is required. First, we try to reuse an active GO. However,
4396 * if it is not present, we will try to reactivate an existing
4397 * persistent group and set group_iface to 1, so the caller will know
4398 * that the pending interface should be used.
4399 */
4400 *group_iface = 0;
4401
4402 if (freq)
4403 *freq = 0;
4404
4405 go = wpas_p2p_get_go_group(wpa_s);
4406 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004408 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004409 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004410 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4411 else
4412 return 0;
4413 } else {
4414 s = go->current_ssid;
4415 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4416 if (freq)
4417 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004418 }
4419
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004420 os_memcpy(ssid, s->ssid, s->ssid_len);
4421 *ssid_len = s->ssid_len;
4422
4423 return 1;
4424}
4425
4426
4427static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4428 const u8 *ssid, size_t ssid_len)
4429{
4430 struct wpa_supplicant *wpa_s = ctx;
4431 struct wpa_ssid *s;
4432 int save_config = 0;
4433 size_t i;
4434
4435 /* Start with our first choice of Persistent Groups */
4436 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4437 if (go && ssid && ssid_len &&
4438 s->ssid_len == ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004439 ether_addr_equal(go, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004440 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4441 break;
4442
4443 /* Remove stale persistent group */
4444 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004445 wpa_dbg(wpa_s, MSG_DEBUG,
4446 "P2P: Remove stale persistent group id=%d",
4447 s->id);
4448 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004449 wpa_config_remove_network(wpa_s->conf, s->id);
4450 save_config = 1;
4451 continue;
4452 }
4453
4454 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004455 if (!ether_addr_equal(s->p2p_client_list +
4456 i * 2 * ETH_ALEN, peer))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004457 continue;
4458
4459 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4460 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4461 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4462 break;
4463 }
4464 s->num_p2p_clients--;
4465 save_config = 1;
4466 }
4467
4468 if (save_config)
4469 p2p_config_write(wpa_s);
4470
4471 /* Return TRUE if valid SSID remains */
4472 return s != NULL;
4473}
4474
4475
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004476static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4477 const u8 *feat_cap, size_t feat_cap_len)
4478{
4479 static const char pref[] = " feature_cap=";
4480 int ret;
4481
4482 buf[0] = '\0';
4483
4484 /*
4485 * We expect a feature capability to contain at least one byte to be
4486 * reported. The string buffer provided by the caller function is
4487 * expected to be big enough to contain all bytes of the attribute for
4488 * known specifications. This function truncates the reported bytes if
4489 * the feature capability data exceeds the string buffer size.
4490 */
4491 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4492 return;
4493
4494 os_memcpy(buf, pref, sizeof(pref));
4495 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4496 buf_len - sizeof(pref) + 1,
4497 feat_cap, feat_cap_len);
4498
4499 if (ret != (2 * (int) feat_cap_len))
4500 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4501}
4502
4503
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004504static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4505 const u8 *adv_mac, const u8 *ses_mac,
4506 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4507 u8 conncap, int passwd_id,
4508 const u8 *persist_ssid,
4509 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004510 int prov_start, const char *session_info,
4511 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004512 unsigned int freq,
4513 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004514{
4515 struct wpa_supplicant *wpa_s = ctx;
4516 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004517 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004518 int save_config = 0;
4519 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004520 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004521
4522 if (!dev)
4523 return;
4524
4525 os_memset(mac, 0, ETH_ALEN);
4526 if (!adv_mac)
4527 adv_mac = mac;
4528 if (!ses_mac)
4529 ses_mac = mac;
4530 if (!grp_mac)
4531 grp_mac = mac;
4532
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004533 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4534 feat_cap, feat_cap_len);
4535
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004536 if (prov_start) {
4537 if (session_info == NULL) {
4538 wpa_msg_global(wpa_s, MSG_INFO,
4539 P2P_EVENT_P2PS_PROVISION_START MACSTR
4540 " adv_id=%x conncap=%x"
4541 " adv_mac=" MACSTR
4542 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004543 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004544 MAC2STR(dev), adv_id, conncap,
4545 MAC2STR(adv_mac),
4546 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004547 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004548 } else {
4549 wpa_msg_global(wpa_s, MSG_INFO,
4550 P2P_EVENT_P2PS_PROVISION_START MACSTR
4551 " adv_id=%x conncap=%x"
4552 " adv_mac=" MACSTR
4553 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004554 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004555 MAC2STR(dev), adv_id, conncap,
4556 MAC2STR(adv_mac),
4557 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004558 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004559 }
4560 return;
4561 }
4562
4563 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4564 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4565
4566 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4567 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4568 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4569
4570 if (persistent_go && !persistent_go->num_p2p_clients) {
4571 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004572 wpa_dbg(wpa_s, MSG_DEBUG,
4573 "P2P: Remove empty persistent group id=%d",
4574 persistent_go->id);
4575 wpas_notify_persistent_group_removed(wpa_s,
4576 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004577 wpa_config_remove_network(wpa_s->conf,
4578 persistent_go->id);
4579 }
4580
4581 wpa_msg_global(wpa_s, MSG_INFO,
4582 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4583 " status=%d"
4584 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004585 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004586 MAC2STR(dev), status,
4587 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004588 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004589 return;
4590 }
4591
4592 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004593 if (persist_ssid && persist_ssid_size)
4594 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4595 persist_ssid_size);
4596
4597 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4598 is_zero_ether_addr(grp_mac)) {
4599 wpa_dbg(wpa_s, MSG_ERROR,
4600 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4601 return;
4602 }
4603
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004604 for (;;) {
4605 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4606 if (!stale)
4607 break;
4608
4609 if (s && s->ssid_len == stale->ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004610 ether_addr_equal(stale->bssid, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004611 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4612 break;
4613
4614 /* Remove stale persistent group */
4615 if (stale->mode != WPAS_MODE_P2P_GO ||
4616 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004617 wpa_dbg(wpa_s, MSG_DEBUG,
4618 "P2P: Remove stale persistent group id=%d",
4619 stale->id);
4620 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004621 wpa_config_remove_network(wpa_s->conf, stale->id);
4622 } else {
4623 size_t i;
4624
4625 for (i = 0; i < stale->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004626 if (ether_addr_equal(stale->p2p_client_list +
4627 i * ETH_ALEN, dev)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004628 os_memmove(stale->p2p_client_list +
4629 i * ETH_ALEN,
4630 stale->p2p_client_list +
4631 (i + 1) * ETH_ALEN,
4632 (stale->num_p2p_clients -
4633 i - 1) * ETH_ALEN);
4634 break;
4635 }
4636 }
4637 stale->num_p2p_clients--;
4638 }
4639 save_config = 1;
4640 }
4641
4642 if (save_config)
4643 p2p_config_write(wpa_s);
4644
4645 if (s) {
4646 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4647 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4648
4649 if (persistent_go && s != persistent_go &&
4650 !persistent_go->num_p2p_clients) {
4651 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004652 wpa_dbg(wpa_s, MSG_DEBUG,
4653 "P2P: Remove empty persistent group id=%d",
4654 persistent_go->id);
4655 wpas_notify_persistent_group_removed(wpa_s,
4656 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004657 wpa_config_remove_network(wpa_s->conf,
4658 persistent_go->id);
4659 /* Save config */
4660 }
4661
4662 wpa_msg_global(wpa_s, MSG_INFO,
4663 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4664 " status=%d"
4665 " adv_id=%x adv_mac=" MACSTR
4666 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004667 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004668 MAC2STR(dev), status,
4669 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004670 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004671 return;
4672 }
4673
Hai Shalom5f92bc92019-04-18 11:54:11 -07004674 wpa_s->global->pending_p2ps_group = 0;
4675 wpa_s->global->pending_p2ps_group_freq = 0;
4676
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004677 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004678 /*
4679 * We need to copy the interface name. Simply saving a
4680 * pointer isn't enough, since if we use pending_interface_name
4681 * it will be overwritten when the group is added.
4682 */
4683 char go_ifname[100];
4684
4685 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004686 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004687 if (!response_done) {
4688 wpa_s->global->pending_p2ps_group = 1;
4689 wpa_s->global->pending_p2ps_group_freq = freq;
4690 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004691
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004692 if (!wpas_p2p_create_iface(wpa_s))
4693 os_memcpy(go_ifname, wpa_s->ifname,
4694 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004695 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004696 os_memcpy(go_ifname,
4697 wpa_s->pending_interface_name,
4698 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004699
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004700 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004701 wpas_p2ps_prov_complete(
4702 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4703 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004704 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004705 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4706 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004707 return;
4708 }
4709
4710 /* If PD Resp complete, start up the GO */
4711 if (response_done && persistent_go) {
4712 wpas_p2p_group_add_persistent(
4713 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004714 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004715 persistent_go->mode ==
4716 WPAS_MODE_P2P_GO ?
4717 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi036cec52023-03-29 11:35:17 -07004718 0, 0, false, 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004719 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004720 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004721 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004722 }
4723
4724 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004725 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4726 ETH_ALEN);
4727 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004728 }
4729 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004730 os_memcpy(go_ifname, go_wpa_s->ifname,
4731 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004733 if (is_zero_ether_addr(grp_mac)) {
4734 wpa_dbg(go_wpa_s, MSG_DEBUG,
4735 "P2P: Setting PIN-1 for ANY");
4736 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4737 "12345670", NULL, 0,
4738 0);
4739 } else {
4740 wpa_dbg(go_wpa_s, MSG_DEBUG,
4741 "P2P: Setting PIN-1 for " MACSTR,
4742 MAC2STR(grp_mac));
4743 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4744 "12345670", NULL, 0,
4745 0);
4746 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004747
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004748 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4749 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004750 }
4751
4752 wpa_msg_global(wpa_s, MSG_INFO,
4753 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4754 " status=%d conncap=%x"
4755 " adv_id=%x adv_mac=" MACSTR
4756 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004757 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004758 MAC2STR(dev), status, conncap,
4759 adv_id, MAC2STR(adv_mac),
4760 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004761 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004762 return;
4763 }
4764
4765 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4766 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4767
4768 if (persistent_go && !persistent_go->num_p2p_clients) {
4769 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004770 wpa_dbg(wpa_s, MSG_DEBUG,
4771 "P2P: Remove empty persistent group id=%d",
4772 persistent_go->id);
4773 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004774 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4775 }
4776
4777 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004778 char ssid_hex[32 * 2 + 1];
4779
4780 if (group_ssid)
4781 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4782 group_ssid, group_ssid_len);
4783 else
4784 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004785 wpa_msg_global(wpa_s, MSG_INFO,
4786 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4787 " status=%d conncap=%x"
4788 " adv_id=%x adv_mac=" MACSTR
4789 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004790 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004791 MAC2STR(dev), status, conncap,
4792 adv_id, MAC2STR(adv_mac),
4793 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004794 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4795 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004796 } else {
4797 wpa_msg_global(wpa_s, MSG_INFO,
4798 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4799 " status=%d conncap=%x"
4800 " adv_id=%x adv_mac=" MACSTR
4801 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004802 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004803 MAC2STR(dev), status, conncap,
4804 adv_id, MAC2STR(adv_mac),
4805 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004806 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004807 }
4808}
4809
4810
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004811static int _wpas_p2p_in_progress(void *ctx)
4812{
4813 struct wpa_supplicant *wpa_s = ctx;
4814 return wpas_p2p_in_progress(wpa_s);
4815}
4816
4817
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004818static int wpas_prov_disc_resp_cb(void *ctx)
4819{
4820 struct wpa_supplicant *wpa_s = ctx;
4821 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004822 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004823
4824 if (!wpa_s->global->pending_p2ps_group)
4825 return 0;
4826
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004827 freq = wpa_s->global->pending_p2ps_group_freq;
4828 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004829 wpa_s->global->pending_p2ps_group = 0;
4830
4831 if (wpas_p2p_get_go_group(wpa_s))
4832 return 0;
4833 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4834
4835 if (persistent_go) {
4836 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004837 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4838 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004839 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004840 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07004841 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004842 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004843 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4844 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004845 }
4846
4847 return 1;
4848}
4849
4850
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004851static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4852 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004853 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004854{
4855 struct wpa_supplicant *wpa_s = ctx;
4856
4857 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4858 WPA_IF_P2P_CLIENT, len, freq_list);
4859}
4860
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004861int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4862{
JaeMan Park9de97322022-07-11 15:36:43 +09004863 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004864 u8 addr[ETH_ALEN] = {0};
4865
4866 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4867 return 0;
4868
Hai Shalom60840252021-02-19 19:02:11 -08004869 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4870 if (is_zero_ether_addr(
4871 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4872 !is_zero_ether_addr(wpa_s->own_addr)) {
4873 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4874 wpa_s->own_addr, ETH_ALEN);
4875 }
4876 return 0;
4877 }
4878
4879 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004880 if (random_mac_addr(addr) < 0) {
4881 wpa_msg(wpa_s, MSG_INFO,
4882 "Failed to generate random MAC address");
4883 return -EINVAL;
4884 }
4885
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004886 /* Store generated MAC address. */
4887 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4888 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004889 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004890 /* If there are existing saved groups, restore last MAC address.
4891 * if there is no last used MAC address, the last one is
4892 * factory MAC. */
4893 if (is_zero_ether_addr(
4894 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004895 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004896 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4897 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004898 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4899 }
4900
JaeMan Park9de97322022-07-11 15:36:43 +09004901 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4902
4903 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004904 wpa_msg(wpa_s, MSG_INFO,
4905 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004906 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004907 }
4908
JaeMan Park9de97322022-07-11 15:36:43 +09004909 ret = wpa_supplicant_update_mac_addr(wpa_s);
4910
4911 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004912 wpa_msg(wpa_s, MSG_INFO,
4913 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004914 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004915 }
4916
4917 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4918 MAC2STR(addr));
4919
4920 return 0;
4921}
4922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004923/**
4924 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4925 * @global: Pointer to global data from wpa_supplicant_init()
4926 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4927 * Returns: 0 on success, -1 on failure
4928 */
4929int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4930{
4931 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 int i;
4933
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004934 if (wpa_s->conf->p2p_disabled)
4935 return 0;
4936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4938 return 0;
4939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940 if (global->p2p)
4941 return 0;
4942
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004943 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4944 wpa_msg(wpa_s, MSG_ERROR,
4945 "Failed to initialize P2P random MAC address.");
4946 return -1;
4947 }
4948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004951 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 p2p.p2p_scan = wpas_p2p_scan;
4953 p2p.send_action = wpas_send_action;
4954 p2p.send_action_done = wpas_send_action_done;
4955 p2p.go_neg_completed = wpas_go_neg_completed;
4956 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4957 p2p.dev_found = wpas_dev_found;
4958 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004959 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960 p2p.start_listen = wpas_start_listen;
4961 p2p.stop_listen = wpas_stop_listen;
4962 p2p.send_probe_resp = wpas_send_probe_resp;
4963 p2p.sd_request = wpas_sd_request;
4964 p2p.sd_response = wpas_sd_response;
4965 p2p.prov_disc_req = wpas_prov_disc_req;
4966 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004967 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968 p2p.invitation_process = wpas_invitation_process;
4969 p2p.invitation_received = wpas_invitation_received;
4970 p2p.invitation_result = wpas_invitation_result;
4971 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004972 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004973 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004974 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004975 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004976 p2p.get_persistent_group = wpas_get_persistent_group;
4977 p2p.get_go_info = wpas_get_go_info;
4978 p2p.remove_stale_groups = wpas_remove_stale_groups;
4979 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4980 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4981 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004982 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004983 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07004984 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004985
4986 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004987 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 p2p.dev_name = wpa_s->conf->device_name;
4989 p2p.manufacturer = wpa_s->conf->manufacturer;
4990 p2p.model_name = wpa_s->conf->model_name;
4991 p2p.model_number = wpa_s->conf->model_number;
4992 p2p.serial_number = wpa_s->conf->serial_number;
4993 if (wpa_s->wps) {
4994 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4995 p2p.config_methods = wpa_s->wps->config_methods;
4996 }
4997
Hai Shalom60840252021-02-19 19:02:11 -08004998 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4999 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005000 wpa_printf(MSG_ERROR,
5001 "P2P: Failed to configure supported channel list");
5002 return -1;
5003 }
5004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 if (wpa_s->conf->p2p_listen_reg_class &&
5006 wpa_s->conf->p2p_listen_channel) {
5007 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5008 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005009 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005011 /*
5012 * Pick one of the social channels randomly as the listen
5013 * channel.
5014 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005015 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005016 &p2p.channel,
5017 &global->p2p_go_avoid_freq,
5018 &global->p2p_disallow_freq) !=
5019 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005020 wpa_printf(MSG_INFO,
5021 "P2P: No social channels supported by the driver - do not enable P2P");
5022 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005023 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005024 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005026 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5027 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005028
5029 if (wpa_s->conf->p2p_oper_reg_class &&
5030 wpa_s->conf->p2p_oper_channel) {
5031 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5032 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5033 p2p.cfg_op_channel = 1;
5034 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5035 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5036
5037 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005039 * Use random operation channel from 2.4 GHz band social
5040 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5041 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005042 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005043 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005044 &p2p.op_channel, NULL,
5045 NULL) != 0) {
5046 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005047 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005048 p2p.op_reg_class = 0;
5049 p2p.op_channel = 0;
5050 /* This will be overridden during group setup in
5051 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053 p2p.cfg_op_channel = 0;
5054 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5055 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5056 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005057
5058 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5059 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5060 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5061 }
5062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5064 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5065 p2p.country[2] = 0x04;
5066 } else
5067 os_memcpy(p2p.country, "XX\x04", 3);
5068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5070 WPS_DEV_TYPE_LEN);
5071
5072 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5073 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5074 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5075
5076 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5077 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5078
5079 p2p.max_peers = 100;
5080
5081 if (wpa_s->conf->p2p_ssid_postfix) {
5082 p2p.ssid_postfix_len =
5083 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5084 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5085 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5086 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5087 p2p.ssid_postfix_len);
5088 }
5089
5090 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5091
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005092 p2p.max_listen = wpa_s->max_remain_on_chan;
5093
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005094 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5095 wpa_s->conf->p2p_passphrase_len <= 63)
5096 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5097 else
5098 p2p.passphrase_len = 8;
5099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 global->p2p = p2p_init(&p2p);
5101 if (global->p2p == NULL)
5102 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005103 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104
5105 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5106 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5107 continue;
5108 p2p_add_wps_vendor_extension(
5109 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5110 }
5111
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005112 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 return 0;
5115}
5116
5117
5118/**
5119 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5120 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5121 *
5122 * This function deinitialize per-interface P2P data.
5123 */
5124void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5125{
5126 if (wpa_s->driver && wpa_s->drv_priv)
5127 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005128
5129 if (wpa_s->go_params) {
5130 /* Clear any stored provisioning info */
5131 p2p_clear_provisioning_info(
5132 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005133 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005134 }
5135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 os_free(wpa_s->go_params);
5137 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005138 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5140 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005141 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5143 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5144 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005145 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005146 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005147 wpas_p2p_listen_work_done(wpa_s);
5148 if (wpa_s->p2p_send_action_work) {
5149 os_free(wpa_s->p2p_send_action_work->ctx);
5150 radio_work_done(wpa_s->p2p_send_action_work);
5151 wpa_s->p2p_send_action_work = NULL;
5152 }
5153 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005155 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5156 wpa_s->p2p_oob_dev_pw = NULL;
5157
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005158 os_free(wpa_s->p2p_group_common_freqs);
5159 wpa_s->p2p_group_common_freqs = NULL;
5160 wpa_s->p2p_group_common_freqs_num = 0;
5161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 /* TODO: remove group interface from the driver if this wpa_s instance
5163 * is on top of a P2P group interface */
5164}
5165
5166
5167/**
5168 * wpas_p2p_deinit_global - Deinitialize global P2P module
5169 * @global: Pointer to global data from wpa_supplicant_init()
5170 *
5171 * This function deinitializes the global (per device) P2P module.
5172 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005173static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174{
5175 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005177 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005178
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005179 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180
5181 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5183 wpa_s = wpa_s->next;
5184 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 tmp = global->ifaces;
5186 while (tmp &&
5187 (tmp == wpa_s ||
5188 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5189 tmp = tmp->next;
5190 }
5191 if (tmp == NULL)
5192 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005193 /* Disconnect from the P2P group and deinit the interface */
5194 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195 }
5196
5197 /*
5198 * Deinit GO data on any possibly remaining interface (if main
5199 * interface is used as GO).
5200 */
5201 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5202 if (wpa_s->ap_iface)
5203 wpas_p2p_group_deinit(wpa_s);
5204 }
5205
5206 p2p_deinit(global->p2p);
5207 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005208 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209}
5210
5211
5212static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5213{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005214 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005215 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216 if (wpa_s->drv_flags &
5217 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5218 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5219 return 1; /* P2P group requires a new interface in every case
5220 */
5221 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5222 return 0; /* driver does not support concurrent operations */
5223 if (wpa_s->global->ifaces->next)
5224 return 1; /* more that one interface already in use */
5225 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5226 return 1; /* this interface is already in use */
5227 return 0;
5228}
5229
5230
5231static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5232 const u8 *peer_addr,
5233 enum p2p_wps_method wps_method,
5234 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005235 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005236 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005237{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005238 if (persistent_group && wpa_s->conf->persistent_reconnect)
5239 persistent_group = 2;
5240
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005241 /*
5242 * Increase GO config timeout if HT40 is used since it takes some time
5243 * to scan channels for coex purposes before the BSS can be started.
5244 */
5245 p2p_set_config_timeout(wpa_s->global->p2p,
5246 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5249 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005250 persistent_group, ssid ? ssid->ssid : NULL,
5251 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005252 wpa_s->p2p_pd_before_go_neg, pref_freq,
5253 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5254 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255}
5256
5257
5258static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5259 const u8 *peer_addr,
5260 enum p2p_wps_method wps_method,
5261 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005262 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005263 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005265 if (persistent_group && wpa_s->conf->persistent_reconnect)
5266 persistent_group = 2;
5267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005268 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5269 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005271 ssid ? ssid->ssid_len : 0, pref_freq,
5272 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5273 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274}
5275
5276
5277static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5278{
5279 wpa_s->p2p_join_scan_count++;
5280 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5281 wpa_s->p2p_join_scan_count);
5282 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5283 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5284 " for join operationg - stop join attempt",
5285 MAC2STR(wpa_s->pending_join_iface_addr));
5286 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005287 if (wpa_s->p2p_auto_pd) {
5288 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005289 wpa_msg_global(wpa_s, MSG_INFO,
5290 P2P_EVENT_PROV_DISC_FAILURE
5291 " p2p_dev_addr=" MACSTR " status=N/A",
5292 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005293 return;
5294 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005295 if (wpa_s->p2p_fallback_to_go_neg) {
5296 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5297 "failed - fall back to GO Negotiation");
5298 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5299 P2P_EVENT_FALLBACK_TO_GO_NEG
5300 "reason=join-failed");
5301 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5302 return;
5303 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005304 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005305 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005306 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307 }
5308}
5309
5310
Dmitry Shmidt04949592012-07-19 12:16:46 -07005311static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5312{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005313 int res;
5314 unsigned int num, i;
5315 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005316
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005317 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5318 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005320 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005321
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005322 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5323 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005324 if (!freqs)
5325 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005326
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005327 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5328 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005329
5330 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005331 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005332 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5333 freq);
5334 res = 0;
5335 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005336 }
5337 }
5338
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005339 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005340 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005341
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005342exit_free:
5343 os_free(freqs);
5344 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005345}
5346
5347
5348static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5349 const u8 *peer_dev_addr)
5350{
5351 struct wpa_bss *bss;
5352 int updated;
5353
5354 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5355 if (bss == NULL)
5356 return -1;
5357 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5358 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5359 "last scan");
5360 return 0;
5361 }
5362
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005363 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5364 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005365 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5366 "%ld.%06ld (%supdated in last scan)",
5367 bss->last_update.sec, bss->last_update.usec,
5368 updated ? "": "not ");
5369
5370 return updated;
5371}
5372
5373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005374static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5375 struct wpa_scan_results *scan_res)
5376{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005377 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005378 int freq;
5379 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5382
5383 if (wpa_s->global->p2p_disabled)
5384 return;
5385
Dmitry Shmidt04949592012-07-19 12:16:46 -07005386 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5387 scan_res ? (int) scan_res->num : -1,
5388 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389
5390 if (scan_res)
5391 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5392
Dmitry Shmidt04949592012-07-19 12:16:46 -07005393 if (wpa_s->p2p_auto_pd) {
5394 int join = wpas_p2p_peer_go(wpa_s,
5395 wpa_s->pending_join_dev_addr);
5396 if (join == 0 &&
5397 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5398 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005399 bss = wpa_bss_get_bssid_latest(
5400 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005401 if (bss) {
5402 freq = bss->freq;
5403 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5404 "the peer " MACSTR " at %d MHz",
5405 wpa_s->auto_pd_scan_retry,
5406 MAC2STR(wpa_s->
5407 pending_join_dev_addr),
5408 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005409 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005410 return;
5411 }
5412 }
5413
5414 if (join < 0)
5415 join = 0;
5416
5417 wpa_s->p2p_auto_pd = 0;
5418 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5419 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5420 MAC2STR(wpa_s->pending_join_dev_addr), join);
5421 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005422 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005423 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005424 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005425 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005426 wpa_msg_global(wpa_s, MSG_INFO,
5427 P2P_EVENT_PROV_DISC_FAILURE
5428 " p2p_dev_addr=" MACSTR " status=N/A",
5429 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005430 }
5431 return;
5432 }
5433
5434 if (wpa_s->p2p_auto_join) {
5435 int join = wpas_p2p_peer_go(wpa_s,
5436 wpa_s->pending_join_dev_addr);
5437 if (join < 0) {
5438 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5439 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005440 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005441 P2P_EVENT_FALLBACK_TO_GO_NEG
5442 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5444 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5445 wpa_s->p2p_persistent_group, 0, 0, 0,
5446 wpa_s->p2p_go_intent,
5447 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005448 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005449 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005450 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005451 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005452 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005453 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005454 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005455 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005456 NULL, 0,
5457 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005458 return;
5459 }
5460
5461 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5462 "try to join the group", join ? "" :
5463 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005464 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005465 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005466 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005468 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005469 }
5470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5472 wpa_s->pending_join_iface_addr);
5473 if (freq < 0 &&
5474 p2p_get_interface_addr(wpa_s->global->p2p,
5475 wpa_s->pending_join_dev_addr,
5476 iface_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005477 !ether_addr_equal(iface_addr, wpa_s->pending_join_dev_addr) &&
5478 !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5480 "address for join from " MACSTR " to " MACSTR
5481 " based on newly discovered P2P peer entry",
5482 MAC2STR(wpa_s->pending_join_iface_addr),
5483 MAC2STR(iface_addr));
5484 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5485 ETH_ALEN);
5486
5487 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5488 wpa_s->pending_join_iface_addr);
5489 }
5490 if (freq >= 0) {
5491 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5492 "from P2P peer table: %d MHz", freq);
5493 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005494 if (wpa_s->p2p_join_ssid_len) {
5495 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5496 MACSTR " and SSID %s",
5497 MAC2STR(wpa_s->pending_join_iface_addr),
5498 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5499 wpa_s->p2p_join_ssid_len));
5500 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5501 wpa_s->p2p_join_ssid,
5502 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005503 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005504 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5505 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5506 bss = wpa_bss_get_bssid_latest(wpa_s,
5507 wpa_s->pending_join_iface_addr);
5508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005510 u8 dev_addr[ETH_ALEN];
5511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512 freq = bss->freq;
5513 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005514 "from BSS table: %d MHz (SSID %s)", freq,
5515 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005516 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005517 dev_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005518 ether_addr_equal(wpa_s->pending_join_dev_addr,
5519 wpa_s->pending_join_iface_addr) &&
5520 !ether_addr_equal(dev_addr, wpa_s->pending_join_dev_addr)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005521 wpa_printf(MSG_DEBUG,
5522 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5523 MAC2STR(dev_addr),
5524 MAC2STR(wpa_s->pending_join_dev_addr));
5525 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5526 ETH_ALEN);
5527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528 }
5529 if (freq > 0) {
5530 u16 method;
5531
Dmitry Shmidt04949592012-07-19 12:16:46 -07005532 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005533 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005534 P2P_EVENT_GROUP_FORMATION_FAILURE
5535 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005536 wpas_notify_p2p_group_formation_failure(
5537 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 return;
5539 }
5540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5542 "prior to joining an existing group (GO " MACSTR
5543 " freq=%u MHz)",
5544 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5545 wpa_s->pending_pd_before_join = 1;
5546
5547 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 case WPS_PIN_DISPLAY:
5549 method = WPS_CONFIG_KEYPAD;
5550 break;
5551 case WPS_PIN_KEYPAD:
5552 method = WPS_CONFIG_DISPLAY;
5553 break;
5554 case WPS_PBC:
5555 method = WPS_CONFIG_PUSHBUTTON;
5556 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005557 case WPS_P2PS:
5558 method = WPS_CONFIG_P2PS;
5559 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560 default:
5561 method = 0;
5562 break;
5563 }
5564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005565 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5566 wpa_s->pending_join_dev_addr) ==
5567 method)) {
5568 /*
5569 * We have already performed provision discovery for
5570 * joining the group. Proceed directly to join
5571 * operation without duplicated provision discovery. */
5572 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5573 "with " MACSTR " already done - proceed to "
5574 "join",
5575 MAC2STR(wpa_s->pending_join_dev_addr));
5576 wpa_s->pending_pd_before_join = 0;
5577 goto start;
5578 }
5579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005581 wpa_s->pending_join_dev_addr,
5582 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005583 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5585 "Discovery Request before joining an "
5586 "existing group");
5587 wpa_s->pending_pd_before_join = 0;
5588 goto start;
5589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590 return;
5591 }
5592
5593 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5594 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5595 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5596 wpas_p2p_check_join_scan_limit(wpa_s);
5597 return;
5598
5599start:
5600 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005601 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5602 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603}
5604
5605
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005606static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5607 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005608{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 int ret;
5610 struct wpa_driver_scan_params params;
5611 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005612 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005613 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005614 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005615
5616 os_memset(&params, 0, sizeof(params));
5617
5618 /* P2P Wildcard SSID */
5619 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005620 if (ssid && ssid_len) {
5621 params.ssids[0].ssid = ssid;
5622 params.ssids[0].ssid_len = ssid_len;
5623 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5624 wpa_s->p2p_join_ssid_len = ssid_len;
5625 } else {
5626 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5627 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5628 wpa_s->p2p_join_ssid_len = 0;
5629 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630
5631 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005632 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5633 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5634 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 if (wps_ie == NULL) {
5636 wpas_p2p_scan_res_join(wpa_s, NULL);
5637 return;
5638 }
5639
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005640 if (!freq) {
5641 int oper_freq;
5642 /*
5643 * If freq is not provided, check the operating freq of the GO
5644 * and use a single channel scan on if possible.
5645 */
5646 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5647 wpa_s->pending_join_iface_addr);
5648 if (oper_freq > 0)
5649 freq = oper_freq;
5650 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005651 if (freq > 0) {
5652 freqs[0] = freq;
5653 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005654 } else {
5655 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005657
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005658 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5659 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5660 if (ies == NULL) {
5661 wpabuf_free(wps_ie);
5662 wpas_p2p_scan_res_join(wpa_s, NULL);
5663 return;
5664 }
5665 wpabuf_put_buf(ies, wps_ie);
5666 wpabuf_free(wps_ie);
5667
5668 bands = wpas_get_bands(wpa_s, freqs);
5669 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5670
5671 params.p2p_probe = 1;
5672 params.extra_ies = wpabuf_head(ies);
5673 params.extra_ies_len = wpabuf_len(ies);
5674
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005675 if (wpa_s->clear_driver_scan_cache) {
5676 wpa_printf(MSG_DEBUG,
5677 "Request driver to clear scan cache due to local BSS flush");
5678 params.only_new_results = 1;
5679 }
5680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 /*
5682 * Run a scan to update BSS table and start Provision Discovery once
5683 * the new scan results become available.
5684 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005685 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005686 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005687 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005688 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005689 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005690 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005691 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005692 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005693 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005694
5695 wpabuf_free(ies);
5696
5697 if (ret) {
5698 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5699 "try again later");
5700 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5701 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5702 wpas_p2p_check_join_scan_limit(wpa_s);
5703 }
5704}
5705
5706
Dmitry Shmidt04949592012-07-19 12:16:46 -07005707static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5708{
5709 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005710 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005711}
5712
5713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005714static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005715 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005716 int auto_join, int op_freq,
5717 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718{
5719 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005720 MACSTR " dev " MACSTR " op_freq=%d)%s",
5721 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005722 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005723 if (ssid && ssid_len) {
5724 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5725 wpa_ssid_txt(ssid, ssid_len));
5726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727
Dmitry Shmidt04949592012-07-19 12:16:46 -07005728 wpa_s->p2p_auto_pd = 0;
5729 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5731 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5732 wpa_s->pending_join_wps_method = wps_method;
5733
5734 /* Make sure we are not running find during connection establishment */
5735 wpas_p2p_stop_find(wpa_s);
5736
5737 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005738 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005739 return 0;
5740}
5741
5742
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005743static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5744 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745{
5746 struct wpa_supplicant *group;
5747 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005748 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749
5750 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5751 if (group == NULL)
5752 return -1;
5753 if (group != wpa_s) {
5754 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5755 sizeof(group->p2p_pin));
5756 group->p2p_wps_method = wpa_s->p2p_wps_method;
5757 }
5758
Hai Shalom74f70d42019-02-11 14:42:39 -08005759 /*
5760 * Need to mark the current interface for p2p_group_formation
5761 * when a separate group interface is not used. This is needed
5762 * to allow p2p_cancel stop a pending p2p_connect-join.
5763 * wpas_p2p_init_group_interface() addresses this for the case
5764 * where a separate group interface is used.
5765 */
5766 if (group == wpa_s->parent)
5767 wpa_s->global->p2p_group_formation = group;
5768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005769 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005770 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771
5772 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005773 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5775 ETH_ALEN);
5776 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005777 if (freq && ssid && ssid_len) {
5778 res.freq = freq;
5779 res.ssid_len = ssid_len;
5780 os_memcpy(res.ssid, ssid, ssid_len);
5781 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005782 if (ssid && ssid_len) {
5783 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5784 ssid, ssid_len);
5785 } else {
5786 bss = wpa_bss_get_bssid_latest(
5787 wpa_s, wpa_s->pending_join_iface_addr);
5788 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005789 if (bss) {
5790 res.freq = bss->freq;
5791 res.ssid_len = bss->ssid_len;
5792 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5793 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5794 bss->freq,
5795 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005796 } else if (ssid && ssid_len) {
5797 res.ssid_len = ssid_len;
5798 os_memcpy(res.ssid, ssid, ssid_len);
5799 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5800 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005801 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005802 }
5803
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005804 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5805 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5806 "starting client");
5807 wpa_drv_cancel_remain_on_channel(wpa_s);
5808 wpa_s->off_channel_freq = 0;
5809 wpa_s->roc_waiting_drv_freq = 0;
5810 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005811 wpas_start_wps_enrollee(group, &res);
5812
5813 /*
5814 * Allow a longer timeout for join-a-running-group than normal 15
5815 * second group formation timeout since the GO may not have authorized
5816 * our connection yet.
5817 */
5818 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5819 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5820 wpa_s, NULL);
5821
5822 return 0;
5823}
5824
5825
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005826static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005827 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005828 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005829 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005830{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005831 struct wpa_used_freq_data *freqs;
5832 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005833 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305834 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005835
5836 max_pref_freq = *num_pref_freq;
5837 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005838
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005839 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5840 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005841 if (!freqs)
5842 return -1;
5843
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005844 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5845 wpa_s->num_multichan_concurrent);
5846
5847 /*
5848 * It is possible that the total number of used frequencies is bigger
5849 * than the number of frequencies used for P2P, so get the system wide
5850 * number of unused frequencies.
5851 */
5852 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5853
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005854 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005855 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5856 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005857
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005858 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005859 int ret;
5860 if (go)
5861 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5862 else
5863 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5864 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005865 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005866 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5867 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005868 /*
5869 * If freq is a DFS channel and DFS is offloaded
5870 * to the driver, allow P2P GO to use it.
5871 */
5872 wpa_printf(MSG_DEBUG,
5873 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5874 freq);
5875 } else {
5876 wpa_printf(MSG_DEBUG,
5877 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5878 freq);
5879 res = -3;
5880 goto exit_free;
5881 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005882 }
5883
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005884 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005885 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005886 freq_in_use = 1;
5887 }
5888
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005889 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005890 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5891 freq);
5892 res = -2;
5893 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005894 }
5895 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5896 "requested channel (%u MHz)", freq);
5897 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005898 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005899 }
5900
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005901 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005902
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305903 if (*pref_freq == 0) {
5904 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005905 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305906 while (i < wpa_s->conf->num_p2p_pref_chan) {
5907 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5908 wpa_s->conf->p2p_pref_chan[i].op_class,
5909 wpa_s->conf->p2p_pref_chan[i].chan);
5910
5911 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5912 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5913 best_freq = p2p_pref_freq;
5914 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5915 "from P2P preferred channel list", best_freq);
5916 break;
5917 } else {
5918 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5919 "frequency (%u MHz) ", p2p_pref_freq);
5920 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005921 i++;
5922 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305923 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305924 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07005925 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305926
Sunil Ravi036cec52023-03-29 11:35:17 -07005927 *num_pref_freq = max_pref_freq;
5928 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
5929 num_pref_freq);
5930 if (res > 0)
5931 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005932 }
5933 }
5934
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005935 /* We have a candidate frequency to use */
5936 if (best_freq > 0) {
5937 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005938 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005939 best_freq);
5940 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005941 } else {
5942 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 -07005943 best_freq);
5944 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005945 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005946 } else if (num_unused > 0) {
5947 wpa_printf(MSG_DEBUG,
5948 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5949 *force_freq = 0;
5950 } else {
5951 wpa_printf(MSG_DEBUG,
5952 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5953 res = -2;
5954 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005955 }
5956
5957exit_ok:
5958 res = 0;
5959exit_free:
5960 os_free(freqs);
5961 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005962}
5963
5964
Hai Shalomc1a21442022-02-04 13:43:00 -08005965static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5966 const u8 *peer_addr)
5967{
5968 if (wpa_s->conf->p2p_6ghz_disable ||
5969 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5970 HOSTAPD_MODE_IEEE80211A, true))
5971 return false;
5972
5973 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5974 return false;
5975 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5976 return false;
5977
5978 return true;
5979}
5980
5981
5982static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5983 const u8 *peer_addr, bool allow_6ghz, int freq)
5984{
5985 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5986 wpa_printf(MSG_DEBUG,
5987 "P2P: Allow connection on 6 GHz channels");
5988 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5989 } else {
5990 if (is_6ghz_freq(freq))
5991 return -2;
5992 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5993 }
5994
5995 return 0;
5996}
5997
5998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005999/**
6000 * wpas_p2p_connect - Request P2P Group Formation to be started
6001 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6002 * @peer_addr: Address of the peer P2P Device
6003 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6004 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07006005 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006006 * @join: Whether to join an existing group (as a client) instead of starting
6007 * Group Owner negotiation; @peer_addr is BSSID in that case
6008 * @auth: Whether to only authorize the connection instead of doing that and
6009 * initiating Group Owner negotiation
6010 * @go_intent: GO Intent or -1 to use default
6011 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006012 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006013 * @persistent_id: Persistent group credentials to use for forcing GO
6014 * parameters or -1 to generate new values (SSID/passphrase)
6015 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6016 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006017 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006018 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006019 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006020 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006021 * @group_ssid: Specific Group SSID for join or %NULL if not set
6022 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006023 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006024 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6025 * failure, -2 on failure due to channel not currently available,
6026 * -3 if forced channel is not supported
6027 */
6028int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6029 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006030 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006031 int go_intent, int freq, unsigned int vht_center_freq2,
6032 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006033 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006034 const u8 *group_ssid, size_t group_ssid_len,
6035 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006037 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006038 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006039 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006040 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006041 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006042 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6043 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044
6045 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6046 return -1;
6047
Dmitry Shmidt04949592012-07-19 12:16:46 -07006048 if (persistent_id >= 0) {
6049 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6050 if (ssid == NULL || ssid->disabled != 2 ||
6051 ssid->mode != WPAS_MODE_P2P_GO)
6052 return -1;
6053 }
6054
Hai Shalomc1a21442022-02-04 13:43:00 -08006055 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006056 return -2;
6057
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006058 os_free(wpa_s->global->add_psk);
6059 wpa_s->global->add_psk = NULL;
6060
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006061 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006062 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006063 wpa_s->global->pending_p2ps_group_freq = 0;
6064 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066 if (go_intent < 0)
6067 go_intent = wpa_s->conf->p2p_go_intent;
6068
6069 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006070 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006071
6072 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006073 wpa_s->p2p_persistent_group = !!persistent_group;
6074 wpa_s->p2p_persistent_id = persistent_id;
6075 wpa_s->p2p_go_intent = go_intent;
6076 wpa_s->p2p_connect_freq = freq;
6077 wpa_s->p2p_fallback_to_go_neg = 0;
6078 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006079 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006080 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006081 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6082 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006083 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006084 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006085
6086 if (pin)
6087 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6088 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006089 if (wps_generate_pin((unsigned int *) &ret) < 0)
6090 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006091 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6092 "%08d", ret);
6093 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6094 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006095 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6096 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006097 } else if (wps_method == WPS_P2PS) {
6098 /* Force the P2Ps default PIN to be used */
6099 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006100 } else
6101 wpa_s->p2p_pin[0] = '\0';
6102
Dmitry Shmidt04949592012-07-19 12:16:46 -07006103 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006104 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6105 if (auth) {
6106 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6107 "connect a running group from " MACSTR,
6108 MAC2STR(peer_addr));
6109 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6110 return ret;
6111 }
6112 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6113 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6114 iface_addr) < 0) {
6115 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6116 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6117 dev_addr);
6118 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006119 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006120 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006121 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6122 "%ld.%06ld",
6123 wpa_s->p2p_auto_started.sec,
6124 wpa_s->p2p_auto_started.usec);
6125 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006126 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006127 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006128 auto_join, freq,
6129 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 return -1;
6131 return ret;
6132 }
6133
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006134 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006135 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006136 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006137 if (res)
6138 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006139 wpas_p2p_set_own_freq_preference(wpa_s,
6140 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006141
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006142 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006144 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006146 if (wpa_s->create_p2p_iface) {
6147 /* Prepare to add a new interface for the group */
6148 iftype = WPA_IF_P2P_GROUP;
6149 if (go_intent == 15)
6150 iftype = WPA_IF_P2P_GO;
6151 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6152 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6153 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006156
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006157 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006158 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006159 if (wpa_s->p2p_mgmt)
6160 if_addr = wpa_s->parent->own_addr;
6161 else
6162 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006163 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006165
6166 if (auth) {
6167 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006168 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006169 force_freq, persistent_group, ssid,
6170 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006171 return -1;
6172 return ret;
6173 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006174
6175 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6176 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006177 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006178 if (wpa_s->create_p2p_iface)
6179 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180 return -1;
6181 }
6182 return ret;
6183}
6184
6185
6186/**
6187 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6188 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6189 * @freq: Frequency of the channel in MHz
6190 * @duration: Duration of the stay on the channel in milliseconds
6191 *
6192 * This callback is called when the driver indicates that it has started the
6193 * requested remain-on-channel duration.
6194 */
6195void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6196 unsigned int freq, unsigned int duration)
6197{
6198 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6199 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006200 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)",
6201 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6202 wpa_s->roc_waiting_drv_freq, freq, duration);
6203 if (wpa_s->off_channel_freq &&
6204 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6206 wpa_s->pending_listen_duration);
6207 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006208 } else {
6209 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6210 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6211 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 }
6213}
6214
6215
Jithu Jance57cea1a2014-08-20 10:22:04 -07006216int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217{
6218 /* Limit maximum Listen state time based on driver limitation. */
6219 if (timeout > wpa_s->max_remain_on_chan)
6220 timeout = wpa_s->max_remain_on_chan;
6221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222 return p2p_listen(wpa_s->global->p2p, timeout);
6223}
6224
6225
6226/**
6227 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6228 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6229 * @freq: Frequency of the channel in MHz
6230 *
6231 * This callback is called when the driver indicates that a remain-on-channel
6232 * operation has been completed, i.e., the duration on the requested channel
6233 * has timed out.
6234 */
6235void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6236 unsigned int freq)
6237{
6238 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6239 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006240 wpa_s->global->p2p_long_listen,
6241 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006242 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006243 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6244 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006245 if (wpa_s->global->p2p_long_listen > 0)
6246 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006247 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6248 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006249 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006251 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006253 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006254 } else {
6255 /*
6256 * When listen duration is over, stop listen & update p2p_state
6257 * to IDLE.
6258 */
6259 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006260 }
6261}
6262
6263
6264/**
6265 * wpas_p2p_group_remove - Remove a P2P group
6266 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6267 * @ifname: Network interface name of the group interface or "*" to remove all
6268 * groups
6269 * Returns: 0 on success, -1 on failure
6270 *
6271 * This function is used to remove a P2P group. This can be used to disconnect
6272 * from a group in which the local end is a P2P Client or to end a P2P Group in
6273 * case the local end is the Group Owner. If a virtual network interface was
6274 * created for this group, that interface will be removed. Otherwise, only the
6275 * configured P2P group network will be removed from the interface.
6276 */
6277int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6278{
6279 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006280 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281
6282 if (os_strcmp(ifname, "*") == 0) {
6283 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006284 bool calling_wpa_s_group_removed = false;
6285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006286 wpa_s = global->ifaces;
6287 while (wpa_s) {
6288 prev = wpa_s;
6289 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006290 if (prev->p2p_group_interface !=
6291 NOT_P2P_GROUP_INTERFACE ||
6292 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006293 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006294 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006295 if (prev == calling_wpa_s)
6296 calling_wpa_s_group_removed = true;
6297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006299
6300 if (!calling_wpa_s_group_removed &&
6301 (calling_wpa_s->p2p_group_interface !=
6302 NOT_P2P_GROUP_INTERFACE ||
6303 (calling_wpa_s->current_ssid &&
6304 calling_wpa_s->current_ssid->p2p_group))) {
6305 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6306 wpas_p2p_disconnect_safely(calling_wpa_s,
6307 calling_wpa_s);
6308 }
6309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006310 return 0;
6311 }
6312
6313 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6314 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6315 break;
6316 }
6317
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006318 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319}
6320
6321
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006322static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6323{
6324 unsigned int r;
6325
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006326 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6327 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006328 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006329 int res;
6330
6331 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6332 &size, pref_freq_list);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006333 if (!res && size > 0 && !is_p2p_allow_6ghz(wpa_s->global->p2p))
Hai Shalomc1a21442022-02-04 13:43:00 -08006334 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6335
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006336 if (!res && size > 0) {
6337 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006338 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006339 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006340 pref_freq_list[i].freq) ||
6341 wpas_p2p_disallowed_freq(
6342 wpa_s->global,
6343 pref_freq_list[i].freq) ||
6344 !p2p_pref_freq_allowed(&pref_freq_list[i],
6345 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006346 wpa_printf(MSG_DEBUG,
6347 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006348 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006349 i++;
6350 }
6351 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006352 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006353 wpa_printf(MSG_DEBUG,
6354 "P2P: Using preferred_freq_list[%d]=%d",
6355 i, freq);
6356 } else {
6357 wpa_printf(MSG_DEBUG,
6358 "P2P: All driver preferred frequencies are disallowed for P2P use");
6359 }
6360 } else {
6361 wpa_printf(MSG_DEBUG,
6362 "P2P: No preferred frequency list available");
6363 }
6364 }
6365
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006366 if (freq == 2) {
6367 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6368 "band");
6369 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006370 p2p_supported_freq_go(wpa_s->global->p2p,
6371 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006372 freq = wpa_s->best_24_freq;
6373 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6374 "channel: %d MHz", freq);
6375 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006376 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6377 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006378 int possible_2g_freqs[] = {
6379 /* operating class 81 */
6380 2412, 2437, 2462,
6381 };
6382 int possible_2g_freqs_num =
6383 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6384 int i;
6385 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6386 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6387 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6388 break;
6389 }
6390 }
6391
6392 if (i >= possible_2g_freqs_num) {
6393 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6394 "2.4 GHz channel for P2P group");
6395 return -1;
6396 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006397 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6398 "channel: %d MHz", freq);
6399 }
6400 }
6401
6402 if (freq == 5) {
6403 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6404 "band");
6405 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006406 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006407 wpa_s->best_5_freq)) {
6408 freq = wpa_s->best_5_freq;
6409 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6410 "channel: %d MHz", freq);
6411 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006412 const int freqs[] = {
6413 /* operating class 115 */
6414 5180, 5200, 5220, 5240,
6415 /* operating class 124 */
6416 5745, 5765, 5785, 5805,
6417 };
6418 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6419
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006420 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6421 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006422
6423 /*
6424 * most of 5G channels are DFS, only operating class 115 and 124
6425 * are available possibly, randomly pick a start to check them.
6426 */
6427 int possible_5g_freqs[] = {
6428 /* operating class 115 */
6429 5180, 5200, 5220, 5240,
6430 /* operating class 124 */
6431 5745, 5765, 5785, 5805,
6432 };
6433 int possible_5g_freqs_num =
6434 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6435
Jimmy Chen64b90632020-09-29 17:27:34 +08006436 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6437 if (p2p_supported_freq_go(
6438 wpa_s->global->p2p,
6439 possible_5g_freqs[r % possible_5g_freqs_num])) {
6440 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6441 break;
6442 }
6443 }
6444
6445 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006446 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6447 "5 GHz channel for P2P group");
6448 return -1;
6449 }
6450 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6451 "channel: %d MHz", freq);
6452 }
6453 }
6454
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006455 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006456 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006457 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6458 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006459 /*
6460 * If freq is a DFS channel and DFS is offloaded to the
6461 * driver, allow P2P GO to use it.
6462 */
6463 wpa_printf(MSG_DEBUG, "P2P: "
6464 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6465 __func__, freq);
6466 return freq;
6467 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006468 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6469 "(%u MHz) is not supported for P2P uses",
6470 freq);
6471 return -1;
6472 }
6473
6474 return freq;
6475}
6476
6477
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006478static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6479 const struct p2p_channels *channels,
6480 int freq)
6481{
6482 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6483 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6484 freq_included(wpa_s, channels, freq))
6485 return 1;
6486 return 0;
6487}
6488
6489
6490static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6491 struct p2p_go_neg_results *params,
6492 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006493{
6494 unsigned int i, r;
6495
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006496 /* try all channels in operating class 115 */
6497 for (i = 0; i < 4; i++) {
6498 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006499 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006500 goto out;
6501 }
6502
6503 /* try all channels in operating class 124 */
6504 for (i = 0; i < 4; i++) {
6505 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006506 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006507 goto out;
6508 }
6509
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006510 /* try social channel class 180 channel 2 */
6511 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006512 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006513 goto out;
6514
6515 /* try all channels in reg. class 180 */
6516 for (i = 0; i < 4; i++) {
6517 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006518 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006519 goto out;
6520 }
6521
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006522 /* try some random selection of the social channels */
6523 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6524 return;
6525
6526 for (i = 0; i < 3; i++) {
6527 params->freq = 2412 + ((r + i) % 3) * 25;
6528 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6529 goto out;
6530 }
6531
6532 /* try all other channels in operating class 81 */
6533 for (i = 0; i < 11; i++) {
6534 params->freq = 2412 + i * 5;
6535
6536 /* skip social channels; covered in the previous loop */
6537 if (params->freq == 2412 ||
6538 params->freq == 2437 ||
6539 params->freq == 2462)
6540 continue;
6541
6542 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6543 goto out;
6544 }
6545
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006546 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006547 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006548 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006549out:
6550 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6551 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006552}
6553
6554
Roshan Pius3a1667e2018-07-03 15:17:14 -07006555static int wpas_same_band(int freq1, int freq2)
6556{
6557 enum hostapd_hw_mode mode1, mode2;
6558 u8 chan1, chan2;
6559
6560 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6561 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6562 if (mode1 == NUM_HOSTAPD_MODES)
6563 return 0;
6564 return mode1 == mode2;
6565}
6566
6567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006568static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6569 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006570 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006571 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006572 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006573 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006575 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006576 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006577 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006578 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006579 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006580
6581 os_memset(params, 0, sizeof(*params));
6582 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006583 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006584 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006585 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006586 params->max_oper_chwidth = max_oper_chwidth;
6587 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006588 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006589
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006590 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6591 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006592 if (!freqs)
6593 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006594
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006595 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006596 wpa_s->num_multichan_concurrent,
6597 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006598
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006599 if (wpa_s->current_ssid &&
6600 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6601 wpa_s->wpa_state == WPA_COMPLETED) {
6602 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6603 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006604
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006605 /*
6606 * If the frequency selection is done for an active P2P GO that
6607 * is not sharing a frequency, allow to select a new frequency
6608 * even if there are no unused frequencies as we are about to
6609 * move the P2P GO so its frequency can be re-used.
6610 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006611 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006612 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6613 freqs[i].flags == 0) {
6614 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006615 break;
6616 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006617 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006618 }
6619
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006620 /* Try to use EDMG channel */
6621 if (params->edmg) {
6622 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6623 goto success;
6624 params->edmg = 0;
6625 }
6626
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006627 /* try using the forced freq */
6628 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006629 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6630 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006631 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006632 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006633 freq);
6634 goto fail;
6635 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006636 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6637 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006638 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6639 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006640 /*
6641 * If freq is a DFS channel and DFS is offloaded
6642 * to the driver, allow P2P GO to use it.
6643 */
6644 wpa_printf(MSG_DEBUG,
6645 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6646 __func__, freq);
6647 } else {
6648 wpa_printf(MSG_DEBUG,
6649 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6650 freq);
6651 goto fail;
6652 }
6653 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006654
6655 for (i = 0; i < num; i++) {
6656 if (freqs[i].freq == freq) {
6657 wpa_printf(MSG_DEBUG,
6658 "P2P: forced freq (%d MHz) is also shared",
6659 freq);
6660 params->freq = freq;
6661 goto success;
6662 }
6663 }
6664
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006665 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006666 wpa_printf(MSG_DEBUG,
6667 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6668 freq);
6669 goto fail;
6670 }
6671
6672 wpa_printf(MSG_DEBUG,
6673 "P2P: force GO freq (%d MHz) on a free channel",
6674 freq);
6675 params->freq = freq;
6676 goto success;
6677 }
6678
6679 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006680 if (num &&
6681 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006682 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6683 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6684 wpa_printf(MSG_DEBUG,
6685 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006686 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006687 params->freq = cand;
6688 goto success;
6689 }
6690
6691 /* try using one of the shared freqs */
6692 for (i = 0; i < num; i++) {
6693 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6694 freqs[i].freq)) {
6695 wpa_printf(MSG_DEBUG,
6696 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006697 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006698 params->freq = freqs[i].freq;
6699 goto success;
6700 }
6701 }
6702 }
6703
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006704 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006705 wpa_printf(MSG_DEBUG,
6706 "P2P: Cannot force GO on any of the channels we are already using");
6707 goto fail;
6708 }
6709
6710 /* try using the setting from the configuration file */
6711 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6712 wpa_s->conf->p2p_oper_channel >= 1 &&
6713 wpa_s->conf->p2p_oper_channel <= 11 &&
6714 wpas_p2p_supported_freq_go(
6715 wpa_s, channels,
6716 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6717 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6718 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6719 "frequency %d MHz", params->freq);
6720 goto success;
6721 }
6722
6723 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6724 wpa_s->conf->p2p_oper_reg_class == 116 ||
6725 wpa_s->conf->p2p_oper_reg_class == 117 ||
6726 wpa_s->conf->p2p_oper_reg_class == 124 ||
6727 wpa_s->conf->p2p_oper_reg_class == 125 ||
6728 wpa_s->conf->p2p_oper_reg_class == 126 ||
6729 wpa_s->conf->p2p_oper_reg_class == 127) &&
6730 wpas_p2p_supported_freq_go(wpa_s, channels,
6731 5000 +
6732 5 * wpa_s->conf->p2p_oper_channel)) {
6733 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6734 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6735 "frequency %d MHz", params->freq);
6736 goto success;
6737 }
6738
6739 /* Try using best channels */
6740 if (wpa_s->conf->p2p_oper_channel == 0 &&
6741 wpa_s->best_overall_freq > 0 &&
6742 wpas_p2p_supported_freq_go(wpa_s, channels,
6743 wpa_s->best_overall_freq)) {
6744 params->freq = wpa_s->best_overall_freq;
6745 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6746 "channel %d MHz", params->freq);
6747 goto success;
6748 }
6749
6750 if (wpa_s->conf->p2p_oper_channel == 0 &&
6751 wpa_s->best_24_freq > 0 &&
6752 wpas_p2p_supported_freq_go(wpa_s, channels,
6753 wpa_s->best_24_freq)) {
6754 params->freq = wpa_s->best_24_freq;
6755 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6756 "channel %d MHz", params->freq);
6757 goto success;
6758 }
6759
6760 if (wpa_s->conf->p2p_oper_channel == 0 &&
6761 wpa_s->best_5_freq > 0 &&
6762 wpas_p2p_supported_freq_go(wpa_s, channels,
6763 wpa_s->best_5_freq)) {
6764 params->freq = wpa_s->best_5_freq;
6765 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6766 "channel %d MHz", params->freq);
6767 goto success;
6768 }
6769
6770 /* try using preferred channels */
6771 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6772 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6773 params->freq = cand;
6774 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6775 "channels", params->freq);
6776 goto success;
6777 }
6778
Roshan Pius3a1667e2018-07-03 15:17:14 -07006779 /* Try using a channel that allows VHT to be used with 80 MHz */
6780 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6781 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6782 enum hostapd_hw_mode mode;
6783 struct hostapd_hw_modes *hwmode;
6784 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006785 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006786
6787 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006788 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006789 mode = ieee80211_freq_to_chan(cand, &chan);
6790 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006791 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006792 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006793 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6794 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006795 continue;
6796 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6797 params->freq = cand;
6798 wpa_printf(MSG_DEBUG,
6799 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6800 params->freq);
6801 goto success;
6802 }
6803 }
6804 }
6805
6806 /* Try using a channel that allows HT to be used with 40 MHz on the same
6807 * band so that CSA can be used */
6808 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6809 wpa_s->p2p_group_common_freqs) {
6810 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6811 enum hostapd_hw_mode mode;
6812 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006813 u8 chan, op_class;
6814 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006815
Hai Shalomc1a21442022-02-04 13:43:00 -08006816 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006817 cand = wpa_s->p2p_group_common_freqs[i];
6818 mode = ieee80211_freq_to_chan(cand, &chan);
6819 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006820 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006821 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6822 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006823 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006824 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006825 if (is_6ghz &&
6826 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6827 BW40) == ALLOWED)
6828 supported = true;
6829
6830 if (!is_6ghz &&
6831 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006832 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6833 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006834 wpas_p2p_verify_channel(
6835 wpa_s, hwmode, op_class, chan,
6836 BW40MINUS) == ALLOWED)
6837 supported = true;
6838
6839 if (!supported && !is_6ghz &&
6840 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006841 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6842 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006843 wpas_p2p_verify_channel(
6844 wpa_s, hwmode, op_class, chan,
6845 BW40PLUS) == ALLOWED)
6846 supported = true;
6847
6848 if (!supported)
6849 continue;
6850
Roshan Pius3a1667e2018-07-03 15:17:14 -07006851 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6852 params->freq = cand;
6853 wpa_printf(MSG_DEBUG,
6854 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6855 params->freq);
6856 goto success;
6857 }
6858 }
6859 }
6860
6861 /* Try using one of the group common freqs on the same band so that CSA
6862 * can be used */
6863 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6864 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6865 cand = wpa_s->p2p_group_common_freqs[i];
6866 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6867 cand))
6868 continue;
6869 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6870 params->freq = cand;
6871 wpa_printf(MSG_DEBUG,
6872 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6873 params->freq);
6874 goto success;
6875 }
6876 }
6877 }
6878
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006879 /* Try using one of the group common freqs */
6880 if (wpa_s->p2p_group_common_freqs) {
6881 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6882 cand = wpa_s->p2p_group_common_freqs[i];
6883 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6884 params->freq = cand;
6885 wpa_printf(MSG_DEBUG,
6886 "P2P: Use freq %d MHz common with the peer",
6887 params->freq);
6888 goto success;
6889 }
6890 }
6891 }
6892
6893 /* no preference, select some channel */
6894 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6895
6896 if (params->freq == 0) {
6897 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6898 goto fail;
6899 }
6900
6901success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006902 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006903 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006904fail:
6905 os_free(freqs);
6906 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907}
6908
6909
6910static struct wpa_supplicant *
6911wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6912 int go)
6913{
6914 struct wpa_supplicant *group_wpa_s;
6915
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006916 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006917 if (wpa_s->p2p_mgmt) {
6918 /*
6919 * We may be called on the p2p_dev interface which
6920 * cannot be used for group operations, so always use
6921 * the primary interface.
6922 */
6923 wpa_s->parent->p2pdev = wpa_s;
6924 wpa_s = wpa_s->parent;
6925 }
6926 wpa_dbg(wpa_s, MSG_DEBUG,
6927 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006928 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006929 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006930 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933
6934 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006935 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006936 wpa_msg_global(wpa_s, MSG_ERROR,
6937 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006939 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006940 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6941 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006942 wpa_msg_global(wpa_s, MSG_ERROR,
6943 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006944 wpas_p2p_remove_pending_group_interface(wpa_s);
6945 return NULL;
6946 }
6947
Roshan Pius3a1667e2018-07-03 15:17:14 -07006948 if (go && wpa_s->p2p_go_do_acs) {
6949 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6950 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6951 wpa_s->p2p_go_do_acs = 0;
6952 }
6953
Hai Shalomc1a21442022-02-04 13:43:00 -08006954 if (go && wpa_s->p2p_go_allow_dfs) {
6955 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6956 wpa_s->p2p_go_allow_dfs = 0;
6957 }
6958
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6960 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006961 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962 return group_wpa_s;
6963}
6964
6965
6966/**
6967 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6968 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6969 * @persistent_group: Whether to create a persistent group
6970 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006971 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006972 * @ht40: Start GO with 40 MHz channel width
6973 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006974 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006975 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006976 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006977 * Returns: 0 on success, -1 on failure
6978 *
6979 * This function creates a new P2P group with the local end as the Group Owner,
6980 * i.e., without using Group Owner Negotiation.
6981 */
6982int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006983 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006984 int max_oper_chwidth, int he, int edmg,
6985 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986{
6987 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006988 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006989
6990 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6991 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006992 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6993 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006994
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006995 os_free(wpa_s->global->add_psk);
6996 wpa_s->global->add_psk = NULL;
6997
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006998 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006999 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007000 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07007001
Roshan Pius3a1667e2018-07-03 15:17:14 -07007002 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00007003 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
7004 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007005 return -1;
7006 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007007
Sunil Ravi99c035e2024-07-12 01:42:03 +00007008 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq,
7009 vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007010 ht40, vht, max_oper_chwidth, he, edmg,
7011 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007012 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007014 p2p_go_params(wpa_s->global->p2p, &params);
7015 params.persistent_group = persistent_group;
7016
7017 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7018 if (wpa_s == NULL)
7019 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007020 if (freq > 0)
7021 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007022 wpas_start_wps_go(wpa_s, &params, 0);
7023
7024 return 0;
7025}
7026
7027
7028static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007029 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007030 int freq, int force_scan, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007031 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007032{
7033 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007034 int other_iface_found = 0;
7035 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036
7037 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7038 if (wpa_s == NULL)
7039 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007040 if (force_scan)
7041 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007042 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043
7044 wpa_supplicant_ap_deinit(wpa_s);
7045
7046 ssid = wpa_config_add_network(wpa_s->conf);
7047 if (ssid == NULL)
7048 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007049 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050 wpa_config_set_network_defaults(ssid);
7051 ssid->temporary = 1;
7052 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007053 ssid->pbss = params->pbss;
7054 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7055 WPA_CIPHER_CCMP;
7056 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007058 if (is_6ghz_freq(freq) &&
7059 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7060 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7061 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7062 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7063 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7064 wpa_dbg(wpa_s, MSG_DEBUG,
7065 "P2P: Enable SAE auth_alg and key_mgmt");
7066 }
7067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007068 ssid->ssid = os_malloc(params->ssid_len);
7069 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 wpa_config_remove_network(wpa_s->conf, ssid->id);
7071 return -1;
7072 }
7073 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7074 ssid->ssid_len = params->ssid_len;
7075 ssid->p2p_group = 1;
7076 ssid->export_keys = 1;
7077 if (params->psk_set) {
7078 os_memcpy(ssid->psk, params->psk, 32);
7079 ssid->psk_set = 1;
7080 }
7081 if (params->passphrase)
7082 ssid->passphrase = os_strdup(params->passphrase);
7083
Sunil Ravi036cec52023-03-29 11:35:17 -07007084 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007085 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007086 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007087 }
7088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007089 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007090 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007091 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007092 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007093 wpa_s->p2p_go_group_formation_completed = 0;
7094 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007095
Matthew Wang9ae940b2022-09-14 21:25:34 -07007096 /*
7097 * Get latest scan results from driver in case cached scan results from
7098 * interfaces on the same wiphy allow us to skip the next scan by fast
7099 * associating. Also update the scan time to the most recent scan result
7100 * fetch time on the same radio so it reflects the actual time the last
7101 * scan result event occurred.
7102 */
Sunil Ravi99c035e2024-07-12 01:42:03 +00007103 wpa_supplicant_update_scan_results(wpa_s, go_bssid);
Matthew Wang9ae940b2022-09-14 21:25:34 -07007104 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7105 radio_list) {
7106 if (ifs == wpa_s)
7107 continue;
7108 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7109 &ifs->last_scan)) {
7110 other_iface_found = 1;
7111 wpa_s->last_scan.sec = ifs->last_scan.sec;
7112 wpa_s->last_scan.usec = ifs->last_scan.usec;
7113 }
7114 }
7115
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007116 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007117 NULL);
7118 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7119 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007120 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007121 wpa_supplicant_select_network(wpa_s, ssid);
7122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007123 return 0;
7124}
7125
7126
7127int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7128 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007129 int force_freq, int neg_freq,
7130 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007131 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007132 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007133 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007134 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007135 bool allow_6ghz, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007136 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137{
7138 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007139 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140
7141 if (ssid->disabled != 2 || ssid->ssid == NULL)
7142 return -1;
7143
7144 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7145 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7146 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7147 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007148 if (go == 0 &&
7149 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007150 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007151 /*
7152 * This can happen if Invitation Response frame was lost
7153 * and the peer (GO of a persistent group) tries to
7154 * invite us again. Reschedule the timeout to avoid
7155 * terminating the wait for the connection too early
7156 * since we now know that the peer is still trying to
7157 * invite us instead of having already started the GO.
7158 */
7159 wpa_printf(MSG_DEBUG,
7160 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7161 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7162 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007163 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165 return 0;
7166 }
7167
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007168 os_free(wpa_s->global->add_psk);
7169 wpa_s->global->add_psk = NULL;
7170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007171 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007172 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173
Dmitry Shmidt04949592012-07-19 12:16:46 -07007174 wpa_s->p2p_fallback_to_go_neg = 0;
7175
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007176 if (ssid->mode == WPAS_MODE_P2P_GO) {
7177 if (force_freq > 0) {
7178 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7179 if (freq < 0)
7180 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007181 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007182 } else {
7183 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7184 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007185 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007186 freq = 0;
7187 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007188 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007189 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007190 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007191 struct os_reltime now;
7192 struct wpa_bss *bss =
7193 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007194
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007195 os_get_reltime(&now);
7196 if (bss &&
7197 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007198 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007199 freq = bss->freq;
7200 else
7201 freq = 0;
7202 }
7203
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007204 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi036cec52023-03-29 11:35:17 -07007205 force_scan, retry_limit, go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007206 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007207 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007208 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007209
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007210 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007211 ht40, vht, max_oper_chwidth, he, edmg,
7212 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007213 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007214
7215 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007216 params.psk_set = ssid->psk_set;
7217 if (params.psk_set)
7218 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007219 if (ssid->passphrase) {
7220 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7221 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7222 "persistent group");
7223 return -1;
7224 }
7225 os_strlcpy(params.passphrase, ssid->passphrase,
7226 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007228 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7229 params.ssid_len = ssid->ssid_len;
7230 params.persistent_group = 1;
7231
7232 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7233 if (wpa_s == NULL)
7234 return -1;
7235
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007236 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7237
Dmitry Shmidt56052862013-10-04 10:23:25 -07007238 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239 wpas_start_wps_go(wpa_s, &params, 0);
7240
7241 return 0;
7242}
7243
7244
7245static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7246 struct wpabuf *proberesp_ies)
7247{
7248 struct wpa_supplicant *wpa_s = ctx;
7249 if (wpa_s->ap_iface) {
7250 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007251 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7252 wpabuf_free(beacon_ies);
7253 wpabuf_free(proberesp_ies);
7254 return;
7255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007256 if (beacon_ies) {
7257 wpabuf_free(hapd->p2p_beacon_ie);
7258 hapd->p2p_beacon_ie = beacon_ies;
7259 }
7260 wpabuf_free(hapd->p2p_probe_resp_ie);
7261 hapd->p2p_probe_resp_ie = proberesp_ies;
7262 } else {
7263 wpabuf_free(beacon_ies);
7264 wpabuf_free(proberesp_ies);
7265 }
7266 wpa_supplicant_ap_update_beacon(wpa_s);
7267}
7268
7269
7270static void wpas_p2p_idle_update(void *ctx, int idle)
7271{
7272 struct wpa_supplicant *wpa_s = ctx;
7273 if (!wpa_s->ap_iface)
7274 return;
7275 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007276 if (idle) {
7277 if (wpa_s->global->p2p_fail_on_wps_complete &&
7278 wpa_s->p2p_in_provisioning) {
7279 wpas_p2p_grpform_fail_after_wps(wpa_s);
7280 return;
7281 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007282 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007283 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007284 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7285}
7286
7287
7288struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007289 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007290{
7291 struct p2p_group *group;
7292 struct p2p_group_config *cfg;
7293
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7295 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007296 return NULL;
7297
7298 cfg = os_zalloc(sizeof(*cfg));
7299 if (cfg == NULL)
7300 return NULL;
7301
Dmitry Shmidt04949592012-07-19 12:16:46 -07007302 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007303 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007304 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007305 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007306 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7307 if (wpa_s->max_stations &&
7308 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7309 cfg->max_clients = wpa_s->max_stations;
7310 else
7311 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007312 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7313 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007314 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007315 cfg->cb_ctx = wpa_s;
7316 cfg->ie_update = wpas_p2p_ie_update;
7317 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007318 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7319 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320
7321 group = p2p_group_init(wpa_s->global->p2p, cfg);
7322 if (group == NULL)
7323 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007324 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007325 p2p_group_notif_formation_done(group);
7326 wpa_s->p2p_group = group;
7327 return group;
7328}
7329
7330
7331void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7332 int registrar)
7333{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007334 struct wpa_ssid *ssid = wpa_s->current_ssid;
7335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007336 if (!wpa_s->p2p_in_provisioning) {
7337 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7338 "provisioning not in progress");
7339 return;
7340 }
7341
Dmitry Shmidt04949592012-07-19 12:16:46 -07007342 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7343 u8 go_dev_addr[ETH_ALEN];
7344 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7345 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7346 ssid->ssid_len);
7347 /* Clear any stored provisioning info */
7348 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7349 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007350
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007351 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007353 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007354 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7355 /*
7356 * Use a separate timeout for initial data connection to
7357 * complete to allow the group to be removed automatically if
7358 * something goes wrong in this step before the P2P group idle
7359 * timeout mechanism is taken into use.
7360 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007361 wpa_dbg(wpa_s, MSG_DEBUG,
7362 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7363 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007364 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7365 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007366 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007367 /* Complete group formation on successful data connection. */
7368 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007369 } else if (ssid) {
7370 /*
7371 * Use a separate timeout for initial data connection to
7372 * complete to allow the group to be removed automatically if
7373 * the client does not complete data connection successfully.
7374 */
7375 wpa_dbg(wpa_s, MSG_DEBUG,
7376 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7377 P2P_MAX_INITIAL_CONN_WAIT_GO);
7378 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7379 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007380 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007381 /*
7382 * Complete group formation on first successful data connection
7383 */
7384 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007385 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007386 if (wpa_s->global->p2p)
7387 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007388 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389}
7390
7391
Jouni Malinen75ecf522011-06-27 15:19:46 -07007392void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7393 struct wps_event_fail *fail)
7394{
7395 if (!wpa_s->p2p_in_provisioning) {
7396 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7397 "provisioning not in progress");
7398 return;
7399 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007400
7401 if (wpa_s->go_params) {
7402 p2p_clear_provisioning_info(
7403 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007404 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007405 }
7406
Jouni Malinen75ecf522011-06-27 15:19:46 -07007407 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007408
7409 if (wpa_s == wpa_s->global->p2p_group_formation) {
7410 /*
7411 * Allow some time for the failed WPS negotiation exchange to
7412 * complete, but remove the group since group formation cannot
7413 * succeed after provisioning failure.
7414 */
7415 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7416 wpa_s->global->p2p_fail_on_wps_complete = 1;
7417 eloop_deplete_timeout(0, 50000,
7418 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007419 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007420 }
7421}
7422
7423
7424int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7425{
7426 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7427 !wpa_s->p2p_in_provisioning)
7428 return 0;
7429
7430 wpas_p2p_grpform_fail_after_wps(wpa_s);
7431
7432 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007433}
7434
7435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007436int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007437 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007438 enum wpas_p2p_prov_disc_use use,
7439 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007440{
7441 u16 config_methods;
7442
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007443 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007444 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007445 wpa_s->p2p_fallback_to_go_neg = 0;
7446 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007447 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7448 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007449 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7450 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7451
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007452 wpa_printf(MSG_DEBUG,
7453 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7454 __func__, p2ps_prov->conncap,
7455 p2ps_prov->adv_id, p2ps_prov->conncap,
7456 p2ps_prov->status, p2ps_prov->info);
7457
7458 config_methods = 0;
7459 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007460 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007461 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7464 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007465 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007466 else {
7467 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007468 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007469 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007470 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007471
Dmitry Shmidt04949592012-07-19 12:16:46 -07007472 if (use == WPAS_P2P_PD_AUTO) {
7473 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7474 wpa_s->pending_pd_config_methods = config_methods;
7475 wpa_s->p2p_auto_pd = 1;
7476 wpa_s->p2p_auto_join = 0;
7477 wpa_s->pending_pd_before_join = 0;
7478 wpa_s->auto_pd_scan_retry = 0;
7479 wpas_p2p_stop_find(wpa_s);
7480 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007481 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007482 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7483 wpa_s->p2p_auto_started.sec,
7484 wpa_s->p2p_auto_started.usec);
7485 wpas_p2p_join_scan(wpa_s, NULL);
7486 return 0;
7487 }
7488
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007489 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7490 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007491 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007493
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007494 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007495 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007496 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497}
7498
7499
7500int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7501 char *end)
7502{
7503 return p2p_scan_result_text(ies, ies_len, buf, end);
7504}
7505
7506
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007507static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s,
7508 bool force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007509{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007510 if (!offchannel_pending_action_tx(wpa_s) && !force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511 return;
7512
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007513 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007514 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007515 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7516 wpa_s, NULL);
7517 offchannel_send_action_done(wpa_s);
7518 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007519
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007520 if (!offchannel_pending_action_tx(wpa_s))
7521 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7523 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007524 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007525}
7526
7527
7528int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7529 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007530 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007531 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007532 u8 seek_cnt, const char **seek_string, int freq,
7533 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007535 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07007536 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007537
Dmitry Shmidt04949592012-07-19 12:16:46 -07007538 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007539 wpa_s->p2p_in_provisioning) {
7540 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7541 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7542 " (P2P disabled)" : "",
7543 wpa_s->p2p_in_provisioning ?
7544 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007545 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007548 wpa_supplicant_cancel_sched_scan(wpa_s);
7549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007550 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007551 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007552 search_delay, seek_cnt, seek_string, freq,
7553 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007554}
7555
7556
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007557static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7558 struct wpa_scan_results *scan_res)
7559{
7560 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7561
7562 if (wpa_s->p2p_scan_work) {
7563 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7564 wpa_s->p2p_scan_work = NULL;
7565 radio_work_done(work);
7566 }
7567
7568 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7569 return;
7570
7571 /*
7572 * Indicate that results have been processed so that the P2P module can
7573 * continue pending tasks.
7574 */
Hai Shalom60840252021-02-19 19:02:11 -08007575 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007576}
7577
7578
7579static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007581 wpas_p2p_clear_pending_action_tx(wpa_s, true);
Hai Shalomfdcde762020-04-02 11:19:20 -07007582 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007583 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7584 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007585
7586 if (wpa_s->global->p2p)
7587 p2p_stop_find(wpa_s->global->p2p);
7588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007589 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7590 wpa_printf(MSG_DEBUG,
7591 "P2P: Do not consider the scan results after stop_find");
7592 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7593 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007594}
7595
7596
7597void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7598{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007599 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007600 if (!wpa_s->global->pending_group_iface_for_p2ps)
7601 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007602}
7603
7604
7605static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7606{
7607 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007608 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007609}
7610
7611
7612int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7613{
7614 int res;
7615
7616 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7617 return -1;
7618
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007619 if (wpa_s->p2p_lo_started) {
7620 wpa_printf(MSG_DEBUG,
7621 "P2P: Cannot start P2P listen, it is offloaded");
7622 return -1;
7623 }
7624
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007625 wpa_supplicant_cancel_sched_scan(wpa_s);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007626 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627
7628 if (timeout == 0) {
7629 /*
7630 * This is a request for unlimited Listen state. However, at
7631 * least for now, this is mapped to a Listen state for one
7632 * hour.
7633 */
7634 timeout = 3600;
7635 }
7636 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007637 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007639 /*
7640 * Stop previous find/listen operation to avoid trying to request a new
7641 * remain-on-channel operation while the driver is still running the
7642 * previous one.
7643 */
7644 if (wpa_s->global->p2p)
7645 p2p_stop_find(wpa_s->global->p2p);
7646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7648 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007649 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007650 eloop_register_timeout(timeout, 0,
7651 wpas_p2p_long_listen_timeout,
7652 wpa_s, NULL);
7653 }
7654
7655 return res;
7656}
7657
7658
7659int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7660 u8 *buf, size_t len, int p2p_group)
7661{
7662 struct wpabuf *p2p_ie;
7663 int ret;
7664
7665 if (wpa_s->global->p2p_disabled)
7666 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007667 /*
7668 * Advertize mandatory cross connection capability even on
7669 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7670 */
7671 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007672 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673 if (wpa_s->global->p2p == NULL)
7674 return -1;
7675 if (bss == NULL)
7676 return -1;
7677
7678 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7679 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7680 p2p_group, p2p_ie);
7681 wpabuf_free(p2p_ie);
7682
7683 return ret;
7684}
7685
7686
7687int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007688 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007689 const u8 *ie, size_t ie_len,
7690 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007691{
7692 if (wpa_s->global->p2p_disabled)
7693 return 0;
7694 if (wpa_s->global->p2p == NULL)
7695 return 0;
7696
Dmitry Shmidt04949592012-07-19 12:16:46 -07007697 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007698 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007699 case P2P_PREQ_NOT_P2P:
7700 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7701 ssi_signal);
7702 /* fall through */
7703 case P2P_PREQ_MALFORMED:
7704 case P2P_PREQ_NOT_LISTEN:
7705 case P2P_PREQ_NOT_PROCESSED:
7706 default: /* make gcc happy */
7707 return 0;
7708 case P2P_PREQ_PROCESSED:
7709 return 1;
7710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007711}
7712
7713
7714void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7715 const u8 *sa, const u8 *bssid,
7716 u8 category, const u8 *data, size_t len, int freq)
7717{
7718 if (wpa_s->global->p2p_disabled)
7719 return;
7720 if (wpa_s->global->p2p == NULL)
7721 return;
7722
7723 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7724 freq);
7725}
7726
7727
7728void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7729{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007730 unsigned int bands;
7731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007732 if (wpa_s->global->p2p_disabled)
7733 return;
7734 if (wpa_s->global->p2p == NULL)
7735 return;
7736
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007737 bands = wpas_get_bands(wpa_s, NULL);
7738 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007739}
7740
7741
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007742static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007743{
7744 p2p_group_deinit(wpa_s->p2p_group);
7745 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007746
7747 wpa_s->ap_configured_cb = NULL;
7748 wpa_s->ap_configured_cb_ctx = NULL;
7749 wpa_s->ap_configured_cb_data = NULL;
7750 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007751}
7752
7753
7754int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7755{
Hai Shalomfdcde762020-04-02 11:19:20 -07007756 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7759 return -1;
7760
7761 return p2p_reject(wpa_s->global->p2p, addr);
7762}
7763
7764
7765/* Invite to reinvoke a persistent group */
7766int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007767 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007768 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007769 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007770{
7771 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007772 u8 *bssid = NULL;
7773 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007774 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007775 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007776 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7777 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007778
Hai Shalomc1a21442022-02-04 13:43:00 -08007779 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7780 return -1;
7781
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007782 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007783 if (peer_addr)
7784 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7785 else
7786 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007787
Jouni Malinen31be0a42012-08-31 21:20:51 +03007788 wpa_s->p2p_persistent_go_freq = freq;
7789 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007790 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007791 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007792 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7793 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007794 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007795 if (ssid->mode == WPAS_MODE_P2P_GO) {
7796 role = P2P_INVITE_ROLE_GO;
7797 if (peer_addr == NULL) {
7798 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7799 "address in invitation command");
7800 return -1;
7801 }
7802 if (wpas_p2p_create_iface(wpa_s)) {
7803 if (wpas_p2p_add_group_interface(wpa_s,
7804 WPA_IF_P2P_GO) < 0) {
7805 wpa_printf(MSG_ERROR, "P2P: Failed to "
7806 "allocate a new interface for the "
7807 "group");
7808 return -1;
7809 }
7810 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007811 } else if (wpa_s->p2p_mgmt)
7812 bssid = wpa_s->parent->own_addr;
7813 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007814 bssid = wpa_s->own_addr;
7815 } else {
7816 role = P2P_INVITE_ROLE_CLIENT;
7817 peer_addr = ssid->bssid;
7818 }
7819 wpa_s->pending_invite_ssid_id = ssid->id;
7820
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007821 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007822 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007823 role == P2P_INVITE_ROLE_GO,
7824 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007825 if (res)
7826 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007828 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7829 return -1;
7830
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007831 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7832
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007833 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7834 no_pref_freq_given && pref_freq > 0 &&
7835 wpa_s->num_multichan_concurrent > 1 &&
7836 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7837 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7838 pref_freq);
7839 pref_freq = 0;
7840 }
7841
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007842 /*
7843 * Stop any find/listen operations before invitation and possibly
7844 * connection establishment.
7845 */
7846 wpas_p2p_stop_find_oper(wpa_s);
7847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007849 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007850 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007851}
7852
7853
7854/* Invite to join an active group */
7855int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007856 const u8 *peer_addr, const u8 *go_dev_addr,
7857 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007858{
7859 struct wpa_global *global = wpa_s->global;
7860 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007861 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007862 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007863 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007864 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007865 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007866 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7867 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007868
Jouni Malinen31be0a42012-08-31 21:20:51 +03007869 wpa_s->p2p_persistent_go_freq = 0;
7870 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007871 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007872 wpa_s->p2p_go_vht_center_freq2 = 0;
7873 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007874 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007876 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7877 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7878 break;
7879 }
7880 if (wpa_s == NULL) {
7881 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7882 return -1;
7883 }
7884
7885 ssid = wpa_s->current_ssid;
7886 if (ssid == NULL) {
7887 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7888 "invitation");
7889 return -1;
7890 }
7891
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007892 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007893 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007894 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007895 ssid->ssid, ssid->ssid_len);
7896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007897 if (ssid->mode == WPAS_MODE_P2P_GO) {
7898 role = P2P_INVITE_ROLE_ACTIVE_GO;
7899 bssid = wpa_s->own_addr;
7900 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007901 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007902 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007903 } else {
7904 role = P2P_INVITE_ROLE_CLIENT;
7905 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7906 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7907 "invite to current group");
7908 return -1;
7909 }
7910 bssid = wpa_s->bssid;
7911 if (go_dev_addr == NULL &&
7912 !is_zero_ether_addr(wpa_s->go_dev_addr))
7913 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007914 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7915 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007917 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007919 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7920 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007921 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7922 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007923
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007924 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007925 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007926 role == P2P_INVITE_ROLE_ACTIVE_GO,
7927 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007928 if (res)
7929 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007930 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007931
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007933 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007934 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007935}
7936
7937
7938void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7939{
7940 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007941 u8 go_dev_addr[ETH_ALEN];
7942 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007943 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007944 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007945 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007946
Dmitry Shmidt04949592012-07-19 12:16:46 -07007947 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7948 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007949 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007950 }
7951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007952 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007953 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007954
7955 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007956 if (!wpa_s->p2p_go_group_formation_completed &&
7957 wpa_s->global->p2p_group_formation == wpa_s) {
7958 wpa_dbg(wpa_s, MSG_DEBUG,
7959 "P2P: Marking group formation completed on client on data connection");
7960 wpa_s->p2p_go_group_formation_completed = 1;
7961 wpa_s->global->p2p_group_formation = NULL;
7962 wpa_s->p2p_in_provisioning = 0;
7963 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007964 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007966
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007967 os_memset(go_dev_addr, 0, ETH_ALEN);
7968 if (ssid->bssid_set)
7969 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7970 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7971 ssid->ssid_len);
7972 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7973
7974 if (wpa_s->global->p2p_group_formation == wpa_s)
7975 wpa_s->global->p2p_group_formation = NULL;
7976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007977 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7978 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007979
7980 ip_addr[0] = '\0';
7981 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007982 int res;
7983
7984 res = os_snprintf(ip_addr, sizeof(ip_addr),
7985 " ip_addr=%u.%u.%u.%u "
7986 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7987 ip[0], ip[1], ip[2], ip[3],
7988 ip[4], ip[5], ip[6], ip[7],
7989 ip[8], ip[9], ip[10], ip[11]);
7990 if (os_snprintf_error(sizeof(ip_addr), res))
7991 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007992 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007993 }
7994
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007995 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7996 ssid->passphrase == NULL && ssid->psk_set ?
7997 ssid->psk : NULL,
7998 ssid->passphrase, go_dev_addr, persistent,
7999 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008000
8001 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008002 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
8003 ssid, go_dev_addr);
8004
Hai Shalom5f92bc92019-04-18 11:54:11 -07008005 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008006}
8007
8008
8009int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
8010 u32 interval1, u32 duration2, u32 interval2)
8011{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008012 int ret;
8013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008014 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8015 return -1;
8016
8017 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8018 wpa_s->current_ssid == NULL ||
8019 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8020 return -1;
8021
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008022 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8023 wpa_s->own_addr, wpa_s->assoc_freq,
8024 duration1, interval1, duration2, interval2);
8025 if (ret == 0)
8026 wpa_s->waiting_presence_resp = 1;
8027
8028 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008029}
8030
8031
8032int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8033 unsigned int interval)
8034{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008035 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8036 return -1;
8037
8038 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8039}
8040
8041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008042static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8043{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008044 if (wpa_s->current_ssid == NULL) {
8045 /*
8046 * current_ssid can be cleared when P2P client interface gets
8047 * disconnected, so assume this interface was used as P2P
8048 * client.
8049 */
8050 return 1;
8051 }
8052 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008053 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8054}
8055
8056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008057static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8058{
8059 struct wpa_supplicant *wpa_s = eloop_ctx;
8060
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008061 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008062 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
8063 "disabled");
8064 return;
8065 }
8066
Dmitry Shmidt04949592012-07-19 12:16:46 -07008067 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8068 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008069 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008070}
8071
8072
8073static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8074{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008075 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008076
Dmitry Shmidt04949592012-07-19 12:16:46 -07008077 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8078 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008080 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8081 return;
8082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008083 timeout = wpa_s->conf->p2p_group_idle;
8084 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8085 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8086 timeout = P2P_MAX_CLIENT_IDLE;
8087
8088 if (timeout == 0)
8089 return;
8090
Dmitry Shmidt04949592012-07-19 12:16:46 -07008091 if (timeout < 0) {
8092 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8093 timeout = 0; /* special client mode no-timeout */
8094 else
8095 return;
8096 }
8097
8098 if (wpa_s->p2p_in_provisioning) {
8099 /*
8100 * Use the normal group formation timeout during the
8101 * provisioning phase to avoid terminating this process too
8102 * early due to group idle timeout.
8103 */
8104 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8105 "during provisioning");
8106 return;
8107 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308108
Dmitry Shmidt04949592012-07-19 12:16:46 -07008109 if (wpa_s->show_group_started) {
8110 /*
8111 * Use the normal group formation timeout between the end of
8112 * the provisioning phase and completion of 4-way handshake to
8113 * avoid terminating this process too early due to group idle
8114 * timeout.
8115 */
8116 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8117 "while waiting for initial 4-way handshake to "
8118 "complete");
8119 return;
8120 }
8121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008122 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008123 timeout);
8124 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8125 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008126}
8127
8128
Jouni Malinen2b89da82012-08-31 22:04:41 +03008129/* Returns 1 if the interface was removed */
8130int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8131 u16 reason_code, const u8 *ie, size_t ie_len,
8132 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008133{
8134 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008135 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008136
Dmitry Shmidt04949592012-07-19 12:16:46 -07008137 if (!locally_generated)
8138 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8139 ie_len);
8140
8141 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8142 wpa_s->current_ssid &&
8143 wpa_s->current_ssid->p2p_group &&
8144 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8145 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8146 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008147 if (wpas_p2p_group_delete(wpa_s,
8148 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8149 > 0)
8150 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008151 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008152
8153 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008154}
8155
8156
8157void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008158 u16 reason_code, const u8 *ie, size_t ie_len,
8159 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008160{
8161 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8162 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008163
Dmitry Shmidt04949592012-07-19 12:16:46 -07008164 if (!locally_generated)
8165 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8166 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008167}
8168
8169
8170void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8171{
8172 struct p2p_data *p2p = wpa_s->global->p2p;
8173
8174 if (p2p == NULL)
8175 return;
8176
8177 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8178 return;
8179
8180 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8181 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8182
8183 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8184 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8185
8186 if (wpa_s->wps &&
8187 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8188 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8189
8190 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8191 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8192
8193 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8194 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8195 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8196 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8197 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8198 }
8199
8200 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8201 p2p_set_sec_dev_types(p2p,
8202 (void *) wpa_s->conf->sec_device_type,
8203 wpa_s->conf->num_sec_device_types);
8204
8205 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8206 int i;
8207 p2p_remove_wps_vendor_extensions(p2p);
8208 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8209 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8210 continue;
8211 p2p_add_wps_vendor_extension(
8212 p2p, wpa_s->conf->wps_vendor_ext[i]);
8213 }
8214 }
8215
8216 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8217 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8218 char country[3];
8219 country[0] = wpa_s->conf->country[0];
8220 country[1] = wpa_s->conf->country[1];
8221 country[2] = 0x04;
8222 p2p_set_country(p2p, country);
8223 }
8224
8225 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8226 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8227 wpa_s->conf->p2p_ssid_postfix ?
8228 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8229 0);
8230 }
8231
8232 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8233 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008234
8235 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8236 u8 reg_class, channel;
8237 int ret;
8238 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008239 u8 channel_forced;
8240
Jouni Malinen75ecf522011-06-27 15:19:46 -07008241 if (wpa_s->conf->p2p_listen_reg_class &&
8242 wpa_s->conf->p2p_listen_channel) {
8243 reg_class = wpa_s->conf->p2p_listen_reg_class;
8244 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008245 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008246 } else {
8247 reg_class = 81;
8248 /*
8249 * Pick one of the social channels randomly as the
8250 * listen channel.
8251 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008252 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8253 channel = 1;
8254 else
8255 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008256 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008257 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008258 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8259 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008260 if (ret)
8261 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8262 "failed: %d", ret);
8263 }
8264 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8265 u8 op_reg_class, op_channel, cfg_op_channel;
8266 int ret = 0;
8267 unsigned int r;
8268 if (wpa_s->conf->p2p_oper_reg_class &&
8269 wpa_s->conf->p2p_oper_channel) {
8270 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8271 op_channel = wpa_s->conf->p2p_oper_channel;
8272 cfg_op_channel = 1;
8273 } else {
8274 op_reg_class = 81;
8275 /*
8276 * Use random operation channel from (1, 6, 11)
8277 *if no other preference is indicated.
8278 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008279 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8280 op_channel = 1;
8281 else
8282 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008283 cfg_op_channel = 0;
8284 }
8285 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8286 cfg_op_channel);
8287 if (ret)
8288 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8289 "failed: %d", ret);
8290 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008291
8292 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8293 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8294 wpa_s->conf->p2p_pref_chan) < 0) {
8295 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8296 "update failed");
8297 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008298
8299 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8300 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8301 "update failed");
8302 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008303 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008304
8305 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8306 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008307}
8308
8309
8310int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8311 int duration)
8312{
8313 if (!wpa_s->ap_iface)
8314 return -1;
8315 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8316 duration);
8317}
8318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008319
8320int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8321{
8322 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8323 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008324
8325 wpa_s->global->cross_connection = enabled;
8326 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8327
8328 if (!enabled) {
8329 struct wpa_supplicant *iface;
8330
8331 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8332 {
8333 if (iface->cross_connect_enabled == 0)
8334 continue;
8335
8336 iface->cross_connect_enabled = 0;
8337 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008338 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008339 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8340 iface->ifname,
8341 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008342 }
8343 }
8344
8345 return 0;
8346}
8347
8348
8349static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8350{
8351 struct wpa_supplicant *iface;
8352
8353 if (!uplink->global->cross_connection)
8354 return;
8355
8356 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8357 if (!iface->cross_connect_enabled)
8358 continue;
8359 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8360 0)
8361 continue;
8362 if (iface->ap_iface == NULL)
8363 continue;
8364 if (iface->cross_connect_in_use)
8365 continue;
8366
8367 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008368 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008369 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8370 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008371 }
8372}
8373
8374
8375static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8376{
8377 struct wpa_supplicant *iface;
8378
8379 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8380 if (!iface->cross_connect_enabled)
8381 continue;
8382 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8383 0)
8384 continue;
8385 if (!iface->cross_connect_in_use)
8386 continue;
8387
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008388 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008389 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8390 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008391 iface->cross_connect_in_use = 0;
8392 }
8393}
8394
8395
8396void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8397{
8398 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8399 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8400 wpa_s->cross_connect_disallowed)
8401 wpas_p2p_disable_cross_connect(wpa_s);
8402 else
8403 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008404 if (!wpa_s->ap_iface &&
8405 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8406 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008407}
8408
8409
8410void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8411{
8412 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008413 if (!wpa_s->ap_iface &&
8414 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8415 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008416 wpas_p2p_set_group_idle_timeout(wpa_s);
8417}
8418
8419
8420static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8421{
8422 struct wpa_supplicant *iface;
8423
8424 if (!wpa_s->global->cross_connection)
8425 return;
8426
8427 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8428 if (iface == wpa_s)
8429 continue;
8430 if (iface->drv_flags &
8431 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8432 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008433 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8434 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008435 continue;
8436
8437 wpa_s->cross_connect_enabled = 1;
8438 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8439 sizeof(wpa_s->cross_connect_uplink));
8440 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8441 "%s to %s whenever uplink is available",
8442 wpa_s->ifname, wpa_s->cross_connect_uplink);
8443
8444 if (iface->ap_iface || iface->current_ssid == NULL ||
8445 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8446 iface->cross_connect_disallowed ||
8447 iface->wpa_state != WPA_COMPLETED)
8448 break;
8449
8450 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008451 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008452 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8453 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008454 break;
8455 }
8456}
8457
8458
Sunil Ravi036cec52023-03-29 11:35:17 -07008459static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008460{
8461 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8462 !wpa_s->p2p_in_provisioning)
8463 return 0; /* not P2P client operation */
8464
8465 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8466 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008467 if (wpa_s != wpa_s->p2pdev)
8468 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008469 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008470 return 1;
8471}
8472
8473
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008474void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8475{
8476 struct wpa_supplicant *wpa_s = eloop_ctx;
8477 wpas_p2p_notif_pbc_overlap(wpa_s);
8478}
8479
8480
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008481void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8482 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008483{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008484 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008485 struct wpa_used_freq_data *freqs = NULL;
8486 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008487
8488 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8489 return;
8490
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008491 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8492 if (!freqs)
8493 return;
8494
Sunil Ravi77d572f2023-01-17 23:58:31 +00008495 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008497 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008498 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008499 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8500 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008501 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8502 "channel list");
8503 return;
8504 }
8505
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008506 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008507
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008508 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008509
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008510 /*
8511 * The used frequencies map changed, so it is possible that a GO is
8512 * using a channel that is no longer valid for P2P use. It is also
8513 * possible that due to policy consideration, it would be preferable to
8514 * move it to a frequency already used by other station interfaces.
8515 */
8516 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8517
8518 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008519}
8520
8521
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008522static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8523 struct wpa_scan_results *scan_res)
8524{
8525 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8526}
8527
8528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008529int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8530{
8531 struct wpa_global *global = wpa_s->global;
8532 int found = 0;
8533 const u8 *peer;
8534
8535 if (global->p2p == NULL)
8536 return -1;
8537
8538 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8539
8540 if (wpa_s->pending_interface_name[0] &&
8541 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8542 found = 1;
8543
8544 peer = p2p_get_go_neg_peer(global->p2p);
8545 if (peer) {
8546 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8547 MACSTR, MAC2STR(peer));
8548 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008549 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008550 }
8551
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008552 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8553 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8554 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8555 found = 1;
8556 }
8557
8558 if (wpa_s->pending_pd_before_join) {
8559 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8560 wpa_s->pending_pd_before_join = 0;
8561 found = 1;
8562 }
8563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008564 wpas_p2p_stop_find(wpa_s);
8565
8566 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8567 if (wpa_s == global->p2p_group_formation &&
8568 (wpa_s->p2p_in_provisioning ||
8569 wpa_s->parent->pending_interface_type ==
8570 WPA_IF_P2P_CLIENT)) {
8571 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8572 "formation found - cancelling",
8573 wpa_s->ifname);
8574 found = 1;
8575 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008576 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008577 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008578 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008579 break;
8580 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008581 wpas_p2p_group_delete(wpa_s,
8582 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008583 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008584 } else if (wpa_s->p2p_in_invitation) {
8585 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8586 wpa_s->ifname);
8587 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008588 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008589 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008590 }
8591 }
8592
8593 if (!found) {
8594 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8595 return -1;
8596 }
8597
8598 return 0;
8599}
8600
8601
8602void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8603{
8604 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8605 return;
8606
8607 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8608 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008609 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008610}
8611
8612
8613void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8614 int freq_24, int freq_5, int freq_overall)
8615{
8616 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008617 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008618 return;
8619 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8620}
8621
8622
8623int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8624{
8625 u8 peer[ETH_ALEN];
8626 struct p2p_data *p2p = wpa_s->global->p2p;
8627
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008628 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008629 return -1;
8630
8631 if (hwaddr_aton(addr, peer))
8632 return -1;
8633
8634 return p2p_unauthorize(p2p, peer);
8635}
8636
8637
8638/**
8639 * wpas_p2p_disconnect - Disconnect from a P2P Group
8640 * @wpa_s: Pointer to wpa_supplicant data
8641 * Returns: 0 on success, -1 on failure
8642 *
8643 * This can be used to disconnect from a group in which the local end is a P2P
8644 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8645 * virtual network interface was created for this group, that interface will be
8646 * removed. Otherwise, only the configured P2P group network will be removed
8647 * from the interface.
8648 */
8649int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8650{
8651
8652 if (wpa_s == NULL)
8653 return -1;
8654
Jouni Malinen2b89da82012-08-31 22:04:41 +03008655 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8656 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008657}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008658
8659
8660int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8661{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008662 int ret;
8663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008664 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8665 return 0;
8666
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008667 ret = p2p_in_progress(wpa_s->global->p2p);
8668 if (ret == 0) {
8669 /*
8670 * Check whether there is an ongoing WPS provisioning step (or
8671 * other parts of group formation) on another interface since
8672 * p2p_in_progress() does not report this to avoid issues for
8673 * scans during such provisioning step.
8674 */
8675 if (wpa_s->global->p2p_group_formation &&
8676 wpa_s->global->p2p_group_formation != wpa_s) {
8677 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8678 "in group formation",
8679 wpa_s->global->p2p_group_formation->ifname);
8680 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008681 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
8682 wpa_dbg(wpa_s, MSG_DEBUG,
8683 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
8684 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008685 }
8686 }
8687
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008688 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008689 struct os_reltime now;
8690 os_get_reltime(&now);
8691 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8692 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008693 /* Wait for the first client has expired */
8694 wpa_s->global->p2p_go_wait_client.sec = 0;
8695 } else {
8696 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8697 ret = 1;
8698 }
8699 }
8700
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008701 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008702}
8703
8704
8705void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8706 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008707{
8708 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8709 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008710 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008711 /**
8712 * Remove the network by scheduling the group formation
8713 * timeout to happen immediately. The teardown code
8714 * needs to be scheduled to run asynch later so that we
8715 * don't delete data from under ourselves unexpectedly.
8716 * Calling wpas_p2p_group_formation_timeout directly
8717 * causes a series of crashes in WPS failure scenarios.
8718 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008719 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8720 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008721 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008722 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008723 }
8724}
8725
8726
8727struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008728 const u8 *addr, const u8 *ssid,
8729 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008730{
8731 struct wpa_ssid *s;
8732 size_t i;
8733
8734 for (s = wpa_s->conf->ssid; s; s = s->next) {
8735 if (s->disabled != 2)
8736 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008737 if (ssid &&
8738 (ssid_len != s->ssid_len ||
8739 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8740 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008741 if (addr == NULL) {
8742 if (s->mode == WPAS_MODE_P2P_GO)
8743 return s;
8744 continue;
8745 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008746 if (ether_addr_equal(s->bssid, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008747 return s; /* peer is GO in the persistent group */
8748 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8749 continue;
8750 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008751 if (ether_addr_equal(s->p2p_client_list +
8752 i * 2 * ETH_ALEN, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008753 return s; /* peer is P2P client in persistent
8754 * group */
8755 }
8756 }
8757
8758 return NULL;
8759}
8760
8761
8762void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8763 const u8 *addr)
8764{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008765 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008766 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008767 /*
8768 * This can happen if WPS provisioning step is not terminated
8769 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8770 * peer was able to connect, there is no need to time out group
8771 * formation after this, though. In addition, this is used with
8772 * the initial connection wait on the GO as a separate formation
8773 * timeout and as such, expected to be hit after the initial WPS
8774 * provisioning step.
8775 */
8776 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008777
8778 if (!wpa_s->p2p_go_group_formation_completed &&
8779 !wpa_s->group_formation_reported) {
8780 /*
8781 * GO has not yet notified group formation success since
8782 * the WPS step was not completed cleanly. Do that
8783 * notification now since the P2P Client was able to
8784 * connect and as such, must have received the
8785 * credential from the WPS step.
8786 */
8787 if (wpa_s->global->p2p)
8788 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008789 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008790 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008791 }
8792 if (!wpa_s->p2p_go_group_formation_completed) {
8793 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8794 wpa_s->p2p_go_group_formation_completed = 1;
8795 wpa_s->global->p2p_group_formation = NULL;
8796 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008797 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008798 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008799 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008800 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008801 if (addr == NULL)
8802 return;
8803 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8804}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008805
Dmitry Shmidt04949592012-07-19 12:16:46 -07008806
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008807static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8808 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008809{
8810 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008811 int ret = 0;
8812
Dmitry Shmidt04949592012-07-19 12:16:46 -07008813 if (wpa_s->global->p2p_group_formation)
8814 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008815 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008816 offchannel_send_action_done(wpa_s);
8817 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008818 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008819 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8820 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8821 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8822 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008823 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008824 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008825 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008826 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008827 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008828 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008829 wpa_s->p2p_go_he,
8830 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008831 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008832 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008833}
8834
8835
8836int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8837{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008838 int res;
8839
Dmitry Shmidt04949592012-07-19 12:16:46 -07008840 if (!wpa_s->p2p_fallback_to_go_neg ||
8841 wpa_s->p2p_in_provisioning <= 5)
8842 return 0;
8843
8844 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8845 return 0; /* peer operating as a GO */
8846
8847 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8848 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008849 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008850 "reason=GO-not-found");
8851 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008852
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008853 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008854}
8855
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008856
8857unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8858{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008859 struct wpa_supplicant *ifs;
8860
8861 if (wpa_s->wpa_state > WPA_SCANNING) {
8862 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8863 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008864 wpa_s->conf->p2p_search_delay);
8865 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008866 }
8867
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008868 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8869 radio_list) {
8870 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008871 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8872 "delay due to concurrent operation on "
8873 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008874 wpa_s->conf->p2p_search_delay,
8875 ifs->ifname);
8876 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008877 }
8878 }
8879
8880 return 0;
8881}
8882
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008883
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008884static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8885 struct wpa_ssid *s, const u8 *addr,
8886 int iface_addr)
8887{
8888 struct psk_list_entry *psk, *tmp;
8889 int changed = 0;
8890
8891 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8892 list) {
8893 if ((iface_addr && !psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008894 ether_addr_equal(addr, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008895 (!iface_addr && psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008896 ether_addr_equal(addr, psk->addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008897 wpa_dbg(wpa_s, MSG_DEBUG,
8898 "P2P: Remove persistent group PSK list entry for "
8899 MACSTR " p2p=%u",
8900 MAC2STR(psk->addr), psk->p2p);
8901 dl_list_del(&psk->list);
8902 os_free(psk);
8903 changed++;
8904 }
8905 }
8906
8907 return changed;
8908}
8909
8910
8911void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8912 const u8 *p2p_dev_addr,
8913 const u8 *psk, size_t psk_len)
8914{
8915 struct wpa_ssid *ssid = wpa_s->current_ssid;
8916 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008917 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008918
8919 if (psk_len != sizeof(p->psk))
8920 return;
8921
8922 if (p2p_dev_addr) {
8923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8924 " p2p_dev_addr=" MACSTR,
8925 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8926 if (is_zero_ether_addr(p2p_dev_addr))
8927 p2p_dev_addr = NULL;
8928 } else {
8929 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8930 MAC2STR(mac_addr));
8931 }
8932
8933 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8934 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8935 /* To be added to persistent group once created */
8936 if (wpa_s->global->add_psk == NULL) {
8937 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8938 if (wpa_s->global->add_psk == NULL)
8939 return;
8940 }
8941 p = wpa_s->global->add_psk;
8942 if (p2p_dev_addr) {
8943 p->p2p = 1;
8944 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8945 } else {
8946 p->p2p = 0;
8947 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8948 }
8949 os_memcpy(p->psk, psk, psk_len);
8950 return;
8951 }
8952
8953 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8954 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8955 return;
8956 }
8957
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008958 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008959 ssid->ssid_len);
8960 if (!persistent) {
8961 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8962 return;
8963 }
8964
8965 p = os_zalloc(sizeof(*p));
8966 if (p == NULL)
8967 return;
8968 if (p2p_dev_addr) {
8969 p->p2p = 1;
8970 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8971 } else {
8972 p->p2p = 0;
8973 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8974 }
8975 os_memcpy(p->psk, psk, psk_len);
8976
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008977 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8978 (last = dl_list_last(&persistent->psk_list,
8979 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8981 MACSTR " (p2p=%u) to make room for a new one",
8982 MAC2STR(last->addr), last->p2p);
8983 dl_list_del(&last->list);
8984 os_free(last);
8985 }
8986
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008987 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008988 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8989 p2p_dev_addr == NULL);
8990 if (p2p_dev_addr) {
8991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8992 MACSTR, MAC2STR(p2p_dev_addr));
8993 } else {
8994 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8995 MAC2STR(mac_addr));
8996 }
8997 dl_list_add(&persistent->psk_list, &p->list);
8998
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008999 if (wpa_s->p2pdev->conf->update_config &&
9000 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009001 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009002}
9003
9004
9005static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
9006 struct wpa_ssid *s, const u8 *addr,
9007 int iface_addr)
9008{
9009 int res;
9010
9011 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009012 if (res > 0 && wpa_s->conf->update_config &&
9013 wpa_config_write(wpa_s->confname, wpa_s->conf))
9014 wpa_dbg(wpa_s, MSG_DEBUG,
9015 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009016}
9017
9018
9019static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
9020 const u8 *peer, int iface_addr)
9021{
9022 struct hostapd_data *hapd;
9023 struct hostapd_wpa_psk *psk, *prev, *rem;
9024 struct sta_info *sta;
9025
9026 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
9027 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
9028 return;
9029
9030 /* Remove per-station PSK entry */
9031 hapd = wpa_s->ap_iface->bss[0];
9032 prev = NULL;
9033 psk = hapd->conf->ssid.wpa_psk;
9034 while (psk) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009035 if ((iface_addr && ether_addr_equal(peer, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009036 (!iface_addr &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009037 ether_addr_equal(peer, psk->p2p_dev_addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009038 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
9039 MACSTR " iface_addr=%d",
9040 MAC2STR(peer), iface_addr);
9041 if (prev)
9042 prev->next = psk->next;
9043 else
9044 hapd->conf->ssid.wpa_psk = psk->next;
9045 rem = psk;
9046 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07009047 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009048 } else {
9049 prev = psk;
9050 psk = psk->next;
9051 }
9052 }
9053
9054 /* Disconnect from group */
9055 if (iface_addr)
9056 sta = ap_get_sta(hapd, peer);
9057 else
9058 sta = ap_get_sta_p2p(hapd, peer);
9059 if (sta) {
9060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
9061 " (iface_addr=%d) from group",
9062 MAC2STR(peer), iface_addr);
9063 hostapd_drv_sta_deauth(hapd, sta->addr,
9064 WLAN_REASON_DEAUTH_LEAVING);
9065 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
9066 }
9067}
9068
9069
9070void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9071 int iface_addr)
9072{
9073 struct wpa_ssid *s;
9074 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009075 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009076
9077 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9078
9079 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009080 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009081 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9082 continue;
9083 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009084 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9085 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009086 }
9087
9088 /* Remove from any operating group */
9089 for (w = wpa_s->global->ifaces; w; w = w->next)
9090 wpas_p2p_remove_client_go(w, peer, iface_addr);
9091}
9092
9093
9094static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9095{
9096 struct wpa_supplicant *wpa_s = eloop_ctx;
9097 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9098}
9099
9100
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009101static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9102{
9103 struct wpa_supplicant *wpa_s = eloop_ctx;
9104
9105 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9106 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9107}
9108
9109
9110int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9111 struct wpa_ssid *ssid)
9112{
9113 struct wpa_supplicant *iface;
9114
9115 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9116 if (!iface->current_ssid ||
9117 iface->current_ssid->frequency == freq ||
9118 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9119 !iface->current_ssid->p2p_group))
9120 continue;
9121
9122 /* Remove the connection with least priority */
9123 if (!wpas_is_p2p_prioritized(iface)) {
9124 /* STA connection has priority over existing
9125 * P2P connection, so remove the interface. */
9126 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9127 eloop_register_timeout(0, 0,
9128 wpas_p2p_group_freq_conflict,
9129 iface, NULL);
9130 /* If connection in progress is P2P connection, do not
9131 * proceed for the connection. */
9132 if (wpa_s == iface)
9133 return -1;
9134 else
9135 return 0;
9136 } else {
9137 /* P2P connection has priority, disable the STA network
9138 */
9139 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9140 ssid);
9141 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9142 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9143 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9144 ETH_ALEN);
9145 /* If P2P connection is in progress, continue
9146 * connecting...*/
9147 if (wpa_s == iface)
9148 return 0;
9149 else
9150 return -1;
9151 }
9152 }
9153
9154 return 0;
9155}
9156
9157
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009158int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9159{
9160 struct wpa_ssid *ssid = wpa_s->current_ssid;
9161
9162 if (ssid == NULL || !ssid->p2p_group)
9163 return 0;
9164
9165 if (wpa_s->p2p_last_4way_hs_fail &&
9166 wpa_s->p2p_last_4way_hs_fail == ssid) {
9167 u8 go_dev_addr[ETH_ALEN];
9168 struct wpa_ssid *persistent;
9169
9170 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9171 ssid->ssid,
9172 ssid->ssid_len) <= 0) {
9173 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9174 goto disconnect;
9175 }
9176
9177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9178 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009179 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009180 ssid->ssid,
9181 ssid->ssid_len);
9182 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9183 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9184 goto disconnect;
9185 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009186 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009187 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9188 persistent->id);
9189 disconnect:
9190 wpa_s->p2p_last_4way_hs_fail = NULL;
9191 /*
9192 * Remove the group from a timeout to avoid issues with caller
9193 * continuing to use the interface if this is on a P2P group
9194 * interface.
9195 */
9196 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9197 wpa_s, NULL);
9198 return 1;
9199 }
9200
9201 wpa_s->p2p_last_4way_hs_fail = ssid;
9202 return 0;
9203}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009204
9205
9206#ifdef CONFIG_WPS_NFC
9207
9208static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9209 struct wpabuf *p2p)
9210{
9211 struct wpabuf *ret;
9212 size_t wsc_len;
9213
9214 if (p2p == NULL) {
9215 wpabuf_free(wsc);
9216 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9217 return NULL;
9218 }
9219
9220 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9221 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9222 if (ret == NULL) {
9223 wpabuf_free(wsc);
9224 wpabuf_free(p2p);
9225 return NULL;
9226 }
9227
9228 wpabuf_put_be16(ret, wsc_len);
9229 if (wsc)
9230 wpabuf_put_buf(ret, wsc);
9231 wpabuf_put_be16(ret, wpabuf_len(p2p));
9232 wpabuf_put_buf(ret, p2p);
9233
9234 wpabuf_free(wsc);
9235 wpabuf_free(p2p);
9236 wpa_hexdump_buf(MSG_DEBUG,
9237 "P2P: Generated NFC connection handover message", ret);
9238
9239 if (ndef && ret) {
9240 struct wpabuf *tmp;
9241 tmp = ndef_build_p2p(ret);
9242 wpabuf_free(ret);
9243 if (tmp == NULL) {
9244 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9245 return NULL;
9246 }
9247 ret = tmp;
9248 }
9249
9250 return ret;
9251}
9252
9253
9254static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9255 struct wpa_ssid **ssid, u8 *go_dev_addr)
9256{
9257 struct wpa_supplicant *iface;
9258
9259 if (go_dev_addr)
9260 os_memset(go_dev_addr, 0, ETH_ALEN);
9261 if (ssid)
9262 *ssid = NULL;
9263 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9264 if (iface->wpa_state < WPA_ASSOCIATING ||
9265 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9266 !iface->current_ssid->p2p_group ||
9267 iface->current_ssid->mode != WPAS_MODE_INFRA)
9268 continue;
9269 if (ssid)
9270 *ssid = iface->current_ssid;
9271 if (go_dev_addr)
9272 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9273 return iface->assoc_freq;
9274 }
9275 return 0;
9276}
9277
9278
9279struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9280 int ndef)
9281{
9282 struct wpabuf *wsc, *p2p;
9283 struct wpa_ssid *ssid;
9284 u8 go_dev_addr[ETH_ALEN];
9285 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9286
9287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9288 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9289 return NULL;
9290 }
9291
9292 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9293 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9294 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9295 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9296 return NULL;
9297 }
9298
9299 if (cli_freq == 0) {
9300 wsc = wps_build_nfc_handover_req_p2p(
9301 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9302 } else
9303 wsc = NULL;
9304 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9305 go_dev_addr, ssid ? ssid->ssid : NULL,
9306 ssid ? ssid->ssid_len : 0);
9307
9308 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9309}
9310
9311
9312struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9313 int ndef, int tag)
9314{
9315 struct wpabuf *wsc, *p2p;
9316 struct wpa_ssid *ssid;
9317 u8 go_dev_addr[ETH_ALEN];
9318 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9319
9320 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9321 return NULL;
9322
9323 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9324 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9325 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9326 return NULL;
9327
9328 if (cli_freq == 0) {
9329 wsc = wps_build_nfc_handover_sel_p2p(
9330 wpa_s->parent->wps,
9331 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9332 DEV_PW_NFC_CONNECTION_HANDOVER,
9333 wpa_s->conf->wps_nfc_dh_pubkey,
9334 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9335 } else
9336 wsc = NULL;
9337 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9338 go_dev_addr, ssid ? ssid->ssid : NULL,
9339 ssid ? ssid->ssid_len : 0);
9340
9341 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9342}
9343
9344
9345static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9346 struct p2p_nfc_params *params)
9347{
9348 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9349 "connection handover (freq=%d)",
9350 params->go_freq);
9351
9352 if (params->go_freq && params->go_ssid_len) {
9353 wpa_s->p2p_wps_method = WPS_NFC;
9354 wpa_s->pending_join_wps_method = WPS_NFC;
9355 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9356 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9357 ETH_ALEN);
9358 return wpas_p2p_join_start(wpa_s, params->go_freq,
9359 params->go_ssid,
9360 params->go_ssid_len);
9361 }
9362
9363 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9364 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009365 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009366 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009367 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009368 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009369 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009370}
9371
9372
9373static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9374 struct p2p_nfc_params *params, int tag)
9375{
9376 int res, persistent;
9377 struct wpa_ssid *ssid;
9378
9379 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9380 "connection handover");
9381 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9382 ssid = wpa_s->current_ssid;
9383 if (ssid == NULL)
9384 continue;
9385 if (ssid->mode != WPAS_MODE_P2P_GO)
9386 continue;
9387 if (wpa_s->ap_iface == NULL)
9388 continue;
9389 break;
9390 }
9391 if (wpa_s == NULL) {
9392 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9393 return -1;
9394 }
9395
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009396 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009397 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009398 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009399 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9400 return -1;
9401 }
9402 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009403 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9404 wpa_s->p2pdev->p2p_oob_dev_pw,
9405 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9406 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009407 if (res)
9408 return res;
9409
9410 if (!tag) {
9411 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9412 return 0;
9413 }
9414
9415 if (!params->peer ||
9416 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9417 return 0;
9418
9419 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9420 " to join", MAC2STR(params->peer->p2p_device_addr));
9421
9422 wpa_s->global->p2p_invite_group = wpa_s;
9423 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009424 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009425 params->peer->p2p_device_addr,
9426 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009427 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009428
9429 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9430 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9431 ssid->ssid, ssid->ssid_len, ssid->frequency,
9432 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009433 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009434}
9435
9436
9437static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9438 struct p2p_nfc_params *params,
9439 int forced_freq)
9440{
9441 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9442 "connection handover");
9443 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9444 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009445 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009446 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009447 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009448 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009449}
9450
9451
9452static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9453 struct p2p_nfc_params *params,
9454 int forced_freq)
9455{
9456 int res;
9457
9458 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9459 "connection handover");
9460 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9461 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009462 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009463 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009464 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009465 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009466 if (res)
9467 return res;
9468
9469 res = wpas_p2p_listen(wpa_s, 60);
9470 if (res) {
9471 p2p_unauthorize(wpa_s->global->p2p,
9472 params->peer->p2p_device_addr);
9473 }
9474
9475 return res;
9476}
9477
9478
9479static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9480 const struct wpabuf *data,
9481 int sel, int tag, int forced_freq)
9482{
9483 const u8 *pos, *end;
9484 u16 len, id;
9485 struct p2p_nfc_params params;
9486 int res;
9487
9488 os_memset(&params, 0, sizeof(params));
9489 params.sel = sel;
9490
9491 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9492
9493 pos = wpabuf_head(data);
9494 end = pos + wpabuf_len(data);
9495
9496 if (end - pos < 2) {
9497 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9498 "attributes");
9499 return -1;
9500 }
9501 len = WPA_GET_BE16(pos);
9502 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009503 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009504 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9505 "attributes");
9506 return -1;
9507 }
9508 params.wsc_attr = pos;
9509 params.wsc_len = len;
9510 pos += len;
9511
9512 if (end - pos < 2) {
9513 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9514 "attributes");
9515 return -1;
9516 }
9517 len = WPA_GET_BE16(pos);
9518 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009519 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009520 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9521 "attributes");
9522 return -1;
9523 }
9524 params.p2p_attr = pos;
9525 params.p2p_len = len;
9526 pos += len;
9527
9528 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9529 params.wsc_attr, params.wsc_len);
9530 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9531 params.p2p_attr, params.p2p_len);
9532 if (pos < end) {
9533 wpa_hexdump(MSG_DEBUG,
9534 "P2P: Ignored extra data after P2P attributes",
9535 pos, end - pos);
9536 }
9537
9538 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9539 if (res)
9540 return res;
9541
9542 if (params.next_step == NO_ACTION)
9543 return 0;
9544
9545 if (params.next_step == BOTH_GO) {
9546 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9547 MAC2STR(params.peer->p2p_device_addr));
9548 return 0;
9549 }
9550
9551 if (params.next_step == PEER_CLIENT) {
9552 if (!is_zero_ether_addr(params.go_dev_addr)) {
9553 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9554 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9555 " ssid=\"%s\"",
9556 MAC2STR(params.peer->p2p_device_addr),
9557 params.go_freq,
9558 MAC2STR(params.go_dev_addr),
9559 wpa_ssid_txt(params.go_ssid,
9560 params.go_ssid_len));
9561 } else {
9562 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9563 "peer=" MACSTR " freq=%d",
9564 MAC2STR(params.peer->p2p_device_addr),
9565 params.go_freq);
9566 }
9567 return 0;
9568 }
9569
9570 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9571 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9572 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9573 return 0;
9574 }
9575
9576 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9577 wpa_s->p2p_oob_dev_pw = NULL;
9578
9579 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9580 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9581 "received");
9582 return -1;
9583 }
9584
9585 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9586 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9587 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9588 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9589 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9590 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9591 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9592
9593 if (tag) {
9594 if (id < 0x10) {
9595 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9596 "peer OOB Device Password Id %u", id);
9597 return -1;
9598 }
9599 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9600 "Device Password Id %u", id);
9601 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9602 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9603 params.oob_dev_pw_len -
9604 WPS_OOB_PUBKEY_HASH_LEN - 2);
9605 wpa_s->p2p_oob_dev_pw_id = id;
9606 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9607 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9608 params.oob_dev_pw_len -
9609 WPS_OOB_PUBKEY_HASH_LEN - 2);
9610 if (wpa_s->p2p_oob_dev_pw == NULL)
9611 return -1;
9612
9613 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9614 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9615 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9616 return -1;
9617 } else {
9618 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9619 "without Device Password");
9620 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9621 }
9622
9623 switch (params.next_step) {
9624 case NO_ACTION:
9625 case BOTH_GO:
9626 case PEER_CLIENT:
9627 /* already covered above */
9628 return 0;
9629 case JOIN_GROUP:
9630 return wpas_p2p_nfc_join_group(wpa_s, &params);
9631 case AUTH_JOIN:
9632 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9633 case INIT_GO_NEG:
9634 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9635 case RESP_GO_NEG:
9636 /* TODO: use own OOB Dev Pw */
9637 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9638 }
9639
9640 return -1;
9641}
9642
9643
9644int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9645 const struct wpabuf *data, int forced_freq)
9646{
9647 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9648 return -1;
9649
9650 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9651}
9652
9653
9654int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9655 const struct wpabuf *req,
9656 const struct wpabuf *sel, int forced_freq)
9657{
9658 struct wpabuf *tmp;
9659 int ret;
9660
9661 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9662 return -1;
9663
9664 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9665
9666 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9667 wpabuf_head(req), wpabuf_len(req));
9668 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9669 wpabuf_head(sel), wpabuf_len(sel));
9670 if (forced_freq)
9671 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9672 tmp = ndef_parse_p2p(init ? sel : req);
9673 if (tmp == NULL) {
9674 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9675 return -1;
9676 }
9677
9678 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9679 forced_freq);
9680 wpabuf_free(tmp);
9681
9682 return ret;
9683}
9684
9685
9686int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9687{
9688 const u8 *if_addr;
9689 int go_intent = wpa_s->conf->p2p_go_intent;
9690 struct wpa_supplicant *iface;
9691
9692 if (wpa_s->global->p2p == NULL)
9693 return -1;
9694
9695 if (!enabled) {
9696 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9697 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9698 {
9699 if (!iface->ap_iface)
9700 continue;
9701 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9702 }
9703 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9704 0, NULL);
9705 if (wpa_s->p2p_nfc_tag_enabled)
9706 wpas_p2p_remove_pending_group_interface(wpa_s);
9707 wpa_s->p2p_nfc_tag_enabled = 0;
9708 return 0;
9709 }
9710
9711 if (wpa_s->global->p2p_disabled)
9712 return -1;
9713
9714 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9715 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9716 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9717 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9718 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9719 "to allow static handover cases");
9720 return -1;
9721 }
9722
9723 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9724
9725 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9726 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9727 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9728 if (wpa_s->p2p_oob_dev_pw == NULL)
9729 return -1;
9730 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9731
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009732 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9733 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9734 /*
9735 * P2P Group Interface present and the command came on group
9736 * interface, so enable the token for the current interface.
9737 */
9738 wpa_s->create_p2p_iface = 0;
9739 } else {
9740 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9741 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009742
9743 if (wpa_s->create_p2p_iface) {
9744 enum wpa_driver_if_type iftype;
9745 /* Prepare to add a new interface for the group */
9746 iftype = WPA_IF_P2P_GROUP;
9747 if (go_intent == 15)
9748 iftype = WPA_IF_P2P_GO;
9749 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9750 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9751 "interface for the group");
9752 return -1;
9753 }
9754
9755 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009756 } else if (wpa_s->p2p_mgmt)
9757 if_addr = wpa_s->parent->own_addr;
9758 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009759 if_addr = wpa_s->own_addr;
9760
9761 wpa_s->p2p_nfc_tag_enabled = enabled;
9762
9763 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9764 struct hostapd_data *hapd;
9765 if (iface->ap_iface == NULL)
9766 continue;
9767 hapd = iface->ap_iface->bss[0];
9768 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9769 hapd->conf->wps_nfc_dh_pubkey =
9770 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9771 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9772 hapd->conf->wps_nfc_dh_privkey =
9773 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9774 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9775 hapd->conf->wps_nfc_dev_pw =
9776 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9777 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9778
9779 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9780 wpa_dbg(iface, MSG_DEBUG,
9781 "P2P: Failed to enable NFC Tag for GO");
9782 }
9783 }
9784 p2p_set_authorized_oob_dev_pw_id(
9785 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9786 if_addr);
9787
9788 return 0;
9789}
9790
9791#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009792
9793
9794static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9795 struct wpa_used_freq_data *freqs,
9796 unsigned int num)
9797{
9798 u8 curr_chan, cand, chan;
9799 unsigned int i;
9800
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009801 /*
9802 * If possible, optimize the Listen channel to be a channel that is
9803 * already used by one of the other interfaces.
9804 */
9805 if (!wpa_s->conf->p2p_optimize_listen_chan)
9806 return;
9807
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009808 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9809 for (i = 0, cand = 0; i < num; i++) {
9810 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9811 if (curr_chan == chan) {
9812 cand = 0;
9813 break;
9814 }
9815
9816 if (chan == 1 || chan == 6 || chan == 11)
9817 cand = chan;
9818 }
9819
9820 if (cand) {
9821 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009822 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009823 cand);
9824 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9825 }
9826}
9827
9828
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009829static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009830{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009831 struct hostapd_config *conf;
9832 struct p2p_go_neg_results params;
9833 struct csa_settings csa_settings;
9834 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9835 int old_freq = current_ssid->frequency;
9836 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009837
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009838 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9839 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9840 return -1;
9841 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009842
9843 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009844 * TODO: This function may not always work correctly. For example,
9845 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009846 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009847 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9848 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009849 wpa_dbg(wpa_s, MSG_DEBUG,
9850 "P2P CSA: Failed to select new frequency for GO");
9851 return -1;
9852 }
9853
9854 if (current_ssid->frequency == params.freq) {
9855 wpa_dbg(wpa_s, MSG_DEBUG,
9856 "P2P CSA: Selected same frequency - not moving GO");
9857 return 0;
9858 }
9859
9860 conf = hostapd_config_defaults();
9861 if (!conf) {
9862 wpa_dbg(wpa_s, MSG_DEBUG,
9863 "P2P CSA: Failed to allocate default config");
9864 return -1;
9865 }
9866
9867 current_ssid->frequency = params.freq;
9868 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9869 wpa_dbg(wpa_s, MSG_DEBUG,
9870 "P2P CSA: Failed to create new GO config");
9871 ret = -1;
9872 goto out;
9873 }
9874
Sunil4a3f9f52022-07-19 22:04:39 +00009875 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9876 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009877 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009878 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9879 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009880 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9881 wpa_s->ap_iface->current_mode->mode,
9882 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9883 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009884 ret = -1;
9885 goto out;
9886 }
9887
9888 os_memset(&csa_settings, 0, sizeof(csa_settings));
9889 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9890 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009891 csa_settings.link_id = -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009892 csa_settings.freq_params.freq = params.freq;
9893 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9894 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9895 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9896
9897 if (conf->ieee80211ac) {
9898 int freq1 = 0, freq2 = 0;
9899 u8 chan, opclass;
9900
9901 if (ieee80211_freq_to_channel_ext(params.freq,
9902 conf->secondary_channel,
9903 conf->vht_oper_chwidth,
9904 &opclass, &chan) ==
9905 NUM_HOSTAPD_MODES) {
9906 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9907 ret = -1;
9908 goto out;
9909 }
9910
9911 if (conf->vht_oper_centr_freq_seg0_idx)
9912 freq1 = ieee80211_chan_to_freq(
9913 NULL, opclass,
9914 conf->vht_oper_centr_freq_seg0_idx);
9915
9916 if (conf->vht_oper_centr_freq_seg1_idx)
9917 freq2 = ieee80211_chan_to_freq(
9918 NULL, opclass,
9919 conf->vht_oper_centr_freq_seg1_idx);
9920
9921 if (freq1 < 0 || freq2 < 0) {
9922 wpa_dbg(wpa_s, MSG_DEBUG,
9923 "P2P CSA: Selected invalid VHT center freqs");
9924 ret = -1;
9925 goto out;
9926 }
9927
9928 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9929 csa_settings.freq_params.center_freq1 = freq1;
9930 csa_settings.freq_params.center_freq2 = freq2;
9931
9932 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009933 case CONF_OPER_CHWIDTH_80MHZ:
9934 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009935 csa_settings.freq_params.bandwidth = 80;
9936 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009937 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009938 csa_settings.freq_params.bandwidth = 160;
9939 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009940 default:
9941 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009942 }
9943 }
9944
9945 ret = ap_switch_channel(wpa_s, &csa_settings);
9946out:
9947 current_ssid->frequency = old_freq;
9948 hostapd_config_free(conf);
9949 return ret;
9950}
9951
9952
9953static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9954{
9955 struct p2p_go_neg_results params;
9956 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009957 void (*ap_configured_cb)(void *ctx, void *data);
9958 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009959
9960 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9961
9962 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9963 current_ssid->frequency);
9964
9965 /* Stop the AP functionality */
9966 /* TODO: Should do this in a way that does not indicated to possible
9967 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009968 /* If this action occurs before a group is started, the callback should
9969 * be preserved, or GROUP-STARTED event would be lost. If this action
9970 * occurs after a group is started, these pointers are all NULL and
9971 * harmless. */
9972 ap_configured_cb = wpa_s->ap_configured_cb;
9973 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9974 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009975 wpa_supplicant_ap_deinit(wpa_s);
9976
9977 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009978 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9979 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9981 wpas_p2p_group_delete(wpa_s,
9982 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9983 return;
9984 }
9985 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9986 params.freq);
9987
9988 if (params.freq &&
9989 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9990 wpa_printf(MSG_DEBUG,
9991 "P2P: Selected freq (%u MHz) is not valid for P2P",
9992 params.freq);
9993 wpas_p2p_group_delete(wpa_s,
9994 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9995 return;
9996 }
9997
Hai Shalom899fcc72020-10-19 14:38:18 -07009998 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009999 wpa_s->ap_configured_cb = ap_configured_cb;
10000 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
10001 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -070010002
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010003 /* Update the frequency */
10004 current_ssid->frequency = params.freq;
10005 wpa_s->connect_without_scan = current_ssid;
10006 wpa_s->reassociate = 1;
10007 wpa_s->disconnected = 0;
10008 wpa_supplicant_req_scan(wpa_s, 0, 0);
10009}
10010
10011
10012static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
10013{
10014 struct wpa_supplicant *wpa_s = eloop_ctx;
10015
10016 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010017 return;
10018
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010019 wpas_p2p_go_update_common_freqs(wpa_s);
10020
10021 /* Do not move GO in the middle of a CSA */
10022 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10023 wpa_printf(MSG_DEBUG,
10024 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010025 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010026 }
10027
10028 /*
10029 * First, try a channel switch flow. If it is not supported or fails,
10030 * take down the GO and bring it up again.
10031 */
10032 if (wpas_p2p_move_go_csa(wpa_s) < 0)
10033 wpas_p2p_move_go_no_csa(wpa_s);
10034}
10035
10036
10037static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
10038{
10039 struct wpa_supplicant *wpa_s = eloop_ctx;
10040 struct wpa_used_freq_data *freqs = NULL;
10041 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010042
10043 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
10044 if (!freqs)
10045 return;
10046
Sunil Ravi77d572f2023-01-17 23:58:31 +000010047 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010048
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010049 /* Previous attempt to move a GO was not possible -- try again. */
10050 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
10051 WPAS_P2P_CHANNEL_UPDATE_ANY);
10052
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010053 os_free(freqs);
10054}
10055
10056
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010057/*
10058 * Consider moving a GO from its currently used frequency:
10059 * 1. It is possible that due to regulatory consideration the frequency
10060 * can no longer be used and there is a need to evacuate the GO.
10061 * 2. It is possible that due to MCC considerations, it would be preferable
10062 * to move the GO to a channel that is currently used by some other
10063 * station interface.
10064 *
10065 * In case a frequency that became invalid is once again valid, cancel a
10066 * previously initiated GO frequency change.
10067 */
10068static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
10069 struct wpa_used_freq_data *freqs,
10070 unsigned int num)
10071{
10072 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10073 unsigned int timeout;
10074 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010075 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010076
10077 wpas_p2p_go_update_common_freqs(wpa_s);
10078
10079 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010080 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010081 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010082 for (i = 0, invalid_freq = 0; i < num; i++) {
10083 if (freqs[i].freq == freq) {
10084 flags = freqs[i].flags;
10085
10086 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010087 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10088 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010089 wpa_dbg(wpa_s, MSG_DEBUG,
10090 "P2P: Freq=%d MHz no longer valid for GO",
10091 freq);
10092 invalid_freq = 1;
10093 }
10094 } else if (freqs[i].flags == 0) {
10095 /* Freq is not used by any other station interface */
10096 continue;
10097 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010098 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010099 /* Freq is not valid for P2P use cases */
10100 continue;
10101 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10102 P2P_GO_FREQ_MOVE_SCM) {
10103 policy_move = 1;
10104 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10105 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10106 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10107 policy_move = 1;
10108 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10109 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10110 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10111 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10112 policy_move = 1;
10113 } else if ((wpa_s->drv_flags &
10114 WPA_DRIVER_FLAGS_AP_CSA) &&
10115 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10116 u8 chan;
10117
10118 /*
10119 * We do not support CSA between bands, so move
10120 * GO only within the same band.
10121 */
10122 if (wpa_s->ap_iface->current_mode->mode ==
10123 ieee80211_freq_to_chan(freqs[i].freq,
10124 &chan))
10125 policy_move = 1;
10126 }
10127 }
10128 }
10129
10130 wpa_dbg(wpa_s, MSG_DEBUG,
10131 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10132 invalid_freq, policy_move, flags);
10133
10134 /*
10135 * The channel is valid, or we are going to have a policy move, so
10136 * cancel timeout.
10137 */
10138 if (!invalid_freq || policy_move) {
10139 wpa_dbg(wpa_s, MSG_DEBUG,
10140 "P2P: Cancel a GO move from freq=%d MHz", freq);
10141 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10142
10143 if (wpas_p2p_in_progress(wpa_s)) {
10144 wpa_dbg(wpa_s, MSG_DEBUG,
10145 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10146 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10147 wpa_s, NULL);
10148 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10149 wpas_p2p_reconsider_moving_go,
10150 wpa_s, NULL);
10151 return;
10152 }
10153 }
10154
10155 if (!invalid_freq && (!policy_move || flags != 0)) {
10156 wpa_dbg(wpa_s, MSG_DEBUG,
10157 "P2P: Not initiating a GO frequency change");
10158 return;
10159 }
10160
10161 /*
10162 * Do not consider moving GO if it is in the middle of a CSA. When the
10163 * CSA is finished this flow should be retriggered.
10164 */
10165 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10166 wpa_dbg(wpa_s, MSG_DEBUG,
10167 "P2P: Not initiating a GO frequency change - CSA is in progress");
10168 return;
10169 }
10170
10171 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10172 timeout = P2P_GO_FREQ_CHANGE_TIME;
10173 else
10174 timeout = 0;
10175
10176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10177 freq, timeout);
10178 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10179 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10180}
10181
10182
10183static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10184 struct wpa_used_freq_data *freqs,
10185 unsigned int num,
10186 enum wpas_p2p_channel_update_trig trig)
10187{
10188 struct wpa_supplicant *ifs;
10189
10190 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10191 NULL);
10192
10193 /*
10194 * Travers all the radio interfaces, and for each GO interface, check
10195 * if there is a need to move the GO from the frequency it is using,
10196 * or in case the frequency is valid again, cancel the evacuation flow.
10197 */
10198 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10199 radio_list) {
10200 if (ifs->current_ssid == NULL ||
10201 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10202 continue;
10203
10204 /*
10205 * The GO was just started or completed channel switch, no need
10206 * to move it.
10207 */
10208 if (wpa_s == ifs &&
10209 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10210 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10211 wpa_dbg(wpa_s, MSG_DEBUG,
10212 "P2P: GO move - schedule re-consideration");
10213 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10214 wpas_p2p_reconsider_moving_go,
10215 wpa_s, NULL);
10216 continue;
10217 }
10218
10219 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10220 }
10221}
10222
10223
10224void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10225{
10226 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10227 return;
10228
10229 wpas_p2p_update_channel_list(wpa_s,
10230 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10231}
10232
10233
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010234void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10235{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010236 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10237 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10238 "the management interface is being removed");
10239 wpas_p2p_deinit_global(wpa_s->global);
10240 }
10241}
10242
10243
10244void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10245{
10246 if (wpa_s->ap_iface->bss)
10247 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10248 wpas_p2p_group_deinit(wpa_s);
10249}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010250
10251
10252int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10253 unsigned int period, unsigned int interval,
10254 unsigned int count)
10255{
10256 struct p2p_data *p2p = wpa_s->global->p2p;
10257 u8 *device_types;
10258 size_t dev_types_len;
10259 struct wpabuf *buf;
10260 int ret;
10261
10262 if (wpa_s->p2p_lo_started) {
10263 wpa_dbg(wpa_s, MSG_DEBUG,
10264 "P2P Listen offload is already started");
10265 return 0;
10266 }
10267
10268 if (wpa_s->global->p2p == NULL ||
10269 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10270 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10271 return -1;
10272 }
10273
10274 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10275 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10276 freq);
10277 return -1;
10278 }
10279
10280 /* Get device type */
10281 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10282 WPS_DEV_TYPE_LEN;
10283 device_types = os_malloc(dev_types_len);
10284 if (!device_types)
10285 return -1;
10286 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10287 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10288 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10289
10290 /* Get Probe Response IE(s) */
10291 buf = p2p_build_probe_resp_template(p2p, freq);
10292 if (!buf) {
10293 os_free(device_types);
10294 return -1;
10295 }
10296
10297 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10298 device_types, dev_types_len,
10299 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10300 if (ret < 0)
10301 wpa_dbg(wpa_s, MSG_DEBUG,
10302 "P2P: Failed to start P2P listen offload");
10303
10304 os_free(device_types);
10305 wpabuf_free(buf);
10306
10307 if (ret == 0) {
10308 wpa_s->p2p_lo_started = 1;
10309
10310 /* Stop current P2P listen if any */
10311 wpas_stop_listen(wpa_s);
10312 }
10313
10314 return ret;
10315}
10316
10317
10318int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10319{
10320 int ret;
10321
10322 if (!wpa_s->p2p_lo_started)
10323 return 0;
10324
10325 ret = wpa_drv_p2p_lo_stop(wpa_s);
10326 if (ret < 0)
10327 wpa_dbg(wpa_s, MSG_DEBUG,
10328 "P2P: Failed to stop P2P listen offload");
10329
10330 wpa_s->p2p_lo_started = 0;
10331 return ret;
10332}