blob: 3c121c825a49233e037b745429f3d8b5987c7505 [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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001106 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107}
1108
1109
1110static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1111 u8 *go_dev_addr,
1112 const u8 *ssid, size_t ssid_len)
1113{
1114 struct wpa_bss *bss;
1115 const u8 *bssid;
1116 struct wpabuf *p2p;
1117 u8 group_capab;
1118 const u8 *addr;
1119
1120 if (wpa_s->go_params)
1121 bssid = wpa_s->go_params->peer_interface_addr;
1122 else
1123 bssid = wpa_s->bssid;
1124
1125 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001126 if (bss == NULL && wpa_s->go_params &&
1127 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1128 bss = wpa_bss_get_p2p_dev_addr(
1129 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 if (bss == NULL) {
1131 u8 iface_addr[ETH_ALEN];
1132 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1133 iface_addr) == 0)
1134 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1135 }
1136 if (bss == NULL) {
1137 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1138 "group is persistent - BSS " MACSTR " not found",
1139 MAC2STR(bssid));
1140 return 0;
1141 }
1142
1143 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001144 if (p2p == NULL)
1145 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1146 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 if (p2p == NULL) {
1148 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1149 "group is persistent - BSS " MACSTR
1150 " did not include P2P IE", MAC2STR(bssid));
1151 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
Hai Shalom60840252021-02-19 19:02:11 -08001152 wpa_bss_ie_ptr(bss), bss->ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 wpa_hexdump(MSG_DEBUG, "P2P: Beacon 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 bss->beacon_ie_len);
1156 return 0;
1157 }
1158
1159 group_capab = p2p_get_group_capab(p2p);
1160 addr = p2p_get_go_dev_addr(p2p);
1161 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1162 "group_capab=0x%x", group_capab);
1163 if (addr) {
1164 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1165 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1166 MAC2STR(addr));
1167 } else
1168 os_memset(go_dev_addr, 0, ETH_ALEN);
1169 wpabuf_free(p2p);
1170
1171 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1172 "go_dev_addr=" MACSTR,
1173 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1174
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001175 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176}
1177
1178
Jouni Malinen75ecf522011-06-27 15:19:46 -07001179static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1180 struct wpa_ssid *ssid,
1181 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182{
1183 struct wpa_ssid *s;
1184 int changed = 0;
1185
1186 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1187 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1188 for (s = wpa_s->conf->ssid; s; s = s->next) {
1189 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001190 ether_addr_equal(go_dev_addr, s->bssid) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 s->ssid_len == ssid->ssid_len &&
1192 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1193 break;
1194 }
1195
1196 if (s) {
1197 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1198 "entry");
1199 if (ssid->passphrase && !s->passphrase)
1200 changed = 1;
1201 else if (ssid->passphrase && s->passphrase &&
1202 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1203 changed = 1;
1204 } else {
1205 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1206 "entry");
1207 changed = 1;
1208 s = wpa_config_add_network(wpa_s->conf);
1209 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001210 return -1;
1211
1212 /*
1213 * Instead of network_added we emit persistent_group_added
1214 * notification. Also to keep the defense checks in
1215 * persistent_group obj registration method, we set the
1216 * relevant flags in s to designate it as a persistent group.
1217 */
1218 s->p2p_group = 1;
1219 s->p2p_persistent_group = 1;
1220 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 wpa_config_set_network_defaults(s);
1222 }
1223
1224 s->p2p_group = 1;
1225 s->p2p_persistent_group = 1;
1226 s->disabled = 2;
1227 s->bssid_set = 1;
1228 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1229 s->mode = ssid->mode;
1230 s->auth_alg = WPA_AUTH_ALG_OPEN;
1231 s->key_mgmt = WPA_KEY_MGMT_PSK;
1232 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001233 s->pbss = ssid->pbss;
1234 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 s->export_keys = 1;
1236 if (ssid->passphrase) {
1237 os_free(s->passphrase);
1238 s->passphrase = os_strdup(ssid->passphrase);
1239 }
1240 if (ssid->psk_set) {
1241 s->psk_set = 1;
1242 os_memcpy(s->psk, ssid->psk, 32);
1243 }
1244 if (s->passphrase && !s->psk_set)
1245 wpa_config_update_psk(s);
1246 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1247 os_free(s->ssid);
1248 s->ssid = os_malloc(ssid->ssid_len);
1249 }
1250 if (s->ssid) {
1251 s->ssid_len = ssid->ssid_len;
1252 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1253 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001254 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1255 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1256 wpa_s->global->add_psk = NULL;
1257 changed = 1;
1258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 if (changed && wpa_s->conf->update_config &&
1261 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1262 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1263 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001264
1265 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266}
1267
1268
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001269static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1270 const u8 *addr)
1271{
1272 struct wpa_ssid *ssid, *s;
1273 u8 *n;
1274 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001275 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001276 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001277
1278 ssid = wpa_s->current_ssid;
1279 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1280 !ssid->p2p_persistent_group)
1281 return;
1282
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001283 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001284 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1285 continue;
1286
1287 if (s->ssid_len == ssid->ssid_len &&
1288 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1289 break;
1290 }
1291
1292 if (s == NULL)
1293 return;
1294
1295 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001296 if (!ether_addr_equal(s->p2p_client_list + i * 2 * ETH_ALEN,
1297 addr))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001298 continue;
1299
1300 if (i == s->num_p2p_clients - 1)
1301 return; /* already the most recent entry */
1302
1303 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001304 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1305 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1306 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001307 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001308 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1309 ETH_ALEN);
1310 os_memset(s->p2p_client_list +
1311 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1312 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001313 found = 1;
1314 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001315 }
1316
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001317 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1318 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001319 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001320 if (n == NULL)
1321 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001322 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1323 ETH_ALEN);
1324 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1325 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001326 s->p2p_client_list = n;
1327 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001328 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001329 /* Not enough room for an additional entry - drop the oldest
1330 * entry */
1331 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001332 s->p2p_client_list + 2 * ETH_ALEN,
1333 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001334 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001335 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001336 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001337 os_memset(s->p2p_client_list +
1338 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1339 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001340 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001341
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001342 if (p2p_wpa_s->conf->update_config &&
1343 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001344 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001345}
1346
1347
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001348static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1349 int go, struct wpa_ssid *ssid, int freq,
1350 const u8 *psk, const char *passphrase,
1351 const u8 *go_dev_addr, int persistent,
1352 const char *extra)
1353{
1354 const char *ssid_txt;
1355 char psk_txt[65];
1356
1357 if (psk)
1358 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1359 else
1360 psk_txt[0] = '\0';
1361
1362 if (ssid)
1363 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1364 else
1365 ssid_txt = "";
1366
1367 if (passphrase && passphrase[0] == '\0')
1368 passphrase = NULL;
1369
1370 /*
1371 * Include PSK/passphrase only in the control interface message and
1372 * leave it out from the debug log entry.
1373 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001374 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001375 P2P_EVENT_GROUP_STARTED
1376 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1377 MACSTR "%s%s",
1378 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1379 psk ? " psk=" : "", psk_txt,
1380 passphrase ? " passphrase=\"" : "",
1381 passphrase ? passphrase : "",
1382 passphrase ? "\"" : "",
1383 MAC2STR(go_dev_addr),
1384 persistent ? " [PERSISTENT]" : "", extra);
1385 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1386 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1387 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1388 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1389 extra);
1390}
1391
1392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001394 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395{
1396 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 int client;
1398 int persistent;
1399 u8 go_dev_addr[ETH_ALEN];
1400
1401 /*
1402 * This callback is likely called for the main interface. Update wpa_s
1403 * to use the group interface if a new interface was created for the
1404 * group.
1405 */
1406 if (wpa_s->global->p2p_group_formation)
1407 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001408 if (wpa_s->p2p_go_group_formation_completed) {
1409 wpa_s->global->p2p_group_formation = NULL;
1410 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001411 } else if (wpa_s->p2p_in_provisioning && !success) {
1412 wpa_msg(wpa_s, MSG_DEBUG,
1413 "P2P: Stop provisioning state due to failure");
1414 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001415 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001416 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00001417 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001418 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419
1420 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001421 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001422 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001423 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001424 if (already_deleted)
1425 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001426 wpas_p2p_group_delete(wpa_s,
1427 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001428 return;
1429 }
1430
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001431 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001432 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433
1434 ssid = wpa_s->current_ssid;
1435 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1436 ssid->mode = WPAS_MODE_P2P_GO;
1437 p2p_group_notif_formation_done(wpa_s->p2p_group);
1438 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1439 }
1440
1441 persistent = 0;
1442 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 client = ssid->mode == WPAS_MODE_INFRA;
1444 if (ssid->mode == WPAS_MODE_P2P_GO) {
1445 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001446 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1447 ETH_ALEN);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001448 } else {
1449 os_memset(go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 persistent = wpas_p2p_persistent_group(wpa_s,
1451 go_dev_addr,
1452 ssid->ssid,
1453 ssid->ssid_len);
George Burgess IV0d2d1bf2021-11-01 03:08:33 -07001454 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 client = wpa_s->p2p_group_interface ==
1457 P2P_GROUP_INTERFACE_CLIENT;
1458 os_memset(go_dev_addr, 0, ETH_ALEN);
1459 }
1460
1461 wpa_s->show_group_started = 0;
1462 if (client) {
1463 /*
1464 * Indicate event only after successfully completed 4-way
1465 * handshake, i.e., when the interface is ready for data
1466 * packets.
1467 */
1468 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001470 wpas_p2p_group_started(wpa_s, 1, ssid,
1471 ssid ? ssid->frequency : 0,
1472 ssid && ssid->passphrase == NULL &&
1473 ssid->psk_set ? ssid->psk : NULL,
1474 ssid ? ssid->passphrase : NULL,
1475 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 wpas_p2p_cross_connect_setup(wpa_s);
1477 wpas_p2p_set_group_idle_timeout(wpa_s);
1478 }
1479
1480 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001481 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1482 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001483 else {
1484 os_free(wpa_s->global->add_psk);
1485 wpa_s->global->add_psk = NULL;
1486 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001487
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001488 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001489 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492}
1493
1494
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495struct send_action_work {
1496 unsigned int freq;
1497 u8 dst[ETH_ALEN];
1498 u8 src[ETH_ALEN];
1499 u8 bssid[ETH_ALEN];
1500 size_t len;
1501 unsigned int wait_time;
1502 u8 buf[0];
1503};
1504
1505
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001506static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1507{
1508 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1509
1510 wpa_printf(MSG_DEBUG,
1511 "P2P: Free Action frame radio work @%p (freq=%u dst="
1512 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1513 wpa_s->p2p_send_action_work, awork->freq,
1514 MAC2STR(awork->dst), MAC2STR(awork->src),
1515 MAC2STR(awork->bssid), awork->wait_time);
1516 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1517 awork->buf, awork->len);
1518 os_free(awork);
1519 wpa_s->p2p_send_action_work->ctx = NULL;
1520 radio_work_done(wpa_s->p2p_send_action_work);
1521 wpa_s->p2p_send_action_work = NULL;
1522}
1523
1524
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001525static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1526 void *timeout_ctx)
1527{
1528 struct wpa_supplicant *wpa_s = eloop_ctx;
1529
1530 if (!wpa_s->p2p_send_action_work)
1531 return;
1532
1533 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001534 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001535}
1536
1537
Dmitry Shmidt03658832014-08-13 11:03:49 -07001538static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001540 if (wpa_s->p2p_send_action_work) {
1541 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001543 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001544 wpa_printf(MSG_DEBUG,
1545 "P2P: Clear Action TX work @%p (wait_time=%u)",
1546 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001547 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001548 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001549 } else {
1550 /*
1551 * In theory, this should not be needed, but number of
1552 * places in the P2P code is still using non-zero wait
1553 * time for the last Action frame in the sequence and
1554 * some of these do not call send_action_done().
1555 */
1556 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1557 wpa_s, NULL);
1558 eloop_register_timeout(
1559 0, awork->wait_time * 1000,
1560 wpas_p2p_send_action_work_timeout,
1561 wpa_s, NULL);
1562 }
1563 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001564}
1565
1566
1567static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1568 unsigned int freq,
1569 const u8 *dst, const u8 *src,
1570 const u8 *bssid,
1571 const u8 *data, size_t data_len,
1572 enum offchannel_send_action_result
1573 result)
1574{
1575 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1576
1577 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001578
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1580 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001582 switch (result) {
1583 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1584 res = P2P_SEND_ACTION_SUCCESS;
1585 break;
1586 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1587 res = P2P_SEND_ACTION_NO_ACK;
1588 break;
1589 case OFFCHANNEL_SEND_ACTION_FAILED:
1590 res = P2P_SEND_ACTION_FAILED;
1591 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 }
1593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001594 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001596 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1597 wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001598 (ether_addr_equal(dst, wpa_s->pending_join_dev_addr) ||
1599 ether_addr_equal(dst, wpa_s->pending_join_iface_addr)) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001600 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001602 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1603 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001604 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001605 P2P_EVENT_FALLBACK_TO_GO_NEG
1606 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001607 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1608 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 }
1610}
1611
1612
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1614{
1615 struct wpa_supplicant *wpa_s = work->wpa_s;
1616 struct send_action_work *awork = work->ctx;
1617
1618 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001619 if (work->started) {
1620 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1621 wpa_s, NULL);
1622 wpa_s->p2p_send_action_work = NULL;
1623 offchannel_send_action_done(wpa_s);
1624 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001625 os_free(awork);
1626 return;
1627 }
1628
1629 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1630 awork->bssid, awork->buf, awork->len,
1631 awork->wait_time,
1632 wpas_p2p_send_action_tx_status, 1) < 0) {
1633 os_free(awork);
1634 radio_work_done(work);
1635 return;
1636 }
1637 wpa_s->p2p_send_action_work = work;
1638}
1639
1640
1641static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1642 unsigned int freq, const u8 *dst,
1643 const u8 *src, const u8 *bssid, const u8 *buf,
1644 size_t len, unsigned int wait_time)
1645{
1646 struct send_action_work *awork;
1647
Hai Shalom81f62d82019-07-22 12:10:00 -07001648 if (radio_work_pending(wpa_s, "p2p-send-action")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001649 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1650 return -1;
1651 }
1652
1653 awork = os_zalloc(sizeof(*awork) + len);
1654 if (awork == NULL)
1655 return -1;
1656
1657 awork->freq = freq;
1658 os_memcpy(awork->dst, dst, ETH_ALEN);
1659 os_memcpy(awork->src, src, ETH_ALEN);
1660 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1661 awork->len = len;
1662 awork->wait_time = wait_time;
1663 os_memcpy(awork->buf, buf, len);
1664
Hai Shalom81f62d82019-07-22 12:10:00 -07001665 if (radio_add_work(wpa_s, freq, "p2p-send-action", 1,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001666 wpas_send_action_cb, awork) < 0) {
1667 os_free(awork);
1668 return -1;
1669 }
1670
1671 return 0;
1672}
1673
1674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1676 const u8 *src, const u8 *bssid, const u8 *buf,
Hai Shalom021b0b52019-04-10 11:17:58 -07001677 size_t len, unsigned int wait_time, int *scheduled)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678{
1679 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001680 int listen_freq = -1, send_freq = -1;
1681
Hai Shalom021b0b52019-04-10 11:17:58 -07001682 if (scheduled)
1683 *scheduled = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001684 if (wpa_s->p2p_listen_work)
1685 listen_freq = wpa_s->p2p_listen_work->freq;
1686 if (wpa_s->p2p_send_action_work)
1687 send_freq = wpa_s->p2p_send_action_work->freq;
1688 if (listen_freq != (int) freq && send_freq != (int) freq) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001689 int res;
1690
1691 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d freq=%u)",
1692 listen_freq, send_freq, freq);
1693 res = wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1694 len, wait_time);
1695 if (res == 0 && scheduled)
1696 *scheduled = 1;
1697 return res;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001698 }
1699
1700 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001701 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1702 wait_time,
1703 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704}
1705
1706
1707static void wpas_send_action_done(void *ctx)
1708{
1709 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001710
1711 if (wpa_s->p2p_send_action_work) {
1712 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1713 wpa_s, NULL);
1714 os_free(wpa_s->p2p_send_action_work->ctx);
1715 radio_work_done(wpa_s->p2p_send_action_work);
1716 wpa_s->p2p_send_action_work = NULL;
1717 }
1718
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001719 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720}
1721
1722
1723static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1724 struct p2p_go_neg_results *params)
1725{
1726 if (wpa_s->go_params == NULL) {
1727 wpa_s->go_params = os_malloc(sizeof(*params));
1728 if (wpa_s->go_params == NULL)
1729 return -1;
1730 }
1731 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1732 return 0;
1733}
1734
1735
1736static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1737 struct p2p_go_neg_results *res)
1738{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001739 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001740 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1741 " dev_addr " MACSTR " wps_method %d",
1742 MAC2STR(res->peer_interface_addr),
1743 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1745 res->ssid, res->ssid_len);
1746 wpa_supplicant_ap_deinit(wpa_s);
1747 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001748 if (res->wps_method == WPS_PBC) {
Hai Shalom021b0b52019-04-10 11:17:58 -07001749 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001750#ifdef CONFIG_WPS_NFC
1751 } else if (res->wps_method == WPS_NFC) {
1752 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1753 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001754 wpa_s->p2pdev->p2p_oob_dev_pw,
1755 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1756 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001757 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001758 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001759 NULL,
1760 NULL, 0, 0);
1761#endif /* CONFIG_WPS_NFC */
1762 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001764 if (wpa_s->p2p_wps_method == WPS_P2PS)
1765 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1767 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1768 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1769 wpa_s->p2p_pin, 1, dev_pw_id);
1770 }
1771}
1772
1773
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001774static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1775 struct wpa_ssid *ssid)
1776{
1777 struct wpa_ssid *persistent;
1778 struct psk_list_entry *psk;
1779 struct hostapd_data *hapd;
1780
1781 if (!wpa_s->ap_iface)
1782 return;
1783
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001784 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001785 ssid->ssid_len);
1786 if (persistent == NULL)
1787 return;
1788
1789 hapd = wpa_s->ap_iface->bss[0];
1790
1791 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1792 list) {
1793 struct hostapd_wpa_psk *hpsk;
1794
1795 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1796 MACSTR " psk=%d",
1797 MAC2STR(psk->addr), psk->p2p);
1798 hpsk = os_zalloc(sizeof(*hpsk));
1799 if (hpsk == NULL)
1800 break;
1801 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1802 if (psk->p2p)
1803 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1804 else
1805 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1806 hpsk->next = hapd->conf->ssid.wpa_psk;
1807 hapd->conf->ssid.wpa_psk = hpsk;
1808 }
1809}
1810
1811
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1813{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001814 char buf[20 + P2P_MAX_CHANNELS * 6];
1815 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001816 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001817 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001818
Roshan Pius3a1667e2018-07-03 15:17:14 -07001819 pos = buf;
1820 end = pos + sizeof(buf);
1821 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1822 res = os_snprintf(pos, end - pos, " %d",
1823 wpa_s->p2p_group_common_freqs[i]);
1824 if (os_snprintf_error(end - pos, res))
1825 break;
1826 pos += res;
1827 }
1828 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001829
Roshan Pius3a1667e2018-07-03 15:17:14 -07001830 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831}
1832
1833
1834static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1835 struct p2p_go_neg_results *params)
1836{
1837 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1838
1839 wpa_s->p2p_group_common_freqs_num = 0;
1840 os_free(wpa_s->p2p_group_common_freqs);
1841 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1842 if (!wpa_s->p2p_group_common_freqs)
1843 return;
1844
1845 for (i = 0; i < len; i++) {
1846 if (!wpa_s->go_params->freq_list[i])
1847 break;
1848 wpa_s->p2p_group_common_freqs[i] =
1849 wpa_s->go_params->freq_list[i];
1850 }
1851 wpa_s->p2p_group_common_freqs_num = i;
1852}
1853
1854
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001855static void p2p_config_write(struct wpa_supplicant *wpa_s)
1856{
1857#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001858 if (wpa_s->p2pdev->conf->update_config &&
1859 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001860 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1861#endif /* CONFIG_NO_CONFIG_WRITE */
1862}
1863
1864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865static void p2p_go_configured(void *ctx, void *data)
1866{
1867 struct wpa_supplicant *wpa_s = ctx;
1868 struct p2p_go_neg_results *params = data;
1869 struct wpa_ssid *ssid;
1870
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001871 wpa_s->ap_configured_cb = NULL;
1872 wpa_s->ap_configured_cb_ctx = NULL;
1873 wpa_s->ap_configured_cb_data = NULL;
1874 if (!wpa_s->go_params) {
1875 wpa_printf(MSG_ERROR,
1876 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1877 return;
1878 }
1879
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 p2p_go_save_group_common_freqs(wpa_s, params);
1881 p2p_go_dump_common_freqs(wpa_s);
1882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883 ssid = wpa_s->current_ssid;
1884 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1885 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1886 if (wpa_s->global->p2p_group_formation == wpa_s)
1887 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001888 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1889 params->passphrase[0] == '\0' ?
1890 params->psk : NULL,
1891 params->passphrase,
1892 wpa_s->global->p2p_dev_addr,
1893 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001894 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001896 if (wpa_s->p2pdev->p2ps_method_config_any) {
1897 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001898 wpa_dbg(wpa_s, MSG_DEBUG,
1899 "P2PS: Setting default PIN for ANY");
1900 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1901 "12345670", NULL, 0,
1902 0);
1903 } else {
1904 wpa_dbg(wpa_s, MSG_DEBUG,
1905 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001906 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001907 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001908 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001909 "12345670", NULL, 0, 0);
1910 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001911 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001912 }
1913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001914 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001915 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001916 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001917 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001918 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001919 wpas_p2p_add_psk_list(wpa_s, ssid);
1920 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001921
1922 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001923 params->persistent_group, 0,
1924 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 wpas_p2p_cross_connect_setup(wpa_s);
1926 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001927
1928 if (wpa_s->p2p_first_connection_timeout) {
1929 wpa_dbg(wpa_s, MSG_DEBUG,
1930 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1931 wpa_s->p2p_first_connection_timeout);
1932 wpa_s->p2p_go_group_formation_completed = 0;
1933 wpa_s->global->p2p_group_formation = wpa_s;
1934 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001935 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001936 eloop_register_timeout(
1937 wpa_s->p2p_first_connection_timeout, 0,
1938 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001939 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001940 }
1941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 return;
1943 }
1944
1945 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1946 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1947 params->peer_interface_addr)) {
1948 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1949 "filtering");
1950 return;
1951 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001952 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001954 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001955#ifdef CONFIG_WPS_NFC
1956 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001957 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001958 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001959 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001960 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1961 return;
1962 }
1963 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001964 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1965 wpa_s->p2pdev->p2p_oob_dev_pw,
1966 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1967 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001968#endif /* CONFIG_WPS_NFC */
1969 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001971 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 os_free(wpa_s->go_params);
1973 wpa_s->go_params = NULL;
1974}
1975
1976
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001977/**
1978 * wpas_p2p_freq_to_edmg_channel - Convert frequency into EDMG channel
1979 * @freq: Frequency (MHz) to convert
1980 * @op_class: Buffer for returning operating class
1981 * @op_edmg_channel: Buffer for returning channel number
1982 * Returns: 0 on success, -1 on failure
1983 *
1984 * This can be used to find the highest channel bonding which includes the
1985 * specified frequency.
1986 */
1987static int wpas_p2p_freq_to_edmg_channel(struct wpa_supplicant *wpa_s,
1988 unsigned int freq,
1989 u8 *op_class, u8 *op_edmg_channel)
1990{
1991 struct hostapd_hw_modes *hwmode;
1992 struct ieee80211_edmg_config edmg;
1993 unsigned int i;
1994 enum chan_width chanwidth[] = {
1995 CHAN_WIDTH_8640,
1996 CHAN_WIDTH_6480,
1997 CHAN_WIDTH_4320,
1998 };
1999
2000 if (!wpa_s->hw.modes)
2001 return -1;
2002
2003 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalom60840252021-02-19 19:02:11 -08002004 HOSTAPD_MODE_IEEE80211AD, false);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002005 if (!hwmode) {
2006 wpa_printf(MSG_ERROR,
2007 "Unsupported AP mode: HOSTAPD_MODE_IEEE80211AD");
2008 return -1;
2009 }
2010
2011 /* Find the highest EDMG channel bandwidth to start the P2P GO */
2012 for (i = 0; i < ARRAY_SIZE(chanwidth); i++) {
2013 if (ieee80211_chaninfo_to_channel(freq, chanwidth[i], 0,
2014 op_class,
2015 op_edmg_channel) < 0)
2016 continue;
2017
2018 hostapd_encode_edmg_chan(1, *op_edmg_channel, 0, &edmg);
2019 if (edmg.channels &&
2020 ieee802_edmg_is_allowed(hwmode->edmg, edmg)) {
2021 wpa_printf(MSG_DEBUG,
2022 "Freq %u to EDMG channel %u at opclass %u",
2023 freq, *op_edmg_channel, *op_class);
2024 return 0;
2025 }
2026 }
2027
2028 return -1;
2029}
2030
2031
2032int wpas_p2p_try_edmg_channel(struct wpa_supplicant *wpa_s,
2033 struct p2p_go_neg_results *params)
2034{
2035 u8 op_channel, op_class;
2036 int freq;
2037
2038 /* Try social channel as primary channel frequency */
2039 freq = (!params->freq) ? 58320 + 1 * 2160 : params->freq;
2040
2041 if (wpas_p2p_freq_to_edmg_channel(wpa_s, freq, &op_class,
2042 &op_channel) == 0) {
2043 wpa_printf(MSG_DEBUG,
2044 "Freq %d will be used to set an EDMG connection (channel=%u opclass=%u)",
2045 freq, op_channel, op_class);
2046 params->freq = freq;
2047 return 0;
2048 }
2049
2050 return -1;
2051}
2052
2053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
2055 struct p2p_go_neg_results *params,
2056 int group_formation)
2057{
2058 struct wpa_ssid *ssid;
2059
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
2061 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
2062 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
2063 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002065 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066
2067 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002068 if (ssid == NULL) {
2069 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002074 wpa_s->p2p_go_group_formation_completed = 0;
2075 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002076 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 wpa_config_set_network_defaults(ssid);
2079 ssid->temporary = 1;
2080 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07002081 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
2083 WPAS_MODE_P2P_GO;
2084 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002085 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002086 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002087 ssid->max_oper_chwidth = params->max_oper_chwidth;
2088 ssid->vht_center_freq2 = params->vht_center_freq2;
Hai Shalom74f70d42019-02-11 14:42:39 -08002089 ssid->he = params->he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002090 if (params->edmg) {
2091 u8 op_channel, op_class;
2092
2093 if (!wpas_p2p_freq_to_edmg_channel(wpa_s, params->freq,
2094 &op_class, &op_channel)) {
2095 ssid->edmg_channel = op_channel;
2096 ssid->enable_edmg = params->edmg;
2097 } else {
2098 wpa_dbg(wpa_s, MSG_DEBUG,
2099 "P2P: Could not match EDMG channel, freq %d, for GO",
2100 params->freq);
2101 }
2102 }
2103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 ssid->ssid = os_zalloc(params->ssid_len + 1);
2105 if (ssid->ssid) {
2106 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
2107 ssid->ssid_len = params->ssid_len;
2108 }
2109 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
2110 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Hai Shalomc1a21442022-02-04 13:43:00 -08002111 if (is_6ghz_freq(ssid->frequency) &&
2112 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
2113 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
2114 ssid->key_mgmt = WPA_KEY_MGMT_SAE;
2115 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002116 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
Hai Shalomc1a21442022-02-04 13:43:00 -08002117 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use SAE auth_alg and key_mgmt");
2118 } else {
2119 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
2120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121 ssid->proto = WPA_PROTO_RSN;
2122 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002123 ssid->group_cipher = WPA_CIPHER_CCMP;
2124 if (params->freq > 56160) {
2125 /*
2126 * Enable GCMP instead of CCMP as pairwise_cipher and
2127 * group_cipher in 60 GHz.
2128 */
2129 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
2130 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002131 /* P2P GO in 60 GHz is always a PCP (PBSS) */
2132 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002133 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002134 if (os_strlen(params->passphrase) > 0) {
2135 ssid->passphrase = os_strdup(params->passphrase);
2136 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002137 wpa_msg_global(wpa_s, MSG_ERROR,
2138 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002139 wpa_config_remove_network(wpa_s->conf, ssid->id);
2140 return;
2141 }
2142 } else
2143 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002144 ssid->psk_set = params->psk_set;
2145 if (ssid->psk_set)
2146 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002147 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002148 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002149 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150
2151 wpa_s->ap_configured_cb = p2p_go_configured;
2152 wpa_s->ap_configured_cb_ctx = wpa_s;
2153 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002154 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002155 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 wpa_s->reassociate = 1;
2157 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07002158 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
2159 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 wpa_supplicant_req_scan(wpa_s, 0, 0);
2161}
2162
2163
2164static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
2165 const struct wpa_supplicant *src)
2166{
2167 struct wpa_config *d;
2168 const struct wpa_config *s;
2169
2170 d = dst->conf;
2171 s = src->conf;
2172
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002173#define C(n) \
2174do { \
2175 if (s->n && !d->n) \
2176 d->n = os_strdup(s->n); \
2177} while (0)
2178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 C(device_name);
2180 C(manufacturer);
2181 C(model_name);
2182 C(model_number);
2183 C(serial_number);
2184 C(config_methods);
2185#undef C
2186
2187 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
2188 os_memcpy(d->sec_device_type, s->sec_device_type,
2189 sizeof(d->sec_device_type));
2190 d->num_sec_device_types = s->num_sec_device_types;
2191
2192 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002193 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002195 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002196 d->max_num_sta = s->max_num_sta;
2197 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002198 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002199 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002200 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002201 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002202 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002203 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002204 d->passive_scan = s->passive_scan;
Hai Shalomc1a21442022-02-04 13:43:00 -08002205 d->pmf = s->pmf;
2206 d->p2p_6ghz_disable = s->p2p_6ghz_disable;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002207 d->sae_pwe = s->sae_pwe;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002208
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002209 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2210 !d->wps_nfc_pw_from_config) {
2211 wpabuf_free(d->wps_nfc_dh_privkey);
2212 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002213 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2214 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2215 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002216 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217 d->go_interworking = s->go_interworking;
2218 d->go_access_network_type = s->go_access_network_type;
2219 d->go_internet = s->go_internet;
2220 d->go_venue_group = s->go_venue_group;
2221 d->go_venue_type = s->go_venue_type;
Hai Shalom60840252021-02-19 19:02:11 -08002222 d->p2p_add_cli_chan = s->p2p_add_cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223}
2224
2225
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002226static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2227 char *ifname, size_t len)
2228{
2229 char *ifname_ptr = wpa_s->ifname;
2230
2231 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2232 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2233 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2234 }
2235
2236 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2237 if (os_strlen(ifname) >= IFNAMSIZ &&
2238 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002239 int res;
2240
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002241 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002242 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2243 if (os_snprintf_error(len, res) && len)
2244 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002245 }
2246}
2247
2248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2250 enum wpa_driver_if_type type)
2251{
2252 char ifname[120], force_ifname[120];
2253
2254 if (wpa_s->pending_interface_name[0]) {
2255 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2256 "- skip creation of a new one");
2257 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2258 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2259 "unknown?! ifname='%s'",
2260 wpa_s->pending_interface_name);
2261 return -1;
2262 }
2263 return 0;
2264 }
2265
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002266 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267 force_ifname[0] = '\0';
2268
2269 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2270 ifname);
2271 wpa_s->p2p_group_idx++;
2272
2273 wpa_s->pending_interface_type = type;
2274 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2275 wpa_s->pending_interface_addr, NULL) < 0) {
2276 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2277 "interface");
2278 return -1;
2279 }
2280
Jimmy Chen36c21992018-11-29 16:46:43 +08002281 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002282 random_mac_addr(wpa_s->pending_interface_addr);
2283 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR
2284 " for the group",
2285 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002286 }
2287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 if (force_ifname[0]) {
2289 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2290 force_ifname);
2291 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2292 sizeof(wpa_s->pending_interface_name));
2293 } else
2294 os_strlcpy(wpa_s->pending_interface_name, ifname,
2295 sizeof(wpa_s->pending_interface_name));
2296 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2297 MACSTR, wpa_s->pending_interface_name,
2298 MAC2STR(wpa_s->pending_interface_addr));
2299
2300 return 0;
2301}
2302
2303
2304static void wpas_p2p_remove_pending_group_interface(
2305 struct wpa_supplicant *wpa_s)
2306{
2307 if (!wpa_s->pending_interface_name[0] ||
2308 is_zero_ether_addr(wpa_s->pending_interface_addr))
2309 return; /* No pending virtual interface */
2310
2311 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2312 wpa_s->pending_interface_name);
2313 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2314 wpa_s->pending_interface_name);
2315 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2316 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002317 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318}
2319
2320
2321static struct wpa_supplicant *
2322wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2323{
2324 struct wpa_interface iface;
2325 struct wpa_supplicant *group_wpa_s;
2326
2327 if (!wpa_s->pending_interface_name[0]) {
2328 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2329 if (!wpas_p2p_create_iface(wpa_s))
2330 return NULL;
2331 /*
2332 * Something has forced us to remove the pending interface; try
2333 * to create a new one and hope for the best that we will get
2334 * the same local address.
2335 */
2336 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2337 WPA_IF_P2P_CLIENT) < 0)
2338 return NULL;
2339 }
2340
2341 os_memset(&iface, 0, sizeof(iface));
2342 iface.ifname = wpa_s->pending_interface_name;
2343 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002344 if (wpa_s->conf->ctrl_interface == NULL &&
2345 wpa_s->parent != wpa_s &&
2346 wpa_s->p2p_mgmt &&
2347 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2348 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2349 else
2350 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002352 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 if (group_wpa_s == NULL) {
2354 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2355 "wpa_supplicant interface");
2356 return NULL;
2357 }
2358 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2360 P2P_GROUP_INTERFACE_CLIENT;
2361 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002362 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363
2364 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2365
Jimmy Chen36c21992018-11-29 16:46:43 +08002366 if (wpa_s->conf->p2p_interface_random_mac_addr) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002367 if (wpa_drv_set_mac_addr(group_wpa_s,
2368 wpa_s->pending_interface_addr) < 0) {
Jimmy Chen36c21992018-11-29 16:46:43 +08002369 wpa_msg(group_wpa_s, MSG_INFO,
2370 "Failed to set random MAC address");
Hai Shalom74f70d42019-02-11 14:42:39 -08002371 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2372 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002373 return NULL;
2374 }
2375
2376 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2377 wpa_msg(group_wpa_s, MSG_INFO,
2378 "Could not update MAC address information");
Hai Shalom74f70d42019-02-11 14:42:39 -08002379 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s,
2380 0);
Jimmy Chen36c21992018-11-29 16:46:43 +08002381 return NULL;
2382 }
2383
Hai Shalom74f70d42019-02-11 14:42:39 -08002384 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR
2385 " for the group",
2386 MAC2STR(wpa_s->pending_interface_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08002387 }
2388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 return group_wpa_s;
2390}
2391
2392
2393static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2394 void *timeout_ctx)
2395{
2396 struct wpa_supplicant *wpa_s = eloop_ctx;
2397 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002398 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002399}
2400
2401
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002402static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2403 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002404{
2405 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002406 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 if (wpa_s->global->p2p)
2408 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002409 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410}
2411
2412
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002413static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2414{
2415 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2416 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002417 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002418 eloop_register_timeout(0, 0, 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 wpa_s->global->p2p_fail_on_wps_complete = 0;
2421}
2422
2423
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002424void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2425{
2426 if (wpa_s->global->p2p_group_formation != wpa_s)
2427 return;
2428 /* Speed up group formation timeout since this cannot succeed */
2429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002430 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002431 eloop_register_timeout(0, 0, 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}
2434
2435
Matthew Wang06b42472022-11-10 06:56:31 +00002436bool wpas_p2p_retry_limit_exceeded(struct wpa_supplicant *wpa_s)
2437{
2438 if (!wpa_s->p2p_in_invitation || !wpa_s->p2p_retry_limit ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00002439 wpa_s->p2p_in_invitation <= wpa_s->p2p_retry_limit)
Matthew Wang06b42472022-11-10 06:56:31 +00002440 return false;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002441
Matthew Wang06b42472022-11-10 06:56:31 +00002442 wpa_printf(MSG_DEBUG, "P2P: Group join retry limit exceeded");
2443 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2444 wpa_s->p2pdev, NULL);
2445 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2446 wpa_s->p2pdev, NULL);
2447 return true;
2448}
2449
2450
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002451static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452{
2453 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002454 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
2456 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2457 wpa_drv_cancel_remain_on_channel(wpa_s);
2458 wpa_s->off_channel_freq = 0;
2459 wpa_s->roc_waiting_drv_freq = 0;
2460 }
2461
2462 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002463 wpa_msg_global(wpa_s, MSG_INFO,
2464 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2465 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002466 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 wpas_p2p_remove_pending_group_interface(wpa_s);
2468 return;
2469 }
2470
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002471 if (!res->role_go) {
2472 /* Inform driver of the operating channel of GO. */
2473 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2474 }
2475
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002476 if (wpa_s->p2p_go_ht40)
2477 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002478 if (wpa_s->p2p_go_vht)
2479 res->vht = 1;
Hai Shalomc3565922019-10-28 11:58:20 -07002480 if (wpa_s->p2p_go_he)
2481 res->he = 1;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002482 if (wpa_s->p2p_go_edmg)
2483 res->edmg = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002484 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2485 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002486
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002487 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2488 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2489 " wps_method=%s",
2490 res->role_go ? "GO" : "client", res->freq, res->ht40,
2491 MAC2STR(res->peer_device_addr),
2492 MAC2STR(res->peer_interface_addr),
2493 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002494 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2497 struct wpa_ssid *ssid;
2498 ssid = wpa_config_get_network(wpa_s->conf,
2499 wpa_s->p2p_persistent_id);
2500 if (ssid && ssid->disabled == 2 &&
2501 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2502 size_t len = os_strlen(ssid->passphrase);
2503 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2504 "on requested persistent group");
2505 os_memcpy(res->passphrase, ssid->passphrase, len);
2506 res->passphrase[len] = '\0';
2507 }
2508 }
2509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002511 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2513 if (group_wpa_s == NULL) {
2514 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002515 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2516 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002517 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 return;
2519 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2521 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002523 group_wpa_s = wpa_s->parent;
2524 wpa_s->global->p2p_group_formation = group_wpa_s;
2525 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002526 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002527 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002529 group_wpa_s->p2p_in_provisioning = 1;
2530 group_wpa_s->p2pdev = wpa_s;
2531 if (group_wpa_s != wpa_s) {
2532 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2533 sizeof(group_wpa_s->p2p_pin));
2534 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2535 }
2536 if (res->role_go) {
2537 wpas_start_wps_go(group_wpa_s, res, 1);
2538 } else {
2539 os_get_reltime(&group_wpa_s->scan_min_time);
2540 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 }
2542
Hai Shalomfdcde762020-04-02 11:19:20 -07002543 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2545
2546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2547 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2548 (res->peer_config_timeout % 100) * 10000,
2549 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2550}
2551
2552
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002553static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2554 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555{
2556 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002557 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002558 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2559 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002561 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562}
2563
2564
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002565static void wpas_dev_found(void *ctx, const u8 *addr,
2566 const struct p2p_peer_info *info,
2567 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002569 u8 *wfd_dev_info = NULL;
2570 u8 wfd_dev_info_len = 0;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002571 u8 *wfd_r2_dev_info = NULL;
Jimmy Chen57e19f52021-03-04 14:19:52 +08002572 u8 wfd_r2_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002573#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 struct wpa_supplicant *wpa_s = ctx;
2575 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002576 char *wfd_dev_info_hex = NULL;
Jimmy Chen0133fc12021-03-04 13:56:11 +08002577 char *wfd_r2_dev_info_hex = NULL;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002578
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002579#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002580 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2581 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002582 if (wfd_dev_info_hex) {
2583 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2584 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2585 // Only used for notification, so not handling error.
2586 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2587 }
Jimmy Chen0133fc12021-03-04 13:56:11 +08002588
2589 wfd_r2_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2590 WFD_SUBELEM_R2_DEVICE_INFO);
2591 if (wfd_r2_dev_info_hex) {
2592 wfd_r2_dev_info_len = strlen(wfd_r2_dev_info_hex) / 2;
2593 wfd_r2_dev_info = os_zalloc(wfd_r2_dev_info_len);
2594 // Only used for notification, so not handling error.
2595 hexstr2bin(wfd_r2_dev_info_hex, wfd_r2_dev_info, wfd_r2_dev_info_len);
2596 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002597#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002598
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002599 if (info->p2ps_instance) {
2600 char str[256];
2601 const u8 *buf = wpabuf_head(info->p2ps_instance);
2602 size_t len = wpabuf_len(info->p2ps_instance);
2603
2604 while (len) {
2605 u32 id;
2606 u16 methods;
2607 u8 str_len;
2608
2609 if (len < 4 + 2 + 1)
2610 break;
2611 id = WPA_GET_LE32(buf);
2612 buf += sizeof(u32);
2613 methods = WPA_GET_BE16(buf);
2614 buf += sizeof(u16);
2615 str_len = *buf++;
2616 if (str_len > len - 4 - 2 - 1)
2617 break;
2618 os_memcpy(str, buf, str_len);
2619 str[str_len] = '\0';
2620 buf += str_len;
2621 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2622
2623 wpa_msg_global(wpa_s, MSG_INFO,
2624 P2P_EVENT_DEVICE_FOUND MACSTR
2625 " p2p_dev_addr=" MACSTR
2626 " pri_dev_type=%s name='%s'"
2627 " config_methods=0x%x"
2628 " dev_capab=0x%x"
2629 " group_capab=0x%x"
2630 " adv_id=%x asp_svc=%s%s",
2631 MAC2STR(addr),
2632 MAC2STR(info->p2p_device_addr),
2633 wps_dev_type_bin2str(
2634 info->pri_dev_type,
2635 devtype, sizeof(devtype)),
2636 info->device_name, methods,
2637 info->dev_capab, info->group_capab,
2638 id, str,
2639 info->vendor_elems ?
2640 " vendor_elems=1" : "");
2641 }
2642 goto done;
2643 }
2644
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002645 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2646 " p2p_dev_addr=" MACSTR
2647 " pri_dev_type=%s name='%s' config_methods=0x%x "
Jimmy Chen0133fc12021-03-04 13:56:11 +08002648 "dev_capab=0x%x group_capab=0x%x%s%s%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002649 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2650 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2651 sizeof(devtype)),
2652 info->device_name, info->config_methods,
2653 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002654 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002655 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Jimmy Chen0133fc12021-03-04 13:56:11 +08002656 wfd_r2_dev_info_hex ? " wfd_r2_dev_info=0x" : "",
2657 wfd_r2_dev_info_hex ? wfd_r2_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002658 info->vendor_elems ? " vendor_elems=1" : "",
2659 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002661done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002662 os_free(wfd_dev_info_hex);
Jimmy Chen0133fc12021-03-04 13:56:11 +08002663 os_free(wfd_r2_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002664#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002665
Roshan Piusfd2fd662017-01-23 13:41:57 -08002666 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
Jimmy Chen0133fc12021-03-04 13:56:11 +08002667 wfd_dev_info_len, wfd_r2_dev_info,
2668 wfd_r2_dev_info_len, new_device);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002669 os_free(wfd_dev_info);
jiangpingpinga96a5e82023-11-16 15:02:17 +08002670 os_free(wfd_r2_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671}
2672
2673
2674static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2675{
2676 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002677
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002678 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2679 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2682}
2683
2684
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002685static void wpas_find_stopped(void *ctx)
2686{
2687 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002688
2689 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2690 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2691
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002692 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002693 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002694}
2695
2696
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002697struct wpas_p2p_listen_work {
2698 unsigned int freq;
2699 unsigned int duration;
2700 struct wpabuf *probe_resp_ie;
2701};
2702
2703
2704static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2705{
2706 if (lwork == NULL)
2707 return;
2708 wpabuf_free(lwork->probe_resp_ie);
2709 os_free(lwork);
2710}
2711
2712
2713static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2714{
2715 struct wpas_p2p_listen_work *lwork;
2716
2717 if (!wpa_s->p2p_listen_work)
2718 return;
2719
2720 lwork = wpa_s->p2p_listen_work->ctx;
2721 wpas_p2p_listen_work_free(lwork);
2722 radio_work_done(wpa_s->p2p_listen_work);
2723 wpa_s->p2p_listen_work = NULL;
2724}
2725
2726
2727static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2728{
2729 struct wpa_supplicant *wpa_s = work->wpa_s;
2730 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002731 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002732
2733 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002734 if (work->started) {
2735 wpa_s->p2p_listen_work = NULL;
2736 wpas_stop_listen(wpa_s);
2737 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002738 wpas_p2p_listen_work_free(lwork);
2739 return;
2740 }
2741
2742 wpa_s->p2p_listen_work = work;
2743
2744 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2745
2746 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2747 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2748 "report received Probe Request frames");
2749 wpas_p2p_listen_work_done(wpa_s);
2750 return;
2751 }
2752
2753 wpa_s->pending_listen_freq = lwork->freq;
2754 wpa_s->pending_listen_duration = lwork->duration;
2755
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002756 duration = lwork->duration;
2757#ifdef CONFIG_TESTING_OPTIONS
2758 if (wpa_s->extra_roc_dur) {
2759 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2760 duration, duration + wpa_s->extra_roc_dur);
2761 duration += wpa_s->extra_roc_dur;
2762 }
2763#endif /* CONFIG_TESTING_OPTIONS */
2764
2765 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002766 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2767 "to remain on channel (%u MHz) for Listen "
2768 "state", lwork->freq);
2769 wpas_p2p_listen_work_done(wpa_s);
2770 wpa_s->pending_listen_freq = 0;
2771 return;
2772 }
2773 wpa_s->off_channel_freq = 0;
2774 wpa_s->roc_waiting_drv_freq = lwork->freq;
2775}
2776
2777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778static int wpas_start_listen(void *ctx, unsigned int freq,
2779 unsigned int duration,
2780 const struct wpabuf *probe_resp_ie)
2781{
2782 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002783 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002785 if (wpa_s->p2p_listen_work) {
2786 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787 return -1;
2788 }
2789
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002790 lwork = os_zalloc(sizeof(*lwork));
2791 if (lwork == NULL)
2792 return -1;
2793 lwork->freq = freq;
2794 lwork->duration = duration;
2795 if (probe_resp_ie) {
2796 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2797 if (lwork->probe_resp_ie == NULL) {
2798 wpas_p2p_listen_work_free(lwork);
2799 return -1;
2800 }
2801 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002802
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002803 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2804 lwork) < 0) {
2805 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 return -1;
2807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808
2809 return 0;
2810}
2811
2812
2813static void wpas_stop_listen(void *ctx)
2814{
2815 struct wpa_supplicant *wpa_s = ctx;
2816 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2817 wpa_drv_cancel_remain_on_channel(wpa_s);
2818 wpa_s->off_channel_freq = 0;
2819 wpa_s->roc_waiting_drv_freq = 0;
2820 }
2821 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002822
2823 /*
2824 * Don't cancel Probe Request RX reporting for a connected P2P Client
2825 * handling Probe Request frames.
2826 */
2827 if (!wpa_s->p2p_cli_probe)
2828 wpa_drv_probe_req_report(wpa_s, 0);
2829
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002830 wpas_p2p_listen_work_done(wpa_s);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00002831
2832 if (radio_work_pending(wpa_s, "p2p-listen")) {
2833 wpa_printf(MSG_DEBUG,
2834 "P2P: p2p-listen is still pending - remove it");
2835 radio_remove_works(wpa_s, "p2p-listen", 0);
2836 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837}
2838
2839
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002840static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2841 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842{
2843 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002844 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
Hai Shalomfdcde762020-04-02 11:19:20 -07002845 freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846}
2847
2848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2850 const u8 *peer, const char *params,
2851 unsigned int generated_pin)
2852{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002853 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2854 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855}
2856
2857
2858static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2859 const u8 *peer, const char *params)
2860{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002861 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2862 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863}
2864
2865
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002866static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2867 const u8 *dev_addr, const u8 *pri_dev_type,
2868 const char *dev_name, u16 supp_config_methods,
2869 u8 dev_capab, u8 group_capab, const u8 *group_id,
2870 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871{
2872 struct wpa_supplicant *wpa_s = ctx;
2873 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002874 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 u8 empty_dev_type[8];
2876 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002878 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002879
2880 if (group_id) {
2881 for (group = wpa_s->global->ifaces; group; group = group->next)
2882 {
2883 struct wpa_ssid *s = group->current_ssid;
2884 if (s != NULL &&
2885 s->mode == WPAS_MODE_P2P_GO &&
2886 group_id_len - ETH_ALEN == s->ssid_len &&
2887 os_memcmp(group_id + ETH_ALEN, s->ssid,
2888 s->ssid_len) == 0)
2889 break;
2890 }
2891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892
2893 if (pri_dev_type == NULL) {
2894 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2895 pri_dev_type = empty_dev_type;
2896 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002897 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2898 " pri_dev_type=%s name='%s' config_methods=0x%x "
2899 "dev_capab=0x%x group_capab=0x%x%s%s",
2900 MAC2STR(dev_addr),
2901 wps_dev_type_bin2str(pri_dev_type, devtype,
2902 sizeof(devtype)),
2903 dev_name, supp_config_methods, dev_capab, group_capab,
2904 group ? " group=" : "",
2905 group ? group->ifname : "");
2906 if (os_snprintf_error(sizeof(params), res))
2907 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002911 if (wps_generate_pin(&generated_pin) < 0) {
2912 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2913 wpas_notify_p2p_provision_discovery(
2914 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002915 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002916 return;
2917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 wpas_prov_disc_local_display(wpa_s, peer, params,
2919 generated_pin);
2920 } else if (config_methods & WPS_CONFIG_KEYPAD)
2921 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2922 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002923 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2924 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002925
2926 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2927 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002928 config_methods, generated_pin,
2929 group ? group->ifname : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002930}
2931
2932
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002933static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934{
2935 struct wpa_supplicant *wpa_s = ctx;
2936 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002937 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002939 if (wpa_s->pending_pd_before_join &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00002940 (ether_addr_equal(peer, wpa_s->pending_join_dev_addr) ||
2941 ether_addr_equal(peer, wpa_s->pending_join_iface_addr))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002942 wpa_s->pending_pd_before_join = 0;
2943 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2944 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002945 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002946 return;
2947 }
2948
Dmitry Shmidt04949592012-07-19 12:16:46 -07002949 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002950 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2951 int res;
2952
2953 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2954 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2955 if (os_snprintf_error(sizeof(params), res))
2956 params[sizeof(params) - 1] = '\0';
2957 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002958 params[0] = '\0';
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002961 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002963 if (wps_generate_pin(&generated_pin) < 0) {
2964 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2965 wpas_notify_p2p_provision_discovery(
2966 wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002967 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0, NULL);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002968 return;
2969 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002970 wpas_prov_disc_local_display(wpa_s, peer, params,
2971 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002973 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2974 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975
Jouni Malinen75ecf522011-06-27 15:19:46 -07002976 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2977 P2P_PROV_DISC_SUCCESS,
Sunil Ravi8bae4252023-11-16 03:05:19 +00002978 config_methods, generated_pin,
2979 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980}
2981
2982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002983static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002984 enum p2p_prov_disc_status status,
2985 u32 adv_id, const u8 *adv_mac,
2986 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002987{
2988 struct wpa_supplicant *wpa_s = ctx;
2989
Dmitry Shmidt04949592012-07-19 12:16:46 -07002990 if (wpa_s->p2p_fallback_to_go_neg) {
2991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2992 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002993 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002994 P2P_EVENT_FALLBACK_TO_GO_NEG
2995 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002996 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2997 return;
2998 }
2999
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003000 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003001 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003002 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3003 "join-existing-group operation (no ACK for PD "
3004 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003005 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003006 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003007 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003008
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003009 if (adv_id && adv_mac && deferred_session_resp) {
3010 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3011 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3012 " deferred_session_resp='%s'",
3013 MAC2STR(peer), status, adv_id,
3014 deferred_session_resp);
3015 } else if (adv_id && adv_mac) {
3016 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3017 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3018 MAC2STR(peer), status, adv_id);
3019 } else {
3020 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3021 " p2p_dev_addr=" MACSTR " status=%d",
3022 MAC2STR(peer), status);
3023 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003024
Jouni Malinen75ecf522011-06-27 15:19:46 -07003025 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
Sunil Ravi8bae4252023-11-16 03:05:19 +00003026 status, 0, 0, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003027}
3028
3029
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003030static int freq_included(struct wpa_supplicant *wpa_s,
3031 const struct p2p_channels *channels,
3032 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003033{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003034 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
3035 wpas_p2p_go_is_peer_freq(wpa_s, freq))
3036 return 1;
3037 return 0;
3038}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003039
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003040
3041static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
3042{
3043 unsigned int num = P2P_MAX_CHANNELS;
3044 int *common_freqs;
3045 int ret;
3046
3047 p2p_go_dump_common_freqs(wpa_s);
3048 common_freqs = os_calloc(num, sizeof(int));
3049 if (!common_freqs)
3050 return;
3051
3052 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
3053 if (ret < 0) {
3054 wpa_dbg(wpa_s, MSG_DEBUG,
3055 "P2P: Failed to get group common freqs");
3056 os_free(common_freqs);
3057 return;
3058 }
3059
3060 os_free(wpa_s->p2p_group_common_freqs);
3061 wpa_s->p2p_group_common_freqs = common_freqs;
3062 wpa_s->p2p_group_common_freqs_num = num;
3063 p2p_go_dump_common_freqs(wpa_s);
3064}
3065
3066
3067/*
3068 * Check if the given frequency is one of the possible operating frequencies
3069 * set after the completion of the GO Negotiation.
3070 */
3071static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
3072{
3073 unsigned int i;
3074
3075 p2p_go_dump_common_freqs(wpa_s);
3076
3077 /* assume no restrictions */
3078 if (!wpa_s->p2p_group_common_freqs_num)
3079 return 1;
3080
3081 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
3082 if (wpa_s->p2p_group_common_freqs[i] == freq)
3083 return 1;
3084 }
3085 return 0;
3086}
3087
3088
3089static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
3090 struct sta_info *sta, void *ctx)
3091{
3092 int *ecsa_support = ctx;
3093
3094 *ecsa_support &= sta->ecsa_supported;
3095
3096 return 0;
3097}
3098
3099
3100/* Check if all the peers support eCSA */
3101static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
3102{
3103 int ecsa_support = 1;
3104
3105 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
3106 &ecsa_support);
3107
3108 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003109}
3110
3111
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003112/**
3113 * Pick the best frequency to use from all the currently used frequencies.
3114 */
3115static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3116 struct wpa_used_freq_data *freqs,
3117 unsigned int num)
3118{
3119 unsigned int i, c;
3120
3121 /* find a candidate freq that is supported by P2P */
3122 for (c = 0; c < num; c++)
3123 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3124 break;
3125
3126 if (c == num)
3127 return 0;
3128
3129 /* once we have a candidate, try to find a 'better' one */
3130 for (i = c + 1; i < num; i++) {
3131 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3132 continue;
3133
3134 /*
3135 * 1. Infrastructure station interfaces have higher preference.
3136 * 2. P2P Clients have higher preference.
3137 * 3. All others.
3138 */
3139 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3140 c = i;
3141 break;
3142 }
3143
3144 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3145 c = i;
3146 }
3147 return freqs[c].freq;
3148}
3149
3150
Sunil Ravi036cec52023-03-29 11:35:17 -07003151/**
3152 * Pick the best frequency the driver suggests.
3153 *
3154 * num_pref_freq is used as both input and output
3155 * - input: the max size of pref_freq_list,
3156 * - output: the valid size of pref_freq_list filled with data.
3157 */
3158static int wpas_p2p_pick_best_pref_freq(struct wpa_supplicant *wpa_s, bool go,
3159 struct weighted_pcl *pref_freq_list,
3160 unsigned int *num_pref_freq)
3161{
3162 int best_freq = 0;
3163 unsigned int max_pref_freq, i;
3164 int res;
3165 enum wpa_driver_if_type iface_type;
3166
3167 max_pref_freq = *num_pref_freq;
3168 *num_pref_freq = 0;
3169
3170 if (go)
3171 iface_type = WPA_IF_P2P_GO;
3172 else
3173 iface_type = WPA_IF_P2P_CLIENT;
3174
3175 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &max_pref_freq,
3176 pref_freq_list);
3177 if (!res && !is_p2p_allow_6ghz(wpa_s->global->p2p))
3178 max_pref_freq = p2p_remove_6ghz_channels(pref_freq_list,
3179 max_pref_freq);
3180 if (res || !max_pref_freq) {
3181 wpa_printf(MSG_DEBUG,
3182 "P2P: No preferred frequency list available");
3183 return 0;
3184 }
3185
3186 *num_pref_freq = max_pref_freq;
3187 i = 0;
3188 while (i < *num_pref_freq &&
3189 (!p2p_supported_freq(wpa_s->global->p2p,
3190 pref_freq_list[i].freq) ||
3191 wpas_p2p_disallowed_freq(wpa_s->global,
3192 pref_freq_list[i].freq) ||
3193 !p2p_pref_freq_allowed(&pref_freq_list[i], go))) {
3194 wpa_printf(MSG_DEBUG,
3195 "P2P: preferred_freq_list[%d]=%d is disallowed",
3196 i, pref_freq_list[i].freq);
3197 i++;
3198 }
3199 if (i != *num_pref_freq) {
3200 best_freq = pref_freq_list[i].freq;
3201 wpa_printf(MSG_DEBUG, "P2P: Using preferred_freq_list[%d]=%d",
3202 i, best_freq);
3203 } else {
3204 wpa_printf(MSG_DEBUG,
3205 "P2P: All driver preferred frequencies are disallowed for P2P use");
3206 *num_pref_freq = 0;
3207 }
3208
3209 return best_freq;
3210}
3211
3212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3214 const u8 *go_dev_addr, const u8 *ssid,
3215 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003216 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003217 const struct p2p_channels *channels,
3218 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219{
3220 struct wpa_supplicant *wpa_s = ctx;
3221 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003222 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003224 int best_freq;
Sunil Ravi036cec52023-03-29 11:35:17 -07003225 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
3226 unsigned int num_pref_freq;
3227 int res;
3228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229
3230 if (!persistent_group) {
3231 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003232 " to join an active group (SSID: %s)",
3233 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003235 (ether_addr_equal(go_dev_addr, wpa_s->p2p_auth_invite) ||
3236 ether_addr_equal(sa, wpa_s->p2p_auth_invite))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3238 "authorized invitation");
3239 goto accept_inv;
3240 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003241
3242#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003243 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3244 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003245 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003246 wpa_s->p2p_wps_method = WPS_NFC;
3247 wpa_s->pending_join_wps_method = WPS_NFC;
3248 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003249 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003250 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003251 bssid, ETH_ALEN);
3252 goto accept_inv;
3253 }
3254#endif /* CONFIG_WPS_NFC */
3255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 /*
3257 * Do not accept the invitation automatically; notify user and
3258 * request approval.
3259 */
3260 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3261 }
3262
3263 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3264 if (grp) {
3265 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3266 "running persistent group");
3267 if (*go)
3268 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3269 goto accept_inv;
3270 }
3271
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003272 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003273 ether_addr_equal(sa, wpa_s->p2p_auth_invite)) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003274 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3275 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003276 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003277 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3279
3280 for (s = wpa_s->conf->ssid; s; s = s->next) {
3281 if (s->disabled == 2 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003282 ether_addr_equal(s->bssid, go_dev_addr) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283 s->ssid_len == ssid_len &&
3284 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3285 break;
3286 }
3287
3288 if (!s) {
3289 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3290 " requested reinvocation of an unknown group",
3291 MAC2STR(sa));
3292 return P2P_SC_FAIL_UNKNOWN_GROUP;
3293 }
3294
3295 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3296 *go = 1;
3297 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3298 wpa_printf(MSG_DEBUG, "P2P: The only available "
3299 "interface is already in use - reject "
3300 "invitation");
3301 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3302 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003303 if (wpa_s->p2p_mgmt)
3304 os_memcpy(group_bssid, wpa_s->parent->own_addr,
3305 ETH_ALEN);
3306 else
3307 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308 } else if (s->mode == WPAS_MODE_P2P_GO) {
3309 *go = 1;
3310 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3311 {
3312 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3313 "interface address for the group");
3314 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3315 }
3316 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3317 ETH_ALEN);
3318 }
3319
3320accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003321 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3322
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003323 best_freq = 0;
3324 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3325 sizeof(struct wpa_used_freq_data));
3326 if (freqs) {
3327 int num_channels = wpa_s->num_multichan_concurrent;
3328 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3329 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3330 os_free(freqs);
3331 }
3332
Sunil Ravi036cec52023-03-29 11:35:17 -07003333 num_pref_freq = P2P_MAX_PREF_CHANNELS;
3334 res = wpas_p2p_pick_best_pref_freq(wpa_s, *go, pref_freq_list,
3335 &num_pref_freq);
3336 if (res > 0)
3337 best_freq = res;
3338
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003339 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003340 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003341 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003342 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003343
3344 if (wpa_s->num_multichan_concurrent < 2 ||
3345 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3346 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 -07003347 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003348 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349 }
3350
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003351 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3352 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003353 if (*go == 0) {
3354 /* We are the client */
3355 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3356 "running a GO but we are capable of MCC, "
3357 "figure out the best channel to use");
3358 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003359 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003360 /* We are the GO, and *force_freq is not in the
3361 * intersection */
3362 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3363 "in intersection but we are capable of MCC, "
3364 "figure out the best channel to use",
3365 *force_freq);
3366 *force_freq = 0;
3367 }
3368 }
3369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370 return P2P_SC_SUCCESS;
3371}
3372
3373
3374static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3375 const u8 *ssid, size_t ssid_len,
3376 const u8 *go_dev_addr, u8 status,
3377 int op_freq)
3378{
3379 struct wpa_supplicant *wpa_s = ctx;
3380 struct wpa_ssid *s;
3381
3382 for (s = wpa_s->conf->ssid; s; s = s->next) {
3383 if (s->disabled == 2 &&
3384 s->ssid_len == ssid_len &&
3385 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3386 break;
3387 }
3388
3389 if (status == P2P_SC_SUCCESS) {
3390 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003391 " was accepted; op_freq=%d MHz, SSID=%s",
3392 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003394 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003395 if (go) {
3396 wpa_msg_global(wpa_s, MSG_INFO,
3397 P2P_EVENT_INVITATION_ACCEPTED
3398 "sa=" MACSTR
3399 " persistent=%d freq=%d",
3400 MAC2STR(sa), s->id, op_freq);
3401 } else {
3402 wpa_msg_global(wpa_s, MSG_INFO,
3403 P2P_EVENT_INVITATION_ACCEPTED
3404 "sa=" MACSTR
3405 " persistent=%d",
3406 MAC2STR(sa), s->id);
3407 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 wpas_p2p_group_add_persistent(
Hai Shalomb755a2a2020-04-23 21:49:02 -07003409 wpa_s, s, go, 0, op_freq, 0,
3410 wpa_s->conf->p2p_go_ht40,
3411 wpa_s->conf->p2p_go_vht,
3412 0,
3413 wpa_s->conf->p2p_go_he,
3414 wpa_s->conf->p2p_go_edmg, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003415 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
Matthew Wang124e5f42022-12-29 07:23:06 +00003416 1, is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003417 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003419 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003420 wpa_msg_global(wpa_s, MSG_INFO,
3421 P2P_EVENT_INVITATION_ACCEPTED
3422 "sa=" MACSTR " go_dev_addr=" MACSTR
3423 " bssid=" MACSTR " unknown-network",
3424 MAC2STR(sa), MAC2STR(go_dev_addr),
3425 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003427 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003428 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429 }
3430 return;
3431 }
3432
3433 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3434 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3435 " was rejected (status %u)", MAC2STR(sa), status);
3436 return;
3437 }
3438
3439 if (!s) {
3440 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003441 wpa_msg_global(wpa_s, MSG_INFO,
3442 P2P_EVENT_INVITATION_RECEIVED
3443 "sa=" MACSTR " go_dev_addr=" MACSTR
3444 " bssid=" MACSTR " unknown-network",
3445 MAC2STR(sa), MAC2STR(go_dev_addr),
3446 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003448 wpa_msg_global(wpa_s, MSG_INFO,
3449 P2P_EVENT_INVITATION_RECEIVED
3450 "sa=" MACSTR " go_dev_addr=" MACSTR
3451 " unknown-network",
3452 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003454 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3455 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 return;
3457 }
3458
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003459 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003460 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3461 "sa=" MACSTR " persistent=%d freq=%d",
3462 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003463 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003464 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3465 "sa=" MACSTR " persistent=%d",
3466 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003467 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003468 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3469 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003470}
3471
3472
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003473static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3474 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003475 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003476{
3477 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003478 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003479
3480 if (ssid == NULL)
3481 return;
3482
3483 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003484 if (ether_addr_equal(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3485 peer))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003486 break;
3487 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003488 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003489 if (ssid->mode != WPAS_MODE_P2P_GO &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00003490 ether_addr_equal(ssid->bssid, peer)) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003491 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3492 "due to invitation result", ssid->id);
3493 wpas_notify_network_removed(wpa_s, ssid);
3494 wpa_config_remove_network(wpa_s->conf, ssid->id);
3495 return;
3496 }
3497 return; /* Peer not found in client list */
3498 }
3499
3500 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003501 "group %d client list%s",
3502 MAC2STR(peer), ssid->id,
3503 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003504 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3505 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3506 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003507 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003508 if (p2p_wpa_s->conf->update_config &&
3509 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003510 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003511}
3512
3513
3514static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3515 const u8 *peer)
3516{
3517 struct wpa_ssid *ssid;
3518
3519 wpa_s = wpa_s->global->p2p_invite_group;
3520 if (wpa_s == NULL)
3521 return; /* No known invitation group */
3522 ssid = wpa_s->current_ssid;
3523 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3524 !ssid->p2p_persistent_group)
3525 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003526 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003527 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003528 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003529}
3530
3531
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003532static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003533 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003534 const u8 *peer, int neg_freq,
3535 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536{
3537 struct wpa_supplicant *wpa_s = ctx;
3538 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003539 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540
3541 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003542 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3543 "status=%d " MACSTR,
3544 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003546 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3547 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 }
3549 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3550
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003551 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3552 status, MAC2STR(peer));
3553 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003554 struct wpa_supplicant *group_if =
3555 wpa_s->global->p2p_invite_group;
3556
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003557 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3558 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003559
3560 /*
3561 * Invitation to an active group. If this is successful and we
3562 * are the GO, set the client wait to postpone some concurrent
3563 * operations and to allow provisioning and connection to happen
3564 * more quickly.
3565 */
3566 if (status == P2P_SC_SUCCESS &&
3567 group_if && group_if->current_ssid &&
3568 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3569 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3570#ifdef CONFIG_TESTING_OPTIONS
3571 if (group_if->p2p_go_csa_on_inv) {
3572 wpa_printf(MSG_DEBUG,
3573 "Testing: force P2P GO CSA after invitation");
3574 eloop_cancel_timeout(
3575 wpas_p2p_reconsider_moving_go,
3576 wpa_s, NULL);
3577 eloop_register_timeout(
3578 0, 50000,
3579 wpas_p2p_reconsider_moving_go,
3580 wpa_s, NULL);
3581 }
3582#endif /* CONFIG_TESTING_OPTIONS */
3583 }
3584 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003585 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003587 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3588 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3589 "invitation exchange to indicate readiness for "
3590 "re-invocation");
3591 }
3592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003594 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3595 ssid = wpa_config_get_network(
3596 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003597 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 wpas_p2p_remove_pending_group_interface(wpa_s);
3600 return;
3601 }
3602
3603 ssid = wpa_config_get_network(wpa_s->conf,
3604 wpa_s->pending_invite_ssid_id);
3605 if (ssid == NULL) {
3606 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3607 "data matching with invitation");
3608 return;
3609 }
3610
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003611 /*
3612 * The peer could have missed our ctrl::ack frame for Invitation
3613 * Response and continue retransmitting the frame. To reduce the
3614 * likelihood of the peer not getting successful TX status for the
3615 * Invitation Response frame, wait a short time here before starting
3616 * the persistent group so that we will remain on the current channel to
3617 * acknowledge any possible retransmission from the peer.
3618 */
3619 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3620 "starting persistent group");
3621 os_sleep(0, 50000);
3622
Dmitry Shmidt15907092014-03-25 10:42:57 -07003623 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003624 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003625 freq = neg_freq;
3626 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003627 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003628 freq = peer_oper_freq;
3629 else
3630 freq = 0;
3631
3632 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3633 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003635 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003636 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003637 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003638 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003639 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003640 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08003641 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003642 wpa_s->p2p_go_edmg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003643 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003644 ssid->mode == WPAS_MODE_P2P_GO ?
3645 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Hai Shalomc1a21442022-02-04 13:43:00 -08003646 0, 1,
Matthew Wang124e5f42022-12-29 07:23:06 +00003647 is_p2p_allow_6ghz(wpa_s->global->p2p), 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07003648 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649}
3650
3651
Dmitry Shmidt04949592012-07-19 12:16:46 -07003652static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3653 unsigned int freq)
3654{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003655 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3656 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003657 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003658}
3659
3660
3661static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3662{
3663 reg->channel[reg->channels] = chan;
3664 reg->channels++;
3665}
3666
3667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003668static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003669 struct p2p_channels *chan,
3670 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671{
3672 int i, cla = 0;
3673
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003674 wpa_s->global->p2p_24ghz_social_channels = 1;
3675
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003676 os_memset(cli_chan, 0, sizeof(*cli_chan));
3677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3679 "band");
3680
3681 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3682 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003683 chan->reg_class[cla].channels = 0;
3684 for (i = 0; i < 11; i++) {
3685 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3686 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3687 }
3688 if (chan->reg_class[cla].channels)
3689 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690
3691 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3692 "band");
3693
3694 /* Operating class 115 - 5 GHz, channels 36-48 */
3695 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003696 chan->reg_class[cla].channels = 0;
3697 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3698 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3699 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3700 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3701 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3702 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3703 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3704 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3705 if (chan->reg_class[cla].channels)
3706 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003707
3708 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3709 "band");
3710
3711 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3712 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003713 chan->reg_class[cla].channels = 0;
3714 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3715 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3716 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3717 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3718 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3719 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3720 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3721 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3722 if (chan->reg_class[cla].channels)
3723 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724
3725 chan->reg_classes = cla;
3726 return 0;
3727}
3728
3729
Hai Shalomc1a21442022-02-04 13:43:00 -08003730static enum chan_allowed has_channel(struct wpa_global *global,
3731 struct hostapd_hw_modes *mode, u8 op_class,
3732 u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733{
3734 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003735 unsigned int freq;
3736
Hai Shalomc1a21442022-02-04 13:43:00 -08003737 freq = ieee80211_chan_to_freq(NULL, op_class, chan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003738 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003739 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740
3741 for (i = 0; i < mode->num_channels; i++) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003742 if ((unsigned int) mode->channels[i].freq == freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 if (flags)
3744 *flags = mode->channels[i].flag;
Hai Shalomc1a21442022-02-04 13:43:00 -08003745 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003746 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003747 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3748 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003749 if (mode->channels[i].flag & HOSTAPD_CHAN_RADAR)
3750 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003751 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 }
3753 }
3754
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003755 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756}
3757
3758
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003759static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3760 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003761 u8 channel, const u8 *center_channels,
3762 size_t num_chan)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003763{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003764 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003765
3766 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3767 return 0;
3768
Hai Shalomc1a21442022-02-04 13:43:00 -08003769 for (i = 0; i < num_chan; i++)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003770 /*
3771 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3772 * so the center channel is 6 channels away from the start/end.
3773 */
3774 if (channel >= center_channels[i] - 6 &&
3775 channel <= center_channels[i] + 6)
3776 return center_channels[i];
3777
3778 return 0;
3779}
3780
3781
Hai Shalomc1a21442022-02-04 13:43:00 -08003782static const u8 center_channels_5ghz_80mhz[] = { 42, 58, 106, 122, 138,
3783 155, 171 };
3784static const u8 center_channels_6ghz_80mhz[] = { 7, 23, 39, 55, 71, 87, 103,
3785 119, 135, 151, 167, 183, 199,
3786 215 };
3787
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003788static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3789 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003790 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003791{
3792 u8 center_chan;
3793 int i, flags;
3794 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003795 const u8 *chans;
3796 size_t num_chans;
3797 bool is_6ghz = is_6ghz_op_class(op_class);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003798
Hai Shalomc1a21442022-02-04 13:43:00 -08003799 if (is_6ghz) {
3800 chans = center_channels_6ghz_80mhz;
3801 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
3802 } else {
3803 chans = center_channels_5ghz_80mhz;
3804 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
3805 }
3806 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
3807 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003808 if (!center_chan)
3809 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003810 if (!wpa_s->p2p_go_allow_dfs &&
3811 !is_6ghz && center_chan >= 58 && center_chan <= 138)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003812 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3813
3814 /* check all the channels are available */
3815 for (i = 0; i < 4; i++) {
3816 int adj_chan = center_chan - 6 + i * 4;
3817
Hai Shalomc1a21442022-02-04 13:43:00 -08003818 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3819 &flags);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003820 if (res == NOT_ALLOWED)
3821 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003822 if (res == RADAR)
3823 ret = RADAR;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003824 if (res == NO_IR)
3825 ret = NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003826 if (!is_6ghz) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003827 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003828 return NOT_ALLOWED;
3829 } else if (is_6ghz &&
3830 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3831 HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G))) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003832 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003833 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003834 }
3835
3836 return ret;
3837}
3838
3839
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003840static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3841 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003842 u8 channel, const u8 *center_channels,
3843 size_t num_chan)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003844{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003845 unsigned int i;
3846
3847 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3848 return 0;
3849
Hai Shalomc1a21442022-02-04 13:43:00 -08003850 for (i = 0; i < num_chan; i++)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003851 /*
3852 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3853 * so the center channel is 14 channels away from the start/end.
3854 */
3855 if (channel >= center_channels[i] - 14 &&
3856 channel <= center_channels[i] + 14)
3857 return center_channels[i];
3858
3859 return 0;
3860}
3861
3862
Hai Shalomc1a21442022-02-04 13:43:00 -08003863static const u8 center_channels_5ghz_160mhz[] = { 50, 114, 163 };
3864static const u8 center_channels_6ghz_160mhz[] = { 15, 47, 79, 111, 143, 175,
3865 207 };
3866
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003867static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3868 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003869 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003870{
3871 u8 center_chan;
3872 int i, flags;
3873 enum chan_allowed res, ret = ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003874 const u8 *chans;
3875 size_t num_chans;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003876
Hai Shalomc1a21442022-02-04 13:43:00 -08003877 if (is_6ghz_op_class(op_class)) {
3878 chans = center_channels_6ghz_160mhz;
3879 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
3880 } else {
3881 chans = center_channels_5ghz_160mhz;
3882 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
3883 }
3884 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
3885 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003886 if (!center_chan)
3887 return NOT_ALLOWED;
3888 /* VHT 160 MHz uses DFS channels in most countries. */
3889
3890 /* Check all the channels are available */
3891 for (i = 0; i < 8; i++) {
3892 int adj_chan = center_chan - 14 + i * 4;
3893
Hai Shalomc1a21442022-02-04 13:43:00 -08003894 res = has_channel(wpa_s->global, mode, op_class, adj_chan,
3895 &flags);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003896 if (res == NOT_ALLOWED)
3897 return NOT_ALLOWED;
3898
Hai Shalomc1a21442022-02-04 13:43:00 -08003899 if (res == RADAR)
3900 ret = RADAR;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003901 if (res == NO_IR)
3902 ret = NO_IR;
3903
Hai Shalomc1a21442022-02-04 13:43:00 -08003904 if (!is_6ghz_op_class(op_class)) {
Sunil Ravi036cec52023-03-29 11:35:17 -07003905 if (!(flags & HOSTAPD_CHAN_VHT_80MHZ_SUBCHANNEL) ||
3906 !(flags & HOSTAPD_CHAN_VHT_160MHZ_SUBCHANNEL))
Hai Shalomc1a21442022-02-04 13:43:00 -08003907 return NOT_ALLOWED;
3908 } else if (is_6ghz_op_class(op_class) &&
3909 (!(wpas_get_6ghz_he_chwidth_capab(mode) &
3910 HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G))) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003911 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003912 }
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003913 }
3914
3915 return ret;
3916}
3917
3918
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003919static enum chan_allowed wpas_p2p_verify_edmg(struct wpa_supplicant *wpa_s,
3920 struct hostapd_hw_modes *mode,
3921 u8 channel)
3922{
3923 struct ieee80211_edmg_config edmg;
3924
3925 hostapd_encode_edmg_chan(1, channel, 0, &edmg);
3926 if (edmg.channels && ieee802_edmg_is_allowed(mode->edmg, edmg))
3927 return ALLOWED;
3928
3929 return NOT_ALLOWED;
3930}
3931
3932
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003933static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3934 struct hostapd_hw_modes *mode,
Hai Shalomc1a21442022-02-04 13:43:00 -08003935 u8 op_class, u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003936{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003937 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003938 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003939
Sunil Ravi89eba102022-09-13 21:04:37 -07003940 if (is_6ghz_op_class(op_class) && !is_6ghz_psc_frequency(
3941 p2p_channel_to_freq(op_class, channel)))
3942 return NOT_ALLOWED;
3943
Hai Shalomc1a21442022-02-04 13:43:00 -08003944 res2 = res = has_channel(wpa_s->global, mode, op_class, channel, &flag);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003945 if (bw == BW40MINUS) {
3946 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3947 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003948 res2 = has_channel(wpa_s->global, mode, op_class, channel - 4,
3949 NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003950 } else if (bw == BW40PLUS) {
3951 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3952 return NOT_ALLOWED;
Hai Shalomc1a21442022-02-04 13:43:00 -08003953 res2 = has_channel(wpa_s->global, mode, op_class, channel + 4,
3954 NULL);
3955 } else if (is_6ghz_op_class(op_class) && bw == BW40) {
3956 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3957 return NOT_ALLOWED;
3958 if (get_6ghz_sec_channel(channel) < 0)
3959 res2 = has_channel(wpa_s->global, mode, op_class,
3960 channel - 4, NULL);
3961 else
3962 res2 = has_channel(wpa_s->global, mode, op_class,
3963 channel + 4, NULL);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003964 } else if (bw == BW80) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003965 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, op_class, channel,
3966 bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003967 } else if (bw == BW160) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003968 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, op_class, channel,
3969 bw);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003970 } else if (bw == BW4320 || bw == BW6480 || bw == BW8640) {
3971 return wpas_p2p_verify_edmg(wpa_s, mode, channel);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003972 }
3973
3974 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3975 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003976 if (res == NO_IR || res2 == NO_IR)
3977 return NO_IR;
Hai Shalomc1a21442022-02-04 13:43:00 -08003978 if (res == RADAR || res2 == RADAR)
3979 return RADAR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003980 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003981}
3982
3983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003985 struct p2p_channels *chan,
Hai Shalom60840252021-02-19 19:02:11 -08003986 struct p2p_channels *cli_chan,
3987 bool p2p_disable_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003989 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003990 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003992 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003993 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3994 "of all supported channels; assume dualband "
3995 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003996 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 }
3998
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003999 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004001 for (op = 0; global_op_class[op].op_class; op++) {
4002 const struct oper_class_map *o = &global_op_class[op];
Hai Shalomc1a21442022-02-04 13:43:00 -08004003 unsigned int ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004004 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Shuibing Daie2fad412023-05-05 14:08:11 -07004005 bool check_dfs_supported = (is_p2p_dfs_chan_enabled(wpa_s->global->p2p)
Shuibing Dai64a8a892023-03-08 10:26:22 -08004006 && is_dfs_global_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004007
Shuibing Dai64a8a892023-03-08 10:26:22 -08004008 if ((!check_dfs_supported && o->p2p == NO_P2P_SUPP) ||
Hai Shalom60840252021-02-19 19:02:11 -08004009 (is_6ghz_op_class(o->op_class) && p2p_disable_6ghz))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004010 continue;
4011
Hai Shalomfdcde762020-04-02 11:19:20 -07004012 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode,
4013 is_6ghz_op_class(o->op_class));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 if (mode == NULL)
4015 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004016 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4017 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004019 enum chan_allowed res;
Hai Shalom899fcc72020-10-19 14:38:18 -07004020
4021 /* Check for non-continuous jump in channel index
4022 * incrementation */
Hai Shalom60840252021-02-19 19:02:11 -08004023 if ((o->op_class >= 128 && o->op_class <= 130) &&
Hai Shalom899fcc72020-10-19 14:38:18 -07004024 ch < 149 && ch + o->inc > 149)
4025 ch = 149;
4026
Hai Shalomc1a21442022-02-04 13:43:00 -08004027 res = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4028 ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004029 if (res == ALLOWED) {
4030 if (reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004031 if (cla == P2P_MAX_REG_CLASSES)
4032 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004033 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4034 o->op_class);
4035 reg = &chan->reg_class[cla];
4036 cla++;
4037 reg->reg_class = o->op_class;
4038 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004039 if (reg->channels == P2P_MAX_REG_CLASS_CHANNELS)
4040 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004041 reg->channel[reg->channels] = ch;
4042 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004043 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004044 wpa_s->conf->p2p_add_cli_chan) {
4045 if (cli_reg == NULL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07004046 if (cli_cla == P2P_MAX_REG_CLASSES)
4047 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004048 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4049 o->op_class);
4050 cli_reg = &cli_chan->reg_class[cli_cla];
4051 cli_cla++;
4052 cli_reg->reg_class = o->op_class;
4053 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004054 if (cli_reg->channels ==
4055 P2P_MAX_REG_CLASS_CHANNELS)
4056 continue;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004057 cli_reg->channel[cli_reg->channels] = ch;
4058 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004060 }
4061 if (reg) {
4062 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4063 reg->channel, reg->channels);
4064 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004065 if (cli_reg) {
4066 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4067 cli_reg->channel, cli_reg->channels);
4068 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069 }
4070
4071 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004072 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074 return 0;
4075}
4076
4077
Hai Shalomc1a21442022-02-04 13:43:00 -08004078int wpas_p2p_get_sec_channel_offset_40mhz(struct wpa_supplicant *wpa_s,
4079 struct hostapd_hw_modes *mode,
4080 u8 channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004081{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004082 int op;
4083 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004084
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004085 for (op = 0; global_op_class[op].op_class; op++) {
4086 const struct oper_class_map *o = &global_op_class[op];
Sunil Ravi77d572f2023-01-17 23:58:31 +00004087 u16 ch = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004088
Hai Shalomc1a21442022-02-04 13:43:00 -08004089 /* Allow DFS channels marked as NO_P2P_SUPP to be used with
4090 * driver offloaded DFS. */
4091 if ((o->p2p == NO_P2P_SUPP &&
4092 (!is_dfs_global_op_class(o->op_class) ||
4093 !wpa_s->p2p_go_allow_dfs)) ||
Hai Shalom899fcc72020-10-19 14:38:18 -07004094 (is_6ghz_op_class(o->op_class) &&
4095 wpa_s->conf->p2p_6ghz_disable))
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004096 continue;
4097
Sunil Ravi77d572f2023-01-17 23:58:31 +00004098 /* IEEE Std 802.11ax-2021 26.17.2.3.2: "A 6 GHz-only AP should
4099 * set up the BSS with a primary 20 MHz channel that coincides
4100 * with a preferred scanning channel (PSC)."
4101 * 6 GHz BW40 operation class 132 in wpa_supplicant uses the
4102 * lowest 20 MHz channel for simplicity, so increase ch by 4 to
4103 * match the PSC.
4104 */
Hai Shalomc1a21442022-02-04 13:43:00 -08004105 if (is_6ghz_op_class(o->op_class) && o->bw == BW40 &&
4106 get_6ghz_sec_channel(channel) < 0)
Sunil Ravi77d572f2023-01-17 23:58:31 +00004107 ch = 4;
Hai Shalomc1a21442022-02-04 13:43:00 -08004108
Sunil Ravi77d572f2023-01-17 23:58:31 +00004109 for (ch += o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004110 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004111 (o->bw != BW40PLUS && o->bw != BW40MINUS &&
4112 o->bw != BW40) ||
Sunil Ravi77d572f2023-01-17 23:58:31 +00004113 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004114 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08004115 ret = wpas_p2p_verify_channel(wpa_s, mode, o->op_class,
4116 ch, o->bw);
4117 if (ret == ALLOWED) {
4118 if (is_6ghz_op_class(o->op_class) &&
4119 o->bw == BW40)
4120 return get_6ghz_sec_channel(channel);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004121 return (o->bw == BW40MINUS) ? -1 : 1;
Hai Shalomc1a21442022-02-04 13:43:00 -08004122 }
4123 if (ret == RADAR && wpa_s->p2p_go_allow_dfs) {
4124 /* Allow RADAR channels used for driver
4125 * offloaded DFS */
4126 return (o->bw == BW40MINUS) ? -1 : 1;
4127 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004128 }
4129 }
4130 return 0;
4131}
4132
4133
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004134int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004135 struct hostapd_hw_modes *mode, u8 channel,
4136 u8 op_class)
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004137{
Hai Shalomc1a21442022-02-04 13:43:00 -08004138 const u8 *chans;
4139 size_t num_chans;
4140 enum chan_allowed ret;
4141
4142 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW80);
4143 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004144 return 0;
4145
Hai Shalomc1a21442022-02-04 13:43:00 -08004146 if (is_6ghz_op_class(op_class)) {
4147 chans = center_channels_6ghz_80mhz;
4148 num_chans = ARRAY_SIZE(center_channels_6ghz_80mhz);
4149 } else {
4150 chans = center_channels_5ghz_80mhz;
4151 num_chans = ARRAY_SIZE(center_channels_5ghz_80mhz);
4152 }
4153 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel,
4154 chans, num_chans);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004155}
4156
4157
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004158int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
Hai Shalomc1a21442022-02-04 13:43:00 -08004159 struct hostapd_hw_modes *mode, u8 channel,
4160 u8 op_class)
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004161{
Hai Shalomc1a21442022-02-04 13:43:00 -08004162 const u8 *chans;
4163 size_t num_chans;
4164 enum chan_allowed ret;
4165
4166 ret = wpas_p2p_verify_channel(wpa_s, mode, op_class, channel, BW160);
4167 if (!(ret == ALLOWED || (ret == RADAR && wpa_s->p2p_go_allow_dfs)))
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004168 return 0;
Hai Shalomc1a21442022-02-04 13:43:00 -08004169 if (is_6ghz_op_class(op_class)) {
4170 chans = center_channels_6ghz_160mhz;
4171 num_chans = ARRAY_SIZE(center_channels_6ghz_160mhz);
4172 } else {
4173 chans = center_channels_5ghz_160mhz;
4174 num_chans = ARRAY_SIZE(center_channels_5ghz_160mhz);
4175 }
4176 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel,
4177 chans, num_chans);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004178}
4179
4180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004181static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4182 size_t buf_len)
4183{
4184 struct wpa_supplicant *wpa_s = ctx;
4185
4186 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004187 if (ether_addr_equal(wpa_s->own_addr, interface_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004188 break;
4189 }
4190 if (wpa_s == NULL)
4191 return -1;
4192
4193 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4194}
4195
4196
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004197struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4198 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004199{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004200 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4201 struct wpa_ssid *s = wpa_s->current_ssid;
4202 if (s == NULL)
4203 continue;
4204 if (s->mode != WPAS_MODE_P2P_GO &&
4205 s->mode != WPAS_MODE_AP &&
4206 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4207 continue;
4208 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004209 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004210 continue;
4211 return wpa_s;
4212 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004213
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004214 return NULL;
4215
4216}
4217
4218
4219struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4220 const u8 *peer_dev_addr)
4221{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004222 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4223 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004224 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004225 continue;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004226 if (ether_addr_equal(wpa_s->go_dev_addr, peer_dev_addr))
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004227 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004228 }
4229
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004230 return NULL;
4231}
4232
4233
4234static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4235{
4236 struct wpa_supplicant *wpa_s = ctx;
4237
4238 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004239}
4240
4241
Dmitry Shmidt18463232014-01-24 12:29:41 -08004242static int wpas_is_concurrent_session_active(void *ctx)
4243{
4244 struct wpa_supplicant *wpa_s = ctx;
4245 struct wpa_supplicant *ifs;
4246
4247 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4248 if (ifs == wpa_s)
4249 continue;
4250 if (ifs->wpa_state > WPA_ASSOCIATED)
4251 return 1;
4252 }
4253 return 0;
4254}
4255
4256
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004257static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4258{
4259 struct wpa_supplicant *wpa_s = ctx;
4260 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4261}
4262
4263
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004264int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4265 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004266{
4267 struct wpa_interface iface;
4268 struct wpa_supplicant *p2pdev_wpa_s;
4269 char ifname[100];
4270 char force_name[100];
4271 int ret;
Hai Shalom60840252021-02-19 19:02:11 -08004272 const u8 *if_addr = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004273
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004274 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4275 wpa_s->ifname);
4276 if (os_snprintf_error(sizeof(ifname), ret))
4277 return -1;
Hai Shalom899fcc72020-10-19 14:38:18 -07004278 /* Cut length at the maximum size. Note that we don't need to ensure
4279 * collision free names here as the created interface is not a netdev.
4280 */
4281 ifname[IFNAMSIZ - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004282 force_name[0] = '\0';
4283 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
Hai Shalom60840252021-02-19 19:02:11 -08004284
4285 if (wpa_s->conf->p2p_device_random_mac_addr == 2 &&
4286 !is_zero_ether_addr(wpa_s->conf->p2p_device_persistent_mac_addr))
4287 if_addr = wpa_s->conf->p2p_device_persistent_mac_addr;
4288
4289 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, if_addr, NULL,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004290 force_name, wpa_s->pending_interface_addr, NULL);
4291 if (ret < 0) {
4292 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4293 return ret;
4294 }
4295 os_strlcpy(wpa_s->pending_interface_name, ifname,
4296 sizeof(wpa_s->pending_interface_name));
4297
4298 os_memset(&iface, 0, sizeof(iface));
4299 iface.p2p_mgmt = 1;
4300 iface.ifname = wpa_s->pending_interface_name;
4301 iface.driver = wpa_s->driver->name;
4302 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004303
4304 /*
4305 * If a P2P Device configuration file was given, use it as the interface
4306 * configuration file (instead of using parent's configuration file.
4307 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004308 if (conf_p2p_dev) {
4309 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004310 iface.ctrl_interface = NULL;
4311 } else {
4312 iface.confname = wpa_s->confname;
4313 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4314 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004315
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004316 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004317 if (!p2pdev_wpa_s) {
4318 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4319 return -1;
4320 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004321
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004322 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004323 wpa_s->pending_interface_name[0] = '\0';
4324 return 0;
4325}
4326
4327
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004328static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4329 const u8 *noa, size_t noa_len)
4330{
4331 struct wpa_supplicant *wpa_s, *intf = ctx;
4332 char hex[100];
4333
4334 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4335 if (wpa_s->waiting_presence_resp)
4336 break;
4337 }
4338 if (!wpa_s) {
4339 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4340 return;
4341 }
4342 wpa_s->waiting_presence_resp = 0;
4343
4344 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4345 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4346 " status=%u noa=%s", MAC2STR(src), status, hex);
4347}
4348
4349
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004350static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4351 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004352 u8 *ret_ssid, size_t *ret_ssid_len,
4353 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004354{
4355 struct wpa_supplicant *wpa_s = ctx;
4356 struct wpa_ssid *s;
4357
4358 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4359 if (s) {
4360 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4361 *ret_ssid_len = s->ssid_len;
4362 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004363
4364 if (s->mode != WPAS_MODE_P2P_GO) {
4365 os_memset(intended_iface_addr, 0, ETH_ALEN);
4366 } else if (wpas_p2p_create_iface(wpa_s)) {
4367 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
4368 return 0;
4369
4370 os_memcpy(intended_iface_addr,
4371 wpa_s->pending_interface_addr, ETH_ALEN);
4372 } else {
4373 os_memcpy(intended_iface_addr, wpa_s->own_addr,
4374 ETH_ALEN);
4375 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004376 return 1;
4377 }
4378
4379 return 0;
4380}
4381
4382
4383static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004384 u8 *ssid, size_t *ssid_len, int *group_iface,
4385 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004386{
4387 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004388 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004389 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004390
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004391 /*
4392 * group_iface will be set to 1 only if a dedicated interface for P2P
4393 * role is required. First, we try to reuse an active GO. However,
4394 * if it is not present, we will try to reactivate an existing
4395 * persistent group and set group_iface to 1, so the caller will know
4396 * that the pending interface should be used.
4397 */
4398 *group_iface = 0;
4399
4400 if (freq)
4401 *freq = 0;
4402
4403 go = wpas_p2p_get_go_group(wpa_s);
4404 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004405 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004406 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004407 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004408 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
4409 else
4410 return 0;
4411 } else {
4412 s = go->current_ssid;
4413 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
4414 if (freq)
4415 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004416 }
4417
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004418 os_memcpy(ssid, s->ssid, s->ssid_len);
4419 *ssid_len = s->ssid_len;
4420
4421 return 1;
4422}
4423
4424
4425static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4426 const u8 *ssid, size_t ssid_len)
4427{
4428 struct wpa_supplicant *wpa_s = ctx;
4429 struct wpa_ssid *s;
4430 int save_config = 0;
4431 size_t i;
4432
4433 /* Start with our first choice of Persistent Groups */
4434 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4435 if (go && ssid && ssid_len &&
4436 s->ssid_len == ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004437 ether_addr_equal(go, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004438 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4439 break;
4440
4441 /* Remove stale persistent group */
4442 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004443 wpa_dbg(wpa_s, MSG_DEBUG,
4444 "P2P: Remove stale persistent group id=%d",
4445 s->id);
4446 wpas_notify_persistent_group_removed(wpa_s, s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004447 wpa_config_remove_network(wpa_s->conf, s->id);
4448 save_config = 1;
4449 continue;
4450 }
4451
4452 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004453 if (!ether_addr_equal(s->p2p_client_list +
4454 i * 2 * ETH_ALEN, peer))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004455 continue;
4456
4457 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4458 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4459 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4460 break;
4461 }
4462 s->num_p2p_clients--;
4463 save_config = 1;
4464 }
4465
4466 if (save_config)
4467 p2p_config_write(wpa_s);
4468
4469 /* Return TRUE if valid SSID remains */
4470 return s != NULL;
4471}
4472
4473
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004474static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
4475 const u8 *feat_cap, size_t feat_cap_len)
4476{
4477 static const char pref[] = " feature_cap=";
4478 int ret;
4479
4480 buf[0] = '\0';
4481
4482 /*
4483 * We expect a feature capability to contain at least one byte to be
4484 * reported. The string buffer provided by the caller function is
4485 * expected to be big enough to contain all bytes of the attribute for
4486 * known specifications. This function truncates the reported bytes if
4487 * the feature capability data exceeds the string buffer size.
4488 */
4489 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4490 return;
4491
4492 os_memcpy(buf, pref, sizeof(pref));
4493 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4494 buf_len - sizeof(pref) + 1,
4495 feat_cap, feat_cap_len);
4496
4497 if (ret != (2 * (int) feat_cap_len))
4498 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4499}
4500
4501
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004502static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4503 const u8 *adv_mac, const u8 *ses_mac,
4504 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4505 u8 conncap, int passwd_id,
4506 const u8 *persist_ssid,
4507 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004508 int prov_start, const char *session_info,
4509 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004510 unsigned int freq,
4511 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004512{
4513 struct wpa_supplicant *wpa_s = ctx;
4514 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004515 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004516 int save_config = 0;
4517 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004518 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004519
4520 if (!dev)
4521 return;
4522
4523 os_memset(mac, 0, ETH_ALEN);
4524 if (!adv_mac)
4525 adv_mac = mac;
4526 if (!ses_mac)
4527 ses_mac = mac;
4528 if (!grp_mac)
4529 grp_mac = mac;
4530
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004531 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4532 feat_cap, feat_cap_len);
4533
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004534 if (prov_start) {
4535 if (session_info == NULL) {
4536 wpa_msg_global(wpa_s, MSG_INFO,
4537 P2P_EVENT_P2PS_PROVISION_START MACSTR
4538 " adv_id=%x conncap=%x"
4539 " adv_mac=" MACSTR
4540 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004541 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004542 MAC2STR(dev), adv_id, conncap,
4543 MAC2STR(adv_mac),
4544 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004545 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004546 } else {
4547 wpa_msg_global(wpa_s, MSG_INFO,
4548 P2P_EVENT_P2PS_PROVISION_START MACSTR
4549 " adv_id=%x conncap=%x"
4550 " adv_mac=" MACSTR
4551 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004552 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004553 MAC2STR(dev), adv_id, conncap,
4554 MAC2STR(adv_mac),
4555 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004556 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004557 }
4558 return;
4559 }
4560
4561 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4562 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4563
4564 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4565 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4566 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4567
4568 if (persistent_go && !persistent_go->num_p2p_clients) {
4569 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004570 wpa_dbg(wpa_s, MSG_DEBUG,
4571 "P2P: Remove empty persistent group id=%d",
4572 persistent_go->id);
4573 wpas_notify_persistent_group_removed(wpa_s,
4574 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004575 wpa_config_remove_network(wpa_s->conf,
4576 persistent_go->id);
4577 }
4578
4579 wpa_msg_global(wpa_s, MSG_INFO,
4580 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4581 " status=%d"
4582 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004583 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004584 MAC2STR(dev), status,
4585 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004586 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004587 return;
4588 }
4589
4590 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004591 if (persist_ssid && persist_ssid_size)
4592 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4593 persist_ssid_size);
4594
4595 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4596 is_zero_ether_addr(grp_mac)) {
4597 wpa_dbg(wpa_s, MSG_ERROR,
4598 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4599 return;
4600 }
4601
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004602 for (;;) {
4603 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4604 if (!stale)
4605 break;
4606
4607 if (s && s->ssid_len == stale->ssid_len &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004608 ether_addr_equal(stale->bssid, s->bssid) &&
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004609 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4610 break;
4611
4612 /* Remove stale persistent group */
4613 if (stale->mode != WPAS_MODE_P2P_GO ||
4614 stale->num_p2p_clients <= 1) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004615 wpa_dbg(wpa_s, MSG_DEBUG,
4616 "P2P: Remove stale persistent group id=%d",
4617 stale->id);
4618 wpas_notify_persistent_group_removed(wpa_s, stale);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004619 wpa_config_remove_network(wpa_s->conf, stale->id);
4620 } else {
4621 size_t i;
4622
4623 for (i = 0; i < stale->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00004624 if (ether_addr_equal(stale->p2p_client_list +
4625 i * ETH_ALEN, dev)) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004626 os_memmove(stale->p2p_client_list +
4627 i * ETH_ALEN,
4628 stale->p2p_client_list +
4629 (i + 1) * ETH_ALEN,
4630 (stale->num_p2p_clients -
4631 i - 1) * ETH_ALEN);
4632 break;
4633 }
4634 }
4635 stale->num_p2p_clients--;
4636 }
4637 save_config = 1;
4638 }
4639
4640 if (save_config)
4641 p2p_config_write(wpa_s);
4642
4643 if (s) {
4644 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4645 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4646
4647 if (persistent_go && s != persistent_go &&
4648 !persistent_go->num_p2p_clients) {
4649 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004650 wpa_dbg(wpa_s, MSG_DEBUG,
4651 "P2P: Remove empty persistent group id=%d",
4652 persistent_go->id);
4653 wpas_notify_persistent_group_removed(wpa_s,
4654 persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004655 wpa_config_remove_network(wpa_s->conf,
4656 persistent_go->id);
4657 /* Save config */
4658 }
4659
4660 wpa_msg_global(wpa_s, MSG_INFO,
4661 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4662 " status=%d"
4663 " adv_id=%x adv_mac=" MACSTR
4664 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004665 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004666 MAC2STR(dev), status,
4667 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004668 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004669 return;
4670 }
4671
Hai Shalom5f92bc92019-04-18 11:54:11 -07004672 wpa_s->global->pending_p2ps_group = 0;
4673 wpa_s->global->pending_p2ps_group_freq = 0;
4674
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004675 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004676 /*
4677 * We need to copy the interface name. Simply saving a
4678 * pointer isn't enough, since if we use pending_interface_name
4679 * it will be overwritten when the group is added.
4680 */
4681 char go_ifname[100];
4682
4683 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004684 if (!go_wpa_s) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07004685 if (!response_done) {
4686 wpa_s->global->pending_p2ps_group = 1;
4687 wpa_s->global->pending_p2ps_group_freq = freq;
4688 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004689
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004690 if (!wpas_p2p_create_iface(wpa_s))
4691 os_memcpy(go_ifname, wpa_s->ifname,
4692 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004693 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004694 os_memcpy(go_ifname,
4695 wpa_s->pending_interface_name,
4696 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004697
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004698 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004699 wpas_p2ps_prov_complete(
4700 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4701 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004702 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004703 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4704 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004705 return;
4706 }
4707
4708 /* If PD Resp complete, start up the GO */
4709 if (response_done && persistent_go) {
4710 wpas_p2p_group_add_persistent(
4711 wpa_s, persistent_go,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004712 0, 0, freq, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004713 persistent_go->mode ==
4714 WPAS_MODE_P2P_GO ?
4715 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Sunil Ravi036cec52023-03-29 11:35:17 -07004716 0, 0, false, 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004717 } else if (response_done) {
Hai Shalom74f70d42019-02-11 14:42:39 -08004718 wpas_p2p_group_add(wpa_s, 1, freq,
Hai Shalomc1a21442022-02-04 13:43:00 -08004719 0, 0, 0, 0, 0, 0, false);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004720 }
4721
4722 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004723 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4724 ETH_ALEN);
4725 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004726 }
4727 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004728 os_memcpy(go_ifname, go_wpa_s->ifname,
4729 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004731 if (is_zero_ether_addr(grp_mac)) {
4732 wpa_dbg(go_wpa_s, MSG_DEBUG,
4733 "P2P: Setting PIN-1 for ANY");
4734 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4735 "12345670", NULL, 0,
4736 0);
4737 } else {
4738 wpa_dbg(go_wpa_s, MSG_DEBUG,
4739 "P2P: Setting PIN-1 for " MACSTR,
4740 MAC2STR(grp_mac));
4741 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4742 "12345670", NULL, 0,
4743 0);
4744 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004745
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004746 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4747 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004748 }
4749
4750 wpa_msg_global(wpa_s, MSG_INFO,
4751 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4752 " status=%d conncap=%x"
4753 " adv_id=%x adv_mac=" MACSTR
4754 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004755 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004756 MAC2STR(dev), status, conncap,
4757 adv_id, MAC2STR(adv_mac),
4758 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004759 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004760 return;
4761 }
4762
4763 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4764 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4765
4766 if (persistent_go && !persistent_go->num_p2p_clients) {
4767 /* remove empty persistent GO */
Hai Shalom74f70d42019-02-11 14:42:39 -08004768 wpa_dbg(wpa_s, MSG_DEBUG,
4769 "P2P: Remove empty persistent group id=%d",
4770 persistent_go->id);
4771 wpas_notify_persistent_group_removed(wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004772 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4773 }
4774
4775 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004776 char ssid_hex[32 * 2 + 1];
4777
4778 if (group_ssid)
4779 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4780 group_ssid, group_ssid_len);
4781 else
4782 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004783 wpa_msg_global(wpa_s, MSG_INFO,
4784 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4785 " status=%d conncap=%x"
4786 " adv_id=%x adv_mac=" MACSTR
4787 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004788 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004789 MAC2STR(dev), status, conncap,
4790 adv_id, MAC2STR(adv_mac),
4791 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004792 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4793 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004794 } else {
4795 wpa_msg_global(wpa_s, MSG_INFO,
4796 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4797 " status=%d conncap=%x"
4798 " adv_id=%x adv_mac=" MACSTR
4799 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004800 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004801 MAC2STR(dev), status, conncap,
4802 adv_id, MAC2STR(adv_mac),
4803 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004804 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004805 }
4806}
4807
4808
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004809static int _wpas_p2p_in_progress(void *ctx)
4810{
4811 struct wpa_supplicant *wpa_s = ctx;
4812 return wpas_p2p_in_progress(wpa_s);
4813}
4814
4815
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004816static int wpas_prov_disc_resp_cb(void *ctx)
4817{
4818 struct wpa_supplicant *wpa_s = ctx;
4819 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004820 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004821
4822 if (!wpa_s->global->pending_p2ps_group)
4823 return 0;
4824
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004825 freq = wpa_s->global->pending_p2ps_group_freq;
4826 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004827 wpa_s->global->pending_p2ps_group = 0;
4828
4829 if (wpas_p2p_get_go_group(wpa_s))
4830 return 0;
4831 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4832
4833 if (persistent_go) {
4834 wpas_p2p_group_add_persistent(
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08004835 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4836 NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004837 persistent_go->mode == WPAS_MODE_P2P_GO ?
Hai Shalomc1a21442022-02-04 13:43:00 -08004838 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0,
Sunil Ravi036cec52023-03-29 11:35:17 -07004839 is_p2p_allow_6ghz(wpa_s->global->p2p), 0, NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004840 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08004841 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0, 0, 0,
4842 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004843 }
4844
4845 return 1;
4846}
4847
4848
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004849static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4850 unsigned int *len,
Sunil8cd6f4d2022-06-28 18:40:46 +00004851 struct weighted_pcl *freq_list)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004852{
4853 struct wpa_supplicant *wpa_s = ctx;
4854
4855 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4856 WPA_IF_P2P_CLIENT, len, freq_list);
4857}
4858
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004859int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4860{
JaeMan Park9de97322022-07-11 15:36:43 +09004861 int ret = 0;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004862 u8 addr[ETH_ALEN] = {0};
4863
4864 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4865 return 0;
4866
Hai Shalom60840252021-02-19 19:02:11 -08004867 if (wpa_s->conf->p2p_device_random_mac_addr == 2) {
4868 if (is_zero_ether_addr(
4869 wpa_s->conf->p2p_device_persistent_mac_addr) &&
4870 !is_zero_ether_addr(wpa_s->own_addr)) {
4871 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr,
4872 wpa_s->own_addr, ETH_ALEN);
4873 }
4874 return 0;
4875 }
4876
4877 if (!wpa_s->conf->ssid) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004878 if (random_mac_addr(addr) < 0) {
4879 wpa_msg(wpa_s, MSG_INFO,
4880 "Failed to generate random MAC address");
4881 return -EINVAL;
4882 }
4883
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004884 /* Store generated MAC address. */
4885 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4886 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004887 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004888 /* If there are existing saved groups, restore last MAC address.
4889 * if there is no last used MAC address, the last one is
4890 * factory MAC. */
4891 if (is_zero_ether_addr(
4892 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004893 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004894 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4895 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004896 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4897 }
4898
JaeMan Park9de97322022-07-11 15:36:43 +09004899 ret = wpa_drv_set_mac_addr(wpa_s, addr);
4900
4901 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004902 wpa_msg(wpa_s, MSG_INFO,
4903 "Failed to set random MAC address");
JaeMan Park9de97322022-07-11 15:36:43 +09004904 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004905 }
4906
JaeMan Park9de97322022-07-11 15:36:43 +09004907 ret = wpa_supplicant_update_mac_addr(wpa_s);
4908
4909 if (ret < 0) {
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004910 wpa_msg(wpa_s, MSG_INFO,
4911 "Could not update MAC address information");
JaeMan Park9de97322022-07-11 15:36:43 +09004912 return ret;
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004913 }
4914
4915 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4916 MAC2STR(addr));
4917
4918 return 0;
4919}
4920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921/**
4922 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4923 * @global: Pointer to global data from wpa_supplicant_init()
4924 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4925 * Returns: 0 on success, -1 on failure
4926 */
4927int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4928{
4929 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 int i;
4931
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004932 if (wpa_s->conf->p2p_disabled)
4933 return 0;
4934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4936 return 0;
4937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 if (global->p2p)
4939 return 0;
4940
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004941 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4942 wpa_msg(wpa_s, MSG_ERROR,
4943 "Failed to initialize P2P random MAC address.");
4944 return -1;
4945 }
4946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004949 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950 p2p.p2p_scan = wpas_p2p_scan;
4951 p2p.send_action = wpas_send_action;
4952 p2p.send_action_done = wpas_send_action_done;
4953 p2p.go_neg_completed = wpas_go_neg_completed;
4954 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4955 p2p.dev_found = wpas_dev_found;
4956 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004957 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 p2p.start_listen = wpas_start_listen;
4959 p2p.stop_listen = wpas_stop_listen;
4960 p2p.send_probe_resp = wpas_send_probe_resp;
4961 p2p.sd_request = wpas_sd_request;
4962 p2p.sd_response = wpas_sd_response;
4963 p2p.prov_disc_req = wpas_prov_disc_req;
4964 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004965 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 p2p.invitation_process = wpas_invitation_process;
4967 p2p.invitation_received = wpas_invitation_received;
4968 p2p.invitation_result = wpas_invitation_result;
4969 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004970 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004971 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004972 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004973 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004974 p2p.get_persistent_group = wpas_get_persistent_group;
4975 p2p.get_go_info = wpas_get_go_info;
4976 p2p.remove_stale_groups = wpas_remove_stale_groups;
4977 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4978 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4979 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004980 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Hai Shalom60840252021-02-19 19:02:11 -08004981 p2p.p2p_6ghz_disable = wpa_s->conf->p2p_6ghz_disable;
Shuibing Daie2fad412023-05-05 14:08:11 -07004982 p2p.p2p_dfs_chan_enable = wpa_s->conf->p2p_dfs_chan_enable;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004983
4984 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004985 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986 p2p.dev_name = wpa_s->conf->device_name;
4987 p2p.manufacturer = wpa_s->conf->manufacturer;
4988 p2p.model_name = wpa_s->conf->model_name;
4989 p2p.model_number = wpa_s->conf->model_number;
4990 p2p.serial_number = wpa_s->conf->serial_number;
4991 if (wpa_s->wps) {
4992 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4993 p2p.config_methods = wpa_s->wps->config_methods;
4994 }
4995
Hai Shalom60840252021-02-19 19:02:11 -08004996 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels,
4997 p2p.p2p_6ghz_disable)) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004998 wpa_printf(MSG_ERROR,
4999 "P2P: Failed to configure supported channel list");
5000 return -1;
5001 }
5002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 if (wpa_s->conf->p2p_listen_reg_class &&
5004 wpa_s->conf->p2p_listen_channel) {
5005 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5006 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005007 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009 /*
5010 * Pick one of the social channels randomly as the listen
5011 * channel.
5012 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005013 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005014 &p2p.channel,
5015 &global->p2p_go_avoid_freq,
5016 &global->p2p_disallow_freq) !=
5017 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07005018 wpa_printf(MSG_INFO,
5019 "P2P: No social channels supported by the driver - do not enable P2P");
5020 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005021 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005022 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005023 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005024 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5025 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026
5027 if (wpa_s->conf->p2p_oper_reg_class &&
5028 wpa_s->conf->p2p_oper_channel) {
5029 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5030 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5031 p2p.cfg_op_channel = 1;
5032 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5033 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5034
5035 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005037 * Use random operation channel from 2.4 GHz band social
5038 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5039 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005041 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
Hai Shalom74f70d42019-02-11 14:42:39 -08005042 &p2p.op_channel, NULL,
5043 NULL) != 0) {
5044 wpa_printf(MSG_INFO,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005045 "P2P: Failed to select random social channel as operation channel");
Hai Shalom74f70d42019-02-11 14:42:39 -08005046 p2p.op_reg_class = 0;
5047 p2p.op_channel = 0;
5048 /* This will be overridden during group setup in
5049 * p2p_prepare_channel(), so allow setup to continue. */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005050 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051 p2p.cfg_op_channel = 0;
5052 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5053 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5054 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005055
5056 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5057 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5058 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5059 }
5060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5062 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5063 p2p.country[2] = 0x04;
5064 } else
5065 os_memcpy(p2p.country, "XX\x04", 3);
5066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5068 WPS_DEV_TYPE_LEN);
5069
5070 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5071 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5072 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5073
5074 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5075 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5076
5077 p2p.max_peers = 100;
5078
5079 if (wpa_s->conf->p2p_ssid_postfix) {
5080 p2p.ssid_postfix_len =
5081 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5082 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5083 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5084 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5085 p2p.ssid_postfix_len);
5086 }
5087
5088 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5089
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005090 p2p.max_listen = wpa_s->max_remain_on_chan;
5091
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005092 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5093 wpa_s->conf->p2p_passphrase_len <= 63)
5094 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5095 else
5096 p2p.passphrase_len = 8;
5097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 global->p2p = p2p_init(&p2p);
5099 if (global->p2p == NULL)
5100 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005101 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102
5103 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5104 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5105 continue;
5106 p2p_add_wps_vendor_extension(
5107 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5108 }
5109
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005110 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112 return 0;
5113}
5114
5115
5116/**
5117 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5118 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5119 *
5120 * This function deinitialize per-interface P2P data.
5121 */
5122void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5123{
5124 if (wpa_s->driver && wpa_s->drv_priv)
5125 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005126
5127 if (wpa_s->go_params) {
5128 /* Clear any stored provisioning info */
5129 p2p_clear_provisioning_info(
5130 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005131 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005132 }
5133
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005134 os_free(wpa_s->go_params);
5135 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005136 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5138 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07005139 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5141 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5142 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005143 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07005144 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005145 wpas_p2p_listen_work_done(wpa_s);
5146 if (wpa_s->p2p_send_action_work) {
5147 os_free(wpa_s->p2p_send_action_work->ctx);
5148 radio_work_done(wpa_s->p2p_send_action_work);
5149 wpa_s->p2p_send_action_work = NULL;
5150 }
5151 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005153 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5154 wpa_s->p2p_oob_dev_pw = NULL;
5155
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005156 os_free(wpa_s->p2p_group_common_freqs);
5157 wpa_s->p2p_group_common_freqs = NULL;
5158 wpa_s->p2p_group_common_freqs_num = 0;
5159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160 /* TODO: remove group interface from the driver if this wpa_s instance
5161 * is on top of a P2P group interface */
5162}
5163
5164
5165/**
5166 * wpas_p2p_deinit_global - Deinitialize global P2P module
5167 * @global: Pointer to global data from wpa_supplicant_init()
5168 *
5169 * This function deinitializes the global (per device) P2P module.
5170 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005171static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172{
5173 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005174
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005175 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005176
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005177 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178
5179 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5181 wpa_s = wpa_s->next;
5182 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 tmp = global->ifaces;
5184 while (tmp &&
5185 (tmp == wpa_s ||
5186 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5187 tmp = tmp->next;
5188 }
5189 if (tmp == NULL)
5190 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005191 /* Disconnect from the P2P group and deinit the interface */
5192 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 }
5194
5195 /*
5196 * Deinit GO data on any possibly remaining interface (if main
5197 * interface is used as GO).
5198 */
5199 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5200 if (wpa_s->ap_iface)
5201 wpas_p2p_group_deinit(wpa_s);
5202 }
5203
5204 p2p_deinit(global->p2p);
5205 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005206 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207}
5208
5209
5210static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5211{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005212 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005213 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 if (wpa_s->drv_flags &
5215 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5216 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5217 return 1; /* P2P group requires a new interface in every case
5218 */
5219 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5220 return 0; /* driver does not support concurrent operations */
5221 if (wpa_s->global->ifaces->next)
5222 return 1; /* more that one interface already in use */
5223 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5224 return 1; /* this interface is already in use */
5225 return 0;
5226}
5227
5228
5229static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5230 const u8 *peer_addr,
5231 enum p2p_wps_method wps_method,
5232 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005233 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005234 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005236 if (persistent_group && wpa_s->conf->persistent_reconnect)
5237 persistent_group = 2;
5238
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005239 /*
5240 * Increase GO config timeout if HT40 is used since it takes some time
5241 * to scan channels for coex purposes before the BSS can be started.
5242 */
5243 p2p_set_config_timeout(wpa_s->global->p2p,
5244 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5247 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005248 persistent_group, ssid ? ssid->ssid : NULL,
5249 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005250 wpa_s->p2p_pd_before_go_neg, pref_freq,
5251 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5252 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253}
5254
5255
5256static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5257 const u8 *peer_addr,
5258 enum p2p_wps_method wps_method,
5259 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005260 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005261 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005262{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005263 if (persistent_group && wpa_s->conf->persistent_reconnect)
5264 persistent_group = 2;
5265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005266 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5267 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005268 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005269 ssid ? ssid->ssid_len : 0, pref_freq,
5270 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5271 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272}
5273
5274
5275static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5276{
5277 wpa_s->p2p_join_scan_count++;
5278 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5279 wpa_s->p2p_join_scan_count);
5280 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5281 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5282 " for join operationg - stop join attempt",
5283 MAC2STR(wpa_s->pending_join_iface_addr));
5284 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005285 if (wpa_s->p2p_auto_pd) {
5286 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005287 wpa_msg_global(wpa_s, MSG_INFO,
5288 P2P_EVENT_PROV_DISC_FAILURE
5289 " p2p_dev_addr=" MACSTR " status=N/A",
5290 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005291 return;
5292 }
Jimmy Chenb7b3f4d2020-09-02 16:50:11 +08005293 if (wpa_s->p2p_fallback_to_go_neg) {
5294 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Join operating "
5295 "failed - fall back to GO Negotiation");
5296 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
5297 P2P_EVENT_FALLBACK_TO_GO_NEG
5298 "reason=join-failed");
5299 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
5300 return;
5301 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005302 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005303 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005304 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 }
5306}
5307
5308
Dmitry Shmidt04949592012-07-19 12:16:46 -07005309static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5310{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005311 int res;
5312 unsigned int num, i;
5313 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005314
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005315 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5316 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005317 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005318 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005320 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5321 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005322 if (!freqs)
5323 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005324
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005325 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5326 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005327
5328 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005329 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005330 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5331 freq);
5332 res = 0;
5333 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 }
5335 }
5336
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005337 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005338 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005339
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005340exit_free:
5341 os_free(freqs);
5342 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005343}
5344
5345
5346static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5347 const u8 *peer_dev_addr)
5348{
5349 struct wpa_bss *bss;
5350 int updated;
5351
5352 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5353 if (bss == NULL)
5354 return -1;
5355 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5356 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5357 "last scan");
5358 return 0;
5359 }
5360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005361 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5362 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005363 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5364 "%ld.%06ld (%supdated in last scan)",
5365 bss->last_update.sec, bss->last_update.usec,
5366 updated ? "": "not ");
5367
5368 return updated;
5369}
5370
5371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5373 struct wpa_scan_results *scan_res)
5374{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005375 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005376 int freq;
5377 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5380
5381 if (wpa_s->global->p2p_disabled)
5382 return;
5383
Dmitry Shmidt04949592012-07-19 12:16:46 -07005384 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5385 scan_res ? (int) scan_res->num : -1,
5386 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387
5388 if (scan_res)
5389 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5390
Dmitry Shmidt04949592012-07-19 12:16:46 -07005391 if (wpa_s->p2p_auto_pd) {
5392 int join = wpas_p2p_peer_go(wpa_s,
5393 wpa_s->pending_join_dev_addr);
5394 if (join == 0 &&
5395 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5396 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005397 bss = wpa_bss_get_bssid_latest(
5398 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005399 if (bss) {
5400 freq = bss->freq;
5401 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5402 "the peer " MACSTR " at %d MHz",
5403 wpa_s->auto_pd_scan_retry,
5404 MAC2STR(wpa_s->
5405 pending_join_dev_addr),
5406 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005407 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005408 return;
5409 }
5410 }
5411
5412 if (join < 0)
5413 join = 0;
5414
5415 wpa_s->p2p_auto_pd = 0;
5416 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5417 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5418 MAC2STR(wpa_s->pending_join_dev_addr), join);
5419 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005420 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005421 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005422 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005423 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005424 wpa_msg_global(wpa_s, MSG_INFO,
5425 P2P_EVENT_PROV_DISC_FAILURE
5426 " p2p_dev_addr=" MACSTR " status=N/A",
5427 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005428 }
5429 return;
5430 }
5431
5432 if (wpa_s->p2p_auto_join) {
5433 int join = wpas_p2p_peer_go(wpa_s,
5434 wpa_s->pending_join_dev_addr);
5435 if (join < 0) {
5436 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5437 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005438 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005439 P2P_EVENT_FALLBACK_TO_GO_NEG
5440 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005441 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5442 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5443 wpa_s->p2p_persistent_group, 0, 0, 0,
5444 wpa_s->p2p_go_intent,
5445 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005446 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005447 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005448 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005449 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005450 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005451 wpa_s->p2p_go_max_oper_chwidth,
Hai Shalom74f70d42019-02-11 14:42:39 -08005452 wpa_s->p2p_go_he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005453 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08005454 NULL, 0,
5455 is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005456 return;
5457 }
5458
5459 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5460 "try to join the group", join ? "" :
5461 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005462 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005463 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005464 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005465 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005466 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 }
5468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5470 wpa_s->pending_join_iface_addr);
5471 if (freq < 0 &&
5472 p2p_get_interface_addr(wpa_s->global->p2p,
5473 wpa_s->pending_join_dev_addr,
5474 iface_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005475 !ether_addr_equal(iface_addr, wpa_s->pending_join_dev_addr) &&
5476 !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5478 "address for join from " MACSTR " to " MACSTR
5479 " based on newly discovered P2P peer entry",
5480 MAC2STR(wpa_s->pending_join_iface_addr),
5481 MAC2STR(iface_addr));
5482 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5483 ETH_ALEN);
5484
5485 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5486 wpa_s->pending_join_iface_addr);
5487 }
5488 if (freq >= 0) {
5489 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5490 "from P2P peer table: %d MHz", freq);
5491 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005492 if (wpa_s->p2p_join_ssid_len) {
5493 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5494 MACSTR " and SSID %s",
5495 MAC2STR(wpa_s->pending_join_iface_addr),
5496 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5497 wpa_s->p2p_join_ssid_len));
5498 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5499 wpa_s->p2p_join_ssid,
5500 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005501 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005502 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5503 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5504 bss = wpa_bss_get_bssid_latest(wpa_s,
5505 wpa_s->pending_join_iface_addr);
5506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005507 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005508 u8 dev_addr[ETH_ALEN];
5509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 freq = bss->freq;
5511 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005512 "from BSS table: %d MHz (SSID %s)", freq,
5513 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Hai Shalom60840252021-02-19 19:02:11 -08005514 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005515 dev_addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00005516 ether_addr_equal(wpa_s->pending_join_dev_addr,
5517 wpa_s->pending_join_iface_addr) &&
5518 !ether_addr_equal(dev_addr, wpa_s->pending_join_dev_addr)) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005519 wpa_printf(MSG_DEBUG,
5520 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
5521 MAC2STR(dev_addr),
5522 MAC2STR(wpa_s->pending_join_dev_addr));
5523 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
5524 ETH_ALEN);
5525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005526 }
5527 if (freq > 0) {
5528 u16 method;
5529
Dmitry Shmidt04949592012-07-19 12:16:46 -07005530 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005531 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005532 P2P_EVENT_GROUP_FORMATION_FAILURE
5533 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005534 wpas_notify_p2p_group_formation_failure(
5535 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005536 return;
5537 }
5538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5540 "prior to joining an existing group (GO " MACSTR
5541 " freq=%u MHz)",
5542 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5543 wpa_s->pending_pd_before_join = 1;
5544
5545 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546 case WPS_PIN_DISPLAY:
5547 method = WPS_CONFIG_KEYPAD;
5548 break;
5549 case WPS_PIN_KEYPAD:
5550 method = WPS_CONFIG_DISPLAY;
5551 break;
5552 case WPS_PBC:
5553 method = WPS_CONFIG_PUSHBUTTON;
5554 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005555 case WPS_P2PS:
5556 method = WPS_CONFIG_P2PS;
5557 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558 default:
5559 method = 0;
5560 break;
5561 }
5562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005563 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5564 wpa_s->pending_join_dev_addr) ==
5565 method)) {
5566 /*
5567 * We have already performed provision discovery for
5568 * joining the group. Proceed directly to join
5569 * operation without duplicated provision discovery. */
5570 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5571 "with " MACSTR " already done - proceed to "
5572 "join",
5573 MAC2STR(wpa_s->pending_join_dev_addr));
5574 wpa_s->pending_pd_before_join = 0;
5575 goto start;
5576 }
5577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005579 wpa_s->pending_join_dev_addr,
5580 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005581 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5583 "Discovery Request before joining an "
5584 "existing group");
5585 wpa_s->pending_pd_before_join = 0;
5586 goto start;
5587 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 return;
5589 }
5590
5591 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5592 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5593 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5594 wpas_p2p_check_join_scan_limit(wpa_s);
5595 return;
5596
5597start:
5598 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005599 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5600 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601}
5602
5603
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005604static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5605 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607 int ret;
5608 struct wpa_driver_scan_params params;
5609 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005610 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005611 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005612 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005613
5614 os_memset(&params, 0, sizeof(params));
5615
5616 /* P2P Wildcard SSID */
5617 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005618 if (ssid && ssid_len) {
5619 params.ssids[0].ssid = ssid;
5620 params.ssids[0].ssid_len = ssid_len;
5621 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5622 wpa_s->p2p_join_ssid_len = ssid_len;
5623 } else {
5624 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5625 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5626 wpa_s->p2p_join_ssid_len = 0;
5627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005628
5629 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005630 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5631 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5632 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633 if (wps_ie == NULL) {
5634 wpas_p2p_scan_res_join(wpa_s, NULL);
5635 return;
5636 }
5637
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005638 if (!freq) {
5639 int oper_freq;
5640 /*
5641 * If freq is not provided, check the operating freq of the GO
5642 * and use a single channel scan on if possible.
5643 */
5644 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5645 wpa_s->pending_join_iface_addr);
5646 if (oper_freq > 0)
5647 freq = oper_freq;
5648 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005649 if (freq > 0) {
5650 freqs[0] = freq;
5651 params.freqs = freqs;
Sunil Ravi77d572f2023-01-17 23:58:31 +00005652 } else {
5653 wpas_p2p_scan_freqs(wpa_s, &params, true);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005654 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005656 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5657 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5658 if (ies == NULL) {
5659 wpabuf_free(wps_ie);
5660 wpas_p2p_scan_res_join(wpa_s, NULL);
5661 return;
5662 }
5663 wpabuf_put_buf(ies, wps_ie);
5664 wpabuf_free(wps_ie);
5665
5666 bands = wpas_get_bands(wpa_s, freqs);
5667 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5668
5669 params.p2p_probe = 1;
5670 params.extra_ies = wpabuf_head(ies);
5671 params.extra_ies_len = wpabuf_len(ies);
5672
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005673 if (wpa_s->clear_driver_scan_cache) {
5674 wpa_printf(MSG_DEBUG,
5675 "Request driver to clear scan cache due to local BSS flush");
5676 params.only_new_results = 1;
5677 }
5678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005679 /*
5680 * Run a scan to update BSS table and start Provision Discovery once
5681 * the new scan results become available.
5682 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005683 ret = wpa_drv_scan(wpa_s, &params);
Hai Shalomc1a21442022-02-04 13:43:00 -08005684 if (params.freqs != freqs)
Hai Shalom899fcc72020-10-19 14:38:18 -07005685 os_free(params.freqs);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005686 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005687 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005688 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005689 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005690 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005691 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005692
5693 wpabuf_free(ies);
5694
5695 if (ret) {
5696 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5697 "try again later");
5698 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5699 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5700 wpas_p2p_check_join_scan_limit(wpa_s);
5701 }
5702}
5703
5704
Dmitry Shmidt04949592012-07-19 12:16:46 -07005705static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5706{
5707 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005708 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005709}
5710
5711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005713 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005714 int auto_join, int op_freq,
5715 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716{
5717 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005718 MACSTR " dev " MACSTR " op_freq=%d)%s",
5719 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005720 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005721 if (ssid && ssid_len) {
5722 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5723 wpa_ssid_txt(ssid, ssid_len));
5724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725
Dmitry Shmidt04949592012-07-19 12:16:46 -07005726 wpa_s->p2p_auto_pd = 0;
5727 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5729 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5730 wpa_s->pending_join_wps_method = wps_method;
5731
5732 /* Make sure we are not running find during connection establishment */
5733 wpas_p2p_stop_find(wpa_s);
5734
5735 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005736 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737 return 0;
5738}
5739
5740
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005741static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5742 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005743{
5744 struct wpa_supplicant *group;
5745 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005746 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005747
5748 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5749 if (group == NULL)
5750 return -1;
5751 if (group != wpa_s) {
5752 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5753 sizeof(group->p2p_pin));
5754 group->p2p_wps_method = wpa_s->p2p_wps_method;
5755 }
5756
Hai Shalom74f70d42019-02-11 14:42:39 -08005757 /*
5758 * Need to mark the current interface for p2p_group_formation
5759 * when a separate group interface is not used. This is needed
5760 * to allow p2p_cancel stop a pending p2p_connect-join.
5761 * wpas_p2p_init_group_interface() addresses this for the case
5762 * where a separate group interface is used.
5763 */
5764 if (group == wpa_s->parent)
5765 wpa_s->global->p2p_group_formation = group;
5766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005767 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005768 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005769
5770 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005771 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5773 ETH_ALEN);
5774 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005775 if (freq && ssid && ssid_len) {
5776 res.freq = freq;
5777 res.ssid_len = ssid_len;
5778 os_memcpy(res.ssid, ssid, ssid_len);
5779 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005780 if (ssid && ssid_len) {
5781 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5782 ssid, ssid_len);
5783 } else {
5784 bss = wpa_bss_get_bssid_latest(
5785 wpa_s, wpa_s->pending_join_iface_addr);
5786 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005787 if (bss) {
5788 res.freq = bss->freq;
5789 res.ssid_len = bss->ssid_len;
5790 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5791 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5792 bss->freq,
5793 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005794 } else if (ssid && ssid_len) {
5795 res.ssid_len = ssid_len;
5796 os_memcpy(res.ssid, ssid, ssid_len);
5797 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5798 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005799 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005800 }
5801
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005802 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5803 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5804 "starting client");
5805 wpa_drv_cancel_remain_on_channel(wpa_s);
5806 wpa_s->off_channel_freq = 0;
5807 wpa_s->roc_waiting_drv_freq = 0;
5808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809 wpas_start_wps_enrollee(group, &res);
5810
5811 /*
5812 * Allow a longer timeout for join-a-running-group than normal 15
5813 * second group formation timeout since the GO may not have authorized
5814 * our connection yet.
5815 */
5816 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5817 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5818 wpa_s, NULL);
5819
5820 return 0;
5821}
5822
5823
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005824static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005825 int *force_freq, int *pref_freq, int go,
Sunil8cd6f4d2022-06-28 18:40:46 +00005826 struct weighted_pcl *pref_freq_list,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005827 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005828{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005829 struct wpa_used_freq_data *freqs;
5830 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005831 unsigned int freq_in_use = 0, num, i, max_pref_freq;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305832 int p2p_pref_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005833
5834 max_pref_freq = *num_pref_freq;
5835 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005836
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005837 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5838 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005839 if (!freqs)
5840 return -1;
5841
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005842 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5843 wpa_s->num_multichan_concurrent);
5844
5845 /*
5846 * It is possible that the total number of used frequencies is bigger
5847 * than the number of frequencies used for P2P, so get the system wide
5848 * number of unused frequencies.
5849 */
5850 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5851
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005852 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005853 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5854 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005855
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005856 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005857 int ret;
5858 if (go)
5859 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5860 else
5861 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5862 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005863 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005864 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5865 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005866 /*
5867 * If freq is a DFS channel and DFS is offloaded
5868 * to the driver, allow P2P GO to use it.
5869 */
5870 wpa_printf(MSG_DEBUG,
5871 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5872 freq);
5873 } else {
5874 wpa_printf(MSG_DEBUG,
5875 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5876 freq);
5877 res = -3;
5878 goto exit_free;
5879 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005880 }
5881
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005882 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005883 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005884 freq_in_use = 1;
5885 }
5886
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005887 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005888 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5889 freq);
5890 res = -2;
5891 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005892 }
5893 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5894 "requested channel (%u MHz)", freq);
5895 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005896 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005897 }
5898
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005899 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005900
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305901 if (*pref_freq == 0) {
5902 if (wpa_s->conf->num_p2p_pref_chan && IS_2GHZ(best_freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005903 i = 0;
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305904 while (i < wpa_s->conf->num_p2p_pref_chan) {
5905 p2p_pref_freq = ieee80211_chan_to_freq(NULL,
5906 wpa_s->conf->p2p_pref_chan[i].op_class,
5907 wpa_s->conf->p2p_pref_chan[i].chan);
5908
5909 if (p2p_supported_freq(wpa_s->global->p2p, p2p_pref_freq) &&
5910 !wpas_p2p_disallowed_freq(wpa_s->global, p2p_pref_freq)) {
5911 best_freq = p2p_pref_freq;
5912 wpa_printf(MSG_DEBUG, "P2P: Using frequency (%u MHz) "
5913 "from P2P preferred channel list", best_freq);
5914 break;
5915 } else {
5916 wpa_printf(MSG_MSGDUMP, "P2P: Skipping preferred "
5917 "frequency (%u MHz) ", p2p_pref_freq);
5918 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005919 i++;
5920 }
Sreeramya Soratkalf928e8f2022-03-22 17:33:31 +05305921 } else if (!wpa_s->conf->num_p2p_pref_chan) {
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305922 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
Sunil Ravi036cec52023-03-29 11:35:17 -07005923 best_freq, go);
Vinayak Yadawad8db34572021-08-30 21:28:05 +05305924
Sunil Ravi036cec52023-03-29 11:35:17 -07005925 *num_pref_freq = max_pref_freq;
5926 res = wpas_p2p_pick_best_pref_freq(wpa_s, go, pref_freq_list,
5927 num_pref_freq);
5928 if (res > 0)
5929 best_freq = res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005930 }
5931 }
5932
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005933 /* We have a candidate frequency to use */
5934 if (best_freq > 0) {
5935 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005936 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005937 best_freq);
5938 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005939 } else {
5940 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 -07005941 best_freq);
5942 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005943 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005944 } else if (num_unused > 0) {
5945 wpa_printf(MSG_DEBUG,
5946 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5947 *force_freq = 0;
5948 } else {
5949 wpa_printf(MSG_DEBUG,
5950 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5951 res = -2;
5952 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005953 }
5954
5955exit_ok:
5956 res = 0;
5957exit_free:
5958 os_free(freqs);
5959 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005960}
5961
5962
Hai Shalomc1a21442022-02-04 13:43:00 -08005963static bool is_p2p_6ghz_supported(struct wpa_supplicant *wpa_s,
5964 const u8 *peer_addr)
5965{
5966 if (wpa_s->conf->p2p_6ghz_disable ||
5967 !get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
5968 HOSTAPD_MODE_IEEE80211A, true))
5969 return false;
5970
5971 if (!p2p_wfd_enabled(wpa_s->global->p2p))
5972 return false;
5973 if (peer_addr && !p2p_peer_wfd_enabled(wpa_s->global->p2p, peer_addr))
5974 return false;
5975
5976 return true;
5977}
5978
5979
5980static int wpas_p2p_check_6ghz(struct wpa_supplicant *wpa_s,
5981 const u8 *peer_addr, bool allow_6ghz, int freq)
5982{
5983 if (allow_6ghz && is_p2p_6ghz_supported(wpa_s, peer_addr)) {
5984 wpa_printf(MSG_DEBUG,
5985 "P2P: Allow connection on 6 GHz channels");
5986 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, true);
5987 } else {
5988 if (is_6ghz_freq(freq))
5989 return -2;
5990 p2p_set_6ghz_dev_capab(wpa_s->global->p2p, false);
5991 }
5992
5993 return 0;
5994}
5995
5996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997/**
5998 * wpas_p2p_connect - Request P2P Group Formation to be started
5999 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6000 * @peer_addr: Address of the peer P2P Device
6001 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
6002 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07006003 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006004 * @join: Whether to join an existing group (as a client) instead of starting
6005 * Group Owner negotiation; @peer_addr is BSSID in that case
6006 * @auth: Whether to only authorize the connection instead of doing that and
6007 * initiating Group Owner negotiation
6008 * @go_intent: GO Intent or -1 to use default
6009 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006010 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07006011 * @persistent_id: Persistent group credentials to use for forcing GO
6012 * parameters or -1 to generate new values (SSID/passphrase)
6013 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
6014 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006015 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006016 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006017 * @vht_chwidth: Channel width supported by GO operating with VHT support
Hai Shalom81f62d82019-07-22 12:10:00 -07006018 * (CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006019 * @group_ssid: Specific Group SSID for join or %NULL if not set
6020 * @group_ssid_len: Length of @group_ssid in octets
Hai Shalomc1a21442022-02-04 13:43:00 -08006021 * @allow_6ghz: Allow P2P connection on 6 GHz channels
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6023 * failure, -2 on failure due to channel not currently available,
6024 * -3 if forced channel is not supported
6025 */
6026int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6027 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006028 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006029 int go_intent, int freq, unsigned int vht_center_freq2,
6030 int persistent_id, int pd, int ht40, int vht,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006031 unsigned int vht_chwidth, int he, int edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08006032 const u8 *group_ssid, size_t group_ssid_len,
6033 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006034{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006035 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006036 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006038 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006039 struct wpa_ssid *ssid = NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00006040 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
6041 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042
6043 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6044 return -1;
6045
Dmitry Shmidt04949592012-07-19 12:16:46 -07006046 if (persistent_id >= 0) {
6047 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6048 if (ssid == NULL || ssid->disabled != 2 ||
6049 ssid->mode != WPAS_MODE_P2P_GO)
6050 return -1;
6051 }
6052
Hai Shalomc1a21442022-02-04 13:43:00 -08006053 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
Hai Shalom899fcc72020-10-19 14:38:18 -07006054 return -2;
6055
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006056 os_free(wpa_s->global->add_psk);
6057 wpa_s->global->add_psk = NULL;
6058
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006059 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006060 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006061 wpa_s->global->pending_p2ps_group_freq = 0;
6062 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064 if (go_intent < 0)
6065 go_intent = wpa_s->conf->p2p_go_intent;
6066
6067 if (!auth)
Hai Shalomfdcde762020-04-02 11:19:20 -07006068 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069
6070 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006071 wpa_s->p2p_persistent_group = !!persistent_group;
6072 wpa_s->p2p_persistent_id = persistent_id;
6073 wpa_s->p2p_go_intent = go_intent;
6074 wpa_s->p2p_connect_freq = freq;
6075 wpa_s->p2p_fallback_to_go_neg = 0;
6076 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006077 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006078 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006079 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
6080 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Hai Shalom74f70d42019-02-11 14:42:39 -08006081 wpa_s->p2p_go_he = !!he;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006082 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006083
6084 if (pin)
6085 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6086 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08006087 if (wps_generate_pin((unsigned int *) &ret) < 0)
6088 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006089 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6090 "%08d", ret);
6091 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6092 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6094 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006095 } else if (wps_method == WPS_P2PS) {
6096 /* Force the P2Ps default PIN to be used */
6097 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098 } else
6099 wpa_s->p2p_pin[0] = '\0';
6100
Dmitry Shmidt04949592012-07-19 12:16:46 -07006101 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6103 if (auth) {
6104 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6105 "connect a running group from " MACSTR,
6106 MAC2STR(peer_addr));
6107 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6108 return ret;
6109 }
6110 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6111 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6112 iface_addr) < 0) {
6113 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6114 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6115 dev_addr);
6116 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006117 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006118 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006119 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6120 "%ld.%06ld",
6121 wpa_s->p2p_auto_started.sec,
6122 wpa_s->p2p_auto_started.usec);
6123 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006124 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006125 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006126 auto_join, freq,
6127 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006128 return -1;
6129 return ret;
6130 }
6131
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006132 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006133 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006134 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006135 if (res)
6136 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006137 wpas_p2p_set_own_freq_preference(wpa_s,
6138 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006140 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006142 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6143
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006144 if (wpa_s->create_p2p_iface) {
6145 /* Prepare to add a new interface for the group */
6146 iftype = WPA_IF_P2P_GROUP;
6147 if (go_intent == 15)
6148 iftype = WPA_IF_P2P_GO;
6149 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6150 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6151 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006153 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006155 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006156 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006157 if (wpa_s->p2p_mgmt)
6158 if_addr = wpa_s->parent->own_addr;
6159 else
6160 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006161 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
6162 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163
6164 if (auth) {
6165 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006166 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006167 force_freq, persistent_group, ssid,
6168 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006169 return -1;
6170 return ret;
6171 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006172
6173 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6174 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006175 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006176 if (wpa_s->create_p2p_iface)
6177 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178 return -1;
6179 }
6180 return ret;
6181}
6182
6183
6184/**
6185 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6186 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6187 * @freq: Frequency of the channel in MHz
6188 * @duration: Duration of the stay on the channel in milliseconds
6189 *
6190 * This callback is called when the driver indicates that it has started the
6191 * requested remain-on-channel duration.
6192 */
6193void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6194 unsigned int freq, unsigned int duration)
6195{
6196 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6197 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006198 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)",
6199 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6200 wpa_s->roc_waiting_drv_freq, freq, duration);
6201 if (wpa_s->off_channel_freq &&
6202 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6204 wpa_s->pending_listen_duration);
6205 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006206 } else {
6207 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6208 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6209 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210 }
6211}
6212
6213
Jithu Jance57cea1a2014-08-20 10:22:04 -07006214int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215{
6216 /* Limit maximum Listen state time based on driver limitation. */
6217 if (timeout > wpa_s->max_remain_on_chan)
6218 timeout = wpa_s->max_remain_on_chan;
6219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006220 return p2p_listen(wpa_s->global->p2p, timeout);
6221}
6222
6223
6224/**
6225 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6226 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6227 * @freq: Frequency of the channel in MHz
6228 *
6229 * This callback is called when the driver indicates that a remain-on-channel
6230 * operation has been completed, i.e., the duration on the requested channel
6231 * has timed out.
6232 */
6233void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6234 unsigned int freq)
6235{
6236 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6237 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Hai Shalomfdcde762020-04-02 11:19:20 -07006238 wpa_s->global->p2p_long_listen,
6239 offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006240 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006241 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6242 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006243 if (wpa_s->global->p2p_long_listen > 0)
6244 wpa_s->global->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6246 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006247 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07006249 if (wpa_s->global->p2p_long_listen > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
Hai Shalomfdcde762020-04-02 11:19:20 -07006251 wpas_p2p_listen_start(wpa_s, wpa_s->global->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006252 } else {
6253 /*
6254 * When listen duration is over, stop listen & update p2p_state
6255 * to IDLE.
6256 */
6257 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258 }
6259}
6260
6261
6262/**
6263 * wpas_p2p_group_remove - Remove a P2P group
6264 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6265 * @ifname: Network interface name of the group interface or "*" to remove all
6266 * groups
6267 * Returns: 0 on success, -1 on failure
6268 *
6269 * This function is used to remove a P2P group. This can be used to disconnect
6270 * from a group in which the local end is a P2P Client or to end a P2P Group in
6271 * case the local end is the Group Owner. If a virtual network interface was
6272 * created for this group, that interface will be removed. Otherwise, only the
6273 * configured P2P group network will be removed from the interface.
6274 */
6275int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6276{
6277 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006278 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279
6280 if (os_strcmp(ifname, "*") == 0) {
6281 struct wpa_supplicant *prev;
Hai Shalom899fcc72020-10-19 14:38:18 -07006282 bool calling_wpa_s_group_removed = false;
6283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006284 wpa_s = global->ifaces;
6285 while (wpa_s) {
6286 prev = wpa_s;
6287 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006288 if (prev->p2p_group_interface !=
6289 NOT_P2P_GROUP_INTERFACE ||
6290 (prev->current_ssid &&
Hai Shalom899fcc72020-10-19 14:38:18 -07006291 prev->current_ssid->p2p_group)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006292 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Hai Shalom899fcc72020-10-19 14:38:18 -07006293 if (prev == calling_wpa_s)
6294 calling_wpa_s_group_removed = true;
6295 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006296 }
Hai Shalom899fcc72020-10-19 14:38:18 -07006297
6298 if (!calling_wpa_s_group_removed &&
6299 (calling_wpa_s->p2p_group_interface !=
6300 NOT_P2P_GROUP_INTERFACE ||
6301 (calling_wpa_s->current_ssid &&
6302 calling_wpa_s->current_ssid->p2p_group))) {
6303 wpa_printf(MSG_DEBUG, "Remove calling_wpa_s P2P group");
6304 wpas_p2p_disconnect_safely(calling_wpa_s,
6305 calling_wpa_s);
6306 }
6307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006308 return 0;
6309 }
6310
6311 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6312 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6313 break;
6314 }
6315
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006316 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006317}
6318
6319
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006320static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6321{
6322 unsigned int r;
6323
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006324 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
6325 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
Sunil8cd6f4d2022-06-28 18:40:46 +00006326 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006327 int res;
6328
6329 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
6330 &size, pref_freq_list);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00006331 if (!res && size > 0 && !is_p2p_allow_6ghz(wpa_s->global->p2p))
Hai Shalomc1a21442022-02-04 13:43:00 -08006332 size = p2p_remove_6ghz_channels(pref_freq_list, size);
6333
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006334 if (!res && size > 0) {
6335 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006336 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006337 (!p2p_supported_freq(wpa_s->global->p2p,
Sunil8cd6f4d2022-06-28 18:40:46 +00006338 pref_freq_list[i].freq) ||
6339 wpas_p2p_disallowed_freq(
6340 wpa_s->global,
6341 pref_freq_list[i].freq) ||
6342 !p2p_pref_freq_allowed(&pref_freq_list[i],
6343 true))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006344 wpa_printf(MSG_DEBUG,
6345 "P2P: preferred_freq_list[%d]=%d is disallowed",
Sunil8cd6f4d2022-06-28 18:40:46 +00006346 i, pref_freq_list[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006347 i++;
6348 }
6349 if (i != size) {
Sunil8cd6f4d2022-06-28 18:40:46 +00006350 freq = pref_freq_list[i].freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006351 wpa_printf(MSG_DEBUG,
6352 "P2P: Using preferred_freq_list[%d]=%d",
6353 i, freq);
6354 } else {
6355 wpa_printf(MSG_DEBUG,
6356 "P2P: All driver preferred frequencies are disallowed for P2P use");
6357 }
6358 } else {
6359 wpa_printf(MSG_DEBUG,
6360 "P2P: No preferred frequency list available");
6361 }
6362 }
6363
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006364 if (freq == 2) {
6365 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6366 "band");
6367 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006368 p2p_supported_freq_go(wpa_s->global->p2p,
6369 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006370 freq = wpa_s->best_24_freq;
6371 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6372 "channel: %d MHz", freq);
6373 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006374 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6375 return -1;
Jimmy Chen801bf462021-11-09 23:08:48 +08006376 int possible_2g_freqs[] = {
6377 /* operating class 81 */
6378 2412, 2437, 2462,
6379 };
6380 int possible_2g_freqs_num =
6381 sizeof(possible_2g_freqs)/sizeof(possible_2g_freqs[0]);
6382 int i;
6383 for (i = 0; i < possible_2g_freqs_num; i++, r++) {
6384 freq = possible_2g_freqs[r % possible_2g_freqs_num];
6385 if (p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6386 break;
6387 }
6388 }
6389
6390 if (i >= possible_2g_freqs_num) {
6391 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6392 "2.4 GHz channel for P2P group");
6393 return -1;
6394 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006395 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6396 "channel: %d MHz", freq);
6397 }
6398 }
6399
6400 if (freq == 5) {
6401 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6402 "band");
6403 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006404 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006405 wpa_s->best_5_freq)) {
6406 freq = wpa_s->best_5_freq;
6407 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6408 "channel: %d MHz", freq);
6409 } else {
Hai Shalomc1a21442022-02-04 13:43:00 -08006410 const int freqs[] = {
6411 /* operating class 115 */
6412 5180, 5200, 5220, 5240,
6413 /* operating class 124 */
6414 5745, 5765, 5785, 5805,
6415 };
6416 unsigned int i, num_freqs = ARRAY_SIZE(freqs);
6417
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006418 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6419 return -1;
Jimmy Chen64b90632020-09-29 17:27:34 +08006420
6421 /*
6422 * most of 5G channels are DFS, only operating class 115 and 124
6423 * are available possibly, randomly pick a start to check them.
6424 */
6425 int possible_5g_freqs[] = {
6426 /* operating class 115 */
6427 5180, 5200, 5220, 5240,
6428 /* operating class 124 */
6429 5745, 5765, 5785, 5805,
6430 };
6431 int possible_5g_freqs_num =
6432 sizeof(possible_5g_freqs)/sizeof(possible_5g_freqs[0]);
6433
Jimmy Chen64b90632020-09-29 17:27:34 +08006434 for (i = 0; i < possible_5g_freqs_num; i++, r++) {
6435 if (p2p_supported_freq_go(
6436 wpa_s->global->p2p,
6437 possible_5g_freqs[r % possible_5g_freqs_num])) {
6438 freq = possible_5g_freqs[r % possible_5g_freqs_num];
6439 break;
6440 }
6441 }
6442
6443 if (i >= possible_5g_freqs_num) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006444 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6445 "5 GHz channel for P2P group");
6446 return -1;
6447 }
6448 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6449 "channel: %d MHz", freq);
6450 }
6451 }
6452
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006453 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006454 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006455 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6456 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006457 /*
6458 * If freq is a DFS channel and DFS is offloaded to the
6459 * driver, allow P2P GO to use it.
6460 */
6461 wpa_printf(MSG_DEBUG, "P2P: "
6462 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
6463 __func__, freq);
6464 return freq;
6465 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006466 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6467 "(%u MHz) is not supported for P2P uses",
6468 freq);
6469 return -1;
6470 }
6471
6472 return freq;
6473}
6474
6475
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006476static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
6477 const struct p2p_channels *channels,
6478 int freq)
6479{
6480 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
6481 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
6482 freq_included(wpa_s, channels, freq))
6483 return 1;
6484 return 0;
6485}
6486
6487
6488static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
6489 struct p2p_go_neg_results *params,
6490 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006491{
6492 unsigned int i, r;
6493
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006494 /* try all channels in operating class 115 */
6495 for (i = 0; i < 4; i++) {
6496 params->freq = 5180 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006497 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006498 goto out;
6499 }
6500
6501 /* try all channels in operating class 124 */
6502 for (i = 0; i < 4; i++) {
6503 params->freq = 5745 + i * 20;
Hai Shalomc1a21442022-02-04 13:43:00 -08006504 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006505 goto out;
6506 }
6507
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006508 /* try social channel class 180 channel 2 */
6509 params->freq = 58320 + 1 * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006510 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006511 goto out;
6512
6513 /* try all channels in reg. class 180 */
6514 for (i = 0; i < 4; i++) {
6515 params->freq = 58320 + i * 2160;
Hai Shalomc1a21442022-02-04 13:43:00 -08006516 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006517 goto out;
6518 }
6519
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006520 /* try some random selection of the social channels */
6521 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6522 return;
6523
6524 for (i = 0; i < 3; i++) {
6525 params->freq = 2412 + ((r + i) % 3) * 25;
6526 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6527 goto out;
6528 }
6529
6530 /* try all other channels in operating class 81 */
6531 for (i = 0; i < 11; i++) {
6532 params->freq = 2412 + i * 5;
6533
6534 /* skip social channels; covered in the previous loop */
6535 if (params->freq == 2412 ||
6536 params->freq == 2437 ||
6537 params->freq == 2462)
6538 continue;
6539
6540 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
6541 goto out;
6542 }
6543
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006544 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006545 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006546 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006547out:
6548 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6549 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006550}
6551
6552
Roshan Pius3a1667e2018-07-03 15:17:14 -07006553static int wpas_same_band(int freq1, int freq2)
6554{
6555 enum hostapd_hw_mode mode1, mode2;
6556 u8 chan1, chan2;
6557
6558 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
6559 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
6560 if (mode1 == NUM_HOSTAPD_MODES)
6561 return 0;
6562 return mode1 == mode2;
6563}
6564
6565
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006566static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6567 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006568 int freq, int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08006569 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006570 int edmg,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006571 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006572{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006573 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006574 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006575 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006576 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006577 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578
6579 os_memset(params, 0, sizeof(*params));
6580 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006581 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006582 params->vht = vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08006583 params->he = he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006584 params->max_oper_chwidth = max_oper_chwidth;
6585 params->vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006586 params->edmg = edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006587
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006588 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6589 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006590 if (!freqs)
6591 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006593 num = get_shared_radio_freqs_data(wpa_s, freqs,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006594 wpa_s->num_multichan_concurrent,
6595 false);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006596
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006597 if (wpa_s->current_ssid &&
6598 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
6599 wpa_s->wpa_state == WPA_COMPLETED) {
6600 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
6601 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006603 /*
6604 * If the frequency selection is done for an active P2P GO that
6605 * is not sharing a frequency, allow to select a new frequency
6606 * even if there are no unused frequencies as we are about to
6607 * move the P2P GO so its frequency can be re-used.
6608 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006609 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006610 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
6611 freqs[i].flags == 0) {
6612 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006613 break;
6614 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006615 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006616 }
6617
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006618 /* Try to use EDMG channel */
6619 if (params->edmg) {
6620 if (wpas_p2p_try_edmg_channel(wpa_s, params) == 0)
6621 goto success;
6622 params->edmg = 0;
6623 }
6624
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006625 /* try using the forced freq */
6626 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006627 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
6628 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006629 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006630 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006631 freq);
6632 goto fail;
6633 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006634 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
6635 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07006636 ieee80211_is_dfs(freq, wpa_s->hw.modes,
6637 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006638 /*
6639 * If freq is a DFS channel and DFS is offloaded
6640 * to the driver, allow P2P GO to use it.
6641 */
6642 wpa_printf(MSG_DEBUG,
6643 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
6644 __func__, freq);
6645 } else {
6646 wpa_printf(MSG_DEBUG,
6647 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
6648 freq);
6649 goto fail;
6650 }
6651 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006652
6653 for (i = 0; i < num; i++) {
6654 if (freqs[i].freq == freq) {
6655 wpa_printf(MSG_DEBUG,
6656 "P2P: forced freq (%d MHz) is also shared",
6657 freq);
6658 params->freq = freq;
6659 goto success;
6660 }
6661 }
6662
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006663 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006664 wpa_printf(MSG_DEBUG,
6665 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6666 freq);
6667 goto fail;
6668 }
6669
6670 wpa_printf(MSG_DEBUG,
6671 "P2P: force GO freq (%d MHz) on a free channel",
6672 freq);
6673 params->freq = freq;
6674 goto success;
6675 }
6676
6677 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006678 if (num &&
6679 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006680 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6681 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6682 wpa_printf(MSG_DEBUG,
6683 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006684 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006685 params->freq = cand;
6686 goto success;
6687 }
6688
6689 /* try using one of the shared freqs */
6690 for (i = 0; i < num; i++) {
6691 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6692 freqs[i].freq)) {
6693 wpa_printf(MSG_DEBUG,
6694 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006695 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006696 params->freq = freqs[i].freq;
6697 goto success;
6698 }
6699 }
6700 }
6701
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006702 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006703 wpa_printf(MSG_DEBUG,
6704 "P2P: Cannot force GO on any of the channels we are already using");
6705 goto fail;
6706 }
6707
6708 /* try using the setting from the configuration file */
6709 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6710 wpa_s->conf->p2p_oper_channel >= 1 &&
6711 wpa_s->conf->p2p_oper_channel <= 11 &&
6712 wpas_p2p_supported_freq_go(
6713 wpa_s, channels,
6714 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6715 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6716 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6717 "frequency %d MHz", params->freq);
6718 goto success;
6719 }
6720
6721 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6722 wpa_s->conf->p2p_oper_reg_class == 116 ||
6723 wpa_s->conf->p2p_oper_reg_class == 117 ||
6724 wpa_s->conf->p2p_oper_reg_class == 124 ||
6725 wpa_s->conf->p2p_oper_reg_class == 125 ||
6726 wpa_s->conf->p2p_oper_reg_class == 126 ||
6727 wpa_s->conf->p2p_oper_reg_class == 127) &&
6728 wpas_p2p_supported_freq_go(wpa_s, channels,
6729 5000 +
6730 5 * wpa_s->conf->p2p_oper_channel)) {
6731 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6732 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6733 "frequency %d MHz", params->freq);
6734 goto success;
6735 }
6736
6737 /* Try using best channels */
6738 if (wpa_s->conf->p2p_oper_channel == 0 &&
6739 wpa_s->best_overall_freq > 0 &&
6740 wpas_p2p_supported_freq_go(wpa_s, channels,
6741 wpa_s->best_overall_freq)) {
6742 params->freq = wpa_s->best_overall_freq;
6743 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6744 "channel %d MHz", params->freq);
6745 goto success;
6746 }
6747
6748 if (wpa_s->conf->p2p_oper_channel == 0 &&
6749 wpa_s->best_24_freq > 0 &&
6750 wpas_p2p_supported_freq_go(wpa_s, channels,
6751 wpa_s->best_24_freq)) {
6752 params->freq = wpa_s->best_24_freq;
6753 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6754 "channel %d MHz", params->freq);
6755 goto success;
6756 }
6757
6758 if (wpa_s->conf->p2p_oper_channel == 0 &&
6759 wpa_s->best_5_freq > 0 &&
6760 wpas_p2p_supported_freq_go(wpa_s, channels,
6761 wpa_s->best_5_freq)) {
6762 params->freq = wpa_s->best_5_freq;
6763 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6764 "channel %d MHz", params->freq);
6765 goto success;
6766 }
6767
6768 /* try using preferred channels */
6769 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6770 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6771 params->freq = cand;
6772 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6773 "channels", params->freq);
6774 goto success;
6775 }
6776
Roshan Pius3a1667e2018-07-03 15:17:14 -07006777 /* Try using a channel that allows VHT to be used with 80 MHz */
6778 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6779 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6780 enum hostapd_hw_mode mode;
6781 struct hostapd_hw_modes *hwmode;
6782 u8 chan;
Hai Shalomc1a21442022-02-04 13:43:00 -08006783 u8 op_class;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006784
6785 cand = wpa_s->p2p_group_common_freqs[i];
Hai Shalomc1a21442022-02-04 13:43:00 -08006786 op_class = is_6ghz_freq(cand) ? 133 : 128;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006787 mode = ieee80211_freq_to_chan(cand, &chan);
6788 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomfdcde762020-04-02 11:19:20 -07006789 mode, is_6ghz_freq(cand));
Roshan Pius3a1667e2018-07-03 15:17:14 -07006790 if (!hwmode ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006791 wpas_p2p_verify_channel(wpa_s, hwmode, op_class,
6792 chan, BW80) != ALLOWED)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006793 continue;
6794 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6795 params->freq = cand;
6796 wpa_printf(MSG_DEBUG,
6797 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6798 params->freq);
6799 goto success;
6800 }
6801 }
6802 }
6803
6804 /* Try using a channel that allows HT to be used with 40 MHz on the same
6805 * band so that CSA can be used */
6806 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6807 wpa_s->p2p_group_common_freqs) {
6808 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6809 enum hostapd_hw_mode mode;
6810 struct hostapd_hw_modes *hwmode;
Hai Shalomc1a21442022-02-04 13:43:00 -08006811 u8 chan, op_class;
6812 bool is_6ghz, supported = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006813
Hai Shalomc1a21442022-02-04 13:43:00 -08006814 is_6ghz = is_6ghz_freq(cand);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006815 cand = wpa_s->p2p_group_common_freqs[i];
6816 mode = ieee80211_freq_to_chan(cand, &chan);
6817 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
Hai Shalomc1a21442022-02-04 13:43:00 -08006818 mode, is_6ghz);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006819 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6820 cand) ||
Hai Shalomc1a21442022-02-04 13:43:00 -08006821 !hwmode)
Roshan Pius3a1667e2018-07-03 15:17:14 -07006822 continue;
Hai Shalomc1a21442022-02-04 13:43:00 -08006823 if (is_6ghz &&
6824 wpas_p2p_verify_channel(wpa_s, hwmode, 132, chan,
6825 BW40) == ALLOWED)
6826 supported = true;
6827
6828 if (!is_6ghz &&
6829 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006830 cand, -1, CONF_OPER_CHWIDTH_USE_HT,
6831 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006832 wpas_p2p_verify_channel(
6833 wpa_s, hwmode, op_class, chan,
6834 BW40MINUS) == ALLOWED)
6835 supported = true;
6836
6837 if (!supported && !is_6ghz &&
6838 ieee80211_freq_to_channel_ext(
Sunil8cd6f4d2022-06-28 18:40:46 +00006839 cand, 1, CONF_OPER_CHWIDTH_USE_HT,
6840 &op_class, &chan) != NUM_HOSTAPD_MODES &&
Hai Shalomc1a21442022-02-04 13:43:00 -08006841 wpas_p2p_verify_channel(
6842 wpa_s, hwmode, op_class, chan,
6843 BW40PLUS) == ALLOWED)
6844 supported = true;
6845
6846 if (!supported)
6847 continue;
6848
Roshan Pius3a1667e2018-07-03 15:17:14 -07006849 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6850 params->freq = cand;
6851 wpa_printf(MSG_DEBUG,
6852 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6853 params->freq);
6854 goto success;
6855 }
6856 }
6857 }
6858
6859 /* Try using one of the group common freqs on the same band so that CSA
6860 * can be used */
6861 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6862 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6863 cand = wpa_s->p2p_group_common_freqs[i];
6864 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6865 cand))
6866 continue;
6867 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6868 params->freq = cand;
6869 wpa_printf(MSG_DEBUG,
6870 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6871 params->freq);
6872 goto success;
6873 }
6874 }
6875 }
6876
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006877 /* Try using one of the group common freqs */
6878 if (wpa_s->p2p_group_common_freqs) {
6879 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6880 cand = wpa_s->p2p_group_common_freqs[i];
6881 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6882 params->freq = cand;
6883 wpa_printf(MSG_DEBUG,
6884 "P2P: Use freq %d MHz common with the peer",
6885 params->freq);
6886 goto success;
6887 }
6888 }
6889 }
6890
6891 /* no preference, select some channel */
6892 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6893
6894 if (params->freq == 0) {
6895 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6896 goto fail;
6897 }
6898
6899success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006900 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006901 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006902fail:
6903 os_free(freqs);
6904 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905}
6906
6907
6908static struct wpa_supplicant *
6909wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6910 int go)
6911{
6912 struct wpa_supplicant *group_wpa_s;
6913
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006914 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006915 if (wpa_s->p2p_mgmt) {
6916 /*
6917 * We may be called on the p2p_dev interface which
6918 * cannot be used for group operations, so always use
6919 * the primary interface.
6920 */
6921 wpa_s->parent->p2pdev = wpa_s;
6922 wpa_s = wpa_s->parent;
6923 }
6924 wpa_dbg(wpa_s, MSG_DEBUG,
6925 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006926 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006927 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006928 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006929 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931
6932 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006933 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006934 wpa_msg_global(wpa_s, MSG_ERROR,
6935 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006937 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6939 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006940 wpa_msg_global(wpa_s, MSG_ERROR,
6941 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006942 wpas_p2p_remove_pending_group_interface(wpa_s);
6943 return NULL;
6944 }
6945
Roshan Pius3a1667e2018-07-03 15:17:14 -07006946 if (go && wpa_s->p2p_go_do_acs) {
6947 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6948 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6949 wpa_s->p2p_go_do_acs = 0;
6950 }
6951
Hai Shalomc1a21442022-02-04 13:43:00 -08006952 if (go && wpa_s->p2p_go_allow_dfs) {
6953 group_wpa_s->p2p_go_allow_dfs = wpa_s->p2p_go_allow_dfs;
6954 wpa_s->p2p_go_allow_dfs = 0;
6955 }
6956
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006957 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6958 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006959 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006960 return group_wpa_s;
6961}
6962
6963
6964/**
6965 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6966 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6967 * @persistent_group: Whether to create a persistent group
6968 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006969 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006970 * @ht40: Start GO with 40 MHz channel width
6971 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006972 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08006973 * @edmg: Start GO with EDMG support
Hai Shalomc1a21442022-02-04 13:43:00 -08006974 * @allow_6ghz: Allow P2P group creation on a 6 GHz channel
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975 * Returns: 0 on success, -1 on failure
6976 *
6977 * This function creates a new P2P group with the local end as the Group Owner,
6978 * i.e., without using Group Owner Negotiation.
6979 */
6980int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006981 int freq, int vht_center_freq2, int ht40, int vht,
Hai Shalomc1a21442022-02-04 13:43:00 -08006982 int max_oper_chwidth, int he, int edmg,
6983 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006984{
6985 struct p2p_go_neg_results params;
Sunil Ravi77a0f092022-10-03 00:53:41 +00006986 int selected_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987
6988 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6989 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08006990 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
6991 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006992
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006993 os_free(wpa_s->global->add_psk);
6994 wpa_s->global->add_psk = NULL;
6995
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006996 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006997 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006998 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006999
Roshan Pius3a1667e2018-07-03 15:17:14 -07007000 if (!wpa_s->p2p_go_do_acs) {
Sunil Ravi77a0f092022-10-03 00:53:41 +00007001 selected_freq = wpas_p2p_select_go_freq(wpa_s, freq);
7002 if (selected_freq < 0)
Roshan Pius3a1667e2018-07-03 15:17:14 -07007003 return -1;
7004 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007005
Sunil Ravi88611412024-06-28 17:34:56 +00007006 if (wpas_p2p_init_go_params(wpa_s, &params, selected_freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007007 ht40, vht, max_oper_chwidth, he, edmg,
7008 NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007009 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011 p2p_go_params(wpa_s->global->p2p, &params);
7012 params.persistent_group = persistent_group;
7013
7014 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
7015 if (wpa_s == NULL)
7016 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007017 if (freq > 0)
7018 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007019 wpas_start_wps_go(wpa_s, &params, 0);
7020
7021 return 0;
7022}
7023
7024
7025static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007026 struct wpa_ssid *params, int addr_allocated,
Matthew Wang124e5f42022-12-29 07:23:06 +00007027 int freq, int force_scan, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007028 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007029{
7030 struct wpa_ssid *ssid;
Matthew Wang9ae940b2022-09-14 21:25:34 -07007031 int other_iface_found = 0;
7032 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033
7034 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
7035 if (wpa_s == NULL)
7036 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007037 if (force_scan)
7038 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007039 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007040
7041 wpa_supplicant_ap_deinit(wpa_s);
7042
7043 ssid = wpa_config_add_network(wpa_s->conf);
7044 if (ssid == NULL)
7045 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007046 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047 wpa_config_set_network_defaults(ssid);
7048 ssid->temporary = 1;
7049 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007050 ssid->pbss = params->pbss;
7051 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
7052 WPA_CIPHER_CCMP;
7053 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007054 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
Sunil Ravi036cec52023-03-29 11:35:17 -07007055 if (is_6ghz_freq(freq) &&
7056 is_p2p_6ghz_capable(wpa_s->global->p2p)) {
7057 ssid->auth_alg |= WPA_AUTH_ALG_SAE;
7058 ssid->key_mgmt |= WPA_KEY_MGMT_SAE;
7059 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
7060 ssid->sae_pwe = SAE_PWE_HASH_TO_ELEMENT;
7061 wpa_dbg(wpa_s, MSG_DEBUG,
7062 "P2P: Enable SAE auth_alg and key_mgmt");
7063 }
7064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065 ssid->ssid = os_malloc(params->ssid_len);
7066 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007067 wpa_config_remove_network(wpa_s->conf, ssid->id);
7068 return -1;
7069 }
7070 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
7071 ssid->ssid_len = params->ssid_len;
7072 ssid->p2p_group = 1;
7073 ssid->export_keys = 1;
7074 if (params->psk_set) {
7075 os_memcpy(ssid->psk, params->psk, 32);
7076 ssid->psk_set = 1;
7077 }
7078 if (params->passphrase)
7079 ssid->passphrase = os_strdup(params->passphrase);
7080
Sunil Ravi036cec52023-03-29 11:35:17 -07007081 if (go_bssid) {
Matthew Wangdcf19452022-11-07 20:42:52 -08007082 ssid->bssid_set = 1;
Sunil Ravi036cec52023-03-29 11:35:17 -07007083 os_memcpy(ssid->bssid, go_bssid, ETH_ALEN);
Matthew Wangdcf19452022-11-07 20:42:52 -08007084 }
7085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007086 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007087 wpa_s->p2p_in_invitation = 1;
Matthew Wang06b42472022-11-10 06:56:31 +00007088 wpa_s->p2p_retry_limit = retry_limit;
Dmitry Shmidt15907092014-03-25 10:42:57 -07007089 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007090 wpa_s->p2p_go_group_formation_completed = 0;
7091 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007092
Matthew Wang9ae940b2022-09-14 21:25:34 -07007093 /*
7094 * Get latest scan results from driver in case cached scan results from
7095 * interfaces on the same wiphy allow us to skip the next scan by fast
7096 * associating. Also update the scan time to the most recent scan result
7097 * fetch time on the same radio so it reflects the actual time the last
7098 * scan result event occurred.
7099 */
Sunil Ravi88611412024-06-28 17:34:56 +00007100 wpa_supplicant_update_scan_results(wpa_s);
Matthew Wang9ae940b2022-09-14 21:25:34 -07007101 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7102 radio_list) {
7103 if (ifs == wpa_s)
7104 continue;
7105 if (!other_iface_found || os_reltime_before(&wpa_s->last_scan,
7106 &ifs->last_scan)) {
7107 other_iface_found = 1;
7108 wpa_s->last_scan.sec = ifs->last_scan.sec;
7109 wpa_s->last_scan.usec = ifs->last_scan.usec;
7110 }
7111 }
7112
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007113 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07007114 NULL);
7115 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7116 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007117 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08007118 wpa_supplicant_select_network(wpa_s, ssid);
7119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120 return 0;
7121}
7122
7123
7124int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
7125 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007126 int force_freq, int neg_freq,
7127 int vht_center_freq2, int ht40,
Hai Shalom74f70d42019-02-11 14:42:39 -08007128 int vht, int max_oper_chwidth, int he,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007129 int edmg,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007130 const struct p2p_channels *channels,
Hai Shalomc1a21442022-02-04 13:43:00 -08007131 int connection_timeout, int force_scan,
Matthew Wang124e5f42022-12-29 07:23:06 +00007132 bool allow_6ghz, int retry_limit,
Sunil Ravi036cec52023-03-29 11:35:17 -07007133 const u8 *go_bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007134{
7135 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08007136 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137
7138 if (ssid->disabled != 2 || ssid->ssid == NULL)
7139 return -1;
7140
7141 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
7142 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
7143 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
7144 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007145 if (go == 0 &&
7146 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007147 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007148 /*
7149 * This can happen if Invitation Response frame was lost
7150 * and the peer (GO of a persistent group) tries to
7151 * invite us again. Reschedule the timeout to avoid
7152 * terminating the wait for the connection too early
7153 * since we now know that the peer is still trying to
7154 * invite us instead of having already started the GO.
7155 */
7156 wpa_printf(MSG_DEBUG,
7157 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
7158 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7159 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007160 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07007161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007162 return 0;
7163 }
7164
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007165 os_free(wpa_s->global->add_psk);
7166 wpa_s->global->add_psk = NULL;
7167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007168 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007169 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007170
Dmitry Shmidt04949592012-07-19 12:16:46 -07007171 wpa_s->p2p_fallback_to_go_neg = 0;
7172
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007173 if (ssid->mode == WPAS_MODE_P2P_GO) {
7174 if (force_freq > 0) {
7175 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
7176 if (freq < 0)
7177 return -1;
Sunil Ravi77a0f092022-10-03 00:53:41 +00007178 wpa_s->p2p_go_no_pri_sec_switch = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007179 } else {
7180 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
7181 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007182 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007183 freq = 0;
7184 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007185 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007186 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007187 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007188 struct os_reltime now;
7189 struct wpa_bss *bss =
7190 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07007191
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007192 os_get_reltime(&now);
7193 if (bss &&
7194 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007195 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007196 freq = bss->freq;
7197 else
7198 freq = 0;
7199 }
7200
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007201 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
Sunil Ravi036cec52023-03-29 11:35:17 -07007202 force_scan, retry_limit, go_bssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007203 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07007204 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07007205 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07007206
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007207 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007208 ht40, vht, max_oper_chwidth, he, edmg,
7209 channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007210 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007211
7212 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007213 params.psk_set = ssid->psk_set;
7214 if (params.psk_set)
7215 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007216 if (ssid->passphrase) {
7217 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
7218 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
7219 "persistent group");
7220 return -1;
7221 }
7222 os_strlcpy(params.passphrase, ssid->passphrase,
7223 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007225 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
7226 params.ssid_len = ssid->ssid_len;
7227 params.persistent_group = 1;
7228
7229 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
7230 if (wpa_s == NULL)
7231 return -1;
7232
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007233 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
7234
Dmitry Shmidt56052862013-10-04 10:23:25 -07007235 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236 wpas_start_wps_go(wpa_s, &params, 0);
7237
7238 return 0;
7239}
7240
7241
7242static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
7243 struct wpabuf *proberesp_ies)
7244{
7245 struct wpa_supplicant *wpa_s = ctx;
7246 if (wpa_s->ap_iface) {
7247 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007248 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
7249 wpabuf_free(beacon_ies);
7250 wpabuf_free(proberesp_ies);
7251 return;
7252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253 if (beacon_ies) {
7254 wpabuf_free(hapd->p2p_beacon_ie);
7255 hapd->p2p_beacon_ie = beacon_ies;
7256 }
7257 wpabuf_free(hapd->p2p_probe_resp_ie);
7258 hapd->p2p_probe_resp_ie = proberesp_ies;
7259 } else {
7260 wpabuf_free(beacon_ies);
7261 wpabuf_free(proberesp_ies);
7262 }
7263 wpa_supplicant_ap_update_beacon(wpa_s);
7264}
7265
7266
7267static void wpas_p2p_idle_update(void *ctx, int idle)
7268{
7269 struct wpa_supplicant *wpa_s = ctx;
7270 if (!wpa_s->ap_iface)
7271 return;
7272 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007273 if (idle) {
7274 if (wpa_s->global->p2p_fail_on_wps_complete &&
7275 wpa_s->p2p_in_provisioning) {
7276 wpas_p2p_grpform_fail_after_wps(wpa_s);
7277 return;
7278 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007279 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007280 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007281 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
7282}
7283
7284
7285struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007286 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287{
7288 struct p2p_group *group;
7289 struct p2p_group_config *cfg;
7290
Dmitry Shmidt849734c2016-05-27 09:59:01 -07007291 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
7292 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007293 return NULL;
7294
7295 cfg = os_zalloc(sizeof(*cfg));
7296 if (cfg == NULL)
7297 return NULL;
7298
Dmitry Shmidt04949592012-07-19 12:16:46 -07007299 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007300 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007301 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007302 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007303 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
7304 if (wpa_s->max_stations &&
7305 wpa_s->max_stations < wpa_s->conf->max_num_sta)
7306 cfg->max_clients = wpa_s->max_stations;
7307 else
7308 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007309 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
7310 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007311 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312 cfg->cb_ctx = wpa_s;
7313 cfg->ie_update = wpas_p2p_ie_update;
7314 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007315 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
7316 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007317
7318 group = p2p_group_init(wpa_s->global->p2p, cfg);
7319 if (group == NULL)
7320 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007321 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007322 p2p_group_notif_formation_done(group);
7323 wpa_s->p2p_group = group;
7324 return group;
7325}
7326
7327
7328void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
7329 int registrar)
7330{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007331 struct wpa_ssid *ssid = wpa_s->current_ssid;
7332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007333 if (!wpa_s->p2p_in_provisioning) {
7334 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
7335 "provisioning not in progress");
7336 return;
7337 }
7338
Dmitry Shmidt04949592012-07-19 12:16:46 -07007339 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7340 u8 go_dev_addr[ETH_ALEN];
7341 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
7342 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7343 ssid->ssid_len);
7344 /* Clear any stored provisioning info */
7345 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
7346 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007347
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007348 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007349 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007350 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007351 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
7352 /*
7353 * Use a separate timeout for initial data connection to
7354 * complete to allow the group to be removed automatically if
7355 * something goes wrong in this step before the P2P group idle
7356 * timeout mechanism is taken into use.
7357 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07007358 wpa_dbg(wpa_s, MSG_DEBUG,
7359 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
7360 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007361 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
7362 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007363 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007364 /* Complete group formation on successful data connection. */
7365 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007366 } else if (ssid) {
7367 /*
7368 * Use a separate timeout for initial data connection to
7369 * complete to allow the group to be removed automatically if
7370 * the client does not complete data connection successfully.
7371 */
7372 wpa_dbg(wpa_s, MSG_DEBUG,
7373 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
7374 P2P_MAX_INITIAL_CONN_WAIT_GO);
7375 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
7376 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007377 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07007378 /*
7379 * Complete group formation on first successful data connection
7380 */
7381 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007383 if (wpa_s->global->p2p)
7384 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007385 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007386}
7387
7388
Jouni Malinen75ecf522011-06-27 15:19:46 -07007389void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
7390 struct wps_event_fail *fail)
7391{
7392 if (!wpa_s->p2p_in_provisioning) {
7393 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
7394 "provisioning not in progress");
7395 return;
7396 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007397
7398 if (wpa_s->go_params) {
7399 p2p_clear_provisioning_info(
7400 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007401 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007402 }
7403
Jouni Malinen75ecf522011-06-27 15:19:46 -07007404 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007405
7406 if (wpa_s == wpa_s->global->p2p_group_formation) {
7407 /*
7408 * Allow some time for the failed WPS negotiation exchange to
7409 * complete, but remove the group since group formation cannot
7410 * succeed after provisioning failure.
7411 */
7412 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
7413 wpa_s->global->p2p_fail_on_wps_complete = 1;
7414 eloop_deplete_timeout(0, 50000,
7415 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007416 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007417 }
7418}
7419
7420
7421int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
7422{
7423 if (!wpa_s->global->p2p_fail_on_wps_complete ||
7424 !wpa_s->p2p_in_provisioning)
7425 return 0;
7426
7427 wpas_p2p_grpform_fail_after_wps(wpa_s);
7428
7429 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007430}
7431
7432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007434 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007435 enum wpas_p2p_prov_disc_use use,
7436 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007437{
7438 u16 config_methods;
7439
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007440 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007441 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007442 wpa_s->p2p_fallback_to_go_neg = 0;
7443 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007444 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
7445 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007446 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
7447 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
7448
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007449 wpa_printf(MSG_DEBUG,
7450 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
7451 __func__, p2ps_prov->conncap,
7452 p2ps_prov->adv_id, p2ps_prov->conncap,
7453 p2ps_prov->status, p2ps_prov->info);
7454
7455 config_methods = 0;
7456 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007457 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007458 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007459 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007460 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
7461 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007463 else {
7464 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007465 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007466 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007468
Dmitry Shmidt04949592012-07-19 12:16:46 -07007469 if (use == WPAS_P2P_PD_AUTO) {
7470 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
7471 wpa_s->pending_pd_config_methods = config_methods;
7472 wpa_s->p2p_auto_pd = 1;
7473 wpa_s->p2p_auto_join = 0;
7474 wpa_s->pending_pd_before_join = 0;
7475 wpa_s->auto_pd_scan_retry = 0;
7476 wpas_p2p_stop_find(wpa_s);
7477 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007478 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007479 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
7480 wpa_s->p2p_auto_started.sec,
7481 wpa_s->p2p_auto_started.usec);
7482 wpas_p2p_join_scan(wpa_s, NULL);
7483 return 0;
7484 }
7485
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007486 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
7487 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007488 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007490
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007491 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007492 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007493 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007494}
7495
7496
7497int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
7498 char *end)
7499{
7500 return p2p_scan_result_text(ies, ies_len, buf, end);
7501}
7502
7503
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007504static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s,
7505 bool force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007506{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007507 if (!offchannel_pending_action_tx(wpa_s) && !force)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508 return;
7509
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007510 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007511 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007512 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
7513 wpa_s, NULL);
7514 offchannel_send_action_done(wpa_s);
7515 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07007516
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007517 if (!offchannel_pending_action_tx(wpa_s))
7518 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007519 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
7520 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007521 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522}
7523
7524
7525int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
7526 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007527 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007528 const u8 *dev_id, unsigned int search_delay,
Hai Shalomc1a21442022-02-04 13:43:00 -08007529 u8 seek_cnt, const char **seek_string, int freq,
7530 bool include_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007532 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Hai Shalomfdcde762020-04-02 11:19:20 -07007533 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534
Dmitry Shmidt04949592012-07-19 12:16:46 -07007535 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007536 wpa_s->p2p_in_provisioning) {
7537 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
7538 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
7539 " (P2P disabled)" : "",
7540 wpa_s->p2p_in_provisioning ?
7541 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007542 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07007543 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007545 wpa_supplicant_cancel_sched_scan(wpa_s);
7546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007548 num_req_dev_types, req_dev_types, dev_id,
Hai Shalomc1a21442022-02-04 13:43:00 -08007549 search_delay, seek_cnt, seek_string, freq,
7550 include_6ghz);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007551}
7552
7553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007554static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7555 struct wpa_scan_results *scan_res)
7556{
7557 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7558
7559 if (wpa_s->p2p_scan_work) {
7560 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7561 wpa_s->p2p_scan_work = NULL;
7562 radio_work_done(work);
7563 }
7564
7565 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7566 return;
7567
7568 /*
7569 * Indicate that results have been processed so that the P2P module can
7570 * continue pending tasks.
7571 */
Hai Shalom60840252021-02-19 19:02:11 -08007572 wpas_p2p_scan_res_handled(wpa_s);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007573}
7574
7575
7576static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577{
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007578 wpas_p2p_clear_pending_action_tx(wpa_s, true);
Hai Shalomfdcde762020-04-02 11:19:20 -07007579 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007580 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7581 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582
7583 if (wpa_s->global->p2p)
7584 p2p_stop_find(wpa_s->global->p2p);
7585
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007586 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7587 wpa_printf(MSG_DEBUG,
7588 "P2P: Do not consider the scan results after stop_find");
7589 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7590 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007591}
7592
7593
7594void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7595{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007596 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007597 if (!wpa_s->global->pending_group_iface_for_p2ps)
7598 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007599}
7600
7601
7602static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7603{
7604 struct wpa_supplicant *wpa_s = eloop_ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07007605 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007606}
7607
7608
7609int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7610{
7611 int res;
7612
7613 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7614 return -1;
7615
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007616 if (wpa_s->p2p_lo_started) {
7617 wpa_printf(MSG_DEBUG,
7618 "P2P: Cannot start P2P listen, it is offloaded");
7619 return -1;
7620 }
7621
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007622 wpa_supplicant_cancel_sched_scan(wpa_s);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00007623 wpas_p2p_clear_pending_action_tx(wpa_s, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007624
7625 if (timeout == 0) {
7626 /*
7627 * This is a request for unlimited Listen state. However, at
7628 * least for now, this is mapped to a Listen state for one
7629 * hour.
7630 */
7631 timeout = 3600;
7632 }
7633 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
Hai Shalomfdcde762020-04-02 11:19:20 -07007634 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007636 /*
7637 * Stop previous find/listen operation to avoid trying to request a new
7638 * remain-on-channel operation while the driver is still running the
7639 * previous one.
7640 */
7641 if (wpa_s->global->p2p)
7642 p2p_stop_find(wpa_s->global->p2p);
7643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007644 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7645 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
Hai Shalomfdcde762020-04-02 11:19:20 -07007646 wpa_s->global->p2p_long_listen = timeout * 1000;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647 eloop_register_timeout(timeout, 0,
7648 wpas_p2p_long_listen_timeout,
7649 wpa_s, NULL);
7650 }
7651
7652 return res;
7653}
7654
7655
7656int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7657 u8 *buf, size_t len, int p2p_group)
7658{
7659 struct wpabuf *p2p_ie;
7660 int ret;
7661
7662 if (wpa_s->global->p2p_disabled)
7663 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007664 /*
7665 * Advertize mandatory cross connection capability even on
7666 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
7667 */
7668 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007669 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670 if (wpa_s->global->p2p == NULL)
7671 return -1;
7672 if (bss == NULL)
7673 return -1;
7674
7675 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7676 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7677 p2p_group, p2p_ie);
7678 wpabuf_free(p2p_ie);
7679
7680 return ret;
7681}
7682
7683
7684int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007685 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007686 const u8 *ie, size_t ie_len,
7687 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007688{
7689 if (wpa_s->global->p2p_disabled)
7690 return 0;
7691 if (wpa_s->global->p2p == NULL)
7692 return 0;
7693
Dmitry Shmidt04949592012-07-19 12:16:46 -07007694 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07007695 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007696 case P2P_PREQ_NOT_P2P:
7697 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7698 ssi_signal);
7699 /* fall through */
7700 case P2P_PREQ_MALFORMED:
7701 case P2P_PREQ_NOT_LISTEN:
7702 case P2P_PREQ_NOT_PROCESSED:
7703 default: /* make gcc happy */
7704 return 0;
7705 case P2P_PREQ_PROCESSED:
7706 return 1;
7707 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007708}
7709
7710
7711void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7712 const u8 *sa, const u8 *bssid,
7713 u8 category, const u8 *data, size_t len, int freq)
7714{
7715 if (wpa_s->global->p2p_disabled)
7716 return;
7717 if (wpa_s->global->p2p == NULL)
7718 return;
7719
7720 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7721 freq);
7722}
7723
7724
7725void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7726{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007727 unsigned int bands;
7728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007729 if (wpa_s->global->p2p_disabled)
7730 return;
7731 if (wpa_s->global->p2p == NULL)
7732 return;
7733
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007734 bands = wpas_get_bands(wpa_s, NULL);
7735 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736}
7737
7738
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007739static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740{
7741 p2p_group_deinit(wpa_s->p2p_group);
7742 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007743
7744 wpa_s->ap_configured_cb = NULL;
7745 wpa_s->ap_configured_cb_ctx = NULL;
7746 wpa_s->ap_configured_cb_data = NULL;
7747 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007748}
7749
7750
7751int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7752{
Hai Shalomfdcde762020-04-02 11:19:20 -07007753 wpa_s->global->p2p_long_listen = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007755 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7756 return -1;
7757
7758 return p2p_reject(wpa_s->global->p2p, addr);
7759}
7760
7761
7762/* Invite to reinvoke a persistent group */
7763int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007764 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007765 int vht_center_freq2, int ht40, int vht, int max_chwidth,
Hai Shalomc1a21442022-02-04 13:43:00 -08007766 int pref_freq, int he, int edmg, bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007767{
7768 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007769 u8 *bssid = NULL;
7770 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007771 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007772 int no_pref_freq_given = pref_freq == 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00007773 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7774 unsigned int size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007775
Hai Shalomc1a21442022-02-04 13:43:00 -08007776 if (wpas_p2p_check_6ghz(wpa_s, NULL, allow_6ghz, freq))
7777 return -1;
7778
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007779 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007780 if (peer_addr)
7781 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7782 else
7783 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007784
Jouni Malinen31be0a42012-08-31 21:20:51 +03007785 wpa_s->p2p_persistent_go_freq = freq;
7786 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007787 wpa_s->p2p_go_vht = !!vht;
Hai Shalom74f70d42019-02-11 14:42:39 -08007788 wpa_s->p2p_go_he = !!he;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007789 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7790 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007791 wpa_s->p2p_go_edmg = !!edmg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007792 if (ssid->mode == WPAS_MODE_P2P_GO) {
7793 role = P2P_INVITE_ROLE_GO;
7794 if (peer_addr == NULL) {
7795 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7796 "address in invitation command");
7797 return -1;
7798 }
7799 if (wpas_p2p_create_iface(wpa_s)) {
7800 if (wpas_p2p_add_group_interface(wpa_s,
7801 WPA_IF_P2P_GO) < 0) {
7802 wpa_printf(MSG_ERROR, "P2P: Failed to "
7803 "allocate a new interface for the "
7804 "group");
7805 return -1;
7806 }
7807 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007808 } else if (wpa_s->p2p_mgmt)
7809 bssid = wpa_s->parent->own_addr;
7810 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007811 bssid = wpa_s->own_addr;
7812 } else {
7813 role = P2P_INVITE_ROLE_CLIENT;
7814 peer_addr = ssid->bssid;
7815 }
7816 wpa_s->pending_invite_ssid_id = ssid->id;
7817
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007818 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007819 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007820 role == P2P_INVITE_ROLE_GO,
7821 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007822 if (res)
7823 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007825 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7826 return -1;
7827
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007828 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7829
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007830 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7831 no_pref_freq_given && pref_freq > 0 &&
7832 wpa_s->num_multichan_concurrent > 1 &&
7833 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7834 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7835 pref_freq);
7836 pref_freq = 0;
7837 }
7838
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007839 /*
7840 * Stop any find/listen operations before invitation and possibly
7841 * connection establishment.
7842 */
7843 wpas_p2p_stop_find_oper(wpa_s);
7844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007845 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007846 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007847 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007848}
7849
7850
7851/* Invite to join an active group */
7852int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
Hai Shalomc1a21442022-02-04 13:43:00 -08007853 const u8 *peer_addr, const u8 *go_dev_addr,
7854 bool allow_6ghz)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007855{
7856 struct wpa_global *global = wpa_s->global;
7857 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007858 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007859 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007860 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007861 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007862 int res;
Sunil8cd6f4d2022-06-28 18:40:46 +00007863 struct weighted_pcl pref_freq_list[P2P_MAX_PREF_CHANNELS];
7864 unsigned int size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007865
Jouni Malinen31be0a42012-08-31 21:20:51 +03007866 wpa_s->p2p_persistent_go_freq = 0;
7867 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007868 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007869 wpa_s->p2p_go_vht_center_freq2 = 0;
7870 wpa_s->p2p_go_max_oper_chwidth = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08007871 wpa_s->p2p_go_edmg = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007873 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7874 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7875 break;
7876 }
7877 if (wpa_s == NULL) {
7878 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7879 return -1;
7880 }
7881
7882 ssid = wpa_s->current_ssid;
7883 if (ssid == NULL) {
7884 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7885 "invitation");
7886 return -1;
7887 }
7888
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007889 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007890 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007891 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007892 ssid->ssid, ssid->ssid_len);
7893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007894 if (ssid->mode == WPAS_MODE_P2P_GO) {
7895 role = P2P_INVITE_ROLE_ACTIVE_GO;
7896 bssid = wpa_s->own_addr;
7897 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007898 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007899 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007900 } else {
7901 role = P2P_INVITE_ROLE_CLIENT;
7902 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7903 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7904 "invite to current group");
7905 return -1;
7906 }
7907 bssid = wpa_s->bssid;
7908 if (go_dev_addr == NULL &&
7909 !is_zero_ether_addr(wpa_s->go_dev_addr))
7910 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007911 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7912 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007913 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007914 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007916 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7917 return -1;
Hai Shalomc1a21442022-02-04 13:43:00 -08007918 if (wpas_p2p_check_6ghz(wpa_s, peer_addr, allow_6ghz, freq))
7919 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007920
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007921 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007922 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007923 role == P2P_INVITE_ROLE_ACTIVE_GO,
7924 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007925 if (res)
7926 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007927 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007929 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007930 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007931 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007932}
7933
7934
7935void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7936{
7937 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007938 u8 go_dev_addr[ETH_ALEN];
7939 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007940 int freq;
Hai Shalom5f92bc92019-04-18 11:54:11 -07007941 u8 ip[3 * 4], *ip_ptr = NULL;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007942 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007943
Dmitry Shmidt04949592012-07-19 12:16:46 -07007944 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7945 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007946 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007947 }
7948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007949 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007950 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007951
7952 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007953 if (!wpa_s->p2p_go_group_formation_completed &&
7954 wpa_s->global->p2p_group_formation == wpa_s) {
7955 wpa_dbg(wpa_s, MSG_DEBUG,
7956 "P2P: Marking group formation completed on client on data connection");
7957 wpa_s->p2p_go_group_formation_completed = 1;
7958 wpa_s->global->p2p_group_formation = NULL;
7959 wpa_s->p2p_in_provisioning = 0;
7960 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00007961 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007962 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007964 os_memset(go_dev_addr, 0, ETH_ALEN);
7965 if (ssid->bssid_set)
7966 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7967 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7968 ssid->ssid_len);
7969 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7970
7971 if (wpa_s->global->p2p_group_formation == wpa_s)
7972 wpa_s->global->p2p_group_formation = NULL;
7973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007974 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7975 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007976
7977 ip_addr[0] = '\0';
7978 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007979 int res;
7980
7981 res = os_snprintf(ip_addr, sizeof(ip_addr),
7982 " ip_addr=%u.%u.%u.%u "
7983 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7984 ip[0], ip[1], ip[2], ip[3],
7985 ip[4], ip[5], ip[6], ip[7],
7986 ip[8], ip[9], ip[10], ip[11]);
7987 if (os_snprintf_error(sizeof(ip_addr), res))
7988 ip_addr[0] = '\0';
Hai Shalom5f92bc92019-04-18 11:54:11 -07007989 ip_ptr = ip;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007990 }
7991
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007992 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7993 ssid->passphrase == NULL && ssid->psk_set ?
7994 ssid->psk : NULL,
7995 ssid->passphrase, go_dev_addr, persistent,
7996 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007997
7998 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007999 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
8000 ssid, go_dev_addr);
8001
Hai Shalom5f92bc92019-04-18 11:54:11 -07008002 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip_ptr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008003}
8004
8005
8006int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
8007 u32 interval1, u32 duration2, u32 interval2)
8008{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008009 int ret;
8010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008011 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8012 return -1;
8013
8014 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
8015 wpa_s->current_ssid == NULL ||
8016 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
8017 return -1;
8018
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008019 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
8020 wpa_s->own_addr, wpa_s->assoc_freq,
8021 duration1, interval1, duration2, interval2);
8022 if (ret == 0)
8023 wpa_s->waiting_presence_resp = 1;
8024
8025 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008026}
8027
8028
8029int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
8030 unsigned int interval)
8031{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008032 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8033 return -1;
8034
8035 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
8036}
8037
8038
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008039static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
8040{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08008041 if (wpa_s->current_ssid == NULL) {
8042 /*
8043 * current_ssid can be cleared when P2P client interface gets
8044 * disconnected, so assume this interface was used as P2P
8045 * client.
8046 */
8047 return 1;
8048 }
8049 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008050 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
8051}
8052
8053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008054static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
8055{
8056 struct wpa_supplicant *wpa_s = eloop_ctx;
8057
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008058 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008059 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
8060 "disabled");
8061 return;
8062 }
8063
Dmitry Shmidt04949592012-07-19 12:16:46 -07008064 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
8065 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008066 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008067}
8068
8069
8070static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
8071{
Dmitry Shmidt04949592012-07-19 12:16:46 -07008072 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008073
Dmitry Shmidt04949592012-07-19 12:16:46 -07008074 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8075 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
8076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008077 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8078 return;
8079
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008080 timeout = wpa_s->conf->p2p_group_idle;
8081 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
8082 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
8083 timeout = P2P_MAX_CLIENT_IDLE;
8084
8085 if (timeout == 0)
8086 return;
8087
Dmitry Shmidt04949592012-07-19 12:16:46 -07008088 if (timeout < 0) {
8089 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
8090 timeout = 0; /* special client mode no-timeout */
8091 else
8092 return;
8093 }
8094
8095 if (wpa_s->p2p_in_provisioning) {
8096 /*
8097 * Use the normal group formation timeout during the
8098 * provisioning phase to avoid terminating this process too
8099 * early due to group idle timeout.
8100 */
8101 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8102 "during provisioning");
8103 return;
8104 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05308105
Dmitry Shmidt04949592012-07-19 12:16:46 -07008106 if (wpa_s->show_group_started) {
8107 /*
8108 * Use the normal group formation timeout between the end of
8109 * the provisioning phase and completion of 4-way handshake to
8110 * avoid terminating this process too early due to group idle
8111 * timeout.
8112 */
8113 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
8114 "while waiting for initial 4-way handshake to "
8115 "complete");
8116 return;
8117 }
8118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008119 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008120 timeout);
8121 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
8122 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008123}
8124
8125
Jouni Malinen2b89da82012-08-31 22:04:41 +03008126/* Returns 1 if the interface was removed */
8127int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
8128 u16 reason_code, const u8 *ie, size_t ie_len,
8129 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008130{
8131 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03008132 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008133
Dmitry Shmidt04949592012-07-19 12:16:46 -07008134 if (!locally_generated)
8135 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8136 ie_len);
8137
8138 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
8139 wpa_s->current_ssid &&
8140 wpa_s->current_ssid->p2p_group &&
8141 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
8142 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
8143 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03008144 if (wpas_p2p_group_delete(wpa_s,
8145 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
8146 > 0)
8147 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008148 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03008149
8150 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008151}
8152
8153
8154void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008155 u16 reason_code, const u8 *ie, size_t ie_len,
8156 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008157{
8158 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8159 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008160
Dmitry Shmidt04949592012-07-19 12:16:46 -07008161 if (!locally_generated)
8162 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
8163 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008164}
8165
8166
8167void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
8168{
8169 struct p2p_data *p2p = wpa_s->global->p2p;
8170
8171 if (p2p == NULL)
8172 return;
8173
8174 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
8175 return;
8176
8177 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
8178 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
8179
8180 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
8181 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
8182
8183 if (wpa_s->wps &&
8184 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
8185 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
8186
8187 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
8188 p2p_set_uuid(p2p, wpa_s->wps->uuid);
8189
8190 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
8191 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
8192 p2p_set_model_name(p2p, wpa_s->conf->model_name);
8193 p2p_set_model_number(p2p, wpa_s->conf->model_number);
8194 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
8195 }
8196
8197 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
8198 p2p_set_sec_dev_types(p2p,
8199 (void *) wpa_s->conf->sec_device_type,
8200 wpa_s->conf->num_sec_device_types);
8201
8202 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
8203 int i;
8204 p2p_remove_wps_vendor_extensions(p2p);
8205 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
8206 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
8207 continue;
8208 p2p_add_wps_vendor_extension(
8209 p2p, wpa_s->conf->wps_vendor_ext[i]);
8210 }
8211 }
8212
8213 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
8214 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
8215 char country[3];
8216 country[0] = wpa_s->conf->country[0];
8217 country[1] = wpa_s->conf->country[1];
8218 country[2] = 0x04;
8219 p2p_set_country(p2p, country);
8220 }
8221
8222 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
8223 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
8224 wpa_s->conf->p2p_ssid_postfix ?
8225 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
8226 0);
8227 }
8228
8229 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
8230 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008231
8232 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
8233 u8 reg_class, channel;
8234 int ret;
8235 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008236 u8 channel_forced;
8237
Jouni Malinen75ecf522011-06-27 15:19:46 -07008238 if (wpa_s->conf->p2p_listen_reg_class &&
8239 wpa_s->conf->p2p_listen_channel) {
8240 reg_class = wpa_s->conf->p2p_listen_reg_class;
8241 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008242 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008243 } else {
8244 reg_class = 81;
8245 /*
8246 * Pick one of the social channels randomly as the
8247 * listen channel.
8248 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008249 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8250 channel = 1;
8251 else
8252 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008253 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008254 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008255 ret = p2p_set_listen_channel(p2p, reg_class, channel,
8256 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07008257 if (ret)
8258 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
8259 "failed: %d", ret);
8260 }
8261 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
8262 u8 op_reg_class, op_channel, cfg_op_channel;
8263 int ret = 0;
8264 unsigned int r;
8265 if (wpa_s->conf->p2p_oper_reg_class &&
8266 wpa_s->conf->p2p_oper_channel) {
8267 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
8268 op_channel = wpa_s->conf->p2p_oper_channel;
8269 cfg_op_channel = 1;
8270 } else {
8271 op_reg_class = 81;
8272 /*
8273 * Use random operation channel from (1, 6, 11)
8274 *if no other preference is indicated.
8275 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008276 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
8277 op_channel = 1;
8278 else
8279 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07008280 cfg_op_channel = 0;
8281 }
8282 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
8283 cfg_op_channel);
8284 if (ret)
8285 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
8286 "failed: %d", ret);
8287 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008288
8289 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
8290 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
8291 wpa_s->conf->p2p_pref_chan) < 0) {
8292 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
8293 "update failed");
8294 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008295
8296 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
8297 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
8298 "update failed");
8299 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07008300 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07008301
8302 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
8303 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008304}
8305
8306
8307int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
8308 int duration)
8309{
8310 if (!wpa_s->ap_iface)
8311 return -1;
8312 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
8313 duration);
8314}
8315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008316
8317int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
8318{
8319 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8320 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008321
8322 wpa_s->global->cross_connection = enabled;
8323 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
8324
8325 if (!enabled) {
8326 struct wpa_supplicant *iface;
8327
8328 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8329 {
8330 if (iface->cross_connect_enabled == 0)
8331 continue;
8332
8333 iface->cross_connect_enabled = 0;
8334 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008335 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008336 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8337 iface->ifname,
8338 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008339 }
8340 }
8341
8342 return 0;
8343}
8344
8345
8346static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
8347{
8348 struct wpa_supplicant *iface;
8349
8350 if (!uplink->global->cross_connection)
8351 return;
8352
8353 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8354 if (!iface->cross_connect_enabled)
8355 continue;
8356 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8357 0)
8358 continue;
8359 if (iface->ap_iface == NULL)
8360 continue;
8361 if (iface->cross_connect_in_use)
8362 continue;
8363
8364 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008365 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008366 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8367 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008368 }
8369}
8370
8371
8372static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
8373{
8374 struct wpa_supplicant *iface;
8375
8376 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
8377 if (!iface->cross_connect_enabled)
8378 continue;
8379 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
8380 0)
8381 continue;
8382 if (!iface->cross_connect_in_use)
8383 continue;
8384
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008385 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008386 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
8387 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008388 iface->cross_connect_in_use = 0;
8389 }
8390}
8391
8392
8393void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
8394{
8395 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
8396 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
8397 wpa_s->cross_connect_disallowed)
8398 wpas_p2p_disable_cross_connect(wpa_s);
8399 else
8400 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008401 if (!wpa_s->ap_iface &&
8402 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
8403 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008404}
8405
8406
8407void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
8408{
8409 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008410 if (!wpa_s->ap_iface &&
8411 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
8412 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008413 wpas_p2p_set_group_idle_timeout(wpa_s);
8414}
8415
8416
8417static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
8418{
8419 struct wpa_supplicant *iface;
8420
8421 if (!wpa_s->global->cross_connection)
8422 return;
8423
8424 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8425 if (iface == wpa_s)
8426 continue;
8427 if (iface->drv_flags &
8428 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
8429 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008430 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
8431 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008432 continue;
8433
8434 wpa_s->cross_connect_enabled = 1;
8435 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
8436 sizeof(wpa_s->cross_connect_uplink));
8437 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
8438 "%s to %s whenever uplink is available",
8439 wpa_s->ifname, wpa_s->cross_connect_uplink);
8440
8441 if (iface->ap_iface || iface->current_ssid == NULL ||
8442 iface->current_ssid->mode != WPAS_MODE_INFRA ||
8443 iface->cross_connect_disallowed ||
8444 iface->wpa_state != WPA_COMPLETED)
8445 break;
8446
8447 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008448 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07008449 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
8450 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008451 break;
8452 }
8453}
8454
8455
Sunil Ravi036cec52023-03-29 11:35:17 -07008456static int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008457{
8458 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
8459 !wpa_s->p2p_in_provisioning)
8460 return 0; /* not P2P client operation */
8461
8462 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
8463 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008464 if (wpa_s != wpa_s->p2pdev)
8465 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008466 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008467 return 1;
8468}
8469
8470
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07008471void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
8472{
8473 struct wpa_supplicant *wpa_s = eloop_ctx;
8474 wpas_p2p_notif_pbc_overlap(wpa_s);
8475}
8476
8477
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008478void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
8479 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008480{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008481 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008482 struct wpa_used_freq_data *freqs = NULL;
8483 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008484
8485 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
8486 return;
8487
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008488 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8489 if (!freqs)
8490 return;
8491
Sunil Ravi77d572f2023-01-17 23:58:31 +00008492 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008494 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008495 os_memset(&cli_chan, 0, sizeof(cli_chan));
Hai Shalom60840252021-02-19 19:02:11 -08008496 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan,
8497 is_p2p_6ghz_disabled(wpa_s->global->p2p))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008498 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
8499 "channel list");
8500 return;
8501 }
8502
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008503 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008504
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008505 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07008506
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008507 /*
8508 * The used frequencies map changed, so it is possible that a GO is
8509 * using a channel that is no longer valid for P2P use. It is also
8510 * possible that due to policy consideration, it would be preferable to
8511 * move it to a frequency already used by other station interfaces.
8512 */
8513 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
8514
8515 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008516}
8517
8518
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008519static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
8520 struct wpa_scan_results *scan_res)
8521{
8522 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
8523}
8524
8525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008526int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
8527{
8528 struct wpa_global *global = wpa_s->global;
8529 int found = 0;
8530 const u8 *peer;
8531
8532 if (global->p2p == NULL)
8533 return -1;
8534
8535 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
8536
8537 if (wpa_s->pending_interface_name[0] &&
8538 !is_zero_ether_addr(wpa_s->pending_interface_addr))
8539 found = 1;
8540
8541 peer = p2p_get_go_neg_peer(global->p2p);
8542 if (peer) {
8543 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
8544 MACSTR, MAC2STR(peer));
8545 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008546 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008547 }
8548
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08008549 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
8550 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
8551 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
8552 found = 1;
8553 }
8554
8555 if (wpa_s->pending_pd_before_join) {
8556 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
8557 wpa_s->pending_pd_before_join = 0;
8558 found = 1;
8559 }
8560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008561 wpas_p2p_stop_find(wpa_s);
8562
8563 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8564 if (wpa_s == global->p2p_group_formation &&
8565 (wpa_s->p2p_in_provisioning ||
8566 wpa_s->parent->pending_interface_type ==
8567 WPA_IF_P2P_CLIENT)) {
8568 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
8569 "formation found - cancelling",
8570 wpa_s->ifname);
8571 found = 1;
8572 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008573 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008574 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008575 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07008576 break;
8577 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008578 wpas_p2p_group_delete(wpa_s,
8579 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008580 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008581 } else if (wpa_s->p2p_in_invitation) {
8582 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
8583 wpa_s->ifname);
8584 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008585 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008586 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008587 }
8588 }
8589
8590 if (!found) {
8591 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
8592 return -1;
8593 }
8594
8595 return 0;
8596}
8597
8598
8599void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8600{
8601 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8602 return;
8603
8604 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8605 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008606 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008607}
8608
8609
8610void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8611 int freq_24, int freq_5, int freq_overall)
8612{
8613 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008614 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008615 return;
8616 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8617}
8618
8619
8620int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8621{
8622 u8 peer[ETH_ALEN];
8623 struct p2p_data *p2p = wpa_s->global->p2p;
8624
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008625 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008626 return -1;
8627
8628 if (hwaddr_aton(addr, peer))
8629 return -1;
8630
8631 return p2p_unauthorize(p2p, peer);
8632}
8633
8634
8635/**
8636 * wpas_p2p_disconnect - Disconnect from a P2P Group
8637 * @wpa_s: Pointer to wpa_supplicant data
8638 * Returns: 0 on success, -1 on failure
8639 *
8640 * This can be used to disconnect from a group in which the local end is a P2P
8641 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8642 * virtual network interface was created for this group, that interface will be
8643 * removed. Otherwise, only the configured P2P group network will be removed
8644 * from the interface.
8645 */
8646int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8647{
8648
8649 if (wpa_s == NULL)
8650 return -1;
8651
Jouni Malinen2b89da82012-08-31 22:04:41 +03008652 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8653 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008654}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008655
8656
8657int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8658{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008659 int ret;
8660
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008661 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8662 return 0;
8663
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008664 ret = p2p_in_progress(wpa_s->global->p2p);
8665 if (ret == 0) {
8666 /*
8667 * Check whether there is an ongoing WPS provisioning step (or
8668 * other parts of group formation) on another interface since
8669 * p2p_in_progress() does not report this to avoid issues for
8670 * scans during such provisioning step.
8671 */
8672 if (wpa_s->global->p2p_group_formation &&
8673 wpa_s->global->p2p_group_formation != wpa_s) {
8674 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8675 "in group formation",
8676 wpa_s->global->p2p_group_formation->ifname);
8677 ret = 1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00008678 } else if (wpa_s->global->p2p_group_formation == wpa_s) {
8679 wpa_dbg(wpa_s, MSG_DEBUG,
8680 "P2P: Skip Extended Listen timeout and allow scans on current interface for group formation");
8681 ret = 2;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008682 }
8683 }
8684
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008685 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008686 struct os_reltime now;
8687 os_get_reltime(&now);
8688 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8689 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008690 /* Wait for the first client has expired */
8691 wpa_s->global->p2p_go_wait_client.sec = 0;
8692 } else {
8693 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8694 ret = 1;
8695 }
8696 }
8697
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008698 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008699}
8700
8701
8702void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8703 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008704{
8705 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8706 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008707 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008708 /**
8709 * Remove the network by scheduling the group formation
8710 * timeout to happen immediately. The teardown code
8711 * needs to be scheduled to run asynch later so that we
8712 * don't delete data from under ourselves unexpectedly.
8713 * Calling wpas_p2p_group_formation_timeout directly
8714 * causes a series of crashes in WPS failure scenarios.
8715 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008716 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8717 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008718 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008719 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008720 }
8721}
8722
8723
8724struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008725 const u8 *addr, const u8 *ssid,
8726 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008727{
8728 struct wpa_ssid *s;
8729 size_t i;
8730
8731 for (s = wpa_s->conf->ssid; s; s = s->next) {
8732 if (s->disabled != 2)
8733 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008734 if (ssid &&
8735 (ssid_len != s->ssid_len ||
8736 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8737 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008738 if (addr == NULL) {
8739 if (s->mode == WPAS_MODE_P2P_GO)
8740 return s;
8741 continue;
8742 }
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008743 if (ether_addr_equal(s->bssid, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008744 return s; /* peer is GO in the persistent group */
8745 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8746 continue;
8747 for (i = 0; i < s->num_p2p_clients; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008748 if (ether_addr_equal(s->p2p_client_list +
8749 i * 2 * ETH_ALEN, addr))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008750 return s; /* peer is P2P client in persistent
8751 * group */
8752 }
8753 }
8754
8755 return NULL;
8756}
8757
8758
8759void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8760 const u8 *addr)
8761{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008762 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008763 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008764 /*
8765 * This can happen if WPS provisioning step is not terminated
8766 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8767 * peer was able to connect, there is no need to time out group
8768 * formation after this, though. In addition, this is used with
8769 * the initial connection wait on the GO as a separate formation
8770 * timeout and as such, expected to be hit after the initial WPS
8771 * provisioning step.
8772 */
8773 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008774
8775 if (!wpa_s->p2p_go_group_formation_completed &&
8776 !wpa_s->group_formation_reported) {
8777 /*
8778 * GO has not yet notified group formation success since
8779 * the WPS step was not completed cleanly. Do that
8780 * notification now since the P2P Client was able to
8781 * connect and as such, must have received the
8782 * credential from the WPS step.
8783 */
8784 if (wpa_s->global->p2p)
8785 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008786 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008787 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008788 }
8789 if (!wpa_s->p2p_go_group_formation_completed) {
8790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8791 wpa_s->p2p_go_group_formation_completed = 1;
8792 wpa_s->global->p2p_group_formation = NULL;
8793 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008794 wpa_s->p2p_in_invitation = 0;
Matthew Wang06b42472022-11-10 06:56:31 +00008795 wpa_s->p2p_retry_limit = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008796 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008797 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008798 if (addr == NULL)
8799 return;
8800 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8801}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008802
Dmitry Shmidt04949592012-07-19 12:16:46 -07008803
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008804static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8805 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008806{
8807 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008808 int ret = 0;
8809
Dmitry Shmidt04949592012-07-19 12:16:46 -07008810 if (wpa_s->global->p2p_group_formation)
8811 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008812 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008813 offchannel_send_action_done(wpa_s);
8814 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008815 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008816 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8817 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8818 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8819 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008820 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008821 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008822 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008823 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008824 wpa_s->p2p_go_vht,
Hai Shalom74f70d42019-02-11 14:42:39 -08008825 wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08008826 wpa_s->p2p_go_he,
8827 wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08008828 NULL, 0, is_p2p_allow_6ghz(wpa_s->global->p2p));
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008829 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008830}
8831
8832
8833int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8834{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008835 int res;
8836
Dmitry Shmidt04949592012-07-19 12:16:46 -07008837 if (!wpa_s->p2p_fallback_to_go_neg ||
8838 wpa_s->p2p_in_provisioning <= 5)
8839 return 0;
8840
8841 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8842 return 0; /* peer operating as a GO */
8843
8844 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8845 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008846 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008847 "reason=GO-not-found");
8848 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008849
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008850 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008851}
8852
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008853
8854unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8855{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008856 struct wpa_supplicant *ifs;
8857
8858 if (wpa_s->wpa_state > WPA_SCANNING) {
8859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8860 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008861 wpa_s->conf->p2p_search_delay);
8862 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008863 }
8864
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008865 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8866 radio_list) {
8867 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8869 "delay due to concurrent operation on "
8870 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008871 wpa_s->conf->p2p_search_delay,
8872 ifs->ifname);
8873 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008874 }
8875 }
8876
8877 return 0;
8878}
8879
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008880
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008881static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8882 struct wpa_ssid *s, const u8 *addr,
8883 int iface_addr)
8884{
8885 struct psk_list_entry *psk, *tmp;
8886 int changed = 0;
8887
8888 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8889 list) {
8890 if ((iface_addr && !psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008891 ether_addr_equal(addr, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008892 (!iface_addr && psk->p2p &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00008893 ether_addr_equal(addr, psk->addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008894 wpa_dbg(wpa_s, MSG_DEBUG,
8895 "P2P: Remove persistent group PSK list entry for "
8896 MACSTR " p2p=%u",
8897 MAC2STR(psk->addr), psk->p2p);
8898 dl_list_del(&psk->list);
8899 os_free(psk);
8900 changed++;
8901 }
8902 }
8903
8904 return changed;
8905}
8906
8907
8908void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8909 const u8 *p2p_dev_addr,
8910 const u8 *psk, size_t psk_len)
8911{
8912 struct wpa_ssid *ssid = wpa_s->current_ssid;
8913 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008914 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008915
8916 if (psk_len != sizeof(p->psk))
8917 return;
8918
8919 if (p2p_dev_addr) {
8920 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8921 " p2p_dev_addr=" MACSTR,
8922 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8923 if (is_zero_ether_addr(p2p_dev_addr))
8924 p2p_dev_addr = NULL;
8925 } else {
8926 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8927 MAC2STR(mac_addr));
8928 }
8929
8930 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8931 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8932 /* To be added to persistent group once created */
8933 if (wpa_s->global->add_psk == NULL) {
8934 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8935 if (wpa_s->global->add_psk == NULL)
8936 return;
8937 }
8938 p = wpa_s->global->add_psk;
8939 if (p2p_dev_addr) {
8940 p->p2p = 1;
8941 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8942 } else {
8943 p->p2p = 0;
8944 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8945 }
8946 os_memcpy(p->psk, psk, psk_len);
8947 return;
8948 }
8949
8950 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8952 return;
8953 }
8954
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008955 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008956 ssid->ssid_len);
8957 if (!persistent) {
8958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8959 return;
8960 }
8961
8962 p = os_zalloc(sizeof(*p));
8963 if (p == NULL)
8964 return;
8965 if (p2p_dev_addr) {
8966 p->p2p = 1;
8967 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8968 } else {
8969 p->p2p = 0;
8970 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8971 }
8972 os_memcpy(p->psk, psk, psk_len);
8973
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008974 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8975 (last = dl_list_last(&persistent->psk_list,
8976 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8978 MACSTR " (p2p=%u) to make room for a new one",
8979 MAC2STR(last->addr), last->p2p);
8980 dl_list_del(&last->list);
8981 os_free(last);
8982 }
8983
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008984 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008985 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8986 p2p_dev_addr == NULL);
8987 if (p2p_dev_addr) {
8988 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8989 MACSTR, MAC2STR(p2p_dev_addr));
8990 } else {
8991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8992 MAC2STR(mac_addr));
8993 }
8994 dl_list_add(&persistent->psk_list, &p->list);
8995
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008996 if (wpa_s->p2pdev->conf->update_config &&
8997 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008998 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008999}
9000
9001
9002static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
9003 struct wpa_ssid *s, const u8 *addr,
9004 int iface_addr)
9005{
9006 int res;
9007
9008 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08009009 if (res > 0 && wpa_s->conf->update_config &&
9010 wpa_config_write(wpa_s->confname, wpa_s->conf))
9011 wpa_dbg(wpa_s, MSG_DEBUG,
9012 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009013}
9014
9015
9016static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
9017 const u8 *peer, int iface_addr)
9018{
9019 struct hostapd_data *hapd;
9020 struct hostapd_wpa_psk *psk, *prev, *rem;
9021 struct sta_info *sta;
9022
9023 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
9024 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
9025 return;
9026
9027 /* Remove per-station PSK entry */
9028 hapd = wpa_s->ap_iface->bss[0];
9029 prev = NULL;
9030 psk = hapd->conf->ssid.wpa_psk;
9031 while (psk) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009032 if ((iface_addr && ether_addr_equal(peer, psk->addr)) ||
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009033 (!iface_addr &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009034 ether_addr_equal(peer, psk->p2p_dev_addr))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
9036 MACSTR " iface_addr=%d",
9037 MAC2STR(peer), iface_addr);
9038 if (prev)
9039 prev->next = psk->next;
9040 else
9041 hapd->conf->ssid.wpa_psk = psk->next;
9042 rem = psk;
9043 psk = psk->next;
Sunil Ravia04bd252022-05-02 22:54:18 -07009044 bin_clear_free(rem, sizeof(*rem));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009045 } else {
9046 prev = psk;
9047 psk = psk->next;
9048 }
9049 }
9050
9051 /* Disconnect from group */
9052 if (iface_addr)
9053 sta = ap_get_sta(hapd, peer);
9054 else
9055 sta = ap_get_sta_p2p(hapd, peer);
9056 if (sta) {
9057 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
9058 " (iface_addr=%d) from group",
9059 MAC2STR(peer), iface_addr);
9060 hostapd_drv_sta_deauth(hapd, sta->addr,
9061 WLAN_REASON_DEAUTH_LEAVING);
9062 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
9063 }
9064}
9065
9066
9067void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
9068 int iface_addr)
9069{
9070 struct wpa_ssid *s;
9071 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009072 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009073
9074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
9075
9076 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009077 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009078 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
9079 continue;
9080 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07009081 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
9082 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009083 }
9084
9085 /* Remove from any operating group */
9086 for (w = wpa_s->global->ifaces; w; w = w->next)
9087 wpas_p2p_remove_client_go(w, peer, iface_addr);
9088}
9089
9090
9091static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
9092{
9093 struct wpa_supplicant *wpa_s = eloop_ctx;
9094 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
9095}
9096
9097
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08009098static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
9099{
9100 struct wpa_supplicant *wpa_s = eloop_ctx;
9101
9102 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
9103 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
9104}
9105
9106
9107int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
9108 struct wpa_ssid *ssid)
9109{
9110 struct wpa_supplicant *iface;
9111
9112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9113 if (!iface->current_ssid ||
9114 iface->current_ssid->frequency == freq ||
9115 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
9116 !iface->current_ssid->p2p_group))
9117 continue;
9118
9119 /* Remove the connection with least priority */
9120 if (!wpas_is_p2p_prioritized(iface)) {
9121 /* STA connection has priority over existing
9122 * P2P connection, so remove the interface. */
9123 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
9124 eloop_register_timeout(0, 0,
9125 wpas_p2p_group_freq_conflict,
9126 iface, NULL);
9127 /* If connection in progress is P2P connection, do not
9128 * proceed for the connection. */
9129 if (wpa_s == iface)
9130 return -1;
9131 else
9132 return 0;
9133 } else {
9134 /* P2P connection has priority, disable the STA network
9135 */
9136 wpa_supplicant_disable_network(wpa_s->global->ifaces,
9137 ssid);
9138 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
9139 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
9140 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
9141 ETH_ALEN);
9142 /* If P2P connection is in progress, continue
9143 * connecting...*/
9144 if (wpa_s == iface)
9145 return 0;
9146 else
9147 return -1;
9148 }
9149 }
9150
9151 return 0;
9152}
9153
9154
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009155int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
9156{
9157 struct wpa_ssid *ssid = wpa_s->current_ssid;
9158
9159 if (ssid == NULL || !ssid->p2p_group)
9160 return 0;
9161
9162 if (wpa_s->p2p_last_4way_hs_fail &&
9163 wpa_s->p2p_last_4way_hs_fail == ssid) {
9164 u8 go_dev_addr[ETH_ALEN];
9165 struct wpa_ssid *persistent;
9166
9167 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
9168 ssid->ssid,
9169 ssid->ssid_len) <= 0) {
9170 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
9171 goto disconnect;
9172 }
9173
9174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
9175 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009176 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009177 ssid->ssid,
9178 ssid->ssid_len);
9179 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
9180 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
9181 goto disconnect;
9182 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009183 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07009184 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
9185 persistent->id);
9186 disconnect:
9187 wpa_s->p2p_last_4way_hs_fail = NULL;
9188 /*
9189 * Remove the group from a timeout to avoid issues with caller
9190 * continuing to use the interface if this is on a P2P group
9191 * interface.
9192 */
9193 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
9194 wpa_s, NULL);
9195 return 1;
9196 }
9197
9198 wpa_s->p2p_last_4way_hs_fail = ssid;
9199 return 0;
9200}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009201
9202
9203#ifdef CONFIG_WPS_NFC
9204
9205static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
9206 struct wpabuf *p2p)
9207{
9208 struct wpabuf *ret;
9209 size_t wsc_len;
9210
9211 if (p2p == NULL) {
9212 wpabuf_free(wsc);
9213 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
9214 return NULL;
9215 }
9216
9217 wsc_len = wsc ? wpabuf_len(wsc) : 0;
9218 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
9219 if (ret == NULL) {
9220 wpabuf_free(wsc);
9221 wpabuf_free(p2p);
9222 return NULL;
9223 }
9224
9225 wpabuf_put_be16(ret, wsc_len);
9226 if (wsc)
9227 wpabuf_put_buf(ret, wsc);
9228 wpabuf_put_be16(ret, wpabuf_len(p2p));
9229 wpabuf_put_buf(ret, p2p);
9230
9231 wpabuf_free(wsc);
9232 wpabuf_free(p2p);
9233 wpa_hexdump_buf(MSG_DEBUG,
9234 "P2P: Generated NFC connection handover message", ret);
9235
9236 if (ndef && ret) {
9237 struct wpabuf *tmp;
9238 tmp = ndef_build_p2p(ret);
9239 wpabuf_free(ret);
9240 if (tmp == NULL) {
9241 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
9242 return NULL;
9243 }
9244 ret = tmp;
9245 }
9246
9247 return ret;
9248}
9249
9250
9251static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
9252 struct wpa_ssid **ssid, u8 *go_dev_addr)
9253{
9254 struct wpa_supplicant *iface;
9255
9256 if (go_dev_addr)
9257 os_memset(go_dev_addr, 0, ETH_ALEN);
9258 if (ssid)
9259 *ssid = NULL;
9260 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9261 if (iface->wpa_state < WPA_ASSOCIATING ||
9262 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
9263 !iface->current_ssid->p2p_group ||
9264 iface->current_ssid->mode != WPAS_MODE_INFRA)
9265 continue;
9266 if (ssid)
9267 *ssid = iface->current_ssid;
9268 if (go_dev_addr)
9269 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
9270 return iface->assoc_freq;
9271 }
9272 return 0;
9273}
9274
9275
9276struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
9277 int ndef)
9278{
9279 struct wpabuf *wsc, *p2p;
9280 struct wpa_ssid *ssid;
9281 u8 go_dev_addr[ETH_ALEN];
9282 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9283
9284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
9285 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
9286 return NULL;
9287 }
9288
9289 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9290 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9291 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
9292 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
9293 return NULL;
9294 }
9295
9296 if (cli_freq == 0) {
9297 wsc = wps_build_nfc_handover_req_p2p(
9298 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
9299 } else
9300 wsc = NULL;
9301 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
9302 go_dev_addr, ssid ? ssid->ssid : NULL,
9303 ssid ? ssid->ssid_len : 0);
9304
9305 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9306}
9307
9308
9309struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
9310 int ndef, int tag)
9311{
9312 struct wpabuf *wsc, *p2p;
9313 struct wpa_ssid *ssid;
9314 u8 go_dev_addr[ETH_ALEN];
9315 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
9316
9317 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9318 return NULL;
9319
9320 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9321 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9322 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9323 return NULL;
9324
9325 if (cli_freq == 0) {
9326 wsc = wps_build_nfc_handover_sel_p2p(
9327 wpa_s->parent->wps,
9328 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
9329 DEV_PW_NFC_CONNECTION_HANDOVER,
9330 wpa_s->conf->wps_nfc_dh_pubkey,
9331 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
9332 } else
9333 wsc = NULL;
9334 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
9335 go_dev_addr, ssid ? ssid->ssid : NULL,
9336 ssid ? ssid->ssid_len : 0);
9337
9338 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
9339}
9340
9341
9342static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
9343 struct p2p_nfc_params *params)
9344{
9345 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
9346 "connection handover (freq=%d)",
9347 params->go_freq);
9348
9349 if (params->go_freq && params->go_ssid_len) {
9350 wpa_s->p2p_wps_method = WPS_NFC;
9351 wpa_s->pending_join_wps_method = WPS_NFC;
9352 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
9353 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
9354 ETH_ALEN);
9355 return wpas_p2p_join_start(wpa_s, params->go_freq,
9356 params->go_ssid,
9357 params->go_ssid_len);
9358 }
9359
9360 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9361 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009362 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009363 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009364 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009365 params->go_ssid_len ? params->go_ssid : NULL,
Hai Shalomc1a21442022-02-04 13:43:00 -08009366 params->go_ssid_len, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009367}
9368
9369
9370static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
9371 struct p2p_nfc_params *params, int tag)
9372{
9373 int res, persistent;
9374 struct wpa_ssid *ssid;
9375
9376 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
9377 "connection handover");
9378 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
9379 ssid = wpa_s->current_ssid;
9380 if (ssid == NULL)
9381 continue;
9382 if (ssid->mode != WPAS_MODE_P2P_GO)
9383 continue;
9384 if (wpa_s->ap_iface == NULL)
9385 continue;
9386 break;
9387 }
9388 if (wpa_s == NULL) {
9389 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
9390 return -1;
9391 }
9392
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009393 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009394 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009395 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009396 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
9397 return -1;
9398 }
9399 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009400 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
9401 wpa_s->p2pdev->p2p_oob_dev_pw,
9402 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
9403 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009404 if (res)
9405 return res;
9406
9407 if (!tag) {
9408 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
9409 return 0;
9410 }
9411
9412 if (!params->peer ||
9413 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
9414 return 0;
9415
9416 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
9417 " to join", MAC2STR(params->peer->p2p_device_addr));
9418
9419 wpa_s->global->p2p_invite_group = wpa_s;
9420 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009421 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009422 params->peer->p2p_device_addr,
9423 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009424 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009425
9426 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
9427 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
9428 ssid->ssid, ssid->ssid_len, ssid->frequency,
9429 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009430 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009431}
9432
9433
9434static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
9435 struct p2p_nfc_params *params,
9436 int forced_freq)
9437{
9438 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
9439 "connection handover");
9440 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9441 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009442 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009443 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009444 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009445 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009446}
9447
9448
9449static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
9450 struct p2p_nfc_params *params,
9451 int forced_freq)
9452{
9453 int res;
9454
9455 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
9456 "connection handover");
9457 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
9458 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009459 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08009460 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009461 wpa_s->p2p_go_he, wpa_s->p2p_go_edmg,
Hai Shalomc1a21442022-02-04 13:43:00 -08009462 NULL, 0, false);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009463 if (res)
9464 return res;
9465
9466 res = wpas_p2p_listen(wpa_s, 60);
9467 if (res) {
9468 p2p_unauthorize(wpa_s->global->p2p,
9469 params->peer->p2p_device_addr);
9470 }
9471
9472 return res;
9473}
9474
9475
9476static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
9477 const struct wpabuf *data,
9478 int sel, int tag, int forced_freq)
9479{
9480 const u8 *pos, *end;
9481 u16 len, id;
9482 struct p2p_nfc_params params;
9483 int res;
9484
9485 os_memset(&params, 0, sizeof(params));
9486 params.sel = sel;
9487
9488 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
9489
9490 pos = wpabuf_head(data);
9491 end = pos + wpabuf_len(data);
9492
9493 if (end - pos < 2) {
9494 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
9495 "attributes");
9496 return -1;
9497 }
9498 len = WPA_GET_BE16(pos);
9499 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009500 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009501 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
9502 "attributes");
9503 return -1;
9504 }
9505 params.wsc_attr = pos;
9506 params.wsc_len = len;
9507 pos += len;
9508
9509 if (end - pos < 2) {
9510 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
9511 "attributes");
9512 return -1;
9513 }
9514 len = WPA_GET_BE16(pos);
9515 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009516 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009517 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
9518 "attributes");
9519 return -1;
9520 }
9521 params.p2p_attr = pos;
9522 params.p2p_len = len;
9523 pos += len;
9524
9525 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
9526 params.wsc_attr, params.wsc_len);
9527 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
9528 params.p2p_attr, params.p2p_len);
9529 if (pos < end) {
9530 wpa_hexdump(MSG_DEBUG,
9531 "P2P: Ignored extra data after P2P attributes",
9532 pos, end - pos);
9533 }
9534
9535 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
9536 if (res)
9537 return res;
9538
9539 if (params.next_step == NO_ACTION)
9540 return 0;
9541
9542 if (params.next_step == BOTH_GO) {
9543 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
9544 MAC2STR(params.peer->p2p_device_addr));
9545 return 0;
9546 }
9547
9548 if (params.next_step == PEER_CLIENT) {
9549 if (!is_zero_ether_addr(params.go_dev_addr)) {
9550 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9551 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
9552 " ssid=\"%s\"",
9553 MAC2STR(params.peer->p2p_device_addr),
9554 params.go_freq,
9555 MAC2STR(params.go_dev_addr),
9556 wpa_ssid_txt(params.go_ssid,
9557 params.go_ssid_len));
9558 } else {
9559 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
9560 "peer=" MACSTR " freq=%d",
9561 MAC2STR(params.peer->p2p_device_addr),
9562 params.go_freq);
9563 }
9564 return 0;
9565 }
9566
9567 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
9568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
9569 MACSTR, MAC2STR(params.peer->p2p_device_addr));
9570 return 0;
9571 }
9572
9573 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9574 wpa_s->p2p_oob_dev_pw = NULL;
9575
9576 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
9577 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
9578 "received");
9579 return -1;
9580 }
9581
9582 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
9583 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
9584 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
9585 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9586 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
9587 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
9588 wpa_s->p2p_peer_oob_pk_hash_known = 1;
9589
9590 if (tag) {
9591 if (id < 0x10) {
9592 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
9593 "peer OOB Device Password Id %u", id);
9594 return -1;
9595 }
9596 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
9597 "Device Password Id %u", id);
9598 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
9599 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9600 params.oob_dev_pw_len -
9601 WPS_OOB_PUBKEY_HASH_LEN - 2);
9602 wpa_s->p2p_oob_dev_pw_id = id;
9603 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
9604 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
9605 params.oob_dev_pw_len -
9606 WPS_OOB_PUBKEY_HASH_LEN - 2);
9607 if (wpa_s->p2p_oob_dev_pw == NULL)
9608 return -1;
9609
9610 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
9611 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
9612 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
9613 return -1;
9614 } else {
9615 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
9616 "without Device Password");
9617 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
9618 }
9619
9620 switch (params.next_step) {
9621 case NO_ACTION:
9622 case BOTH_GO:
9623 case PEER_CLIENT:
9624 /* already covered above */
9625 return 0;
9626 case JOIN_GROUP:
9627 return wpas_p2p_nfc_join_group(wpa_s, &params);
9628 case AUTH_JOIN:
9629 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9630 case INIT_GO_NEG:
9631 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9632 case RESP_GO_NEG:
9633 /* TODO: use own OOB Dev Pw */
9634 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9635 }
9636
9637 return -1;
9638}
9639
9640
9641int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9642 const struct wpabuf *data, int forced_freq)
9643{
9644 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9645 return -1;
9646
9647 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9648}
9649
9650
9651int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9652 const struct wpabuf *req,
9653 const struct wpabuf *sel, int forced_freq)
9654{
9655 struct wpabuf *tmp;
9656 int ret;
9657
9658 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9659 return -1;
9660
9661 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9662
9663 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9664 wpabuf_head(req), wpabuf_len(req));
9665 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9666 wpabuf_head(sel), wpabuf_len(sel));
9667 if (forced_freq)
9668 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9669 tmp = ndef_parse_p2p(init ? sel : req);
9670 if (tmp == NULL) {
9671 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9672 return -1;
9673 }
9674
9675 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9676 forced_freq);
9677 wpabuf_free(tmp);
9678
9679 return ret;
9680}
9681
9682
9683int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9684{
9685 const u8 *if_addr;
9686 int go_intent = wpa_s->conf->p2p_go_intent;
9687 struct wpa_supplicant *iface;
9688
9689 if (wpa_s->global->p2p == NULL)
9690 return -1;
9691
9692 if (!enabled) {
9693 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9694 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9695 {
9696 if (!iface->ap_iface)
9697 continue;
9698 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9699 }
9700 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9701 0, NULL);
9702 if (wpa_s->p2p_nfc_tag_enabled)
9703 wpas_p2p_remove_pending_group_interface(wpa_s);
9704 wpa_s->p2p_nfc_tag_enabled = 0;
9705 return 0;
9706 }
9707
9708 if (wpa_s->global->p2p_disabled)
9709 return -1;
9710
9711 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9712 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9713 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9714 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9715 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9716 "to allow static handover cases");
9717 return -1;
9718 }
9719
9720 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9721
9722 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9723 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9724 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9725 if (wpa_s->p2p_oob_dev_pw == NULL)
9726 return -1;
9727 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9728
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009729 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9730 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9731 /*
9732 * P2P Group Interface present and the command came on group
9733 * interface, so enable the token for the current interface.
9734 */
9735 wpa_s->create_p2p_iface = 0;
9736 } else {
9737 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9738 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009739
9740 if (wpa_s->create_p2p_iface) {
9741 enum wpa_driver_if_type iftype;
9742 /* Prepare to add a new interface for the group */
9743 iftype = WPA_IF_P2P_GROUP;
9744 if (go_intent == 15)
9745 iftype = WPA_IF_P2P_GO;
9746 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9747 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9748 "interface for the group");
9749 return -1;
9750 }
9751
9752 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08009753 } else if (wpa_s->p2p_mgmt)
9754 if_addr = wpa_s->parent->own_addr;
9755 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009756 if_addr = wpa_s->own_addr;
9757
9758 wpa_s->p2p_nfc_tag_enabled = enabled;
9759
9760 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9761 struct hostapd_data *hapd;
9762 if (iface->ap_iface == NULL)
9763 continue;
9764 hapd = iface->ap_iface->bss[0];
9765 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9766 hapd->conf->wps_nfc_dh_pubkey =
9767 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9768 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9769 hapd->conf->wps_nfc_dh_privkey =
9770 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9771 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9772 hapd->conf->wps_nfc_dev_pw =
9773 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9774 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9775
9776 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9777 wpa_dbg(iface, MSG_DEBUG,
9778 "P2P: Failed to enable NFC Tag for GO");
9779 }
9780 }
9781 p2p_set_authorized_oob_dev_pw_id(
9782 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9783 if_addr);
9784
9785 return 0;
9786}
9787
9788#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009789
9790
9791static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9792 struct wpa_used_freq_data *freqs,
9793 unsigned int num)
9794{
9795 u8 curr_chan, cand, chan;
9796 unsigned int i;
9797
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009798 /*
9799 * If possible, optimize the Listen channel to be a channel that is
9800 * already used by one of the other interfaces.
9801 */
9802 if (!wpa_s->conf->p2p_optimize_listen_chan)
9803 return;
9804
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009805 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9806 for (i = 0, cand = 0; i < num; i++) {
9807 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9808 if (curr_chan == chan) {
9809 cand = 0;
9810 break;
9811 }
9812
9813 if (chan == 1 || chan == 6 || chan == 11)
9814 cand = chan;
9815 }
9816
9817 if (cand) {
9818 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009819 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009820 cand);
9821 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9822 }
9823}
9824
9825
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009826static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009827{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009828 struct hostapd_config *conf;
9829 struct p2p_go_neg_results params;
9830 struct csa_settings csa_settings;
9831 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9832 int old_freq = current_ssid->frequency;
9833 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009834
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009835 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9836 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9837 return -1;
9838 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009839
9840 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009841 * TODO: This function may not always work correctly. For example,
9842 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009843 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009844 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9845 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009846 wpa_dbg(wpa_s, MSG_DEBUG,
9847 "P2P CSA: Failed to select new frequency for GO");
9848 return -1;
9849 }
9850
9851 if (current_ssid->frequency == params.freq) {
9852 wpa_dbg(wpa_s, MSG_DEBUG,
9853 "P2P CSA: Selected same frequency - not moving GO");
9854 return 0;
9855 }
9856
9857 conf = hostapd_config_defaults();
9858 if (!conf) {
9859 wpa_dbg(wpa_s, MSG_DEBUG,
9860 "P2P CSA: Failed to allocate default config");
9861 return -1;
9862 }
9863
9864 current_ssid->frequency = params.freq;
9865 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9866 wpa_dbg(wpa_s, MSG_DEBUG,
9867 "P2P CSA: Failed to create new GO config");
9868 ret = -1;
9869 goto out;
9870 }
9871
Sunil4a3f9f52022-07-19 22:04:39 +00009872 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode &&
9873 (wpa_s->ap_iface->current_mode->mode != HOSTAPD_MODE_IEEE80211A ||
Sunil Ravi89eba102022-09-13 21:04:37 -07009874 is_6ghz_freq(wpa_s->ap_iface->freq) ||
Sunil4a3f9f52022-07-19 22:04:39 +00009875 conf->hw_mode != HOSTAPD_MODE_IEEE80211G)) {
9876 wpa_dbg(wpa_s, MSG_INFO,
Sunil Ravi89eba102022-09-13 21:04:37 -07009877 "P2P CSA: CSA from hardware mode %d%s to %d is not supported",
9878 wpa_s->ap_iface->current_mode->mode,
9879 is_6ghz_freq(wpa_s->ap_iface->freq) ? " (6 GHz)" : "",
9880 conf->hw_mode);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009881 ret = -1;
9882 goto out;
9883 }
9884
9885 os_memset(&csa_settings, 0, sizeof(csa_settings));
9886 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9887 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00009888 csa_settings.link_id = -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009889 csa_settings.freq_params.freq = params.freq;
9890 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9891 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9892 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9893
9894 if (conf->ieee80211ac) {
9895 int freq1 = 0, freq2 = 0;
9896 u8 chan, opclass;
9897
9898 if (ieee80211_freq_to_channel_ext(params.freq,
9899 conf->secondary_channel,
9900 conf->vht_oper_chwidth,
9901 &opclass, &chan) ==
9902 NUM_HOSTAPD_MODES) {
9903 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9904 ret = -1;
9905 goto out;
9906 }
9907
9908 if (conf->vht_oper_centr_freq_seg0_idx)
9909 freq1 = ieee80211_chan_to_freq(
9910 NULL, opclass,
9911 conf->vht_oper_centr_freq_seg0_idx);
9912
9913 if (conf->vht_oper_centr_freq_seg1_idx)
9914 freq2 = ieee80211_chan_to_freq(
9915 NULL, opclass,
9916 conf->vht_oper_centr_freq_seg1_idx);
9917
9918 if (freq1 < 0 || freq2 < 0) {
9919 wpa_dbg(wpa_s, MSG_DEBUG,
9920 "P2P CSA: Selected invalid VHT center freqs");
9921 ret = -1;
9922 goto out;
9923 }
9924
9925 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9926 csa_settings.freq_params.center_freq1 = freq1;
9927 csa_settings.freq_params.center_freq2 = freq2;
9928
9929 switch (conf->vht_oper_chwidth) {
Sunil8cd6f4d2022-06-28 18:40:46 +00009930 case CONF_OPER_CHWIDTH_80MHZ:
9931 case CONF_OPER_CHWIDTH_80P80MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009932 csa_settings.freq_params.bandwidth = 80;
9933 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009934 case CONF_OPER_CHWIDTH_160MHZ:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009935 csa_settings.freq_params.bandwidth = 160;
9936 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00009937 default:
9938 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009939 }
9940 }
9941
9942 ret = ap_switch_channel(wpa_s, &csa_settings);
9943out:
9944 current_ssid->frequency = old_freq;
9945 hostapd_config_free(conf);
9946 return ret;
9947}
9948
9949
9950static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9951{
9952 struct p2p_go_neg_results params;
9953 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
Hai Shalom899fcc72020-10-19 14:38:18 -07009954 void (*ap_configured_cb)(void *ctx, void *data);
9955 void *ap_configured_cb_ctx, *ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009956
9957 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9958
9959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9960 current_ssid->frequency);
9961
9962 /* Stop the AP functionality */
9963 /* TODO: Should do this in a way that does not indicated to possible
9964 * P2P Clients in the group that the group is terminated. */
Hai Shalom899fcc72020-10-19 14:38:18 -07009965 /* If this action occurs before a group is started, the callback should
9966 * be preserved, or GROUP-STARTED event would be lost. If this action
9967 * occurs after a group is started, these pointers are all NULL and
9968 * harmless. */
9969 ap_configured_cb = wpa_s->ap_configured_cb;
9970 ap_configured_cb_ctx = wpa_s->ap_configured_cb_ctx;
9971 ap_configured_cb_data = wpa_s->ap_configured_cb_data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009972 wpa_supplicant_ap_deinit(wpa_s);
9973
9974 /* Reselect the GO frequency */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08009975 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, 0, 0,
9976 NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9978 wpas_p2p_group_delete(wpa_s,
9979 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9980 return;
9981 }
9982 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9983 params.freq);
9984
9985 if (params.freq &&
9986 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9987 wpa_printf(MSG_DEBUG,
9988 "P2P: Selected freq (%u MHz) is not valid for P2P",
9989 params.freq);
9990 wpas_p2p_group_delete(wpa_s,
9991 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9992 return;
9993 }
9994
Hai Shalom899fcc72020-10-19 14:38:18 -07009995 /* Restore preserved callback parameters */
Jimmy Chen4b2a5022020-08-26 18:55:45 +08009996 wpa_s->ap_configured_cb = ap_configured_cb;
9997 wpa_s->ap_configured_cb_ctx = ap_configured_cb_ctx;
9998 wpa_s->ap_configured_cb_data = ap_configured_cb_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07009999
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010000 /* Update the frequency */
10001 current_ssid->frequency = params.freq;
10002 wpa_s->connect_without_scan = current_ssid;
10003 wpa_s->reassociate = 1;
10004 wpa_s->disconnected = 0;
10005 wpa_supplicant_req_scan(wpa_s, 0, 0);
10006}
10007
10008
10009static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
10010{
10011 struct wpa_supplicant *wpa_s = eloop_ctx;
10012
10013 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010014 return;
10015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010016 wpas_p2p_go_update_common_freqs(wpa_s);
10017
10018 /* Do not move GO in the middle of a CSA */
10019 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10020 wpa_printf(MSG_DEBUG,
10021 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010022 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010023 }
10024
10025 /*
10026 * First, try a channel switch flow. If it is not supported or fails,
10027 * take down the GO and bring it up again.
10028 */
10029 if (wpas_p2p_move_go_csa(wpa_s) < 0)
10030 wpas_p2p_move_go_no_csa(wpa_s);
10031}
10032
10033
10034static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
10035{
10036 struct wpa_supplicant *wpa_s = eloop_ctx;
10037 struct wpa_used_freq_data *freqs = NULL;
10038 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010039
10040 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
10041 if (!freqs)
10042 return;
10043
Sunil Ravi77d572f2023-01-17 23:58:31 +000010044 num = get_shared_radio_freqs_data(wpa_s, freqs, num, false);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010045
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010046 /* Previous attempt to move a GO was not possible -- try again. */
10047 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
10048 WPAS_P2P_CHANNEL_UPDATE_ANY);
10049
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010050 os_free(freqs);
10051}
10052
10053
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010054/*
10055 * Consider moving a GO from its currently used frequency:
10056 * 1. It is possible that due to regulatory consideration the frequency
10057 * can no longer be used and there is a need to evacuate the GO.
10058 * 2. It is possible that due to MCC considerations, it would be preferable
10059 * to move the GO to a channel that is currently used by some other
10060 * station interface.
10061 *
10062 * In case a frequency that became invalid is once again valid, cancel a
10063 * previously initiated GO frequency change.
10064 */
10065static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
10066 struct wpa_used_freq_data *freqs,
10067 unsigned int num)
10068{
10069 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
10070 unsigned int timeout;
10071 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010072 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010073
10074 wpas_p2p_go_update_common_freqs(wpa_s);
10075
10076 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010077 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070010078 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010079 for (i = 0, invalid_freq = 0; i < num; i++) {
10080 if (freqs[i].freq == freq) {
10081 flags = freqs[i].flags;
10082
10083 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010084 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
10085 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010086 wpa_dbg(wpa_s, MSG_DEBUG,
10087 "P2P: Freq=%d MHz no longer valid for GO",
10088 freq);
10089 invalid_freq = 1;
10090 }
10091 } else if (freqs[i].flags == 0) {
10092 /* Freq is not used by any other station interface */
10093 continue;
10094 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070010095 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080010096 /* Freq is not valid for P2P use cases */
10097 continue;
10098 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10099 P2P_GO_FREQ_MOVE_SCM) {
10100 policy_move = 1;
10101 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
10102 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
10103 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10104 policy_move = 1;
10105 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
10106 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
10107 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
10108 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
10109 policy_move = 1;
10110 } else if ((wpa_s->drv_flags &
10111 WPA_DRIVER_FLAGS_AP_CSA) &&
10112 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
10113 u8 chan;
10114
10115 /*
10116 * We do not support CSA between bands, so move
10117 * GO only within the same band.
10118 */
10119 if (wpa_s->ap_iface->current_mode->mode ==
10120 ieee80211_freq_to_chan(freqs[i].freq,
10121 &chan))
10122 policy_move = 1;
10123 }
10124 }
10125 }
10126
10127 wpa_dbg(wpa_s, MSG_DEBUG,
10128 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
10129 invalid_freq, policy_move, flags);
10130
10131 /*
10132 * The channel is valid, or we are going to have a policy move, so
10133 * cancel timeout.
10134 */
10135 if (!invalid_freq || policy_move) {
10136 wpa_dbg(wpa_s, MSG_DEBUG,
10137 "P2P: Cancel a GO move from freq=%d MHz", freq);
10138 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10139
10140 if (wpas_p2p_in_progress(wpa_s)) {
10141 wpa_dbg(wpa_s, MSG_DEBUG,
10142 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
10143 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
10144 wpa_s, NULL);
10145 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10146 wpas_p2p_reconsider_moving_go,
10147 wpa_s, NULL);
10148 return;
10149 }
10150 }
10151
10152 if (!invalid_freq && (!policy_move || flags != 0)) {
10153 wpa_dbg(wpa_s, MSG_DEBUG,
10154 "P2P: Not initiating a GO frequency change");
10155 return;
10156 }
10157
10158 /*
10159 * Do not consider moving GO if it is in the middle of a CSA. When the
10160 * CSA is finished this flow should be retriggered.
10161 */
10162 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
10163 wpa_dbg(wpa_s, MSG_DEBUG,
10164 "P2P: Not initiating a GO frequency change - CSA is in progress");
10165 return;
10166 }
10167
10168 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
10169 timeout = P2P_GO_FREQ_CHANGE_TIME;
10170 else
10171 timeout = 0;
10172
10173 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
10174 freq, timeout);
10175 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
10176 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
10177}
10178
10179
10180static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
10181 struct wpa_used_freq_data *freqs,
10182 unsigned int num,
10183 enum wpas_p2p_channel_update_trig trig)
10184{
10185 struct wpa_supplicant *ifs;
10186
10187 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
10188 NULL);
10189
10190 /*
10191 * Travers all the radio interfaces, and for each GO interface, check
10192 * if there is a need to move the GO from the frequency it is using,
10193 * or in case the frequency is valid again, cancel the evacuation flow.
10194 */
10195 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
10196 radio_list) {
10197 if (ifs->current_ssid == NULL ||
10198 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
10199 continue;
10200
10201 /*
10202 * The GO was just started or completed channel switch, no need
10203 * to move it.
10204 */
10205 if (wpa_s == ifs &&
10206 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
10207 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
10208 wpa_dbg(wpa_s, MSG_DEBUG,
10209 "P2P: GO move - schedule re-consideration");
10210 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
10211 wpas_p2p_reconsider_moving_go,
10212 wpa_s, NULL);
10213 continue;
10214 }
10215
10216 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
10217 }
10218}
10219
10220
10221void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
10222{
10223 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
10224 return;
10225
10226 wpas_p2p_update_channel_list(wpa_s,
10227 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
10228}
10229
10230
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010231void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
10232{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -070010233 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
10234 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
10235 "the management interface is being removed");
10236 wpas_p2p_deinit_global(wpa_s->global);
10237 }
10238}
10239
10240
10241void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
10242{
10243 if (wpa_s->ap_iface->bss)
10244 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
10245 wpas_p2p_group_deinit(wpa_s);
10246}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -070010247
10248
10249int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
10250 unsigned int period, unsigned int interval,
10251 unsigned int count)
10252{
10253 struct p2p_data *p2p = wpa_s->global->p2p;
10254 u8 *device_types;
10255 size_t dev_types_len;
10256 struct wpabuf *buf;
10257 int ret;
10258
10259 if (wpa_s->p2p_lo_started) {
10260 wpa_dbg(wpa_s, MSG_DEBUG,
10261 "P2P Listen offload is already started");
10262 return 0;
10263 }
10264
10265 if (wpa_s->global->p2p == NULL ||
10266 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
10267 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
10268 return -1;
10269 }
10270
10271 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
10272 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
10273 freq);
10274 return -1;
10275 }
10276
10277 /* Get device type */
10278 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
10279 WPS_DEV_TYPE_LEN;
10280 device_types = os_malloc(dev_types_len);
10281 if (!device_types)
10282 return -1;
10283 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
10284 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
10285 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
10286
10287 /* Get Probe Response IE(s) */
10288 buf = p2p_build_probe_resp_template(p2p, freq);
10289 if (!buf) {
10290 os_free(device_types);
10291 return -1;
10292 }
10293
10294 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
10295 device_types, dev_types_len,
10296 wpabuf_mhead_u8(buf), wpabuf_len(buf));
10297 if (ret < 0)
10298 wpa_dbg(wpa_s, MSG_DEBUG,
10299 "P2P: Failed to start P2P listen offload");
10300
10301 os_free(device_types);
10302 wpabuf_free(buf);
10303
10304 if (ret == 0) {
10305 wpa_s->p2p_lo_started = 1;
10306
10307 /* Stop current P2P listen if any */
10308 wpas_stop_listen(wpa_s);
10309 }
10310
10311 return ret;
10312}
10313
10314
10315int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
10316{
10317 int ret;
10318
10319 if (!wpa_s->p2p_lo_started)
10320 return 0;
10321
10322 ret = wpa_drv_p2p_lo_stop(wpa_s);
10323 if (ret < 0)
10324 wpa_dbg(wpa_s, MSG_DEBUG,
10325 "P2P: Failed to stop P2P listen offload");
10326
10327 wpa_s->p2p_lo_started = 0;
10328 return ret;
10329}